:root {
  color-scheme: dark;
  /* Autorally's own: a deep slate night desk under the light grid-paper board,
     chunky pressable controls, and the vermilion of car 1 for "go". --paper is
     the surface of cards and controls, --ink the text on them; --sheet is the
     board's paper and --bar the dark bars over it. */
  --desk: #17202a;
  --desk-glow: #1e2a36;
  --paper: #212c38;
  --raised: #2b3846;
  --raised-edge: #0f161e;
  --sheet: #fbf8f2;
  --bar: #111921;
  --ink: #f4f1ea;
  --muted: #94a3b2;
  --line: #2e3c4b;
  --hover: #283544;
  --accent: #e0532e;
  --accent-hover: #c8441f;
  --accent-ink: #ffffff;
  --go: #e0532e;
  --go-light: #f0714a;
  --go-edge: #9c3317;
  --success: #3cbfae;
  /* The four cars' pencil colours, used as accents. */
  --car1: #e0532e;
  --car2: #3a7bd5;
  --car3: #2a9d8f;
  --car4: #8e5bd0;
  --warn: #f4b400;
  --gold: #f6c343;
  --radius: 10px;
  --card-radius: 16px; /* board and panel share one corner radius */
  --appbar: 40px; /* height of the header with the logo and name */
  --space: 24px; /* outer margins and the gap between board and panel */
  --shadow: 0 12px 32px rgb(6 14 22 / 38%), 0 2px 6px rgb(6 14 22 / 22%);
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
}

* { box-sizing: border-box; }

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

/* Belt and braces: nothing on this page should ever scroll sideways. */
html, body { overflow-x: clip; }

body {
  /* A soft pool of light behind the board. */
  background: var(--desk);
  color: var(--ink);
  font: 600 15px/1.45 "Baloo 2", ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

.desk {
  display: grid;
  /* The board is as big as the window height allows (3:2), and the pair is
     centred, so the margins match all round. */
  grid-template-columns: minmax(0, calc((100vh - 3 * var(--space) - var(--appbar)) * 1.5)) 320px;
  justify-content: center;
  align-content: center; /* spare height goes equally above and below */
  gap: var(--space);
  align-items: stretch;
  padding: max(var(--space), env(safe-area-inset-top)) max(var(--space), env(safe-area-inset-right))
    max(var(--space), env(safe-area-inset-bottom)) max(var(--space), env(safe-area-inset-left));
  min-height: 100vh;
}

.sheet {
  position: relative;
  background: var(--sheet);
  border-radius: var(--card-radius);
  box-shadow: 0 5px 0 #1b1a18, var(--shadow);
  overflow: hidden;
  /* The sheet holds the 3:2 shape and the board just fills it. Leaving the
     shape to the canvas itself went wrong in Safari after rotating the phone. */
  aspect-ratio: 3 / 2;
  max-height: calc(100vh - 3 * var(--space) - var(--appbar));
  /* Its width sets its height (3:2); the panel then stretches to match. */
  align-self: start;
}

#board {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: manipulation;
}

#board.drawing { touch-action: none; cursor: crosshair; }
#board.picking { cursor: pointer; }

.panel {
  --panel-bg: var(--paper);
  background: var(--panel-bg);
  padding: var(--space);
  border-radius: var(--card-radius);
  box-shadow: var(--shadow);
  overflow-y: auto;
  /* Exactly as tall as the board beside it: its own content doesn't size the
     row, and anything longer scrolls inside. */
  contain: size;
}

/* Header: logo and name, across the top. */
.appbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--appbar);
  color: #fbf8f2;
}
.appbar img { width: 34px; height: 34px; border-radius: 9px; box-shadow: 0 3px 0 rgb(0 0 0 / 35%); }

h1 {
  margin: 0;
  font-size: 24px;
  letter-spacing: -0.01em;
  font-weight: 900;
  line-height: 1.1;
}

h2 {
  margin: 26px 0 10px;
  font-size: 18px;
  font-weight: 800;
  color: var(--ink);
}

p { margin: 10px 0; }

