:root {
  --bg: #0d1424;
  --panel: #16203a;
  --panel-2: #1e2b4d;
  --ink: #eef2fb;
  --muted: #9aa8c7;
  --accent: #2fd3c5;
  --accent-ink: #062b28;
  --good: #3ddc84;
  --warn: #f4b740;
  --bad: #ff6b6b;
  --tile: #f6f1e4;
  --tile-ink: #1b2236;
  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); }
h1, h2, h3 { margin: 0 0 0.4em; }
button {
  font: inherit; font-weight: 700; border: 0; border-radius: 999px; padding: 0.8em 1.4em;
  background: var(--panel-2); color: var(--ink); cursor: pointer;
}
button.primary { background: var(--accent); color: var(--accent-ink); }
button.selected { background: var(--accent); color: var(--accent-ink); }
button:disabled { opacity: 0.45; cursor: default; }
input {
  font: inherit; font-size: 1.2rem; padding: 0.7em 1em; border-radius: var(--radius);
  border: 2px solid var(--panel-2); background: var(--panel); color: var(--ink); width: 100%;
}
.brand { font-weight: 800; letter-spacing: 0.02em; }
.brand span { color: var(--accent); }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* Board */
.board {
  --size: 4;
  display: grid; grid-template-columns: repeat(var(--size), 1fr); gap: 2.2%;
  aspect-ratio: 1; width: 100%; position: relative; user-select: none; -webkit-user-select: none;
}
.tile {
  background: var(--tile); color: var(--tile-ink); border-radius: 16%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; aspect-ratio: 1; box-shadow: 0 0.25em 0 rgba(0, 0, 0, 0.25);
  transition: transform 80ms, background 80ms;
}
.tile.on { background: var(--accent); color: var(--accent-ink); transform: scale(0.94); }
.board.flash-good .tile.on { background: var(--good); }
.board.flash-warn .tile.on { background: var(--warn); }
.board.flash-bad .tile.on { background: var(--bad); color: #fff; }
.board svg.trail { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.board svg.trail polyline { fill: none; stroke: rgba(47, 211, 197, 0.55); stroke-linecap: round; stroke-linejoin: round; }

/* Display page */
body.display { padding: 2.5vh 3vw; min-height: 100vh; }
.display header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 2vh; }
.display header .brand { font-size: 3vh; }
.display .code-pill { font-size: 2.4vh; background: var(--panel); padding: 0.3em 0.9em; border-radius: 999px; }
.display .code-pill b { letter-spacing: 0.2em; color: var(--accent); }
.lobby-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 4vw; align-items: center; }
.qr-card { background: #fff; border-radius: 24px; padding: 2vh; width: min(60vh, 100%); justify-self: center; }
.qr-card img { width: 100%; display: block; }
.join-help { font-size: 2.6vh; }
.join-help .url { color: var(--accent); font-weight: 700; word-break: break-all; }
.player-list { list-style: none; padding: 0; margin: 2vh 0; display: grid; grid-template-columns: 1fr 1fr; gap: 1vh 2vw; }
.player-list li { background: var(--panel); border-radius: var(--radius); padding: 1.2vh 1.4vw; font-size: 2.4vh; display: flex; justify-content: space-between; }
.player-list li.away { opacity: 0.45; }
.badge { font-size: 0.7em; background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 0.1em 0.6em; font-weight: 800; }
.play-grid { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 4vw; align-items: start; }
.play-grid .board { width: min(82vh, 55vw); font-size: calc(min(82vh, 55vw) / var(--size) * 0.45); }
.timer { font-size: 9vh; font-weight: 800; font-variant-numeric: tabular-nums; }
.timer.low { color: var(--bad); }
.results-head { text-align: center; margin-bottom: 3vh; }
.results-head .winner { font-size: 6vh; font-weight: 800; color: var(--accent); }
.results-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1.5vw; }
.result-card { background: var(--panel); border-radius: var(--radius); padding: 1.6vh 1.2vw; }
.result-card.win { outline: 3px solid var(--accent); }
.result-card h3 { display: flex; justify-content: space-between; font-size: 2.6vh; }
.word-list { list-style: none; padding: 0; margin: 0; columns: 2; font-size: 1.9vh; }
.word-list li { break-inside: avoid; display: flex; justify-content: space-between; gap: 0.6em; padding: 0.1em 0; }
.word-list li.unique { color: var(--accent); font-weight: 700; }
.x2 { font-size: 0.75em; background: var(--accent); color: var(--accent-ink); border-radius: 6px; padding: 0 0.35em; margin-left: 0.3em; }

/* Phone page */
body.phone {
  position: fixed; inset: 0; overflow: hidden; touch-action: manipulation; overscroll-behavior: none;
  padding: max(env(safe-area-inset-top), 12px) 16px max(env(safe-area-inset-bottom), 12px);
  display: flex; flex-direction: column;
}
.phone .screen { display: flex; flex-direction: column; gap: 14px; flex: 1; min-height: 0; }
.phone .top { display: flex; justify-content: space-between; align-items: center; }
.phone .top .brand { font-size: 1.2rem; }
.phone .status { font-size: 0.85rem; color: var(--warn); }
.phone .card { background: var(--panel); border-radius: var(--radius); padding: 16px; }
.phone .options { display: flex; gap: 8px; flex-wrap: wrap; }
.phone .options button { flex: 1; padding: 0.7em 0.4em; }
.phone .big { font-size: 1.25rem; padding: 1em; width: 100%; }
.phone .players { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.phone .players li { background: var(--panel-2); border-radius: 999px; padding: 0.35em 0.8em; font-size: 0.95rem; }
.phone .hud { display: flex; justify-content: space-between; align-items: baseline; }
.phone .hud .timer { font-size: 2.2rem; }
.phone .current { height: 2.4rem; font-size: 1.8rem; font-weight: 800; text-align: center; letter-spacing: 0.08em; }
.phone .current.good { color: var(--good); }
.phone .current.warn { color: var(--warn); }
.phone .current.bad { color: var(--bad); }
.phone .board { touch-action: none; font-size: calc((100vw - 32px) / var(--size) * 0.45); max-width: 520px; margin: 0 auto; }
.phone .found { flex: 1; min-height: 0; overflow-y: auto; }
.phone .found ul { columns: 3; font-size: 0.95rem; }
.phone .score { font-weight: 800; color: var(--accent); }
