/* ---------- Fonts ---------- */
@font-face {
  font-family: 'Orbitron';
  src: url('../fonts/Orbitron-Variable.ttf') format('truetype');
  font-weight: 400 900;
  font-display: swap;
}
/* Fan-made Cybertruck logo font (letters only). Digits/punctuation fall through to Orbitron. */
@font-face {
  font-family: 'Cybertruck';
  src: url('../fonts/cybertruck.ttf') format('truetype');
  font-display: swap;
}

/* ---------- Tokens ---------- */
:root {
  --ppi: 122;
  --readout-in: 0.6;
  --cap-ratio: 0.72; /* Orbitron cap height ≈ 0.72 em */
  --readout-px: calc(var(--readout-in) * var(--ppi) / var(--cap-ratio) * 1px);
  --label-px: 10px; /* profile axis label font size (settings.labelPx) */
  --profile-h: clamp(130px, max(24vh, calc(var(--label-px) * 5.5)), 45vh);

  --font-display: 'Orbitron', system-ui, sans-serif; /* headings, labels, readouts */
  --font-numeric: 'Orbitron', system-ui, sans-serif; /* digits, units, chart labels */
  --font-body: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

  /* dark theme (default) */
  --surface: #121212;
  --surface-1: #1a1a19;
  --surface-2: #262625;
  --surface-glass: rgba(18, 18, 18, 0.72);
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8a8983;
  --grid: #2f2f2d;
  --border: #33332f;
  --accent: #3987e5; /* series hue on dark */
  --accent-soft: rgba(57, 135, 229, 0.12);
  --scrub: #f28c28;
  --up: #55c46a;
  --down: #ef6a5a;
  --rec: #e5484d;

  /* elevation-source identity (fixed order, never re-assigned) */
  --src-gps: #3987e5;
  --src-terrarium: #37b36a;
  --src-openmeteo: #e8a33d;
  --src-none: #6f6f6a;
}

:root[data-theme='light'] {
  --surface: #f6f6f4;
  --surface-1: #fcfcfb;
  --surface-2: #ebebe8;
  --surface-glass: rgba(252, 252, 251, 0.82);
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --grid: #e3e3df;
  --border: #d7d7d2;
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, 0.12);
  --scrub: #d9741a;
  --up: #1f8f3a;
  --down: #c9372c;
  --rec: #d4353b;
  --src-gps: #2a78d6;
  --src-terrarium: #1f8f4c;
  --src-openmeteo: #b8741a;
  --src-none: #8d8d87;
}

/* ---------- Base ---------- */
* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--surface);
  color: var(--text-primary);
  font-family: var(--font-body);
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button {
  font: inherit;
  color: inherit;
}
.hidden {
  display: none !important;
}
.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;
}
code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.9em;
  color: var(--text-secondary);
  word-break: break-all;
}

#app {
  position: fixed;
  inset: 0;
  height: 100dvh;
  display: grid;
  grid-template-rows: 1fr var(--profile-h);
}

/* ---------- Map ---------- */
#map-wrap {
  grid-row: 1;
  position: relative;
  overflow: hidden;
  background: var(--surface-1);
  z-index: 0;
}
/* The map fills the wrapper north-up. In heading-up mode JS enlarges it to the wrapper's
   diagonal and rotates it, so the corners stay covered while the vehicle's heading points up. */
#map {
  position: absolute;
  inset: 0;
  background: var(--surface-1);
  transform-origin: 50% 50%;
  transition: transform 0.6s ease;
  will-change: transform;
}
#map.rotating {
  inset: auto;
}
#zoom-ctl {
  position: absolute;
  left: 10px;
  bottom: calc(var(--profile-h) + 24px);
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#zoom-ctl button {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-glass);
  backdrop-filter: blur(8px);
  cursor: pointer;
  display: grid;
  place-items: center;
}
#zoom-ctl svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
}
#map-attrib {
  position: absolute;
  right: 4px;
  bottom: calc(var(--profile-h) + 2px);
  z-index: 500;
  font-size: 10px;
  padding: 2px 6px;
  background: var(--surface-glass);
  color: var(--text-muted);
  border-radius: 4px;
  pointer-events: auto;
  max-width: 60vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#map-attrib a {
  color: var(--text-secondary);
}
.leaflet-container {
  font-family: var(--font-body);
}

