:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --pos: #16a34a;
  --pos-soft: #dcfce7;
  --neg: #dc2626;
  --neg-soft: #fee2e2;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --sidebar: #0f172a;
  --sidebar-text: #cbd5e1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 8px rgba(15, 23, 42, .04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --card: #131c2e;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #23304a;
    --accent: #60a5fa;
    --accent-soft: #1e3a5f;
    --pos: #22c55e;
    --pos-soft: #14361f;
    --neg: #f87171;
    --neg-soft: #3b1818;
    --warn: #fbbf24;
    --warn-soft: #3a2c0c;
    --sidebar: #070b16;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h3 { font-size: 15px; margin-bottom: 12px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; padding: 18px 12px;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 20px; }
.brand-logo {
  width: 36px; height: 36px; border-radius: 10px; background: linear-gradient(135deg, #3b82f6, #22c55e);
  color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 18px;
}
.brand-name { color: #fff; font-weight: 700; }
.brand-sub { font-size: 12px; opacity: .7; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  color: var(--sidebar-text); text-decoration: none; padding: 9px 12px; border-radius: 8px;
  display: flex; align-items: center; gap: 10px;
}
.nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav a.active { background: rgba(96, 165, 250, .18); color: #fff; }
.nav .ico { width: 18px; text-align: center; opacity: .85; }
.nav-badge { margin-left: auto; background: var(--neg); color: #fff; font-size: 11px; border-radius: 99px; padding: 0 7px; }
.sidebar-foot { padding-top: 12px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 16px 28px; background: var(--card);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 20px; flex: 1; }
.topbar-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.btn.menu-toggle { display: none; }
.view { padding: 24px 28px 48px; max-width: 1400px; width: 100%; }

/* ---------- Cards / KPI ---------- */
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); margin-bottom: 18px;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 12px; }
.card-head h3 { margin: 0; }
.big { font-size: 22px; font-weight: 700; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 { margin-bottom: 18px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 10px; font-weight: 600; }
.section-title .muted { text-transform: none; letter-spacing: 0; font-weight: 400; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); position: relative; overflow: hidden; color: inherit; text-decoration: none; display: block;
}
.kpi::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--border); }
.kpi-pos::before { background: var(--pos); }
.kpi-neg::before { background: var(--neg); }
.kpi-strong .kpi-value { font-size: 26px; }
.kpi-label { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.kpi-value { font-size: 22px; font-weight: 700; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--muted); font-size: 12px; }
.kpi-link:hover { border-color: var(--accent); }
.kpi-day input { margin: 6px 0; width: 100%; }
.delta { font-weight: 600; font-size: 12px; }
.delta.up { color: var(--pos); }
.delta.down { color: var(--neg); }

.chart-box { position: relative; height: 300px; }
.chart-sm { height: 220px; margin-bottom: 12px; }

.period-bar { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; padding: 12px 16px; }
.period-sel { display: flex; align-items: center; gap: 6px; }
.period-label { font-weight: 600; margin-left: auto; }

