:root {
  --ink: #10151c;
  --ink-2: #0b0f14;
  --steel: #1e2a36;
  --steel-2: #18222c;
  --panel: #2f4152;
  --panel-2: #263543;
  --concrete: #d8dee4;
  --amber: #f2b134;
  --cyan: #2ec4b6;
  --lcd: #9ee06a;
  --paper: #fbf3e4;
  --muted: #9fb2c1;
  --line: #0a0e13;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--ink-2);
  color: var(--paper);
  font-family: "Barlow Semi Condensed", "Arial Narrow", sans-serif;
  overflow-x: hidden;
}

img, canvas { image-rendering: pixelated; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
[role="button"]:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 3px;
}

.poster {
  position: relative;
  width: min(1240px, 100%, calc((100dvh - 32px) * 1.5));
  aspect-ratio: 3 / 2;
  display: grid;
  grid-template-rows: 50fr 37fr 13fr;
  background: var(--steel);
  border: 3px solid #070a0e;
  box-shadow: 10px 10px 0 #05080b;
  overflow: hidden;
}

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

.garage {
  position: relative;
  overflow: hidden;
  background: var(--ink);
  border-bottom: 3px solid #070a0e;
}

.panGroup {
  position: absolute;
  inset: 0;
  will-change: transform;
}

.wall {
  position: absolute;
  left: -8%;
  top: 0;
  width: 116%;
  height: 84%;
  object-fit: cover;
  object-position: center top;
  display: block;
}

.floor {
  position: absolute;
  left: -8%;
  bottom: 0;
  width: 116%;
  height: 27%;
  object-fit: cover;
  object-position: center top;
  display: block;
}

.sign {
  position: absolute;
  left: 3%;
  top: 2%;
  width: 24%;
  display: block;
  filter: drop-shadow(4px 5px 0 rgba(0, 0, 0, 0.55));
}

.lcd {
  position: absolute;
  right: 3%;
  top: 2%;
  width: 22%;
  filter: drop-shadow(4px 5px 0 rgba(0, 0, 0, 0.55));
}

.lcdFrame { display: block; width: 100%; height: auto; }

.lcdScreen {
  position: absolute;
  left: 7%;
  top: 16%;
  width: 86%;
  height: 68%;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  padding: 1% 4%;
  container-type: size;
  overflow: hidden;
}

.lcdLine {
  font-family: "VT323", monospace;
  color: #29351e;
  line-height: 0.88;
  letter-spacing: 0.015em;
  white-space: nowrap;
  text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.10);
}

.lcdName { font-size: 11cqw; }
.lcdSpecs { font-size: 9.4cqw; }
.lcdZero { font-size: 10cqw; }

.carSlot {
  position: absolute;
  bottom: 25.5%;
  width: var(--car-w, 44%);
  cursor: pointer;
}

.carSlot.left { left: 4.5%; }
.carSlot.right { right: 4.5%; }

.carSlot.selected.left { transform: translateX(14px); }
.carSlot.selected.right { transform: translateX(-14px); }
.carSlot.left:not(.selected) { transform: translateX(-6px); filter: brightness(0.74) saturate(0.88); }
.carSlot.right:not(.selected) { transform: translateX(6px); filter: brightness(0.74) saturate(0.88); }

.carRig { position: relative; }
.carJitter { position: relative; }

.carSlot.selected:not(.driving) .carJitter { animation: idle 2.8s steps(1, end) infinite; }

@keyframes idle {
  0%, 49% { transform: translateY(0); }
  50%, 100% { transform: translateY(-1px); }
}

.carBody { display: block; width: 100%; height: auto; position: relative; }

.flash {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  pointer-events: none;
}

.flash.show { opacity: 1; }

.wheel {
  position: absolute;
  width: var(--wheel-w, 15.6%);
  height: auto;
  display: block;
  transform: translate(-50%, -50%) rotate(var(--spin, 0deg));
  transform-origin: 50% 50%;
  pointer-events: none;
}

