:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efea;
  --border: #e2e0d8;
  --text: #1c1c1a;
  --text-2: #55544f;
  --muted: #7b7a73;
  --accent: #1f5fa8;
  --accent-ink: #ffffff;
  --accent-soft: #e5eefa;
  --good: #1d7a3a;
  --good-soft: #e3f3e8;
  --warn: #9a5b00;
  --warn-soft: #fbf0dc;
  --bad: #b3261e;
  --bad-soft: #fbe6e4;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --grid: #e8e6df;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #141413;
    --surface: #1d1d1b;
    --surface-2: #262624;
    --border: #353531;
    --text: #f4f3ee;
    --text-2: #c3c2b7;
    --muted: #97968d;
    --accent: #5a9be8;
    --accent-ink: #0d1520;
    --accent-soft: #1e2a3a;
    --good: #6cc98a;
    --good-soft: #1b2e21;
    --warn: #e0a84a;
    --warn-soft: #33291a;
    --bad: #f08a82;
    --bad-soft: #3a1f1d;
    --series-1: #3987e5;
    --series-2: #d95926;
    --grid: #2c2c29;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #141413; --surface: #1d1d1b; --surface-2: #262624; --border: #353531;
  --text: #f4f3ee; --text-2: #c3c2b7; --muted: #97968d;
  --accent: #5a9be8; --accent-ink: #0d1520; --accent-soft: #1e2a3a;
  --good: #6cc98a; --good-soft: #1b2e21; --warn: #e0a84a; --warn-soft: #33291a;
  --bad: #f08a82; --bad-soft: #3a1f1d; --series-1: #3987e5; --series-2: #d95926;
  --grid: #2c2c29; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
a { color: var(--accent); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { display: block; }
.brand-name { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12px; color: var(--muted); }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; flex: 1; }
.tabs button {
  border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--text-2);
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.year-pick { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-2); }

