:root {
  --ink: #1b1430;
  --ink-2: #2b2347;
  --panel: rgba(27, 20, 48, 0.9);
  --cream: #fff4e0;
  --coral: #ff6f61;
  --teal: #2ec4b6;
  --butter: #ffd166;
  --lilac: #b39ddb;
  --cyan: #5ff4ff;
  --pink: #ff8fc7;
  --muted: #a99bc9;
  --display: 'Press Start 2P', monospace;
  --body: 'Silkscreen', monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ink);
  color: var(--cream);
  font-family: var(--body);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app { position: fixed; inset: 0; display: grid; place-items: center; }

#game {
  position: absolute;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
}

button { font: inherit; color: inherit; cursor: pointer; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.noscript {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 24px;
  font-family: var(--display);
  font-size: 14px;
  line-height: 1.6;
  text-align: center;
  color: var(--butter);
}
[hidden] { display: none !important; }

/* ---------- screens ---------- */

#screens { position: absolute; inset: 0; pointer-events: none; }
.screen {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  pointer-events: auto;
  overflow-y: auto;
}
.screen.active { display: flex; animation: pop-in 0.18s steps(4); }
.screen.dim { background: rgba(14, 9, 30, 0.55); }
@keyframes pop-in { from { opacity: 0; transform: translateY(6px); } }

.panel {
  background: var(--panel);
  border: 3px solid var(--cream);
  box-shadow: 6px 6px 0 var(--coral), 0 0 0 3px var(--ink);
  padding: 18px 20px;
  width: min(420px, 100%);
  margin: auto;
}
.panel.wide { width: min(760px, 100%); }
.panel.narrow { width: min(400px, 100%); }
.panel h2 {
  font-family: var(--display);
  font-size: 16px;
  margin: 0 0 14px;
  color: var(--butter);
  text-shadow: 2px 2px 0 var(--coral);
  letter-spacing: 1px;
}
.sub { margin: -6px 0 12px; color: var(--muted); }

.menu { display: flex; flex-direction: column; gap: 8px; }
.menu button, .primary, .ghost {
  font-family: var(--display);
  font-size: 12px;
  padding: 12px 16px;
  border: 3px solid var(--cream);
  background: var(--ink-2);
  box-shadow: 4px 4px 0 var(--ink);
  text-align: left;
  letter-spacing: 1px;
  transition: transform 0.05s;
}
.menu button:hover, .menu button:focus-visible, .primary:hover, .ghost:hover,
.primary:focus-visible, .ghost:focus-visible {
  outline: none;
  background: var(--coral);
  color: var(--ink);
  transform: translate(-2px, -2px);
  box-shadow: 6px 6px 0 var(--ink);
}
.menu button:focus-visible::before { content: '\25B6  '; }
.primary { background: var(--teal); color: var(--ink); text-align: center; }
.ghost { background: transparent; text-align: center; }
.row { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

/* ---------- boot / loading ---------- */

.boot, .loading { text-align: center; }
.bar { width: 220px; height: 12px; border: 3px solid var(--cream); margin: 18px auto 10px; background: var(--ink-2); }
.bar i { display: block; height: 100%; width: 0; background: var(--butter); transition: width 0.1s steps(6); }
.spinner { display: flex; gap: 8px; justify-content: center; }
.spinner i { width: 14px; height: 14px; background: var(--coral); animation: hopdot 0.6s steps(3) infinite; box-shadow: 3px 3px 0 var(--ink); }
.spinner i:nth-child(2) { background: var(--butter); animation-delay: 0.1s; }
.spinner i:nth-child(3) { background: var(--teal); animation-delay: 0.2s; }
@keyframes hopdot { 50% { transform: translateY(-10px); } }
.loading p, .boot p { color: var(--cream); text-shadow: 2px 2px 0 var(--ink); }

/* ---------- title ---------- */

.title-wrap { position: relative; display: flex; flex-direction: column; align-items: flex-start; margin-right: auto; margin-left: max(4vw, 0px); }
.logo {
  font-family: var(--display);
  font-size: clamp(28px, 6.4vw, 64px);
  line-height: 1.05;
  margin: 0;
  color: var(--butter);
  text-shadow: 3px 3px 0 var(--coral), 6px 6px 0 var(--ink), 9px 9px 0 var(--teal);
  letter-spacing: 2px;
}
.logo span { color: var(--cream); font-size: 0.86em; }
.logo.small { font-size: 28px; }
.tag {
  margin: 14px 0 18px;
  font-size: 14px;
  background: var(--ink);
  padding: 4px 8px;
  box-shadow: 3px 3px 0 var(--coral);
}
.title-wrap .menu { width: 260px; }
.starburst {
  position: absolute;
  left: -30px; top: -34px;
  width: 64px; height: 64px;
  background:
    linear-gradient(0deg, transparent 45%, var(--cream) 45% 55%, transparent 55%),
    linear-gradient(90deg, transparent 45%, var(--cream) 45% 55%, transparent 55%),
    linear-gradient(45deg, transparent 47%, var(--butter) 47% 53%, transparent 53%),
    linear-gradient(-45deg, transparent 47%, var(--butter) 47% 53%, transparent 53%);
  animation: twinkle 2.4s steps(6) infinite;
}
@keyframes twinkle { 50% { transform: rotate(45deg) scale(0.7); } }

.pilot-card {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 6px;
  background: var(--panel);
  border: 3px solid var(--cream);
  box-shadow: 4px 4px 0 var(--teal);
}
.pilot-card:hover, .pilot-card:focus-visible { outline: none; box-shadow: 4px 4px 0 var(--coral); }
.pilot-card canvas { width: 120px; height: 70px; image-rendering: pixelated; }
.pilot-card span { display: flex; flex-direction: column; text-align: left; }
.pilot-card b { font-family: var(--display); font-size: 11px; color: var(--butter); }
.pilot-card small { color: var(--muted); margin-top: 4px; }

/* ---------- garage ---------- */

.garage {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(280px, 1fr);
  gap: 16px;
  width: min(1060px, 100%);
  align-items: center;
  margin: auto;
}
.showroom { position: relative; display: flex; flex-direction: column; align-items: center; }
#garage-canvas {
  width: 100%;
  aspect-ratio: 5 / 3;
  image-rendering: pixelated;
  border: 3px solid var(--cream);
  box-shadow: 6px 6px 0 var(--teal), 0 0 0 3px var(--ink);
  cursor: grab;
  touch-action: none;
}
#garage-canvas:active { cursor: grabbing; }
.body-pick { display: flex; align-items: center; gap: 10px; margin-top: 14px; width: 100%; }
.body-name { flex: 1; text-align: center; background: var(--panel); border: 3px solid var(--cream); padding: 8px; }
.body-name b { display: block; font-family: var(--display); font-size: 14px; color: var(--butter); }
.body-name small { display: block; margin-top: 6px; color: var(--cream); font-size: 12px; }
.arrow {
  font-size: 18px; width: 48px; height: 48px;
  border: 3px solid var(--cream); background: var(--ink-2); box-shadow: 3px 3px 0 var(--ink);
}
.arrow:hover, .arrow:focus-visible { outline: none; background: var(--coral); color: var(--ink); }
.hint { margin: 8px 0 0; font-size: 11px; color: var(--muted); text-shadow: 1px 1px 0 var(--ink); }

.g-panel { width: 100%; max-height: calc(100vh - 32px); overflow-y: auto; }
.stats { display: grid; grid-template-columns: auto 1fr; gap: 6px 10px; margin-bottom: 12px; align-items: center; }
.stats span { font-size: 12px; color: var(--muted); }
.stats .pips { display: flex; gap: 3px; }
.stats .pips i { flex: 1; height: 10px; background: var(--ink-2); border: 2px solid var(--muted); }
.stats .pips i.on { background: var(--butter); border-color: var(--butter); }
.opt { display: grid; grid-template-columns: 82px 1fr; align-items: center; gap: 8px; margin: 8px 0; }
.opt label { font-size: 12px; color: var(--muted); }
.swatches, .chips { display: flex; flex-wrap: wrap; gap: 5px; }
.sw {
  width: 24px; height: 24px; border: 2px solid var(--ink); box-shadow: 0 0 0 2px var(--ink-2);
  padding: 0;
}
.sw[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--cream), 3px 3px 0 2px var(--coral); }
.sw:focus-visible { outline: 2px dashed var(--cream); outline-offset: 2px; }
.chip {
  font-size: 12px; padding: 5px 8px; border: 2px solid var(--muted); background: transparent;
}
.chip[aria-pressed="true"] { background: var(--butter); color: var(--ink); border-color: var(--butter); }
.chip:focus-visible { outline: 2px dashed var(--cream); outline-offset: 2px; }
.opt input {
  font: inherit; font-size: 14px; color: var(--cream); background: var(--ink-2);
  border: 2px solid var(--muted); padding: 6px 8px; text-transform: uppercase; width: 100%;
  -webkit-user-select: text; user-select: text;
}
.opt input:focus { outline: none; border-color: var(--butter); }

