:root {
  color-scheme: light dark;
  --bg: #eef1f8;
  --bg-2: #dde3f0;
  --panel: rgba(255, 255, 255, 0.76);
  --panel-solid: #ffffff;
  --ink: #111a2e;
  --muted: #4e5c76;
  --line: rgba(17, 26, 46, 0.11);
  --accent: #2fd9bb;
  --accent-deep: #17a68d;
  --accent-ink: #032a24;
  --cool: #0b6f68;
  --gold: #ffb321;
  --star-edge: #a86f00;
  --gold-dim: rgba(17, 26, 46, 0.16);
  --good: #16895a;
  --bad: #cf4a3c;
  --radius: 18px;
  --display: 'Space Grotesk', system-ui, sans-serif;
  --text: 'DM Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;

  /* board palette, read by the canvas renderer (the chamber stays dark in both themes) */
  --void: #141c30;
  --void-deep: #070b15;
  --frame: #2a3550;
  --frame-hi: rgba(140, 170, 230, 0.16);
  --floor: #111a2c;
  --floor-hi: rgba(150, 180, 240, 0.09);
  --floor-line: rgba(150, 180, 240, 0.18);
  --wall: #232d45;
  --wall-top: #3a4766;
  --wall-hi: rgba(190, 210, 255, 0.28);
  --wall-side: #0b101c;
  --metal: #6f7d99;
  --metal-hi: #c3cde2;
  --metal-dark: #2f3850;
  --mirror: #9fb0cc;
  --mirror-hi: #f4f8ff;
  --mirror-back: #39445e;
  --beam-clear: #5cf2d6;
  --beam-amber: #ffb02e;
  --beam-blue: #5b8cff;
  --core-clear: #e9fffa;
  --core-amber: #fff0c9;
  --core-blue: #e3ebff;
  --deny: #ff5a6a;
  --cursor: #ffffff;
  --hover: rgba(92, 242, 214, 0.45);
}
/* More contrast asked for: brighter grid lines and walls, and beam colours further apart. */
@media (prefers-contrast: more) {
  :root {
    --floor-line: rgba(190, 210, 255, 0.4);
    --wall-hi: rgba(220, 230, 255, 0.6);
    --beam-amber: #ffc400;
    --beam-blue: #4f7dff;
  }
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #06080f;
    --bg-2: #0b1020;
    --panel: rgba(22, 30, 52, 0.72);
    --panel-solid: #121a2e;
    --ink: #e6ecfa;
    --muted: #8b98b8;
    --star-edge: transparent;
    --line: rgba(200, 215, 255, 0.1);
    --accent: #3ee6c6;
    --accent-deep: #1fae94;
    --cool: #5cf2d6;
    --gold-dim: rgba(200, 215, 255, 0.16);
    --good: #4fd08a;
    --bad: #ff7466;

    --void: #10172a;
    --void-deep: #04070e;
  }
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  font-family: var(--text);
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(90% 50% at 50% -12%, rgba(47, 217, 187, 0.16), transparent 65%),
    linear-gradient(180deg, var(--bg), var(--bg-2));
  background-attachment: fixed;
}
@media (prefers-color-scheme: dark) {
  body {
    background:
      radial-gradient(60% 36% at 18% -8%, rgba(62, 230, 198, 0.13), transparent 70%),
      radial-gradient(55% 32% at 86% -10%, rgba(91, 140, 255, 0.15), transparent 70%),
      radial-gradient(40% 24% at 60% 105%, rgba(255, 176, 46, 0.06), transparent 70%),
      linear-gradient(180deg, var(--bg), var(--bg-2));
    background-attachment: fixed;
  }
}

.app {
  width: 100%;
  max-width: 580px;
  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: 12px;
}

