:root {
  color-scheme: light;
  --bg: #f4f1e8;
  --surface: #fffdf6;
  --surface-2: #e9e2cf;
  --ink: #23211b;
  --muted: #5f5747;
  --line: #d3c9ae;
  --accent: #9c2b1f;
  --accent-ink: #ffffff;
  --focus: #1a4fd6;
  --ok: #1e6b2e;
  --warn: #7a4d00;
  --asphalt: #3d4147;
  --asphalt-line: #f5c518;
  --cell: #e3dcc7;
  --cell-grid: #c9bd9c;
  --exit: #1e6b2e;
  --player: #c02718;
  --player-ink: #ffffff;
  --c0: #4f6f9e; --c1: #3f7d4e; --c2: #b9862f; --c3: #7a5fa0;
  --c4: #4fa3a5; --c5: #b25a2a; --c6: #6b7f3a; --c7: #a04a6e;
  --c8: #5a6b7a; --c9: #8a6d3b;
  --car-ink: #ffffff;
  --radius: 10px;
  --topbar-h: 3rem;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #16181d;
  --surface: #1f2229;
  --surface-2: #2a2f38;
  --ink: #f0ead9;
  --muted: #bdb49c;
  --line: #434a56;
  --accent: #ff8d7a;
  --accent-ink: #2a0d08;
  --focus: #9db9ff;
  --ok: #8fd694;
  --warn: #ffd479;
  --asphalt: #0f1114;
  --asphalt-line: #f5c518;
  --cell: #262b34;
  --cell-grid: #3a414d;
  --exit: #8fd694;
  --player: #d63a26;
  --player-ink: #ffffff;
  --c0: #6f93c4; --c1: #5da372; --c2: #d3a24a; --c3: #9a82c4;
  --c4: #6fc3c5; --c5: #d27945; --c6: #8ba351; --c7: #c46a8d;
  --c8: #7e93a8; --c9: #b08d4f;
  --car-ink: #101216;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
section { scroll-margin-top: calc(var(--topbar-h) + 0.75rem); }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Georgia, "Times New Roman", "DejaVu Serif", serif;
  line-height: 1.6;
  font-size: 1.0625rem;
}

/* ---------- one-line top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: var(--topbar-h);
  padding: 0 0.75rem;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  white-space: nowrap;
}
.topbar .home { font-weight: bold; color: var(--ink); text-decoration: none; flex: none; }
.topnav { display: flex; gap: 0.25rem; overflow-x: auto; scrollbar-width: none; }
.topnav::-webkit-scrollbar { display: none; }
.topnav a {
  color: var(--muted);
  text-decoration: none;
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  flex: none;
}
.topnav a[aria-current="true"] { color: var(--ink); font-weight: bold; }
.topbar .spacer { flex: 1; }
.topbar .chip {
  flex: none;
  color: var(--ink);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.3rem 0.8rem;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 0.85rem;
}
.icon-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  cursor: pointer;
}
.icon-btn svg { display: block; }
.icon-btn svg[hidden] { display: none; }
@media (max-width: 380px) {
  .topbar { gap: 0.4rem; padding: 0 0.5rem; }
  .topbar .home {
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar .chip { padding: 0.3rem 0.6rem; font-size: 0.8rem; }
}

a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.skip {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--surface);
  padding: 0.5rem 1rem;
  z-index: 50;
}
.skip:focus { left: 0.5rem; top: 0.5rem; }

main { max-width: 68ch; margin: 0 auto; padding: 0 1rem 4rem; }
main.wide { max-width: 1100px; }
.hero { padding: 2rem 0 0.5rem; }
.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0 0 0.5rem;
  font-family: Verdana, Geneva, sans-serif;
}
h1 { font-size: clamp(2rem, 5vw, 3.2rem); line-height: 1.12; margin: 0 0 0.75rem; text-wrap: balance; }
.deck { font-size: 1.2rem; max-width: 56ch; }
.source-cue { color: var(--muted); font-size: 0.9rem; }

/* ---------- game ---------- */
.game-shell {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  margin: 1.5rem 0;
}
.game-top { display: flex; gap: 0.5rem; align-items: baseline; flex-wrap: wrap; margin-bottom: 0.5rem; }
.game-top h2 { font-size: 1.25rem; margin: 0 1rem 0 0; }
.instructions { margin: 0.25rem 0 0.75rem; color: var(--muted); font-size: 0.95rem; }
.game-layout { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) {
  .game-layout { grid-template-columns: minmax(0, 7fr) minmax(0, 5fr); align-items: start; }
}

.board-frame {
  background: var(--asphalt);
  border-radius: 14px;
  padding: clamp(10px, 2vw, 18px);
  position: relative;
}
.board {
  position: relative;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 3px;
  background: var(--cell-grid);
  border-radius: 6px;
  padding: 6px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}
