/* Tracker GPS - stile dashboard. Tema chiaro/scuro automatico, mobile first. */
:root {
  color-scheme: light dark;
  --bg: #f3f5f8; --card: #ffffff; --card2: #f8fafc; --ink: #17202b; --muted: #637083; --line: #e1e6ec;
  --brand: #2563eb; --brand-ink: #ffffff; --brand-soft: #e8f0fe;
  --ok: #15803d; --ok-bg: #dcf5e4; --warn: #a16207; --warn-bg: #fdf0c9; --bad: #b91c1c; --bad-bg: #fde4e4; --off-bg: #e9edf2;
  --head: #111b27; --head-ink: #d4e1f2;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e141b; --card: #161e28; --card2: #1b2531; --ink: #e6edf5; --muted: #93a1b3; --line: #263241;
    --brand: #5b9bff; --brand-ink: #08111d; --brand-soft: #172742;
    --ok: #5fd68a; --ok-bg: #12301f; --warn: #f0c14b; --warn-bg: #3a2e0c; --bad: #ff8a8a; --bad-bg: #3b1616; --off-bg: #222d3a;
    --head: #0a0f15; --head-ink: #aebdd0;
    --shadow: none;
  }
  .leaflet-tile-pane { filter: brightness(.82) contrast(1.05); }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 6px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- Intestazione ---- */
header.top { position: sticky; top: 0; z-index: 1000; background: var(--head); color: #fff; display: flex; align-items: center; gap: 6px 18px; padding: 0 18px; min-height: 54px; flex-wrap: wrap; }
header.top .brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 16px; color: #fff; padding: 12px 0; }
header.top .brand svg { width: 22px; height: 22px; color: var(--brand); }
header.top nav { display: flex; gap: 4px; flex: 1; }
header.top nav a { color: var(--head-ink); padding: 8px 12px; border-radius: 8px; font-weight: 500; }
header.top nav a:hover { background: rgba(255, 255, 255, .08); text-decoration: none; }
header.top nav a.active { color: #fff; background: rgba(255, 255, 255, .14); }
header.top .who { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--head-ink); }
header.top .who a { color: var(--head-ink); padding: 6px 8px; border-radius: 6px; }
header.top .who a:hover { background: rgba(255, 255, 255, .08); text-decoration: none; }
header.top .avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--brand); color: var(--brand-ink); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
header.top .who .uname { line-height: 1.2; }
header.top .who .uname small { display: block; color: #8ea1b8; font-size: 11px; }

main { max-width: 1180px; margin: 0 auto; padding: 22px 18px 60px; }
h1 { font-size: 24px; line-height: 1.25; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
.pagehead { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-bottom: 18px; }
.pagehead .grow { flex: 1; min-width: 0; }
.crumb { font-size: 13px; color: var(--muted); margin-bottom: 4px; display: inline-block; }
.sub { color: var(--muted); font-size: 13px; }

/* ---- Card e griglie ---- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card > h2:first-child, .cardhead h2 { margin-bottom: 12px; }
.cardhead { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.cardhead h2 { margin: 0; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); margin-bottom: 16px; }
.grid .card { margin: 0; }
.kpi { display: flex; gap: 12px; align-items: flex-start; }
.kpi .ico { flex: none; width: 38px; height: 38px; border-radius: 10px; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; }
.kpi .ico svg { width: 20px; height: 20px; }
.kpi.t-ok .ico { background: var(--ok-bg); color: var(--ok); }
.kpi.t-warn .ico { background: var(--warn-bg); color: var(--warn); }
.kpi.t-bad .ico { background: var(--bad-bg); color: var(--bad); }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 21px; font-weight: 650; line-height: 1.25; margin-top: 1px; overflow-wrap: anywhere; }
.kpi .sub { margin-top: 1px; }
.kpi > div:last-child { min-width: 0; }

/* ---- Tabelle ---- */
.tablewrap { overflow-x: auto; padding: 0; }
.card.tablewrap > h2 { padding: 16px 16px 0; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); white-space: nowrap; vertical-align: middle; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; background: var(--card2); }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--card2); }
tr.clickable { cursor: pointer; }
td .devname { font-weight: 600; }

