:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede6;
  --text: #1d2327;
  --muted: #5f6b72;
  --border: #dcd8cc;
  --accent: #1f5f8b;
  --accent-soft: #e2edf5;
  --ok: #2e7d4f;
  --ok-soft: #e1f1e7;
  --ko: #b3412f;
  --ko-soft: #f7e3df;
  --ind: #c0721c;
  --ind-soft: #f8ead8;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --radius: 10px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15191c;
    --surface: #1e2428;
    --surface-2: #262d32;
    --text: #e6e9eb;
    --muted: #9aa5ab;
    --border: #343d43;
    --accent: #6fb0e0;
    --accent-soft: #1d3342;
    --ok: #6fcf97;
    --ok-soft: #1c3528;
    --ko: #f08a78;
    --ko-soft: #3d231f;
    --ind: #f0a65a;
    --ind-soft: #3b2b18;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }

/* Header */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; text-decoration: none; color: var(--text); }
.brand svg { flex: none; }
.brand small { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav a {
  padding: 6px 14px; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 500;
}
.nav a.active, .nav a:hover { background: var(--accent-soft); color: var(--accent); }

/* Elenco */
.container { max-width: 1180px; margin: 0 auto; padding: 24px 20px 60px; }
.intro h1 { margin: 0 0 4px; font-size: 26px; }
.intro p { margin: 0 0 18px; color: var(--muted); max-width: 820px; }
.stats { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 16px; min-width: 130px; }
.stat b { display: block; font-size: 22px; }
.stat span { color: var(--muted); font-size: 13px; }
.filters {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; margin-bottom: 20px; position: sticky; top: 57px; z-index: 50;
}
.filters input[type=search] { flex: 1 1 240px; }
.filters input, .filters select {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.filters .count { color: var(--muted); font-size: 13px; margin-left: auto; }
.region-title { font-size: 20px; margin: 28px 0 10px; padding-bottom: 6px; border-bottom: 2px solid var(--border); }
.group-title { font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 8px; }

.site {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 10px; box-shadow: var(--shadow); overflow: hidden;
  scroll-margin-top: 190px;
}
.site > summary {
  list-style: none; cursor: pointer; padding: 12px 16px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.site > summary::-webkit-details-marker { display: none; }
.site > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
.site[open] > summary::before { transform: rotate(90deg); }
.site-name { font-weight: 650; font-size: 16px; }
.site-meta { color: var(--muted); font-size: 13px; }
.site-body { padding: 0 16px 16px; }
.site-desc { margin: 0 0 12px; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; white-space: nowrap; }
.badge.operativo { background: var(--ok-soft); color: var(--ok); }
.badge.dismesso { background: var(--ko-soft); color: var(--ko); }
.badge.chiuso { background: #ebe7fb; color: #5a48b8; }
.badge.tipo { background: var(--surface-2); color: var(--muted); }
.badge.tipo.ind { background: var(--ind-soft); color: var(--ind); }
.badge.approx { background: var(--surface-2); color: var(--muted); font-weight: 500; }
.site-actions { margin-left: auto; display: flex; gap: 8px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; text-decoration: none; color: var(--text); font-size: 13px;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); color: #fff; }
.btn.danger:hover { border-color: var(--ko); color: var(--ko); }
.btn:disabled { opacity: .45; cursor: not-allowed; pointer-events: none; }
.btn.on {background: var(--accent); color: #fff; border-color: var(--accent); }

.lifts { width: 100%; border-collapse: collapse; font-size: 14px; }
.lifts th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; border-bottom: 1px solid var(--border); padding: 6px 8px; }
.lifts td { border-bottom: 1px solid var(--border); padding: 8px; vertical-align: top; }
.lifts tr:last-child td { border-bottom: 0; }
.lifts .lname { font-weight: 600; }
.lifts .sub { color: var(--muted); font-size: 13px; }
.lifts .desc { color: var(--muted); font-size: 13px; margin-top: 4px; }
.stations { margin: 0; padding: 0; list-style: none; font-size: 13px; }
.stations li::before { content: "● "; color: var(--accent); }
.stations li.mid::before { content: "○ "; }
.empty { text-align: center; color: var(--muted); padding: 40px; }
.footer { color: var(--muted); font-size: 13px; margin-top: 40px; border-top: 1px solid var(--border); padding-top: 14px; }

@media (max-width: 760px) {
  .topbar { padding: 8px 16px; gap: 12px; }
  .brand small { display: none; }
  .container { padding: 16px; }
  .filters { position: static; }
  .lifts thead { display: none; }
  .lifts tr { display: block; border-bottom: 1px solid var(--border); padding: 8px 0; }
  .lifts td { display: block; border: 0; padding: 2px 0; }
  .site-actions { margin-left: 0; width: 100%; }
}

/* Mappa */
body.map-page { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
.map-layout { flex: 1; display: flex; min-height: 0; }
#map { flex: 1; min-height: 0; }
.panel {
  width: 360px; flex: none; background: var(--surface); border-left: 1px solid var(--border);
  overflow-y: auto; padding: 14px 16px 30px;
}
.panel h2 { font-size: 15px; margin: 18px 0 8px; }
.panel h2:first-child { margin-top: 0; }
.panel .row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.panel select, .panel input, .panel textarea {
  width: 100%; padding: 6px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.panel label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 3px; }
.panel .hint { font-size: 13px; color: var(--muted); background: var(--surface-2); border-radius: 8px; padding: 8px 10px; margin: 8px 0; }
.panel .hint.active { background: var(--accent-soft); color: var(--accent); }
.panel textarea.output { font-family: ui-monospace, Menlo, monospace; font-size: 11.5px; height: 200px; resize: vertical; }
.legend { font-size: 13px; }
.legend div { display: flex; align-items: center; gap: 8px; margin: 3px 0; }
.legend i { display: inline-block; width: 26px; height: 4px; border-radius: 2px; }
.legend i.dot { width: 12px; height: 12px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.3); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.form-grid .full { grid-column: 1 / -1; }
.edit-form { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; margin: 10px 0; background: var(--bg); }
.edit-form h3 { margin: 0 0 4px; font-size: 14px; }
.station-row { display: grid; grid-template-columns: 1fr 80px; gap: 6px; margin-bottom: 4px; }
.panel-toggle { display: none; }

.leaflet-popup-content { font-family: var(--font); font-size: 13px; line-height: 1.45; min-width: 220px; }
.leaflet-popup-content h4 { margin: 0 0 2px; font-size: 15px; }
.leaflet-popup-content .muted { color: #667; }
.leaflet-popup-content .pbtns { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.leaflet-popup-content .pbtns button, .leaflet-popup-content .pbtns a {
  font-size: 12px; padding: 3px 8px; border: 1px solid #ccd; border-radius: 6px; background: #fff; cursor: pointer; text-decoration: none; color: #1f5f8b;
}
.handle { width: 14px; height: 14px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1.5px #222, 0 1px 4px rgba(0,0,0,.4); cursor: grab; }
.handle.start { background: #2e7d4f; }
.handle.end { background: #b3412f; }
.handle.mid { background: #f2c94c; width: 12px; height: 12px; }
.map-banner {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 900;
  background: #1f5f8b; color: #fff; padding: 6px 14px; border-radius: 999px; font-size: 13px; box-shadow: var(--shadow); display: none;
}
.map-wrap { position: relative; flex: 1; display: flex; }

@media (max-width: 760px) {
  .map-layout { flex-direction: column; }
  .panel { width: 100%; border-left: 0; border-top: 1px solid var(--border); max-height: 45vh; padding: 12px 16px 24px; }
  .panel.collapsed { display: none; }
  .panel-toggle { display: inline-flex; }
}

/* Salvataggio e storico */
.save-status { font-size: 13px; min-height: 18px; margin: 4px 0; color: var(--muted); }
.save-status.ok { color: var(--ok); }
.save-status.err { color: var(--ko); font-weight: 600; }
.storico { font-size: 13px; }
.storico .item { border-bottom: 1px solid var(--border); padding: 6px 0; display: flex; gap: 8px; align-items: flex-start; }
.storico .item div { flex: 1; }
.storico .item small { color: var(--muted); display: block; }
.storico .item button { flex: none; font-size: 12px; padding: 2px 8px; }
.edit-form h3 + .hint { margin-top: 4px; }
.edit-form .section { border-top: 1px dashed var(--border); margin-top: 10px; padding-top: 6px; }

/* Testo grande (accessibilità) */
.a11y-toggle { margin-left: 8px; font-weight: 700; }
html.big .topbar, html.big .container, html.big .panel { zoom: 1.35; }
html.big .panel { width: 300px; }
html.big .leaflet-popup-content { font-size: 17px; min-width: 260px; }
html.big .leaflet-popup-content h4 { font-size: 20px; }
html.big .leaflet-popup-content .pbtns button, html.big .leaflet-popup-content .pbtns a { font-size: 16px; padding: 6px 12px; }
html.big .leaflet-control-zoom a { width: 44px; height: 44px; line-height: 44px; font-size: 26px; }
html.big .leaflet-tooltip { font-size: 16px; }
html.big .handle { width: 24px; height: 24px; border-width: 3px; }
html.big .handle.mid { width: 20px; height: 20px; }
html.big .map-banner { font-size: 18px; padding: 10px 20px; }
@media (max-width: 760px) {
  html.big .panel { width: 100%; }
}

/* Legenda per età */
.legend i.age-1 { background: #2e9d4f; }
.legend i.age-2 { background: #e0b400; }
.legend i.age-3 { background: #ee7a10; }
.legend i.age-4 { background: #d1342b; }
.legend i.age-0 { background: #8a8f94; }
@media (max-width: 760px) {
  html.big .panel { max-height: 34vh; }
}

/* Terzo livello di testo */
html.bigger .topbar, html.bigger .container, html.bigger .panel { zoom: 1.7; }
html.bigger .panel { width: 260px; }
html.bigger .leaflet-popup-content { font-size: 21px; min-width: 300px; }
html.bigger .leaflet-popup-content h4 { font-size: 24px; }
html.bigger .leaflet-popup-content .pbtns button, html.bigger .leaflet-popup-content .pbtns a { font-size: 19px; padding: 8px 14px; }
html.bigger .leaflet-control-zoom a { width: 54px; height: 54px; line-height: 54px; font-size: 32px; }
html.bigger .leaflet-tooltip { font-size: 20px; }
html.bigger .handle { width: 30px; height: 30px; }
html.bigger .handle.mid { width: 26px; height: 26px; }
html.bigger .map-banner { font-size: 22px; }
@media (max-width: 760px) {
  html.bigger .panel { width: 100%; max-height: 28vh; }
}

/* Filtri età */
.age-filter { border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px 8px; margin: 0 0 8px; font-size: 13px; }
.age-filter legend { color: var(--muted); font-size: 12px; padding: 0 4px; }
.age-filter label { display: flex; align-items: center; gap: 6px; margin: 2px 0; color: var(--text); font-size: 13px; cursor: pointer; }
.age-filter input { width: auto; margin: 0; }
.swatch { display: inline-block; width: 22px; height: 5px; border-radius: 3px; }
.swatch.age-1 { background: #2e9d4f; } .swatch.age-2 { background: #e0b400; } .swatch.age-3 { background: #ee7a10; }
.swatch.age-4 { background: #d1342b; } .swatch.age-0 { background: #8a8f94; }
.filters .age-filter { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 0; flex: 1 1 100%; }
.filters .age-filter legend { width: 100%; }
.handle.site { background: #00a8ff; width: 20px; height: 20px; border-radius: 4px; }
html.big .handle.site { width: 28px; height: 28px; }
html.bigger .handle.site { width: 34px; height: 34px; }
.panel { overflow-x: hidden; }

/* ===== Layout mappa: menu a sinistra, mappa, pannello modifica a destra ===== */
.logo { display: block; border-radius: 8px; flex: none; }
.top-tools { display: flex; align-items: center; gap: 8px; margin-left: auto; position: relative; }
.top-tools + .nav { margin-left: 12px; }
.btn.icon { width: 32px; height: 32px; padding: 0; justify-content: center; border-radius: 50%; font-weight: 700; }
.btn.icon.info { font-family: Georgia, serif; font-style: italic; }
.who { font-size: 13px; color: var(--muted); white-space: nowrap; }
.info-pop {
  position: absolute; top: calc(100% + 10px); right: 0; z-index: 1300; width: min(420px, 90vw);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0,0,0,.18); padding: 12px 14px; font-size: 14px; line-height: 1.5;
}
.info-pop ul { margin: 6px 0 0; padding-left: 18px; }
.info-pop[hidden] { display: none; }

.map-layout { position: relative; }
.sidebar {
  width: 290px; flex: none; background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto; overflow-x: hidden; padding: 12px 16px 30px;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.sidebar h2 { font-size: 15px; margin: 18px 0 8px; }
.sidebar-head h2 { margin: 0; }
.sidebar .row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.sidebar select, .sidebar input:not([type=checkbox]) {
  width: 100%; padding: 6px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
body.sidebar-hidden .sidebar { display: none; }
.edit-panel { display: none; }
body.editing .edit-panel { display: block; }
.log-box { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 12px; margin-top: 14px; }
.log-box summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.sidebar-toggle a { font-size: 18px; font-weight: 700; color: #222; text-decoration: none; }

/* Nomi dei comprensori sulla mappa */
.leaflet-tooltip.site-label {
  background: rgba(255,255,255,.88); border: 0; box-shadow: 0 1px 3px rgba(0,0,0,.25);
  font-weight: 600; font-size: 12px; padding: 1px 6px; color: #1d2327;
}
.leaflet-tooltip.site-label::before { display: none; }
.labels-off .leaflet-tooltip.site-label { display: none; }
html.big .leaflet-tooltip.site-label { font-size: 16px; }
html.bigger .leaflet-tooltip.site-label { font-size: 20px; }

html.big .sidebar { zoom: 1.35; width: 240px; }
html.bigger .sidebar { zoom: 1.7; width: 210px; }

@media (max-width: 760px) {
  .sidebar {
    position: absolute; left: 0; top: 0; bottom: 0; z-index: 1100; width: 85%; max-width: 340px;
    box-shadow: 4px 0 20px rgba(0,0,0,.25);
  }
  html.big .sidebar, html.bigger .sidebar { width: 62%; }
  .who { display: none; }
  .top-tools + .nav { margin-left: 4px; }
}
.map-page .topbar { z-index: 1500; }

/* Comprensorio nel form impianto (dettagli richiudibili) */
.site-box { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin: 8px 0; background: var(--surface); }
.site-box summary { cursor: pointer; font-size: 14px; color: var(--accent); }
.site-box summary b { color: var(--text); }
.site-box[open] summary { margin-bottom: 4px; }
.btn svg { flex: none; }
/* Nome comprensorio cliccabile ed evidenziato */
.leaflet-tooltip.site-label { cursor: pointer; pointer-events: auto; }
.leaflet-tooltip.site-label:hover { background: #fff; text-decoration: underline; }
.leaflet-tooltip.site-label.active { background: #00a8ff; color: #fff; }

/* Stato del singolo impianto */
.badge.stato-imp.attivo { background: var(--ok-soft); color: var(--ok); }
.badge.stato-imp.dismesso { background: var(--surface-2); color: var(--muted); }
.badge.stato-imp.demolito { background: #3b3f44; color: #fff; }
#fStatoImp, #fCostruttore { margin-top: 0; }

/* Icona del tipo di impianto nel popup */
.pop-head { display: flex; gap: 10px; align-items: flex-start; }
.tipo-icon {
  flex: none; width: 42px; height: 42px; border-radius: 12px; display: flex; align-items: center; justify-content: center;
  background: #e2edf5; color: #1f5f8b;
}
html.big .tipo-icon { width: 54px; height: 54px; }
html.big .tipo-icon svg { width: 34px; height: 34px; }
html.bigger .tipo-icon { width: 66px; height: 66px; }
html.bigger .tipo-icon svg { width: 42px; height: 42px; }

/* Comprensorio in testa alla scheda */
.pop-site {
  display: flex; align-items: center; gap: 6px; margin: -2px 0 8px; padding-bottom: 6px;
  border-bottom: 1px solid #e3e6ea; color: #1f5f8b; font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .03em;
}
.pop-site svg { flex: none; }
html.big .pop-site { font-size: 16px; } html.big .pop-site svg { width: 22px; height: 22px; }
html.bigger .pop-site { font-size: 19px; } html.bigger .pop-site svg { width: 26px; height: 26px; }

/* Selettore degli sfondi: lista lunga, scorrevole */
.leaflet-control-layers-expanded { max-height: 70vh; overflow-y: auto; }
.leaflet-control-layers-base label { font-size: 12.5px; }
html.big .leaflet-control-layers-base label, html.big .leaflet-control-layers-overlays label { font-size: 16px; }
