/* Tachimetro HUD
   Tema scuro (nero puro) per il riflesso sul parabrezza e per la notte, tema chiaro per la luce del sole.
   L'elemento memorabile sono le cifre a sette segmenti; tutto il resto resta sobrio. */

:root {
  --bg: #000;              /* sfondo */
  --on: #000;              /* testo sopra le superfici colorate */
  --lit: #ffb000;          /* colore acceso: cifre, testi principali */
  --warn: #ff3b2f;
  --focus: #fff;
  --dim: 1;
  --label: color-mix(in srgb, var(--lit) 58%, var(--bg));
  --line: color-mix(in srgb, var(--lit) 25%, var(--bg));
  --soft: color-mix(in srgb, var(--lit) 78%, var(--bg));   /* etichette delle statistiche: più contrasto di --label */
  --font: "sans-serif-condensed", "Roboto Condensed", "Arial Narrow", system-ui, sans-serif;
}
html { --k: 1.4; }                                   /* scala delle statistiche: Grande */
html[data-stats="normal"] { --k: 1; }
html[data-stats="xlarge"] { --k: 1.9; }
html[data-color="ice"]   { --lit: #dcf0ff; }
html[data-color="green"] { --lit: #4dffa6; }
html[data-color="day"]   { --bg: #fff; --on: #fff; --lit: #000; --warn: #d21f0f; --focus: #0a56d6; }

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--lit);
  font-family: var(--font);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

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

/* ---------- HUD ---------- */

#hud {
  position: absolute;
  inset: 0;
  display: grid;
  gap: 3vmin;
  padding:
    calc(env(safe-area-inset-top) + 3vmin)
    calc(env(safe-area-inset-right) + 3vmin)
    calc(env(safe-area-inset-bottom) + 3vmin)
    calc(env(safe-area-inset-left) + 3vmin);
  opacity: var(--dim);
  transition: opacity 0.8s;
}
html[data-mirror="on"] #hud { transform: scaleX(-1); }

.readout { min-width: 0; min-height: 0; display: flex; }
#speed { width: 100%; height: 100%; color: var(--lit); }
#speed.warn { color: var(--warn); }
#speed svg { display: block; width: 100%; height: 100%; overflow: visible; }
#speed .digits { filter: drop-shadow(0 0 2px currentColor); }
html[data-color="day"] #speed .digits { filter: none; }   /* sul chiaro il bagliore sembrerebbe un'ombra */
#speed .sg { fill: currentColor; opacity: 0.07; }
html[data-color="day"] #speed .sg { opacity: 0.09; }
#speed .sg.on, html[data-color="day"] #speed .sg.on { opacity: 1; }   /* il tema chiaro non deve battere lo stato acceso */
#speed text {
  fill: var(--label);
  font-family: var(--font);
  font-size: 15px;
  font-weight: 600;
}

.side { display: grid; gap: 2.5vmin; align-content: center; min-width: 0; }
.stats { margin: 0; display: flex; gap: 4vmin; }
.stats > div { min-width: 0; }
.stats dt {
  font-size: calc(clamp(12px, 2.6vmin, 18px) * var(--k));
  color: var(--soft);
  letter-spacing: 0.04em;
}
.stats dd {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 0.25em;
  margin: 0;
  font-size: calc(clamp(20px, 6.4vmin, 48px) * var(--k));
  font-weight: 700;
  line-height: 1.08;
  font-variant-numeric: tabular-nums;
}
.stats dd small { font-size: 0.42em; font-weight: 700; color: var(--soft); }
.gps { margin: 0; font-size: calc(clamp(12px, 2.6vmin, 18px) * min(var(--k), 1.5)); color: var(--soft); letter-spacing: 0.04em; }
.gps span { font-weight: 600; }

@media (orientation: landscape) {
  #hud { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
  .readout { height: 100%; }
  .side { text-align: right; min-width: 22vw; }
  .stats { flex-direction: column; gap: 3.2vmin; }
  .stats dd { justify-content: flex-end; }               /* valori allineati a destra come le etichette */

  /* Molto grande: in orizzontale l'altezza è poca, quindi le quattro statistiche passano su due colonne */
  html[data-stats="xlarge"] .side { min-width: 0; text-align: left; }
  html[data-stats="xlarge"] .stats { display: grid; grid-template-columns: repeat(2, auto); gap: 3vmin 4vmin; }
  html[data-stats="xlarge"] .stats dd { justify-content: flex-start; }
}