/* ---- Badge, barre, puntini ---- */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; line-height: 1.6; vertical-align: middle; }
.badge .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.b-ok { background: var(--ok-bg); color: var(--ok); }
.b-warn { background: var(--warn-bg); color: var(--warn); }
.b-bad { background: var(--bad-bg); color: var(--bad); }
.b-off { background: var(--off-bg); color: var(--muted); }
.bar { width: 64px; height: 8px; background: var(--off-bg); border-radius: 4px; display: inline-block; vertical-align: middle; margin-right: 8px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--ok); border-radius: 4px; }
.bar.mid > i { background: var(--warn); }
.bar.low > i { background: var(--bad); }
.battcell { display: inline-flex; align-items: center; white-space: nowrap; }
.chg { color: var(--ok); margin-left: 4px; font-weight: 600; }

/* ---- Pulsanti e campi ---- */
button, .btn { font: inherit; font-weight: 500; padding: 8px 14px; border-radius: 9px; border: 1px solid var(--line); background: var(--card); color: var(--ink); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 7px; transition: background .12s, border-color .12s; }
button:hover { background: var(--card2); border-color: #b9c4d1; }
button.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, var(--line)); }
button.danger:hover { background: var(--bad-bg); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); padding: 6px 10px; }
button.ghost:hover { background: var(--off-bg); color: var(--ink); }
button.sm { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: .55; cursor: default; }
button svg { width: 16px; height: 16px; }
form.inline, .row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.row.tight { gap: 8px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 500; }
label small { font-weight: 400; color: var(--muted); font-size: 12px; }
input, select { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--card); color: var(--ink); min-width: 0; }
input:focus, select:focus { border-color: var(--brand); outline: 2px solid color-mix(in srgb, var(--brand) 28%, transparent); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--brand); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
.search { position: relative; min-width: 220px; }
.search input { width: 100%; padding-left: 34px; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--muted); pointer-events: none; }
.seg { display: inline-flex; background: var(--off-bg); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 12px; border-radius: 8px; font-size: 13px; color: var(--muted); }
.seg button:hover { background: transparent; color: var(--ink); }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); font-weight: 600; }

/* ---- Messaggi, avvisi ---- */
.err, .okmsg, .warnmsg { padding: 10px 14px; border-radius: 10px; margin-bottom: 14px; }
.err { background: var(--bad-bg); color: var(--bad); }
.okmsg { background: var(--ok-bg); color: var(--ok); }
.warnmsg { background: var(--warn-bg); color: var(--warn); }
.secret { background: var(--warn-bg); border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); border-radius: 10px; padding: 14px; margin-bottom: 16px; }
.secret code { display: block; background: var(--card); border: 1px solid var(--line); padding: 7px 10px; margin: 8px 0; border-radius: 8px; word-break: break-all; user-select: all; color: var(--ink); }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 34px 16px; }
.empty svg { width: 34px; height: 34px; opacity: .5; margin-bottom: 6px; }
.alerts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }

/* ---- Toast ---- */
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 3000; display: flex; flex-direction: column; gap: 8px; max-width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--head); color: #fff; padding: 11px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, .25); animation: pop .18s ease-out; border-left: 4px solid var(--ok); }
.toast.bad { border-left-color: #ff6b6b; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---- Finestre di dialogo ---- */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--card); color: var(--ink); width: min(420px, calc(100vw - 32px)); box-shadow: 0 20px 50px rgba(0, 0, 0, .3); }
dialog::backdrop { background: rgba(8, 14, 22, .55); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
dialog h2 { margin: 0; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---- Mappa e grafico ---- */
.map { height: 380px; border-radius: 10px; border: 1px solid var(--line); background: var(--off-bg); position: relative; z-index: 0; }
.map.big { height: 340px; }
.leaflet-container { font: inherit; }
.pin { width: 30px; height: 30px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); background: var(--brand); border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, .4); }
.pin.bad { background: #dc2626; } .pin.off { background: #7b8794; } .pin.ok { background: #16a34a; }
.pin::after { content: ""; position: absolute; inset: 7px; border-radius: 50%; background: #fff; }
.trkdot { width: 10px; height: 10px; border-radius: 50%; background: var(--brand); border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .25); }
svg.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chartwrap { position: relative; }
.charttip { position: absolute; pointer-events: none; background: var(--head); color: #fff; font-size: 12px; padding: 5px 9px; border-radius: 7px; transform: translate(-50%, -125%); white-space: nowrap; display: none; z-index: 2; }

/* ---- Accesso ---- */
.loginwrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(1200px 500px at 50% -10%, var(--brand-soft), transparent), var(--bg); }
.login { width: min(400px, 100%); margin: 0; padding: 28px; }
.login .logo { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.login .logo svg { width: 30px; height: 30px; color: var(--brand); }
.login form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.login button.primary { padding: 10px; }
.pw { position: relative; display: flex; }
.pw input { flex: 1; padding-right: 64px; }
.pw button { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); padding: 4px 9px; font-size: 12px; }

/* ---- Gestione dispositivo ---- */
.fieldgrid { display: grid; gap: 14px 16px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); align-items: start; }
.fieldgrid label input, .fieldgrid label select { width: 100%; }
.section { border-top: 1px solid var(--line); padding-top: 16px; margin-top: 16px; }
.section:first-of-type { border-top: 0; padding-top: 0; margin-top: 0; }
.spacer { flex: 1; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: transparent; color: var(--muted); padding: 9px 14px; }
.tabs button:hover { background: transparent; color: var(--ink); }
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }
.skel { background: linear-gradient(90deg, var(--off-bg) 25%, var(--card2) 50%, var(--off-bg) 75%); background-size: 200% 100%; animation: sh 1.2s infinite; border-radius: 8px; height: 18px; margin: 8px 0; }
@keyframes sh { to { background-position: -200% 0; } }

