:root {
  color-scheme: light;
  --page: #f3f2ef;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --grid: #e7e6e1;
  --series-1: #2a78d6;  /* speed, longitudinal */
  --series-2: #eb6834;  /* lateral */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --car-body: #c9c8c2;
  --car-glass: #7d8a96;
  --radius: 14px;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0f0f0e;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #2c2c2a;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8d8c84;
    --grid: #2c2c2a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --car-body: #4a4a46;
    --car-glass: #20262c;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0f0f0e;
  --surface: #1a1a19;
  --surface-2: #242422;
  --border: #2c2c2a;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --text-3: #8d8c84;
  --grid: #2c2c2a;
  --series-1: #3987e5;
  --series-2: #d95926;
  --car-body: #4a4a46;
  --car-glass: #20262c;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  font-variant-numeric: tabular-nums;
}
button { font: inherit; color: inherit; }

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 17px; margin-right: auto; }
.brand svg { width: 26px; height: 26px; }
.brand-ring { fill: var(--good); }
.brand-mark { stroke: #fff; stroke-width: 3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.sources { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.sources button {
  border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; cursor: pointer;
  color: var(--text-2); font-weight: 550; min-height: 36px;
}
.sources button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .15); }
.conn { display: flex; align-items: center; gap: 7px; color: var(--text-2); font-size: 14px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-3); }
.conn[data-state="live"] .dot { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }
.conn[data-state="connecting"] .dot { background: var(--warning); animation: pulse 1s infinite; }
.conn[data-state="error"] .dot { background: var(--critical); }
@keyframes pulse { 50% { opacity: .35; } }
.badge {
  background: color-mix(in srgb, var(--warning) 22%, transparent); color: var(--text);
  border: 1px solid color-mix(in srgb, var(--warning) 60%, transparent);
  padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 650; letter-spacing: .03em; text-transform: uppercase;
}
.ghost {
  border: 1px solid var(--border); background: var(--surface); border-radius: 9px;
  padding: 7px 12px; cursor: pointer; min-height: 36px; font-weight: 550;
}
.ghost:hover { background: var(--surface-2); }
.ghost.small { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.notice {
  margin: 12px 16px 0; padding: 10px 14px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text-2); font-size: 14px;
}

/* ---- grid ---- */
/* 12 columns on wide screens, 6 on tablets, 1 on phones */
.grid { display: grid; gap: 12px; padding: 12px 16px 16px; grid-template-columns: repeat(12, minmax(0, 1fr)); }
.fsd { grid-column: span 8; }
.speed { grid-column: span 4; }
.gforce, .roll, .steer { grid-column: span 4; }
.chart { grid-column: span 6; }
.map { grid-column: span 6; }
.trip, .events { grid-column: span 3; }
@media (max-width: 1100px) {
  .grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .fsd, .chart, .map { grid-column: span 6; }
  .speed, .gforce, .roll, .steer, .trip, .events { grid-column: span 3; }
}
@media (max-width: 640px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .grid > .card { grid-column: auto; }
}
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; min-width: 0; display: flex; flex-direction: column;
}
.card h2 {
  margin: 0 0 10px; font-size: 12px; font-weight: 650; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-3); display: flex; align-items: center; gap: 8px;
}
.tag {
  font-size: 11px; letter-spacing: .02em; text-transform: none; font-weight: 600;
  padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-2);
}
.readouts { display: flex; justify-content: space-between; gap: 12px; margin-top: auto; padding-top: 8px; }
.readouts div { display: flex; flex-direction: column; }
.readouts small, .fsd-meta small, .stats dt { color: var(--text-3); font-size: 12px; }
.readouts b { font-size: 20px; font-weight: 650; }

