/* Layout: three-column cockpit (garage | gauge cluster | controls) that folds to
   two columns, then one, on narrower screens. One dark theme on purpose: this
   lives on a car dashboard. */
:root {
  --carbon: #121110;
  --panel: #1b1a18;
  --raised: #26241f;
  --line: #3a3731;
  --text: #efece5;
  --muted: #a6a093;
  --giallo: #f2c200;
  --rosso: #e0261b;
  --go: #43c06f;
  --cool: #5ab8e8;
  --brand: #c9a227;
  --font-display: 'Saira Condensed', 'Arial Narrow', 'Roboto Condensed', sans-serif;
  --font-body: 'Saira', 'Segoe UI', Roboto, system-ui, sans-serif;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  background: var(--carbon);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

:focus-visible {
  outline: 2px solid var(--giallo);
  outline-offset: 2px;
}

button {
  font: inherit;
  color: inherit;
}

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

/* ---------------------------------------------------------------- top bar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px 20px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-family: var(--font-display);
  text-transform: uppercase;
  white-space: nowrap;
}

.brand-mark {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.brand-sub {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.14em;
  color: var(--giallo);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.chip {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.03em;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.chip[data-state='live'] {
  color: var(--go);
  border-color: var(--go);
}
.chip[data-state='searching'] {
  color: var(--giallo);
  border-color: var(--giallo);
}
.chip[data-state='lost'],
.chip[data-state='denied'],
.chip[data-state='insecure'],
.chip[data-state='unsupported'],
.chip[data-state='unavailable'] {
  color: var(--rosso);
  border-color: var(--rosso);
}
.chip[data-state='test'] {
  color: var(--cool);
  border-color: var(--cool);
}

.btn-quiet {
  min-height: 40px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 16px;
  letter-spacing: 0.03em;
  cursor: pointer;
}

.btn-quiet:active {
  background: var(--raised);
}

.label {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---------------------------------------------------------------- segmented */

.seg {
  display: flex;
  gap: 3px;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 11px;
}

.seg button {
  flex: 1 1 0;
  min-width: 0;
  min-height: 46px;
  padding: 0 6px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 17px;
  letter-spacing: 0.02em;
  white-space: nowrap;
  cursor: pointer;
}

.seg button.on {
  background: var(--giallo);
  color: #17140d;
}

.topbar .seg button {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0 12px;
  font-size: 15px;
}

/* ---------------------------------------------------------------- stage */

.stage {
  display: grid;
  grid-template-columns: minmax(230px, 300px) minmax(0, 1fr) minmax(270px, 330px);
  gap: 16px;
  padding-block: 16px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  min-height: 0;
}

/* Garage */

.garage {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  min-width: 0;
}

.car-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  min-height: 0;
  padding-right: 4px;
  scrollbar-width: thin;
}

.car {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  min-height: 62px;
  padding: 9px 12px;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}

.car:active {
  background: var(--raised);
}

.car.selected {
  background: var(--raised);
  border-color: var(--giallo);
}

.car-code {
  display: grid;
  place-items: center;
  height: 42px;
  border-radius: 6px;
  background: #0d0c0b;
  box-shadow: inset 0 -4px 0 var(--brand);
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 18px;
  letter-spacing: 0.03em;
}

.car-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.car-name {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.25;
}

