:root {
  color-scheme: dark;
  --ink: #f6f1e7;
  --muted: #d9cbb8;
  --deep: #10212c;
  --glass: rgba(16, 28, 36, 0.62);
  --line: rgba(246, 241, 231, 0.2);
  --gold: #e7c48a;
  --ev: #d7e7c8;
  --ev-ink: #163024;
  --hybrid: #f3e0b5;
  --hybrid-ink: #5a3e12;
  --engine: #f0c2b0;
  --engine-ink: #6a2418;
  --safe-top: max(10px, env(safe-area-inset-top, 0px));
  --safe-right: max(12px, env(safe-area-inset-right, 0px));
  --safe-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  --safe-left: max(12px, env(safe-area-inset-left, 0px));
  --serif: "Iowan Old Style", Palatino, "Palatino Linotype", "Noto Serif", Georgia, serif;
  --sans: "Avenir Next", "Segoe UI", "Noto Sans", "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: #10212c;
  color: var(--ink);
  font-family: var(--sans);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button, input, label, summary { touch-action: manipulation; }

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

#view {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #143044;
  transition: opacity 280ms linear;
}

#view.is-fading { opacity: 0; }

button, .route-card {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

button:focus-visible, .route-card:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.noscript, .boot-error {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: 16px;
  padding: 16px;
  background: var(--deep);
  border: 1px solid var(--line);
  border-radius: 12px;
  z-index: 9;
}

.title-wrap {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 68%;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 22px var(--safe-right) calc(18px + env(safe-area-inset-bottom, 0px)) var(--safe-left);
  background: linear-gradient(to top, rgba(12, 22, 30, 0.94) 20%, rgba(12, 22, 30, 0.72) 62%, rgba(12, 22, 30, 0));
  touch-action: pan-y;
  z-index: 3;
}

.eyebrow {
  margin: 0;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.72rem;
  color: var(--gold);
}

h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 560;
  font-size: clamp(2.4rem, 7vw, 4.4rem);
  letter-spacing: -0.03em;
  line-height: 0.92;
}

h2 {
  margin: 0 0 8px;
  font-family: var(--serif);
  font-weight: 560;
  font-size: clamp(1.6rem, 4vw, 2.4rem);
}

.lede, .note, .fine, .help p, .card p { color: var(--muted); }

.lede { margin: 8px 0 0; max-width: 38rem; line-height: 1.45; }

.route-row { display: flex; flex-direction: column; gap: 8px; }

.route-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
  padding: 8px 12px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: rgba(246, 241, 231, 0.06);
  min-height: 44px;
}

.route-card:disabled {
  opacity: 0.55;
  cursor: default;
}

.route-kicker { font-size: 0.72rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--gold); }
.route-name { font-family: var(--serif); font-size: 1.25rem; }
.route-meta { color: var(--muted); font-size: 0.92rem; }

.best-line { margin: 4px 0; font-variant-numeric: tabular-nums; }

.sound-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
}

.sound-row button, .card button, .primary, .hud-actions button, .update button {
  min-height: 44px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: transparent;
  padding: 0 16px;
}

.primary {
  background: var(--ink);
  color: #1a2420;
  border-color: transparent;
  font-weight: 650;
}