/* ---------------------------------------------------------------- header */
.logo {
  margin: 0;
  font-family: var(--display);
  font-weight: 700;
  font-size: 2.1rem;
  line-height: 1;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: linear-gradient(90deg, var(--cool), var(--ink) 55%, var(--cool) 110%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.day { margin: 4px 0 0; font-size: 0.8rem; font-weight: 700; color: var(--muted); letter-spacing: 0.02em; }

.icon-btn { -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); }

/* ---------------------------------------------------------------- chamber tabs */
.chambers {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}
.chambers[hidden] { display: none; }
/* A weekend's bonus chamber (#246) makes a fourth tab: narrower labels on phones. */
.chambers.four { grid-template-columns: repeat(4, 1fr); }
.tab-short { display: none; }
@media (max-width: 420px) {
  .chambers.four .tab-long { display: none; }
  .chambers.four .tab-short { display: inline; }
  .chambers.four .tab-s { font-size: 0.7rem; }
}
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 7px 4px 6px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  transition: background 0.15s ease, transform 0.12s ease;
}
.tab:not(:disabled):hover { background: rgba(120, 150, 210, 0.13); }
.tab:disabled { cursor: default; opacity: 0.5; }
.tab.on { background: var(--panel-solid); box-shadow: 0 1px 2px rgba(10, 20, 50, 0.1), 0 4px 12px rgba(10, 20, 50, 0.08); }
.tab-n { font-family: var(--display); font-weight: 600; font-size: 0.98rem; }
.tab-s { font-size: 0.76rem; font-weight: 700; color: var(--muted); letter-spacing: 0.03em; min-height: 1.1em; }
.tab-s .st { color: var(--gold); }
.tab-s .st.off { color: var(--gold-dim); }

/* ---------------------------------------------------------------- HUD */
.hud { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 44px; }
.crystals { display: flex; gap: 8px; flex-wrap: wrap; }
.crystals i {
  width: 26px; height: 26px;
  display: grid; place-items: center;
  border-radius: 8px;
  font-style: normal;
  font-size: 0.95rem;
  line-height: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  transition: color 0.3s ease, background 0.3s ease, box-shadow 0.3s ease, transform 0.3s ease;
}
.crystals i.c0 { --c: var(--beam-clear); }
.crystals i.c1 { --c: var(--beam-amber); }
.crystals i.c2 { --c: var(--beam-blue); }
.crystals i { color: color-mix(in srgb, var(--c) 45%, transparent); }
/* On the light page the unlit chips take the lit chips' dark tile, so the dimmed glyphs stay legible. */
@media (prefers-color-scheme: light) {
  .crystals i:not(.on) { background: #141c30; border-color: transparent; color: color-mix(in srgb, var(--c) 70%, transparent); }
}
.crystals i.on { color: var(--c); background: #0d1424; border-color: transparent; box-shadow: 0 0 12px color-mix(in srgb, var(--c) 70%, transparent); }
/* A lit chip also gets a tick, so lit and unlit differ by more than brightness. */
.crystals i { position: relative; }
.crystals i.on::after {
  content: "✓"; position: absolute; right: -6px; bottom: -5px; width: 14px; height: 14px; border-radius: 50%;
  display: grid; place-items: center; font-size: 9px; font-weight: 800; line-height: 1;
  background: #fff; color: #0d1424; box-shadow: 0 0 0 1.5px #0d1424;
}
@media (forced-colors: active) {
  .crystals i { border: 1px solid CanvasText; }
  .crystals i.on { border-width: 3px; }
}
.crystals i.pop { animation: gempop 0.45s ease; }
@keyframes gempop { 40% { transform: scale(1.35) rotate(-8deg); } }

.counts { display: flex; align-items: center; gap: 14px; }
.count { display: flex; flex-direction: column; align-items: center; line-height: 1; }
.count b { font-family: var(--display); font-weight: 600; font-size: 1.55rem; font-variant-numeric: tabular-nums; }
.count b span { font-size: 0.9rem; color: var(--muted); font-weight: 500; }
.count small { font-size: 0.68rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); margin-top: 2px; }
.count.par b { color: var(--muted); }
.count b.bump { animation: bump 0.3s ease; }
@keyframes bump { 40% { transform: scale(1.25); } }
.meter { display: flex; gap: 1px; font-size: 1.15rem; }
.meter i { font-style: normal; color: var(--gold); transition: color 0.3s ease, transform 0.3s ease; }
.meter i.off { color: var(--gold-dim); transform: scale(0.85); }

/* ---------------------------------------------------------------- board */
.stage { position: relative; display: flex; justify-content: center; }
#board {
  display: block;
  border-radius: 24px;
  touch-action: manipulation;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(5, 10, 30, 0.18), 0 18px 44px rgba(5, 10, 30, 0.3);
  outline: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}
