:root {
  --blu: #0d47a1;
  --blu-d: #08306b;
  --bg: #0f1115;
  --card: #1a1d24;
  --card-2: #232732;
  --txt: #f2f4f8;
  --muted: #9aa3b2;
  --ok: #19c37d;
  --warn: #ff6b3d;
  --line: #2a2f3a;
  --hi: #16223d;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

/* tema chiaro (applicato di giorno) */
.theme-light {
  --bg: #f4f6f9;
  --card: #ffffff;
  --card-2: #eef1f6;
  --txt: #14181f;
  --muted: #5f6b7a;
  --line: #e1e6ee;
  --hi: #e6efff;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--txt);
  overscroll-behavior: none;
}

.screen { display: none; height: 100dvh; }
.screen.active { display: flex; }

/* ----------------------------- SETUP ----------------------------- */
#setup { flex-direction: column; }
.setup-head {
  display: flex; align-items: center; gap: 12px;
  padding: calc(16px + var(--safe-t)) 18px 14px;
  background: linear-gradient(135deg, var(--blu), var(--blu-d));
}
.logo {
  width: 44px; height: 44px; border-radius: 12px;
  background: rgba(255,255,255,.16); display: grid; place-items: center;
  font-size: 22px; transform: rotate(0deg);
}
.setup-head h1 { font-size: 19px; margin: 0; }
.setup-head .sub { margin: 2px 0 0; font-size: 13px; color: #cdd8f0; }

.wizard { flex: 1; overflow-y: auto; padding: 16px 16px 0; }
.step { margin-bottom: 22px; }
.step-label { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 8px; }
.step-label .num {
  width: 22px; height: 22px; border-radius: 50%; background: var(--blu);
  display: grid; place-items: center; font-size: 13px;
}
.hint { font-size: 12px; color: var(--muted); margin: 6px 2px 0; }

input[type="date"], input[type="search"] {
  width: 100%; padding: 13px 14px; font-size: 16px;
  background: var(--card); color: var(--txt);
  border: 1px solid var(--line); border-radius: 12px; outline: none;
}
input:focus { border-color: var(--blu); }
input:disabled { opacity: .5; }

.list { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; max-height: 46vh; overflow-y: auto; }
.list:empty { display: none; }

.item {
  display: flex; align-items: center; gap: 13px;
  padding: 14px 15px; background: var(--card);
  border: 1px solid var(--line); border-radius: 13px; cursor: pointer;
}
.item:active { background: var(--card-2); }
.item.selected { border-color: var(--blu); background: var(--hi); }
.chip {
  min-width: 52px; padding: 7px 9px; border-radius: 9px; text-align: center;
  font-weight: 800; font-size: 16px; flex-shrink: 0;
}
.item .main { flex: 1; min-width: 0; }
.item .title { font-weight: 700; font-size: 15.5px; line-height: 1.25; }
.item .desc { font-size: 13px; color: var(--muted); margin-top: 2px; }
.item .time { font-size: 23px; font-weight: 800; font-variant-numeric: tabular-nums; }
.item .arrow-dir { font-size: 12px; color: var(--muted); }

.dir-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.dir-tabs button {
  flex: 1; padding: 9px; border: 1px solid var(--line); background: var(--card);
  color: var(--muted); border-radius: 10px; font-size: 14px;
}
.dir-tabs button.active { background: var(--blu); color: #fff; border-color: var(--blu); }

.setup-foot { padding: 12px 16px calc(12px + var(--safe-b)); border-top: 1px solid var(--line); }
button.primary {
  width: 100%; padding: 16px; font-size: 17px; font-weight: 700;
  background: var(--blu); color: #fff; border: 0; border-radius: 14px;
}
button.primary:disabled { background: #2a2f3a; color: var(--muted); }

.loading { text-align: center; color: var(--muted); padding: 14px; font-size: 14px; }

/* --------------------------- NAVIGAZIONE --------------------------- */
#nav { position: relative; overflow: hidden; }
#map { position: absolute; inset: 0; }

/* controlli flottanti sulla mappa */
.map-top {
  position: absolute; top: 0; left: 0; right: 0; z-index: 6;
  display: flex; align-items: flex-start; gap: 10px;
  padding: calc(10px + var(--safe-t)) 12px 0;
  pointer-events: none;
}
.map-top > * { pointer-events: auto; }
.icon-btn {
  width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0;
  background: rgba(20,24,32,.9); color: #fff; border: 1px solid var(--line);
  font-size: 20px; display: grid; place-items: center; box-shadow: 0 2px 8px rgba(0,0,0,.35);
}
.offroute {
  flex: 1; background: var(--warn); color: #fff; font-weight: 800; text-align: center;
  font-size: 16px; letter-spacing: .01em;
  padding: 13px; border-radius: 12px; box-shadow: 0 3px 12px rgba(0,0,0,.4);
  animation: offpulse 1.2s ease-in-out infinite;
}
@keyframes offpulse { 0%,100% { opacity: 1; } 50% { opacity: .72; } }

.map-fabs {
  position: absolute; right: 12px; z-index: 6;
  bottom: calc(var(--sheet-h, 30dvh) + 14px);
  transition: bottom .25s ease;
  display: flex; flex-direction: column; gap: 10px;
}
.fab {
  width: 58px; height: 58px; border-radius: 50%;
  background: #fff; color: #0d47a1; border: 0; font-size: 26px;
  box-shadow: 0 3px 12px rgba(0,0,0,.45); display: grid; place-items: center;
}
.fab.active { background: var(--blu); color: #fff; }

/* posizione corrente */
.me { width: 26px; height: 26px; display: grid; place-items: center; }
.me-arrow {
  width: 0; height: 0;
  border-left: 11px solid transparent; border-right: 11px solid transparent;
  border-bottom: 22px solid #ff6d00; /* arancione: distinto dal blu del percorso */
  filter: drop-shadow(0 0 3px rgba(0,0,0,.7));
  transform-origin: 50% 65%;
}

/* -------- pannello a scomparsa (bottom sheet stile Google Maps) -------- */
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 8;
  height: var(--sheet-h, 30dvh);
  background: var(--card); border-top-left-radius: 18px; border-top-right-radius: 18px;
  display: flex; flex-direction: column; box-shadow: 0 -8px 30px rgba(0,0,0,.55);
}
.sheet.animating { transition: height .28s cubic-bezier(.22,.61,.36,1); }
.grip { padding: 10px 0 6px; display: grid; place-items: center; cursor: grab; flex-shrink: 0; touch-action: none; }
.grip span { width: 42px; height: 5px; border-radius: 3px; background: #556; display: block; }

.sheet-fixed { padding: 2px 16px 12px; flex-shrink: 0; }
.sheet-route { display: flex; align-items: center; gap: 11px; margin-bottom: 11px; }
.line-badge {
  min-width: 56px; padding: 8px 11px; border-radius: 10px; font-size: 19px;
  background: var(--blu); color: #fff; font-weight: 800; text-align: center; flex-shrink: 0;
}
.route-dir {
  font-weight: 700; font-size: 15px; line-height: 1.25;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* blocco "prossima fermata": è l'informazione principale per l'autista */
.next-stop {
  background: var(--card-2); border-radius: 14px; padding: 13px 16px;
  border-left: 5px solid var(--ok);
}
.ns-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 700; }
.ns-name { font-size: 27px; font-weight: 800; margin-top: 3px; line-height: 1.1; }
.ns-dist { font-size: 21px; color: var(--ok); font-weight: 800; margin-top: 4px; font-variant-numeric: tabular-nums; }

.progress { height: 8px; background: var(--card-2); border-radius: 5px; overflow: hidden; margin-top: 12px; }
.progress-bar { height: 100%; width: 0; background: var(--ok); transition: width .4s; }
.nav-stats { display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); margin-top: 10px; font-weight: 600; }
.gps-bad { color: var(--warn); }
.gps-ok { color: var(--ok); }

/* livello "ridotto": mostra solo linea + prossima fermata, mappa più visibile */
.sheet--peek .progress,
.sheet--peek .nav-stats { display: none; }
.sheet--peek .sheet-route { margin-bottom: 8px; }
.sheet--peek .next-stop { padding: 8px 12px; }

.sheet-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(12px + var(--safe-b)); border-top: 1px solid var(--line); }
.sheet-row { display: flex; gap: 13px; padding: 14px 16px; align-items: center; cursor: pointer; font-size: 16px; }
.sheet-row:active { background: var(--card-2); }
.sheet-row.passed { opacity: .42; }
.sheet-row.current { background: var(--hi); }
.sheet-row.current .n { font-weight: 800; }
.sheet-row .t { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 15px; min-width: 52px; font-weight: 600; }
.sheet-row .n { flex: 1; line-height: 1.2; }
.sheet-row .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.sheet-row.current .dot { background: var(--ok); box-shadow: 0 0 0 5px rgba(25,195,125,.25); }
.sheet-row.passed .dot { background: var(--blu); }

/* -------- orizzontale: pannello laterale stretto e trasparente -------- */
@media (orientation: landscape) {
  .sheet {
    top: 0; bottom: 0; left: 0; right: auto;
    width: min(32%, 300px); height: 100% !important;
    border-radius: 0 16px 16px 0; box-shadow: 4px 0 24px rgba(0,0,0,.35);
    background: var(--card); /* fallback */
    background: color-mix(in srgb, var(--card) 70%, transparent);
    -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  }
  .grip { display: none; }
  /* in orizzontale mostra sempre tutto, compatto */
  .sheet--peek .progress, .sheet--peek .nav-stats { display: flex; }
  .sheet-fixed { padding: calc(8px + var(--safe-t)) 12px 8px; }
  .sheet-route { margin-bottom: 6px; }
  .ns-name { font-size: 16px; }
  .next-stop { padding: 8px 11px; }
  .sheet-scroll { padding-left: 2px; }
  .sheet-row { padding: 8px 12px; gap: 9px; font-size: 13px; }
  .sheet-row .t { min-width: 42px; font-size: 12px; }
  .map-fabs { bottom: calc(20px + var(--safe-b)); }
}

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%);
  background: #000; color: #fff; padding: 12px 18px; border-radius: 12px; z-index: 20;
  font-size: 14px; max-width: 90vw; text-align: center; border: 1px solid var(--line);
}