/* ---- FSD card ---- */
.fsd { position: relative; overflow: hidden; }
.fsd::before {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 6px; background: var(--text-3);
}
.fsd[data-mode="engaged"]::before { background: var(--good); }
.fsd[data-mode="available"]::before { background: var(--series-1); }
.fsd[data-mode="nag"]::before { background: var(--warning); }
.fsd[data-mode="takeover"]::before { background: var(--critical); }
.fsd[data-mode="aborted"]::before { background: var(--serious); }
.fsd-state { font-size: clamp(28px, 5vw, 44px); font-weight: 750; letter-spacing: -.01em; display: flex; align-items: center; gap: 12px; }
.fsd-state::before {
  content: ""; width: 18px; height: 18px; border-radius: 50%; background: var(--text-3); flex: none;
}
.fsd[data-mode="engaged"] .fsd-state::before { background: var(--good); }
.fsd[data-mode="available"] .fsd-state::before { background: var(--series-1); }
.fsd[data-mode="nag"] .fsd-state::before { background: var(--warning); }
.fsd[data-mode="takeover"] .fsd-state::before { background: var(--critical); }
.fsd[data-mode="aborted"] .fsd-state::before { background: var(--serious); }
.fsd-sub { color: var(--text-2); margin-top: 4px; font-size: 14px; }
.sep { margin: 0 8px; color: var(--text-3); }
.fsd-meta { display: flex; gap: 32px; margin-top: auto; padding-top: 14px; }
.fsd-meta div { display: flex; flex-direction: column; }
.fsd-meta b { font-size: 24px; font-weight: 650; }

