/* Sizes adapt to the browser window. Tesla's browser (firmware 2026.26+, page zoom about 1.5×)
   gives roughly 1256×706 CSS px parked and only about 773×601 while driving, and the window
   changes size live when the car shifts gear. Three size classes:
   - wide:    the default below (parked, or older firmware with a larger window)
   - compact: max-width 999px or max-height 649px (the driving window)
   - tiny:    max-width 639px or max-height 459px (phones, or an even smaller window) */
:root {
  --panel: rgba(255, 255, 255, 0.96);
  --panel-text: #111418;
  --panel-muted: #5b6472;
  --panel-line: rgba(0, 0, 0, 0.12);
  --accent: #3e8bff;
  --turn-bg: #1f4fa3;
  --danger: #d93838;
  --shadow: 0 4px 18px rgba(0, 0, 0, 0.25);

  --col: min(440px, 40vw);
  --gap: 16px;
  --fs: 20px;
  --fs-small: 16px;
  --btn: 72px;
  --search-h: 68px;
  --search-fs: 22px;
  --row-h: 68px;
  --card-title: 26px;
  --primary-h: 72px;
  --primary-fs: 26px;
  --secondary-h: 56px;
  --turn-min: 120px;
  --turn-arrow: 88px;
  --turn-dist: 40px;
  --turn-text: 22px;
  --trip-big: 30px;
  --trip-h: 92px;
  --end-h: 64px;
  --kb-h: min(46vh, 430px);
  --kb-gap: 8px;
  --kb-fs: 26px;
  --speed-h: 76px;
  --speed-w: 170px;
  --lane: 40px;
}

@media (max-width: 999px), (max-height: 649px) {
  :root {
    --col: clamp(290px, 44vw, 360px);
    --gap: 10px;
    --fs: 17px;
    --fs-small: 14px;
    --btn: 58px;
    --search-h: 54px;
    --search-fs: 19px;
    --row-h: 54px;
    --card-title: 21px;
    --primary-h: 58px;
    --primary-fs: 22px;
    --secondary-h: 46px;
    --turn-min: 88px;
    --turn-arrow: 60px;
    --turn-dist: 30px;
    --turn-text: 18px;
    --trip-big: 23px;
    --trip-h: 72px;
    --end-h: 50px;
    --kb-h: 50vh;
    --kb-gap: 6px;
    --kb-fs: 22px;
    --speed-h: 60px;
    --speed-w: 136px;
    --lane: 32px;
  }
}

@media (max-width: 639px), (max-height: 459px) {
  :root {
    --col: min(300px, calc(100vw - 20px));
    --fs: 15px;
    --fs-small: 13px;
    --btn: 48px;
    --search-h: 46px;
    --search-fs: 17px;
    --row-h: 46px;
    --card-title: 18px;
    --primary-h: 48px;
    --primary-fs: 19px;
    --secondary-h: 40px;
    --turn-min: 70px;
    --turn-arrow: 46px;
    --turn-dist: 24px;
    --turn-text: 15px;
    --trip-big: 19px;
    --trip-h: 60px;
    --end-h: 42px;
    --kb-h: 54vh;
    --kb-gap: 4px;
    --kb-fs: 18px;
    --speed-h: 48px;
    --speed-w: 108px;
    --lane: 26px;
  }
}

:root[data-theme="night"] {
  --panel: rgba(24, 28, 36, 0.96);
  --panel-text: #f2f4f8;
  --panel-muted: #9aa3b2;
  --panel-line: rgba(255, 255, 255, 0.14);
  --turn-bg: #1a3f82;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1b2330;
  font: var(--fs)/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#map {
  position: fixed;
  inset: 0;
}

/* ---------- left column (driver's side) ---------- */

.left {
  position: fixed;
  top: var(--gap);
  left: var(--gap);
  bottom: var(--gap);
  width: var(--col);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  z-index: 3;
  pointer-events: none; /* the map stays touchable between panels */
}
.left > * { pointer-events: auto; }
.left-spacer { flex: 1; pointer-events: none; }
body.kb-open .left { bottom: calc(var(--kb-h) + env(safe-area-inset-bottom, 0px) + var(--gap)); }