/* ---------- layout ---------- */
.main { max-width: 1180px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--text-2); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.grid { display: grid; gap: 16px; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 16px; }
.grid.two { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
@media (max-width: 900px) { .grid.two { grid-template-columns: 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; min-width: 0;
}
.card h2 { font-size: 15px; margin-bottom: 12px; }
.card h2 .sub { font-weight: 400; color: var(--muted); font-size: 13px; margin-left: 6px; }
.card + .card { margin-top: 16px; }
.grid > .card + .card { margin-top: 0; }

.kpi .label { font-size: 13px; color: var(--text-2); }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .foot { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kpi .value.neg { color: var(--bad); }

/* ---------- buttons & forms ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.danger { color: var(--bad); }
.btn.link { border: 0; background: none; padding: 3px 6px; color: var(--accent); }
.btn.link:hover { text-decoration: underline; background: none; }

input[type=text], input[type=password], input[type=date], input[type=month], input[type=number], input[type=email], select, textarea, input:not([type]) {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface);
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, .tabs button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { min-height: 70px; resize: vertical; }
.year-pick select { width: auto; padding: 5px 8px; }
input.search { width: 220px; max-width: 100%; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th {
  text-align: left; font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover td { background: var(--surface-2); }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data .actions { text-align: right; white-space: nowrap; }
table.data tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: 0; }
table.data .dim { color: var(--muted); font-size: 13px; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.pill.open { background: var(--accent-soft); color: var(--accent); }
.pill.paid { background: var(--good-soft); color: var(--good); }
.pill.overdue { background: var(--bad-soft); color: var(--bad); }
.pill.void { background: var(--surface-2); color: var(--muted); }
.pill.inactive { background: var(--surface-2); color: var(--muted); }

/* ---------- dashboard bits ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--grid); }
.list li:last-child { border-bottom: 0; }
.list .when { font-weight: 600; white-space: nowrap; }
.list .what { color: var(--text-2); font-size: 14px; }
.list .what small { display: block; color: var(--muted); font-size: 12px; }
.amount-in { color: var(--good); font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount-out { color: var(--text); font-variant-numeric: tabular-nums; white-space: nowrap; }

.callout { border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; border: 1px solid var(--border); background: var(--surface); }
.callout.warn { background: var(--warn-soft); border-color: transparent; }
.callout.info { background: var(--accent-soft); border-color: transparent; }
.callout h2 { font-size: 15px; margin-bottom: 8px; }
.callout ol { margin: 0; padding-left: 20px; }
.callout li { margin: 4px 0; }
.callout li.done { color: var(--muted); text-decoration: line-through; }

.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; font-size: 14px; }
.bar-row .track { grid-column: 1 / -1; height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-row .fill { height: 100%; background: var(--series-2); border-radius: 0 4px 4px 0; }

/* ---------- chart ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: hidden; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--border); stroke-width: 1; }
.chart .b-in { fill: var(--series-1); }
.chart .b-out { fill: var(--series-2); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover { fill: var(--surface-2); opacity: .6; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.tip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,.12); border-radius: 8px; padding: 8px 10px; font-size: 13px; min-width: 150px;
  display: none; z-index: 2;
}
.tip b { display: block; margin-bottom: 4px; }
.tip div { display: flex; justify-content: space-between; gap: 12px; font-variant-numeric: tabular-nums; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(20, 20, 18, .45); }
.form { padding: 22px 24px; }
.form h2 { font-size: 18px; margin-bottom: 4px; }
.form .hint { color: var(--text-2); font-size: 13px; margin: 0 0 14px; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; margin-top: 14px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field.full { grid-column: 1 / -1; }
.field > span { font-size: 13px; font-weight: 600; color: var(--text-2); }
.field .help { font-size: 12px; color: var(--muted); font-weight: 400; }
.field.check { flex-direction: row; align-items: center; gap: 8px; }
.field.check input { width: auto; }
.money-input { position: relative; }
.money-input::before { content: "$"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.money-input input { padding-left: 22px; }
.form-error { color: var(--bad); background: var(--bad-soft); padding: 8px 10px; border-radius: 7px; margin: 14px 0 0; font-size: 14px; }
.form .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
@media (max-width: 560px) { .fields { grid-template-columns: 1fr; } }

dialog.wide { width: min(1100px, calc(100vw - 32px)); }
dialog.wide[open] { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); }
dialog .aside { background: var(--surface-2); border-right: 1px solid var(--border); min-height: 520px; display: flex; }
dialog .aside iframe, dialog .aside img { width: 100%; border: 0; background: #fff; }
dialog .aside iframe { min-height: 100%; }
dialog .aside img { object-fit: contain; max-height: 80vh; }
dialog .no-preview { margin: auto; padding: 24px; text-align: center; color: var(--text-2); }
@media (max-width: 800px) {
  dialog.wide[open] { grid-template-columns: 1fr; }
  dialog .aside { min-height: 300px; border-right: 0; border-bottom: 1px solid var(--border); }
}
.due-preview { margin-top: 12px; }

.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; cursor: pointer;
  padding: 18px; margin-bottom: 16px; border: 2px dashed var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text-2); font-size: 14px; transition: border-color .15s, background .15s;
}
.dropzone b { color: var(--text); font-size: 15px; }
.dropzone:hover, body.dragging .dropzone { border-color: var(--accent); background: var(--accent-soft); }
body.dragging tr.drop-target td { background: var(--accent-soft) !important; box-shadow: inset 0 2px 0 var(--accent), inset 0 -2px 0 var(--accent); }
.file-link { font-size: 12px; text-decoration: none; }
.file-link:hover { text-decoration: underline; }

body.signed-out .tabs, body.signed-out .year-pick, body.signed-out #signOutBtn { visibility: hidden; }
.signin { max-width: 400px; margin: 40px auto; display: flex; flex-direction: column; gap: 14px; }
.signin h1 { font-size: 20px; }
.signin .note { margin: 0; }
.signin .btn { justify-content: center; padding: 10px; }
.signin .form-error { margin: 0; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px;
  transition: opacity .2s, transform .2s; pointer-events: none; max-width: calc(100vw - 32px); z-index: 20;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--bad); color: #fff; }

.note { font-size: 13px; color: var(--muted); margin-top: 10px; }
.kv { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 16px; font-size: 15px; }
.kv .k { color: var(--text-2); }
.kv .v { text-align: right; font-variant-numeric: tabular-nums; }
.kv .total { font-weight: 700; border-top: 1px solid var(--border); padding-top: 8px; }
.kv .big { font-size: 18px; }

@media (max-width: 700px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .main { padding: 16px; }
  .table-wrap { margin: 0 -20px; }
}
@media print {
  .topbar, .no-print, .btn { display: none !important; }
  .main { padding: 0; max-width: none; }
  .card { box-shadow: none; break-inside: avoid; }
}
