:root {
  --red: #b71c1c;
  --red-soft: #fdecea;
  --blue: #1565c0;
  --blue-soft: #e3f0fc;
  --green: #2e7d32;
  --green-soft: #e8f5e9;
  --ink: #1a1a1a;
  --muted: #5f6368;
  --line: #e0e0e0;
  --bg: #ffffff;
  --bg-2: #f5f5f5;
  --profile-line: #b71c1c;
  --profile-fill: rgba(183, 28, 28, .14);
  --profile-text: #5f6368;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* Barra in alto */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 1000;
  display: flex; gap: 8px; padding: calc(8px + var(--safe-t)) 12px 8px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.topbar input {
  flex: 1; min-width: 0; height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 22px; background: var(--bg-2);
  font: inherit; outline: none;
}
.topbar input:focus { border-color: var(--red); background: var(--bg); }
.icon-btn {
  width: 44px; height: 44px; flex: none; border: 1px solid var(--line);
  border-radius: 22px; background: var(--bg); font-size: 20px;
}

main { position: fixed; inset: calc(61px + var(--safe-t)) 0 0 0; }
#map, #nav-map { position: absolute; inset: 0; background: #e8e4da; }

.fab {
  position: absolute; right: 12px; bottom: calc(40px + var(--safe-b)); z-index: 900;
  width: 52px; height: 52px; border-radius: 26px; border: none;
  background: var(--bg); box-shadow: 0 2px 8px rgba(0,0,0,.3); font-size: 24px;
}

/* Elenco */
.list { position: absolute; inset: 0; overflow-y: auto; background: var(--bg); z-index: 1050; padding-bottom: 40px; }
.seg { display: flex; margin: 12px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; padding: 10px 4px; border: none; background: var(--bg); font-size: 14px; }
.seg button.on { background: var(--red); color: #fff; }
.filter { display: block; padding: 0 16px 8px; color: var(--muted); font-size: 14px; }
.group-title {
  position: sticky; top: 0; padding: 8px 16px; background: var(--bg-2);
  font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.item {
  display: flex; gap: 12px; align-items: center; width: 100%; padding: 10px 16px;
  border: none; border-bottom: 1px solid var(--line); background: none; text-align: left;
}
.ref {
  flex: none; min-width: 48px; padding: 4px 6px; border: 2px solid var(--red); border-radius: 4px;
  background: linear-gradient(var(--red) 0 30%, #fff 30% 70%, var(--red) 70%);
  font-weight: 800; font-size: 13px; text-align: center; color: var(--ink);
}
.ref span { background: #fff; padding: 0 2px; }
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: 13px; color: var(--muted); }
.empty { padding: 24px 16px; color: var(--muted); text-align: center; }

/* Scheda sentiero */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1100; max-height: 75vh; overflow-y: auto;
  padding: 16px 16px calc(16px + var(--safe-b)); background: var(--bg);
  border-radius: 16px 16px 0 0; box-shadow: 0 -2px 16px rgba(0,0,0,.25);
}
.sheet-close { position: absolute; top: 10px; right: 10px; }
.sheet h2 { margin: 0 52px 4px 0; font-size: 20px; line-height: 1.25; }
.sheet .sub { color: var(--muted); font-size: 14px; margin-bottom: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip { padding: 4px 10px; border-radius: 14px; background: var(--bg-2); font-size: 14px; }
.chip.scale { background: var(--red); color: #fff; font-weight: 700; }
.plan {
  margin: 10px 0; padding: 10px 12px; border-radius: 10px; background: var(--blue-soft); font-size: 14px;
}
.plan b { font-weight: 700; }
.plan input { font: inherit; border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px; background: #fff; }
.desc { font-size: 14px; color: var(--muted); margin: 8px 0; }
.warn { font-size: 13px; color: #8a5300; background: #fff4e0; padding: 6px 10px; border-radius: 8px; margin: 8px 0; }
.dir-choice { display: flex; gap: 8px; margin: 12px 0 8px; }
.dir-choice button {
  flex: 1; padding: 10px 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 14px;
}
.dir-choice button.on { border: 2px solid var(--red); font-weight: 700; }
.actions { display: flex; gap: 8px; margin-top: 8px; }
.actions button, .actions a {
  flex: 1; display: flex; align-items: center; justify-content: center; min-height: 48px; padding: 8px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg); text-decoration: none; color: inherit;
  font-size: 15px; text-align: center;
}
.actions .primary { background: var(--red); border-color: var(--red); color: #fff; font-weight: 700; font-size: 17px; }
.actions .ok { background: var(--green-soft); border-color: var(--green); color: var(--green); }
.progress { height: 6px; border-radius: 3px; background: var(--bg-2); overflow: hidden; margin-top: 8px; }
.progress > div { height: 100%; width: 0; background: var(--green); transition: width .2s; }
.src { font-size: 12px; color: var(--muted); margin-top: 12px; }
.src a { color: inherit; }

/* Navigazione */
.nav { position: fixed; inset: 0; z-index: 1200; background: var(--bg); display: flex; flex-direction: column; }
.nav #nav-map { position: relative; inset: auto; flex: 1; min-height: 0; }
.banner {
  position: absolute; top: calc(10px + var(--safe-t)); left: 10px; right: 10px; z-index: 1000;
  padding: 12px 14px; border-radius: 12px; font-weight: 700; font-size: 17px; text-align: center;
  background: var(--blue); color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.3);
}
.banner.on { background: var(--green); }
.banner.off { background: var(--red); font-size: 20px; animation: pulse 1s ease-in-out infinite alternate; }
.banner.arrived { background: var(--green); font-size: 20px; }
.banner.weak { background: #6d4c41; }
@keyframes pulse { from { opacity: 1; } to { opacity: .82; } }
.nav-panel {
  flex: none; padding: 10px 12px calc(8px + var(--safe-b)); background: var(--bg); border-top: 1px solid var(--line);
  box-shadow: 0 -2px 8px rgba(0,0,0,.08);
}
.compass-row { display: flex; gap: 12px; align-items: center; }
.compass { flex: none; width: 104px; height: 104px; }
.compass svg { width: 100%; height: 100%; display: block; }
.compass .ring { fill: var(--bg-2); stroke: var(--line); stroke-width: 2; }
.compass .arrow { fill: var(--green); stroke: #fff; stroke-width: 2; stroke-linejoin: round; }
.compass.off .arrow { fill: var(--red); }
.compass.approach .arrow { fill: var(--blue); }
.compass.north .ring { stroke: #f9a825; stroke-dasharray: 4 3; }
#arrow { transform-origin: 50px 50px; transition: transform .25s linear; }
.nav-stats { flex: 1; min-width: 0; }
.nav-stats .big { font-size: 26px; font-weight: 800; line-height: 1.1; }
.nav-stats .small { font-size: 13px; color: var(--muted); margin: 2px 0 6px; }
.stats { display: flex; gap: 12px; }
.stats div { display: flex; flex-direction: column; font-weight: 700; font-size: 16px; }
.stats small { font-weight: 400; font-size: 11px; color: var(--muted); }
.dest { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 13px; color: var(--muted); min-width: 0; }
.dest span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dest svg { width: 18px; height: 18px; flex: none; }
.dest path { fill: #111; transform-origin: 10px 10px; transition: transform .25s linear; }
.profile { display: block; width: 100%; height: 60px; margin-top: 6px; }
.nav-buttons { display: flex; gap: 8px; margin-top: 6px; }
.nav-buttons button { flex: 1; height: 40px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 14px; }

/* Posizione utente */
.me { position: relative; }
.me .dot {
  position: absolute; left: 50%; top: 50%; width: 18px; height: 18px; margin: -9px 0 0 -9px;
  border-radius: 50%; background: var(--blue); border: 3px solid #fff; box-shadow: 0 0 4px rgba(0,0,0,.5);
}
.me .cone {
  position: absolute; left: 50%; top: 50%; width: 0; height: 0; margin-left: -14px; margin-top: -40px;
  border-left: 14px solid transparent; border-right: 14px solid transparent; border-bottom: 34px solid rgba(21,101,192,.35);
  transform-origin: 14px 40px;
}

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); z-index: 2000;
  max-width: 90vw; padding: 10px 16px; border-radius: 20px; background: rgba(0,0,0,.85); color: #fff; font-size: 14px;
}
.leaflet-container { font: inherit; }