.wheel.rear { left: var(--rear-x, 20.9%); top: var(--rear-y, 66.7%); }
.wheel.front { left: var(--front-x, 80.1%); top: var(--front-y, 66.7%); }

.carSlot.driving { z-index: 6; }
.carSlot.driving .carJitter { animation: none; }

/* lights */

.lights {
  position: absolute;
  left: 50%;
  top: 15%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 8px 12px 7px;
  background: var(--ink);
  border: 2px solid #05080b;
  box-shadow: inset 0 0 0 2px #2b3946, 4px 5px 0 rgba(0, 0, 0, 0.5);
}

.lampRow { display: flex; gap: 8px; }

.lamp {
  width: 22px;
  height: 22px;
  display: block;
  background: #22303c;
  border: 2px solid #0a0f15;
  box-shadow: inset 0 0 0 2px #2b3a47;
}

.lamp.on { background: var(--amber); border-color: #8b5f0d; box-shadow: inset 0 0 0 2px #ffd27a; }
.lights.go .lamp { background: var(--lcd); border-color: #436f26; box-shadow: inset 0 0 0 2px #c8f39e; }
.lights.falseStart { border-color: #ea1e1a; }
.lights.falseStart .lightsLabel { color: #ff8b7e; }

.lightsLabel {
  font-family: "Silkscreen", monospace;
  font-size: 8px;
  letter-spacing: 0.08em;
  color: var(--paper);
  white-space: nowrap;
}

/* pause + result */

.pauseCard {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  z-index: 12;
  padding: 10px 18px;
  background: var(--ink);
  border: 3px solid var(--amber);
  box-shadow: 6px 6px 0 rgba(0, 0, 0, 0.55);
  font-family: "Silkscreen", monospace;
  font-size: 15px;
  letter-spacing: 0.12em;
  color: var(--paper);
}

.resultCard {
  position: absolute;
  left: 50%;
  top: 47%;
  transform: translate(-50%, -50%);
  z-index: 13;
  width: min(46%, 440px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 18px 11px;
  background: var(--ink);
  border: 3px solid var(--cyan);
  box-shadow: inset 0 0 0 2px #1d4f4b, 7px 7px 0 rgba(0, 0, 0, 0.55);
  text-align: center;
}

.rKicker {
  font-family: "Silkscreen", monospace;
  font-size: 8px;
  letter-spacing: 0.16em;
  color: var(--cyan);
}

.rName {
  font-family: "Silkscreen", monospace;
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--paper);
}

.rTime {
  font-family: "VT323", monospace;
  font-size: 56px;
  line-height: 0.85;
  color: var(--lcd);
}

.rTime b { font-weight: 400; }
.rTime i { font-style: normal; font-size: 0.45em; margin-left: 2px; }

.rBest {
  font-family: "Barlow Semi Condensed", sans-serif;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--amber);
}

.rHint {
  font-family: "Barlow Semi Condensed", sans-serif;
  font-weight: 600;
  font-size: 9px;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-top: 2px;
}

/* ---------- deck ---------- */

.deck {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 7px 10px 6px;
  background: var(--steel);
  border-bottom: 3px solid #070a0e;
  min-height: 0;
}

.deckHead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 2px;
}

.help {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.025em;
  color: #c3cfd8;
}

.scope {
  margin: 0;
  flex-shrink: 0;
  font-family: "Silkscreen", monospace;
  font-size: 8px;
  letter-spacing: 0.08em;
  color: var(--amber);
}

/* car picker */

.garagePicker {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  min-height: 0;
}

.pickCard {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  height: 48px;
  padding: 3px 5px;
  background: #1a2530;
  border: 2px solid #0e151c;
  text-align: left;
}

.pickCard.selected {
  border-color: var(--amber);
  box-shadow: inset 0 0 0 1px #5a4a22;
}

.pickCard.selected::after {
  content: "";
  position: absolute;
  top: -4px;
  right: -4px;
  width: 8px;
  height: 8px;
  background: var(--cyan);
  border: 1px solid #062723;
  animation: blink 1.1s steps(2, end) infinite;
}

.pickTile {
  flex: 0 0 auto;
  width: 92px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
  background: #121b24;
  border: 2px solid #0b1118;
}

.pickImg {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
}

.pickImg.flip { transform: scaleX(-1); }

.pickMeta {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  line-height: 1;
}

.pickName {
  font-family: "Silkscreen", monospace;
  font-size: 9px;
  letter-spacing: 0.01em;
  color: var(--paper);
  white-space: normal;
  overflow: hidden;
}

.pickYear {
  font-style: normal;
  font-size: 8.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--muted);
  white-space: nowrap;
}

/* workbench */

.workbench {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 7px;
}

.tunePanel,
.decalPanel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 5px 7px 6px;
  background: var(--panel-2);
  border: 2px solid #101820;
  box-shadow: inset 0 2px 0 #3b4e60;
}