/* ---- speed card ---- */
.hero { display: flex; align-items: baseline; gap: 8px; }
.hero #speed { font-size: clamp(56px, 9vw, 84px); font-weight: 750; line-height: 1; letter-spacing: -.03em; }
.hero .unit { color: var(--text-2); font-size: 18px; font-weight: 600; }
.gear-row { display: flex; justify-content: space-between; align-items: center; margin: 12px 0; }
.gears { display: flex; gap: 6px; }
.gears span {
  width: 30px; height: 30px; display: grid; place-items: center; border-radius: 8px;
  color: var(--text-3); font-weight: 700; background: var(--surface-2);
}
.gears span.on { background: var(--text); color: var(--surface); }
.turns { display: flex; gap: 10px; }
.turn { font-size: 22px; color: var(--surface-2); transition: color .08s; }
.turn.on { color: var(--good); }
.pedals { display: grid; grid-template-columns: 1fr auto; gap: 14px; align-items: end; margin-top: auto; }
.pedal { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-3); }
.pedal b { font-size: 14px; color: var(--text); }
.bar { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--series-1); border-radius: 4px; transition: width .1s; }
.brake-lamp {
  font-weight: 700; font-size: 13px; padding: 5px 12px; border-radius: 8px;
  background: var(--surface-2); color: var(--text-3); text-align: center; min-width: 56px;
}
.brake-lamp.on { background: var(--critical); color: #fff; }

/* ---- g-force / roll / steering ---- */
#gg { width: 100%; max-width: 240px; aspect-ratio: 1; align-self: center; }
.attitude { width: 100%; max-height: 150px; }
.attitude .ground { stroke: var(--grid); stroke-width: 2; stroke-dasharray: 4 6; }
.attitude .body { fill: var(--car-body); }
.attitude .glass { fill: var(--car-glass); }
.attitude .bumper { fill: color-mix(in srgb, var(--car-body) 80%, var(--text) 20%); }
.attitude .tire { fill: #111; }
.attitude .lamp { fill: var(--critical); }
#roll-car { transform-origin: 120px 128px; transition: transform .1s linear; }
.wheel { width: 100%; max-width: 150px; align-self: center; }
#wheel { transform-origin: 80px 80px; transition: transform .08s linear; }
.wheel .rim { fill: none; stroke: var(--text-2); stroke-width: 12; }
.wheel .spoke { stroke: var(--text-2); stroke-width: 10; stroke-linecap: round; }
.wheel .hub { fill: var(--surface-2); stroke: var(--text-2); stroke-width: 3; }
.wheel .marker { fill: var(--series-1); }

/* ---- charts ---- */
.chart-wrap { position: relative; height: 170px; }
.chart-wrap canvas { width: 100%; height: 100%; display: block; }
.legend { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); margin: -4px 0 6px; }
.key { width: 14px; height: 3px; border-radius: 2px; display: inline-block; margin-left: 8px; }
.key:first-child { margin-left: 0; }
.k-long { background: var(--series-1); }
.k-lat { background: var(--series-2); }
.tip {
  position: absolute; pointer-events: none; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 9px; font-size: 12px; color: var(--text); white-space: nowrap;
  box-shadow: 0 4px 14px rgb(0 0 0 / .2);
}
.tip b { font-weight: 650; }

/* ---- map ---- */
.map-box { position: relative; height: 260px; border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.map-box canvas { width: 100%; height: 100%; display: block; }
.map-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 10px; font-size: 13px; color: var(--text-2); flex-wrap: wrap; }
.leaflet-container { background: var(--surface-2); font: inherit; }
.car-dot { width: 16px; height: 16px; border-radius: 50%; background: var(--series-1); border: 3px solid #fff; box-shadow: 0 0 0 2px var(--series-1); }

/* ---- trip / events ---- */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; margin: 0; }
.stats div { display: flex; flex-direction: column; }
.stats dd { margin: 0; font-size: 20px; font-weight: 650; }
.reasons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; }
.reasons span { font-size: 12px; background: var(--surface-2); color: var(--text-2); padding: 3px 9px; border-radius: 999px; }
.event-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; max-height: 260px; }
.event-list li { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.event-list li:last-child { border-bottom: 0; }
.event-list li.empty { display: block; color: var(--text-3); border: 0; }
.event-list .ico { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; color: #fff; font-weight: 700; }
.event-list .ico.engaged { background: var(--good); }
.event-list .ico.takeover { background: var(--critical); }
.event-list .ico.aborted { background: var(--serious); }
.event-list .ico.park { background: var(--text-3); }
.event-list time { color: var(--text-3); font-size: 12px; }

/* ---- status bar ---- */
.statusbar {
  display: flex; flex-wrap: wrap; gap: 6px 18px; padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
  color: var(--text-3); font-size: 13px; border-top: 1px solid var(--border);
}
.statusbar b { color: var(--text-2); font-weight: 600; }
.statusbar a { color: var(--series-1); margin-left: auto; }

/* ---- Pi settings dialog ---- */
dialog#settings {
  width: min(460px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow-y: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: 0 20px 60px rgb(0 0 0 / .45);
}
dialog#settings::backdrop { background: rgb(0 0 0 / .5); }
.settings-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.settings-head h2 { margin: 0; font-size: 17px; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-2); margin-bottom: 10px; }
.field input {
  font: inherit; font-size: 15px; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-height: 38px; width: 100%;
}
#code { font-family: ui-monospace, monospace; letter-spacing: .12em; text-transform: uppercase; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row input { flex: 1; min-width: 0; }
.hint { font-size: 12px; color: var(--text-3); margin: -4px 0 12px; }
.hint code { font-size: 11px; }
.pi-status { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 0 0 12px; }
.pi-status dt { font-size: 12px; color: var(--text-3); }
.pi-status dd { margin: 0; font-weight: 600; font-size: 14px; }
.networks { list-style: none; padding: 0; margin: 12px 0 0; }
.networks li button {
  width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 10px;
  background: transparent; border: 0; border-bottom: 1px solid var(--border); padding: 10px 2px; cursor: pointer; text-align: left;
}
.networks li button:hover { background: var(--surface-2); }
.networks .meta { color: var(--text-3); font-size: 12px; white-space: nowrap; }
.join { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.check { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--text-2); margin-bottom: 12px; }
.primary {
  border: 0; background: var(--series-1); color: #fff; font-weight: 650; border-radius: 9px;
  padding: 9px 16px; min-height: 40px; cursor: pointer;
}
.primary:disabled, .ghost:disabled { opacity: .55; cursor: default; }
.link { background: none; border: 0; color: var(--series-1); padding: 10px 0 0; cursor: pointer; font-size: 13px; }
.settings-msg { font-size: 13px; color: var(--text-2); margin: 12px 0 0; min-height: 1.4em; }
.settings-msg.err { color: var(--critical); }
