:root {
  --bg: #f3efe7;
  --surface: #fffdf8;
  --text: #1f2430;
  --muted: #6b6f7a;
  --line: #e1dacd;
  --accent: #e5484d;
  --gold: #f5c542;
  --btn: #ffffff;
  --btn-hover: #f6f1e8;
  --board: #2b3140;
  --board-edge: #1c212c;
  --lane: rgba(255, 255, 255, 0.06);
  --shadow: 0 10px 30px rgba(31, 36, 48, 0.18);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171f;
    --surface: #1d212b;
    --text: #eceef2;
    --muted: #9aa0ad;
    --line: #2d3240;
    --btn: #242936;
    --btn-hover: #2d3342;
    --board: #2b3140;
    --board-edge: #0d1016;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- header & HUD ---------- */

.top { display: flex; align-items: center; justify-content: space-between; }
.top-actions { display: flex; gap: 8px; }
#btn-sound { min-width: 44px; min-height: 44px; font-size: 1.1rem; }

.logo {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: -0.02em;
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-mark {
  width: 30px;
  height: 18px;
  border-radius: 6px;
  background: var(--accent);
  box-shadow: inset -7px 0 0 rgba(0, 0, 0, 0.18);
  position: relative;
}
.logo-mark::after {
  content: "";
  position: absolute;
  right: -11px;
  top: 3px;
  border: 6px solid transparent;
  border-left: 7px solid var(--gold);
}

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.level-title { display: flex; flex-direction: column; font-weight: 700; font-size: 1.15rem; }

.tier {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stats { display: flex; gap: 8px; margin: 0; }
.stats div {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px 12px;
  text-align: center;
  min-width: 64px;
}
.stats dt { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.stats dd { margin: 0; font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }

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

.board-wrap { position: relative; }

.board {
  --pad: clamp(8px, 2.5vw, 14px);
  position: relative;
  aspect-ratio: 1;
  width: 100%;
  padding: var(--pad);
  background: var(--board);
  border-radius: 22px;
  box-shadow: var(--shadow), inset 0 0 0 4px var(--board-edge);
  overflow: clip;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.grid {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  background-image:
    linear-gradient(var(--lane) 2px, transparent 2px),
    linear-gradient(90deg, var(--lane) 2px, transparent 2px);
  background-size: calc(100% / 6) calc(100% / 6);
  background-position: -1px -1px;
}

/* Gap in the board frame at the exit row */
.exit {
  position: absolute;
  right: 0;
  top: calc(var(--pad) + (100% - 2 * var(--pad)) * 2 / 6);
  height: calc((100% - 2 * var(--pad)) / 6);
  width: var(--pad);
  background: var(--board);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1px;
}
.exit span {
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-left: 6px solid var(--gold);
  animation: exit-blink 1.6s infinite;
}
.exit span:nth-child(2) { animation-delay: 0.2s; }
@keyframes exit-blink { 0%, 100% { opacity: 0.35; } 50% { opacity: 1; } }

/* ---------- vehicles ---------- */

.vehicle {
  --c: #4f8ef7;
  position: absolute;
  padding: calc(var(--cell) * 0.07);
  border: 0;
  background: none;
  cursor: grab;
  transition: left 0.16s ease-out, top 0.16s ease-out;
  touch-action: none;
  outline: none;
  z-index: 1;
}
.vehicle.dragging { transition: none; cursor: grabbing; z-index: 3; }
.vehicle.exiting { transition: left 0.55s cubic-bezier(0.5, 0, 0.9, 0.4); z-index: 3; }

.vehicle .body {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: calc(var(--cell) * 0.2);
  background: linear-gradient(var(--grad), color-mix(in srgb, var(--c) 100%, white 18%), var(--c) 55%, color-mix(in srgb, var(--c) 82%, black));
  box-shadow: 0 3px 0 color-mix(in srgb, var(--c) 60%, black), 0 6px 12px rgba(0, 0, 0, 0.35);
  transition: transform 0.12s, box-shadow 0.12s;
}
.vehicle.h .body { --grad: to bottom; }
.vehicle.v .body { --grad: to right; }

.vehicle.dragging .body,
.vehicle:active .body { transform: scale(1.03); box-shadow: 0 4px 0 color-mix(in srgb, var(--c) 60%, black), 0 12px 22px rgba(0, 0, 0, 0.45); }

.vehicle:focus-visible .body { outline: 3px solid var(--gold); outline-offset: 2px; }

/* Windows: a windshield at the front and a rear window, drawn as tinted glass panels */
.vehicle .glass {
  position: absolute;
  background: linear-gradient(135deg, rgba(20, 28, 40, 0.75), rgba(20, 28, 40, 0.5));
  border-radius: calc(var(--cell) * 0.07);
}
.vehicle .glass.front { --s: 0.16; }
.vehicle .glass.rear { --s: 0.11; }

/* horizontal, facing right (+) or left (-) */
.vehicle.h .glass { top: 16%; bottom: 16%; width: calc(var(--cell) * var(--s)); }
.vehicle.h.fwd .glass.front { right: calc(var(--cell) * 0.24); }
.vehicle.h.fwd .glass.rear  { left: calc(var(--cell) * 0.16); }
.vehicle.h.back .glass.front { left: calc(var(--cell) * 0.24); }
.vehicle.h.back .glass.rear  { right: calc(var(--cell) * 0.16); }
/* vertical, facing down (+) or up (-) */
.vehicle.v .glass { left: 16%; right: 16%; height: calc(var(--cell) * var(--s)); }
.vehicle.v.fwd .glass.front { bottom: calc(var(--cell) * 0.24); }
.vehicle.v.fwd .glass.rear  { top: calc(var(--cell) * 0.16); }
.vehicle.v.back .glass.front { top: calc(var(--cell) * 0.24); }
.vehicle.v.back .glass.rear  { bottom: calc(var(--cell) * 0.16); }

/* Trucks: a cab with a windshield and a ribbed cargo box behind it */
.vehicle.truck .glass.rear { display: none; }
.vehicle.truck .cargo {
  position: absolute;
  border-radius: calc(var(--cell) * 0.1);
  background: repeating-linear-gradient(var(--rib), rgba(255, 255, 255, 0.14) 0 3px, transparent 3px 12px), rgba(0, 0, 0, 0.12);
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.12);
}
.vehicle.truck.h .cargo { --rib: 90deg; top: 9%; bottom: 9%; width: 64%; }
.vehicle.truck.v .cargo { --rib: 0deg; left: 9%; right: 9%; height: 64%; }
.vehicle.truck.h.fwd .cargo { left: 4%; }
.vehicle.truck.h.back .cargo { right: 4%; }
.vehicle.truck.v.fwd .cargo { top: 4%; }
.vehicle.truck.v.back .cargo { bottom: 4%; }
.vehicle.truck.h.fwd .glass.front { right: calc(var(--cell) * 0.2); }
.vehicle.truck.h.back .glass.front { left: calc(var(--cell) * 0.2); }
.vehicle.truck.v.fwd .glass.front { bottom: calc(var(--cell) * 0.2); }
.vehicle.truck.v.back .glass.front { top: calc(var(--cell) * 0.2); }

/* Target car gets a racing stripe */
.vehicle.target .body::after {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  top: 44%;
  height: 12%;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 99px;
  mix-blend-mode: soft-light;
}

/* Hint */
.vehicle.hinted .body { animation: hint-pulse 0.9s ease-in-out infinite; }
@keyframes hint-pulse {
  50% { box-shadow: 0 0 0 4px var(--gold), 0 6px 14px rgba(0, 0, 0, 0.4); }
}
.ghost-target {
  position: absolute;
  border: 3px dashed var(--gold);
  border-radius: calc(var(--cell) * 0.2);
  margin: calc(var(--cell) * 0.07);
  pointer-events: none;
  opacity: 0.85;
  z-index: 0;
}

@media (prefers-reduced-motion: reduce) {
  .vehicle, .vehicle.exiting { transition: none; }
  .exit span, .vehicle.hinted .body { animation: none; }
}

.loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  background: rgba(20, 23, 31, 0.6);
  border-radius: 22px;
  z-index: 5;
}
.loading[hidden] { display: none; }

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

.controls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.controls.secondary { grid-template-columns: 1fr 1fr; margin-top: -6px; }

.btn {
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--btn);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
  transition: background 0.12s, transform 0.08s;
  min-height: 44px;
}
.btn:hover:not(:disabled) { background: var(--btn-hover); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
.btn.icon { padding: 6px 12px; min-height: 36px; }

.help { color: var(--muted); font-size: 0.92rem; margin: 0; text-align: center; }
.red { color: var(--accent); }
.muted { color: var(--muted); margin: 0 0 10px; font-size: 0.9rem; }

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

dialog.sheet {
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(480px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(15, 18, 25, 0.55); backdrop-filter: blur(2px); }

.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet h2 { margin: 0; font-size: 1.3rem; }

.tier-block { margin-top: 16px; }
.tier-block h3 {
  margin: 0 0 8px;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}

.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.level-btn {
  position: relative;
  padding: 8px 0 6px;
  min-height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.1;
}
.level-btn .mini { font-size: 0.68rem; letter-spacing: 1px; color: var(--gold); min-height: 0.8rem; }
.level-btn.done { border-color: color-mix(in srgb, var(--gold) 60%, var(--line)); }
.level-btn.current { outline: 2px solid var(--accent); outline-offset: 1px; }

.random-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }

.win { text-align: center; }
.stars { font-size: 2.4rem; letter-spacing: 6px; color: var(--line); margin: 8px 0; }
.stars .on { color: var(--gold); display: inline-block; animation: pop 0.4s backwards; }
.stars .on:nth-child(2) { animation-delay: 0.12s; }
.stars .on:nth-child(3) { animation-delay: 0.24s; }
@keyframes pop { from { transform: scale(0); } 70% { transform: scale(1.3); } }
.win-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }

@media (max-width: 380px) {
  .stats div { min-width: 54px; padding: 4px 8px; }
  .level-grid { grid-template-columns: repeat(4, 1fr); }
}
