:root {
  color-scheme: light dark;
  --bg: #f3f2ef; --surface: #fcfcfb; --ink: #0b0b0b; --ink2: #52514e; --muted: #75746f; --line: #e3e2dd; --soft: #f0efec;
  --accent: #1c5cab; --accent-ink: #fff; --danger: #c0392b; --ok: #0a7a3a; --warn: #9a6a00; --focus: #2a78d6;
  --c-luce: #eda100; --c-gas: #eb6834; --c-acqua: #2a78d6; --c-altro: #6250d6;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111110; --surface: #1a1a19; --ink: #f0efec; --ink2: #c3c2b7; --muted: #9a998f; --line: #2e2e2b; --soft: #242422;
    --accent: #3987e5; --accent-ink: #0b0b0b; --danger: #ef7468; --ok: #4cc27d; --warn: #e3b341;
    --c-luce: #c98500; --c-gas: #d95926; --c-acqua: #3987e5; --c-altro: #9085e9;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; display: flex; align-items: center; gap: 8px; } h4 { font-size: .95rem; margin: 18px 0 2px; }
a { color: var(--accent); }
code { background: var(--soft); padding: 1px 5px; border-radius: 5px; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); } .small { font-size: .85rem; }
.boot { text-align: center; color: var(--muted); padding: 40px 16px; } .boot.err { color: var(--danger); }
.sub { color: var(--ink2); font-size: .88rem; margin: 2px 0 0; }
.hint { color: var(--muted); font-size: .82rem; margin: 2px 0; }
.note { color: var(--muted); font-size: .8rem; }
.warn { color: var(--warn); } .bad { color: var(--danger); font-style: normal; } em.warn { font-style: normal; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; flex: none; }
.dot.big { width: 14px; height: 14px; margin: 0 4px 0 0; }

/* layout */
.top { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.house-name, .top select { margin-left: auto; color: var(--ink2); font-size: .9rem; max-width: 45vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top select { background: var(--soft); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--line); background: var(--soft); display: grid; place-items: center; font-weight: 700; padding: 0; overflow: hidden; cursor: pointer; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.tabs { position: fixed; bottom: 0; left: 0; right: 0; z-index: 6; display: grid; grid-template-columns: repeat(5, 1fr); background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tabs a { display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 8px 4px 7px; text-decoration: none; color: var(--ink2); font-size: .74rem; }
.tabs a span { font-size: 1.15rem; }
.tabs a.on { color: var(--accent); font-weight: 700; box-shadow: inset 0 2px 0 var(--accent); }
main { max-width: 860px; margin: 0 auto; padding: 14px 16px 96px; display: flex; flex-direction: column; gap: 14px; }
@media (min-width: 760px) {
  .tabs { position: sticky; top: 53px; bottom: auto; border-top: 0; border-bottom: 1px solid var(--line); padding: 0 16px; display: flex; justify-content: center; gap: 8px; }
  .tabs a { flex-direction: row; gap: 7px; padding: 11px 18px; font-size: .92rem; }
  main { padding-bottom: 40px; }
}
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.card.empty { text-align: center; color: var(--ink2); } .card.empty h2 { margin-bottom: 8px; color: var(--ink); }
.steps { text-align: left; max-width: 420px; margin: 8px auto 0; padding-left: 20px; }
.empty-note { padding: 18px; color: var(--muted); text-align: center; margin: 0; }
.row { display: flex; gap: 8px; align-items: center; } .row.between { justify-content: space-between; } .row.end { justify-content: flex-end; margin-top: 6px; } .row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; } .spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 420px) { .grid2 { grid-template-columns: 1fr; } }

/* controlli */
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px; color: var(--ink); min-height: 40px; }
.btn:hover { background: var(--soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; } .btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
.btn.small { padding: 5px 11px; min-height: 34px; font-size: .88rem; }
.btn.link { border: 0; background: none; color: var(--accent); }
.btn:disabled { opacity: .55; cursor: wait; }
.ico { border: 0; background: none; cursor: pointer; font-size: 1.05rem; padding: 6px; border-radius: 8px; text-decoration: none; line-height: 1; } .ico:hover { background: var(--soft); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: .86rem; display: inline-flex; align-items: center; min-height: 34px; }
.chip .dot { margin-right: 6px; }
.chip.on { background: var(--ink); color: var(--surface); border-color: var(--ink); } .chip.on.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--ink2); }
label.check { flex-direction: row; align-items: flex-start; gap: 8px; color: var(--ink); } label.check input { margin-top: 3px; }
input:not([type=checkbox]):not([type=file]), select { font-size: 16px; width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; min-height: 42px; color: var(--ink); }
input.ocr { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 9%, var(--surface)); }
.unit { color: var(--muted); }
.form-err { color: var(--danger); margin: 0; min-height: 0; font-size: .88rem; } .form-err:empty { display: none; }
.badge { display: inline-block; font-size: .7rem; padding: 1px 7px; border-radius: 999px; background: var(--soft); color: var(--ink2); border: 1px solid var(--line); vertical-align: middle; }
.warn-box { background: color-mix(in srgb, var(--warn) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--warn) 40%, var(--line)); border-radius: 10px; padding: 8px 12px 8px 28px; margin: 0; font-size: .86rem; }
p.warn-box { padding-left: 12px; }
details > summary { cursor: pointer; color: var(--ink2); font-size: .86rem; padding: 4px 0; }
.rawtext { white-space: pre-wrap; max-height: 220px; overflow: auto; background: var(--soft); padding: 8px; border-radius: 8px; font-size: .75rem; }