.panel {
  background: var(--panel);
  color: var(--panel-text);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

/* search bar */
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--search-h);
  padding: 0 8px 0 16px;
  border-radius: 16px;
  background: var(--panel);
  color: var(--panel-text);
  box-shadow: var(--shadow);
  cursor: text;
  flex-shrink: 0;
}
.search-icon { width: 26px; height: 26px; flex-shrink: 0; color: var(--panel-muted); }
.search-text { font-size: var(--search-fs); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-text.placeholder { color: var(--panel-muted); }
.caret { display: none; width: 2px; height: 1.3em; background: var(--accent); animation: blink 1s steps(1) infinite; flex-shrink: 0; }
.search-bar.open .caret { display: block; }
.search-bar.open { outline: 3px solid var(--accent); }
.search-bar #search-clear { margin-left: auto; }
/* Like a normal text field: with the field empty, the caret stands before the placeholder. */
.search-icon { order: 0; }
.search-text { order: 2; }
/* The caret sits 2 px after the last letter (the bar's 10 px gap is taken back), and with the field
   empty it stands where the first letter will appear: no space-like gap before or after the text. */
.caret { order: 3; margin-left: -8px; }
.search-bar #search-clear { order: 4; }
.search-bar.open .search-text.placeholder ~ .caret { order: 1; margin-left: 0; margin-right: -8px; }
body:not([data-mode="idle"]):not([data-mode="preview"]) .search-bar { display: none; }
@keyframes blink { 50% { opacity: 0; } }

/* In the smaller windows the idle search bar is a short pill, leaving the map clear. */
@media (max-width: 999px), (max-height: 649px) {
  body[data-mode="idle"]:not(.kb-open) .search-bar { align-self: flex-start; width: min(100%, 250px); }
}

.icon-btn {
  min-width: 48px;
  min-height: 48px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--panel-muted);
  font-size: 20px;
}