#board:focus-visible { box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--cool); }

/* The sealed card sits under the board in place of the controls, so the whole solved chamber stays in view. */
.cleared {
  width: min(100%, 420px);
  margin: 0 auto;
  padding: 14px 16px 16px;
  border-radius: 20px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 10px 30px rgba(5, 10, 30, 0.3);
  -webkit-backdrop-filter: blur(14px) saturate(1.3);
  backdrop-filter: blur(14px) saturate(1.3);
  animation: rise 0.4s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.cleared[hidden] { display: none; }
@keyframes rise { from { opacity: 0; transform: translateY(18px) scale(0.96); } }
.cl-kicker { margin: 0; font-size: 0.74rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: var(--cool); }
.cl-stars { font-size: 2rem; line-height: 1.1; letter-spacing: 0.06em; margin: 2px 0; }
.cl-stars span { display: inline-block; color: var(--gold-dim); }
.cl-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); } }
.cl-line { margin: 0 0 12px; font-weight: 700; }
.cl-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------------------------------------------------------------- buttons */
.controls { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }
.controls[hidden] { display: none; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.3; stroke-linecap: round; stroke-linejoin: round; }
.btn.ghost { -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); }
.link { border: 0; background: none; padding: 4px 2px; color: var(--cool); font-weight: 800; font-size: 0.85rem; text-decoration: underline; text-underline-offset: 3px; }

.how { margin: 0; text-align: center; font-size: 0.85rem; color: var(--muted); font-weight: 600; }

/* ---------------------------------------------------------------- dialogs */
@keyframes pop { from { opacity: 0; transform: scale(0.94) translateY(8px); } }