/* liste e tabelle */
.rows { list-style: none; margin: 8px 0 0; padding: 0; } .rows li { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--line); } .rows li.dim { opacity: .55; }
.tbl-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; } th { color: var(--ink2); font-weight: 600; font-size: .78rem; } tfoot td { border-bottom: 0; }
.r { text-align: right; } td.act { white-space: nowrap; }
table.list tbody tr:last-child td { border-bottom: 0; }
details.tbl { margin-top: 4px; } details.tbl table { font-size: .8rem; }

/* tiles */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 620px) { .tiles { grid-template-columns: 1fr; } }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.tile span { color: var(--ink2); font-size: .8rem; } .tile b { font-size: 1.45rem; font-variant-numeric: tabular-nums; } .tile small { color: var(--muted); font-size: .8rem; }

/* bollette */
.bill { display: flex; flex-direction: column; gap: 8px; }
.bill-head { display: flex; align-items: flex-start; gap: 8px; } .bill-head .dot { margin-top: 5px; }
.amount { font-size: 1.25rem; font-weight: 700; white-space: nowrap; }
.bill-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--ink2); font-size: .85rem; }
.bill-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.sharebar { display: flex; height: 16px; border-radius: 8px; overflow: hidden; gap: 2px; background: var(--surface); }
.sharebar span { min-width: 4px; }
.alloc td .dot { margin-right: 6px; }

/* foto / ocr */
.photo-zone { border: 1px dashed var(--line); border-radius: 12px; padding: 10px; background: var(--soft); }
.photo-btn { width: 100%; cursor: pointer; }
.ocr-out:not(:empty) { margin-top: 10px; font-size: .88rem; } .ocr-out p { margin: 4px 0; }
.ocr-out { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; } .ocr-out > div { flex: 1; min-width: 180px; }
.thumb { width: 90px; height: 90px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: sp .8s linear infinite; vertical-align: -2px; } @keyframes sp { to { transform: rotate(360deg); } }