/* results */
.results {
  overflow-y: auto;
  padding: 4px;
  min-height: 0;
  flex-shrink: 1;
}
.result {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: var(--row-h);
  padding: 6px 10px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  text-align: left;
}
.result:active { background: var(--panel-line); }
.result-icon { width: 28px; text-align: center; font-size: 1.2em; color: var(--accent); flex-shrink: 0; }
.result-body { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.result-main { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result-sub { font-size: var(--fs-small); color: var(--panel-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result-dist { font-size: var(--fs-small); color: var(--panel-muted); flex-shrink: 0; }
.result-heading { padding: 8px 10px 2px; font-size: var(--fs-small); font-weight: 700; color: var(--panel-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.result-message { margin: 0; padding: 12px; color: var(--panel-muted); }
.result-detail { display: block; margin-top: 6px; font-size: var(--fs-small); font-family: ui-monospace, Consolas, monospace; }

/* destination card */
.card { position: relative; padding: calc(var(--gap) + 4px); flex-shrink: 1; min-height: 0; overflow-y: auto; }
.card-close { position: absolute; top: 4px; right: 4px; }
.card-title { margin: 0 44px 4px 0; font-size: var(--card-title); line-height: 1.2; overflow-wrap: anywhere; }
.card-sub { margin: 0 0 4px; color: var(--panel-muted); font-size: var(--fs-small); }
.card-dist { margin: 0 0 10px; font-weight: 600; }
.primary-btn {
  width: 100%;
  min-height: var(--primary-h);
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: var(--primary-fs);
  font-weight: 700;
}
.card-note { margin: 4px 0 10px; color: var(--panel-muted); font-size: var(--fs-small); }
.card-row { display: flex; gap: 8px; margin-top: 8px; }
/* Charger card (plan 2026-11-12, P2 addendum): plugs as chips, CCS2 in green like its icon. */
.plugs { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 8px; }
.plug { padding: 4px 10px; border-radius: 999px; border: 2px solid var(--panel-line); font-weight: 700; font-size: var(--fs-small); }
.plug-ccs2 { border-color: #1f9d55; background: color-mix(in srgb, #1f9d55 18%, transparent); }
.card-fast { margin: 2px 0 2px; color: #1f9d55; font-weight: 800; }

/* route choice */
.route-list { display: flex; flex-direction: column; gap: 6px; margin: 4px 0 10px; }
.route-option {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  gap: 0 10px;
  width: 100%;
  padding: 8px 12px;
  border: 2px solid var(--panel-line);
  border-radius: 12px;
  background: transparent;
  text-align: left;
}
.route-option.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.route-main { font-weight: 700; }
.route-delta { font-size: var(--fs-small); color: var(--panel-muted); }
.route-option.selected .route-delta { color: var(--accent); font-weight: 700; }
.route-traffic { grid-column: 1 / -1; font-size: var(--fs-small); font-weight: 700; color: var(--danger); }
.route-via { grid-column: 1 / -1; font-size: var(--fs-small); color: var(--panel-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.secondary-btn {
  flex: 1;
  min-height: var(--secondary-h);
  padding: 4px 6px;
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  background: transparent;
  font-size: var(--fs-small);
}
/* "Send to car", on a phone paired with the car: full width under Go. */
.card-send { width: 100%; margin-top: 8px; color: inherit; font-weight: 700; border-color: var(--accent); }

/* turn banner */
.turn {
  background: var(--turn-bg);
  color: #fff;
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  flex-shrink: 0;
}
.turn-main { display: flex; align-items: center; gap: 12px; padding: 10px 16px; min-height: var(--turn-min); }
.turn-arrow { flex-shrink: 0; }
.turn-arrow svg { width: var(--turn-arrow); height: var(--turn-arrow); display: block; }
.turn-body { min-width: 0; }
.turn-dist { font-size: var(--turn-dist); font-weight: 800; line-height: 1.1; }
.turn-text { font-size: var(--turn-text); font-weight: 600; }
.turn-text.only { font-size: calc(var(--turn-text) + 2px); }
.turn-street { font-size: var(--fs-small); opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.turn-then {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  background: rgba(0, 0, 0, 0.22);
  font-size: var(--fs-small);
}
.turn-then svg { width: 22px; height: 22px; flex-shrink: 0; }

/* lane arrows: usable lanes bright, the others dimmed */
.turn-lanes {
  display: flex;
  justify-content: center;
  gap: 4px;
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.18);
}
.lane {
  width: var(--lane);
  height: var(--lane);
  padding: 3px;
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.35);
}
.lane.valid { color: #fff; background: rgba(255, 255, 255, 0.16); }
.lane svg { width: 100%; height: 100%; display: block; }
@media (max-width: 639px), (max-height: 459px) {
  .turn-then { display: none; }
}

/* trip bar */
.trip {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--trip-h);
  padding: 8px 8px 8px 16px;
  flex-shrink: 0;
}
.trip-stat { flex: 1; min-width: 0; }
.trip-big { font-size: var(--trip-big); font-weight: 800; line-height: 1.1; white-space: nowrap; }
.trip-small { font-size: var(--fs-small); color: var(--panel-muted); white-space: nowrap; }
.end-btn {
  min-height: var(--end-h);
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--danger);
  color: #fff;
  font-weight: 700;
  flex-shrink: 0;
}

/* ---------- map controls rail (bottom left, above the trip bar) ---------- */

.rail {
  position: fixed;
  left: var(--gap);
  bottom: calc(var(--gap) + 8px);
  display: flex;
  gap: var(--gap);
  z-index: 2;
}
body[data-mode="navigating"] .rail { bottom: calc(var(--trip-h) + var(--gap) * 2); }
body.kb-open .rail { display: none; }

.round-btn {
  width: var(--btn);
  height: var(--btn);
  border-radius: 50%;
  border: 1px solid var(--panel-line);
  background: var(--panel);
  color: var(--panel-text);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  padding: 0;
}
.round-btn svg { width: 45%; height: 45%; }
.lang-btn { font-size: calc(var(--btn) * 0.28); font-weight: 700; }

/* map options panel (plan 2026-11-11, M1), above the rail */
.options {
  position: fixed;
  left: var(--gap);
  bottom: calc(var(--gap) * 2 + 8px + var(--btn));
  width: min(460px, calc(100vw - var(--gap) * 2));
  max-height: calc(100vh - var(--btn) - var(--gap) * 4);
  overflow-y: auto;
  padding: var(--gap);
  z-index: 6;
}
body[data-mode="navigating"] .options { bottom: calc(var(--trip-h) + var(--gap) * 3 + var(--btn)); }
body.kb-open .options { display: none; }
.opt-head { display: flex; align-items: center; justify-content: space-between; }
.opt-title { margin: 0; font-size: var(--fs); }
.opt-label { margin: 10px 0 6px; font-size: var(--fs-small); font-weight: 700; color: var(--panel-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.opt-row { display: flex; gap: 8px; }
.opt-wrap { flex-wrap: wrap; }
.opt-choice, .opt-toggle {
  flex: 1 1 0;
  min-height: var(--secondary-h);
  padding: 4px 10px;
  border: 2px solid var(--panel-line);
  border-radius: 12px;
  background: transparent;
  color: var(--panel-text);
  font-size: var(--fs-small);
}
.opt-wrap .opt-toggle { flex: 1 1 40%; }
.opt-choice.on, .opt-toggle.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); font-weight: 700; }
.opt-toggle:disabled { opacity: 0.4; }
/* Optimal settings (plan 2026-11-12): 🚗 / 💻 markers, the legend, this device, the reset button. */
.opt-choice:disabled { opacity: 0.4; }
.opt-best { margin-left: 5px; font-size: 0.9em; }
.opt-legend, .opt-device, .opt-note { margin: 2px 0; font-size: var(--fs-small); color: var(--panel-muted); }
.opt-device { color: var(--panel-text); font-weight: 600; }
.opt-optimal { margin-top: 12px; border-color: var(--accent); font-weight: 700; }
/* Phone pairing code (plan 2026-11-12, P1): black on white in both themes, so any phone camera reads it. */
.opt-ok { color: #2f9e44; text-transform: none; letter-spacing: 0; }
.phone-qr { width: 240px; height: 240px; margin: 10px auto 6px; background: #fff; border-radius: 8px; color: #111; font-size: 12px; overflow-wrap: anywhere; }
.phone-qr svg { display: block; width: 100%; height: 100%; }
.phone-hint { margin: 6px 0 10px; text-align: center; font-size: var(--fs-small); }
.phone-hint.small { color: var(--panel-muted); margin-top: 10px; }

/* ---------- keyboard ---------- */

.keyboard {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* On phones the bottom row stays clear of the home bar (0 in the car). */
  height: calc(var(--kb-h) + env(safe-area-inset-bottom, 0px));
  padding: var(--kb-gap) var(--kb-gap) calc(var(--kb-gap) + 2px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: var(--kb-gap);
  background: #16191f;
  box-shadow: 0 -4px 18px rgba(0, 0, 0, 0.35);
  z-index: 5;
  touch-action: none;
}
.kb-row { flex: 1; display: flex; gap: var(--kb-gap); justify-content: center; min-height: 0; }
.kb-key {
  position: relative;
  flex: 1 1 0;
  max-width: 110px;
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: #3a3f4a;
  color: #fff;
  font-size: var(--kb-fs);
  touch-action: none;
}
.kb-key.pressed { background: #5a6170; }
.kb-fn { background: #262a32; flex-grow: 1.5; max-width: 160px; font-size: calc(var(--kb-fs) * 0.7); font-weight: 700; }
.kb-fn svg { width: 1.6em; height: 1.6em; }
.kb-shift.active { background: #e8ebf0; color: #111; }
.kb-space { flex-grow: 5; max-width: 520px; font-size: calc(var(--kb-fs) * 0.7); color: #c9ced8; }
.kb-close {
  position: absolute;
  right: 12px;
  top: -60px;
  width: 50px;
  height: 50px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: #16191f;
  color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  touch-action: none;
}
.kb-close svg { width: 28px; height: 28px; }
.kb-hint { position: absolute; top: 2px; right: 5px; font-size: calc(var(--kb-fs) * 0.5); color: #aab1bf; }

/* ---------- speed badge (bottom right, above the Mapbox logo) ---------- */

.speed {
  position: fixed;
  right: var(--gap);
  bottom: calc(var(--gap) + 26px);
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 2;
  pointer-events: none;
}
body.kb-open .speed { display: none; }
/* Camera ahead (plan 2026-11-12, P3): above the speed badge. */
.cam-alert {
  position: fixed;
  right: var(--gap);
  bottom: calc(var(--gap) + 26px + var(--speed-h) + 10px);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px 6px 8px;
  border-radius: 999px;
  background: #e8590c;
  color: #fff;
  font-weight: 800;
  font-size: var(--fs);
  z-index: 2;
  pointer-events: none;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}
.cam-alert svg { width: 30px; height: 30px; }
.cam-alert .cam-limit { min-width: 1.9em; padding: 0 4px; border-radius: 999px; background: #fff; color: #111; border: 3px solid #d93838; text-align: center; font-size: 0.85em; }
body.kb-open .cam-alert { display: none; }
.speed-limit {
  width: var(--speed-h);
  height: var(--speed-h);
  border-radius: 50%;
  border: calc(var(--speed-h) * 0.11) solid #d93838;
  background: #fff;
  color: #111;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: calc(var(--speed-h) * 0.36);
  box-shadow: var(--shadow);
}
.speed-now {
  min-width: var(--speed-h);
  height: var(--speed-h);
  padding: 0 10px;
  border-radius: 14px;
  background: var(--panel);
  color: var(--panel-text);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.speed-value { font-size: calc(var(--speed-h) * 0.42); font-weight: 800; }
.speed-unit { font-size: calc(var(--speed-h) * 0.17); color: var(--panel-muted); margin-top: 2px; }
.speed.over .speed-now { background: #d93838; color: #fff; }
.speed.over .speed-unit { color: rgba(255, 255, 255, 0.85); }

/* ---------- status, toast, badges ---------- */

.banner, .toast {
  position: fixed;
  top: var(--gap);
  left: calc(var(--col) + var(--gap) * 2);
  max-width: min(560px, calc(100% - var(--col) - var(--gap) * 3));
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--panel);
  color: var(--panel-text);
  box-shadow: var(--shadow);
  font-weight: 600;
  z-index: 4;
}
.toast {
  top: auto;
  bottom: calc(var(--gap) + 8px);
  /* leave room for the speed badge on the right */
  max-width: min(560px, calc(100% - var(--col) - var(--gap) * 4 - var(--speed-w)));
}
body.kb-open .toast { bottom: calc(var(--kb-h) + env(safe-area-inset-bottom, 0px) + var(--gap) + 64px); }
.banner[data-kind="error"] { border-left: 6px solid var(--danger); }

/* Field-test tools (?trace=1, ?live=1): top right, under the badges */
.field-tools {
  position: fixed;
  top: calc(var(--gap) + 64px);
  right: var(--gap);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  z-index: 6;
}
.live-badge {
  padding: 4px 10px;
  border-radius: 8px;
  background: #1f7a3d;
  color: #fff;
  font: 700 13px/1.3 ui-monospace, Consolas, monospace;
}
.live-badge[data-state="bad"] { background: #a33; }
.trace-btn {
  min-height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  background: #f5b84b;
  color: #111;
  font-weight: 700;
  font-size: 15px;
}

/* ?perf=1 measurements, top right, clear of the left-column panels */
.perf {
  position: fixed;
  top: calc(var(--gap) + 34px);
  right: var(--gap);
  max-width: calc(100% - var(--col) - var(--gap) * 3);
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.72);
  color: #9ef0b0;
  font: 12px/1.3 ui-monospace, Consolas, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 6;
  pointer-events: none;
}

.version {
  position: fixed;
  right: 8px;
  top: 6px;
  font-size: 12px;
  color: rgba(128, 128, 128, 0.8);
  z-index: 2;
  pointer-events: none;
}

.sim-badge {
  position: fixed;
  top: var(--gap);
  right: var(--gap);
  padding: 4px 10px;
  border-radius: 8px;
  background: #f5b84b;
  color: #111;
  font-weight: 700;
  font-size: 14px;
  z-index: 2;
}

.fatal {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  padding: 16px;
  text-align: center;
  background: #0f1115;
  color: #f2f4f8;
  z-index: 10;
}
.fatal h1 { margin: 0; font-size: 1.6em; }
.fatal p { margin: 0; color: #9aa3b2; max-width: 640px; }
.fatal button {
  justify-self: center;
  min-height: var(--primary-h);
  padding: 0 32px;
  border-radius: 14px;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.car-marker {
  width: 48px;
  height: 48px;
}

/* Keep the required Mapbox logo and attribution clear of the rail. */
.mapboxgl-ctrl-bottom-right { margin-right: 4px; }
