/* Radar Urbano backoffice */
:root {
  color-scheme: light;
  --bg: #f5f7fa; --panel: #ffffff; --soft: #eef1f5; --line: #e2e6ec;
  --text: #0f172a; --text2: #475569; --muted: #64748b;
  --accent: #16a34a; --accent-hover: #15803d; --accent-text: #ffffff; --accent-soft: #dcfce7;
  --ok: #0ca30c; --warn: #b26a00; --err: #d03b3b;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 1px rgba(15, 23, 42, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b1120; --panel: #111827; --soft: #1a2333; --line: #263043;
    --text: #e5e9f0; --text2: #b6c0cf; --muted: #8b97a8;
    --accent: #22c55e; --accent-hover: #4ade80; --accent-text: #052e16; --accent-soft: #14361f;
    --ok: #0ca30c; --warn: #f0a63a; --err: #e66767;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1120; --panel: #111827; --soft: #1a2333; --line: #263043;
  --text: #e5e9f0; --text2: #b6c0cf; --muted: #8b97a8;
  --accent: #22c55e; --accent-hover: #4ade80; --accent-text: #052e16; --accent-soft: #14361f;
  --ok: #0ca30c; --warn: #f0a63a; --err: #e66767;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.loading { color: var(--muted); padding: 24px 0; }
.loading.boot { padding: 40px; text-align: center; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ shell */
.topbar { position: sticky; top: 0; z-index: 1000; display: flex; align-items: center; gap: 16px;
  padding: 0 20px; height: 56px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; white-space: nowrap; }
.brand img { width: 28px; height: 28px; border-radius: 7px; }
.brand small { font-weight: 500; color: var(--muted); }
nav.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs a { padding: 8px 12px; border-radius: 8px; color: var(--text2); text-decoration: none; white-space: nowrap; font-weight: 500; }
nav.tabs a:hover { background: var(--soft); color: var(--text); }
nav.tabs a.on { background: var(--accent-soft); color: var(--text); }
.who { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.who .role { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--soft); color: var(--muted); }
main.page { max-width: 1280px; margin: 0 auto; padding: 20px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px; gap: 8px; }
  .brand small, .who .role { display: none; }
  nav.tabs { order: 3; flex-basis: 100%; }
  main.page { padding: 16px; }
}

/* ------------------------------------------------------------ controlli */
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 8px 14px;
  border-radius: 8px; cursor: pointer; font-weight: 500; }
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.link { border: 0; background: none; color: var(--text2); padding: 6px 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.seg { display: inline-flex; background: var(--soft); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; cursor: pointer; color: var(--text2); white-space: nowrap; }
.seg button.on { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: var(--shadow), 0 0 0 1px var(--line); }
label.field { display: block; font-weight: 600; margin: 12px 0 4px; }
label.field:first-child { margin-top: 0; }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; }
input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--soft); }
select { width: auto; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; min-height: 70px; }
.counter { float: right; font-weight: 400; color: var(--muted); font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1; min-width: 120px; }

