:root {
  --bg:#f3f7f4; --card:#ffffff; --fg:#1f2d27; --muted:#5f6f68; --border:#d9e3dd; --hover:#eef5f0;
  --accent:#4fa877; --accent2:#3b8a60; --danger:#c0392b; --income:#2e8b57; --expense:#c0392b;
  --sidebar:#1f2d27; --sidebar-fg:#dfe9e3; --sidebar-active:#4fa877; --sidebar-w:232px; --rail-w:64px;
  --st-paid:#5ee15e; --st-overdue:#e34948; --st-due3:#9b6bd6; --st-due7:#eda100; --st-ok:#c3c2b7;
  --chart-grid:#e1e0d9; --chart-ink:#52514e;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:#0f1512; --card:#19211d; --fg:#e6ede9; --muted:#9fb0a7; --border:#2a3530; --hover:#1f2a25;
    --sidebar:#0b100e; --sidebar-fg:#cdd8d2; --chart-grid:#2c2c2a; --chart-ink:#c3c2b7;
  }
}
* { box-sizing:border-box; }
html, body { height:100%; }
body { margin:0; font:15px/1.5 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif; background:var(--bg); color:var(--fg); }
a { color:var(--accent2); }
h1 { font-size:20px; font-weight:600; margin:0; }
h2 { margin:0 0 12px; font-size:17px; }
h3 { margin:16px 0 8px; font-size:15px; }
.hidden { display:none !important; }
.muted { color:var(--muted); font-size:13px; }
.error { color:var(--danger); margin-top:8px; }
.ok { color:var(--income); margin-top:8px; }
.income { color:var(--income); } .expense { color:var(--expense); }
.mt0 { margin-top:0 !important; } .mt8 { margin-top:8px; } .mt12 { margin-top:12px; } .mt14 { margin-top:14px; } .w200 { max-width:200px; }
code { background:var(--hover); padding:2px 5px; border-radius:4px; word-break:break-all; }
.key { font-family:ui-monospace,Consolas,monospace; background:var(--hover); padding:8px; border-radius:6px; word-break:break-all; margin-top:8px; white-space:pre-wrap; }
.badge { display:inline-block; font-size:12px; padding:1px 7px; border-radius:10px; background:var(--hover); color:var(--muted); vertical-align:middle; }
.badge.on { background:#dff3e6; color:var(--income); }
.ms { font-family:'Material Symbols Outlined'; font-weight:normal; font-style:normal; font-size:22px; line-height:1; display:inline-block; vertical-align:middle; user-select:none; }
button .ms { font-size:18px; margin-right:4px; vertical-align:-3px; }

/* ---- form ---- */
label { display:block; font-size:13px; color:var(--muted); margin-top:10px; }
label.inline { display:inline-flex; gap:6px; align-items:center; color:var(--fg); font-size:14px; margin-top:14px; }
input, select, textarea { width:100%; padding:9px 10px; border:1px solid var(--border); border-radius:6px; font-size:15px; background:var(--card); color:var(--fg); }
input[type=checkbox] { width:auto; }
textarea { min-height:70px; resize:vertical; }
button { background:var(--card); color:var(--fg); border:1px solid var(--border); border-radius:6px; padding:8px 14px; font-size:14px; cursor:pointer; margin-top:10px; }
button.primary { background:var(--accent); color:#fff; border-color:var(--accent); }
button.secondary { background:var(--card); }
button.danger { color:var(--danger); border-color:var(--danger); }
button.small { padding:4px 9px; font-size:13px; margin:0 2px; }
button.wide { width:100%; }
button:disabled { opacity:.5; cursor:default; }
.icon-btn { background:transparent; border:0; padding:6px; margin:0; border-radius:50%; color:inherit; display:inline-flex; }
.icon-btn:hover { background:rgba(128,128,128,.15); }
.row { display:grid; grid-template-columns:repeat(auto-fit,minmax(170px,1fr)); gap:0 14px; }
.row.compact { display:flex; gap:8px; align-items:center; flex-wrap:wrap; }
.row.compact input, .row.compact select { width:auto; }
.row.compact button { margin:0; }
.seg { display:inline-flex; border:1px solid var(--border); border-radius:6px; overflow:hidden; margin-top:6px; }
.seg button { margin:0; border:0; border-radius:0; background:var(--card); }
.seg button.active { background:var(--accent); color:#fff; }
.days { display:flex; gap:6px; flex-wrap:wrap; margin-top:6px; }
.days label { display:inline-flex; gap:4px; align-items:center; margin:0; font-size:14px; color:var(--fg); }

/* ---- layout ---- */
.login { min-height:100%; display:flex; align-items:center; justify-content:center; padding:20px; }
.login-card { width:100%; max-width:380px; }
.login h1 { margin-bottom:10px; }
.app { display:flex; min-height:100%; }
.sidebar { position:fixed; inset:0 auto 0 0; width:var(--sidebar-w); background:var(--sidebar); color:var(--sidebar-fg); display:flex; flex-direction:column; z-index:30; transition:width .18s ease, transform .18s ease; overflow:hidden; }
.brand { display:flex; align-items:center; gap:10px; padding:14px 12px 14px 20px; font-weight:600; font-size:17px; white-space:nowrap; }
.brand-icon { color:var(--sidebar-active); font-size:26px; }
.brand .dock-btn { margin-left:auto; color:var(--sidebar-fg); }
.brand .dock-btn .ms { font-size:20px; margin:0; }
nav { display:flex; flex-direction:column; padding:6px 10px; gap:2px; flex:1; }
nav a { display:flex; align-items:center; gap:12px; padding:10px 12px; border-radius:8px; color:var(--sidebar-fg); text-decoration:none; white-space:nowrap; }
nav a:hover { background:rgba(255,255,255,.08); }
nav a.active { background:var(--sidebar-active); color:#fff; }
nav a .ms { font-size:22px; flex:none; }
.nav-sep { height:1px; background:rgba(255,255,255,.12); margin:6px 8px; }
.sidebar-footer { display:flex; align-items:center; gap:8px; padding:10px 12px 14px 20px; border-top:1px solid rgba(255,255,255,.12); white-space:nowrap; }
.sidebar-footer .label { flex:1; overflow:hidden; text-overflow:ellipsis; }
.sidebar-footer .icon-btn { color:var(--sidebar-fg); }
.content { flex:1; margin-left:var(--sidebar-w); min-width:0; padding:0 20px 28px; transition:margin-left .18s ease; }
.topbar { display:flex; align-items:center; gap:10px; padding:14px 0 10px; position:sticky; top:0; background:var(--bg); z-index:5; }
.topbar #menuBtn { display:none; }
.topbar-actions { margin-left:auto; display:flex; gap:8px; }
.scrim { display:none; position:fixed; inset:0; background:rgba(0,0,0,.4); z-index:25; }
.page { max-width:1200px; }
.card { background:var(--card); border:1px solid var(--border); border-radius:10px; padding:18px; margin-bottom:18px; }
.card-head { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; margin-bottom:10px; }
.card-head h2 { margin:0; }
.card-head button { margin:0; }
.grid2 { display:grid; grid-template-columns:repeat(auto-fit,minmax(360px,1fr)); gap:18px; }
.tiles { display:grid; grid-template-columns:repeat(auto-fit,minmax(170px,1fr)); gap:12px; margin-bottom:18px; }
.tiles.small { grid-template-columns:repeat(auto-fit,minmax(130px,1fr)); margin:12px 0; }
.tile { background:var(--hover); border-radius:8px; padding:12px; }
.tile .v { font-size:22px; font-weight:600; }
.tile .l { font-size:12px; color:var(--muted); }
.tile .s { font-size:12px; color:var(--muted); margin-top:2px; }
.chart-box { position:relative; height:260px; }
.chart-box.tall { height:320px; }
.legend { display:flex; flex-wrap:wrap; gap:6px 14px; margin-top:8px; font-size:13px; }
.legend span::before { content:""; display:inline-block; width:10px; height:10px; border-radius:2px; background:var(--c); margin-right:5px; vertical-align:-1px; }

/* docked (rail) sidebar on desktop */
.app.rail .sidebar { width:var(--rail-w); }
.app.rail .brand { padding-left:16px; }
.app.rail .brand-text, .app.rail nav .label, .app.rail .sidebar-footer .label, .app.rail .sidebar-footer a { display:none; }
.app.rail .brand .dock-btn { margin:0 auto; }
.app.rail nav a { justify-content:center; padding:10px 0; }
.app.rail .sidebar-footer { flex-direction:column; padding-left:12px; }
.app.rail .content { margin-left:var(--rail-w); }
.app.rail .sidebar:hover { width:var(--sidebar-w); box-shadow:4px 0 16px rgba(0,0,0,.25); }
.app.rail .sidebar:hover .brand-text, .app.rail .sidebar:hover nav .label, .app.rail .sidebar:hover .sidebar-footer .label, .app.rail .sidebar:hover .sidebar-footer a { display:inline; }
.app.rail .sidebar:hover nav a { justify-content:flex-start; padding:10px 12px; }
.app.rail .sidebar:hover .sidebar-footer { flex-direction:row; padding-left:20px; }
.app.rail .sidebar:hover .brand .dock-btn { margin-left:auto; }
.app.rail .sidebar:hover .brand { padding-left:20px; }

/* mobile: sidebar off-canvas */
@media (max-width: 900px) {
  .sidebar { transform:translateX(-100%); width:var(--sidebar-w) !important; }
  .app.open .sidebar { transform:none; }
  .app.open .scrim { display:block; }
  .content { margin-left:0 !important; padding:0 12px 20px; }
  .topbar #menuBtn { display:inline-flex; }
  .brand .dock-btn { display:none; }
  .app.rail .brand-text, .app.rail nav .label, .app.rail .sidebar-footer .label, .app.rail .sidebar-footer a { display:inline; }
  .app.rail nav a { justify-content:flex-start; padding:10px 12px; }
  .app.rail .sidebar-footer { flex-direction:row; padding-left:20px; }
}

/* ---- tables ---- */
table { width:100%; border-collapse:collapse; font-size:14px; }
th, td { text-align:left; padding:7px 6px; border-bottom:1px solid var(--border); vertical-align:middle; }
th { color:var(--muted); font-weight:500; }
td.num, th.num { text-align:right; white-space:nowrap; }
tr.clickable { cursor:pointer; }
tr.clickable:hover { background:var(--hover); }
td input, td select { padding:5px 7px; font-size:14px; }
.icon-img { width:28px; height:28px; object-fit:contain; vertical-align:middle; }
.icon-img.big { width:40px; height:40px; }

/* ---- icon picker ---- */
.icon-pick { display:inline-flex; align-items:center; gap:8px; margin-top:0; padding:5px 10px; }
.icon-pick img { width:28px; height:28px; }
.icon-pick span { font-size:13px; color:var(--muted); }
.icon-grid { display:grid; grid-template-columns:repeat(auto-fill,minmax(64px,1fr)); gap:6px; max-height:60vh; overflow:auto; }
.icon-grid button { margin:0; padding:6px 2px; display:flex; flex-direction:column; align-items:center; gap:3px; font-size:10px; color:var(--muted); overflow:hidden; }
.icon-grid button img { width:32px; height:32px; }
.icon-grid button.sel { border-color:var(--accent); background:var(--hover); }
.icon-grid button span { max-width:100%; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }

/* ---- bollette ---- */
.bl-group { border:1px solid var(--border); border-radius:8px; margin-bottom:10px; overflow:hidden; }
.bl-group-head { display:flex; align-items:center; gap:10px; padding:10px 12px; background:var(--hover); cursor:pointer; }
.bl-group-head .ms { color:var(--accent2); }
.bl-group-head .title { font-weight:600; flex:1; }
.bl-group-head .sum { font-size:13px; color:var(--muted); white-space:nowrap; }
.bl-year { padding:6px 12px 6px 28px; font-size:13px; color:var(--muted); display:flex; gap:12px; cursor:pointer; border-top:1px solid var(--border); }
.bl-year .title { font-weight:600; flex:1; color:var(--fg); }
.bl-item { display:flex; gap:10px; padding:8px 12px 8px 24px; border-top:1px solid var(--border); cursor:pointer; align-items:center; }
.bl-item:hover { background:var(--hover); }
.bl-bar { width:5px; align-self:stretch; border-radius:3px; background:var(--st-ok); flex:none; }
.bl-item.paid .bl-bar { background:var(--st-paid); } .bl-item.overdue .bl-bar { background:var(--st-overdue); }
.bl-item.due3 .bl-bar { background:var(--st-due3); } .bl-item.due7 .bl-bar { background:var(--st-due7); }
.bl-item .sup { width:32px; flex:none; display:flex; align-items:center; justify-content:center; }
.bl-item .sup .icon-img { width:28px; height:28px; }
.sup-ms { color:var(--accent2); font-size:24px; }
.bl-item .main { flex:1; min-width:0; }
.bl-item .t { font-weight:600; }
.bl-item .d { font-size:13px; color:var(--muted); word-break:break-word; }
.bl-item .p { font-size:13px; white-space:nowrap; color:var(--muted); }
.bl-item .p.yes { color:var(--income); }
.att-list { display:flex; flex-wrap:wrap; gap:8px; margin-top:6px; }
.att { display:inline-flex; align-items:center; gap:6px; padding:4px 8px; border:1px solid var(--border); border-radius:6px; font-size:13px; }
.att .ms { font-size:18px; }
.att button { margin:0; padding:0 4px; border:0; background:transparent; color:var(--danger); }
.att img { width:48px; height:48px; object-fit:cover; border-radius:4px; }

/* ---- modal / qr ---- */
.modal { position:fixed; inset:0; background:rgba(0,0,0,.45); z-index:50; display:flex; align-items:flex-start; justify-content:center; padding:30px 12px; overflow:auto; }
.modal-box { background:var(--card); color:var(--fg); border-radius:10px; width:100%; max-width:760px; padding:16px 18px 18px; box-shadow:0 10px 40px rgba(0,0,0,.3); }
.modal-head { display:flex; align-items:center; justify-content:space-between; margin-bottom:6px; }
.modal-head h2 { margin:0; }
.modal-body h3 { color:var(--accent2); margin-top:14px; }
.qr { display:flex; gap:20px; align-items:flex-start; flex-wrap:wrap; margin-top:12px; }
.qrbox { background:#fff; padding:8px; border:1px solid var(--border); border-radius:8px; }