/* In verticale (telefono al manubrio) le statistiche diventano una griglia grande, leggibile a colpo d'occhio */
@media (orientation: portrait) {
  #hud { grid-template-rows: minmax(0, 1.1fr) auto; }
  .side { gap: 4vmin; align-content: end; }
  .stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4vmin; }
  .stats dt { font-size: calc(clamp(13px, 3.4vmin, 20px) * var(--k)); }
  .stats dd { font-size: calc(clamp(28px, 11vmin, 68px) * var(--k)); }
  /* Con i caratteri grandi le statistiche passano su una colonna sola: valori lunghi come 1:23:45 o 128,4 km non vanno a capo */
  html:not([data-stats="normal"]) .stats { grid-template-columns: minmax(0, 1fr); gap: 2.4vmin; }
}

/* ---------- Comandi ---------- */

#bar {
  position: absolute;
  left: 0; right: 0;
  bottom: max(env(safe-area-inset-bottom), 14px);
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 0 14px;
}
#bar[hidden] { display: none; }

.btn {
  min-height: 52px;
  padding: 0 22px;
  font: 600 17px/1 var(--font);
  color: var(--lit);
  background: var(--bg);
  border: 1px solid var(--label);
  border-radius: 8px;
  cursor: pointer;
}
.btn[aria-pressed="true"], .btn.primary {
  background: var(--lit);
  color: var(--on);
  border-color: var(--lit);
}
.btn.confirm { background: var(--warn); border-color: var(--warn); color: var(--on); }
.btn:focus-visible, .go:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

/* ---------- Schermata iniziale ---------- */

#start {
  position: absolute;
  z-index: 30;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 26px;
  padding: 24px;
  text-align: center;
  background: color-mix(in srgb, var(--bg) 96%, transparent);
}
#start[hidden] { display: none; }
#start .intro, #start .actions { display: grid; justify-items: center; gap: 18px; min-width: 0; }
#start h1 { margin: 0; font-size: clamp(34px, 9vmin, 64px); font-weight: 600; }
#start p { margin: 0; max-width: 36ch; color: var(--label); font-size: clamp(16px, 3.2vmin, 21px); line-height: 1.4; }
#start small { max-width: 44ch; color: var(--label); font-size: 14px; line-height: 1.4; }

@media (orientation: landscape) {
  #start { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: center; column-gap: 6vw; text-align: left; }
  #start .intro { justify-items: start; }
  #start .actions { justify-items: stretch; max-width: 380px; }
  #start .actions .go { justify-self: center; }
  #start small { text-align: center; }
}

.vehicles { width: 100%; }
.vehicles span { padding: 16px 6px; font-size: 20px; }

/* Il pulsante principale riprende la sagoma a esagono dei segmenti */
.go {
  min-width: 220px;
  min-height: 64px;
  padding: 0 44px;
  font: 700 22px/1 var(--font);
  color: var(--on);
  background: var(--lit);
  border: 0;
  cursor: pointer;
  clip-path: polygon(20px 0, calc(100% - 20px) 0, 100% 50%, calc(100% - 20px) 100%, 20px 100%, 0 50%);
}
.go:focus-visible { background: var(--focus); color: var(--bg); outline-offset: -6px; }

/* ---------- Impostazioni ---------- */

dialog {
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 20px;
  overflow: auto;
  color: var(--lit);
  background: var(--bg);
  border: 1px solid var(--label);
  border-radius: 10px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.78); }
dialog h2 { margin: 0 0 16px; font-size: 26px; font-weight: 600; }

fieldset { margin: 0 0 18px; padding: 0; border: 0; }
legend { padding: 0; margin-bottom: 8px; color: var(--label); font-size: 15px; }

.choices { display: flex; gap: 8px; }
.choices label { flex: 1; min-width: 0; }
.choices input { position: absolute; opacity: 0; pointer-events: none; }
.choices span {
  display: block;
  padding: 14px 6px;
  text-align: center;
  font-weight: 600;
  border: 1px solid var(--label);
  border-radius: 8px;
  cursor: pointer;
}
.choices input:checked + span { background: var(--lit); color: var(--on); border-color: var(--lit); }
.choices input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }

.swatches { flex-wrap: wrap; }
.swatches label { flex: 1 1 30%; }
.swatches i {
  display: inline-block;
  width: 0.75em; height: 0.75em;
  margin-right: 0.45em;
  vertical-align: -0.02em;
  border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px var(--label);
}
.swatches input:checked + span { background: var(--bg); color: var(--lit); border: 2px solid var(--lit); padding: 13px 5px; }

