:root {
  --board: #22123F;
  --board-2: #311B57;
  --board-ink: #E9E1F7;
  --paper: #F9F8FC;
  --sheet: #FFFFFF;
  --ink: #1F1A2B;
  --ink-2: #5A5270;
  --ink-3: #8B84A0;
  --rule: #E3DEEE;
  --rule-strong: #C4BAD9;
  --accent: #6A1FD6;
  --accent-hover: #5718B3;
  --board-3: #47297A;
  --board-muted: #B3A2D6;
  --board-soft: #CDBFEA;
  --leaf: #E67817;
  --chart-out: #E67817;
  --row-hover: #F3EFFB;
  --th-bg: #FBFAFE;
  --backdrop: rgba(34, 18, 63, 0.4);
  --grid-line: rgba(230, 120, 23, 0.10);
  --red-ink: #A8321F;
  --pencil: #9A6A12;
  --focus: #2D7FF9;
  --radius-s: 4px;
  --radius-m: 8px;
  --serif: Charter, "Bitstream Charter", "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.5 var(--sans); color: var(--ink); background: var(--paper); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0; letter-spacing: -0.005em; }
h1 { font-size: 1.85rem; line-height: 1.2; }
h2 { font-size: 1.3rem; line-height: 1.3; }
h3 { font-size: 1.05rem; }
.num, td.num, th.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.neg { color: var(--red-ink); }
.muted { color: var(--ink-2); }
.small { font-size: 0.85rem; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.hidden { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Shell ---------- */
.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.shell > div { min-width: 0; }
.nav { background: var(--board); color: var(--board-ink); padding: 20px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.brand { font-family: var(--serif); font-size: 1.35rem; color: #fff; padding: 0 10px 16px; display: flex; align-items: baseline; gap: 8px; }
.brand-mark .leaf-body { fill: var(--leaf); }
.brand-mark .leaf-vein { stroke: var(--board); }
.brand-mark { width: 22px; height: 22px; flex: none; position: relative; top: 3px; }
.org-switch { margin: 0 6px 14px; }
.org-switch select { width: 100%; background: var(--board-2); color: #fff; border: 1px solid var(--board-3); }
.nav a { color: var(--board-ink); text-decoration: none; padding: 7px 10px; border-radius: var(--radius-s); display: flex; justify-content: space-between; align-items: center; }
.nav a:hover { background: var(--board-2); color: #fff; }
.nav a.active { background: var(--paper); color: var(--board); font-weight: 600; }
.nav .group { margin-top: 14px; padding: 0 10px 4px; font-size: 0.8rem; color: var(--board-muted); }
.nav .badge { background: var(--leaf); color: var(--board); border-radius: 10px; font-size: 0.75rem; padding: 0 7px; font-weight: 600; }
.nav .spacer { flex: 1; }
.nav .who { overflow-wrap: anywhere; padding: 10px; font-size: 0.85rem; color: var(--board-soft); border-top: 1px solid var(--board-3); margin-top: 10px; }
.nav .who button { background: none; border: 0; color: #DCE8E1; padding: 0; text-decoration: underline; cursor: pointer; font: inherit; }
.main { padding: 28px 36px 64px; max-width: 1180px; width: 100%; }
.topbar { display: none; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--ink-2); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.banner { background: #FFF6E0; border: 1px solid #E8D29A; color: #5C4510; padding: 10px 14px; border-radius: var(--radius-s); margin-bottom: 18px; }
.banner.err { background: #FBECE9; border-color: #E6B3A9; color: #6E1F12; }

/* ---------- Controls ---------- */
button, .btn { font: inherit; border-radius: var(--radius-s); border: 1px solid var(--rule-strong); background: var(--sheet); color: var(--ink); padding: 7px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; line-height: 1.3; }
button:hover, .btn:hover { border-color: var(--accent); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-hover); }
button.danger { color: var(--red-ink); border-color: #E2B6AD; }
button.link { border: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--sheet); border: 1px solid var(--rule-strong); border-radius: var(--radius-s); padding: 7px 9px; width: 100%; }
textarea { min-height: 70px; resize: vertical; }
input[type=checkbox] { width: auto; accent-color: var(--accent); }
input.num { text-align: right; }
label { display: block; font-size: 0.85rem; color: var(--ink-2); }
label > input, label > select, label > textarea { margin-top: 4px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--ink); font-size: 0.95rem; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.filters { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.filters button[aria-pressed=true] { background: var(--board); color: #fff; border-color: var(--board); }
.filters .grow { flex: 1; }

/* ---------- Sheets & tables ---------- */
.sheet { overflow-x: auto; background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius-m); }
.sheet.pad { padding: 20px 22px; }
.sheet + .sheet, .section { margin-top: 20px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-weight: 600; font-size: 0.82rem; color: var(--ink-2); padding: 9px 12px; border-bottom: 1px solid var(--rule-strong); background: var(--th-bg); }
td { padding: 9px 12px; border-bottom: 1px solid var(--rule); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: var(--row-hover); }
.table-wrap { overflow-x: auto; }

/* Columnar-pad money columns: faint vertical rulings like a ledger pad. */
.ledger td.num, .ledger th.num { border-left: 1px solid var(--rule); min-width: 120px; }
.ledger tr.subtotal td { border-top: 1px solid var(--ink-3); font-weight: 600; }
.ledger tr.total td { font-weight: 700; border-bottom: 0; }
.ledger tr.total td.num span { border-bottom: 3px double var(--ink); padding-bottom: 1px; }
.ledger tr.section-head td { font-family: var(--serif); font-weight: 600; font-size: 1.02rem; padding-top: 18px; background: transparent; border-bottom: 1px solid var(--rule-strong); }
.ledger td.indent { padding-left: 28px; }

.status { font-family: var(--sans); font-weight: 500; vertical-align: middle; display: inline-block; font-size: 0.8rem; padding: 1px 8px; border-radius: 10px; border: 1px solid currentColor; white-space: nowrap; }
.s-paid { color: var(--accent); }
.s-open { color: var(--ink-2); }
.s-partial { color: #2D6390; }
.s-overdue { color: var(--red-ink); }
.s-draft { color: var(--pencil); border-style: dashed; }
.s-void { color: var(--ink-3); text-decoration: line-through; }
.s-matched, .s-categorized { color: var(--accent); }
.s-excluded { color: var(--ink-3); }

.empty { padding: 36px 20px; text-align: center; color: var(--ink-2); }
.empty h3 { color: var(--ink); margin-bottom: 6px; }
.empty button { margin-top: 12px; }

/* ---------- Dashboard ---------- */
.strip { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius-m); }
.strip.strip-3 { grid-template-columns: repeat(3, 1fr); }
#pr h2.section { font-size: 1.05rem; margin: 22px 0 8px; }
#pr details summary { cursor: pointer; color: var(--accent); font-size: 0.95rem; }
ol.steps { padding-left: 22px; line-height: 1.6; }
ol.steps li { margin-bottom: 8px; }
.strip > div { padding: 16px 20px; border-left: 1px solid var(--rule); }
.strip > div:first-child { border-left: 0; }
.strip .label { color: var(--ink-2); font-size: 0.88rem; }
.strip .value { font-family: var(--serif); font-size: 1.7rem; font-variant-numeric: tabular-nums; margin-top: 2px; }
.strip .note { font-size: 0.82rem; color: var(--ink-3); }
.two-col > .sheet { margin-top: 0; }
.two-col { display: grid; grid-template-columns: 1.5fr 1fr; gap: 20px; margin-top: 20px; }
.chart { display: flex; align-items: flex-end; gap: 14px; height: 190px; padding: 10px 4px 0; border-bottom: 1px solid var(--ink-3); }
.chart .m { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; }
.chart .bars { display: flex; gap: 3px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.chart .bar { width: 38%; max-width: 22px; border-radius: 2px 2px 0 0; min-height: 1px; }
.chart .in { background: var(--accent); }
.chart .out { background: var(--chart-out); }
.chart-labels { display: flex; gap: 14px; padding: 6px 4px 0; }
.chart-labels span { flex: 1; text-align: center; font-size: 0.8rem; color: var(--ink-2); }
.legend { display: flex; gap: 16px; font-size: 0.85rem; color: var(--ink-2); margin-top: 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.todo li { margin: 6px 0; }

/* ---------- Document editor ---------- */
.lines td { padding: 6px; }
.lines input, .lines select { padding: 6px 7px; }
.doc-totals { margin-left: auto; width: 320px; margin-top: 12px; }
.doc-totals td { padding: 6px 12px; }

/* ---------- Dialog ---------- */
dialog { border: 1px solid var(--rule-strong); border-radius: var(--radius-m); padding: 0; width: min(560px, calc(100vw - 32px)); color: var(--ink); background: var(--sheet); }
dialog.wide { width: min(900px, calc(100vw - 32px)); }
dialog::backdrop { background: var(--backdrop); }
dialog header { padding: 16px 22px; border-bottom: 1px solid var(--rule); }
dialog .body { padding: 18px 22px; max-height: 70vh; overflow-y: auto; }
dialog footer { padding: 12px 22px; border-top: 1px solid var(--rule); display: flex; justify-content: flex-end; gap: 8px; }
.form-error { color: var(--red-ink); margin-top: 10px; min-height: 1.2em; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--board); color: #fff; padding: 10px 18px; border-radius: var(--radius-s); z-index: 100; }

/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; }
.auth-side { background: var(--board); color: var(--board-ink); padding: 56px; display: flex; flex-direction: column; justify-content: space-between; background-image: repeating-linear-gradient(90deg, transparent 0 119px, var(--grid-line) 119px 120px); }
.auth-side h1 { color: #fff; font-size: 2.4rem; max-width: 14ch; }
.auth-side p { max-width: 42ch; font-size: 1.05rem; }
.auth-form { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px 24px; position: relative; }
.parent-brand { position: absolute; bottom: 28px; display: flex; align-items: center; gap: 10px; color: var(--ink-3); font-size: 0.85rem; }
.parent-brand img { height: 21px; width: auto; display: block; }
@media (max-width: 900px) { .parent-brand { position: static; margin-top: 36px; } }
.auth-form form { width: 100%; max-width: 380px; display: grid; gap: 14px; }
.auth-form h2 { margin-bottom: 4px; }

/* ---------- Invoice print view ---------- */
.paper-doc { background: #fff; border: 1px solid var(--rule); border-radius: var(--radius-m); padding: 44px 48px; max-width: 820px; }
.paper-doc .head { display: flex; justify-content: space-between; gap: 24px; margin-bottom: 32px; }
.paper-doc .title { font-family: var(--serif); font-size: 2rem; }
.paper-doc .addr { white-space: pre-line; }

@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .nav { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 50; transform: translateX(-100%); transition: transform .2s; }
  .nav.open { transform: none; }
  .topbar { display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--board); color: #fff; position: sticky; top: 0; z-index: 40; }
  .topbar button { background: none; color: #fff; border-color: var(--board-3); }
  .main { padding: 20px 16px 48px; }
  .strip { grid-template-columns: 1fr 1fr; }
  .strip > div:nth-child(3) { border-left: 0; }
  .strip > div:nth-child(n+3) { border-top: 1px solid var(--rule); }
  .two-col, .g3, .g4 { grid-template-columns: 1fr; }
  .auth { grid-template-columns: 1fr; }
  .auth-side { padding: 32px 24px; }
  .doc-totals { width: 100%; }
  .paper-doc { padding: 24px; }
}
@media (max-width: 560px) { .g2 { grid-template-columns: 1fr; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

@media print {
  .nav, .topbar, .page-head .actions, .no-print { display: none !important; }
  .shell { display: block; }
  .main { padding: 0; max-width: none; }
  body { background: #fff; }
  .sheet, .paper-doc { border: 0; }
}

.doc-logo { max-height: 72px; max-width: 240px; width: auto; display: block; margin-bottom: 12px; }
.logo-row { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.logo-row .doc-logo { margin: 0; padding: 8px; border: 1px solid var(--rule); border-radius: var(--radius-s); background: #fff; }

.invites { display: grid; gap: 10px; margin-top: 12px; }
.invite { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 14px; border: 1px solid var(--rule); border-radius: var(--radius-s); background: var(--th-bg); }
.badge-inline { display: inline-block; background: var(--leaf); color: var(--board); border-radius: 10px; padding: 0 8px; font-weight: 600; font-size: 0.85rem; }

/* ---------- Check printing (voucher checks: check on top, two stubs; 3.5in sections on letter paper) ---------- */
#check-print { display: none; }
@media print {
  body.printing-checks > *:not(#check-print) { display: none !important; }
  body.printing-checks #check-print { display: block; }
  body.printing-checks { background: #fff; }
}
@page checks { size: letter; margin: 0; }
.check-page { page: checks; width: 8.5in; height: 11in; position: relative; page-break-after: always; break-after: page; color: #000; font: 11pt/1.3 Arial, Helvetica, sans-serif; }
.check-page .check-face { position: relative; height: 3.5in; }
.check-page .check-face .f { position: absolute; white-space: nowrap; }
.check-page .check-face .date { top: 0.70in; left: 6.55in; }
.check-page .check-face .payee { top: 1.30in; left: 1.05in; font-weight: 700; }
.check-page .check-face .amount { top: 1.30in; left: 6.85in; font-weight: 700; }
.check-page .check-face .words { top: 1.68in; left: 0.45in; width: 6.3in; overflow: hidden; display: flex; }
.check-page .check-face .words .fill { flex: 1; border-bottom: 1px dotted #000; margin: 0 0 4px 6px; }
.check-page .check-face .address { top: 2.05in; left: 1.05in; white-space: normal; width: 3.5in; font-size: 10pt; }
.check-page .check-face .memo { top: 2.90in; left: 0.70in; font-size: 10pt; max-width: 3.4in; overflow: hidden; }
.stub { height: 3.75in; padding: 0.35in 0.5in; box-sizing: border-box; font-size: 10pt; }
.stub-head { display: flex; justify-content: space-between; border-bottom: 1px solid #000; padding-bottom: 4px; margin-bottom: 8px; }
.stub table { border-collapse: collapse; }
.stub td { padding: 3px 14px 3px 0; border: 0; vertical-align: top; }
.stub td:first-child { color: #444; width: 0.9in; }
