:root {
  --bg: #f6f7f5;
  --panel: #ffffff;
  --ink: #1d2320;
  --muted: #65706a;
  --line: #dde2de;
  --accent: #e2572b;
  --accent-ink: #ffffff;
  --route: #e2572b;
  --soft: #eef1ee;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151917;
    --panel: #1e2421;
    --ink: #e8ece9;
    --muted: #9aa69f;
    --line: #333b37;
    --soft: #262d29;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { display: grid; grid-template-columns: 360px 1fr; height: 100vh; }

#sidebar {
  overflow-y: auto; padding: 12px 14px 16px; background: var(--bg);
  border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px;
}
.brand { display: flex; align-items: center; justify-content: space-between; }
.brand h1 { margin: 0; font-size: 22px; letter-spacing: -0.02em; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.card h2 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips label { cursor: pointer; }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span {
  display: inline-block; padding: 6px 10px; border-radius: 999px; border: 1px solid var(--line);
  font-size: 14px; background: var(--soft);
}
.chips input:checked + span { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chips input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.segmented { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.segmented label { flex: 1; text-align: center; padding: 7px; cursor: pointer; font-size: 14px; }
.segmented input { display: none; }
.segmented label:has(input:checked) { background: var(--accent); color: var(--accent-ink); }

.row { display: flex; gap: 6px; margin-top: 6px; }
.row.end { justify-content: flex-end; }
.row input { flex: 1; min-width: 0; }

input[type=search], input[type=text], input[type=number], input[type=password], select {
  font: inherit; font-size: 14px; padding: 7px 9px; border: 1px solid var(--line);
  border-radius: 7px; background: var(--panel); color: var(--ink); width: 100%;
}
input[type=range] { width: 100%; accent-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 14px; }
.field:last-child { margin-bottom: 0; }

button {
  font: inherit; font-size: 14px; padding: 7px 12px; border-radius: 7px; cursor: pointer;
  border: 1px solid var(--ink); background: var(--ink); color: var(--bg);
}
button.secondary { background: transparent; color: var(--ink); border-color: var(--line); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
#btn-generate { padding: 11px; font-size: 16px; }
button:disabled { opacity: .55; cursor: progress; }
.icon-btn { background: transparent; color: var(--ink); border: 1px solid var(--line); font-size: 17px; padding: 4px 9px; }

.hint { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.status { margin: 0; font-size: 14px; min-height: 1.2em; }
.status.error { color: #c0392b; }

.results { list-style: none; margin: 6px 0 0; padding: 0; }
.results li { padding: 6px 4px; border-bottom: 1px solid var(--line); cursor: pointer; font-size: 14px; }
.results li:hover { background: var(--soft); }

.candidates { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.candidates li {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; cursor: pointer; font-size: 14px;
  display: grid; grid-template-columns: auto 1fr; gap: 2px 10px;
}
.candidates li.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.candidates .n { grid-row: span 2; font-weight: 700; color: var(--accent); font-size: 18px; align-self: center; }
.candidates .meta { color: var(--muted); font-size: 13px; }

.saved { list-style: none; margin: 0; padding: 0; }
.saved li { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.saved li .t { flex: 1; cursor: pointer; }
.saved li .t small { display: block; color: var(--muted); }
.saved li button { padding: 3px 8px; font-size: 13px; }

.foot { font-size: 11px; color: var(--muted); margin-top: auto; }

#map-wrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }
#profile {
  position: absolute; left: 12px; right: 12px; bottom: 12px; height: 170px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px 6px;
  box-shadow: 0 4px 18px rgba(0,0,0,.15); display: flex; flex-direction: column;
}
#profile[hidden] { display: none; }
.profile-head { display: flex; justify-content: space-between; font-size: 13px; gap: 10px; }
#profile-hover { color: var(--muted); }
#profile-canvas { flex: 1; width: 100%; min-height: 0; cursor: crosshair; }

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); max-width: 440px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h2 { margin-top: 0; }
dialog a { color: var(--accent); }

@media (max-width: 760px) {
  body { grid-template-columns: 1fr; grid-template-rows: 55vh auto; height: auto; }
  #map-wrap { grid-row: 1; height: 55vh; position: sticky; top: 0; z-index: 1; }
  #sidebar { grid-row: 2; border-right: 0; overflow: visible; }
  #profile { height: 130px; left: 8px; right: 8px; bottom: 8px; }
}

/* ---------- Punti, tappe ---------- */
.click-mode { display: flex; flex-direction: column; gap: 4px; margin-bottom: 4px; }
.hint.inline { margin: 0; }
.segmented.small label { padding: 5px; font-size: 13px; }
.points { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.points li { display: flex; align-items: center; gap: 6px; font-size: 14px; padding: 4px 6px; border-radius: 7px; background: var(--soft); }
.points .dot { width: 20px; height: 20px; border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center; flex: none; }
.points .lbl { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.points button { padding: 1px 7px; font-size: 13px; }
.results li { display: flex; align-items: center; gap: 6px; }
.results li .lbl { flex: 1; }
.results li button { padding: 2px 7px; font-size: 12px; }
.via-marker { width: 24px; height: 24px; border-radius: 50%; background: #1e66d0; color: #fff; font: 700 12px system-ui, sans-serif;
  display: grid; place-items: center; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); cursor: grab; }

/* ---------- Analisi ---------- */
.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.tab { background: var(--soft); color: var(--ink); border: 1px solid var(--line); padding: 4px 9px; font-size: 13px; }
.tab.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.check { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); margin-top: 8px; }
.advice { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.advice li { font-size: 14px; padding: 7px 9px; border-radius: 7px; border-left: 4px solid; background: var(--soft); }
.advice li.warn { border-color: #e2572b; }
.advice li.info { border-color: #1e66d0; }
.advice li.good { border-color: #2e7d32; }
.climb { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 6px; cursor: pointer; font-size: 14px; }
.climb.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.climb .top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.climb .badge { font-size: 12px; font-weight: 700; color: #fff; padding: 2px 7px; border-radius: 999px; white-space: nowrap; }
.climb .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px 8px; margin-top: 6px; font-size: 13px; }
.climb .stats b { display: block; font-size: 14px; }
.climb .stats span { color: var(--muted); }
.gradebar { display: flex; height: 16px; border-radius: 4px; overflow: hidden; margin-top: 6px; }
.gradebar div { height: 100%; font-size: 9px; color: #fff; display: grid; place-items: center; overflow: hidden; }
.summary-line { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.stackbar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; margin-bottom: 8px; }
.breakdown { list-style: none; margin: 0; padding: 0; }
.breakdown li { display: grid; grid-template-columns: 12px 1fr auto auto; gap: 8px; align-items: center; font-size: 14px; padding: 3px 0; }
.breakdown .sw { width: 12px; height: 12px; border-radius: 3px; }
.breakdown .km { color: var(--muted); font-variant-numeric: tabular-nums; }
.breakdown .pc { font-weight: 600; font-variant-numeric: tabular-nums; min-width: 3.2em; text-align: right; }
.empty { font-size: 14px; color: var(--muted); }

/* ---------- Controlli sulla mappa ---------- */
.map-ctrl { position: absolute; background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.15); font-size: 13px; z-index: 2; }
#color-by { top: 10px; left: 50%; transform: translateX(-50%); padding: 4px 6px; display: flex; gap: 6px; align-items: center; }
#color-by select { width: auto; padding: 3px 6px; font-size: 13px; }
.legend { top: 52px; left: 10px; padding: 6px 8px; max-width: 220px; max-height: 40%; overflow: auto; }
.legend div { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.legend i { width: 14px; height: 4px; border-radius: 2px; flex: none; }
[hidden] { display: none !important; }
#color-by label { white-space: nowrap; }

.check.strong { color: var(--ink); font-size: 14px; margin: 4px 0 6px; }
.steep-fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 10px; }
.steep-fields .field { margin: 0; font-size: 12px; }
.candidates .warn { color: #c0392b; }

.surface-prefs { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px 8px; margin: 0 0 10px; }
.surface-prefs legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.surface-prefs .check { color: var(--ink); font-size: 14px; margin-top: 4px; }
.inline-field { flex-direction: row; align-items: center; justify-content: space-between; margin: 6px 0 0; font-size: 13px; }
.inline-field input { width: 90px; }

/* ---------- Mappa: selettore ---------- */
#color-by { flex-wrap: wrap; justify-content: center; max-width: calc(100% - 120px); }

/* ---------- Import/condivisione ---------- */
.file-btn { display: inline-block; margin-top: 8px; font-size: 14px; padding: 7px 12px; border: 1px solid var(--line); border-radius: 7px; cursor: pointer; }
.file-btn input { display: none; }
.nav-start { width: 100%; margin-top: 8px; }

/* ---------- Navigazione ---------- */
body.navigating #sidebar, body.navigating #profile, body.navigating #color-by, body.navigating #legend { display: none !important; }
body.navigating { grid-template-columns: 1fr; grid-template-rows: 1fr; height: 100vh; height: 100dvh; }
body.navigating #map-wrap { grid-row: 1; height: 100vh; height: 100dvh; position: relative; }
.nav-top { position: absolute; top: calc(10px + env(safe-area-inset-top)); left: 10px; right: 10px; z-index: 3;
  background: #1d2320; color: #fff; border-radius: 14px; padding: 12px 14px; display: flex; gap: 12px; align-items: center;
  box-shadow: 0 6px 20px rgba(0,0,0,.3); }
.nav-top.off { background: #b3261e; }
.nav-icon { font-size: 40px; line-height: 1; width: 48px; text-align: center; flex: none; }
.nav-dist { font-size: 26px; font-weight: 700; }
.nav-instr { font-size: 17px; }
.nav-sub { font-size: 13px; opacity: .8; margin-top: 2px; }
.nav-bottom { position: absolute; left: 10px; right: 10px; bottom: calc(10px + env(safe-area-inset-bottom)); z-index: 3;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; box-shadow: 0 6px 20px rgba(0,0,0,.25); }
.nav-stats { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; margin-bottom: 8px; }
.nav-stats b { display: block; font-size: 20px; }
.nav-stats span { font-size: 12px; color: var(--muted); }
.nav-buttons { display: flex; gap: 6px; }
.nav-buttons button { flex: 1; padding: 9px 4px; }
