:root {
  --navy: #14213D;
  --navy-2: #1F3157;
  --amber: #F5A524;
  --amber-d: #C47F0A;
  --bg: #F4F5F7;
  --card: #FFFFFF;
  --ink: #111827;
  --ink-2: #4B5563;
  --line: #E2E5EA;
  --red: #DC2626;
  --green: #16A34A;
  --blue: #2563EB;
  --r: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* ---------- testata ---------- */
.top { position: fixed; inset: 0 0 auto 0; z-index: 1000; background: var(--navy); color: #fff; padding-top: var(--safe-t); box-shadow: 0 2px 8px rgba(0,0,0,.25); }
.bar { display: flex; align-items: center; gap: 4px; height: 54px; padding-inline: 6px; }
.ico { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 10px; flex: none; }
.ico:active { background: rgba(255,255,255,.12); }
.categoria { flex: 1; min-width: 0; position: relative; display: flex; align-items: center; height: 42px; border-radius: 10px; background: rgba(255,255,255,.1); padding-left: 12px; }
.categoria i { width: 12px; height: 12px; border-radius: 50%; border: 2px solid #fff; flex: none; }
.categoria select { flex: 1; min-width: 0; height: 100%; appearance: none; -webkit-appearance: none; background: transparent; border: 0; color: #fff; font: inherit; font-size: 17px; font-weight: 650; padding: 0 34px 0 10px; }
.categoria select option { color: var(--ink); }
.categoria svg { position: absolute; right: 8px; width: 18px; height: 18px; pointer-events: none; opacity: .8; }
.azioni { display: grid; grid-template-columns: repeat(5, 1fr); background: #fff; color: var(--navy); border-bottom: 1px solid var(--line); }
.az { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; min-height: 62px; font-size: 12.5px; font-weight: 600; border-right: 1px solid var(--line); }
.az:last-child { border-right: 0; }
.az svg { width: 24px; height: 24px; }
.az.on { background: var(--navy-2); color: #fff; }
.az.rilievo { background: var(--amber); color: var(--navy); }
.az.rilievo svg { stroke-width: 2.4; }
.az:active { filter: brightness(.9); }
.az .sub { font-weight: 500; font-size: 11px; opacity: .8; margin-top: -3px; }

#map { position: fixed; top: calc(117px + var(--safe-t)); left: 0; right: 0; bottom: calc(52px + var(--safe-b)); background: #DDE3EA; }
.leaflet-control-attribution { font-size: 10px; }
.etichetta { background: rgba(255,255,255,.9); border: 0; box-shadow: none; padding: 0 4px; font-size: 11px; font-weight: 700; color: var(--navy); border-radius: 4px; }
.etichetta::before { display: none; }

/* ---------- pulsanti laterali ---------- */
.laterali { position: fixed; right: 10px; bottom: calc(66px + var(--safe-b)); z-index: 900; display: flex; flex-direction: column; gap: 8px; }
.lat { width: 50px; height: 50px; border-radius: 12px; background: var(--card); color: var(--navy); display: grid; place-items: center; box-shadow: 0 2px 8px rgba(0,0,0,.22); }
.lat svg { width: 24px; height: 24px; }
.lat.attivo { background: var(--blue); color: #fff; }
.lat:active { filter: brightness(.92); }

/* ---------- legenda ---------- */
.legenda {
  position: fixed; left: 10px; bottom: calc(66px + var(--safe-b)); z-index: 900; max-width: calc(100% - 84px);
  background: var(--card); border-radius: var(--r); box-shadow: 0 2px 10px rgba(0,0,0,.18); padding: 8px 12px;
  font-size: 13px; display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center;
}
.legenda .v { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.legenda .dot { width: 11px; height: 11px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.25); flex: none; }
.legenda .dot.inc { border-color: var(--red); box-shadow: none; }
.legenda b { font-variant-numeric: tabular-nums; }
.legenda .titolo { width: 100%; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); }

/* ---------- piè di pagina coordinate ---------- */
.coord { position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000; min-height: calc(52px + var(--safe-b)); padding: 4px 10px calc(4px + var(--safe-b)); background: #fff; border-top: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.c-val { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; font-variant-numeric: tabular-nums; font-size: 14px; }
.c-val small { color: var(--ink-2); font-size: 11px; font-weight: 600; }
.c-val b { font-weight: 650; }
.c-val .ok { color: var(--green); } .c-val .med { color: var(--amber-d); } .c-val .ko { color: var(--red); }
.stato { display: flex; align-items: center; gap: 6px; font-size: 12px; padding: 6px 10px; border-radius: 99px; background: var(--bg); white-space: nowrap; color: var(--ink-2); }
.stato i { width: 8px; height: 8px; border-radius: 50%; background: #9CA3AF; }
.stato.on i { background: #22C55E; }
.stato.off i { background: var(--amber); }

/* ---------- pin centrale ---------- */
.pin { position: fixed; z-index: 950; left: 50%; top: calc((117px + var(--safe-t) + 100vh - 52px - var(--safe-b)) / 2); transform: translate(-50%, -100%); pointer-events: none; }
.pin svg { width: 34px; height: 46px; filter: drop-shadow(0 2px 2px rgba(0,0,0,.35)); }
.posiziona {
  position: fixed; z-index: 1001; left: 8px; right: 8px; bottom: calc(60px + var(--safe-b));
  background: var(--card); border-radius: 16px; box-shadow: 0 6px 24px rgba(0,0,0,.3); padding: 12px;
}
.pos-info { display: flex; flex-direction: column; gap: 2px; margin-bottom: 10px; }
.pos-info span { font-size: 13.5px; color: var(--ink-2); }
.pos-btn { display: flex; gap: 8px; }
.pos-btn .btn { flex: 1; }

/* ---------- bottoni ---------- */
.btn { min-height: 48px; padding: 0 16px; border-radius: 10px; font-weight: 600; font-size: 15.5px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn.primary { background: var(--navy); color: #fff; }
.btn.accent { background: var(--amber); color: var(--navy); }
.btn.ghost { background: #EEF0F3; color: var(--ink); }
.btn.danger { background: #FDECEC; color: var(--red); }
.btn:active { filter: brightness(.92); }
.btn svg { width: 20px; height: 20px; }

/* ---------- sheet ---------- */
.sheet-bg { position: fixed; inset: 0; z-index: 1100; background: rgba(17,24,39,.35); }
.sheet {
  position: fixed; z-index: 1101; left: 0; right: 0; bottom: 0; max-height: 85vh; overflow: auto;
  background: var(--card); border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + var(--safe-b)); box-shadow: 0 -6px 24px rgba(0,0,0,.2);
  animation: su .18s ease-out;
}
@keyframes su { from { transform: translateY(30px); opacity: .5; } }
.sheet::before { content: ""; display: block; width: 40px; height: 4px; border-radius: 4px; background: #D1D5DB; margin: 0 auto 10px; }
.sheet h3 { margin: 4px 0 12px; font-size: 18px; }
.sheet .scelte { display: grid; gap: 10px; }
.scelta { display: flex; align-items: center; gap: 14px; padding: 14px; border-radius: var(--r); background: var(--bg); text-align: left; font-size: 16px; font-weight: 600; }
.scelta .dot { width: 18px; height: 18px; border-radius: 50%; flex: none; border: 3px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.2); }
.scelta small { display: block; font-weight: 400; color: var(--ink-2); font-size: 13px; margin-top: 2px; }
.scelta:active { background: #E5E7EB; }
.anteprima .riga { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 14.5px; }
.anteprima .riga span:first-child { color: var(--ink-2); }
.anteprima .riga span:last-child { text-align: right; font-weight: 550; }
.anteprima .azioni { display: flex; gap: 8px; margin-top: 14px; }
.anteprima .azioni .btn { flex: 1; }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 99px; font-size: 12.5px; font-weight: 650; }
.badge.ok { background: #DCFCE7; color: #166534; }
.badge.ko { background: #FEE2E2; color: #991B1B; }
.badge.cat { color: #fff; }

/* ---------- scheda ---------- */
.scheda { position: fixed; inset: 0; z-index: 1200; background: var(--bg); display: flex; flex-direction: column; animation: dx .16s ease-out; }
@keyframes dx { from { transform: translateX(24px); opacity: .6; } }
.sch-top { background: var(--navy); color: #fff; padding: var(--safe-t) 6px 0; flex: none; }
.sch-head { display: flex; align-items: center; gap: 4px; min-height: 56px; }
.sch-head .tit { flex: 1; min-width: 0; }
.sch-head .tit b { display: block; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sch-head .tit small { display: block; font-size: 12.5px; opacity: .75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs { display: flex; overflow-x: auto; scrollbar-width: none; gap: 2px; padding: 0 4px; }
.tabs::-webkit-scrollbar { display: none; }
.tab { flex: none; padding: 12px 14px 11px; font-size: 14.5px; font-weight: 600; color: rgba(255,255,255,.72); border-bottom: 3px solid transparent; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.tab.on { color: #fff; border-bottom-color: var(--amber); }
.tab .n { min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--red); color: #fff; font-size: 11px; display: grid; place-items: center; }
.sch-body { flex: 1; overflow-y: auto; padding: 14px 14px 110px; -webkit-overflow-scrolling: touch; }
.sch-foot { position: fixed; left: 0; right: 0; bottom: 0; z-index: 2; padding: 10px 14px calc(10px + var(--safe-b)); background: linear-gradient(transparent, var(--bg) 30%); display: flex; gap: 10px; }
.sch-foot .btn { flex: 1; box-shadow: 0 2px 8px rgba(0,0,0,.15); }

.campo { background: var(--card); border-radius: var(--r); padding: 12px 12px 12px; margin-bottom: 10px; border: 1.5px solid transparent; }
.campo.manca { border-color: var(--red); }
.campo > label { display: block; font-size: 12.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .02em; color: var(--ink-2); margin-bottom: 8px; line-height: 1.3; }
.campo > label .req { color: var(--red); margin-left: 3px; }
.campo > label .u { text-transform: none; font-weight: 500; }
.campo input[type=text], .campo input[type=date], .campo select, .campo textarea {
  width: 100%; min-height: 48px; border: 1.5px solid var(--line); border-radius: 10px; padding: 10px 12px; font: inherit; font-size: 16px; background: #fff; color: var(--ink);
}
.campo textarea { min-height: 72px; resize: vertical; }
.campo input:focus, .campo select:focus, .campo textarea:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,.15); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { min-height: 42px; padding: 8px 14px; border-radius: 10px; background: #EEF0F3; font-size: 15px; font-weight: 550; text-align: left; line-height: 1.25; }
.chip.on { background: var(--navy); color: #fff; }
.chip.altro { background: transparent; border: 1.5px dashed #C3C8D0; color: var(--ink-2); }
.chips.mini { margin-top: 8px; gap: 6px; }
.chips.mini .chip { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.campo .valore-altro { margin-top: 8px; }
.seg { display: inline-flex; background: #EEF0F3; border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { min-width: 78px; min-height: 42px; border-radius: 8px; font-weight: 600; }
.seg button.on { background: var(--navy); color: #fff; }

.foto-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.foto-grid .th { aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: #E5E7EB; position: relative; }
.foto-grid .th img { width: 100%; height: 100%; object-fit: cover; display: block; }
.foto-grid .th.link { display: grid; place-items: center; font-size: 12px; color: var(--ink-2); padding: 6px; text-align: center; }
.foto-add { aspect-ratio: 1; border-radius: 10px; border: 2px dashed #C3C8D0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; color: var(--ink-2); font-size: 13px; font-weight: 600; }
.foto-add svg { width: 28px; height: 28px; }

.lamp { background: var(--card); border-radius: var(--r); margin-bottom: 12px; overflow: hidden; border: 1.5px solid var(--line); }
.lamp.manca { border-color: var(--red); }
.lamp-h { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 14px; background: #EAF0FB; }
.lamp-h .t { flex: 1; min-width: 0; text-align: left; }
.lamp-h .t b { display: block; font-size: 15.5px; }
.lamp-h .t small { display: block; color: var(--ink-2); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lamp-h .ico { color: var(--navy); width: 42px; height: 42px; }
.lamp-h .ico.del { color: var(--red); }
.lamp-b { padding: 10px 10px 2px; background: var(--bg); }
.lamp-b .campo { margin-bottom: 8px; }
.add-lamp { width: 100%; }

.avviso { background: #FEF3C7; color: #78350F; border-radius: var(--r); padding: 10px 12px; font-size: 14px; margin-bottom: 12px; line-height: 1.4; }
.avviso.rosso { background: #FEE2E2; color: #7F1D1D; }
.avviso a, .avviso button.lnk { color: inherit; font-weight: 700; text-decoration: underline; padding: 0; }

/* ---------- drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 1300; display: flex; }
.drawer .pan { width: min(340px, 88vw); height: 100%; background: var(--card); overflow-y: auto; padding: calc(14px + var(--safe-t)) 0 calc(20px + var(--safe-b)); box-shadow: 4px 0 24px rgba(0,0,0,.25); animation: sx .18s ease-out; }
@keyframes sx { from { transform: translateX(-30px); opacity: .5; } }
.drawer .fuori { flex: 1; background: rgba(17,24,39,.35); }
.drawer h4 { margin: 18px 18px 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); }
.drawer .brand { padding: 4px 18px 10px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line); }
.drawer .brand img { width: 40px; height: 40px; border-radius: 10px; }
.drawer .brand b { font-size: 17px; display: block; }
.drawer .brand small { color: var(--ink-2); }
.voce { width: 100%; display: flex; align-items: center; gap: 14px; padding: 13px 18px; text-align: left; font-size: 15.5px; }
.voce:active { background: var(--bg); }
.voce svg { color: var(--navy); flex: none; }
.voce small { display: block; color: var(--ink-2); font-size: 12.5px; }
.drawer .info { margin: 6px 18px; font-size: 13px; color: var(--ink-2); line-height: 1.5; }

/* ---------- form generici nei sheet ---------- */
.f-row { margin-bottom: 12px; }
.f-row label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }
.f-row input, .f-row select { width: 100%; min-height: 48px; border: 1.5px solid var(--line); border-radius: 10px; padding: 10px 12px; font: inherit; font-size: 16px; background: #fff; }
.check { display: flex; align-items: center; gap: 10px; padding: 10px 0; font-size: 15px; }
.check input { width: 22px; height: 22px; accent-color: var(--navy); }
.lista-comuni .scelta.on { outline: 2px solid var(--navy); }

/* ---------- foto a schermo intero ---------- */
.viewer { position: fixed; inset: 0; z-index: 1400; background: #000; display: flex; flex-direction: column; }
.viewer img { flex: 1; min-height: 0; object-fit: contain; width: 100%; }
.viewer .vb { display: flex; gap: 10px; padding: 10px 14px calc(10px + var(--safe-b)); }
.viewer .vb .btn { flex: 1; }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(66px + var(--safe-b)); z-index: 1500; background: var(--ink); color: #fff; padding: 11px 16px; border-radius: 10px; font-size: 14.5px; box-shadow: 0 4px 16px rgba(0,0,0,.3); max-width: calc(100% - 32px); }
.toast.err { background: var(--red); }

/* gps */
.gps-dot { width: 18px; height: 18px; border-radius: 50%; background: var(--blue); border: 3px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.2), 0 0 10px rgba(37,99,235,.6); }

@media (min-width: 900px) {
  .scheda { left: auto; width: 520px; box-shadow: -6px 0 24px rgba(0,0,0,.2); }
  .sch-foot { left: auto; width: 520px; }
  .sheet { left: 50%; right: auto; width: 480px; transform: translateX(-50%); }
  @keyframes su { from { opacity: .5; } }
}

.risultati .scelta { padding: 10px 12px; font-weight: 550; }
.risultati .scelta small { font-weight: 400; }
.seg.largo { display: flex; } .seg.largo button { flex: 1; min-width: 0; }
.scheda:not([hidden]) ~ .toast { bottom: calc(96px + var(--safe-b)); }
.sheet:not([hidden]) ~ .toast, .drawer:not([hidden]) ~ .toast { bottom: auto; top: calc(126px + var(--safe-t)); }
.sheet input[type=search] { width: 100%; min-height: 50px; border: 1.5px solid var(--line); border-radius: 10px; padding: 10px 14px; font: inherit; font-size: 16px; }
.sheet input[type=search]:focus { outline: none; border-color: var(--blue); }