.live-marker {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
}
.live-marker .arrow {
  width: 0;
  height: 0;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 26px solid var(--accent);
  filter: drop-shadow(0 0 2px #fff) drop-shadow(0 0 3px rgba(0, 0, 0, 0.6));
  transform-origin: 50% 65%;
}
.live-marker .dotm {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid #fff;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.6);
}

/* ---------- Toolbar ---------- */
#toolbar {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  z-index: 500;
  max-width: 40vw;
}
.tb-btn {
  min-width: 56px;
  height: 56px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-glass);
  backdrop-filter: blur(8px);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 18px;
  letter-spacing: 0.06em;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.tb-btn.on {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}
.tb-btn svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tb-btn.on svg {
  stroke: var(--accent);
}
.tb-btn.rec .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-muted);
}
.tb-btn.rec.recording .dot {
  background: var(--rec);
  animation: pulse 1.2s infinite;
}
.tb-btn.rec.recording {
  border-color: var(--rec);
}
@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.35;
  }
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: 8px;
  right: 12px;
  z-index: 500;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  max-width: 78vw;
  pointer-events: none;
  text-align: right;
  font-family: var(--font-display);
  text-shadow:
    0 0 6px var(--surface),
    0 0 12px var(--surface),
    0 2px 2px rgba(0, 0, 0, 0.35);
}
.hud-mode {
  font-size: max(16px, calc(var(--readout-px) * 0.16));
  letter-spacing: 0.12em;
  color: var(--scrub);
  font-weight: 700;
}
.readout {
  line-height: 1;
}
/* Tap targets: the elevation figure swaps the elevation source, the grade figure swaps the grade series. */
.readout.primary,
.readout.grade {
  pointer-events: auto;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.readout.primary:active .readout-value,
.readout.grade:active .readout-value {
  opacity: 0.6;
}
.readout-value {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.readout.primary .readout-value {
  font-size: min(var(--readout-px), 34vw);
}
.readout.secondary .readout-value {
  font-size: min(calc(var(--readout-px) * 0.36), 14vw);
  color: var(--text-secondary);
  font-weight: 600;
}
.readout.grade .readout-value {
  font-size: min(calc(var(--readout-px) * 0.6), 22vw);
}
.readout-delta {
  color: var(--down);
  font-weight: 600;
  margin-right: 0.3em;
}
.readout-unit {
  font-family: var(--font-numeric);
  font-size: 0.32em;
  font-weight: 500;
  margin-left: 0.15em;
  color: var(--text-secondary);
}
/* The Cybertruck logo face is uppercase-only by design: when it is selected, uppercase labels set in it. */
:root[data-font='cybertruck'] .hud-mode,
:root[data-font='cybertruck'] .readout-tag,
:root[data-font='cybertruck'] #profile-legend,
:root[data-font='cybertruck'] #btn-live,
:root[data-font='cybertruck'] .tb-btn,
:root[data-font='cybertruck'] .panel h2,
:root[data-font='cybertruck'] .panel h3 {
  text-transform: uppercase;
}
#grade-arrow {
  display: inline-block;
  font-size: 0.6em;
  margin-right: 0.15em;
  vertical-align: 0.15em;
  color: var(--text-muted);
}
.readout.grade[data-dir='up'] #grade-arrow {
  color: var(--up);
}
.readout.grade[data-dir='down'] #grade-arrow {
  color: var(--down);
}
.readout-tag {
  font-size: max(15px, calc(var(--readout-px) * 0.14));
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  margin-top: 0.15em;
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  white-space: nowrap;
}
.readout-tag::before,
.badge[data-src]::before,
.swatch::before {
  content: '';
  display: inline-block;
  width: 0.7em;
  height: 0.7em;
  border-radius: 2px;
  background: var(--src-none);
}
[data-src='gps']::before {
  background: var(--src-gps) !important;
}
[data-src='terrarium']::before {
  background: var(--src-terrarium) !important;
}
[data-src='openmeteo']::before {
  background: var(--src-openmeteo) !important;
}
#grade-tag::before {
  display: none;
}
.scrubbed .readout-value {
  color: var(--scrub);
}