.car-engine {
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

/* Cluster */

.cluster {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 12px;
  min-height: 0;
  min-width: 0;
  padding: 16px 20px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: radial-gradient(ellipse 80% 70% at 50% 45%, #24221d 0%, var(--panel) 75%);
}

.car-head {
  text-align: center;
}

.car-make {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--brand);
}

.car-model {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(26px, 3vw, 42px);
  line-height: 1.05;
  text-wrap: balance;
}

.car-spec {
  margin-top: 4px;
  color: var(--muted);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.shift-lights {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.shift-lights i {
  width: 24px;
  height: 10px;
  border-radius: 3px;
  background: #2b2925;
}

.shift-lights i.on.g {
  background: var(--go);
  box-shadow: 0 0 10px var(--go);
}
.shift-lights i.on.a {
  background: var(--giallo);
  box-shadow: 0 0 10px var(--giallo);
}
.shift-lights i.on.r,
.shift-lights.flash i.on {
  background: var(--rosso);
  box-shadow: 0 0 12px var(--rosso);
}

.tach-wrap {
  display: grid;
  place-items: center;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.tach-wrap canvas {
  display: block;
  max-width: 100%;
}

.readouts {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.readout {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.bar {
  height: 8px;
  border-radius: 4px;
  background: #2b2925;
  overflow: hidden;
}

.bar i {
  display: block;
  height: 100%;
  background: var(--giallo);
  transform: scaleX(0);
  transform-origin: left;
}

.bar.boost i {
  background: var(--cool);
}

.value {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 18px;
  line-height: 1.1;
}

/* Controls */

.controls {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

input[type='range'] {
  width: 100%;
  height: 32px;
  accent-color: var(--giallo);
}

.start-btn {
  align-self: center;
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 128px;
  height: 128px;
  margin: 10px 0 6px;
  border: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 38%, #4a120e, #170605 72%);
  box-shadow:
    0 0 0 6px #2c2a26,
    0 0 0 9px var(--rosso),
    0 12px 30px rgba(0, 0, 0, 0.55);
  font-family: var(--font-display);
  text-transform: uppercase;
  cursor: pointer;
  transition: box-shadow 0.25s ease, transform 0.08s ease;
}

.start-btn:active {
  transform: scale(0.96);
}

.start-btn.busy {
  opacity: 0.7;
}

.start-btn.running {
  box-shadow:
    0 0 0 6px #2c2a26,
    0 0 0 9px var(--giallo),
    0 0 34px rgba(242, 194, 0, 0.35),
    0 12px 30px rgba(0, 0, 0, 0.55);
}

.start-small {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.22em;
  color: var(--muted);
}

.start-big {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.06em;
  line-height: 1.1;
}

.pedals {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex: 1 1 auto;
  min-height: 210px;
}

.pedal {
  position: relative;
  flex: 1 1 0;
  display: grid;
  place-items: end center;
  padding-bottom: 14px;
  border-radius: 14px;
  border: 1px solid #5a564e;
  overflow: hidden;
  background:
    repeating-linear-gradient(0deg, transparent 0 15px, rgba(0, 0, 0, 0.32) 15px 19px),
    linear-gradient(90deg, #6b675f, #aca89e 45%, #6b675f);
  color: #17140d;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  touch-action: none;
  cursor: pointer;
}

.pedal.gas {
  align-self: stretch;
  min-height: 210px;
}

.pedal.brake {
  flex: 1.3 1 0;
  height: 62%;
  min-height: 130px;
  border: 1px solid #5a564e;
}

.pedal span {
  position: relative;
}

.pedal-fill {
  position: absolute;
  inset: 0;
  background: linear-gradient(0deg, rgba(242, 194, 0, 0.9), rgba(242, 194, 0, 0.35));
  transform: scaleY(0);
  transform-origin: bottom;
  pointer-events: none;
}

.pedal.pressed {
  box-shadow: inset 0 0 0 3px var(--giallo);
}

.pedal.brake.pressed {
  box-shadow: inset 0 0 0 3px var(--rosso);
}

.hint {
  margin: 0;
  min-height: 2.9em;
  color: var(--muted);
  font-size: 14px;
}

/* ---------------------------------------------------------------- settings sheet */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  justify-content: flex-end;
  background: rgba(0, 0, 0, 0.55);
}

.sheet-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: min(540px, 100%);
  height: 100%;
  overflow-y: auto;
  padding-block: max(18px, env(safe-area-inset-top, 0px)) max(24px, env(safe-area-inset-bottom, 0px));
  padding-left: max(20px, env(safe-area-inset-left, 0px));
  padding-right: max(20px, env(safe-area-inset-right, 0px));
  background: var(--panel);
  border-left: 1px solid var(--line);
  user-select: text;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sheet-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 28px;
}

.sheet-grid {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.sliders {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 64px;
  gap: 8px 14px;
  align-items: center;
}

.sliders label {
  font-size: 15px;
}

.sliders output {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

#resetTone {
  align-self: flex-start;
}

.details {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 6px 18px;
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.details dt {
  color: var(--muted);
}

.details dd {
  margin: 0;
}

.fine {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.safety {
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom, 0px));
  z-index: 20;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 16px;
  border: 1px solid var(--rosso);
  border-radius: 10px;
  background: #2a1210;
}

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

@media (max-width: 1150px) {
  .stage {
    grid-template-columns: minmax(0, 1fr) minmax(260px, 320px);
    grid-template-rows: auto minmax(0, 1fr);
  }
  .garage {
    grid-column: 1 / -1;
  }
  .car-list {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0 0 6px;
  }
  .car {
    flex: 0 0 230px;
  }
}

@media (max-width: 760px) {
  html,
  body,
  .app {
    height: auto;
  }
  .app {
    min-height: 100%;
  }
  .topbar {
    flex-wrap: wrap;
  }
  .topbar-right {
    width: 100%;
    flex-wrap: nowrap;
    justify-content: space-between;
  }
  .topbar-right .seg {
    flex: 1 1 auto;
    min-width: 0;
  }
  .stage {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
  }
  .tach-wrap {
    height: min(86vw, 420px);
  }
  .controls {
    overflow: visible;
  }
  .readouts {
    gap: 10px;
  }
  .sliders {
    grid-template-columns: minmax(0, 1fr) 64px;
  }
  .sliders label {
    grid-column: 1 / -1;
  }
}

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