button, input[type="text"] {
  font: inherit;
  color: var(--ink);
  border: 0;
  border-radius: var(--radius);
  background: var(--raised);
  min-height: 44px;
  padding: 6px 14px;
}
input[type="text"] { background: var(--bar); box-shadow: inset 0 2px 0 rgb(0 0 0 / 25%); font-weight: 700; }
input[type="text"]::placeholder { color: #77716a; }

/* Chunky buttons: a darker bottom edge that squashes when pressed. */
button {
  cursor: pointer;
  font-weight: 800;
  box-shadow: 0 4px 0 var(--raised-edge);
  transition: transform 0.08s ease, box-shadow 0.08s ease, background-color 0.15s, filter 0.15s;
}
button:hover:not(:disabled):not([aria-disabled="true"]) { filter: brightness(1.08); }
button:active:not(:disabled):not([aria-disabled="true"]) { transform: translateY(3px); box-shadow: 0 1px 0 var(--raised-edge); }
button:disabled { opacity: 0.45; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button[aria-disabled="true"] { opacity: 0.45; cursor: default; }

select {
  flex: none;
  font: inherit;
  font-size: 16px; /* under 16px, iPhones zoom in when it's tapped */
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  min-height: 36px;
  padding: 4px;
}

/* The big green go button. */
button.primary {
  background: linear-gradient(var(--go-light), var(--go));
  color: #fff;
  font-weight: 900;
  font-size: 20px;
  min-height: 58px;
  border-radius: 12px;
  text-shadow: 0 2px 0 rgb(0 0 0 / 20%);
  box-shadow: 0 5px 0 var(--go-edge), 0 10px 24px rgb(0 0 0 / 35%);
}
button.primary:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 1px 0 var(--go-edge), 0 4px 10px rgb(0 0 0 / 30%); }
button.wide { display: block; width: 100%; margin-top: 12px; }

.row { display: flex; gap: 10px; margin-top: 12px; }
.row button { flex: 1; font-size: 14px; }

.track-list { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.track-pick {
  position: relative; /* anchors the hidden radio inside the tile */
  display: block;
  padding: 6px 6px 8px;
  font-size: 13px;
  line-height: 1.3;
  border: 2px solid transparent;
  border-radius: 12px;
  background: var(--raised);
  box-shadow: 0 4px 0 var(--raised-edge);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.18s var(--ease), box-shadow 0.18s var(--ease), border-color 0.18s;
}
.track-pick:hover { transform: translateY(-2px); }
.track-pick img { border-radius: 7px; }
.track-pick img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 3 / 2;
  margin-bottom: 4px;
}
.track-pick:has(input:checked) { border-color: var(--go); box-shadow: 0 4px 0 var(--go-edge); }
.track-pick:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.player-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.player-row { display: flex; align-items: center; gap: 10px; }
.player-row input { flex: 1; width: 0; min-width: 0; font-size: 16px; /* stops iPhone tap-zoom */ }

.swatch {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  color: #fff;
  font: 700 12px/1 system-ui, sans-serif;
}

.choice { display: flex; align-items: center; gap: 8px; min-height: 30px; cursor: pointer; }
.choice input { margin: 0; accent-color: var(--go); width: 18px; height: 18px; }

.slider { display: block; margin: 12px 0; }
.slider output { font-weight: 700; }
.slider input { display: block; width: 100%; margin-top: 4px; accent-color: var(--go); }

.message { min-height: 1.45em; color: #ff8a65; font-weight: 700; }

.turn { display: flex; align-items: center; gap: 8px; font-size: 19px; font-weight: 700; line-height: 1.2; margin: 4px 0; }
.speed { font-variant-numeric: tabular-nums; color: var(--muted); margin: 4px 0 10px; }
.hint { font-size: 13px; color: var(--muted); }



.standings { list-style: none; margin: 0; padding: 0; }
.standings li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}
.standings .who { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.standings .state { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; }

@media (max-width: 820px) {
  :root { --space: 14px; }
  body:not(.racing) .desk { align-content: start; }
  .desk { grid-template-columns: minmax(0, 1fr); }
  /* On a phone the sections sit straight on the charcoal; the tiles and
     buttons are the cards. */
  .panel { --panel-bg: var(--desk); contain: none; overflow-y: visible; padding: 4px 2px 8px; box-shadow: none; }
  .sheet { max-height: none; }
}

/* --- Campaign, leaderboard, results ------------------------------------- */

.tabs { display: flex; gap: 22px; margin: 4px 0 18px; }
.tabs button {
  flex: none;
  min-height: 36px;
  padding: 0 2px;
  border-radius: 0;
  background: none;
  box-shadow: none;
  border-bottom: 3px solid transparent;
  font-size: 18px;
  font-weight: 900;
  color: var(--muted);
  transition: color 0.2s, border-color 0.25s var(--ease);
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--go); }
.tabs button:hover:not(:disabled), .tabs button:active:not(:disabled) { transform: none; box-shadow: none; filter: none; color: var(--ink); }

.field { display: grid; gap: 6px; font-size: 13px; font-weight: 700; color: var(--muted); }
.field input { color: var(--ink); font-size: 16px; }

.level-list { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.level-pick {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 9px 2px 8px;
  border: 2px solid transparent;
  border-radius: 12px;
  background: var(--raised);
  box-shadow: 0 4px 0 var(--raised-edge);
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: transform 0.12s var(--ease), box-shadow 0.12s var(--ease), background-color 0.18s;
}
.level-pick:hover:not(.locked) { transform: translateY(-2px); }
.level-pick:active:not(.locked) { transform: translateY(3px); box-shadow: 0 1px 0 var(--raised-edge); }
.level-pick b { font-size: 20px; font-weight: 900; line-height: 1.1; }
.level-pick small { font-size: 11px; font-weight: 700; color: var(--muted); }
.level-pick .lock { display: block; width: 20px; height: 22px; color: var(--muted); }
.level-pick.locked { cursor: default; background: var(--paper); box-shadow: none; opacity: 0.55; }
.level-pick:has(input:checked) { border-color: var(--go); box-shadow: 0 4px 0 var(--go-edge); }
.level-pick:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.level-info { font-size: 13px; color: var(--muted); }

#show-scores { margin-top: 16px; }

.score-list { margin: 0; padding-left: 28px; }
.score-list li { padding: 5px 0; border-bottom: 1px solid var(--line); }
.score-list li > * { vertical-align: baseline; }
.score-list .who { display: inline-block; width: calc(100% - 92px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-list .state { display: inline-block; width: 30px; color: var(--muted); font-size: 13px; }
.score-list b { display: inline-block; width: 58px; text-align: right; font-variant-numeric: tabular-nums; }

.result-title { font-size: 24px; font-weight: 900; color: var(--ink); margin-top: 4px; }
.result-total { font-size: 28px; font-weight: 900; margin: 4px 0 8px; font-variant-numeric: tabular-nums; }
/* Results: turns against the best possible, then points out of the maximum,
   each part with its rule in small print. */
.result-race { display: flex; align-items: center; gap: 14px; margin: 8px 0 4px; }
.result-count { margin: 0; white-space: nowrap; color: var(--muted); }
.result-count b { font-size: 40px; line-height: 1; color: var(--ink); font-variant-numeric: tabular-nums; }
.result-compare { margin: 0; font-size: 14px; line-height: 1.35; }
.result-compare #result-record { display: block; color: var(--muted); }
.result-parts { display: grid; gap: 6px; margin: 0; font-size: 14px; }
.result-parts .part { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.result-parts .what b { font-weight: 600; margin-right: 6px; }
.result-parts .what small { color: var(--muted); font-size: 12px; }
.result-parts .points { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--muted); }
.result-parts .part.full .points { color: var(--success); font-weight: 600; }

/* Heads-up display over the top edge of the board. */
.hud {
  position: absolute;
  top: 6px;
  left: 50%;
  z-index: 1;
  transform: translateX(-50%);
  display: grid;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--bar) 92%, transparent);
  backdrop-filter: blur(4px);
  box-shadow: 0 4px 0 rgb(0 0 0 / 25%), 0 6px 16px rgb(0 0 0 / 25%);
  color: #fff;
  font: 800 14px/1.3 "Baloo 2", ui-rounded, system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  pointer-events: none;
}
/* A dot in the pencil colour of whoever the HUD is showing. */
.hud::before {
  content: '';
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--hud-color, #fff);
  box-shadow: 0 0 0 2px rgb(255 255 255 / 25%);
}
.hud-label { opacity: 0.7; font-weight: 500; }
.hud-row { display: flex; gap: 10px; align-items: center; }
/* The colour dot moves into the first row. */
.hud::before { display: none; }
.hud-row:first-child::before {
  content: '';
  flex: none;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--hud-color, #fff);
  box-shadow: 0 0 0 2px rgb(255 255 255 / 25%);
}
/* Desktop has the side panel for these; phones get them in the bar. */
.hud-actions, .hud-extra, .hud-log { display: none; }
.hud-speed b { display: inline-block; min-width: 2.6em; }
.hud-score { opacity: 0.9; }
/* Points and turns: side by side on a wide screen, stacked on a phone. */
.hud-pts:not([hidden]) + .hud-turns::before { content: ' · '; }

/* The main action is always in reach: Start sticks to the bottom of the
   screen (or of the panel) while the options above it scroll. */
#start-level, #start-race {
  position: sticky;
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 2;
  box-shadow: 0 5px 0 var(--go-edge), 0 0 0 8px var(--panel-bg), 0 10px 26px rgb(0 0 0 / 45%);
}

@media (max-width: 820px) {
  .level-list { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .level-pick { padding: 6px 2px; }
  .level-pick b { font-size: 18px; }
  .level-pick small { font-size: 10px; }
}

button.quiet {
  display: block;
  margin: 18px auto 0;
  min-height: 32px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
  box-shadow: none;
}
button.quiet:hover:not(:disabled), button.quiet:active:not(:disabled) { background: none; box-shadow: none; transform: none; color: var(--ink); }

.update {
  /* Centred with margins, not a transform: the slide-in animation uses
     transform and would otherwise push it off the right edge. */
  position: fixed;
  left: 0;
  right: 0;
  top: max(12px, env(safe-area-inset-top));
  width: max-content;
  max-width: calc(100vw - 24px);
  margin: 0 auto;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 10px 10px 18px;
  border-radius: 999px;
  background: var(--bar);
  color: #fff;
  box-shadow: var(--shadow);
  white-space: nowrap;
  animation: rise 0.3s var(--ease) both;
}
.update button.primary { min-height: 36px; font-size: 15px; border-radius: 999px; padding: 4px 18px; box-shadow: 0 3px 0 var(--go-edge); }

/* Sound on/off. */
.sound-toggle {
  min-height: 36px;
  min-width: 36px;
  padding: 0;
  border-radius: 999px;
  font-size: 16px;
  line-height: 1;
}
.appbar .sound-toggle { margin-left: auto; }
/* Little counters in the header: stars earned, records set. */
.appbar-stats { display: flex; gap: 14px; margin-left: auto; font-size: 17px; font-weight: 900; }
.appbar-stats + .sound-toggle { margin-left: 8px; }
.appbar-stats span {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 12px 4px 8px; border-radius: 999px;
  background: var(--paper); box-shadow: 0 3px 0 var(--raised-edge);
}
.appbar-stats svg { width: 18px; height: 18px; color: var(--warn); }
.race-actions { display: flex; gap: 10px; margin-top: 12px; }
.race-actions #new-race { flex: 1; }

/* Level stars on the tiles and the results screen. */
.tile-stars { font-size: 12px; letter-spacing: 1px; line-height: 1; min-height: 10px; }
.tile-stars i { font-style: normal; color: #57524c; }
.tile-stars i.on { color: var(--gold); }
.result-stars { list-style: none; margin: 12px 0 4px; padding: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.result-stars li { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; color: var(--muted); font-size: 12px; line-height: 1.25; }
.result-stars .mark {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 18px;
  color: #6e6862;
  background: var(--raised);
}
.result-stars li.earned { color: var(--ink); }
.result-stars li.earned .mark {
  color: #fff;
  background: var(--warn);
  box-shadow: 0 3px 10px color-mix(in srgb, var(--warn) 50%, transparent);
  animation: star-in 0.5s var(--ease) both;
  animation-delay: calc(0.45s + var(--i) * 0.38s);
}
@keyframes star-in {
  from { transform: scale(0.2) rotate(-40deg); opacity: 0; }
  60% { transform: scale(1.25) rotate(8deg); opacity: 1; }
  to { transform: none; }
}

/* Play here / Play online: the first choice on the setup screen. */
.place-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  margin: 0 0 18px;
  border-radius: 14px;
  background: var(--bar);
}
.place-switch button {
  min-height: 42px;
  border-radius: 11px;
  background: none;
  box-shadow: none;
  font-size: 16px;
  font-weight: 900;
  color: var(--muted);
}
.place-switch button:hover:not(:disabled), .place-switch button:active:not(:disabled) { transform: none; box-shadow: none; filter: none; color: var(--ink); }
.place-switch button[aria-selected="true"] { background: var(--raised); color: var(--ink); box-shadow: 0 3px 0 var(--raised-edge); }
#host-online { position: sticky; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 2; margin-top: 16px; }
#online-pane .join-code { margin-top: 22px; }

/* Hints off: the 💡 button looks switched off. */
.hint-off { opacity: 0.45; filter: grayscale(1); }

/* One-time tips for new drivers, at the bottom of the board. */
.tip {
  position: absolute;
  left: 0;
  right: 0;
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 2;
  width: min(420px, calc(100% - 24px));
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 12px 12px 16px;
  border-radius: 16px;
  background: var(--bar);
  color: #fff;
  box-shadow: 0 5px 0 rgb(0 0 0 / 30%), var(--shadow);
  animation: rise 0.3s var(--ease) both;
}
.tip p { margin: 0; flex: 1; font: 700 14px/1.4 "Baloo 2", ui-rounded, system-ui, sans-serif; }
.tip button { flex: none; min-height: 40px; border-radius: 10px; background: var(--go); box-shadow: 0 3px 0 var(--go-edge); color: #fff; }

/* The finish card, over the board when a free or online race ends. */
.finish {
  position: absolute;
  inset: 0;
  z-index: 3;
  width: min(360px, calc(100% - 32px));
  height: fit-content;
  margin: auto;
  padding: 22px 22px 18px;
  border-radius: var(--card-radius);
  background: var(--paper);
  box-shadow: 0 6px 0 var(--raised-edge), var(--shadow);
  animation: rise 0.35s var(--ease) both;
}
.finish-title { margin: 0; font-size: 30px; font-weight: 900; color: var(--ink); animation: cheer 0.6s cubic-bezier(0.2, 0.9, 0.3, 1.5) both; }
.finish-sub { margin: 2px 0 10px; color: var(--muted); font-size: 15px; }
.finish-sub:empty { display: none; }
@keyframes cheer {
  from { transform: scale(0.6); opacity: 0; }
  60% { transform: scale(1.08); opacity: 1; }
}
/* Your turns against the best possible. */
.finish-turns {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px;
  margin: 0 0 12px; padding: 10px 14px; border-radius: 12px;
  background: var(--bar); color: var(--muted); font-size: 14px;
}
.finish-turns b { font-size: 30px; line-height: 1; color: var(--ink); font-variant-numeric: tabular-nums; }
.finish-turns .who:empty { display: none; }
.finish-turns .who { color: var(--ink); font-weight: 800; }
.finish-turns .best { flex-basis: 100%; color: #ffb199; font-weight: 800; }
/* Rating change after a race. */
.rating-change { margin: 8px 0 0; font-weight: 800; font-size: 15px; color: var(--muted); }
.rating-change:empty { display: none; }
.rating-change.up { color: #3cbfae; }
.rating-change.down { color: #ff8a65; }
.appbar-stats .head-rating svg { color: #b48cf0; }

/* The coach: the Autorally car, with a speech bubble. */
.coach { display: flex; align-items: flex-end; gap: 10px; margin: 10px 0; }
.coach img { flex: none; width: 40px; height: 40px; border-radius: 10px; box-shadow: 0 3px 0 var(--raised-edge); }
.bubble {
  position: relative; flex: 1; margin: 0; padding: 9px 13px;
  border-radius: 14px 14px 14px 4px; background: var(--sheet); color: #1f2d3a;
  font-size: 15px; font-weight: 700; line-height: 1.35;
  animation: rise 0.35s var(--ease) both;
}
/* Move ratings: counts with an icon in the rating's colour. */
.ratings { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin: 4px 0 12px; }
.rating {
  display: grid; justify-items: center; gap: 0; padding: 6px 2px;
  min-height: 0; border-radius: 10px; background: var(--bar); box-shadow: none;
  color: var(--ink); font-weight: 800;
}
.rating i {
  display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%;
  font-style: normal; font-size: 14px; font-weight: 900; color: #fff; background: var(--rating);
}
.rating b { font-size: 20px; line-height: 1.2; color: var(--rating); }
.rating small { font-size: 11px; color: var(--muted); }
.rating.none { opacity: 0.4; }
button.rating:active:not(:disabled) { transform: translateY(1px); box-shadow: none; }

/* Reviewing: counts across the top, the step bar at the bottom. */
.review { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.review > * { pointer-events: auto; }
.review-counts {
  position: absolute; left: 8px; right: 8px; top: max(8px, env(safe-area-inset-top));
  margin: 0; padding: 6px; border-radius: 14px; background: color-mix(in srgb, var(--bar) 92%, transparent);
  box-shadow: 0 4px 0 rgb(0 0 0 / 25%);
}
.review-bar {
  position: absolute; left: 8px; right: 8px; bottom: max(10px, env(safe-area-inset-bottom));
  max-width: 520px; margin: 0 auto; padding: 12px; border-radius: 16px;
  background: var(--paper); color: var(--ink); box-shadow: 0 5px 0 var(--raised-edge), var(--shadow);
}
.review-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 18px; font-weight: 900; color: var(--rating); }
.badge i {
  display: grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
  font-style: normal; font-size: 15px; color: #fff; background: var(--rating);
}
.review-head .step { color: var(--muted); font-size: 14px; font-weight: 800; }
.review-text { margin: 6px 0 10px; font-size: 15px; line-height: 1.35; min-height: 2.7em; }
.review-nav { display: grid; grid-template-columns: 48px 48px 1fr 1fr; gap: 8px; }
.review-nav button { min-height: 44px; padding: 0 8px; font-size: 15px; }
.review-nav button.go { background: linear-gradient(var(--go-light), var(--go)); box-shadow: 0 4px 0 var(--go-edge); color: #fff; }
body.reviewing .hud, body.reviewing .tip { display: none !important; }
body.reviewing .panel #race { opacity: 0.4; }
#result { position: relative; }
#result .coach { margin-top: 4px; }

/* Confetti over the card. */
.finish { overflow: visible; }
.confetti { position: absolute; inset: -40px 0 auto; height: 0; pointer-events: none; }
.confetti i {
  position: absolute; top: 0; left: var(--x);
  width: 8px; height: 13px; border-radius: 2px; opacity: 0;
  animation: confetti var(--dur) cubic-bezier(0.25, 0.6, 0.4, 1) var(--delay) both;
}
.confetti i.round { width: 9px; height: 9px; border-radius: 50%; }
@keyframes confetti {
  0% { opacity: 1; transform: translate(0, 0) rotate(0); }
  100% { opacity: 0; transform: translate(var(--drift), 460px) rotate(var(--spin)); }
}
.finish-standings { list-style: none; margin: 0; padding: 0; }
.finish-standings li { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.finish-standings .place { width: 1.6em; font-weight: 800; color: var(--muted); font-variant-numeric: tabular-nums; }
.finish-standings .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.finish-standings .state { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.finish-standings li:first-child .who { font-weight: 800; }
.finish-note { margin: 10px 0 0; font-size: 14px; color: var(--muted); }
.finish-note:empty { display: none; }

.finish-note { white-space: pre-line; }

/* Results: Next level stays in reach; the other two share a row. */
#result-next { position: sticky; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 2; }
.result-more { margin-top: 10px; }
.result-more button { font-size: 16px; }

/* On phones the results don't need a full-size board: a strip of the track
   leaves the score and the buttons on screen. */
@media (max-width: 820px) {
  body[data-screen="result"] .sheet { height: 20vh; aspect-ratio: auto; max-height: none; }
  body[data-screen="result"] .result-total { font-size: 24px; margin: 6px 0 4px; }
  /* One line per part, small print beside the label. */
  body[data-screen="result"] .result-parts { font-size: 13px; gap: 3px; }
  body[data-screen="result"] .result-count b { font-size: 34px; }
  body[data-screen="result"] .bubble { font-size: 14px; padding: 7px 11px; }
  body[data-screen="result"] .rating { grid-template-columns: auto auto; column-gap: 5px; align-items: center; padding: 5px 4px; }
  body[data-screen="result"] .rating i { width: 20px; height: 20px; font-size: 12px; }
  body[data-screen="result"] .rating b { font-size: 17px; }
  body[data-screen="result"] .rating small { grid-column: 1 / -1; font-size: 10px; }
  body[data-screen="result"] #result-unlock, body[data-screen="result"] #result-online { font-size: 13px; margin: 6px 0; }
}

/* --- Home screen on a phone: the action first. Free races and online show
   no big map (the track tiles are the map); the campaign keeps a strip of
   the selected level. --- */
@media (max-width: 820px) {
  body[data-screen="setup"][data-tab="free"] .sheet,
  body[data-screen="setup"][data-tab="online"] .sheet { display: none; }
  body[data-screen="setup"][data-tab="campaign"] .sheet { height: 26vh; min-height: 150px; aspect-ratio: auto; }
  body[data-screen="setup"] .hero { padding: 6px 12px 7px; }
  body[data-screen="setup"] .hero-title { font-size: 21px; }
}
/* Numbered steps. */
h2.step { display: flex; align-items: center; gap: 10px; }
h2.step i {
  display: grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
  font-style: normal; font-size: 15px; font-weight: 900; color: #fff;
  background: var(--go); box-shadow: 0 3px 0 var(--go-edge);
}
#start-race { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* --- Home screen: banner over the board, and the stats row. --- */
.hero {
  position: absolute;
  left: 12px;
  top: 12px;
  z-index: 1;
  display: grid;
  padding: 8px 14px 9px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--bar) 88%, transparent);
  box-shadow: 0 4px 0 rgb(0 0 0 / 25%);
  color: #fff;
  pointer-events: none;
  animation: rise 0.4s var(--ease) both;
}
body:not([data-screen="setup"]) .hero { display: none; }
.hero-kicker { font-size: 12px; font-weight: 800; color: #ffb199; text-transform: uppercase; letter-spacing: 0.06em; }
.hero-title { font-size: 26px; font-weight: 900; line-height: 1.1; }

.section-head { display: flex; align-items: baseline; justify-content: space-between; margin: 22px 0 10px; }
.section-head h2 { margin: 0; }
button.link {
  min-height: 0;
  padding: 0;
  background: none;
  box-shadow: none;
  color: var(--muted);
  font-size: 16px;
  font-weight: 800;
}
button.link:hover:not(:disabled), button.link:active:not(:disabled) { transform: none; box-shadow: none; color: var(--ink); }
.stat-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
/* Progress tiles: each topped with a stripe in one car's colour, like lanes. */
.stat {
  --lane: var(--car1);
  position: relative;
  overflow: hidden;
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 14px 4px 10px;
  border-radius: 12px;
  background: var(--raised);
  box-shadow: 0 4px 0 var(--raised-edge);
  animation: rise 0.4s var(--ease) both;
}
.stat::before { content: ''; position: absolute; inset: 0 0 auto; height: 4px; background: var(--lane); }
.stat:nth-child(1) { --lane: #f4b400; }
.stat:nth-child(2) { --lane: var(--car3); }
.stat:nth-child(3) { --lane: var(--car2); }
.stat:nth-child(4) { --lane: var(--car4); }
.stat-icon svg { display: block; width: 26px; height: 26px; color: var(--lane); }
.stat:nth-child(2) { animation-delay: 0.05s; }
.stat:nth-child(3) { animation-delay: 0.1s; }
.stat:nth-child(4) { animation-delay: 0.15s; }
.stat-icon { line-height: 1; margin-bottom: 2px; }
.stat b { font-size: 22px; font-weight: 900; line-height: 1.15; font-variant-numeric: tabular-nums; }
.stat small { font-size: 12px; font-weight: 800; color: var(--muted); }
#campaign-pane .field { margin-top: 16px; }
/* The desktop side panel is narrow: stats two by two. */
@media (min-width: 821px) {
  .stat-row { grid-template-columns: 1fr 1fr; }
  .stat { grid-template-columns: auto 1fr; grid-template-rows: auto auto; justify-items: start; column-gap: 10px; padding: 10px 12px; }
  .stat-icon { grid-row: span 2; align-self: center; }
  .stat b { font-size: 20px; }
}
#campaign-pane > h2:first-child, #local-pane .tabs + div > h2:first-child { margin-top: 0; }

/* Online lobby. */
.share-row { display: flex; gap: 8px; }
.share-row input { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); }
.lobby-bots { display: flex; align-items: center; gap: 10px; margin-top: 14px; }
.lobby-bots span { flex: 1; color: var(--muted); font-size: 14px; }
.lobby-bots button { min-width: 40px; padding: 0; font-size: 18px; }
.lobby-bots b { min-width: 1.5em; text-align: center; font-variant-numeric: tabular-nums; }
#lobby-players .you { font-weight: 700; }
.room-code {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "label copy" "value copy";
  align-items: center;
  column-gap: 12px;
  padding: 10px 14px;
  border-radius: 14px;
  background: var(--bar);
}
.room-code-label { grid-area: label; font-size: 12px; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; }
.room-code-value {
  grid-area: value;
  font: 800 26px/1.2 ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.08em;
  user-select: all;
}
.room-code button { grid-area: copy; }
/* A disabled sticky Start button sits over other things, so keep it solid. */
#lobby-start:disabled, #start-level:disabled, #start-race:disabled, #host-online:disabled {
  opacity: 1;
  background: #4a4743;
  color: #8d8780;
  text-shadow: none;
  box-shadow: 0 5px 0 var(--raised-edge), 0 0 0 8px var(--panel-bg);
}
.join-code { margin-top: 14px; }
.join-code label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.join-code input { font-size: 16px; color: var(--ink); }
.join-code .message { margin: 6px 0 0; font-size: 13px; }
#lobby-start { position: sticky; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 2; }

/* --- Motion: small and quick; all of it off when the device asks for less. */

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
@keyframes pop {
  40% { transform: scale(1.18); }
}
.panel > section { animation: rise 0.32s var(--ease) both; }
.hud b, .hud-score > span { display: inline-block; }
.hud .pop { animation: pop 0.3s var(--ease); }
.message:not(:empty) { animation: rise 0.25s var(--ease) both; }
.result-total { animation: pop 0.5s var(--ease) 0.15s both; }

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

/* Racing on a phone: the track fills the whole screen and nothing scrolls,
   upright or sideways. The controls float on top as a small card, which
   main.js keeps in whichever corner is clear of the cars (see placeCard). */
@media (max-width: 820px), (orientation: landscape) and (max-height: 520px) {
  body.racing { height: 100dvh; overflow: hidden; overscroll-behavior: none; }
  body.racing .desk { display: block; height: 100dvh; min-height: 0; padding: 0; }
  body.racing .appbar { display: none; }
  body.racing .sheet {
    position: fixed;
    inset: 0;
    align-self: stretch; /* undo the desktop 'start', which collapses a fixed box */
    aspect-ratio: auto;
    max-height: none;
    box-shadow: none;
    border-radius: 0;
  }
  body.racing #board { touch-action: none; }

  body.racing .panel {
    contain: none; /* the desktop height-matching would collapse the card */
    position: fixed;
    left: var(--card-x, 8px);
    top: var(--card-y, 8px);
    z-index: 2;
    width: 168px;
    max-height: none;
    padding: 8px;
    overflow: hidden;
    border-radius: 12px;
    background: color-mix(in srgb, var(--paper) 96%, transparent);
    backdrop-filter: blur(6px);
    box-shadow: var(--shadow);
    transition: left 0.25s ease, top 0.25s ease;
  }
  body.racing h1, body.racing .hint, body.racing #race h2 { display: none; }
  body.racing #race { display: flex; flex-direction: column; }
  body.racing #turn {
    font-size: 14px;
    margin: 0;
    gap: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
  }
  body.racing #turn .swatch { width: 16px; height: 16px; font-size: 10px; vertical-align: -3px; margin-right: 4px; }
  body.racing #speed { font-size: 12px; margin: 0 0 6px; }
  body.racing #log {
    margin: 6px 0 0;
    min-height: 0;
    font-size: 12px;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  body.racing #log:empty { display: none; }
  /* Standings as a row of badges: colour, number and progress. */
  body.racing #standings { display: flex; flex-wrap: wrap; gap: 2px 8px; margin-top: 6px; }
  body.racing .standings li { padding: 0; border: 0; gap: 3px; font-size: 11px; }
  body.racing .standings .who { display: none; }
  body.racing .standings .state { font-size: 11px; }
  body.racing .standings .swatch { width: 15px; height: 15px; font-size: 9px; }
  body.racing .race-actions { margin-top: 6px; gap: 6px; }
  body.racing #new-race, body.racing #mute-race { min-height: 30px; padding: 2px 8px; font-size: 13px; }
  body.racing #mute-race { min-width: 30px; }
}

/* Phone race: one bar on top holds everything. The floating card goes away. */
@media (max-width: 820px), (orientation: landscape) and (max-height: 520px) {
  body.racing .panel { display: none; }
  body.racing .hud {
    top: max(8px, env(safe-area-inset-top)) !important; /* pinned to the top of the screen */
    left: 8px;
    right: 8px;
    transform: none;
    border-radius: 18px;
    padding: 6px 6px 6px 12px;
    font-size: 14px;
    pointer-events: auto;
  }
  body.racing .hud-row { min-width: 0; gap: 8px; }
  body.racing .hud-speed { flex: none; }
  /* The score gives way first ("…"); the buttons always stay on screen. */
  body.racing .hud-score { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-end; font-size: 13px; line-height: 1.25; white-space: nowrap; }
  body.racing .hud-score > span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
  body.racing .hud-pts:not([hidden]) + .hud-turns::before { content: none; }
  body.racing .hud-actions { display: flex; flex: none; gap: 6px; margin-left: auto; }
  body.racing .hud-extra { display: flex; gap: 10px; padding-right: 6px; }
  body.racing .hud-standings { flex-wrap: wrap; justify-content: flex-end; flex: 0 1 auto; min-width: 0; }
  body.racing .hud-log:not([hidden]) {
    display: block;
    margin: 0;
    padding: 4px 6px 2px 0;
    font: 700 13px/1.35 "Baloo 2", ui-rounded, system-ui, sans-serif;
    color: #ffd7c8;
    white-space: normal;
    animation: rise 0.25s var(--ease) both;
  }
  .hud-btn {
    min-height: 32px;
    min-width: 32px;
    padding: 0 8px;
    border-radius: 10px;
    background: #3d3a36;
    box-shadow: 0 3px 0 #141312;
    color: #fff;
    font: 800 16px/1 "Baloo 2", ui-rounded, system-ui, sans-serif;
  }
  .hud-turn { font-weight: 500; opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; }
  .hud-standings { display: flex; gap: 8px; list-style: none; margin: 0; padding: 0; flex: none; }
  .hud-standings li { display: flex; align-items: center; gap: 3px; font-size: 12px; opacity: 0.85; }
  .hud-standings li.current { opacity: 1; font-weight: 700; }
  .hud-standings .swatch { width: 16px; height: 16px; font-size: 10px; box-shadow: 0 0 0 1.5px rgb(255 255 255 / 35%); }
  .hud-standings li.current .swatch { box-shadow: 0 0 0 2px #fff; }
}

/* Upright phones: the card is wide and short, sitting in the blank band of
   paper above or below the board. */
@media (max-width: 820px) and (orientation: portrait) {
  body.racing .panel { width: min(calc(100vw - 16px), 420px); }
  body.racing #race {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "turn actions" "speed actions" "standings standings" "log log";
    column-gap: 12px;
    align-items: center;
  }
  body.racing #turn { grid-area: turn; }
  body.racing #speed { grid-area: speed; margin-bottom: 0; }
  body.racing #standings { grid-area: standings; }
  body.racing #log { grid-area: log; }
  body.racing .race-actions { grid-area: actions; margin: 0; }
}

@media (prefers-reduced-motion: reduce) {
  body.racing .panel { transition: none; }
}