/* Status pills: a bar along the bottom edge of the map, above the profile. */
#badges {
  position: absolute;
  left: 72px;
  right: 12px;
  bottom: calc(var(--profile-h) + 28px);
  z-index: 500;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  padding-bottom: 2px;
  pointer-events: none;
}
.badge {
  font-family: var(--font-body);
  font-size: 13px;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
  padding: 5px 9px 6px;
  border-radius: 6px;
  background: var(--surface-glass);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  gap: 5px;
  text-shadow: none;
}
.badge.tappable {
  pointer-events: auto;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.badge.tappable:active {
  opacity: 0.6;
}
.badge[data-src='pending'] {
  font-style: italic;
}
.row-actions .hint {
  align-self: center;
}
.badge[data-state='recording'] {
  color: var(--rec);
  border-color: var(--rec);
  font-weight: 700;
}
.badge[data-state='viewing'] {
  color: var(--scrub);
  border-color: var(--scrub);
}

/* ---------- Profile ---------- */
#profile-wrap {
  grid-row: 2;
  position: relative;
  background: var(--surface-1);
  border-top: 1px solid var(--border);
  overflow: hidden;
}
#profile {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}
#profile-legend {
  position: absolute;
  top: 6px;
  left: 12px;
  right: 150px; /* keep clear of the LIVE button */
  font-family: var(--font-display);
  font-size: calc(var(--label-px) * 0.8);
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
#profile-legend > span {
  margin-right: 1.2em;
}
#profile-legend .swatch::before {
  width: 0.8em;
  height: 0.8em;
  margin-right: 0.3em;
  vertical-align: -0.05em;
}
.swatch-diff::before {
  background: var(--down) !important;
}
#btn-live,
.profile-btn {
  position: absolute;
  top: 8px;
  height: 48px;
  padding: 0 18px;
  border-radius: 10px;
  border: 2px solid var(--scrub);
  background: var(--surface-glass);
  color: var(--scrub);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.1em;
  cursor: pointer;
  z-index: 2;
}
#btn-live {
  right: 12px;
}
/* Compare toggle sits inside the plot area at its lower-left (over the oldest part of the trace),
   offset by the axis margins that profile.js publishes as --plot-left / --plot-bottom. */
.profile-btn {
  top: auto;
  bottom: calc(var(--plot-bottom, 30px) + 4px);
  left: calc(var(--plot-left, 60px) + 6px);
  height: 36px;
  padding: 0 12px;
  font-size: 14px;
  border-color: var(--border);
  color: var(--text-secondary);
  opacity: 0.9;
}
.profile-btn.on {
  border-color: var(--down);
  color: var(--text-primary);
}
#btn-grade {
  left: calc(var(--plot-left, 60px) + 128px);
}
.profile-btn[disabled] {
  opacity: 0.35;
  cursor: default;
}
.swatch-up::before {
  background: var(--up) !important; /* uphill shaded green, like the HUD arrow */
}
.swatch-down::before {
  background: var(--down) !important; /* downhill shaded red */
}