/* ------------------------------------------------------------ card, stat */
.grid { display: grid; gap: 16px; }
.grid.stats { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); margin-bottom: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
@media (max-width: 480px) { .grid.two { grid-template-columns: 1fr; } }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card h2 { font-size: 15px; margin: 0 0 12px; }
.card h2 .hint { margin-left: 6px; }
.card p.note { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat-label { color: var(--text2); font-size: 13px; }
.stat-value { font-size: 28px; font-weight: 700; letter-spacing: -.01em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.stat-delta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat-delta.up { color: var(--text2); }
.stat-delta.down { color: var(--text2); }
.msg { margin-top: 10px; padding: 10px 12px; border-radius: 8px; background: var(--soft); white-space: pre-line; border-left: 4px solid var(--muted); }
.msg.ok { border-left-color: var(--ok); }
.msg.warn { border-left-color: var(--warn); }
.msg.err { border-left-color: var(--err); }
.msg.info { border-left-color: var(--s1); }

/* ------------------------------------------------------------ grafici */
.chart { margin: 0; }
.chart-box { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { stroke: var(--muted); stroke-width: 1; opacity: .5; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .cross { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .dot { stroke: var(--panel); stroke-width: 2; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text2); margin-bottom: 8px; }
.legend i, .tooltip i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.tooltip { position: fixed; z-index: 5000; display: none; pointer-events: none; background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.18); min-width: 150px; }
.tip-title { font-weight: 600; margin-bottom: 4px; }
.tip-row { display: grid; grid-template-columns: auto 1fr auto; gap: 0 4px; align-items: center; color: var(--text2); }
.tip-row b { color: var(--text); margin-left: 12px; font-variant-numeric: tabular-nums; }
details.datatable { margin-top: 10px; font-size: 12px; }
details.datatable summary { cursor: pointer; color: var(--muted); }
.table-wrap { overflow-x: auto; max-height: 340px; overflow-y: auto; }

/* barre orizzontali */
.barlist { display: grid; gap: 8px; }
.barlist-row { display: grid; grid-template-columns: minmax(90px, 140px) 1fr auto; gap: 10px; align-items: center; }
.barlist-label { color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barlist-track { height: 10px; background: var(--soft); border-radius: 5px; overflow: hidden; }
.barlist-fill { height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.barlist-val { font-variant-numeric: tabular-nums; text-align: right; min-width: 80px; }

/* tabelle */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12px; position: sticky; top: 0; background: var(--panel); }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
td code { font-size: 12px; }
.tag { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 10px; background: var(--soft); color: var(--text2); white-space: nowrap; }
.tag.ok::before, .tag.no::before, .tag.warn::before { content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%; margin-right: 5px; vertical-align: 1px; }
.tag.ok::before { background: var(--ok); }
.tag.no::before { background: var(--err); }
.tag.warn::before { background: var(--warn); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px; }
.cohort td { text-align: center; font-variant-numeric: tabular-nums; border: 2px solid var(--panel); border-radius: 4px; }
.cohort td.lbl { text-align: left; white-space: nowrap; }

/* ------------------------------------------------------------ login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 380px; }
.login .brand { justify-content: center; margin-bottom: 18px; font-size: 18px; }
.login .brand img { width: 40px; height: 40px; border-radius: 10px; }
.login .btn.primary { width: 100%; margin-top: 16px; padding: 11px; }
.qr { display: grid; place-items: center; background: #fff; border-radius: 10px; padding: 10px; margin: 12px auto; width: fit-content; }
.qr svg { display: block; }
.secret { font-family: ui-monospace, Consolas, monospace; font-size: 13px; word-break: break-all; background: var(--soft); padding: 8px; border-radius: 6px; user-select: all; }
input.code { font-size: 22px; letter-spacing: .3em; text-align: center; font-variant-numeric: tabular-nums; }
ol.steps { padding-left: 20px; color: var(--text2); margin: 8px 0; }

/* ------------------------------------------------------------ mappa */
.map-layout { display: grid; grid-template-columns: 1fr 320px; gap: 16px; height: calc(100vh - 56px - 40px - 70px); min-height: 480px; }
.map-layout .mapbox { position: relative; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); min-height: 360px; }
.map-layout .side { overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
#map, .leaflet-container { height: 100%; width: 100%; background: var(--soft); font: inherit; }
.map-legend { position: absolute; z-index: 500; left: 10px; bottom: 22px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: var(--shadow); }
.map-legend .ramp { width: 160px; height: 10px; border-radius: 3px; margin: 4px 0 2px; }
.map-legend .ends { display: flex; justify-content: space-between; color: var(--muted); }
.maphint { position: absolute; z-index: 500; top: 10px; left: 50%; transform: translateX(-50%); background: var(--panel);
  border: 1px solid var(--line); border-radius: 20px; padding: 6px 14px; font-size: 13px; box-shadow: 0 2px 8px rgba(0,0,0,.15); white-space: nowrap; }
.toplist { list-style: none; padding: 0; margin: 0; }
.toplist li { display: flex; justify-content: space-between; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.toplist li:hover { color: var(--accent); }
.toplist li:last-child { border-bottom: 0; }
.leaflet-tooltip { font: 12px/1.4 system-ui, sans-serif; }
@media (max-width: 900px) {
  .map-layout { grid-template-columns: 1fr; height: auto; }
  .map-layout .mapbox { height: 60vh; }
}

/* ------------------------------------------------------------ notifiche */
.push-layout { display: grid; grid-template-columns: 420px 1fr; gap: 16px; }
.push-layout .mapbox { position: relative; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); height: 560px; position: sticky; top: 76px; }
.tabs4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; background: var(--soft); padding: 3px; border-radius: 9px; }
.tabs4 button { border: 0; background: transparent; padding: 7px 4px; border-radius: 7px; cursor: pointer; color: var(--text2); }
.tabs4 button.on { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--line); }
.phone { background: var(--soft); border-radius: 14px; padding: 10px 12px; margin-top: 12px; }
.phone .app { font-size: 11px; color: var(--muted); }
.phone .t { font-weight: 700; word-break: break-word; }
.phone .b { word-break: break-word; }
.big { font-size: 26px; font-weight: 700; }
.devlist { list-style: none; padding: 0; margin: 8px 0 0; }
.devlist li { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); }
.actions { display: flex; gap: 8px; margin-top: 14px; }
.actions .btn { flex: 1; padding: 10px; }
@media (max-width: 960px) {
  .push-layout { grid-template-columns: 1fr; }
  .push-layout .mapbox { position: relative; top: 0; height: 380px; order: -1; }
}

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); padding: 20px; max-width: 440px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(2, 6, 23, .55); }
dialog h3 { margin: 0 0 8px; }

/* mappa di base OpenStreetMap attenuata: i dati restano in primo piano */
.basemap { filter: grayscale(1) contrast(.85) brightness(1.06); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .basemap { filter: grayscale(1) invert(1) brightness(.85) contrast(.85); }
}
:root[data-theme="dark"] .basemap { filter: grayscale(1) invert(1) brightness(.85) contrast(.85); }