/* ---------- track select ---------- */

.tracks { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.track-card {
  text-align: left; padding: 0; border: 3px solid var(--muted); background: var(--ink-2);
  display: flex; flex-direction: column;
}
.track-card canvas { width: 100%; aspect-ratio: 16 / 9; image-rendering: pixelated; display: block; background: var(--ink); }
.track-card b { font-family: var(--display); font-size: 10px; padding: 8px 8px 4px; color: var(--butter); line-height: 1.4; }
.track-card small { padding: 0 8px 8px; font-size: 11px; color: var(--muted); }
.track-card[aria-pressed="true"] { border-color: var(--butter); box-shadow: 4px 4px 0 var(--coral); }
.track-card:focus-visible { outline: 2px dashed var(--cream); outline-offset: 3px; }
.cup-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.cup-list .track-card { pointer-events: none; border-color: var(--lilac); }
.settings-row { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 14px; }
.seg { display: flex; border: 3px solid var(--cream); }
.seg button { padding: 8px 10px; background: transparent; border: none; font-size: 12px; }
.seg button + button { border-left: 2px solid var(--muted); }
.seg button[aria-checked="true"] { background: var(--teal); color: var(--ink); }
.seg button:focus-visible { outline: 2px dashed var(--cream); outline-offset: -4px; }

/* ---------- results ---------- */

table.results { width: 100%; border-collapse: collapse; font-size: 13px; }
.results th { text-align: left; color: var(--muted); font-weight: 400; padding: 4px 6px; border-bottom: 2px solid var(--muted); }
.results td { padding: 5px 6px; border-bottom: 1px dashed rgba(169, 155, 201, 0.35); white-space: nowrap; }
.results td.num { text-align: right; font-variant-numeric: tabular-nums; }
.results tr.me td { background: rgba(255, 209, 102, 0.16); color: var(--butter); }
.results .chip-car { display: inline-block; width: 10px; height: 10px; margin-right: 6px; border: 2px solid var(--ink); vertical-align: -1px; }
.results .place { font-family: var(--display); font-size: 11px; }
#podium-canvas { width: 100%; aspect-ratio: 2 / 1; image-rendering: pixelated; margin-bottom: 10px; border: 3px solid var(--cream); }

/* ---------- settings / help ---------- */

.toggles { display: flex; flex-direction: column; gap: 10px; }
.tog { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.tog input { appearance: none; width: 34px; height: 18px; border: 2px solid var(--cream); background: var(--ink-2); position: relative; margin: 0; cursor: pointer; }
.tog input::after { content: ''; position: absolute; top: 2px; left: 2px; width: 10px; height: 10px; background: var(--muted); }
.tog input:checked { background: var(--teal); }
.tog input:checked::after { left: 18px; background: var(--ink); }
.tog input:focus-visible { outline: 2px dashed var(--cream); outline-offset: 2px; }
.slider { display: flex; align-items: center; gap: 10px; }
.slider input { flex: 1; accent-color: var(--butter); }
table.keys { width: 100%; border-collapse: collapse; font-size: 12px; }
.keys th { color: var(--muted); font-weight: 400; text-align: left; padding: 3px 4px; }
.keys td { padding: 4px; border-top: 1px dashed rgba(169, 155, 201, 0.35); }
.keys td:first-child { color: var(--butter); }
.tips { padding-left: 18px; font-size: 12px; line-height: 1.5; color: var(--cream); }

/* ---------- HUD ---------- */

#hud { position: absolute; inset: 0; pointer-events: none; font-family: var(--display); text-shadow: 2px 2px 0 var(--ink); }
#hud .k { color: var(--muted); }
.hud-tl { position: absolute; left: max(14px, env(safe-area-inset-left)); top: 12px; }
.hud-pos { font-size: clamp(26px, 5vw, 44px); color: var(--butter); text-shadow: 3px 3px 0 var(--coral), 5px 5px 0 var(--ink); }
.hud-pos #h-suffix { font-size: 0.45em; margin-left: 2px; }
.hud-lap { font-size: 12px; margin-top: 6px; }
.hud-tr { position: absolute; right: max(14px, env(safe-area-inset-right)); top: 12px; text-align: right; font-size: 11px; line-height: 1.8; font-variant-numeric: tabular-nums; }
.hud-strip { position: absolute; left: 50%; top: 14px; width: min(34vw, 320px); height: 14px; transform: translateX(-50%); }
.strip-line { position: absolute; left: 0; right: 0; top: 6px; height: 3px; background: var(--cream); box-shadow: 2px 2px 0 var(--ink); }
#h-dots i { position: absolute; top: 3px; width: 8px; height: 8px; margin-left: -4px; border: 2px solid var(--ink); }
#h-dots i.me { top: 0; width: 12px; height: 12px; margin-left: -6px; border-color: var(--cream); z-index: 2; }
.hud-bl { position: absolute; left: max(14px, env(safe-area-inset-left)); bottom: 14px; }
.hud-speed { font-size: clamp(20px, 3.6vw, 30px); color: var(--cream); }
.hud-speed small { font-size: 9px; margin-left: 6px; color: var(--muted); }
.hud-boost { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 9px; }
.boost-track { width: min(28vw, 180px); height: 12px; border: 3px solid var(--cream); background: rgba(27, 20, 48, 0.7); box-shadow: 2px 2px 0 var(--ink); }
#h-boost { height: 100%; width: 0; background: repeating-linear-gradient(90deg, var(--cyan) 0 8px, #b8fbff 8px 10px); }
#h-boost.full { animation: blink 0.4s steps(2) infinite; }
#h-boost.on { background: repeating-linear-gradient(90deg, var(--pink) 0 8px, var(--butter) 8px 10px); }
@keyframes blink { 50% { opacity: 0.55; } }

#h-count {
  position: absolute; left: 50%; top: 32%; transform: translate(-50%, -50%);
  font-size: clamp(40px, 10vw, 96px); color: var(--butter);
  text-shadow: 4px 4px 0 var(--coral), 8px 8px 0 var(--ink);
}
#h-count.pulse { animation: count-pop 0.6s steps(6) forwards; }
@keyframes count-pop { 0% { transform: translate(-50%, -50%) scale(1.6); } 30% { transform: translate(-50%, -50%) scale(1); } 100% { opacity: 0; } }
#h-lights { position: absolute; left: 50%; top: 14%; transform: translateX(-50%); display: flex; gap: 10px; padding: 8px 10px; background: var(--ink); border: 3px solid var(--cream); }
#h-lights i { width: 20px; height: 20px; background: #4a2240; border: 2px solid #000; }
#h-lights i.red { background: var(--coral); box-shadow: 0 0 12px var(--coral); }
#h-lights i.green { background: #7dff8a; box-shadow: 0 0 14px #7dff8a; }
#h-msg {
  position: absolute; left: 50%; top: 44%; transform: translateX(-50%);
  font-size: clamp(14px, 3vw, 26px); color: var(--cream); white-space: nowrap;
  text-shadow: 3px 3px 0 var(--coral), 5px 5px 0 var(--ink); opacity: 0;
}
#h-msg.show { animation: msg 1.8s steps(12) forwards; }
#h-msg.good { color: #7dff8a; }
#h-msg.big { color: var(--butter); }
@keyframes msg { 0% { opacity: 0; transform: translate(-50%, 12px); } 12% { opacity: 1; transform: translate(-50%, 0); } 80% { opacity: 1; } 100% { opacity: 0; } }
#h-ticker {
  position: absolute; left: 50%; bottom: 18%; transform: translateX(-50%);
  font-size: 11px; color: var(--cyan); opacity: 0;
}
#h-ticker.show { animation: msg 1.1s steps(8) forwards; }

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

