:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #5f6672;
  --line: #d9dde3;
  --primary: #1f5f8b;
  --primary-text: #ffffff;
  --danger: #a3262c;
  --ok: #1e6b3a;
  --warn: #8a5a00;
  --warn-bg: #fff4d6;
  --alert-bg: #fde4e4;
  --income: #2c7a4b;
  --expense: #b8472b;
  --radius: 6px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181c;
    --surface: #1e2227;
    --text: #e6e8eb;
    --muted: #9aa3ad;
    --line: #353b43;
    --primary: #4d94c6;
    --primary-text: #0d1117;
    --danger: #e06c75;
    --ok: #5cb87a;
    --warn: #e0b24d;
    --warn-bg: #3a3014;
    --alert-bg: #3d1f1f;
    --income: #5cb87a;
    --expense: #e0845c;
  }
}
* { box-sizing: border-box; }
html { font-size: 15px; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); line-height: 1.45; }
a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }
h3 { font-size: 1rem; margin: 0.75rem 0 0.4rem; }
.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 1.25rem; padding: 0.6rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.05rem; }
.nav { display: flex; flex-wrap: wrap; gap: 0.15rem; flex: 1; }
.nav a { padding: 0.35rem 0.6rem; border-radius: var(--radius); text-decoration: none; color: var(--text); }
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--primary); color: var(--primary-text); }
.userbox { display: flex; align-items: center; gap: 0.5rem; margin: 0; }
.badge { font-size: 0.75rem; padding: 0.1rem 0.45rem; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); }
.main { max-width: 1240px; margin: 0 auto; padding: 1.25rem; }
.page-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem; margin-bottom: 1rem; }
.page-header .actions { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.25rem; margin-bottom: 1rem; }
.card.narrow { max-width: 560px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); gap: 1rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 0.75rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.8rem 1rem; }
.kpi.warn { background: var(--warn-bg); }
.kpi.alert { background: var(--alert-bg); }
.kpi-label { font-size: 0.8rem; color: var(--muted); }
.kpi-value { font-size: 1.35rem; font-weight: 700; margin: 0.2rem 0; }
.kpi-sub { font-size: 0.8rem; color: var(--muted); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 0.45rem 0.5rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.table th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.table tfoot th { color: var(--text); }
.table.compact th, .table.compact td { padding: 0.3rem 0.4rem; font-size: 0.9rem; }
.num { text-align: right !important; white-space: nowrap; font-variant-numeric: tabular-nums; }
.neg { color: var(--danger); }
.pos { color: var(--ok); }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 0.85rem; word-break: break-all; }
.truncate { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { color: var(--muted); font-style: italic; }
.ok { color: var(--ok); }
.error { color: var(--danger); }
.flash { padding: 0.6rem 0.9rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid; }
.flash-ok { color: var(--ok); border-color: var(--ok); background: color-mix(in srgb, var(--ok) 10%, var(--surface)); }
.flash-error { color: var(--danger); border-color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, var(--surface)); }
.plain { list-style: none; padding: 0; margin: 0; }
.plain li { margin: 0.25rem 0; }
.links li { margin: 0.4rem 0; }
.status { display: inline-block; font-size: 0.78rem; padding: 0.1rem 0.5rem; border-radius: 999px; border: 1px solid var(--line); white-space: nowrap; }
.status-draft { color: var(--muted); }
.status-final, .status-credit { color: var(--primary); border-color: var(--primary); }
.status-partial { color: var(--warn); border-color: var(--warn); }
.status-overdue { color: var(--danger); border-color: var(--danger); }
.status-paid { color: var(--ok); border-color: var(--ok); }
.status-cancelled { color: var(--muted); text-decoration: line-through; }
.btn { display: inline-block; padding: 0.4rem 0.8rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); text-decoration: none; cursor: pointer; font: inherit; line-height: 1.2; }
.btn:hover { border-color: var(--primary); }
.btn-primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-small { padding: 0.2rem 0.5rem; font-size: 0.85rem; }
.btn-link { border: none; background: none; color: var(--primary); padding: 0.2rem; }
form.inline { display: inline; margin: 0; }
.stack { display: flex; flex-direction: column; gap: 0.75rem; }
.stack label, .form-grid label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem 1rem; }
.form-grid .span-2 { grid-column: span 2; }
.form-grid .actions { display: flex; gap: 0.5rem; }
label.check { flex-direction: row; align-items: center; gap: 0.5rem; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.4rem 0.5rem; width: 100%; }
input[type="checkbox"], input[type="file"] { width: auto; }
textarea { resize: vertical; }
.filterbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 1rem; }
.filterbar input, .filterbar select { width: auto; }
.filterbar input[type="search"] { min-width: 220px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: flex-end; margin-top: 0.75rem; }
.inline-form label { display: flex; flex-direction: column; font-size: 0.8rem; color: var(--muted); }
.inline-form input, .inline-form select { width: auto; min-width: 120px; }
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 0.3rem 1rem; margin: 0; }
.dl dt { color: var(--muted); font-size: 0.85rem; }
.dl dd { margin: 0; }
.toolbar { display: flex; gap: 0.5rem; align-items: center; margin-bottom: 0.5rem; }
.toolbar select { width: auto; max-width: 420px; }
.lines td { vertical-align: middle; }
.lines textarea { min-width: 260px; }
.lines .num-input { width: 90px; text-align: right; }
.lines select { width: auto; }
.lines .pos { color: var(--muted); }
.totals { display: flex; justify-content: flex-end; gap: 2rem; padding: 0.75rem 0.5rem; }
.totals div { display: flex; flex-direction: column; align-items: flex-end; }
.totals span { font-size: 0.8rem; color: var(--muted); }
.totals strong { font-size: 1.1rem; }
.no-tax .tax-col { display: none; }
.chart { width: 100%; height: auto; max-height: 260px; }
.chart .bar-income { fill: var(--income); }
.chart .bar-expense { fill: var(--expense); }
.chart .axis { font-size: 11px; fill: var(--muted); }
.chart .axis-line { stroke: var(--line); }
.legend { font-size: 0.85rem; color: var(--muted); display: flex; gap: 1rem; align-items: center; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 2px; }
.swatch.income { background: var(--income); }
.swatch.expense { background: var(--expense); }
details summary { cursor: pointer; color: var(--primary); margin: 0.5rem 0; }
.username { font-size: 0.9rem; }
@media (max-width: 720px) {
  .main { padding: 0.75rem; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: span 1; }
  .table { display: block; overflow-x: auto; }
  .totals { flex-wrap: wrap; gap: 1rem; }
}
@media print {
  .topbar, .actions, .filterbar, .btn { display: none !important; }
}
