:root {
  color-scheme: light;
  --bg: #ffffff;
  --panel: #f6f7f9;
  --border: #dde1e6;
  --border-soft: #eef0f2;
  --text: #1f2933;
  --muted: #616e7c;
  --accent: #2f6fec;
  --accent-soft: #eef3ff;
  --good: #1a7f37;
  --warning: #9a6700;
  --warning-soft: #fff7e0;
  --critical: #b00020;
  --critical-soft: #fdecee;
  --top: #1f2933;
  --star-off: #c1c7cd;
  --shadow: rgba(0,0,0,.08);
  --tiles-filter: none;
  --button: #2f6fec;
}
/* Dark theme: follows the system unless the user picked Chiaro, or forced with Scuro. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
  color-scheme: dark;
  --bg: #161b22;
  --panel: #0f1419;
  --border: #2d3540;
  --border-soft: #222a33;
  --text: #e6e9ed;
  --muted: #9aa5b1;
  --accent: #79a6ff;
  --accent-soft: #1c2a45;
  --good: #4ac26b;
  --warning: #e3b341;
  --warning-soft: #2e2614;
  --critical: #ff7b72;
  --critical-soft: #3a1d1d;
  --top: #0b0f14;
  --star-off: #4b5563;
  --shadow: rgba(0,0,0,.5);
  --tiles-filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #161b22;
  --panel: #0f1419;
  --border: #2d3540;
  --border-soft: #222a33;
  --text: #e6e9ed;
  --muted: #9aa5b1;
  --accent: #79a6ff;
  --accent-soft: #1c2a45;
  --good: #4ac26b;
  --warning: #e3b341;
  --warning-soft: #2e2614;
  --critical: #ff7b72;
  --critical-soft: #3a1d1d;
  --top: #0b0f14;
  --star-off: #4b5563;
  --shadow: rgba(0,0,0,.5);
  --tiles-filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.5);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}
a { color: var(--accent); }
code { font-size: 12px; background: var(--panel); padding: 1px 4px; border-radius: 3px; }
.muted { color: var(--muted); font-size: 12px; }

/* ---------- buttons ---------- */
button { font: inherit; }
button.primario, a.primario {
  display: inline-block; padding: 7px 14px; border: 0; border-radius: 6px;
  background: var(--button); color: #fff; font-weight: 600; cursor: pointer; text-decoration: none;
}
button.primario:disabled { opacity: .5; cursor: not-allowed; }
button.secondario {
  padding: 7px 14px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); cursor: pointer;
}
button.piccolo { padding: 3px 10px; font-size: 12px; }
button.link, a.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 12px; text-decoration: none; }
button.link:hover, a.link:hover { text-decoration: underline; }
button.icon { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 4px; }
button.icon:hover { color: var(--critical); }