.rules { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 10px; }
.rules li { display: flex; gap: 12px; align-items: flex-start; line-height: 1.4; font-size: 0.95rem; }
.tile { flex: none; width: 34px; height: 34px; }
.t-floor { fill: var(--floor); }
.t-metal { fill: var(--metal); }
.t-lens { fill: var(--core-clear); }
.t-beam { fill: none; stroke: var(--beam-clear); stroke-width: 3.5; stroke-linecap: round; stroke-linejoin: round; filter: drop-shadow(0 0 2px var(--beam-clear)); }
.t-mirror { stroke: var(--mirror-hi); stroke-width: 3.5; stroke-linecap: round; }
.t-gem { stroke: #fff; stroke-width: 1.4; stroke-linejoin: round; }
.t-gem.g0 { fill: var(--beam-clear); }
.t-gem.g1 { fill: var(--beam-amber); }
.t-filter { fill: color-mix(in srgb, var(--beam-amber) 22%, transparent); stroke: var(--beam-amber); stroke-width: 2; }
.t-glyph { fill: none; stroke: var(--beam-amber); stroke-width: 1.6; stroke-linejoin: round; }
.t-wall { fill: var(--wall-side); }
.t-wall-top { fill: var(--wall-top); }
.t-plate { fill: var(--wall-side); }
.t-rivet { fill: var(--wall-hi); }
.rule-note { margin: 0 0 10px; line-height: 1.45; font-size: 0.95rem; }
.keys { margin: 4px 0 16px; font-size: 0.85rem; color: var(--muted); line-height: 1.7; }
@media (pointer: coarse) { .keys { display: none; } }
/* The help's button stays in view while its rules scroll (#108). */
#dlgHelp > .btn.primary { position: sticky; bottom: 0; z-index: 1; box-shadow: 0 0 0 10px var(--panel-solid), 0 16px 0 10px var(--panel-solid); }
.more-rules { margin: 0 0 10px; }
.more-rules summary { cursor: pointer; font-weight: 700; font-size: 0.95rem; padding: 4px 0; }
.more-rules[open] summary { margin-bottom: 10px; }

/* results */
.kicker { color: var(--cool); }
.res-stars { display: flex; gap: 2px; font-size: 1.45rem; margin: -4px 0 12px; }
.res-stars span { color: var(--gold-dim); }
.res-stars span.on { color: var(--gold); }
.res-rows { list-style: none; margin: 0 0 10px; padding: 0; border-top: 1px solid var(--line); }
.res-rows li { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 4px 12px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.rp-name { font-weight: 800; }
.rp-name small { display: block; font-weight: 600; font-size: 0.78rem; color: var(--muted); }
.rp-moves { font-family: var(--display); font-weight: 600; font-variant-numeric: tabular-nums; }
.rp-moves span { color: var(--muted); font-weight: 500; }
.rp-stars { color: var(--gold); letter-spacing: 0.04em; min-width: 3.4em; text-align: right; }
.rp-stars .off { color: var(--gold-dim); }
.rp-watch { grid-column: 1 / -1; justify-self: start; margin-top: -4px; }
.res-total { margin: 0 0 14px; font-weight: 700; color: var(--muted); }
.res-total b { color: var(--ink); }
.res-actions .btn { flex: 1; }
.section-h { margin: 20px 0 10px; font-size: 0.76rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.wd.done i { border: 0; background: rgba(120, 150, 210, 0.2); }
.wd.perfect i { border: 0; background: var(--gold); color: #3a2600; }
.wd.today i { border-style: solid; border-color: var(--cool); }
.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: 10px; }
  .count b { font-size: 1.35rem; }
  .crystals { gap: 4px; }
  .crystals i { width: 22px; height: 22px; font-size: 0.8rem; border-radius: 7px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  /* A compact sealed card, so it fits under the board on a small phone. */
  .cleared { padding: 10px 12px 12px; border-radius: 16px; }
  .cl-stars { font-size: 1.5rem; }
  .cl-line { margin-bottom: 8px; }
  .cl-actions .btn { flex: 1 1 auto; padding: 0 10px; font-size: 0.92rem; }
  .cl-long { display: none; }
}
/* the same for the site-wide Reduce motion setting (hub/prefs.js) */

/* Stars: an amber edge keeps a gold star visible on the light page (3:1), and unearned stars are
   hollow, so earned and unearned differ by shape as well as colour. */
.meter i, .tab-s .st, .cl-stars span.on, .res-stars span.on, .rp-stars { -webkit-text-stroke: 1px var(--star-edge); }
.meter i.off, .tab-s .st.off, .cl-stars span:not(.on), .res-stars span:not(.on), .rp-stars .off { color: transparent; -webkit-text-stroke: 1px var(--muted); }

/* Landscape phones and other short screens: the board fills the left at full height, and the
   header, chambers, status, controls and hint 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(6, auto) 1fr;
    grid-template-areas: "stage top" "stage banner" "stage tabs" "stage hud" "stage controls" "stage how" "stage .";
  }
  .app > .top { grid-area: top; }
  .app > .banner { grid-area: banner; }
  .app > .chambers { grid-area: tabs; }
  .app > .hud { grid-area: hud; }
  .app > .stage { grid-area: stage; align-self: start; }
  .app > .controls, .app > .cleared { grid-area: controls; }
  .app > .how { grid-area: how; }
}

/* The weekend bonus on the results. */
.res-bonus { margin: 10px 0 4px; padding: 12px 14px; border-radius: 14px; border: 1px dashed var(--line); text-align: center; }
.res-bonus p { margin: 0 0 10px; font-size: 0.92rem; }
.res-bonus-rows { margin-top: 8px; }
