/* ==========================================================================
   Car Emulator
   ========================================================================== */

/* `hidden` is only a user-agent rule, so any author rule that sets `display`
   silently beats it and the element stays painted while `el.hidden` reads true.
   One !important here, once, costs nothing and closes the whole class. */
[hidden] { display: none !important; }

:root {
  color-scheme: dark;

  --bg: #080a0e;
  --bg-raised: #10141b;
  --bg-sunken: #0b0e13;
  --line: #1e242e;
  --line-soft: #171c24;
  --text: #e8eef7;
  --text-dim: #8e99a9;
  --text-faint: #5d6878;
  --accent: #5ad1ff;
  --accent-dim: #2a7fa3;
  --good: #3ddc84;
  --warn: #ffb020;
  --danger: #ff4438;
  --radius: 10px;
  --radius-sm: 6px;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

h3 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
}

/* ------------------------------------------------------------------ topbar */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand strong { display: block; font-size: 15px; letter-spacing: 0.01em; }
.brand small { display: block; color: var(--text-faint); font-size: 11px; }

.brand-mark {
  width: 26px;
  height: 26px;
  flex: 0 0 auto;
  border-radius: 7px;
  background:
    radial-gradient(circle at 30% 28%, #ff7a5f 0%, #ff3b30 45%, #7b1a14 100%);
  box-shadow: 0 0 0 1px #2a1210, 0 3px 10px rgba(255, 59, 48, 0.28);
}

.quick {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ------------------------------------------------------------------ layout */

.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  grid-template-rows: minmax(150px, 0.72fr) minmax(0, 1fr);
  grid-template-areas:
    "cluster panel"
    "track   panel";
  gap: 10px;
  padding: 10px;
}

.cluster-pane { grid-area: cluster; }
.track-pane   { grid-area: track; }
.panel        { grid-area: panel; }

.cluster-pane,
.track-pane,
.panel {
  min-width: 0;
  min-height: 0;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

/* The canvases fill their grid cell and are measured from it. `aspect-ratio`
   is no help here: a grid or flex item whose size is resolved on both axes has
   two definite axes, and the property only sizes an axis that is still auto. */
.cluster-pane canvas,
.track-pane canvas {
  display: block;
  width: 100%;
  height: 100%;
}

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

.track-pane {
  position: relative;
  /* Container units so the overlay scales with this pane rather than with the
     viewport — the two stop agreeing the moment their shapes differ. */
  container-type: size;
}

.hud {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  gap: clamp(8px, 1.6cqw, 16px);
  padding: clamp(6px, 1.2cqh, 10px) clamp(8px, 1.6cqw, 14px);
  background: rgba(8, 10, 14, 0.72);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  backdrop-filter: blur(6px);
  pointer-events: none;
}

.hud-right { left: auto; right: 10px; }

.hud-block { display: flex; flex-direction: column; gap: 1px; }

.hud-label {
  font-size: clamp(8px, 1.5cqh, 10px);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.hud-value {
  font-family: var(--mono);
  font-size: clamp(12px, 3cqh, 18px);
  font-variant-numeric: tabular-nums;
}

.hud-value.is-best { color: var(--good); }
.hud-value.is-invalid { color: var(--warn); }

.hud-flag {
  position: absolute;
  top: clamp(58px, 15cqh, 96px);
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 5px 14px;
  font-size: clamp(11px, 2.4cqh, 15px);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #0b0e13;
  background: var(--warn);
  border-radius: var(--radius-sm);
  pointer-events: none;
}

.hud-hint {
  position: absolute;
  left: 10px;
  bottom: 8px;
  margin: 0;
  font-size: clamp(9px, 1.8cqh, 12px);
  color: var(--text-faint);
  pointer-events: none;
}

/* ------------------------------------------------------------------- panel */

.panel { display: flex; flex-direction: column; }

.tabs {
  flex: 0 0 auto;
  display: flex;
  border-bottom: 1px solid var(--line);
  background: var(--bg-sunken);
}

.tab {
  flex: 1 1 0;
  padding: 9px 4px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}

.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.tab-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.tab-page { display: none; padding: 12px; }
.tab-page.is-active { display: block; }

.group { margin-bottom: 18px; }

.field {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.fl {
  flex: 0 0 74px;
  font-size: 12px;
  color: var(--text-dim);
}

select,
input[type="range"] {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 5px 7px;
}

input[type="range"] { padding: 0; accent-color: var(--accent); }

output {
  flex: 0 0 52px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  text-align: right;
}

.switches { display: flex; flex-wrap: wrap; gap: 6px; }

.sw {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 9px 4px 6px;
  font-size: 12px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  user-select: none;
}

.sw input { accent-color: var(--accent); margin: 0; }

/* A control styled as a button keeps its own colour: a theme-scoped element
   rule such as `:root[data-theme] button` scores higher than a single class and
   would paint the label the colour of the background underneath it. */
button.key,
button.chip {
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

button.key:hover,
button.chip:hover { border-color: var(--accent-dim); }
button.key:focus-visible,
button.chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button.key.primary {
  color: #05212c;
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 700;
}

button.key[aria-pressed="true"],
button.chip.is-on {
  color: #05212c;
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}

button.chip.is-warn {
  color: #2a1a00;
  background: var(--warn);
  border-color: var(--warn);
  font-weight: 600;
}

kbd {
  font-family: var(--mono);
  font-size: 10px;
  padding: 1px 4px;
  color: var(--text-dim);
  background: #060810;
  border: 1px solid var(--line);
  border-radius: 3px;
}

button.key[aria-pressed="true"] kbd,
button.key.primary kbd,
button.chip.is-on kbd { color: #05212c; background: rgba(255, 255, 255, 0.45); border-color: transparent; }

/* --------------------------------------------------------------- telemetry */

.tgroup { margin-bottom: 16px; }

.rows { display: grid; gap: 2px; }

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  gap: 8px;
  padding: 3px 6px;
  border-radius: 4px;
  font-size: 12px;
}

.row:nth-child(odd) { background: rgba(255, 255, 255, 0.022); }
.row .k { color: var(--text-dim); }
.row .v {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.row .v.is-warn { color: var(--warn); }
.row .v.is-danger { color: var(--danger); }
.row .v.is-good { color: var(--good); }

.meter {
  grid-column: 1 / -1;
  height: 3px;
  border-radius: 2px;
  background: var(--line-soft);
  overflow: hidden;
}

.meter i {
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width 90ms linear;
}

/* --------------------------------------------------------------------- OBD */

.obd-head p { margin: 0 0 8px; font-size: 12px; color: var(--text-dim); }

.pids {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 11px;
  margin-bottom: 18px;
}

.pids col.c-pid { width: 42px; }
.pids col.c-bytes { width: 48px; }
.pids col.c-value { width: 84px; }

.pids th {
  text-align: left;
  font-size: 9px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 4px 6px;
  border-bottom: 1px solid var(--line);
}

.pids td {
  padding: 3px 5px;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: baseline;
}

.pids td:first-child,
.pids td:nth-child(3) { white-space: nowrap; }
.pids td:nth-child(2) { overflow-wrap: anywhere; }

.pids td:first-child,
.pids td:nth-child(3),
.pids td:last-child {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

.pids td:first-child { color: var(--accent); }
.pids td:nth-child(3) { color: var(--text-faint); }
.pids td:last-child { text-align: right; white-space: nowrap; }

.dtc-list, .monitors { display: grid; gap: 5px; margin-bottom: 18px; }

.dtc {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  padding: 7px 9px;
  border-radius: var(--radius-sm);
  background: rgba(255, 68, 56, 0.09);
  border: 1px solid rgba(255, 68, 56, 0.28);
  font-size: 12px;
}

.dtc.is-pending {
  background: rgba(255, 176, 32, 0.08);
  border-color: rgba(255, 176, 32, 0.28);
}

.dtc code {
  font-family: var(--mono);
  font-weight: 700;
  color: var(--danger);
}

.dtc.is-pending code { color: var(--warn); }
.dtc span { color: var(--text-dim); }
.dtc .bytes { grid-column: 1 / -1; font-family: var(--mono); font-size: 10px; color: var(--text-faint); }

.empty { font-size: 12px; color: var(--text-faint); font-style: italic; }

.freeze {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 9px;
  margin-bottom: 18px;
  white-space: pre-wrap;
}

.monitor {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  font-size: 11px;
}

.monitor code { font-family: var(--mono); color: var(--text-dim); }
.monitor .bar { height: 3px; border-radius: 2px; background: var(--line-soft); overflow: hidden; }
.monitor .bar i { display: block; height: 100%; background: var(--warn); }
.monitor .state { font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-faint); }
.monitor .state.is-pending { color: var(--warn); }
.monitor .state.is-confirmed { color: var(--danger); }

/* --------------------------------------------------------------- spec, log */

.spec { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; font-size: 12px; }
.spec dt { color: var(--text-dim); }
.spec dd { margin: 0; font-family: var(--mono); text-align: right; }

.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; }

.log li {
  display: grid;
  grid-template-columns: 54px 1fr;
  gap: 8px;
  font-size: 12px;
  padding: 3px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.022);
}

.log time { font-family: var(--mono); color: var(--text-faint); }
.log .kind-stall, .log .kind-impact, .log .kind-grind { color: var(--warn); }
.log .kind-start, .log .kind-lap { color: var(--good); }

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

/* Shown only on a touch device; see the media query at the end. Note this is a
   `display` rule and not the `hidden` attribute — an author rule setting
   `display` beats `[hidden]`, so mixing the two makes the element visible while
   `el.hidden` reads true. */
.touch {
  position: fixed;
  inset: auto 0 0 0;
  display: none;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  pointer-events: none;
}

.pad {
  pointer-events: auto;
  height: 62px;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  background: rgba(16, 20, 27, 0.9);
  border: 1px solid var(--line);
  border-radius: 14px;
  touch-action: none;
  user-select: none;
}

.pad:active { background: var(--accent); color: #05212c; }
.pad-throttle { background: rgba(61, 220, 132, 0.18); }
.pad-brake { background: rgba(255, 68, 56, 0.18); }

/* ------------------------------------------------------------- adaptations */

/* The HUD is sized against the stage, so this query is too — a viewport query
   would measure a box the stage does not occupy. */
@container (max-width: 470px) {
  .hud { gap: 7px; padding: 4px 7px; }
  .hud-block.is-optional { display: none; }
  .hud-right { display: none; }
}

@media (max-width: 1080px) {
  .layout { grid-template-columns: minmax(0, 1fr) 310px; }
}

@media (max-width: 900px) {
  body { overflow: auto; }
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    grid-template-areas: "cluster" "track" "panel";
    height: auto;
  }
  .cluster-pane { aspect-ratio: 1060 / 400; }
  .track-pane { aspect-ratio: 4 / 3; }
  .panel { min-height: 420px; }
  .quick { width: 100%; }
}

@media (pointer: coarse) and (max-width: 900px) {
  .touch { display: grid; }
  .hud-hint { display: none; }
  /* Leave room for the fixed pedals so the last panel can be scrolled clear. */
  .layout { padding-bottom: calc(94px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  .meter i { transition: none; }
}