/* grafici */
.chart-fig { margin: 6px 0 0; }
.chart { width: 100%; min-height: 40px; }
.svg { display: block; overflow: visible; }
.svg .grid { stroke: var(--line); stroke-width: 1; }
.svg .base { stroke: var(--muted); stroke-width: 1; }
.svg .ax { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.svg .lbl { fill: var(--ink); font-size: 11px; font-weight: 600; }
.svg .hit { fill: transparent; cursor: crosshair; } .svg rect.hit:hover { fill: color-mix(in srgb, var(--ink) 6%, transparent); }
.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 0; margin: 4px 0 6px; font-size: .82rem; color: var(--ink2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
#tip { position: fixed; z-index: 100; pointer-events: none; background: var(--ink); color: var(--surface); border-radius: 10px; padding: 8px 11px; font-size: .8rem; box-shadow: 0 6px 22px rgba(0,0,0,.28); max-width: 260px; }
#tip[hidden] { display: none; } #tip b { display: block; margin-bottom: 3px; }
#tip .tl { display: flex; align-items: center; gap: 7px; } #tip .tl em { font-style: normal; margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; font-weight: 600; }
#tip i { width: 9px; height: 9px; border-radius: 3px; flex: none; } #tip i.nosw { background: none; width: 0; }

/* dialog / toast */
dialog { border: 0; border-radius: 18px; padding: 20px; width: calc(100vw - 24px); max-width: 440px; max-height: 92vh; background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog.wide { max-width: 640px; } dialog::backdrop { background: rgba(0,0,0,.5); }
dialog .confirm p { margin: 0 0 14px; }
#toasts { position: fixed; left: 0; right: 0; bottom: 84px; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 200; pointer-events: none; }
.toast { background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 12px; font-size: .9rem; max-width: 90vw; box-shadow: 0 6px 20px rgba(0,0,0,.3); } .toast.err { background: var(--danger); color: #fff; }

/* login */
.login { align-items: center; text-align: center; padding-top: 12vh; max-width: 420px; gap: 14px; }
.login form.stack { width: 100%; text-align: left; }
.gbtn { min-height: 44px; display: flex; justify-content: center; }
.dev { display: flex; flex-direction: column; gap: 8px; width: 100%; border-top: 1px dashed var(--line); padding-top: 14px; }

/* ---- mobile ---- */
@media (max-width: 560px) {
  main { padding: 12px 12px 92px; gap: 12px; }
  .card { padding: 14px 12px; border-radius: 12px; }
  .top { padding: 8px 12px; }
  h3 { font-size: 1rem; }
  .chips { flex-wrap: nowrap; overflow-x: auto; margin: 0 -12px; padding: 2px 12px 6px; scrollbar-width: none; }
  .chips::-webkit-scrollbar { display: none; }
  .chips .chip, .chips .spacer { flex: none; }
  .chips .spacer { flex: 1 0 8px; }
  .ocr-out .chips { flex-wrap: wrap; overflow: visible; margin: 0; padding: 0; }
  .row.between.wrap { align-items: stretch; }
  .tile b { font-size: 1.3rem; }
  .bill-actions .btn.small { flex: 1 1 auto; }
  .bill-actions .spacer { display: none; }
  .tbl-wrap { overflow: visible; }
  details.tbl .tbl-wrap { overflow-x: auto; }
  table.stackm thead { display: none; }
  table.stackm, table.stackm tbody, table.stackm tfoot { display: block; }
  table.stackm tr { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 14px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
  table.stackm tbody tr:last-child { border-bottom: 0; }
  table.stackm td { border: 0; padding: 2px 0; display: flex; justify-content: space-between; gap: 8px; text-align: right; align-items: baseline; }
  table.stackm td::before { content: attr(data-label); color: var(--muted); font-size: .78rem; text-align: left; }
  table.stackm .hide-m { display: none; }
  table.stackm td:first-child { grid-column: 1 / -1; text-align: left; font-weight: 600; display: block; }
  table.stackm td:first-child::before { content: none; }
  table.stackm td.act { grid-column: 1 / -1; justify-content: flex-end; }
  table.stackm tfoot tr { display: flex; justify-content: space-between; }
  table.stackm tfoot td { display: block; } table.stackm tfoot td::before { content: none; }
  .alloc table.stackm tr { padding-left: 0; padding-right: 0; }
  dialog { margin: auto 0 0; width: 100vw; max-width: 100vw; max-height: 92vh; border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  dialog.wide { max-width: 100vw; }
  #toasts { bottom: 76px; }
  .login { padding-top: 8vh; }
}

/* rate */
.tiles.two { grid-template-columns: 1fr 1fr; } @media (max-width: 560px) { .tiles.two { grid-template-columns: 1fr; } }
.progress { height: 8px; border-radius: 999px; background: var(--soft); overflow: hidden; margin: 8px 0 2px; border: 1px solid var(--line); }
.progress span { display: block; height: 100%; background: var(--ok); border-radius: 999px; }
.inst .n { width: 26px; height: 26px; border-radius: 50%; background: var(--soft); border: 1px solid var(--line); display: grid; place-items: center; font-size: .78rem; color: var(--ink2); flex: none; }
.chips.tight { margin-top: 6px; }
.preview { background: var(--soft); border-radius: 12px; padding: 4px 8px; max-height: 240px; overflow: auto; }
.preview table { font-size: .84rem; } .preview td, .preview th { padding: 5px 6px; }
.linkbtn { border: 0; background: none; color: var(--accent); cursor: pointer; font-size: .8rem; padding: 2px 6px; text-decoration: underline; }
@media (max-width: 560px) { .tabs a { font-size: .68rem; } }
