:root {
  --bg: #070a14;
  --bg-panel: #111729;
  --bg-panel-2: #0d1322;
  --line: rgba(255, 255, 255, 0.09);
  --text: #e8ecff;
  --muted: #8d96b8;
  --accent: #4ade80;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  background:
    radial-gradient(1000px 600px at 50% -10%, #16301f 0%, transparent 60%),
    var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

/* ---- top bar ---- */

.topbar {
  width: 100%;
  max-width: 900px;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.topbar h1 { font-size: 20px; letter-spacing: 0.02em; margin: 0; flex: 1; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.back {
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
}
.back:hover { color: var(--text); border-color: rgba(255,255,255,0.22); }

.btn {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 14px;
  cursor: pointer;
}
.btn:hover { border-color: rgba(255,255,255,0.25); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn[aria-pressed="true"] { border-color: rgba(74, 222, 128, 0.5); color: #bbf7d0; }

.btn-primary {
  background: linear-gradient(180deg, #5ee68e, #22a95c);
  border-color: transparent;
  color: #04190c;
  font-weight: 650;
  padding: 10px 22px;
  font-size: 15px;
}

/* ---- layout ---- */

.game {
  /* One source of truth for the board size: the grid column and the wrapper
     both read it, so the column can't be stretched by the canvas's intrinsic
     (device-pixel) width. */
  --board: max(200px, min(100%, 504px, calc(100vh - 200px)));
  display: grid;
  grid-template-columns: var(--board) 180px;
  gap: 18px;
  align-items: start;
  justify-content: center;
}

.rail { display: flex; flex-direction: column; gap: 14px; }

.panel {
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.stats dl { margin: 0; display: grid; gap: 8px; }
.stats dl div { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.stats dt { color: var(--muted); font-size: 12px; }
.stats dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 650; font-size: 16px; }

.keys ul, .legend ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.keys li, .legend li { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.keys span, .legend li span:last-child { margin-left: auto; }

kbd {
  font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: #1c2440;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 3px 5px;
  color: var(--text);
}

.legend .dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 6px;
  flex: 0 0 auto;
}
.dot-food { background: #f87171; }
.dot-bonus { background: #facc15; }
.legend p { margin: 10px 0 0; font-size: 11px; color: var(--muted); line-height: 1.45; }

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

.board-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  line-height: 0;
}

/* The wrapper owns the size. The board is square, so one `min()` that fits
   both the column width and the viewport height keeps it square everywhere. */
.board-wrap { width: var(--board); }

#board {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  padding: 24px;
  line-height: 1.5;
  background: rgba(7, 10, 20, 0.86);
  backdrop-filter: blur(6px);
}
.overlay[hidden] { display: none; }
.overlay h2 { margin: 0; font-size: 28px; }
.overlay p { margin: 0 0 6px; color: var(--muted); font-size: 13px; max-width: 28ch; }

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

.touch { display: none; }

@media (max-width: 760px), (pointer: coarse) {
  .touch {
    display: grid;
    grid-template-columns: repeat(3, 72px);
    grid-template-areas: ". up ." "left down right";
    gap: 8px;
    justify-content: center;
  }
  .touch .up { grid-area: up; }
  .touch [data-action="left"] { grid-area: left; }
  .touch [data-action="down"] { grid-area: down; }
  .touch [data-action="right"] { grid-area: right; }
  .touch button {
    font: 600 20px/1 ui-sans-serif, system-ui, sans-serif;
    color: var(--text);
    background: var(--bg-panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px 0;
    cursor: pointer;
    touch-action: manipulation;
  }
  .touch button:active { background: #1c2440; }
}

@media (max-width: 760px) {
  body { gap: 12px; padding-top: 12px; }
  .topbar { justify-content: center; }
  .topbar h1 { flex: 0 0 auto; font-size: 17px; }
  .game { --board: max(200px, min(100%, 504px, calc(100vh - 330px))); grid-template-columns: var(--board); }
  .rail { flex-direction: row; width: 100%; max-width: 460px; }
  .rail .panel { flex: 1; }
  .keys, .legend p { display: none; }
}
