:root {
  --bg: #0B0D10;
  --panel: #12161B;
  --panel-2: #1A2027;
  --line: #2A323B;
  --text: #F4F1EA;
  --muted: #9AA3AE;
  --amber: #FFB020;
  --amber-dim: #3A2A08;
  --red: #FF3B30;
  --green: #35D07F;
  --tap: 48px;
  --radius: 14px;
  --font: "SF Pro Display", "Helvetica Neue", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior: none;
}
body { -webkit-tap-highlight-color: transparent; }

button, input, output, label { font: inherit; color: inherit; }
button { touch-action: manipulation; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: pointer; }

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

.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;
}

.app { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }

/* Header: solid, sticky, clear of the notch */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: calc(56px + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: #0B0D10;
  border-bottom: 1px solid #1B2229;
}
.brand { display: flex; align-items: center; gap: 10px; margin: 0; font-size: 19px; font-weight: 700; letter-spacing: .01em; white-space: nowrap; }
.brand-mark { border-radius: 7px; display: block; }
.header-right { display: flex; align-items: center; gap: 6px; }

.pill {
  display: inline-flex; align-items: center; gap: 8px;
  height: 32px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--panel);
  font-size: 13px; font-weight: 600; letter-spacing: .02em; white-space: nowrap;
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill[data-state="live"] .dot { background: var(--green); }
.pill[data-state="sim"] .dot { background: var(--amber); }
.pill[data-state="locating"] .dot { background: var(--amber); }
.pill[data-state="lost"] .dot { background: var(--red); }
.pill[data-state="live"] .dot, .pill[data-state="locating"] .dot { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.icon-btn {
  width: var(--tap); height: var(--tap);
  display: inline-grid; place-items: center;
  background: transparent; border: 0; border-radius: 12px; padding: 0;
}
.icon-btn:active { background: var(--panel-2); }
.stroke { fill: none; stroke: var(--text); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Stage */
.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 6px max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: radial-gradient(120% 55% at 50% 12%, #161B21 0%, #0B0D10 62%);
}

.instrument { flex: 1 1 0; min-height: 230px; min-width: 0; display: flex; flex-direction: column; }
.tach-wrap { position: relative; flex: 1 1 0; min-height: 0; }
#tach { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.track { fill: none; stroke: #1E252D; stroke-width: 10; stroke-linecap: butt; }
.redzone { fill: none; stroke: var(--red); stroke-width: 10; stroke-linecap: butt; }
.tick { stroke: #C9C5BC; stroke-width: 1.6; }
.tick.major { stroke: var(--text); stroke-width: 3; }
.tick.hot { stroke: var(--red); }
.tick-label { fill: var(--text); font-size: 20px; font-weight: 700; text-anchor: middle; font-variant-numeric: tabular-nums; }
.tick-label.hot { fill: var(--red); }
.needle-body { fill: var(--amber); }
.needle-glow { stroke: var(--amber); stroke-width: 9; stroke-linecap: round; opacity: .16; fill: none; }
#needle.hot .needle-body { fill: var(--red); }
#needle.hot .needle-glow { stroke: var(--red); }
.badge rect { fill: #1E252D; stroke: var(--line); stroke-width: 1; }
.badge text { fill: var(--amber); font-size: 15px; font-weight: 800; text-anchor: middle; }
.gear-num { fill: var(--text); font-size: 118px; font-weight: 800; text-anchor: middle; font-variant-numeric: tabular-nums; }
.rpm-num { fill: var(--text); font-size: 38px; font-weight: 700; text-anchor: middle; font-variant-numeric: tabular-nums; }
.rpm-num.limit { fill: var(--red); }
.rpm-label { fill: var(--muted); font-size: 14px; font-weight: 700; letter-spacing: .22em; text-anchor: middle; }

.speed { display: flex; align-items: baseline; justify-content: center; gap: 10px; padding-top: 2px; }
.speed-num { font-size: clamp(54px, 9vh, 92px); font-weight: 800; line-height: 1; min-width: 3ch; text-align: right; font-variant-numeric: tabular-nums; }
.speed-unit { font-size: 18px; font-weight: 700; letter-spacing: .14em; color: var(--muted); min-width: 4.4ch; }

/* Controls */
.controls { display: flex; flex-direction: column; gap: 10px; min-width: 0; }

.picker {
  display: flex; gap: 10px; overflow-x: auto; padding: 2px 0 4px;
  scroll-snap-type: x proximity; scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.picker::-webkit-scrollbar { display: none; }
.card {
  flex: 0 0 auto; width: 160px; min-height: 82px;
  display: flex; flex-direction: column; align-items: flex-start; gap: 3px;
  padding: 10px 12px; text-align: left;
  background: var(--panel); border: 2px solid var(--line); border-radius: var(--radius);
  scroll-snap-align: start;
}
.card-name { font-size: 16px; font-weight: 700; line-height: 1.2; }
.card-blurb { font-size: 12px; line-height: 1.25; color: var(--muted); }
.card-cyl { margin-top: auto; font-size: 11px; font-weight: 600; letter-spacing: .04em; color: #7C8692; }
.card[aria-checked="true"] { border-color: var(--amber); background: #19140A; }
.card[aria-checked="true"] .card-name { color: var(--amber); }

.btn-start {
  position: relative; overflow: hidden;
  height: 60px; width: 100%; border: 0; border-radius: var(--radius);
  background: var(--amber); color: #0B0D10;
  font-size: 21px; font-weight: 800; letter-spacing: .02em;
}
.btn-start:active { filter: brightness(.9); }
.btn-start[data-state="running"] { background: var(--red); }
.btn-start[data-state="interrupted"] { background: #FFD27A; }
.btn-start-fill { position: absolute; inset: 0 auto 0 0; width: 0; background: rgba(11, 13, 16, .22); }
.btn-start[data-state="loading"] .btn-start-fill { width: calc(var(--p, 0) * 100%); }
.btn-start-text { position: relative; }

.row-two { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 10px; }
.btn-rev {
  height: 56px; border: 2px solid var(--amber); border-radius: var(--radius);
  background: var(--panel); color: var(--amber);
  font-size: 17px; font-weight: 800; letter-spacing: .02em;
}
.btn-rev.on, .btn-rev:active { background: var(--amber); color: #0B0D10; }

.seg { display: flex; padding: 4px; gap: 4px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); min-width: 0; }
.seg button {
  flex: 1 1 0; min-height: 46px; min-width: 0; padding: 0 10px;
  background: transparent; border: 2px solid transparent; border-radius: 10px;
  font-size: 15px; font-weight: 700; color: var(--muted);
}
.seg button[aria-checked="true"] { background: #262D36; border-color: var(--amber); color: var(--amber); }

.paddles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.paddle {
  height: 60px; display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--panel-2); border: 2px solid var(--line); border-radius: var(--radius);
  font-size: 18px; font-weight: 800;
}
.paddle:active { background: var(--amber); border-color: var(--amber); color: #0B0D10; }
.paddle:active .stroke { stroke: #0B0D10; }

.volume-row { display: flex; align-items: center; gap: 12px; min-height: 44px; }
.volume-row label { font-size: 14px; font-weight: 700; color: var(--muted); letter-spacing: .04em; }
.volume-row output { min-width: 4ch; text-align: right; font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }

input[type="range"] { -webkit-appearance: none; appearance: none; flex: 1 1 auto; min-width: 0; height: 44px; margin: 0; background: transparent; }
input[type="range"]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: #2A323B; }
input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: #2A323B; }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 28px; height: 28px; margin-top: -11px; border-radius: 50%; background: var(--amber); border: 3px solid #0B0D10; box-shadow: 0 0 0 1px var(--amber); }
input[type="range"]::-moz-range-thumb { width: 22px; height: 22px; border-radius: 50%; background: var(--amber); border: 3px solid #0B0D10; }

/* Toasts */
.toasts {
  position: fixed; left: 0; right: 0; z-index: 40;
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 0 max(12px, env(safe-area-inset-left)); pointer-events: none;
}
.notice, .toast {
  pointer-events: auto; margin: 0; max-width: 520px;
  background: #1B2128; border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0, 0, 0, .55);
}
.notice { padding: 12px 16px; font-size: 14px; line-height: 1.35; }
.toast { display: flex; align-items: center; gap: 14px; padding: 6px 6px 6px 18px; border-color: var(--amber); }
.toast-text { font-size: 15px; font-weight: 700; }
.toast-btn { min-height: 44px; min-width: 88px; padding: 0 18px; border: 0; border-radius: 10px; background: var(--amber); color: #0B0D10; font-size: 15px; font-weight: 800; }

/* Settings sheet */
.sheet {
  width: 100%; max-width: 560px; max-height: 88vh; max-height: 88dvh;
  margin: auto auto 0; padding: 0; overflow: auto;
  background: #12161B; color: var(--text);
  border: 1px solid var(--line); border-bottom: 0; border-radius: 22px 22px 0 0;
}
.sheet::backdrop { background: rgba(0, 0, 0, .66); }
.sheet-body { padding: 8px max(18px, env(safe-area-inset-right)) max(22px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left)); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; min-height: 56px; }
.sheet-head h2 { margin: 0; font-size: 22px; font-weight: 800; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 14px; min-height: 60px; padding: 6px 0; border-top: 1px solid #1F262E; }
.row-col { flex-direction: column; align-items: stretch; justify-content: center; gap: 4px; }
.row-line { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row-label { font-size: 16px; font-weight: 600; }
.row .seg { flex: 0 1 240px; }
.sim-out { font-size: 16px; font-weight: 700; color: var(--amber); font-variant-numeric: tabular-nums; }
.help { margin: 0 0 8px; font-size: 14px; line-height: 1.45; color: var(--muted); }
.row-col .help { margin: 0; }
.about-title { margin: 14px 0 8px; padding-top: 16px; border-top: 1px solid #1F262E; font-size: 13px; font-weight: 800; letter-spacing: .14em; color: var(--muted); }

.switch { position: relative; flex: 0 0 auto; width: 64px; height: 44px; padding: 0; background: #262D36; border: 2px solid var(--line); border-radius: 999px; }
.switch-knob { position: absolute; top: 4px; left: 4px; width: 32px; height: 32px; border-radius: 50%; background: var(--muted); transition: transform .15s ease, background .15s ease; }
.switch[aria-checked="true"] { background: var(--amber-dim); border-color: var(--amber); }
.switch[aria-checked="true"] .switch-knob { transform: translateX(20px); background: var(--amber); }

/* Landscape phone and Tesla browser: instrument left, controls right */
@media (orientation: landscape) and (min-width: 700px) {
  .stage { flex-direction: row; align-items: stretch; gap: 22px; }
  .instrument { flex: 1.1 1 0; min-height: 0; }
  .controls { flex: 1 1 0; max-width: 580px; justify-content: center; }
  .speed-num { font-size: clamp(48px, 13vh, 96px); }
}
@media (orientation: landscape) and (max-height: 500px) {
  .app-header { min-height: calc(46px + env(safe-area-inset-top)); }
  .brand { font-size: 17px; }
  .stage { gap: 16px; padding-top: 4px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .controls { gap: 8px; }
  .card { min-height: 76px; padding: 8px 12px; }
  .btn-start { height: 52px; font-size: 19px; }
  .btn-rev { height: 48px; }
  .seg button { min-height: 44px; }
  .paddle { height: 52px; }
  .volume-row { min-height: 44px; }
  .speed-num { font-size: 52px; }
}
@media (min-width: 1000px) and (min-height: 700px) {
  :root { --tap: 56px; }
  .app-header { min-height: calc(68px + env(safe-area-inset-top)); }
  .brand { font-size: 22px; }
  .pill { height: 36px; font-size: 15px; }
  .stage { padding-top: 14px; gap: 32px; }
  .controls { gap: 14px; max-width: 600px; }
  .card { width: 190px; min-height: 100px; }
  .card-name { font-size: 18px; }
  .card-blurb { font-size: 13px; }
  .btn-start { height: 72px; font-size: 24px; }
  .btn-rev { height: 64px; font-size: 19px; }
  .seg button { min-height: 52px; font-size: 17px; }
  .paddle { height: 76px; font-size: 21px; }
  .speed-unit { font-size: 22px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
