:root {
  color-scheme: light dark;
  --bg: #f5eedf;
  --bg-2: #ebe0c9;
  --panel: rgba(255, 252, 244, 0.78);
  --panel-solid: #fffcf4;
  --ink: #2a2236;
  --muted: #62566d; /* 5.2:1 on --bg-2, the darkest page colour */
  --line: rgba(42, 34, 54, 0.12);
  --accent: #f4c430;
  --accent-deep: #c9970c;
  --accent-ink: #2a1d00;
  --royal: #6b3fa0;
  --gold: #e9a91c;
  --gold-dim: rgba(42, 34, 54, 0.15);
  --good: #2f8a4e;
  --bad: #d23c45;
  --bad-soft: rgba(210, 60, 69, 0.22);
  --dim: rgba(245, 238, 223, 0.62);
  --radius: 18px;
  --display: Fraunces, Georgia, 'Times New Roman', serif;
  --display-weight: 800; /* the weight hub/game.css gives display text */
  --heading: var(--ink); /* the gold accent is too light for headings on a light dialog */
  --text: 'DM Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;

  /* the map */
  --p0: #f4aebd;
  --p1: #f8c48a;
  --p2: #f1e08b;
  --p3: #bddba0;
  --p4: #8fd6c9;
  --p5: #a7cbf2;
  --p6: #c8b7f0;
  --p7: #dcc6a4;
  --p8: #e9ade0;
  --p9: #b9c3d1;
  --pat: rgba(42, 34, 54, 0.13);
  --grid: rgba(42, 34, 54, 0.2);
  --edge: #2a2236;
  --x: rgba(42, 34, 54, 0.62);
  --x-auto: rgba(42, 34, 54, 0.3);
  --crown-line: #5a3a06;
  --shadow: rgba(70, 45, 20, 0.22);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15131d;
    --bg-2: #1d1a28;
    --panel: rgba(38, 34, 52, 0.75);
    --panel-solid: #24202f;
    --ink: #f1ebfa;
    --muted: #aea6bd;
    --line: rgba(236, 226, 255, 0.11);
    --accent: #f6c945;
    --accent-deep: #b98c10;
    --royal: #b892ec;
    --gold: #f6c945;
    --gold-dim: rgba(236, 226, 255, 0.17);
    --good: #5ccf85;
    --bad: #ff5d68;
    --bad-soft: rgba(255, 93, 104, 0.28);
    --dim: rgba(21, 19, 29, 0.62);
    --p0: #a34d63;
    --p1: #a8692b;
    --p2: #a08f2c;
    --p3: #4f8a42;
    --p4: #278a7c;
    --p5: #3a72ad;
    --p6: #6a55b3;
    --p7: #87714b;
    --p8: #974d8e;
    --p9: #5a6679;
    --pat: rgba(255, 255, 255, 0.11);
    --grid: rgba(10, 6, 18, 0.42);
    --edge: #0c0a12;
    --x: rgba(255, 255, 255, 0.78);
    --x-auto: rgba(255, 255, 255, 0.36);
    --crown-line: #3f2700;
    --shadow: rgba(0, 0, 0, 0.5);
  }
}
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  font-family: var(--text);
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(110% 55% at 50% -12%, rgba(255, 255, 255, 0.8), transparent 62%),
    linear-gradient(180deg, var(--bg), var(--bg-2));
  background-attachment: fixed;
}
@media (prefers-color-scheme: dark) {
  body {
    background:
      radial-gradient(70% 40% at 18% -8%, rgba(184, 146, 236, 0.16), transparent 70%),
      radial-gradient(60% 34% at 88% -10%, rgba(246, 201, 69, 0.1), transparent 70%),
      linear-gradient(180deg, var(--bg), var(--bg-2));
    background-attachment: fixed;
  }
}
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.cr-body { fill: url(#crownGold); stroke: var(--crown-line); stroke-width: 3.4; stroke-linejoin: round; }
.cr-tip { fill: #ffe680; stroke: var(--crown-line); stroke-width: 3; }
.cr-gem { fill: #d7334a; stroke: var(--crown-line); stroke-width: 2.4; }
.cr-glint { fill: none; stroke: rgba(255, 255, 255, 0.75); stroke-width: 3; stroke-linecap: round; }
.app {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---------------------------------------------------------------- header */
.logo {
  margin: 0;
  font-family: var(--display);
  font-weight: 800;
  font-size: 2.1rem;
  line-height: 1;
  letter-spacing: 0.01em;
  color: var(--ink);
}
.logo::first-letter { color: var(--royal); }
.day { margin: 4px 0 0; font-size: 0.8rem; font-weight: 700; color: var(--muted); letter-spacing: 0.02em; }
.icon-btn:disabled { opacity: 0.4; cursor: default; transform: none; background: var(--panel); }
.icon-btn[hidden] { display: none; }

/* ---------------------------------------------------------------- HUD */
.hud { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 42px; }
.hud-day { display: flex; flex-direction: column; line-height: 1.15; }
.hud-day b { font-family: var(--display); font-weight: 800; font-size: 1.2rem; }
.hud-day span { font-size: 0.8rem; font-weight: 700; color: var(--muted); letter-spacing: 0.02em; }
.counts { display: flex; align-items: center; gap: 16px; }
.count { display: flex; flex-direction: column; align-items: flex-end; line-height: 1; }
.count b { font-family: var(--display); font-weight: 600; font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.count small { font-size: 0.66rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); margin-top: 3px; }
#timer { min-width: 3.1em; text-align: right; transition: color 0.4s ease; }
#timer.over { color: var(--muted); }

/* ---------------------------------------------------------------- board */
.stage { position: relative; display: flex; justify-content: center; }
.board {
  --size: 340px;
  position: relative;
  width: var(--size);
  height: var(--size);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
  border-radius: 14px;
}
.board:focus-visible { box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--royal); }
.map { width: 100%; height: 100%; overflow: visible; }
.board > .map { position: absolute; inset: 0; filter: drop-shadow(0 10px 22px var(--shadow)); }
.map .pf { transition: filter 0.6s ease; }
.pf.p0 { fill: var(--p0); } .pf.p1 { fill: var(--p1); } .pf.p2 { fill: var(--p2); } .pf.p3 { fill: var(--p3); } .pf.p4 { fill: var(--p4); }
.pf.p5 { fill: var(--p5); } .pf.p6 { fill: var(--p6); } .pf.p7 { fill: var(--p7); } .pf.p8 { fill: var(--p8); } .pf.p9 { fill: var(--p9); }
.map pattern * { fill: var(--pat); stroke: none; }
.map pattern [fill='none'] { fill: none; stroke: var(--pat); }
.map .gl { fill: none; stroke: var(--grid); stroke-width: 1.6; }
.map .edge { fill: none; stroke: var(--edge); stroke-width: 7; stroke-linecap: round; }
.map .frame { fill: none; stroke: var(--edge); stroke-width: 9; }
.cells {
  position: absolute;
  inset: var(--inset, 1.5%);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  cursor: pointer;
}
.row { display: contents; }
.cell { position: relative; display: grid; place-items: center; }
.cell::before { content: ''; position: absolute; inset: 7%; border-radius: 22%; pointer-events: none; }
.cell .xm, .cell .cw { display: none; grid-area: 1 / 1; }
.xm { position: relative; width: 34%; aspect-ratio: 1; color: var(--x); }
.xm::before, .xm::after {
  content: '';
  position: absolute;
  left: -14%; top: 42%;
  width: 128%; height: 16%;
  border-radius: 99px;
  background: currentColor;
  transform: rotate(45deg);
}
.xm::after { transform: rotate(-45deg); }
.cell.x .xm { display: block; animation: xin 0.16s cubic-bezier(0.2, 1.6, 0.4, 1); }
.cell.ax .xm { display: block; width: 24%; color: var(--x-auto); animation: axin 0.3s ease var(--d, 0ms) both; }
@keyframes xin { from { transform: scale(0.3); opacity: 0; } }
@keyframes axin { from { transform: scale(0); opacity: 0; } }
.cell.cr .cw {
  display: block;
  width: 76%; height: 76%;
  filter: drop-shadow(0 2px 0 rgba(60, 30, 0, 0.25));
  animation: crownin 0.5s cubic-bezier(0.2, 1.5, 0.35, 1) both;
}
@keyframes crownin {
  0% { transform: translateY(-30%) scale(0.2) rotate(-18deg); opacity: 0; }
  55% { transform: translateY(4%) scale(1.12) rotate(4deg); opacity: 1; }
}
.cell.cr::after {
  content: '';
  position: absolute;
  inset: 10%;
  border-radius: 50%;
  border: 3px solid var(--gold);
  pointer-events: none;
  animation: burst 0.55s ease-out both;
}
@keyframes burst { from { transform: scale(0.4); opacity: 0.9; } to { transform: scale(1.5); opacity: 0; } }
/* clashes, dead lines, hints, keyboard cursor */
.cell.dead::before { background: repeating-linear-gradient(45deg, var(--bad-soft) 0 3px, transparent 3px 8px); }
.board.spot .cell:not(.hl):not(.tgt)::before { inset: 0; border-radius: 0; background: var(--dim); box-shadow: none; animation: dimin 0.35s ease both; }
@keyframes dimin { from { opacity: 0; } }
.cell.tgt::before { box-shadow: inset 0 0 0 3px var(--royal); animation: ring 0.9s ease-in-out 3; }
@keyframes ring { 50% { box-shadow: inset 0 0 0 5px var(--royal); } }
.cell.bad::before { background: var(--bad-soft); box-shadow: inset 0 0 0 2.5px var(--bad); animation: glow 1.1s ease-in-out infinite alternate; }
.cell.cr.bad .cw { animation: shake 0.45s ease both; filter: drop-shadow(0 0 6px var(--bad)); }
@keyframes glow { to { box-shadow: inset 0 0 0 2.5px var(--bad), 0 0 14px var(--bad); } }
@keyframes shake { 20% { transform: rotate(-12deg); } 45% { transform: rotate(10deg); } 70% { transform: rotate(-6deg); } }
.board.kbd .cell.cur { outline: 3px solid var(--royal); outline-offset: -3px; border-radius: 6px; z-index: 1; }
/* the win */
.board.won .cells { cursor: default; }
.board.won .pf { filter: saturate(1.25) brightness(1.04); }
.board.won .cell.cr .cw { animation: crownwin 0.7s cubic-bezier(0.2, 1.5, 0.35, 1) var(--w, 0ms) both; filter: drop-shadow(0 0 8px rgba(255, 200, 60, 0.85)); }
@keyframes crownwin { 0% { transform: scale(1); } 40% { transform: translateY(-14%) scale(1.3) rotate(-6deg); } 100% { transform: scale(1.05); } }
.board.shine::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 14px;
  pointer-events: none;
  background: linear-gradient(105deg, transparent 35%, rgba(255, 246, 200, 0.6) 50%, transparent 65%) no-repeat;
  background-size: 300% 100%;
  animation: sheen 1.4s ease-in-out 0.25s both;
  mix-blend-mode: soft-light;
}
@keyframes sheen { from { background-position: 120% 0; } to { background-position: -20% 0; } }
.fx { position: absolute; inset: var(--inset, 1.5%); pointer-events: none; }
.spark {
  position: absolute;
  width: 10px; height: 10px;
  background: var(--gold);
  clip-path: polygon(50% 0, 61% 39%, 100% 50%, 61% 61%, 50% 100%, 39% 61%, 0 50%, 39% 39%);
  animation: spark 0.9s ease-out both;
}
@keyframes spark {
  from { transform: translate(-50%, -50%) scale(0); opacity: 1; }
  60% { opacity: 1; }
  to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(1.1) rotate(90deg); opacity: 0; }
}

/* the curtain over an unstarted or paused board: the map and marks are hidden, not blurred, so
   nothing of the day's provinces shows before the clock runs. A blank grid holds their place. */
.stage.veiled .board { pointer-events: none; }
.stage.veiled .board > * { visibility: hidden; }
.stage.veiled .board::before {
  content: '';
  position: absolute;
  inset: var(--inset, 1.5%);
  border-radius: 6px;
  background:
    linear-gradient(var(--grid) 1.5px, transparent 0) 0 0 / 100% calc(100% / var(--n)),
    linear-gradient(90deg, var(--grid) 1.5px, transparent 0) 0 0 / calc(100% / var(--n)) 100%,
    var(--panel-solid);
  box-shadow: 0 0 0 calc(var(--size) * 4.5 / (var(--n) * 100 + 16)) var(--edge), 0 10px 22px var(--shadow);
}
.curtain {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: min(88%, 330px);
  padding: 20px 20px 22px;
  border-radius: 22px;
  text-align: center;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  box-shadow: 0 16px 40px var(--shadow);
  animation: rise 0.4s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.curtain[hidden] { display: none; }
@keyframes rise { from { opacity: 0; transform: translate(-50%, calc(-50% + 16px)) scale(0.96); } }
.cu-kicker { margin: 0; font-size: 0.74rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: var(--royal); }
.cu-title { margin: 4px 0 6px; font-family: var(--display); font-weight: 800; font-size: 2rem; line-height: 1.05; }
.cu-line { margin: 0 0 16px; font-weight: 600; color: var(--muted); line-height: 1.4; font-size: 0.95rem; }
.curtain .btn { min-width: 150px; }

/* ---------------------------------------------------------------- hints + buttons */
.hintbar {
  margin: 0;
  min-height: 2.9em;
  padding: 7px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--muted);
  border-radius: 14px;
  text-wrap: balance;
  transition: background 0.25s ease, color 0.25s ease;
}
.hintbar.on { display: block; text-align: left; color: var(--ink); background: var(--panel); border: 1px solid var(--line); animation: hintin 0.3s ease; }
.hintbar b { color: var(--royal); }
.hintbar .hint-more { color: var(--muted); }
@keyframes hintin { from { opacity: 0; transform: translateY(-4px); } }
.chip {
  display: inline-block;
  padding: 0 7px;
  border-radius: 7px;
  background: var(--c);
  color: var(--ink);
  font-weight: 800;
  box-shadow: inset 0 0 0 1.5px var(--edge);
}
.controls { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.btn { position: relative; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.3; stroke-linecap: round; stroke-linejoin: round; }
.btn.icon { width: 44px; padding: 0; }
.badge {
  min-width: 20px; height: 20px;
  padding: 0 5px;
  display: inline-grid; place-items: center;
  border-radius: 99px;
  background: var(--royal);
  color: var(--panel-solid);
  font-size: 0.72rem;
  font-weight: 800;
}
.badge[hidden] { display: none; }
.toggle .sw {
  position: relative;
  width: 30px; height: 18px;
  border-radius: 99px;
  background: var(--gold-dim);
  transition: background 0.2s ease;
}
.toggle .sw::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--panel-solid);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.toggle[aria-pressed='true'] .sw { background: var(--royal); }
.toggle[aria-pressed='true'] .sw::after { transform: translateX(12px); }

/* ---------------------------------------------------------------- dialogs */
@keyframes pop { from { opacity: 0; transform: scale(0.94) translateY(8px); } }
.help-grid { display: grid; grid-template-columns: 128px 1fr; gap: 14px; align-items: center; }
.example .map { filter: drop-shadow(0 4px 10px var(--shadow)); }
.ex-x { fill: none; stroke: var(--x); stroke-width: 7; stroke-linecap: round; }
.rules { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.rules li { line-height: 1.4; font-size: 0.93rem; padding-left: 18px; position: relative; }
.rules li::before { content: '♛'; position: absolute; left: 0; top: 0; color: var(--gold); font-size: 0.85rem; }
.how-list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 10px; }
.how-list li { display: flex; gap: 12px; align-items: center; line-height: 1.4; font-size: 0.93rem; }
.mini {
  flex: none;
  width: 46px; height: 34px;
  display: flex; align-items: center; justify-content: center; gap: 3px;
  border-radius: 9px;
  background: var(--p5);
  box-shadow: inset 0 0 0 2px var(--edge);
  font-family: var(--display); font-weight: 800; font-size: 1.1rem; color: var(--ink);
}
.mini.drag { background: var(--p3); }
.mini.hint { background: var(--accent); color: var(--accent-ink); box-shadow: none; }
.mini .mx { position: relative; width: 10px; height: 10px; color: var(--x); }
.mini .mx::before, .mini .mx::after { content: ''; position: absolute; left: -2px; top: 4px; width: 14px; height: 2.5px; border-radius: 2px; background: currentColor; transform: rotate(45deg); }
.mini .mx::after { transform: rotate(-45deg); }
.rule-note { margin: 0 0 10px; line-height: 1.45; font-size: 0.92rem; }
.keys { margin: 4px 0 16px; font-size: 0.82rem; color: var(--muted); line-height: 1.9; }
/* results */
.kicker { color: var(--royal); }
.res-hero { display: flex; align-items: baseline; gap: 14px; margin: -2px 0 6px; }
.res-time { font-family: var(--display); font-weight: 800; font-size: 2.8rem; line-height: 1; font-variant-numeric: tabular-nums; }
.res-stars { display: flex; gap: 2px; font-size: 1.6rem; }
.res-stars span { color: var(--gold-dim); }
.res-stars span.on { color: var(--gold); animation: star 0.5s cubic-bezier(0.2, 1.6, 0.4, 1) both; }
@keyframes star { from { transform: scale(0) rotate(-40deg); } }
.res-line { margin: 0 0 4px; font-weight: 700; color: var(--muted); font-size: 0.9rem; }
.res-line.res-pb { color: var(--ink); }
.res-actions .btn { flex: 1; }
.section-h { margin: 18px 0 10px; font-size: 0.74rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.wd.done i { border: 0; background: var(--gold-dim); color: var(--ink); }
.wd.clean i { border: 0; background: var(--gold); color: #3a2600; }
.wd.today i { border-style: solid; border-color: var(--royal); color: var(--ink); }
.legend { display: flex; gap: 6px; align-items: center; justify-content: center; margin: 10px 0 0; font-size: 0.75rem; font-weight: 700; color: var(--muted); }
.lg { width: 12px; height: 12px; border-radius: 4px; display: inline-block; margin-left: 8px; }
.lg.clean { background: var(--gold); }
.lg.done { background: var(--gold-dim); }
.countdown b { font-family: var(--display); font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }
.progress-note { margin: 0 0 6px; font-weight: 700; }
.toast { z-index: 10; }
@media (max-width: 420px) {
  .logo { font-size: 1.8rem; }
  .icon-btn { width: 36px; height: 36px; border-radius: 11px; }
  .counts { gap: 12px; }
  .count b { font-size: 1.3rem; }
  .hud-day b { font-size: 1.05rem; }
  .controls { gap: 6px; }
  .controls .btn { padding: 0 12px; font-size: 0.9rem; gap: 5px; }
  .controls .btn.icon { width: 44px; padding: 0; }
  .controls .btn svg { width: 16px; height: 16px; }
  .toggle .sw { width: 26px; height: 16px; }
  .toggle .sw::after { width: 12px; height: 12px; }
  .toggle[aria-pressed='true'] .sw::after { transform: translateX(10px); }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .help-grid { grid-template-columns: 104px 1fr; gap: 12px; }
  .hintbar { font-size: 0.84rem; }
}
@media (max-width: 380px) {
  .controls { gap: 5px; }
  .controls .btn { padding: 0 9px; gap: 4px; font-size: 0.86rem; }
  .controls .btn.icon { width: 42px; padding: 0; }
  .badge { min-width: 18px; height: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-delay: 0ms !important; }
}
/* the same for the site-wide Reduce motion setting (hub/prefs.js) */
html.reduce-motion *, html.reduce-motion *::before, html.reduce-motion *::after { animation-delay: 0ms !important; }

/* Landscape phones and other short screens: the board fills the left at full height, and the
   header, status, hint and controls stack on the right. layout() in game.js sizes the board from
   the height alone here. */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    max-width: 960px; display: grid; column-gap: 20px; row-gap: 8px; align-content: start;
    grid-template-columns: minmax(0, calc(100vh - 24px)) minmax(240px, 1fr);
    grid-template-rows: repeat(5, auto) 1fr;
    grid-template-areas: "stage top" "stage banner" "stage hud" "stage hint" "stage controls" "stage .";
  }
  .app > .top { grid-area: top; }
  .app > .banner { grid-area: banner; }
  .app > .hud { grid-area: hud; }
  .app > .stage { grid-area: stage; align-self: start; }
  .app > .hintbar { grid-area: hint; }
  .app > .controls { grid-area: controls; }
}