.range { display: flex; justify-content: space-between; margin: 12px 0 4px; font-size: 15px; }
.range output { font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; height: 32px; accent-color: var(--lit); }
input[type="number"] {
  width: 100%;
  min-height: 48px;
  padding: 0 12px;
  color: var(--lit);
  background: var(--bg);
  border: 1px solid var(--label);
  border-radius: 8px;
  font: 600 20px var(--font);
}
dialog .btn.primary { width: 100%; }

/* ---------- Storico viaggi ---------- */

.note { margin: 0 0 14px; color: var(--label); font-size: 15px; line-height: 1.4; }
.note.hint { margin: 8px 0 0; font-size: 14px; }
.note[hidden] { display: none; }
.trips { list-style: none; margin: 0 0 18px; padding: 0; }
.trips li { padding: 14px 0; border-top: 1px solid var(--line); }
.trips li:first-child { border-top: 0; }
.trips header { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 8px; }
.trips time { font-size: 18px; font-weight: 600; }
.trips .cloud { font-size: 14px; color: var(--label); text-align: right; }
.trips .cloud.on { color: var(--lit); }
.trips dl { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin: 0; }
.trips dt { font-size: 13px; color: var(--label); }
.trips dd { margin: 0; font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
@media (max-width: 480px) { .trips dl { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

@media (prefers-reduced-motion: reduce) {
  #hud { transition: none; }
}

/* ---------- Schermata di avvio (splash) ---------- */

#splash {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
  background: #000;               /* colori fissi: come l'avvio di un'app, indipendente dal tema */
  color: #ffb000;
  transition: opacity 0.3s;
}
#splash.out { opacity: 0; pointer-events: none; }
#splash img { border-radius: 24px; }
#splash h1 { margin: 10px 0 6px; font-size: 36px; font-weight: 700; }
#splash p { margin: 0; font-size: 16px; color: #b8860b; }

/* ---------- Registrazione e mappa ---------- */

#recBadge { margin-left: 0.6em; padding: 0 0.4em; font-weight: 700; color: var(--on); background: var(--warn); border-radius: 4px; }
#recBadge[hidden] { display: none; }

#mapView {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
}
#mapView[hidden] { display: none; }
#map { position: absolute; inset: 0; }
#map.leaflet-container { background: var(--bg); font-family: var(--font); }

/* Con i temi scuri le tessere chiare vengono invertite, per non abbagliare */
html:not([data-color="day"]) #map .leaflet-tile-pane {
  filter: invert(1) hue-rotate(180deg) brightness(0.8) contrast(0.95) saturate(0.6);
}

#map .route { fill: none; stroke: var(--lit); }
html:not([data-color="day"]) #map .route { stroke: var(--lit); }
#map .me { fill: var(--lit); fill-opacity: 1; stroke: var(--bg); stroke-width: 3; }
#map .acc { fill: var(--lit); fill-opacity: 0.14; stroke: var(--lit); stroke-width: 1; }

#map .leaflet-control-attribution {
  font: 13px/1.3 var(--font);
  color: var(--lit);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
}
#map .leaflet-control-attribution a { color: var(--lit); }

.mapTop {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 10px);
  left: 10px;
  z-index: 500;
  padding: 8px 14px;
  color: var(--lit);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  border: 1px solid var(--label);
  border-radius: 10px;
}
.mapLive { display: flex; align-items: baseline; gap: 8px; }
#mapSpeed { font-size: 56px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.mapLive small { font-size: 20px; font-weight: 700; color: var(--soft); }
#mapDist { margin-left: 10px; font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.mapTrack { display: none; flex-direction: column; gap: 2px; max-width: 60vw; }
.mapTrack strong { font-size: 22px; font-weight: 700; }
.mapTrack span { font-size: 17px; color: var(--soft); font-variant-numeric: tabular-nums; }
#mapView[data-mode="track"] .mapLive { display: none; }
#mapView[data-mode="track"] .mapTrack { display: flex; }
#mapView[data-mode="track"] .live { display: none; }

#mapNote {
  position: absolute;
  left: 10px; right: 10px;
  bottom: 146px;
  z-index: 500;
  margin: 0 auto;
  width: fit-content;
  max-width: 100%;
  padding: 8px 14px;
  font-size: 15px;
  color: var(--lit);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  border: 1px solid var(--label);
  border-radius: 8px;
}
#mapNote[hidden] { display: none; }

.mapBar {
  position: absolute;
  left: 0; right: 0;
  bottom: max(env(safe-area-inset-bottom), 12px);
  z-index: 500;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  padding: 0 12px;
}

/* Azioni nello storico */
.trips .actions { display: flex; gap: 8px; margin-top: 10px; }
.trips .actions .btn { min-height: 44px; padding: 0 16px; font-size: 16px; }