/* ---- Mobile: le tabelle diventano schede ---- */
@media (max-width: 720px) {
  main { padding: 14px 12px 50px; }
  h1 { font-size: 20px; }
  header.top { padding: 0 12px; }
  header.top .who .uname { display: none; }
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack tr { padding: 12px 14px; border-bottom: 1px solid var(--line); }
  table.stack td { border: 0; padding: 3px 0; white-space: normal; display: flex; gap: 10px; justify-content: space-between; align-items: center; }
  table.stack td::before { content: attr(data-l); color: var(--muted); font-size: 12px; flex: none; }
  table.stack td.first { font-size: 16px; justify-content: flex-start; padding-bottom: 6px; }
  table.stack td.first::before { display: none; }
  tbody tr:hover td { background: transparent; }
  .map { height: 300px; }
  .search { flex: 1 1 100%; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---- Grafico e segnaposto ---- */
svg.chart .gl { stroke: var(--line); }
svg.chart .tx { fill: var(--muted); font-size: 11px; }
svg.chart .ln { fill: none; stroke: var(--brand); stroke-width: 2; stroke-linejoin: round; }
svg.chart .ar { fill: var(--brand); opacity: .1; }
svg.chart .cur { fill: var(--brand); stroke: var(--card); stroke-width: 2; }
.pin { position: relative; }
[hidden] { display: none !important; }
label.check{padding-top:20px}
@media (max-width:720px){.pagehead .grow{flex-basis:100%}}

/* ---- Configurazione dispositivo ---- */
.cfgsec { display: grid; grid-template-columns: minmax(180px, 280px) 1fr; gap: 12px 32px; padding: 20px 0; border-top: 1px solid var(--line); }
.cfgsec:first-of-type { border-top: 0; padding-top: 4px; }
.cfgintro h3 { margin: 0 0 4px; font-size: 15px; }
.cfgintro p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.45; }
.cfgfields { display: grid; gap: 18px 20px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); align-items: start; }
.fld { display: flex; flex-direction: column; gap: 6px; color: var(--ink); }
.fld .fl { font-size: 13px; font-weight: 600; }
.fld small { color: var(--muted); font-size: 12px; font-weight: 400; }
.inwrap { position: relative; display: flex; }
.inwrap input { width: 100%; padding-right: 70px; }
.inwrap .unit { position: absolute; right: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 12px; pointer-events: none; }
.fld.dis { opacity: .5; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button { padding: 4px 11px; font-size: 12px; border-radius: 99px; }
.chips button.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); font-weight: 600; }
.switch { display: flex; align-items: flex-start; gap: 12px; cursor: pointer; grid-column: 1 / -1; color: var(--ink); }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { flex: none; width: 40px; height: 22px; border-radius: 99px; background: var(--off-bg); border: 1px solid var(--line); position: relative; transition: background .15s; margin-top: 1px; }
.switch .track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3); transition: transform .15s; }
.switch input:checked + .track { background: var(--brand); border-color: var(--brand); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--brand); outline-offset: 2px; }
.switch strong { display: block; font-size: 13px; }
.switch small { display: block; color: var(--muted); font-size: 12px; }
.cfgbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; border-top: 1px solid var(--line); padding-top: 16px; margin-top: 4px; }
@media (max-width: 720px) { .cfgsec { grid-template-columns: 1fr; } }
.switch { flex-direction: row; }
.fld .seg { align-self: flex-start; }
.row:has(> .search) { align-items: center; }
.search { min-width: 280px; }
.search input { height: 40px; }
.row > .seg { height: 40px; align-items: center; }
.wrapcol td:last-child { white-space: normal; overflow-wrap: anywhere; min-width: 240px; font-size: 13px; }
.wrapcol td { vertical-align: top; }
@media (max-width: 720px) { .wrapcol td:last-child { min-width: 0; } }