/* ---------- Segmented ---------- */
.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg button {
  border: 0; background: none; color: var(--muted); padding: 6px 12px; border-radius: 7px; cursor: pointer; font: inherit; font-weight: 500;
}
.seg button.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }
.seg-lg { display: flex; margin-bottom: 16px; }
.seg-lg button { flex: 1; padding: 10px; font-size: 15px; }
.seg-lg[data-tipo=entrata] button.active { background: var(--pos); color: #fff; }
.seg-lg[data-tipo=uscita] button.active { background: var(--neg); color: #fff; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 500; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-pos { background: var(--pos); border-color: var(--pos); color: #fff; }
.btn-danger { background: var(--neg); border-color: var(--neg); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--bg); border-color: var(--border); }
.btn-sm { padding: 4px 9px; font-size: 13px; }
.btn-block { width: 100%; }
.linkbtn { background: none; border: 0; padding: 0; cursor: pointer; font: inherit; }
.row-actions { display: flex; gap: 6px; align-items: center; }
.row-actions.wrap { flex-wrap: wrap; }
.row-actions.center { justify-content: center; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; white-space: nowrap; }
.table tfoot th { border-bottom: 0; border-top: 2px solid var(--border); }
.table tbody tr:hover { background: var(--bg); }
.table.compact th, .table.compact td { padding: 6px 8px; }
.num { text-align: right !important; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.row-danger td:first-child { box-shadow: inset 3px 0 0 var(--neg); }

.totals { display: flex; flex-wrap: wrap; gap: 8px 22px; padding: 4px 4px 14px; font-size: 13px; color: var(--muted); }
.totals b { color: var(--text); margin-left: 4px; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--bg); color: var(--muted); white-space: nowrap; }
.badge-ok { background: var(--pos-soft); color: var(--pos); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--neg-soft); color: var(--neg); }
.cat { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.tipo { display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 5px; font-weight: 700; margin-right: 4px; font-size: 13px; }
.tipo-entrata { background: var(--pos-soft); color: var(--pos); }
.tipo-uscita { background: var(--neg-soft); color: var(--neg); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { background: var(--card); border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; font-size: 12px; }

.txt-pos { color: var(--pos); }
.txt-neg { color: var(--neg); }
.txt-danger { color: var(--neg); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Filters ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 10px; padding: 12px; }
.filters input[type=search] { flex: 1 1 260px; }

/* ---------- Forms ---------- */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type=color] { padding: 2px; width: 36px; height: 32px; }
.form fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin: 0 0 14px; }
.form legend { font-weight: 600; padding: 0 6px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; margin-bottom: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.span-2 { grid-column: span 2; }
.inline { display: flex; gap: 6px; }
.inline input { flex: 1; }
.radio-row { display: flex; flex-wrap: wrap; gap: 8px 20px; margin-bottom: 12px; }
.radio-row label, .check { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.check { margin: 8px 0; align-items: flex-start; }
.check input { margin-top: 3px; }
.calc-out { background: var(--bg); border-radius: 8px; padding: 10px 12px; font-size: 13px; margin-bottom: 4px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
[hidden] { display: none !important; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .5); z-index: 50;
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto;
}
.modal { background: var(--card); border-radius: 14px; width: 100%; max-width: 480px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal.modal-wide { max-width: 1000px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 17px; }
.modal-body { padding: 18px 20px 20px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: #0f172a; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
}
.toast-err { background: var(--neg); }

/* ---------- Varie ---------- */
.empty { text-align: center; padding: 30px 16px; }
.empty h2 { margin-bottom: 8px; }
.info ul { margin: 0; padding-left: 20px; color: var(--muted); }
.info li { margin-bottom: 4px; }
.aging { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.aging-item { background: var(--bg); border-radius: 8px; padding: 8px 10px; display: flex; flex-direction: column; font-size: 12px; color: var(--muted); }
.aging-item b { color: var(--text); font-size: 14px; }
.aging-item.danger b { color: var(--neg); }
.progress { height: 4px; background: var(--bg); border-radius: 4px; margin-top: 4px; overflow: hidden; }
.progress div { height: 100%; background: var(--accent); }
.cat-list { list-style: none; padding: 0; margin: 0 0 12px; }
.cat-list li { display: flex; gap: 8px; align-items: center; padding: 5px 0; border-bottom: 1px solid var(--border); }
.cat-list li input:not([type=color]) { flex: 1; }

/* ---------- Gruppi scadenzario ---------- */
.gruppi { display: flex; flex-direction: column; gap: 8px; }
.gruppo { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.gruppo-scaduto { border-left: 3px solid var(--neg); }
.gruppo summary {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; cursor: pointer; list-style: none; background: var(--bg);
}
.gruppo summary::-webkit-details-marker { display: none; }
.gruppo summary::before { content: "▸"; color: var(--muted); display: inline-block; transition: transform .15s; }
.gruppo[open] summary::before { transform: rotate(90deg); }
.gruppo-nome { display: flex; align-items: center; gap: 8px; font-weight: 600; flex: 1; min-width: 0; }
.gruppo-info { white-space: nowrap; }
.gruppo-tot { white-space: nowrap; font-variant-numeric: tabular-nums; }
.gruppo .table { margin: 0; }
@media (max-width: 800px) { .gruppo summary { flex-wrap: wrap; } .gruppo-info { order: 3; width: 100%; padding-left: 18px; } }

/* ---------- Accesso e sincronizzazione ---------- */
body.locked .app { visibility: hidden; }
.auth {
  position: fixed; inset: 0; z-index: 45; background: var(--bg); overflow-y: auto;
  display: flex; align-items: center; justify-content: center; padding: 24px 16px;
}
.auth-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow);
  width: 100%; max-width: 400px; padding: 28px;
}
.auth-brand { padding: 0 0 20px; }
.auth-title { font-weight: 700; font-size: 18px; }
.auth-card .form .field { margin-bottom: 14px; }
.auth-card .btn-block { padding: 11px; margin-top: 4px; }
.auth-links { display: flex; flex-direction: column; gap: 8px; align-items: center; margin-top: 16px; font-size: 13px; }
.auth-msg { border-radius: 8px; padding: 9px 12px; font-size: 13px; margin-bottom: 12px; }
.auth-msg-err { background: var(--neg-soft); color: var(--neg); }
.auth-msg-ok { background: var(--pos-soft); color: var(--pos); }
.spinner {
  width: 32px; height: 32px; margin: 8px auto; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.pre-line { white-space: pre-line; }
.form-actions.wrap { flex-wrap: wrap; }
.account { margin-top: 12px; padding-top: 12px; border-top: 1px solid rgba(255, 255, 255, .08); }
.account-row { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.account-email { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; opacity: .8; }
.account .btn-ghost { color: var(--sidebar-text); }
.account .btn-ghost:hover { background: rgba(255, 255, 255, .08); border-color: transparent; color: #fff; }
.sync {
  display: flex; align-items: center; gap: 7px; width: 100%; background: none; border: 0; padding: 2px 0;
  color: var(--sidebar-text); font: inherit; font-size: 12px; cursor: pointer; text-align: left;
}
.sync::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #64748b; flex-shrink: 0; }
.sync-ok::before { background: #22c55e; }
.sync-pending::before { background: #60a5fa; animation: blink 1s ease-in-out infinite; }
.sync-warn::before { background: #fbbf24; }
.sync-err::before { background: #f87171; }
.sync-err { color: #fca5a5; }
@keyframes blink { 50% { opacity: .3; } }

.cloud-info { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.cloud-info div { display: flex; flex-direction: column; background: var(--bg); border-radius: 8px; padding: 8px 10px; min-width: 0; }
.cloud-info b { overflow: hidden; text-overflow: ellipsis; }
.cloud-info .muted { font-size: 12px; }

/* ---------- Condivisione e ruoli ---------- */
.share-list { list-style: none; padding: 0; margin: 0; }
.share-list li { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.share-main { flex: 1; min-width: 0; }
.perm-cats { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 16px; }
.perm-cats .check { margin: 4px 0; }
.archivi { display: flex; flex-direction: column; gap: 8px; }
.archivio {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; width: 100%;
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--card);
  color: var(--text); font: inherit; cursor: pointer; text-align: left;
}
.archivio:hover { border-color: var(--accent); }
.account-archivio { display: flex; align-items: center; gap: 6px; font-size: 12px; margin-bottom: 6px; color: #fff; }
.account-arch-nome { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-archivio .badge { background: rgba(255, 255, 255, .1); color: var(--sidebar-text); }
.account-cambia { color: #93c5fd; font-size: 12px; margin-left: auto; }
/* sola lettura: nascoste tutte le azioni che modificano i dati */
body.readonly [data-action="nuovo-movimento"], body.readonly [data-act="edit"], body.readonly [data-act="dup"],
body.readonly [data-act="del"], body.readonly [data-act="paga"], body.readonly #nuovo-cespite, body.readonly #nc2,
body.readonly [data-key], body.readonly #load-demo { display: none !important; }
body.readonly [data-act="stato"] { pointer-events: none; }
@media (max-width: 800px) { .perm-cats { grid-template-columns: 1fr; } }

.conti-table input, .conti-table select { width: 100%; min-width: 110px; }
.conti-table .num-input { text-align: right; }

.placeholder { padding: 56px 16px; }
.placeholder-ico { font-size: 36px; margin-bottom: 8px; }
.placeholder p { max-width: 460px; margin: 8px auto 16px; }

.pv-list { list-style: none; padding: 0; margin: 0 0 14px; }
.pv-list li { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); }
.pv-list li > input:not([type=color]) { flex: 1; }
.pv-perc { display: flex; align-items: center; gap: 4px; }
.pv-perc input { width: 70px; text-align: right; }
.pv-badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; white-space: nowrap; }
.pv-badge .dot { width: 8px; height: 8px; }

.bulk-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 10px; margin-bottom: 10px; background: var(--accent-soft); border-radius: 8px; }
.table .chk { width: 28px; }
.rip-custom { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 6px; }
.rip-custom label { display: flex; align-items: center; gap: 4px; font-size: 13px; }
.rip-custom input { width: 64px; text-align: right; }

.seg-pv button { display: inline-flex; align-items: center; gap: 6px; }
.seg-pv .dot { width: 8px; height: 8px; }

/* ---------- Food cost ---------- */
.seg-tabs { display: flex; flex-wrap: wrap; margin-bottom: 16px; }
.seg-tabs button { padding: 8px 16px; }
.txt-warn { color: var(--warn); }
.riga-click { cursor: pointer; }
.table .gruppo-riga td { background: var(--bg); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.fatt-head { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.fatt-head > div { display: flex; flex-direction: column; gap: 2px; background: var(--bg); border-radius: 8px; padding: 8px 10px; }
.righe-fatt select { max-width: 150px; }
.imp-table select { max-width: 150px; }
.imp-table td, .imp-table th { padding-left: 5px; padding-right: 5px; }
.comp-table select { width: 100%; min-width: 180px; }
.comp-table .num-input { width: 90px; text-align: right; }
.spacer { flex: 1; }
body.readonly .fc-w { display: none !important; }
.im-map { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin: 6px 0 10px; }
.da-completare { margin: 0; white-space: nowrap; }
.nuovo-ing { border: 1px dashed var(--accent); border-radius: 10px; padding: 10px 12px; margin: 10px 0; background: var(--bg); }
.nuovo-ing .form-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); margin: 8px 0; }
@media (max-width: 800px) { .nuovo-ing .form-grid { grid-template-columns: 1fr; } }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 800px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .4); }
  .btn.menu-toggle { display: inline-flex; }
  .topbar { padding: 12px 16px; flex-wrap: wrap; }
  .view { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .period-label { margin-left: 0; }
  .aging { grid-template-columns: repeat(2, 1fr); }
}

@media print {
  .sidebar, .topbar-actions, .menu-toggle, .period-bar .seg, .period-sel .btn, .kpi-day input { display: none !important; }
  .view { padding: 0; }
  .card, .kpi { box-shadow: none; break-inside: avoid; }
  body { background: #fff; }
}