#touch { position: absolute; inset: 0; pointer-events: none; }
.t-btn {
  position: absolute;
  pointer-events: auto;
  touch-action: none;
  font-family: var(--display);
  font-size: 11px;
  background: rgba(27, 20, 48, 0.45);
  border: 3px solid rgba(255, 244, 224, 0.75);
  color: var(--cream);
  box-shadow: 3px 3px 0 rgba(27, 20, 48, 0.6);
}
.t-btn.on { background: rgba(255, 111, 97, 0.7); }
.t-left, .t-right { bottom: max(18px, env(safe-area-inset-bottom)); width: 17vw; max-width: 120px; height: 34vh; max-height: 170px; font-size: 22px; }
.t-left { left: max(14px, env(safe-area-inset-left)); }
.t-right { left: calc(max(14px, env(safe-area-inset-left)) + min(17vw, 120px) + 10px); }
.t-boost { right: max(14px, env(safe-area-inset-right)); bottom: max(18px, env(safe-area-inset-bottom)); width: 22vw; max-width: 140px; height: 22vh; max-height: 110px; background: rgba(95, 244, 255, 0.25); }
.t-hop { right: max(14px, env(safe-area-inset-right)); bottom: calc(max(18px, env(safe-area-inset-bottom)) + min(22vh, 110px) + 10px); width: 22vw; max-width: 140px; height: 16vh; max-height: 80px; }
.t-brake { right: calc(max(14px, env(safe-area-inset-right)) + min(22vw, 140px) + 10px); bottom: max(18px, env(safe-area-inset-bottom)); width: 14vw; max-width: 90px; height: 16vh; max-height: 80px; }
.t-gas { right: calc(max(14px, env(safe-area-inset-right)) + min(22vw, 140px) + 10px); bottom: calc(max(18px, env(safe-area-inset-bottom)) + min(16vh, 80px) + 10px); width: 14vw; max-width: 90px; height: 16vh; max-height: 80px; }
.t-pause { left: 50%; transform: translateX(-50%); top: 36px; width: 44px; height: 36px; }
#touch.auto .t-gas { display: none; }
body.touch .hud-strip { top: 82px; }
body.touch .hud-bl { bottom: auto; top: calc(12px + clamp(26px, 5vw, 44px) + 30px); }
body.touch .hud-speed { font-size: 16px; }
body.touch .boost-track { width: min(22vw, 140px); }

#rotate {
  position: absolute; inset: 0; z-index: 10; display: grid; place-items: center;
  background: var(--ink); font-family: var(--display); font-size: 14px; text-align: center; padding: 24px; line-height: 1.6;
}

/* ---------- small screens ---------- */

@media (max-width: 760px) {
  .garage { grid-template-columns: 1fr; }
  .g-panel { max-height: none; }
  .title-wrap { margin-left: 0; }
  .title-wrap .menu { width: 220px; }
  .menu button { font-size: 11px; padding: 10px 12px; }
  .pilot-card canvas { width: 84px; height: 49px; }
}
@media (max-height: 480px) {
  .logo { font-size: clamp(22px, 7vh, 40px); }
  .tag { margin: 8px 0 10px; font-size: 12px; }
  .menu { gap: 5px; }
  .menu button { padding: 7px 12px; font-size: 10px; }
  .panel { padding: 12px 14px; }
  .panel h2 { margin-bottom: 8px; font-size: 13px; }
  .starburst { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .screen.active, .starburst, .spinner i, #h-boost.full { animation: none; }
}