/* ---- Procedura guidata ---- */
.steps { list-style: none; display: flex; gap: 8px; padding: 0; margin: 0 0 16px; flex-wrap: wrap; }
.steps li { display: flex; align-items: center; gap: 8px; padding: 6px 14px 6px 8px; border-radius: 99px; background: var(--off-bg); color: var(--muted); font-weight: 500; font-size: 14px; }
.steps .n { width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; background: var(--card); font-size: 12px; font-weight: 700; }
.steps li.on { background: var(--brand-soft); color: var(--brand); }
.steps li.on .n { background: var(--brand); color: var(--brand-ink); }
.steps li.done { background: var(--ok-bg); color: var(--ok); }
.steps li.done .n { background: var(--ok); color: #fff; }
.how { margin: 0 0 14px; padding-left: 20px; color: var(--muted); }
.how li { margin: 4px 0; }
.opts { display: flex; flex-wrap: wrap; gap: 10px; }
.opt { flex-direction: row; align-items: flex-start; gap: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; cursor: pointer; color: var(--ink); min-width: 220px; }
.opt:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }
.opt strong { display: block; font-size: 14px; }
.opt small { display: block; color: var(--muted); font-size: 12px; }
.opt input { margin-top: 3px; }
.checklist { list-style: none; padding: 0; margin: 0 0 14px; }
.checklist li { display: flex; align-items: center; gap: 10px; padding: 7px 0; color: var(--muted); }
.checklist li small { color: var(--muted); }
.checklist .ck { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line); flex: none; }
.checklist li.doing { color: var(--ink); font-weight: 600; }
.checklist li.doing .ck { border-color: var(--brand); border-top-color: transparent; animation: spin .8s linear infinite; }
.checklist li.ok { color: var(--ink); }
.checklist li.ok .ck { background: var(--ok); border-color: var(--ok); box-shadow: inset 0 0 0 3px var(--card); }
.checklist li.bad { color: var(--bad); }
.checklist li.bad .ck { background: var(--bad); border-color: var(--bad); box-shadow: inset 0 0 0 3px var(--card); }
@keyframes spin { to { transform: rotate(360deg); } }
a.btn { text-decoration: none; padding: 8px 14px; border-radius: 9px; border: 1px solid var(--line); display: inline-flex; align-items: center; color: var(--ink); }
a.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.prog { height: 8px; background: var(--off-bg); border-radius: 4px; overflow: hidden; margin: 6px 0 4px; }
.prog > i { display: block; height: 100%; background: var(--brand); transition: width .2s; }

/* Elenco con caselle (eliminazione multipla) */
.picklist{display:flex;flex-direction:column;gap:4px;max-height:46vh;overflow:auto;margin:10px 0;padding:2px}
.pick{display:flex;flex-direction:row;align-items:center;gap:10px;padding:8px 10px;border:1px solid var(--line);border-radius:8px;cursor:pointer;font-weight:400}
.pick input{width:auto;flex:none;margin:0}
.pick small{color:var(--muted);margin-left:2px}

/* Altezza uniforme: campi e pulsanti allineati ovunque (intestazioni, schede, moduli) */
input:not([type=checkbox]):not([type=radio]):not([type=range]), select { height: 40px; box-sizing: border-box; }
button:not(.sm):not(.ghost):not(.seg button):not(.tabs button):not(.chips button):not(.pw button), .btn { min-height: 40px; box-sizing: border-box; }
.pagehead button, .pagehead .btn, .cardhead button.sm, .row.tight > button.sm { min-height: 40px; box-sizing: border-box; font-size: 14px; }
.pagehead button.ghost { padding: 6px 12px; }
.cardhead .row.tight, .pagehead { align-items: center; }
form.inline { align-items: flex-end; }