/* ---------- Panels ---------- */
.panel {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(520px, 100vw);
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 800;
  display: flex;
  flex-direction: column;
  box-shadow: 8px 0 30px rgba(0, 0, 0, 0.35);
}
.panel header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.panel h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 22px;
  letter-spacing: 0.12em;
}
.panel h3 {
  font-family: var(--font-display);
  font-size: 16px;
  letter-spacing: 0.1em;
  color: var(--text-secondary);
  margin: 18px 0 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.panel-close {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.panel-close svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
}
.panel-body {
  overflow-y: auto;
  padding: 4px 16px 24px;
  font-size: 16px;
}
.panel label {
  display: block;
  margin: 10px 0 14px;
  color: var(--text-secondary);
}
.panel label.row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.panel select,
.panel input[type='text'],
.panel input[type='number'] {
  display: block;
  width: 100%;
  margin-top: 6px;
  height: 48px;
  padding: 0 10px;
  font-size: 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
}
.panel input[type='range'] {
  display: block;
  width: 100%;
  margin-top: 8px;
  height: 40px;
  accent-color: var(--accent);
}
.panel input[type='checkbox'] {
  width: 24px;
  height: 24px;
  accent-color: var(--accent);
}
.panel output {
  float: right;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}
.hint {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 6px;
  line-height: 1.4;
}
#ruler {
  margin-top: 8px;
  height: 22px;
  width: calc(var(--ppi) * 1px);
  border: 1px solid var(--text-secondary);
  border-top: none;
  display: grid;
  place-items: center;
  font-size: 12px;
  color: var(--text-secondary);
}
.btn {
  height: 48px;
  padding: 0 18px;
  border-radius: 10px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.btn.secondary {
  background: var(--surface-2);
  color: var(--text-primary);
  border-color: var(--border);
}
.btn.danger {
  background: transparent;
  color: var(--down);
  border-color: var(--down);
}
.btn.small {
  height: 40px;
  padding: 0 12px;
  font-size: 14px;
}
.row-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 12px 0;
}
.list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.list li {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.list .trip-name {
  font-family: var(--font-display);
  font-size: 17px;
  letter-spacing: 0.04em;
}
.list .trip-meta {
  color: var(--text-muted);
  font-size: 13px;
  margin: 4px 0 8px;
  font-variant-numeric: tabular-nums;
}
.list li.active .trip-name {
  color: var(--accent);
}
dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 14px;
  margin: 8px 0;
  font-size: 14px;
}
dl dt {
  color: var(--text-muted);
}
dl dd {
  margin: 0;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  word-break: break-word;
}
.counter-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 6px 0 4px;
}
.counter-row img {
  display: block;
  height: 20px;
  width: auto;
  image-rendering: pixelated;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: #fff;
}
.counter-row .hint {
  margin-top: 0;
}
.log {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
  font-size: 13px;
  font-family: ui-monospace, Consolas, monospace;
  color: var(--text-secondary);
  max-height: 200px;
  overflow: auto;
}
.log li {
  padding: 2px 0;
  border-bottom: 1px dotted var(--border);
}

/* ---------- Rate picker (opens from the Hz pill) ---------- */
.picker {
  position: absolute;
  right: 12px;
  bottom: calc(var(--profile-h) + 64px);
  z-index: 700;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  max-width: min(560px, calc(100vw - 24px));
}
.picker-title {
  font-family: var(--font-display);
  font-size: 14px;
  letter-spacing: 0.1em;
  color: var(--text-secondary);
  margin-bottom: 8px;
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}
.picker-title .hint {
  display: inline;
  margin: 0;
  letter-spacing: 0;
}
.picker-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(64px, 1fr));
  gap: 6px;
}
.picker-grid button {
  height: 48px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-family: var(--font-display);
  font-size: 15px;
  cursor: pointer;
  white-space: nowrap;
}
.picker-grid button.on {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  color: var(--accent);
}

/* ---------- Toast & dialog ---------- */
#toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--profile-h) + 16px);
  transform: translateX(-50%);
  background: var(--surface-glass);
  border: 1px solid var(--border);
  backdrop-filter: blur(8px);
  padding: 12px 18px;
  border-radius: 10px;
  z-index: 900;
  font-size: 16px;
  display: flex;
  gap: 12px;
  align-items: center;
  max-width: 90vw;
}
#dialog {
  position: absolute;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.5);
  display: grid;
  place-items: center;
}
.dialog-box {
  width: min(460px, 92vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  font-size: 17px;
}
.dialog-box input {
  width: 100%;
  height: 48px;
  margin-top: 12px;
  padding: 0 10px;
  font-size: 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}