/* ---------- top bar ---------- */
.top {
  display: flex; align-items: center; gap: 20px;
  height: 48px; padding: 0 16px;
  background: var(--top); color: #f5f7fa;
}
.top .brand { display: flex; align-items: center; gap: 8px; color: #fff; font-weight: 700; text-decoration: none; white-space: nowrap; }
.top .brand .logo { height: 28px; width: auto; flex-shrink: 0; }
.brand-azienda { font-weight: 800; letter-spacing: .02em; }

/* theme switch: in the top bar, and under the login box */
.tema { display: inline-flex; border: 1px solid rgba(255,255,255,.22); border-radius: 6px; overflow: hidden; flex-shrink: 0; }
.tema button { border: 0; background: none; color: #cbd2d9; padding: 3px 8px; font-size: 14px; line-height: 1.2; cursor: pointer; }
.tema button + button { border-left: 1px solid rgba(255,255,255,.22); }
.tema button:hover { color: #fff; background: rgba(255,255,255,.08); }
.tema button.attivo { color: #fff; background: rgba(255,255,255,.18); }
.login-tema { margin-top: 16px; }
.login-tema .tema { border-color: var(--border); }
.login-tema .tema button { color: var(--muted); }
.login-tema .tema button + button { border-left-color: var(--border); }
.login-tema .tema button:hover, .login-tema .tema button.attivo { color: var(--text); background: var(--panel); }
.top nav { display: flex; gap: 4px; flex: 1; align-items: center; min-width: 0; overflow-x: auto; }
.top nav a, .top nav .nav-btn {
  color: #cbd2d9; text-decoration: none; padding: 6px 10px; border-radius: 6px; font-size: 14px;
}
.top nav a:hover, .top nav .nav-btn:hover { color: #fff; background: rgba(255,255,255,.08); text-decoration: none; }
.top nav a.attivo { color: #fff; background: rgba(255,255,255,.14); }
.top form button.link { color: #cbd2d9; font-size: 14px; }
.nav-icona { display: none; }

/* ---------- login ---------- */
body.pagina-login { min-height: 100vh; background: var(--panel); }
.login { display: flex; flex-direction: column; align-items: center; padding: 10vh 16px; }
.login .card { text-align: center; }
.login-marchio .logo { height: 76px; width: auto; color: var(--text); margin-bottom: 8px; }
.card {
  width: 100%; max-width: 360px; padding: 24px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--bg);
  box-shadow: 0 4px 16px var(--shadow);
}
.card h1 { font-size: 20px; margin: 0 0 4px; }
.card form { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
input[type=password], input[type=text], input[type=search] { background: var(--bg); color: var(--text); }
.card input[type=password] { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
.avviso { margin-top: 10px; padding: 8px 10px; border-radius: 6px; background: var(--warning-soft); color: var(--warning); font-size: 13px; }
.avviso.errore { background: var(--critical-soft); color: var(--critical); }

/* ---------- map page layout ---------- */
body.pagina-mappa { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
.layout { flex: 1; display: flex; min-height: 0; }
#sidebar {
  width: 350px; flex-shrink: 0; overflow-y: auto;
  border-right: 1px solid var(--border); background: var(--panel); padding: 16px;
}
#map-wrap { flex: 1; position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }
.nuovo-set { display: block; margin: 8px 0 0; }

.upload {
  display: block; padding: 14px; border: 2px dashed var(--border); border-radius: 8px;
  text-align: center; cursor: pointer; background: var(--bg);
}
.upload span { display: block; color: var(--muted); font-size: 12px; }
.upload.dragover { border-color: var(--accent); background: var(--accent-soft); }

.option { display: flex; gap: 6px; align-items: flex-start; margin: 8px 0 0; font-size: 13px; cursor: pointer; }
.option input { margin-top: 3px; }
#notice { min-height: 1em; margin: 8px 0; font-size: 12px; color: var(--critical); }
#notice.ok { color: var(--good); }
.migrazione { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; padding: 10px; border-radius: 8px; background: var(--warning-soft); font-size: 12px; }

.dataset {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; margin-bottom: 10px;
}
.dataset.hidden-ds { opacity: 0.55; }
.row { display: flex; gap: 6px; align-items: center; }
.ds-color { width: 30px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 4px; background: none; cursor: pointer; }
.ds-label { flex: 1; min-width: 0; padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px; font: inherit; font-weight: 600; }
.ds-stats { margin: 6px 0 2px; }
.ds-arricchisci { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--warning-soft); color: var(--warning); font-weight: 600; }
.azioni-set { margin-top: 6px; }
.ds-job { margin-top: 8px; }
.ds-job-text { margin: 4px 0; }
.barra { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; }
.barra > div { height: 100%; width: 0; background: var(--accent); transition: width .4s; }

.ds-list summary { cursor: pointer; font-size: 13px; margin-top: 6px; }
.list-tools { display: flex; gap: 4px; align-items: center; margin: 6px 0; font-size: 12px; color: var(--muted); }
.ds-filter { flex: 1; min-width: 0; padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px; font: inherit; font-size: 12px; }
.companies { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow-y: auto; }
.companies li { display: flex; gap: 6px; align-items: flex-start; padding: 3px 0; font-size: 12px; border-bottom: 1px solid var(--border-soft); }
.companies li .name { flex: 1; cursor: pointer; }
.companies li .name:hover { color: var(--accent); }
.companies li.no-loc .name { color: var(--muted); }
.companies li.is-hidden .name { color: var(--muted); text-decoration: line-through; }
.companies li .tag { font-size: 10px; color: var(--muted); white-space: nowrap; }
.star { border: 0; background: none; cursor: pointer; padding: 0 2px; font-size: 14px; line-height: 1.2; color: var(--star-off); }
.star.on { color: #f5a300; }

/* ---------- Leaflet in the current theme ---------- */
.leaflet-container { background: var(--panel); }
.leaflet-tile-pane { filter: var(--tiles-filter); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--bg); color: var(--text); }
.leaflet-container a { color: var(--accent); }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-bar a, .leaflet-bar a:hover { background: var(--bg); color: var(--text); border-bottom-color: var(--border); }
.leaflet-control-attribution { background: var(--bg) !important; color: var(--muted); opacity: .85; }

/* ---------- markers ---------- */
.pin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.35); }
.pin.approx { background: #fff !important; border-width: 3px; box-shadow: 0 0 0 1px rgba(0,0,0,.2); }
.pin-fav { font-size: 22px; line-height: 22px; width: 22px; text-align: center; -webkit-text-stroke: 1.5px #fff; text-shadow: 0 0 2px rgba(0,0,0,.6); }
.cluster {
  display: flex; align-items: center; justify-content: center; border-radius: 50%;
  color: #fff; font-weight: 700; font-size: 12px;
  border: 3px solid rgba(255,255,255,.85); box-shadow: 0 0 0 1px rgba(0,0,0,.3);
}

/* ---------- popups ---------- */
.popup h3 { margin: 0 0 2px; font-size: 14px; }
.popup .sub { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.popup dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 2px 8px; font-size: 12px; }
.popup dt { color: var(--muted); }
.popup dd { margin: 0; word-break: break-word; }
.popup .warn { margin-top: 6px; font-size: 11px; color: var(--warning); }
.popup .actions { display: flex; gap: 6px; margin-top: 8px; }
.popup .actions button { padding: 3px 8px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); cursor: pointer; font-size: 12px; }
.popup .actions button.on { border-color: #f5a300; color: var(--warning); background: var(--warning-soft); }

/* ---------- dialogs ---------- */
.dialog {
  width: min(480px, calc(100vw - 32px)); padding: 20px; border: 0; border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,.25); color: var(--text); background: var(--bg);
}
.dialog::backdrop { background: rgba(15,23,32,.45); }
.dialog h2 { margin: 0 0 8px; font-size: 18px; }
.dialog p { margin: 0 0 8px; }
.dialog fieldset { margin: 14px 0 0; padding: 8px 12px 10px; border: 1px solid var(--border); border-radius: 8px; }
.dialog legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.campo { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 13px; }
.campo input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; font-weight: 400; }
.campo small { font-weight: 400; }
.dialog-azioni { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.dialog-azioni .spazio { flex: 1; }
button.pericolo { color: var(--critical); border-color: var(--critical); }

/* company editor */
.dialog-largo { width: min(640px, calc(100vw - 32px)); }
.dialog-largo .campo { margin-top: 10px; }
.campo textarea { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; font-weight: 400;
  background: var(--bg); color: var(--text); resize: vertical; }
.campo small { font-weight: 400; }
.cerca { display: flex; gap: 6px; }
.cerca input { flex: 1; min-width: 0; }
.cerca button { white-space: nowrap; padding: 6px 10px; font-size: 13px; }
.griglia { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.risultati { list-style: none; margin: 6px 0 0; padding: 6px; border: 1px solid var(--border); border-radius: 6px; max-height: 160px; overflow-y: auto; }
.risultati li { padding: 4px 2px; border-bottom: 1px solid var(--border-soft); }
.risultati li:last-child { border-bottom: 0; }
.risultati li button.link { font-size: 13px; text-align: left; display: block; }
.risultati li small { display: block; }
.mini-mappa { height: 220px; margin-top: 8px; border-radius: 8px; border: 1px solid var(--border); }
#az-pos { display: block; margin-top: 4px; }
.companies li .modifica-riga { padding: 0 2px; font-size: 12px; }
.stima { margin-top: 14px; padding: 12px; border-radius: 8px; background: var(--panel); font-size: 13px; }
.stima ul { margin: 6px 0; padding-left: 18px; }
.stima-costo { font-size: 15px; }
.stima-costo strong { font-size: 20px; }

/* ---------- toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 2000; display: flex; flex-direction: column; gap: 8px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  padding: 12px 14px; border-radius: 8px; background: var(--top); color: #fff; font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); transition: opacity .5s, transform .5s;
}
.toast.errore { background: #b00020; }
.toast.via { opacity: 0; transform: translateY(8px); }

/* ---------- guide ---------- */
.guida { max-width: 820px; margin: 0 auto; padding: 24px 16px 64px; font-size: 15px; line-height: 1.6; }
.guida h1 { font-size: 26px; margin: 0 0 4px; }
.guida h2 { font-size: 20px; margin: 36px 0 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.guida h3 { font-size: 16px; margin: 20px 0 6px; }
.guida ol, .guida ul { padding-left: 22px; }
.guida li { margin: 4px 0; }
.guida table { border-collapse: collapse; width: 100%; font-size: 14px; margin: 8px 0; }
.guida th, .guida td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.guida .riquadro { padding: 14px 16px; border-radius: 10px; background: var(--panel); border: 1px solid var(--border); margin: 12px 0; }
.guida .download { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }
/* Download buttons: one colour per browser so they're easy to tell apart. */
.scarica {
  display: inline-flex; align-items: center; gap: 10px; padding: 8px 16px 8px 8px; border-radius: 8px;
  color: #fff; font-weight: 600; text-decoration: none; box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.scarica:hover { filter: brightness(1.1); text-decoration: none; }
.scarica-firefox { background: #c2410c; }
.scarica-chrome { background: #1a73e8; }
.scarica-icona { display: inline-flex; width: 34px; height: 34px; padding: 4px; border-radius: 50%; background: #fff; flex-shrink: 0; }
.scarica-icona svg { width: 100%; height: 100%; }
.guida .indice { columns: 2; font-size: 14px; }
.guida kbd { font: 12px ui-monospace, monospace; padding: 1px 5px; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; background: var(--bg); }

@media (max-width: 700px) {
  .layout { flex-direction: column-reverse; }
  #sidebar { width: 100%; height: 45%; border-right: 0; border-top: 1px solid var(--border); }
  .griglia { grid-template-columns: 1fr; }
  .cerca { flex-direction: column; }
    .top { gap: 8px; padding: 0 10px; }
  .top .brand > span { display: none; } /* logo only: the bar is too narrow for the name */
  .nav-icona { display: inline; font-size: 16px; }
  .nav-testo { display: none; }
  .tema button { padding: 3px 6px; }
  .top nav a, .top nav .nav-btn { padding: 6px 7px; }
  .guida .indice { columns: 1; }
}