.volume { display: flex; align-items: center; gap: 8px; }
.volume input { width: min(180px, 36vw); accent-color: var(--gold); }
.tilt { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.note, .fine { margin: 0; font-size: 0.85rem; }
.help { color: var(--muted); }
.help summary { min-height: 44px; display: flex; align-items: center; }

.hud {
  position: absolute;
  top: var(--safe-top);
  left: var(--safe-left);
  right: var(--safe-right);
  z-index: 3;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-areas:
    "speed eco"
    "place place"
    "actions actions";
  gap: 4px 12px;
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.cluster-speed { grid-area: speed; display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px; }
.cluster-place { grid-area: place; }
.cluster-eco { grid-area: eco; justify-self: end; text-align: right; }
.hud-actions {
  grid-area: actions;
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.speed-line, .fuel-line, .time-line, .soc-line, .place { margin: 0; }
.speed-line {
  font-family: var(--serif);
  font-size: clamp(2.4rem, 8vh, 4.2rem);
  line-height: 0.9;
  font-variant-numeric: tabular-nums;
}
.unit { font-family: var(--sans); font-size: 0.85rem; letter-spacing: 0.04em; color: var(--muted); }

.mode {
  margin: 0;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  font-weight: 700;
}
.mode-ev { background: var(--ev); color: var(--ev-ink); }
.mode-hybrid { background: var(--hybrid); color: var(--hybrid-ink); }
.mode-engine { background: var(--engine); color: var(--engine-ink); }

.flag {
  margin: 0;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.flag-boost { background: #f6e7c4; color: #5a3e12; }
.flag-regen { background: #d5e4ef; color: #163044; }

.place {
  font-family: var(--serif);
  font-size: clamp(1.2rem, 3vh, 1.8rem);
}
.place.is-new { animation: place-in 700ms ease; }

@keyframes place-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.soc-line { display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
.lbl { font-size: 0.75rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.soc-track {
  width: 92px;
  height: 8px;
  border-radius: 99px;
  background: rgba(246, 241, 231, 0.2);
  overflow: hidden;
}
.soc-fill { display: block; height: 100%; width: 0; background: #b7d7a8; }
.b0 { width: 0%; } .b1 { width: 5%; } .b2 { width: 10%; } .b3 { width: 15%; } .b4 { width: 20%; }
.b5 { width: 25%; } .b6 { width: 30%; } .b7 { width: 35%; } .b8 { width: 40%; } .b9 { width: 45%; }
.b10 { width: 50%; } .b11 { width: 55%; } .b12 { width: 60%; } .b13 { width: 65%; } .b14 { width: 70%; }
.b15 { width: 75%; } .b16 { width: 80%; } .b17 { width: 85%; } .b18 { width: 90%; } .b19 { width: 95%; }
.b20 { width: 100%; }
.b0, .b1, .b2, .b3, .b4, .b5 { background: #e7b15a; }

.fuel-line, .time-line { font-variant-numeric: tabular-nums; }

.touch-layer { display: none; position: fixed; inset: 0; z-index: 4; pointer-events: none; }

body[data-mode="drive"] .touch-layer { display: none; }

@media (pointer: coarse), (max-width: 1180px) {
  body[data-mode="drive"] .touch-layer { display: block; }
}

.steer-pad, .pedals { pointer-events: auto; position: absolute; }

.steer-pad {
  left: var(--safe-left);
  bottom: var(--safe-bottom);
  width: min(46vw, 300px);
  height: min(30vh, 210px);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 6px;
}

.steer-track {
  position: relative;
  height: 48px;
  border-radius: 999px;
  background: rgba(16, 28, 36, 0.45);
  border: 1px solid var(--line);
}

.knob {
  position: absolute;
  top: 50%;
  width: 36px;
  height: 36px;
  margin-left: -18px;
  margin-top: -18px;
  border-radius: 50%;
  background: var(--ink);
}

.k0 { left: 10%; } .k1 { left: 14%; } .k2 { left: 18%; } .k3 { left: 22%; } .k4 { left: 26%; }
.k5 { left: 30%; } .k6 { left: 34%; } .k7 { left: 38%; } .k8 { left: 42%; } .k9 { left: 46%; }
.k10 { left: 50%; } .k11 { left: 54%; } .k12 { left: 58%; } .k13 { left: 62%; } .k14 { left: 66%; }
.k15 { left: 70%; } .k16 { left: 74%; } .k17 { left: 78%; } .k18 { left: 82%; } .k19 { left: 86%; }
.k20 { left: 90%; }

.ctl-label {
  font-size: 0.75rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.pedals {
  right: var(--safe-right);
  bottom: var(--safe-bottom);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pedal {
  width: 118px;
  height: 92px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: rgba(16, 28, 36, 0.48);
  color: var(--ink);
  font-weight: 700;
  font-size: 1rem;
}

.pedal-go.is-down { background: var(--ev); color: var(--ev-ink); }
.pedal-brake.is-down { background: var(--engine); color: var(--engine-ink); }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 6;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: calc(var(--safe-top) + 8vh) var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: rgba(10, 16, 22, 0.46);
  overflow: auto;
  touch-action: pan-y;
}

.card {
  width: min(440px, 100%);
  background: rgba(16, 28, 36, 0.9);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card button { align-self: flex-start; }
.score {
  margin: 0;
  font-family: var(--serif);
  font-size: 4rem;
  line-height: 0.9;
}
.score-label { margin: 0; letter-spacing: 0.12em; text-transform: uppercase; font-size: 0.75rem; color: var(--gold); }
.stats { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; font-variant-numeric: tabular-nums; }

.update {
  position: fixed;
  left: var(--safe-left);
  right: var(--safe-right);
  bottom: var(--safe-bottom);
  z-index: 7;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: #143044;
  border: 1px solid var(--line);
  border-radius: 14px;
}

/* Folded portrait: narrow and tall, about 21:9. */
@media (max-aspect-ratio: 5/8) {
  .title-wrap { max-height: 62%; }
  .steer-pad { width: min(52vw, 220px); height: min(28vh, 200px); }
  .pedal { width: 104px; height: 84px; }
  .hud { grid-template-columns: 1fr auto; }
  .speed-line { font-size: 2.5rem; }
}

/* Folded landscape: short and very wide. */
@media (min-aspect-ratio: 8/5) and (max-height: 520px) {
  .title-wrap {
    max-height: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr);
    gap: 8px 18px;
    align-items: end;
    padding-top: 8px;
  }
  h1 { font-size: clamp(2rem, 6vh, 3rem); }
  .lede { display: none; }
  .hud {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "speed place eco" "actions place eco";
    align-items: start;
  }
  .cluster-eco { align-self: start; }
  .speed-line { font-size: 2.2rem; }
  .steer-pad { height: 46%; width: min(32vw, 240px); }
  .pedals { flex-direction: row; }
  .pedal { width: 92px; height: 64px; }
  .hud-actions button { min-height: 36px; }
}

/* Unfolded portrait: near-square, vertical hinge down the middle. */
@media (min-width: 640px) and (max-aspect-ratio: 1/1) and (min-aspect-ratio: 5/8) {
  .title-wrap {
    max-height: none;
    height: 48%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 10vw;
    align-items: end;
  }
  .hud {
    grid-template-columns: minmax(0, 1fr) 14vw minmax(0, 1fr);
    grid-template-areas:
      "speed . eco"
      "place . actions";
  }
  .cluster-eco { justify-self: end; }
  .hud-actions { justify-content: flex-end; }
  .steer-pad {
    width: calc(38vw - 24px);
    max-width: none;
    height: min(22vh, 210px);
    left: var(--safe-left);
  }
  .pedals { right: var(--safe-right); }
  .overlay { justify-content: flex-start; }
  .card { margin-left: 2vw; }
}

/* Unfolded landscape: hinge runs across the middle. Keep chrome in the outer halves. */
@media (min-width: 800px) and (min-aspect-ratio: 1/1) and (max-aspect-ratio: 8/5) {
  .title-wrap {
    top: 48%;
    bottom: 0;
    max-height: none;
    height: auto;
  }
  .hud {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "speed place eco" "actions place eco";
    max-height: 42%;
  }
  .steer-pad, .pedals {
    top: auto;
    bottom: var(--safe-bottom);
    height: auto;
  }
  .steer-pad { width: min(280px, 28vw); height: min(190px, 24%); }
  .pedals { flex-direction: column; }
}

@media (pointer: fine) and (min-width: 1200px) and (min-height: 640px) {
  .title-wrap {
    top: auto;
    height: auto;
    max-height: 48%;
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(0, 0.85fr);
    align-items: end;
    column-gap: 28px;
  }
  .lede { display: block; }
  .hud {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "speed place eco" "actions place eco";
    max-height: none;
  }
}

/* Real fold segments, where the browser exposes them. These win over the aspect fallbacks. */
@media (horizontal-viewport-segments: 2) {
  .title-wrap {
    top: auto;
    height: auto;
    max-height: 58%;
    display: grid;
    grid-template-columns: env(viewport-segment-width 0 0, 50%) env(viewport-segment-width 1 0, 50%);
    column-gap: 0;
  }
  .title-copy {
    padding-left: calc(env(viewport-segment-left 0 0, 0px) + 8px);
    padding-right: 18px;
  }
  .title-actions { padding-right: 18px; }
  .cluster-speed, .cluster-place, .hud-actions {
    position: absolute;
    left: calc(env(viewport-segment-left 0 0, 0px) + var(--safe-left));
    width: calc(env(viewport-segment-width 0 0, 46vw) - 28px);
  }
  .cluster-speed { top: var(--safe-top); }
  .cluster-place { top: calc(var(--safe-top) + 4.6rem); }
  .hud-actions { top: calc(var(--safe-top) + 7.4rem); }
  .cluster-eco {
    position: absolute;
    left: calc(env(viewport-segment-left 1 0, 54vw) + 12px);
    width: calc(env(viewport-segment-width 1 0, 46vw) - 28px);
    top: var(--safe-top);
    text-align: right;
  }
  .steer-pad {
    left: calc(env(viewport-segment-left 0 0, 0px) + var(--safe-left));
    width: calc(env(viewport-segment-width 0 0, 42vw) - 36px);
    bottom: var(--safe-bottom);
  }
  .pedals {
    right: auto;
    left: calc(env(viewport-segment-left 1 0, 58vw) + env(viewport-segment-width 1 0, 42vw) - 150px);
    bottom: var(--safe-bottom);
  }
  .overlay { justify-content: flex-start; }
  .card {
    margin-left: calc(env(viewport-segment-left 0 0, 0px) + 16px);
    margin-right: auto;
  }
}

@media (vertical-viewport-segments: 2) {
  .title-wrap {
    top: calc(env(viewport-segment-top 0 1, 50vh));
    height: calc(env(viewport-segment-height 0 1, 50vh));
    max-height: none;
    bottom: 0;
  }
  .hud {
    top: calc(env(viewport-segment-top 0 0, 0px) + var(--safe-top));
    max-height: calc(env(viewport-segment-height 0 0, 46vh) - 16px);
  }
  .steer-pad, .pedals {
    top: calc(env(viewport-segment-top 0 1, 54vh) + 12px);
    bottom: var(--safe-bottom);
    height: auto;
  }
  .steer-pad {
    left: calc(env(viewport-segment-left 0 1, 0px) + var(--safe-left));
    width: min(300px, 32vw);
  }
  .pedals { right: var(--safe-right); left: auto; }
}

@media (prefers-reduced-motion: reduce) {
  #view { transition: none; }
  .place.is-new { animation: none; }
}
