:root {
  --night: #0f1912;
  --panel: #1a2a1e;
  --panel-2: #26382b;
  --ink: #fbf5e6;
  --muted: #c3ccb9;
  --amber: #ffb938;
  --amber-2: #ffd27a;
  --red: #e2573e;
  --line: #3f5c45;
  --focus: #ffffff;
}
:root[data-contrast='high'] {
  --line: #ffffff;
  --muted: #ffffff;
  --panel: #0a110c;
  --panel-2: #16241a;
}
* { box-sizing: border-box; }
html { color-scheme: dark; }
body {
  margin: 0;
  background: radial-gradient(120% 80% at 50% 0%, #27402c 0%, var(--night) 62%) fixed;
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  min-height: 100vh;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: 8px; top: -60px; background: var(--amber); color: #111; padding: 8px 12px; border-radius: 6px; z-index: 40; }
.skip-link:focus { top: 8px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.dr-header {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px;
  padding: 10px max(16px, env(safe-area-inset-left)); border-bottom: 2px solid var(--line);
  background: rgba(15, 25, 18, 0.92);
}
.back-link { color: var(--ink); text-decoration: none; font-weight: 700; min-height: 44px; display: inline-flex; align-items: center; gap: 4px; }
.back-link:hover span { text-decoration: underline; }
.dr-brand { display: grid; line-height: 1.1; text-align: center; }
.dr-brand strong { font: 900 20px/1 Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif; letter-spacing: 0.12em; color: var(--amber); }
.dr-brand small { color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; font-size: 11px; }
.header-actions { display: flex; gap: 8px; }
button, .button {
  font: inherit; font-weight: 700; color: var(--ink); background: var(--panel-2); border: 2px solid var(--line);
  border-radius: 8px; padding: 8px 14px; min-height: 44px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
button:hover:not(:disabled), .button:hover { border-color: var(--amber); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--amber); color: #1a1408; border-color: #ffe3a6; }
button.primary:hover { background: var(--amber-2); }

.dr-main { width: min(1100px, 100%); margin: 0 auto; padding: 16px max(16px, env(safe-area-inset-left)) 40px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.dr-main > * { min-width: 0; }
.stage { position: relative; width: 100%; scroll-margin-top: 6px; }
.view {
  position: relative; width: 100%; aspect-ratio: 16 / 9; background: #0b120d; border: 3px solid var(--line); border-radius: 10px; overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}
.view canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.card { position: absolute; inset: 0; display: grid; place-items: safe center; background: rgba(12, 20, 14, 0.58); padding: clamp(8px, 2vw, 20px); overflow: auto; border-radius: 10px; }
.card[hidden], .card[data-card][hidden] { display: none !important; }
.panel {
  width: min(520px, 100%); background: rgba(26, 42, 30, 0.96); border: 2px solid var(--line); border-radius: 12px;
  padding: clamp(12px, 2.2vw, 22px); display: grid; gap: clamp(6px, 1.2vw, 11px); text-align: center;
}
.kicker { margin: 0; color: var(--amber); font-weight: 800; letter-spacing: 0.14em; font-size: clamp(10px, 1.4vw, 12px); }
h1 { margin: 0; font: 900 clamp(28px, 4.4vw, 46px)/0.98 Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif; letter-spacing: 0.04em; text-transform: uppercase; }
h1 span { color: var(--amber); }
h2 { margin: 0; font-size: 22px; }
.lede { margin: 0; color: var(--muted); font-size: clamp(13px, 1.8vw, 15px); }
.small { margin: 0; color: var(--muted); font-size: 13px; }
.blurb { margin: 0; color: var(--muted); font-size: 13px; min-height: 3.9em; text-align: left; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

.vehicles, .segmented { border: 0; margin: 0; padding: 0; min-width: 0; }
.vehicles legend, .segmented legend { padding: 0; color: var(--muted); font-size: 13px; margin: 0 auto 4px; }
.vehicle-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.vehicle-grid label { position: relative; display: block; }
.vehicle-grid input { position: absolute; opacity: 0; inset: 0; }
.vehicle-grid .card-face { display: grid; gap: 1px; justify-items: center; padding: 8px 4px; border: 2px solid var(--line); border-radius: 10px; background: var(--panel-2); cursor: pointer; min-height: 62px; align-content: center; }
.vehicle-grid strong { font-size: 14px; line-height: 1.15; }
.vehicle-grid small { color: var(--muted); font-size: 11px; }
.vehicle-grid input:checked + .card-face { background: var(--amber); color: #1a1408; border-color: #ffe3a6; }
.vehicle-grid input:checked + .card-face small { color: #3a2d0c; }
.vehicle-grid input:focus-visible + .card-face { outline: 3px solid var(--focus); outline-offset: 2px; }
.ratings { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 2px 14px; margin: 0; text-align: left; }
.ratings div { display: grid; grid-template-columns: 74px 1fr; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); }
.ratings dt { margin: 0; }
.ratings dd { margin: 0; height: 7px; border-radius: 4px; background: #0e1610; overflow: hidden; }
.ratings dd span { display: block; height: 100%; background: var(--amber); border-radius: 4px; }

.segmented { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.segmented legend { width: 100%; text-align: center; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; }
.segmented span { display: inline-flex; min-height: 44px; align-items: center; padding: 6px 16px; border: 2px solid var(--line); border-radius: 8px; background: var(--panel-2); font-weight: 700; cursor: pointer; }
.segmented input:checked + span { background: var(--amber); color: #1a1408; border-color: #ffe3a6; }
.segmented input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }

.result-panel { width: min(680px, 100%); }
.result-top { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.place { font: 900 40px/1 Impact, Haettenschweiler, sans-serif; color: var(--amber); border: 3px solid var(--amber); border-radius: 10px; min-width: 64px; padding: 4px 10px; }
.standings { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; text-align: left; }
.standings li { display: grid; grid-template-columns: 28px 14px minmax(0, 1fr) auto; gap: 8px; align-items: center; background: var(--panel-2); border: 2px solid var(--line); border-radius: 8px; padding: 4px 10px; font-size: 14px; }
.standings li[data-me='true'] { border-color: var(--amber); }
.standings .rank { font-weight: 900; color: var(--amber); }
.standings .chip { width: 12px; height: 12px; border-radius: 50%; border: 2px solid #0b120d; }
.standings .who small { color: var(--muted); margin-left: 6px; }
.standings .time { font-variant-numeric: tabular-nums; color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 0; }
.stats div { background: var(--panel-2); border-radius: 8px; padding: 6px; }
.stats dt { font-size: 11px; color: var(--muted); }
.stats dd { margin: 0; font-weight: 900; font-size: 18px; font-variant-numeric: tabular-nums; }
.best { margin: 0; min-height: 1.4em; color: var(--amber); font-weight: 700; }

.touch { display: none; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 10px 12px; user-select: none; -webkit-user-select: none; touch-action: none; }
.touch button { touch-action: none; -webkit-touch-callout: none; font-size: 22px; padding: 0; }
.steer-pad { display: flex; gap: 8px; }
.steer-pad button { width: clamp(54px, 17vw, 74px); height: clamp(70px, 22vw, 90px); border-radius: 18px; font-size: 28px; }
.pedals { margin-left: auto; display: grid; grid-template-columns: auto auto; grid-template-areas: 'drift gas' 'brake gas'; gap: 8px; align-items: end; justify-items: end; }
.pedals .gas { grid-area: gas; width: clamp(72px, 22vw, 96px); height: clamp(72px, 22vw, 96px); border-radius: 50%; font-size: 16px; }
.pedals .brake { grid-area: brake; width: clamp(52px, 15vw, 64px); height: clamp(52px, 15vw, 64px); border-radius: 50%; font-size: 13px; }
.pedals .drift { grid-area: drift; width: clamp(46px, 13.5vw, 56px); height: clamp(46px, 13.5vw, 56px); border-radius: 50%; font-size: 11px; }
.touch .reset { width: 40px; height: 40px; border-radius: 50%; align-self: center; font-size: 20px; }
.touch button.is-down { background: var(--amber); color: #1a1408; border-color: #ffe3a6; }
@media (pointer: coarse), (max-width: 820px) { .touch { display: flex; } }

.info { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; align-items: start; }
.info > * { background: rgba(26, 42, 30, 0.7); border: 2px solid var(--line); border-radius: 10px; padding: 14px 16px; margin: 0; min-width: 0; }
.info h2 { font-size: 18px; margin-bottom: 8px; color: var(--amber); }
.keys { border-collapse: collapse; width: 100%; font-size: 14px; }
.keys th { text-align: left; padding: 4px 8px 4px 0; vertical-align: top; color: var(--ink); width: 38%; }
.keys td { padding: 4px 0; color: var(--muted); }
.plain { margin: 0; padding-left: 1.1em; color: var(--muted); font-size: 14px; display: grid; gap: 6px; }
.options { display: grid; gap: 8px; }
.options legend { font-weight: 800; color: var(--amber); padding: 0 6px; }
.options label { display: flex; align-items: center; gap: 10px; min-height: 36px; }
.options select { font: inherit; color: var(--ink); background: var(--panel-2); border: 2px solid var(--line); border-radius: 6px; padding: 6px 8px; margin-left: auto; }
.options input[type='checkbox'] { width: 20px; height: 20px; accent-color: var(--amber); flex: none; }
.options .range { display: grid; grid-template-columns: 1fr minmax(0, 120px) 44px; gap: 8px; }
.options input[type='range'] { width: 100%; accent-color: var(--amber); }
.options output { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; text-align: right; }

@media (max-width: 640px) {
  .dr-brand { display: none; }
  .dr-header { padding-block: 6px; }
  .back-link { white-space: nowrap; }
  .header-actions { flex-shrink: 0; }
  .header-actions button { padding: 6px 10px; font-size: 14px; }
  .view { aspect-ratio: 4 / 3; }
}
@media (max-width: 560px) {
  .stage { display: grid; gap: 10px; }
  .card[data-card='title'] { position: static; background: none; padding: 0; overflow: visible; }
  .card[data-card='title'] .panel { width: 100%; }
  .card[data-card='result'], .card[data-card='paused'] { position: fixed; inset: 0; z-index: 30; background: rgba(12, 20, 14, 0.94); border-radius: 0; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .ratings { display: none; }
  .options .range { grid-template-columns: 1fr 44px; }
  .options .range input { grid-column: 1 / -1; grid-row: 2; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }

@media (min-width: 900px) and (max-height: 880px) { .card[data-card='title'] .lede { display: none; } }
@media (min-width: 900px) {
  .card[data-card='title'] { place-items: center start; padding-left: 3%; background: linear-gradient(90deg, rgba(12, 20, 14, 0.82) 0%, rgba(12, 20, 14, 0.4) 55%, rgba(12, 20, 14, 0) 100%); }
  .card[data-card='title'] .panel { width: min(470px, 50%); }
  .result-panel { grid-template-columns: 1fr; }
}

/* Landscape phones: the whole stage must fit above the fold, with the controls floating over it. */
@media (max-height: 520px) and (orientation: landscape) {
  .dr-header { padding-block: 4px; }
  .dr-header button, .back-link { min-height: 36px; }
  .dr-main { padding: 8px 12px 24px; gap: 10px; }
  .stage { width: min(100%, calc((100vh - 76px) * 16 / 9)); margin: 0 auto; }
  .view { aspect-ratio: 16 / 9; }
  .touch { position: fixed; left: 0; right: 0; bottom: 6px; padding: 0 14px; pointer-events: none; z-index: 5; }
  .touch button { pointer-events: auto; opacity: 0.72; }
  .touch { flex-wrap: nowrap; }
  .steer-pad button { width: 66px; height: 66px; font-size: 24px; }
  .pedals .gas { width: 76px; height: 76px; }
  .pedals .brake { width: 56px; height: 56px; }
  .pedals .drift { width: 48px; height: 48px; }
  .touch .reset { width: 38px; height: 38px; }
  .card[data-card='title'] { position: absolute; display: grid; place-items: center; background: rgba(12, 20, 14, 0.6); padding: 6px; overflow: auto; }
  .card[data-card='title'] .panel { width: min(560px, 80%); gap: 5px; padding: 8px 12px; }
  .stage { display: block; }
  .lede, .kicker, .blurb, .ratings, #best-line { display: none; }
  h1 { font-size: 30px; }
  .vehicles legend, .segmented legend { display: none; }
  .vehicle-grid .card-face { min-height: 40px; padding: 4px; }
  .vehicle-grid small { display: none; }
  .segmented { display: flex; flex-wrap: nowrap; }
  .segmented span { min-height: 34px; padding: 3px 14px; }
  button.primary { min-height: 36px; }
  .card[data-card='result'], .card[data-card='paused'] { position: fixed; inset: 0; z-index: 30; background: rgba(12, 20, 14, 0.94); border-radius: 0; }
  .result-panel { gap: 4px; padding: 8px 14px; }
  .standings { gap: 2px; }
  .standings li { padding: 1px 8px; font-size: 12px; }
  .stats { grid-template-columns: repeat(6, 1fr); gap: 3px; }
  .stats div { padding: 3px; }
  .stats dd { font-size: 14px; }
  .stats dt { font-size: 9px; }
  .place { font-size: 26px; padding: 2px 8px; min-width: 44px; border-width: 2px; }
  h2 { font-size: 18px; }
  .best { min-height: 0; font-size: 13px; }
  .button-row button, .button-row .button { min-height: 34px; padding: 3px 12px; }
}
