:root {
  --bg: #03050c;
  --bg-2: #07102a;
  --ink: #c9d6ff;
  --ink-dim: rgba(170, 190, 255, 0.45);
  --cyan: #3eecff;
  --magenta: #ff48d6;
  --warn: #ffb048;
  --line: rgba(120, 150, 255, 0.18);
  --panel: rgba(8, 14, 36, 0.96);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

body {
  background:
    radial-gradient(ellipse 70% 60% at 50% 48%, var(--bg-2) 0%, rgba(7, 16, 42, 0.35) 45%, transparent 75%),
    var(--bg);
}

.app {
  --chrome: 210px; /* HUD + score bar + controls + gaps, reserved when sizing the square board */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding:
    max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  gap: 12px;
}

/* The result card and the lesson panel need room below the board, so the board shrinks a little. */
.app[data-phase="result"] { --chrome: 330px; }
.app[data-mode="lesson"] { --chrome: 300px; }

/* ---------- HUD ---------- */

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px 12px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.center { text-align: center; min-width: 0; }

.wordmark {
  font-size: 10px;
  letter-spacing: 0.5em;
  margin-right: -0.5em;
  color: var(--ink-dim);
}

.status {
  margin-top: 4px;
  font-size: 14px;
  letter-spacing: 0.06em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.25s;
}
.status[data-color="cyan"] { color: var(--cyan); }
.status[data-color="magenta"] { color: var(--magenta); }
.status[data-tone="warn"] { color: var(--warn); }
.status[data-tone="end"] { color: var(--ink); }
.status[data-tone="think"] { animation: think 1.4s ease-in-out infinite; }
@keyframes think { 50% { opacity: 0.45; } }
.status[data-tone="win"] { font-weight: 600; letter-spacing: 0.1em; }
.status[data-tone="win"][data-color="cyan"] { color: var(--cyan); text-shadow: 0 0 12px rgba(62, 236, 255, 0.6); }
.status[data-tone="win"][data-color="magenta"] { color: var(--magenta); text-shadow: 0 0 12px rgba(255, 72, 214, 0.6); }

.player {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 8px;
  align-items: center;
  opacity: 0.45;
  transition: opacity 0.3s;
}
.player[data-player="magenta"] {
  grid-template-columns: 1fr auto;
  text-align: right;
}
.player[data-player="magenta"] .dot { grid-column: 2; grid-row: 1; }
.player[data-player="magenta"] .name { grid-column: 1; grid-row: 1; }
.player[data-player="magenta"] .caps-wrap { grid-column: 1 / -1; }
.player.active { opacity: 1; }

.name { font-size: 12px; letter-spacing: 0.22em; text-transform: uppercase; }
.caps-wrap { grid-column: 1 / -1; font-size: 11px; color: var(--ink-dim); letter-spacing: 0.05em; }
.caps { color: var(--ink); font-variant-numeric: tabular-nums; }

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  transition: box-shadow 0.3s;
}
[data-player="cyan"] .dot { background: #eafeff; box-shadow: 0 0 0 2px rgba(62, 236, 255, 0.5); }
[data-player="magenta"] .dot { background: #ffeefb; box-shadow: 0 0 0 2px rgba(255, 72, 214, 0.5); }
[data-player="cyan"].active .dot { box-shadow: 0 0 0 2px var(--cyan), 0 0 14px 3px rgba(62, 236, 255, 0.7); }
[data-player="magenta"].active .dot { box-shadow: 0 0 0 2px var(--magenta), 0 0 14px 3px rgba(255, 72, 214, 0.7); }

/* ---------- score bar ---------- */

.scorebar {
  grid-column: 1 / -1;
  display: grid;
  gap: 4px;
  transition: opacity 0.3s;
}
.scorebar.off { visibility: hidden; }
.scorebar.stale { opacity: 0.6; }
.scorebar .track {
  position: relative;
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, rgba(255, 72, 214, 0.75), rgba(176, 40, 255, 0.6));
}
.scorebar .fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 50%;
  border-radius: 2px 0 0 2px;
  background: linear-gradient(90deg, rgba(40, 110, 255, 0.7), var(--cyan));
  box-shadow: 0 0 8px rgba(62, 236, 255, 0.6);
  transition: width 0.6s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.scorebar .mid {
  position: absolute;
  left: 50%;
  top: -3px;
  bottom: -3px;
  width: 2px;
  margin-left: -1px;
  border-radius: 1px;
  background: rgba(230, 240, 255, 0.85);
}
.score-label {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-align: center;
  color: var(--ink-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 1.3em;
}
.score-label[data-color="cyan"] { color: rgba(62, 236, 255, 0.85); }
.score-label[data-color="magenta"] { color: rgba(255, 72, 214, 0.85); }

/* ---------- board ---------- */

.stage {
  position: relative;
  width: min(100%, calc(100vh - var(--chrome)));
  width: min(100%, calc(100dvh - var(--chrome)));
  aspect-ratio: 1;
  margin: 0 auto;
  flex: none;
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
.app[data-phase="scoring"] #board { cursor: pointer; }
.app[data-phase="result"] #board { cursor: default; }

/* ---------- controls ---------- */

.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.row {
  display: flex;
  justify-content: center;
  gap: 10px;
  width: 100%;
}

/* Only the controls for the current phase are shown; a lesson replaces them with its panel. */
.app:not([data-phase="play"]) .for-play,
.app:not([data-phase="scoring"]) .for-scoring,
.app:not([data-phase="result"]) .for-result,
.app:not([data-mode="lesson"]) .for-lesson,
.app[data-mode="lesson"] .controls > :not(.for-lesson) { display: none; }
.app[data-mode="lesson"] .player,
.app[data-mode="lesson"] .scorebar { visibility: hidden; }
.app:not([data-mode="game"]) .wordmark { color: var(--ink); letter-spacing: 0.2em; }

/* ---------- lesson panel ---------- */

.lesson { display: grid; gap: 10px; width: 100%; max-width: 520px; }
.lesson-head { display: flex; align-items: center; gap: 10px; }
.steps { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; min-width: 0; }
.step {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  border: 1px solid rgba(150, 175, 255, 0.45);
}
.step.done { background: rgba(62, 236, 255, 0.55); border-color: rgba(62, 236, 255, 0.7); }
.step.now { box-shadow: 0 0 0 2px rgba(62, 236, 255, 0.35), 0 0 10px rgba(62, 236, 255, 0.6); border-color: var(--cyan); }
.lesson-say {
  margin: 0;
  min-height: 3em;
  font-size: 15px;
  line-height: 1.45;
  text-align: center;
  letter-spacing: 0.02em;
  text-wrap: balance;
}
.lesson-say[data-tone="warn"] { color: var(--warn); }
.lesson-say[data-tone="good"] { color: var(--cyan); }
.choices { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.choices[hidden] { display: none; }
.choices button.lit { border-color: var(--cyan); box-shadow: 0 0 0 2px rgba(62, 236, 255, 0.3), 0 0 12px rgba(62, 236, 255, 0.5); }
button.icon.small { width: 36px; height: 36px; min-height: 36px; }
button.icon.small svg { width: 16px; height: 16px; }

button {
  font: inherit;
  font-size: 12px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink);
  background: rgba(20, 30, 70, 0.35);
  border: 1px solid var(--line);
  border-radius: 999px;
  min-height: 44px;
  min-width: 120px;
  padding: 0 20px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, color 0.2s, box-shadow 0.2s;
}
button:hover:not(:disabled) { border-color: rgba(150, 175, 255, 0.45); }
button:active:not(:disabled) { background: rgba(40, 60, 130, 0.45); }
button:focus-visible { outline: 2px solid rgba(150, 175, 255, 0.6); outline-offset: 2px; }
button:disabled { opacity: 0.35; cursor: default; }
button.primary { border-color: rgba(150, 175, 255, 0.5); background: rgba(40, 60, 140, 0.45); }

button.icon {
  min-width: 44px;
  width: 44px;
  padding: 0;
  display: inline-grid;
  place-items: center;
}
button.icon svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Sound toggle: speaker with waves when on, crossed out when off. */
button.sound .on { display: none; }
button.sound[aria-pressed="true"] .on { display: inline; }
button.sound[aria-pressed="true"] .off { display: none; }
button.sound[aria-pressed="true"] { color: var(--cyan); border-color: rgba(62, 236, 255, 0.35); }

.accept[data-color="cyan"] { color: var(--cyan); }
.accept[data-color="magenta"] { color: var(--magenta); }
.accept[aria-pressed="true"][data-color="cyan"] {
  border-color: var(--cyan);
  background: rgba(62, 236, 255, 0.14);
  box-shadow: 0 0 14px rgba(62, 236, 255, 0.35);
}
.accept[aria-pressed="true"][data-color="magenta"] {
  border-color: var(--magenta);
  background: rgba(255, 72, 214, 0.14);
  box-shadow: 0 0 14px rgba(255, 72, 214, 0.35);
}

/* ---------- result ---------- */

.result {
  width: 100%;
  display: grid;
  gap: 8px;
  justify-items: center;
}

.tally {
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.tally th, .tally td { padding: 2px 14px; text-align: right; }
.tally thead th { font-size: 11px; letter-spacing: 0.2em; text-transform: uppercase; font-weight: 400; }
.tally tbody th { text-align: left; font-weight: 400; color: var(--ink-dim); letter-spacing: 0.08em; }
.tally .c { color: var(--cyan); }
.tally .m { color: var(--magenta); }
.tally .total th, .tally .total td { border-top: 1px solid var(--line); padding-top: 4px; font-size: 15px; }
.tally .total th { color: var(--ink); }
.tally[data-winner="cyan"] .total td:nth-of-type(1) { font-weight: 700; color: var(--cyan); text-shadow: 0 0 10px rgba(62, 236, 255, 0.7); }
.tally[data-winner="magenta"] .total td:nth-of-type(2) { font-weight: 700; color: var(--magenta); text-shadow: 0 0 10px rgba(255, 72, 214, 0.7); }

.hint { margin: 0; font-size: 11px; color: var(--ink-dim); text-align: center; letter-spacing: 0.03em; }

/* ---------- dialogs ---------- */

dialog.sheet {
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 20px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid rgba(120, 150, 255, 0.28);
  border-radius: 16px;
  box-shadow: 0 0 40px rgba(40, 110, 255, 0.18);
}
dialog.sheet::backdrop { background: rgba(2, 4, 12, 0.7); }
dialog h2 {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0.35em;
  text-transform: uppercase;
  text-align: center;
}
dialog form { display: grid; gap: 14px; }

.field { display: grid; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }
.label { font-size: 11px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--ink-dim); padding: 0; }
.field small { font-size: 11px; color: var(--ink-dim); }
.field[hidden] { display: none; }

select, input[type="number"] {
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  background: rgba(20, 30, 70, 0.5);
  border: 1px solid var(--line);
  border-radius: 10px;
  min-height: 44px;
  padding: 0 12px;
  -webkit-user-select: text;
  user-select: text;
}
select:focus-visible, input:focus-visible { outline: 2px solid rgba(150, 175, 255, 0.6); outline-offset: 1px; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.segmented.two { grid-template-columns: repeat(2, 1fr); }
.segmented.five { grid-template-columns: repeat(5, 1fr); }
.segmented.five span { font-size: 13px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: grid;
  place-items: center;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.segmented input:checked + span { border-color: rgba(62, 236, 255, 0.7); background: rgba(62, 236, 255, 0.12); color: var(--cyan); }
.segmented input[name="color"][value="2"]:checked + span { border-color: rgba(255, 72, 214, 0.7); background: rgba(255, 72, 214, 0.12); color: var(--magenta); }
.segmented input:focus-visible + span { outline: 2px solid rgba(150, 175, 255, 0.6); outline-offset: 2px; }

.actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }
.actions button { min-width: 0; flex: 1; }

/* ---------- learn path, codex, concept cards ---------- */

.sheet-sub { margin: -6px 0 0; font-size: 12px; color: var(--ink-dim); text-align: center; }
.learn-list, .codex-list { display: grid; gap: 14px; }

/* Campaign map: one region per world, nodes on a zigzag joined by filaments (src/ui/map.js). */
.map { display: grid; gap: 18px; }
.region { display: grid; gap: 4px; }
.region-head { display: flex; align-items: baseline; gap: 10px; }
.region-number { font-size: 11px; color: var(--ink-dim); font-variant-numeric: tabular-nums; }
.region-place { font-size: 14px; letter-spacing: 0.22em; text-transform: uppercase; }
.region-board { margin-left: auto; font-size: 11px; color: var(--ink-dim); white-space: nowrap; }
.region-line { margin: 0; font-size: 12px; line-height: 1.45; color: var(--ink-dim); }
.region[data-state="open"] .region-place { color: var(--cyan); }
.region[data-state="open"] .region-line { color: var(--ink); font-style: italic; }
.region[data-state="soon"] { opacity: 0.55; }
.region[data-state="locked"] { opacity: 0.8; }
.region-soon { margin: 0; font-size: 11px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--ink-dim); }
.region-field { position: relative; margin-top: 4px; }
.region-threads { position: absolute; inset: 0; pointer-events: none; }

button.node {
  position: absolute;
  left: calc(var(--x) * 100%);
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc((1 - var(--x)) * 100%);
  min-width: 0;
  min-height: 40px;
  padding: 0 8px 0 0;
  border: 0;
  background: none;
  box-shadow: none;
  text-align: left;
  text-transform: none;
  letter-spacing: 0.04em;
}
.node-mark {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel);
  font-size: 14px;
  color: var(--ink-dim);
}
.node.boss .node-mark { border-radius: 12px; }
.node-text { display: grid; gap: 1px; min-width: 0; }
.node-name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node.boss .node-name { letter-spacing: 0.16em; text-transform: uppercase; }
.node-detail { font-size: 11px; color: var(--ink-dim); }
.node:focus-visible { outline: none; }
.node:focus-visible .node-mark { outline: 2px solid rgba(150, 175, 255, 0.6); outline-offset: 2px; }
.node[data-state="locked"] { opacity: 0.5; }
.node[data-state="open"] .node-mark { border-color: rgba(62, 236, 255, 0.4); }
.node[data-state="next"] .node-mark { border-color: var(--cyan); color: var(--cyan); box-shadow: 0 0 14px rgba(62, 236, 255, 0.45); animation: node-breathe 2.4s ease-in-out infinite; }
.node[data-state="cleared"] .node-mark { border-color: rgba(62, 236, 255, 0.7); background: rgba(62, 236, 255, 0.16); color: var(--cyan); }
.node[data-state="mastered"] .node-mark { border-color: var(--magenta); background: rgba(255, 72, 214, 0.14); color: #ffd6f6; box-shadow: 0 0 12px rgba(255, 72, 214, 0.35); }
.node.boss[data-state="next"] .node-mark, .node.boss[data-state="open"] .node-mark { border-color: var(--magenta); color: var(--magenta); box-shadow: 0 0 14px rgba(255, 72, 214, 0.4); }
@keyframes node-breathe { 50% { box-shadow: 0 0 22px rgba(62, 236, 255, 0.7); } }

.codex-tabs button { min-width: 0; }
.codex-tabs button[aria-selected="true"] { border-color: rgba(62, 236, 255, 0.7); background: rgba(62, 236, 255, 0.12); color: var(--cyan); }
.lore-world { display: grid; gap: 8px; }
.lore-place { margin: 0; font-size: 13px; font-weight: 400; letter-spacing: 0.22em; text-transform: uppercase; color: var(--cyan); }
.lore-line { margin: 0; font-size: 13px; line-height: 1.5; font-style: italic; }
.lore-memory { margin: 0; padding-left: 12px; border-left: 1px solid rgba(255, 72, 214, 0.45); font-size: 13px; line-height: 1.5; color: var(--ink); }
.lore-chapter .codex-title { color: var(--magenta); }
.codex-empty { margin: 8px 0; font-size: 13px; color: var(--ink-dim); text-align: center; }
button.codex-item { display: grid; gap: 4px; min-width: 0; border-radius: 12px; text-align: left; text-transform: none; letter-spacing: 0.04em; padding: 10px 14px; }
.codex-title { font-size: 14px; color: var(--cyan); }
.codex-win { font-size: 12px; color: var(--ink-dim); }
.concept-card {
  display: grid;
  gap: 8px;
  padding: 16px;
  border: 1px solid rgba(62, 236, 255, 0.35);
  border-radius: 12px;
  background: radial-gradient(ellipse at 50% 0%, rgba(62, 236, 255, 0.1), transparent 70%);
  box-shadow: inset 0 0 24px rgba(62, 236, 255, 0.06);
}
.card-title { margin: 0 0 2px; font-size: 18px; font-weight: 400; letter-spacing: 0.18em; text-transform: uppercase; color: var(--cyan); text-align: center; }
.card-line { margin: 0; font-size: 14px; line-height: 1.45; }
.card-win { margin: 4px 0 0; padding-top: 8px; border-top: 1px solid var(--line); font-size: 14px; line-height: 1.45; color: var(--ink); }
.card-win::before { content: "How it helps you win · "; color: var(--ink-dim); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; display: block; }
.card-memory { margin: 2px 0 0; padding-left: 12px; border-left: 1px solid rgba(255, 72, 214, 0.45); font-size: 13px; line-height: 1.5; font-style: italic; }
.concept-card.chapter { border-color: rgba(255, 72, 214, 0.4); background: radial-gradient(ellipse at 50% 0%, rgba(255, 72, 214, 0.1), transparent 70%); }
.concept-card.chapter .card-title { color: var(--magenta); font-size: 15px; }
.concept-card.chapter .card-line { font-style: italic; }
.concept-card.chapter .card-win::before { content: "What the world taught you · "; }

.menu-list { display: grid; gap: 10px; }
.menu-list button { width: 100%; }

.switch {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  padding: 0 4px;
  font-size: 13px;
  letter-spacing: 0.06em;
  cursor: pointer;
}
.switch input { width: 20px; height: 20px; margin: 0; accent-color: var(--cyan); }

@media (prefers-reduced-motion: reduce) {
  .scorebar .fill, .status, .player, button { transition: none; }
  .status[data-tone="think"], .node[data-state="next"] .node-mark { animation: none; }
}

/* Narrow phones: tighter HUD and controls. */
@media (max-width: 420px) {
  .hud { gap: 8px 6px; }
  .name { letter-spacing: 0.12em; font-size: 11px; }
  .status { font-size: 13px; }
  .row { gap: 8px; }
  .row button { min-width: 0; flex: 1; max-width: 180px; padding: 0 10px; letter-spacing: 0.12em; }
  .row button.icon { flex: none; }
  .tally th, .tally td { padding: 2px 10px; }
}

/* Phones in landscape: HUD and controls move to the sides so the board gets full height. */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    display: grid;
    grid-template-rows: 1fr;
    grid-template-columns: minmax(150px, 1fr) auto minmax(150px, 1fr);
    grid-template-areas: "hud stage controls";
    gap: 12px;
  }
  .app[data-phase] { --chrome: 24px; }
  .hud {
    grid-area: hud;
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    align-content: center;
    gap: 18px;
  }
  .hud .center { order: -1; text-align: left; }
  .score-label { text-align: left; white-space: normal; }
  .player[data-player="magenta"] { grid-template-columns: auto 1fr; text-align: left; }
  .player[data-player="magenta"] .dot { grid-column: 1; }
  .player[data-player="magenta"] .name { grid-column: 2; }
  .stage { grid-area: stage; width: auto; height: 100%; align-self: center; }
  .controls { grid-area: controls; justify-content: center; align-items: flex-end; }
  .row { flex-wrap: wrap; justify-content: flex-end; }
  .row.for-scoring { flex-direction: column; align-items: flex-end; }
  .result { justify-items: end; }
  .result .hint { text-align: right; }
  .tally { font-size: 12px; }
}