.board .grid-cell {
  background: var(--cell);
  border-radius: 4px;
  min-width: 0;
  min-height: 0;
}
.exit-notch {
  position: absolute;
  right: 0;
  top: calc(6px + 2 * (100% - 12px) / 6 + 3px);
  height: calc((100% - 12px) / 6 - 3px);
  width: 10px;
  background: var(--exit);
  border-radius: 6px 0 0 6px;
}
.exit-flag {
  position: absolute;
  right: 12px;
  top: calc(6px + 2 * (100% - 12px) / 6);
  height: calc((100% - 12px) / 6);
  display: flex;
  align-items: center;
  color: var(--exit);
  font-weight: bold;
  font-size: 1.4rem;
  pointer-events: none;
}
.car-layer { position: absolute; inset: 6px; pointer-events: none; }
.car {
  position: absolute;
  pointer-events: auto;
  border: none;
  border-radius: 22% / 30%;
  cursor: grab;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--car-ink);
  font-family: Verdana, Geneva, sans-serif;
  font-weight: bold;
  font-size: 0.8rem;
  transition: left 120ms ease-out, top 120ms ease-out, transform 120ms ease-out, box-shadow 120ms;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.18), inset 0 3px 0 rgba(255, 255, 255, 0.25);
  touch-action: none;
}
.car.v { border-radius: 30% / 22%; flex-direction: column; }
.car .glass {
  background: rgba(255, 255, 255, 0.35);
  border-radius: 4px;
  pointer-events: none;
}
.car.h .glass { width: 34%; height: 46%; margin-right: 28%; }
.car.v .glass { width: 46%; height: 22%; margin-bottom: 0; margin-top: 28%; }
.car.player { background: var(--player); color: var(--player-ink); }
.car.c0 { background: var(--c0); } .car.c1 { background: var(--c1); }
.car.c2 { background: var(--c2); } .car.c3 { background: var(--c3); }
.car.c4 { background: var(--c4); } .car.c5 { background: var(--c5); }
.car.c6 { background: var(--c6); } .car.c7 { background: var(--c7); }
.car.c8 { background: var(--c8); } .car.c9 { background: var(--c9); }
.car.selected { outline: 3px solid var(--focus); outline-offset: 1px; z-index: 3; }
.car.dragging { cursor: grabbing; z-index: 4; transition: none; box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35); }
.car.exiting { transition: transform 650ms ease-in; }

.controls { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.75rem; }
button, .btn {
  font-family: Verdana, Geneva, sans-serif;
  font-size: 0.95rem;
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: bold; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
.status-line { display: flex; gap: 1rem; flex-wrap: wrap; font-size: 0.95rem; color: var(--muted); margin-top: 0.5rem; }
.status-line strong { color: var(--ink); }
.live {
  margin-top: 0.5rem;
  min-height: 2.6em;
  padding: 0.5rem 0.75rem;
  border-left: 4px solid var(--accent);
  background: var(--surface-2);
  border-radius: 0 8px 8px 0;
  font-size: 0.98rem;
}
.live.good { border-left-color: var(--ok); }

.level-grid { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.6rem; grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr)); }
.level-grid button {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  text-align: left;
  min-height: 5.5rem;
}
.level-grid .lvl-name { font-weight: bold; font-size: 1rem; }
.level-grid .lvl-meta { font-size: 0.85rem; color: var(--muted); }
.level-grid .lvl-stars { color: var(--warn); letter-spacing: 0.1em; }
.win-panel {
  border: 2px solid var(--ok);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-top: 1rem;
  background: var(--surface);
}
.win-panel h3 { margin: 0 0 0.25rem; font-size: 1.3rem; }
.win-panel .stars { font-size: 2rem; color: var(--warn); letter-spacing: 0.15em; margin: 0.25rem 0; }
.win-panel p { margin: 0.25rem 0 0.75rem; }
.win-panel .row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
[hidden] { display: none !important; }

/* ---------- editorial ---------- */
.chapter { margin: 3rem 0; }
.chapter h2 { font-size: clamp(1.5rem, 3.4vw, 2.1rem); line-height: 1.2; text-wrap: balance; }
.prose p { max-width: 62ch; }
.answer-box {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin: 1.25rem 0;
}
.answer-box strong { font-size: 1.1rem; }
.rules { padding-left: 1.25rem; }
.rules li { margin-bottom: 0.4rem; max-width: 62ch; }
.method { font-size: 0.95rem; color: var(--muted); }
.method dt { font-weight: bold; color: var(--ink); }
.method dd { margin: 0 0 0.5rem; }

footer.site {
  border-top: 1px solid var(--line);
  padding: 1.5rem 1rem 3rem;
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: center;
  color: var(--muted);
  font-size: 0.9rem;
  max-width: 68ch;
  margin: 0 auto;
}
footer.site .spacer { flex: 1; }
