:root {
    --primary: #0f766e;
    --primary-dark: #115e59;
    --primary-soft: #ccfbf1;
    --bg: #f4f6f8;
    --card: #ffffff;
    --text: #1f2933;
    --muted: #6b7785;
    --border: #dfe3e8;
    --ok: #15803d;
    --ok-soft: #dcfce7;
    --warn: #b45309;
    --warn-soft: #fef3c7;
    --danger: #b91c1c;
    --danger-soft: #fee2e2;
    --gas: #ea580c;
    --el: #ca8a04;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
    --bottomnav-h: 64px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1419;
        --card: #182028;
        --text: #e6ebf0;
        --muted: #93a1b0;
        --border: #2a3542;
        --primary: #14b8a6;
        --primary-dark: #0d9488;
        --primary-soft: #0f3d3a;
        --ok-soft: #12351f;
        --warn-soft: #3d2c0b;
        --danger-soft: #3f1515;
        --ok: #4ade80;
        --warn: #fbbf24;
        --danger: #f87171;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.45;
    background: var(--bg);
    color: var(--text);
    padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom) + 16px);
}
h1, h2 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.05rem; }
p { margin: .25rem 0; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.grow { flex: 1; min-width: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
[hidden] { display: none !important; }

.icon { width: 20px; height: 20px; flex: none; vertical-align: middle; }
.icon-sm { width: 15px; height: 15px; }

/* ---------- Barra superiore / navigazione ---------- */
.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 12px;
    padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
    background: var(--primary); color: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font-weight: 700; font-size: 1.05rem; }
.brand-icon { width: 24px; height: 24px; }
.topnav { display: none; margin-left: 24px; gap: 4px; flex: 1; }
.topnav a { display: flex; align-items: center; gap: 6px; padding: 8px 12px; border-radius: 10px; color: #e0f2f1; text-decoration: none; font-weight: 500; }
.topnav a:hover { background: rgba(255,255,255,.12); }
.topnav a.active { background: rgba(255,255,255,.2); color: #fff; }
.logout-form { margin-left: auto; }
.topbar .icon-btn { color: #fff; }
.topbar .icon-btn:hover { background: rgba(255,255,255,.15); }

.bottomnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    display: grid; grid-template-columns: repeat(4, 1fr);
    height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--card); border-top: 1px solid var(--border);
}
.bottomnav a {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    font-size: .72rem; color: var(--muted); text-decoration: none; font-weight: 500;
}
.bottomnav a.active { color: var(--primary); }
.bottomnav a .icon { width: 24px; height: 24px; }
.bottomnav a.fab .icon { background: var(--primary); color: #fff; border-radius: 50%; width: 34px; height: 34px; padding: 6px; }

@media (min-width: 900px) {
    .topnav { display: flex; }
    .bottomnav { display: none; }
    body { padding-bottom: 32px; }
}

/* ---------- Layout ---------- */
.container { max-width: 980px; margin: 0 auto; padding: 16px; }
.page-head { display: flex; align-items: center; gap: 10px; margin: 4px 0 16px; flex-wrap: wrap; }
.page-head h1 { flex: 1; }
.page-head .grow h1 { flex: none; }
.section-title { margin: 22px 0 10px; }
.card {
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 12px;
}
.stack > * + * { margin-top: 14px; }
.list { display: flex; flex-direction: column; gap: 10px; }
.empty { text-align: center; padding: 32px 16px; }
.empty .btn { margin-top: 12px; }
.hide-mobile { display: none !important; }
@media (min-width: 900px) {
    .hide-mobile { display: inline-flex !important; }
    .show-mobile { display: none !important; }
}

/* ---------- Pulsanti ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 44px; padding: 10px 16px; border-radius: 12px;
    border: 1px solid var(--border); background: var(--card); color: var(--text);
    font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
    transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--primary); }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-danger-outline { color: var(--danger); border-color: var(--danger-soft); }
.btn-danger-outline:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-block { width: 100%; }
.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent;
    color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--primary-soft); color: var(--primary); }
.link { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 4px; }
.link:hover { text-decoration: underline; }
.actions-bar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.actions-bar form { margin: 0; }
.inline-form { display: flex; gap: 8px; align-items: center; }
.inline-form input[type=date] { min-height: 44px; }

/* ---------- Form ---------- */
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: .85rem; font-weight: 600; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
input[type=text], input[type=password], input[type=search], input[type=date], select, textarea {
    width: 100%; min-height: 46px; padding: 10px 12px;
    border: 1px solid var(--border); border-radius: 10px;
    background: var(--card); color: var(--text); font: inherit; font-size: 16px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
[aria-invalid=true] { border-color: var(--danger); background: var(--danger-soft); }
.error { color: var(--danger); font-weight: 600; }
.hint { font-size: .85rem; color: var(--muted); }
.check { display: flex; gap: 10px; align-items: flex-start; }
.check input { width: 22px; height: 22px; margin: 0; flex: none; accent-color: var(--primary); }
.grid { display: grid; gap: 12px; }
.grid-2, .grid-3 { grid-template-columns: 1fr 1fr; }
.grid-3 > :last-child { grid-column: 1 / -1; }
.grid-name { grid-template-columns: 110px 1fr; }
@media (min-width: 640px) {
    .grid-3 { grid-template-columns: repeat(3, 1fr); }
    .grid-3 > :last-child { grid-column: auto; }
}
.form-section h2 { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.form-section h2 ~ h2 { margin-top: 10px; }
.meter-gas h2 .icon { color: var(--gas); }
.meter-el h2 .icon { color: var(--el); }
.meter-result { margin: 12px 0; padding: 10px 12px; border-radius: 10px; background: var(--bg); font-size: 1.05rem; }
.meter-result.bad strong { color: var(--danger); }

.sticky-total {
    position: sticky; bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom) + 8px); z-index: 10;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0,0,0,.12); padding: 10px 12px 10px 16px;
}
.sticky-total > div { display: flex; flex-direction: column; }
.sticky-total strong { font-size: 1.35rem; }
@media (min-width: 900px) { .sticky-total { bottom: 12px; } }

/* ---------- Alert, badge, statistiche ---------- */
.alert { padding: 12px 14px; border-radius: 12px; margin-bottom: 12px; font-weight: 500; }
.alert-success { background: var(--ok-soft); color: var(--ok); }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-info { background: var(--primary-soft); color: var(--primary-dark); }
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: .75rem; font-weight: 700; white-space: nowrap; }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; box-shadow: var(--shadow); }
.stat-label { display: block; font-size: .75rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat strong { font-size: 1.05rem; white-space: nowrap; }
.stat-warn strong { color: var(--warn); }
@media (min-width: 640px) { .stat strong { font-size: 1.3rem; } }

/* ---------- Filtri ---------- */
.filters { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; padding: 10px; }
.filters .field-search { grid-column: 1 / -1; position: relative; }
.filters .field-search .icon { position: absolute; left: 12px; top: 13px; color: var(--muted); }
.filters .field-search input { padding-left: 40px; }
.filters select:first-of-type { grid-column: 1 / -1; }
@media (min-width: 900px) {
    .filters { grid-template-columns: 2fr 2fr 1fr 1fr; }
    .filters .field-search, .filters select:first-of-type { grid-column: auto; }
}

/* ---------- Elenco bollette ---------- */
.bill-row { display: flex; align-items: stretch; padding: 0; overflow: hidden; margin: 0; }
.bill-main {
    flex: 1; min-width: 0; display: grid; gap: 2px 12px; padding: 12px 14px;
    grid-template-columns: 1fr auto; grid-template-areas: "who total" "period total" "cons cons";
    color: inherit; text-decoration: none;
}
.bill-main:hover { background: var(--bg); }
.bill-who { grid-area: who; display: flex; flex-direction: column; min-width: 0; }
.bill-who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bill-who .muted { font-size: .85rem; }
.bill-period { grid-area: period; font-size: .85rem; }
.bill-cons { grid-area: cons; display: flex; gap: 14px; font-size: .85rem; margin-top: 4px; }
.bill-cons span { display: inline-flex; align-items: center; gap: 4px; }
.bill-total { grid-area: total; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.bill-total strong { font-size: 1.1rem; }
.bill-actions { display: flex; flex-direction: column; justify-content: center; border-left: 1px solid var(--border); padding: 4px; }
.bill-actions form { margin: 0; }
@media (min-width: 900px) {
    .bill-main { grid-template-columns: 1.4fr 1fr 1fr auto; grid-template-areas: "who period cons total"; align-items: center; }
    .bill-cons { margin: 0; }
    .bill-actions { flex-direction: row; align-items: center; }
}
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin: 16px 0; }

/* ---------- Appartamenti ---------- */
.apt-row { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; margin: 0; }
.apt-row:hover { border-color: var(--primary); }
.apt-row.inactive { opacity: .6; }
.apt-icon { width: 44px; height: 44px; border-radius: 12px; background: var(--primary-soft); color: var(--primary); display: flex; align-items: center; justify-content: center; }

.table-wrap { padding: 0; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.table thead th { font-size: .75rem; text-transform: uppercase; color: var(--muted); letter-spacing: .03em; }
.table tr:last-child td { border-bottom: 0; }
.table .r { text-align: right; }
.table-history td:first-child { white-space: normal; }

/* ---------- Anteprima bolletta (replica del modello) ---------- */
.sheet-wrap { overflow-x: auto; background: #fff; color: #000; }
.sheet { max-width: 620px; margin: 0 auto; padding: 6px 0; font-family: Arial, Helvetica, sans-serif; font-size: clamp(8px, 2.75vw, 14px); }
.sheet-name { border: 2px solid #000; text-align: center; font-size: 1.2em; padding: 2px; margin-right: 7%; }
.sheet-sum { display: grid; grid-template-columns: 183fr 98fr 203fr; border: 1.5px solid #000; margin: 1.3em 7% 0 0; }
.sheet-sum > div { padding: 1px 6px; text-align: center; }
.sheet-sum > div + div { border-left: 1.5px solid #000; }
.sheet-label { font-size: .8em; font-weight: 700; margin: 22px 0 4px 4px; }
.sheet-table { border-collapse: collapse; width: 100%; table-layout: fixed; }
.sheet-table td, .sheet-table th { border: 1.5px solid #000; padding: 1px .35em; overflow: hidden; text-align: right; font-weight: 400; white-space: nowrap; }
.sheet-table th { font-weight: 700; text-align: left; }
.sheet-table th.c, .sheet-table td.c { text-align: center; }
.sheet-table tr.head td { font-size: .85em; border-width: 1px; }
.sheet-table td.blank { border: 0; }
.sheet-table td.sc { border: 0; text-align: left; font-size: .8em; padding: 0 0 0 .3em; }
.sheet-sum .eur { display: flex; justify-content: space-between; }
.sheet-table td.eur > span:first-child { float: left; }
.sheet-table td.eur .dash { padding-right: 1.6em; }
.sheet-date { font-weight: 700; font-size: 1.05em; margin: 16px 0 6px 4px; }
.tariffs { font-size: .85rem; }

/* ---------- Login ---------- */
body.guest { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.guest-box { width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--border); border-radius: 20px; padding: 28px 22px; box-shadow: var(--shadow); }
.guest-box h1 { text-align: center; margin-top: 8px; }
.guest-logo { text-align: center; }
.guest-logo img { border-radius: 18px; }
.sheet-table .w1 { width: 22.5%; } .sheet-table .w2 { width: 15%; } .sheet-table .w3 { width: 17.5%; }
.sheet-table .w4 { width: 14.5%; } .sheet-table .w5 { width: 23.5%; } .sheet-table .w6 { width: 7%; }
.grid > * { min-width: 0; }
input[type=date] { min-width: 0; -webkit-appearance: none; appearance: none; }

/* ---------- Bollette appena create ---------- */
.batch-card { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; border-color: var(--primary); background: var(--primary-soft); }
.fixed-items { margin: 0; display: grid; gap: 8px; }
.fixed-items > div { display: flex; justify-content: space-between; gap: 12px; padding: 10px 12px; background: var(--bg); border-radius: 10px; }
.fixed-items dt { color: var(--muted); font-weight: 600; font-size: .9rem; }
.fixed-items dd { margin: 0; font-weight: 700; white-space: nowrap; }

/* ---------- Caricamento massivo ---------- */
.mt { margin-top: 12px; }
.bulk-list { display: grid; gap: 10px; margin-bottom: 12px; }
@media (min-width: 900px) { .bulk-list { grid-template-columns: 1fr 1fr; } }
.bulk-row { margin: 0; }
.bulk-row.has-error { border-color: var(--danger); }
.bulk-head { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; }
.bulk-fixed { text-align: right; white-space: nowrap; }
.warn-text { color: var(--warn); font-weight: 600; }
.bulk-grid { display: grid; grid-template-columns: auto 1fr 1fr; gap: 8px; align-items: center; }
.bulk-grid > * { min-width: 0; }
.bulk-label { display: inline-flex; align-items: center; gap: 4px; font-weight: 600; font-size: .9rem; min-width: 52px; }
.bulk-label .gas { color: var(--gas); }
.bulk-label .el { color: var(--el); }
.bulk-result { margin-top: 8px; }
.bulk-result.ready { color: var(--text); }
.bulk-result.bad { color: var(--danger); font-weight: 600; }

/* ---------- Letturista ---------- */
body.no-bottomnav { padding-bottom: 24px; }
body.no-bottomnav .sticky-total { bottom: calc(12px + env(safe-area-inset-bottom)); }
.topbar-user { margin-left: auto; font-size: .9rem; opacity: .9; }
.topbar-user + .logout-form { margin-left: 0; }
.reader { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.reader summary { display: flex; align-items: center; gap: 10px; cursor: pointer; min-height: 32px; }
.reader[open] summary { margin-bottom: 12px; }
.new-reader { border-top: 1px solid var(--border); padding-top: 14px; }
.new-reader h3 { margin: 0; font-size: 1rem; }
.sticky-total strong { white-space: nowrap; }
body.no-bottomnav .sticky-total strong { font-size: 1.1rem; }
