:root {
  --bg: #f3f6f7;
  --card: #ffffff;
  --text: #17242a;
  --muted: #5d6f77;
  --line: #dde5e8;
  --primary: #0f4c5c;
  --primary-2: #16697a;
  --primary-soft: #e3f0f2;
  --red: #b42318; --red-soft: #fdeceb;
  --orange: #b54708; --orange-soft: #fef3e6;
  --yellow: #8a6100; --yellow-soft: #fff7d6;
  --green: #127a4a; --green-soft: #e5f6ee;
  --grey-soft: #eef1f2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 40, 50, .06), 0 2px 8px rgba(16, 40, 50, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1517; --card: #162125; --text: #e5eef0; --muted: #93a7ae; --line: #26363b;
    --primary: #5fb7c7; --primary-2: #7fcad8; --primary-soft: #183238;
    --red: #ff8a80; --red-soft: #3a1d1b; --orange: #ffb267; --orange-soft: #3a2a17;
    --yellow: #f2d06b; --yellow-soft: #352f15; --green: #6fd3a0; --green-soft: #15302a; --grey-soft: #1f2b2f;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1517; --card: #162125; --text: #e5eef0; --muted: #93a7ae; --line: #26363b;
  --primary: #5fb7c7; --primary-2: #7fcad8; --primary-soft: #183238;
  --red: #ff8a80; --red-soft: #3a1d1b; --orange: #ffb267; --orange-soft: #3a2a17;
  --yellow: #f2d06b; --yellow-soft: #352f15; --green: #6fd3a0; --green-soft: #15302a; --grey-soft: #1f2b2f;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
a { color: var(--primary); }
h1 { font-size: 1.35rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1rem; margin: 1.1rem 0 .6rem; color: var(--primary); letter-spacing: .01em; }
.scheda > h2:first-child, .sez-titolo h2 { margin-top: 0; }
[hidden] { display: none !important; }

/* ---- Barra superiore / navigazione */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--primary); color: #fff;
}
:root[data-theme="dark"] .topbar { background: #11353d; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .topbar { background: #11353d; } }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 650; font-size: 1.05rem; }
.brand img { border-radius: 7px; }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav a { display: flex; align-items: center; gap: 6px; padding: 8px 12px; border-radius: 8px; color: rgba(255,255,255,.85); text-decoration: none; font-size: .92rem; }
.nav a:hover { background: rgba(255,255,255,.1); }
.nav a.attivo { background: rgba(255,255,255,.18); color: #fff; }
.nav svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

@media (max-width: 760px) {
  .topbar { padding: 8px 16px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; margin: 0; z-index: 30;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); justify-content: space-around; gap: 0;
  }
  .nav a { flex-direction: column; gap: 2px; padding: 4px 6px; color: var(--muted); font-size: .7rem; min-width: 56px; }
  .nav a.attivo { background: none; color: var(--primary); }
  .nav a:hover { background: none; }
  .nav a.nav-new svg { background: var(--primary); color: #fff; border-radius: 50%; padding: 3px; width: 28px; height: 28px; }
}
@media (min-width: 761px) { body { padding-bottom: 24px; } }

.offline-banner { background: var(--orange-soft); color: var(--orange); padding: 8px 16px; text-align: center; font-size: .9rem; }

/* ---- Contenuto */
.view { max-width: 960px; margin: 0 auto; padding: 16px; }
.intestazione { display: flex; align-items: center; gap: 10px; margin: 4px 0 12px; flex-wrap: wrap; }
.intestazione h1 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.intestazione .azioni { display: flex; gap: 8px; }
.indietro { font-size: 1.9rem; line-height: 1; text-decoration: none; padding: 0 6px 4px 0; color: var(--primary); }
.sottotitolo { margin: -6px 0 12px; color: var(--muted); }
.scheda { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.scheda.stretta { max-width: 460px; }
.sez-titolo { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.loading, .vuoto { color: var(--muted); text-align: center; padding: 28px 8px; }
.vuoto.piccolo { padding: 8px 0; text-align: left; }
.muto { color: var(--muted); }
.piccolo { font-size: .85rem; }
.meta { color: var(--muted); font-size: .78rem; text-align: center; margin: 18px 0; }
.nota { color: var(--muted); font-size: .85rem; }
.avviso { background: var(--yellow-soft); color: var(--yellow); border-radius: 8px; padding: 10px 12px; margin: 10px 0; font-size: .92rem; }
.prossimo { margin: 12px 0 0; padding-top: 10px; border-top: 1px solid var(--line); }

/* ---- Liste */
.lista { display: flex; flex-direction: column; }
.riga { display: flex; align-items: flex-start; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none; }
.riga:last-child { border-bottom: 0; }
a.riga:hover { background: var(--primary-soft); border-radius: 8px; }
.riga-data { min-width: 86px; font-weight: 600; font-variant-numeric: tabular-nums; font-size: .9rem; }
.riga-data small { display: block; font-weight: 400; color: var(--muted); font-size: .75rem; }
.riga-data.rosso { color: var(--red); }
.riga-corpo { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.riga-azioni { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-weight: 700; flex: none; }
.tel { font-size: 1.3rem; padding: 4px 8px; border-radius: 8px; background: var(--green-soft); color: var(--green); cursor: pointer; }

.tag { display: inline-block; font-size: .72rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--grey-soft); color: var(--muted); vertical-align: 1px; white-space: nowrap; }
.tag.rosso { background: var(--red-soft); color: var(--red); }
.tag.arancio { background: var(--orange-soft); color: var(--orange); }
.tag.giallo { background: var(--yellow-soft); color: var(--yellow); }
.tag.verde { background: var(--green-soft); color: var(--green); }
.tag.blu { background: var(--primary-soft); color: var(--primary); }
.tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.chips-fu { display: flex; gap: 4px; flex-wrap: wrap; margin: 4px 0; }

.contatori { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.contatore { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.contatori.piccoli { margin: 0; }
.contatori.piccoli .contatore { background: var(--bg); }
.contatore b { display: block; font-size: 1.45rem; line-height: 1.2; overflow-wrap: anywhere; }
.contatori.piccoli .contatore b { font-size: 1.05rem; }
.contatore span { color: var(--muted); font-size: .82rem; }
.contatore.rosso b { color: var(--red); } .contatore.arancio b { color: var(--orange); } .contatore.verde b { color: var(--green); }

.dl { margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px 18px; }
.dl div { min-width: 0; }
.dl dt { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.dl dd { margin: 1px 0 0; overflow-wrap: anywhere; white-space: pre-line; }

/* ---- Moduli */
.griglia { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px 14px; }
.campo { display: flex; flex-direction: column; gap: 4px; min-width: 0; margin: 0; padding: 0; border: 0; }
.campo.full { grid-column: 1 / -1; }
.campo > label, .campo > legend { font-size: .82rem; color: var(--muted); font-weight: 550; padding: 0; }
.obbl { color: var(--red); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--card); width: 100%;
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; min-height: 42px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-2); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
.con-unita { display: flex; align-items: center; gap: 6px; }
.con-unita span { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.campo.check { flex-direction: row; align-items: center; gap: 8px; align-self: end; min-height: 42px; }
.campo.check input, label.check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--primary); flex: none; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip span { display: inline-block; padding: 6px 11px; border-radius: 99px; border: 1px solid var(--line); font-size: .86rem; cursor: pointer; user-select: none; background: var(--card); }
.chip input:checked + span { background: var(--primary); border-color: var(--primary); color: #fff; }
:root[data-theme="dark"] .chip input:checked + span { color: #062228; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .chip input:checked + span { color: #062228; } }
.chip input:focus-visible + span { outline: 2px solid var(--primary-2); outline-offset: 2px; }
.sottoform { background: var(--bg); border-radius: 10px; padding: 12px; margin: 8px 0 12px; }
details.aggiungi { margin-top: 12px; }
details summary { cursor: pointer; color: var(--primary); font-weight: 600; padding: 6px 0; }

.lista-fu { display: flex; flex-direction: column; gap: 6px; }
.fu-riga { display: grid; grid-template-columns: 24px 74px minmax(140px, 170px) 1fr; gap: 8px; align-items: center; }
.fu-riga input[type=checkbox] { width: 20px; height: 20px; min-height: 0; accent-color: var(--primary); }
.fu-et { font-weight: 600; font-size: .9rem; }
@media (max-width: 560px) { .fu-riga { grid-template-columns: 24px 1fr 1fr; } .fu-riga input[type=text] { grid-column: 2 / -1; } }

.filtri { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: end; margin-bottom: 12px; }
.filtri label { display: flex; flex-direction: column; gap: 3px; font-size: .82rem; color: var(--muted); }
.filtri label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: .92rem; min-height: 42px; }
.filtri select, .filtri input[type=date] { width: auto; min-width: 150px; }
.cerca { margin-bottom: 12px; }
.cerca input { font-size: 1rem; padding: 12px 14px; }

/* ---- Bottoni */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600; font-size: .92rem;
  padding: 9px 14px; min-height: 40px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--text);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--primary-2); }
.btn:disabled { opacity: .6; cursor: default; }
.btn.primario { background: var(--primary); border-color: var(--primary); color: #fff; }
:root[data-theme="dark"] .btn.primario { color: #062228; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn.primario { color: #062228; } }
.btn.pericolo { color: var(--red); }
.btn.piccolo { padding: 5px 10px; min-height: 32px; font-size: .82rem; }
.btn.largo { width: 100%; }
.btn.link { border: 0; background: none; color: var(--primary); font-weight: 500; }
.barra-azioni { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

/* ---- Login */
.login { max-width: 400px; margin: 6vh auto 0; text-align: center; }
.login img { border-radius: 18px; }
.login h1 { margin: 12px 0 4px; }
.login .sotto { color: var(--muted); margin: 0 0 18px; }
.login form { text-align: left; display: flex; flex-direction: column; gap: 12px; }

/* ---- Tabelle */
.tabella-wrap { overflow-x: auto; margin-top: 12px; }
.tabella { width: 100%; border-collapse: collapse; font-size: .85rem; }
.tabella th, .tabella td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }

/* ---- Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 50;
  background: #1d2b30; color: #fff; padding: 11px 16px; border-radius: 10px; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.2); font-size: .92rem;
}
.toast.errore { background: #8f1d14; }
.toast.ok { background: #0f5c3a; }
@media (min-width: 761px) { .toast { bottom: 24px; } }

/* ---- Stampa */
@media print {
  .topbar, .nav, .barra-azioni, .azioni, .indietro, .btn, .riga-azioni, .toast, .offline-banner, details summary { display: none !important; }
  body { background: #fff; color: #000; padding: 0; font-size: 11pt; }
  .view { max-width: none; padding: 0; }
  .scheda { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
  a { color: #000; text-decoration: none; }
}
