:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1f2733;
  --ink-soft: #5b6675;
  --line: #e3e8f0;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --chip: #eef2ff;
  --th-bg: #f8fafc;
  --row-hover: #f9fbff;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.06);
  --radius: 10px;
  color-scheme: light;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
input, select, textarea, button { font-family: inherit; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  display: flex;
  flex-direction: column;
}

/* ---------------- Topbar ---------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  z-index: 5;
}
.tabs { display: flex; gap: 4px; }
.tab {
  border: none; background: transparent; padding: 8px 16px; border-radius: 8px;
  font-size: 14px; color: var(--ink-soft); cursor: pointer; font-weight: 600;
}
.tab:hover { background: var(--bg); }
.tab.active { background: var(--brand); color: #fff; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

/* ---------------- Buttons ---------------- */
.btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.btn:hover { border-color: #c7d0de; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.icon { padding: 4px 8px; font-size: 13px; border: none; background: transparent; }
.btn.icon:hover { background: var(--bg); }
.btn.danger { color: var(--danger); }

/* ---------------- Views ---------------- */
main { flex: 1; overflow: auto; padding: 18px; }
.view { display: none; }
.view.active { display: block; }

/* ---------------- Toolbar ---------------- */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.search { flex: 1 1 220px; min-width: 180px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; background: var(--panel); color: var(--ink); }
.filter { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--ink); font-size: 13px; }
.chk { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-soft); font-size: 13px; }
.chk.inline { grid-column: 1 / -1; }
.form .full { grid-column: 1 / -1; }
.lbl { color: var(--ink-soft); font-weight: 600; }
.spacer { flex: 1; }
.count { color: var(--ink-soft); font-size: 13px; }

/* ---------------- Tables ---------------- */
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; box-shadow: var(--shadow); }
.table-wrap.short { max-height: 340px; }
table.grid { width: 100%; border-collapse: collapse; }
table.grid th, table.grid td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.grid th { position: sticky; top: 0; background: var(--th-bg); font-size: 12px; text-transform: uppercase; letter-spacing: .3px; color: var(--ink-soft); z-index: 1; }
table.grid.compact th, table.grid.compact td { padding: 6px 10px; }
table.grid tbody tr:hover { background: var(--row-hover); }
.num { text-align: right; }
.center { text-align: center; }
.empty { padding: 40px; text-align: center; color: var(--ink-soft); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.badge.rif { background: #fef3c7; color: var(--warn); }
.cell-input { width: 90px; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; font-size: 13px; text-align: right; background: var(--panel); color: var(--ink); }
.cell-input.text { width: 100%; text-align: left; }

/* ---------------- Cards ---------------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card .k { color: var(--ink-soft); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.card .v { font-size: 24px; font-weight: 700; margin-top: 6px; }
.card .v.money { color: var(--brand); }
.card .v.margin { color: var(--ok); }

/* ---------------- Charts ---------------- */
.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.chart-box { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.chart-box.wide { grid-column: 1 / -1; }
.chart-box h3 { margin: 0 0 10px; font-size: 14px; }
.chart-box canvas { max-height: 260px; }

/* ---------------- Anagrafiche ---------------- */
.anag-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 14px; align-items: start; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.panel.wide { grid-column: 1 / -1; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.panel-head h3 { margin: 0; font-size: 15px; }
.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.chips li { background: var(--chip); border-radius: 999px; padding: 6px 12px; display: flex; align-items: center; gap: 8px; font-size: 13px; }
.chips li button { border: none; background: none; cursor: pointer; color: var(--danger); font-weight: 700; }
.hint { color: var(--ink-soft); font-size: 12px; margin-top: 14px; }

/* ---------------- Modal ---------------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal { background: var(--panel); border-radius: 14px; padding: 22px; width: 460px; max-width: 92vw; box-shadow: 0 20px 40px rgba(0,0,0,.2); }
.modal h3 { margin: 0 0 16px; }
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-soft); font-weight: 600; }
.form input, .form select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; color: var(--ink); background: var(--panel); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------------- Toast ---------------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px;
  font-size: 13px; z-index: 100; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}

@media (max-width: 980px) {
  .charts { grid-template-columns: 1fr; }
  .anag-grid { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
}

/* Dark palette. Applied when the user explicitly picks Dark, or when the OS is
   in dark mode and the user has not forced a theme from the toolbar. */
:root[data-theme="dark"] {
  --bg: #0f141b; --panel: #182029; --ink: #e6ebf2; --ink-soft: #9aa7b8;
  --line: #2a3543; --chip: #1e2b45; --shadow: none;
  --th-bg: #1c2531; --row-hover: #1b2430;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    --bg: #0f141b; --panel: #182029; --ink: #e6ebf2; --ink-soft: #9aa7b8;
    --line: #2a3543; --chip: #1e2b45; --shadow: none;
    --th-bg: #1c2531; --row-hover: #1b2430;
    color-scheme: dark;
  }
}