.tunePanel { border-color: var(--amber); box-shadow: inset 0 2px 0 #5a4a22; }

.tuneHead {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  padding-bottom: 2px;
}

.tuneName {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.03em;
  color: var(--paper);
  white-space: nowrap;
}

.tuneSpec {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.07em;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tuneBest {
  margin-left: auto;
  font-family: "Silkscreen", monospace;
  font-size: 8px;
  letter-spacing: 0.05em;
  color: var(--amber);
  white-space: nowrap;
}

.optBlock {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 3px 0 0;
  min-height: 0;
}

.optLabel {
  font-family: "Silkscreen", monospace;
  font-size: 7px;
  line-height: 1;
  letter-spacing: 0.13em;
  color: #8fa2b1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.optLabel b {
  font-weight: 400;
  color: var(--amber);
}

.opts { display: flex; gap: 4px; min-width: 0; }

.chip {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  display: block;
  padding: 0;
  line-height: 0;
}

.chip img { display: block; width: 100%; height: auto; }

.chip.selected,
.rim.selected,
.decal.selected {
  outline: 2px solid var(--amber);
  outline-offset: 2px;
}

.chip.selected::after,
.rim.selected::after,
.decal.selected::after {
  content: "";
  position: absolute;
  top: -5px;
  right: -5px;
  width: 8px;
  height: 8px;
  background: var(--cyan);
  border: 1px solid #062723;
  animation: blink 1.1s steps(2, end) infinite;
}

.rimRow {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.rim {
  position: relative;
  min-width: 0;
  height: clamp(38px, 5vw, 54px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1a2530;
  border: 2px solid #0e151c;
}

.rim img { display: block; max-width: 100%; max-height: 100%; width: auto; height: auto; }

.panelHead {
  display: flex;
  align-items: center;
  min-width: 0;
  padding-bottom: 3px;
}

.decalGrid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 4px;
}

.decal {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
  background: #1a2530;
  border: 2px solid #0e151c;
}

.decal img {
  display: block;
  max-width: 92%;
  max-height: 84%;
  width: auto;
  height: auto;
}

@keyframes blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0.15; }
}

/* ---------- control rail ---------- */

.rail {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: clamp(3px, 0.9vw, 10px);
  padding: 5px clamp(7px, 1.1vw, 14px) 6px;
  background: var(--steel-2);
  border-top: 2px solid #070a0e;
}

.ctl {
  flex: 1 1 0;
  min-width: 0;
  max-width: 170px;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
}

.ctl img {
  display: block;
  width: auto;
  max-width: 100%;
  max-height: calc(100% - 13px);
  height: auto;
}

.ctlLabel {
  font-family: "Silkscreen", monospace;
  font-size: clamp(7px, 0.75vw, 9px);
  letter-spacing: 0.03em;
  line-height: 1.15;
  color: var(--concrete);
  white-space: nowrap;
}

#btn-drive .ctlLabel { color: var(--amber); }

.ctl:hover img { filter: brightness(1.13); }
.ctl:active img { transform: translateY(1px); }
.ctl:disabled { opacity: 0.42; cursor: not-allowed; }
.ctl:disabled:hover img { filter: none; }

.ctl.armed { outline: 2px solid var(--amber); outline-offset: 2px; }

.no-idle .carSlot.selected:not(.driving) .carJitter { animation: none; }

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

@media (max-aspect-ratio: 1/1), (max-width: 720px) {
  body {
    display: block;
    padding: 0;
    background: var(--ink-2);
  }

  .poster {
    width: 100%;
    max-width: none;
    aspect-ratio: auto;
    display: block;
    border: 0;
    border-bottom: 0;
    box-shadow: none;
    padding-bottom: calc(66px + env(safe-area-inset-bottom, 0px));
  }

  .garage {
    aspect-ratio: 1.5 / 1;
    border-bottom: 3px solid #070a0e;
  }

  .sign { left: 2.5%; top: 3%; width: 40%; }
  .lcd { right: 2.5%; top: 3%; width: 48%; }

  .carSlot { bottom: 25.5%; }
  .carSlot.left { left: 3%; }
  .carSlot.right { right: 3%; }
  .carSlot.selected.left { transform: translateX(6px); }
  .carSlot.selected.right { transform: translateX(-6px); }

  .lights { top: 36%; padding: 6px 9px 5px; }
  .lamp { width: 16px; height: 16px; }
  .lightsLabel { font-size: 7px; }

  .pauseCard { font-size: 12px; padding: 8px 13px; }
  .resultCard { width: 78%; padding: 10px 12px 9px; }
  .rTime { font-size: 48px; }

  .deck {
    padding: 7px 8px 10px;
    gap: 6px;
    border-bottom: 0;
  }

  .deckHead { gap: 8px; align-items: flex-start; }
  .help { font-size: 11px; line-height: 1.25; }
  .scope { font-size: 7px; padding-top: 2px; }

  .garagePicker {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .pickCard { height: 54px; padding: 4px 6px; }
  .pickTile { width: 88px; height: 36px; }
  .pickName { font-size: 9px; }
  .pickYear { font-size: 8px; }

  .workbench {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .tunePanel { padding: 6px 7px 7px; }
  .tuneHead { gap: 6px; }
  .tuneName { font-size: 13px; }
  .tuneSpec { font-size: 9.5px; }
  .tuneBest { font-size: 7px; }

  .paintRow { gap: 5px; }
  .rimRow { grid-template-columns: repeat(4, 1fr); gap: 5px; }
  .rim { height: 56px; }

  .decalGrid {
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 58px 58px;
  }
  .decal { padding: 4px; }
  .decalPanel { min-height: 148px; }

  .rail {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    height: auto;
    padding: 5px 6px calc(5px + env(safe-area-inset-bottom, 0px));
    background: var(--ink);
    border-top: 2px solid #05080b;
    box-shadow: 0 -4px 0 rgba(0, 0, 0, 0.35);
  }

  .ctl { max-width: none; gap: 0; }
  .ctl img { max-height: 40px; width: auto; max-width: 100%; }
  .ctlLabel { font-size: 6.6px; letter-spacing: 0; }
}

@media (max-width: 380px) {
  .ctlLabel { font-size: 6px; }
  .pickTile { width: 64px; height: 34px; }
  .pickName { font-size: 8.5px; }
  .pickYear { font-size: 7.5px; }
  .tuneName { font-size: 12px; }
  .help { font-size: 10.5px; }
}

@media (prefers-reduced-motion: reduce) {
  .carSlot.selected:not(.driving) .carJitter { animation: none; }
  .pickCard.selected::after,
  .chip.selected::after,
  .rim.selected::after,
  .decal.selected::after { animation: none; }
}
