:root {
  --paper: #F3F5F8;
  --surface: #FFFFFF;
  --ink: #172238;
  --ink-2: #3C475C;
  --muted: #5F6A7E;
  --line: #DDE2EA;
  --line-strong: #C3CAD6;
  --red: #C0392B;
  --red-soft: #FCECEA;
  --green: #1E7A4C;
  --green-soft: #E3F3EA;
  --hl: #FFE14D;
  --hl-soft: #FFF7CC;
  --focus: #2456A6;
  --radius: 10px;
  --radius-sm: 6px;
  --font-body: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-head: "Bricolage Grotesque", "Atkinson Hyperlegible", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { font-family: var(--font-head); line-height: 1.15; margin: 0; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 2.1rem; letter-spacing: -0.02em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1.08rem; }
a { color: var(--ink); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ---------- Shell ---------- */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 28px;
  padding: 28px 16px 20px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}
.brand { font-family: var(--font-head); font-weight: 700; font-size: 1.15rem; line-height: 1.2; padding: 0 12px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: block; padding: 9px 12px; border-radius: var(--radius-sm);
  color: var(--ink-2); text-decoration: none; font-weight: 700; font-size: 0.95rem;
}
.nav a:hover { background: var(--paper); color: var(--ink); }
.nav a[aria-current="page"] { background: var(--hl-soft); color: var(--ink); box-shadow: inset 4px 0 0 var(--hl); }
.sign-out { margin-top: auto; align-self: flex-start; margin-left: 6px; }
.main { padding: 40px 44px 72px; max-width: 1320px; width: 100%; }
.main:focus { outline: none; }

/* ---------- Page header ---------- */
.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between;
  gap: 16px 24px; margin-bottom: 28px;
}
.page-head p { margin: 8px 0 0; color: var(--muted); max-width: 64ch; }
.head-tools { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 700; font-size: 0.9rem; line-height: 1.2;
  padding: 9px 15px; border-radius: 8px; cursor: pointer; white-space: nowrap; text-decoration: none;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  position: relative;
}
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: #26345a; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger:hover { background: #a93226; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--paper); }
.btn.small { padding: 6px 11px; font-size: 0.84rem; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn.busy { color: transparent !important; pointer-events: none; }
.btn.busy::after {
  content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid currentColor; border-color: var(--line-strong) var(--line-strong) var(--line-strong) transparent;
  animation: spin 0.7s linear infinite;
}
.btn.primary.busy::after, .btn.danger.busy::after { border-color: #fff #fff #fff transparent; }
@keyframes spin { to { transform: rotate(360deg); } }

.link-btn {
  background: none; border: 0; padding: 4px 6px; cursor: pointer;
  font: inherit; font-size: 0.86rem; font-weight: 700; color: var(--ink-2);
  text-decoration: underline; text-decoration-color: var(--line-strong); text-underline-offset: 3px;
  border-radius: 4px;
}
.link-btn:hover { color: var(--ink); text-decoration-color: currentColor; }
.link-btn.danger { color: var(--red); }
.link-btn[disabled] { opacity: 0.45; cursor: not-allowed; text-decoration: none; }
.actions { display: flex; flex-wrap: wrap; gap: 2px 4px; justify-content: flex-end; align-items: center; }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field > span { font-weight: 700; font-size: 0.9rem; }
.field small { color: var(--muted); font-size: 0.82rem; }
input, select, textarea {
  font: inherit; color: var(--ink); background: #fff;
  padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 3px solid rgba(36, 86, 166, 0.25); border-color: var(--focus); }
textarea { resize: vertical; min-height: 84px; line-height: 1.45; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.inline-field { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; font-size: 0.9rem; }
.inline-field input { width: auto; padding: 7px 10px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; font-size: 0.88rem; cursor: pointer; }
.toggle input { width: 16px; height: 16px; accent-color: var(--ink); }
.form-error {
  margin: 0 0 14px; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--red-soft); color: var(--red); font-weight: 700; font-size: 0.88rem;
}
.hint { font-size: 0.86rem; color: var(--muted); margin: 0 0 12px; }

/* ---------- Panels ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px 24px; }
.panel-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 8px 16px; margin-bottom: 16px; }

/* ---------- Dashboard ---------- */
.ledger {
  display: grid; grid-template-columns: minmax(250px, 1fr) 2.2fr;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; margin-bottom: 20px;
}
.ledger-net { padding: 26px 28px; background: var(--ink); color: #fff; }
.ledger-caption { margin: 0; font-weight: 700; color: #C9D1E0; }
.ledger-figure {
  margin: 8px 0 6px; font-family: var(--font-head); font-weight: 700;
  font-size: clamp(2.3rem, 4vw, 3.2rem); letter-spacing: -0.03em; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.ledger-figure.is-neg { color: #FFB4A9; }
.ledger-note { margin: 0; font-size: 0.86rem; color: #AAB4C6; }
.ledger-cols { display: grid; grid-template-columns: repeat(4, 1fr); margin: 0; }
.ledger-cols > div { padding: 26px 22px; border-left: 1px solid var(--line); }
.ledger-cols dt { font-size: 0.88rem; color: var(--muted); font-weight: 700; }
.ledger-cols dd { margin: 8px 0 0; font-family: var(--font-head); font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.ledger-cols dd small { display: block; font-family: var(--font-body); font-size: 0.8rem; font-weight: 400; color: var(--muted); letter-spacing: 0; margin-top: 2px; }
.pos { color: var(--green); }
.neg { color: var(--red); }

.dash-grid { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr); gap: 20px; margin-bottom: 20px; }
.chart svg { width: 100%; height: auto; display: block; }
.chart text { font-family: var(--font-body); font-size: 11px; fill: var(--muted); }
.chart .now { fill: var(--ink); font-weight: 700; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .bar-inv { fill: var(--green-soft); stroke: var(--green); stroke-width: 1; stroke-dasharray: 3 2; }
.chart .bar-rec { fill: var(--green); }
.chart .bar-exp { fill: var(--ink); }
.chart .now-band { fill: var(--hl-soft); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; list-style: none; margin: 0; padding: 0; font-size: 0.84rem; color: var(--muted); }
.swatch { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.swatch.inv { background: var(--green-soft); border: 1px dashed var(--green); }
.swatch.rec { background: var(--green); }
.swatch.exp { background: var(--ink); }
.year-strip { display: flex; flex-wrap: wrap; gap: 8px 32px; margin: 18px 0 0; padding-top: 14px; border-top: 1px solid var(--line); }
.year-strip dt { font-size: 0.84rem; color: var(--muted); }
.year-strip dd { margin: 2px 0 0; font-weight: 700; font-variant-numeric: tabular-nums; }

.owed-list { list-style: none; margin: 0; padding: 0; max-height: 380px; overflow-y: auto; }
.owed-list li { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-top: 1px solid var(--line); }
.owed-list li:first-child { border-top: 0; padding-top: 0; }
.owed-who { flex: 1; min-width: 0; }
.owed-who strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.owed-who span { font-size: 0.84rem; color: var(--muted); }
.owed-amt { font-weight: 700; color: var(--red); font-variant-numeric: tabular-nums; }
.next-steps { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 16px 24px; }
.next-steps p { margin: 6px 0 0; color: var(--ink-2); max-width: 62ch; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }

/* ---------- Payments register (the red/green tracker) ---------- */
.register-key { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px; margin: -10px 0 18px; font-size: 0.88rem; color: var(--muted); }
.register-key .mark { pointer-events: none; }
.register-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
.register { border-collapse: separate; border-spacing: 0; width: 100%; min-width: 1120px; table-layout: fixed; font-variant-numeric: tabular-nums; }
.register col.who-col { width: 190px; }
.register col.total-col-w { width: 92px; }
.register th, .register td { padding: 10px 3px; text-align: center; border-bottom: 1px solid var(--line); font-size: 0.86rem; }
.register thead th { font-weight: 700; color: var(--muted); padding-top: 16px; padding-bottom: 12px; }
.register tbody tr:last-child td { border-bottom: 0; }
.register .who {
  text-align: left; position: sticky; left: 0; z-index: 1;
  background: var(--surface); padding-left: 18px; padding-right: 10px;
  border-right: 1px solid var(--line);
}
.register .who strong, .register .who span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.register .who strong { font-size: 0.95rem; }
.register .who span { color: var(--muted); font-size: 0.8rem; }
.register .is-now { background: var(--hl-soft); }
.register thead .is-now { color: var(--ink); box-shadow: inset 0 4px 0 var(--hl); }
.register .total-col { text-align: right; padding-right: 16px; font-weight: 700; white-space: nowrap; }
.register .total-col.first { border-left: 1px solid var(--line); padding-left: 10px; }
.register tfoot td { background: var(--paper); font-weight: 700; font-size: 0.8rem; border-bottom: 0; border-top: 1px solid var(--line); }
.register tfoot .who { background: var(--paper); }
.register tfoot .paid-line { color: var(--green); display: block; }
.register tfoot .owed-line { color: var(--red); display: block; }

.mark {
  font: inherit; font-weight: 700; font-size: 0.8rem; line-height: 1.2; white-space: nowrap;
  width: 100%; max-width: 86px; padding: 6px 2px; overflow: hidden; text-overflow: clip; border-radius: 999px; cursor: pointer;
  border: 2px solid var(--red); background: var(--red-soft); color: var(--red);
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.mark:hover { background: #F9DCD8; }
.mark:active { transform: scale(0.96); }
.mark.paid { background: var(--green); border-color: var(--green); color: #fff; }
.mark.paid:hover { background: #196640; }
.mark.paid::before { content: "✓\2009"; }
.register-key .mark { width: auto; max-width: none; padding: 4px 10px; }
.mark-empty { color: var(--line-strong); }
.tag { display: inline-block; font-size: 0.72rem; font-weight: 700; padding: 1px 7px; border-radius: 999px; background: var(--paper); color: var(--muted); border: 1px solid var(--line); vertical-align: 1px; margin-left: 4px; }

/* ---------- Tables ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; min-width: 720px; }
.table th { text-align: left; font-size: 0.84rem; color: var(--muted); padding: 13px 16px; border-bottom: 1px solid var(--line); font-weight: 700; white-space: nowrap; }
.table td { padding: 14px 16px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tbody tr:last-child td { border-bottom: 0; }
.table td small { display: block; color: var(--muted); font-size: 0.84rem; }
.table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.table .actions-cell { text-align: right; }
.table tfoot td { font-weight: 700; background: var(--paper); border-top: 1px solid var(--line); border-bottom: 0; }
.is-archived > td { color: var(--muted); }
.kids { margin: 0; padding: 0; list-style: none; }
.kids li { font-size: 0.9rem; }
.kids li span { color: var(--muted); }

.chip { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 0.8rem; font-weight: 700; background: var(--paper); color: var(--ink-2); border: 1px solid var(--line); white-space: nowrap; }
.chip.paid { background: var(--green-soft); color: var(--green); border-color: transparent; }
.chip.unpaid { background: var(--red-soft); color: var(--red); border-color: transparent; }
.chip.sent { background: var(--green-soft); color: var(--green); border-color: transparent; }
.stale { display: block; font-size: 0.8rem; color: var(--red); margin-top: 4px; }

/* ---------- Families ---------- */
.family-list { display: flex; flex-direction: column; gap: 14px; }
.family { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.family.is-archived { background: transparent; border-style: dashed; }
.family.is-archived h3, .family.is-archived .family-total { color: var(--muted); }
.family-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 22px; padding: 16px 20px; }
.family-id { flex: 1 1 220px; min-width: 0; }
.family-id a { color: var(--muted); font-size: 0.9rem; word-break: break-all; }
.family-total { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; font-size: 1.05rem; }
.family-total span { display: block; font-weight: 400; font-size: 0.8rem; color: var(--muted); }
.children { list-style: none; margin: 0; padding: 4px 20px 6px; border-top: 1px solid var(--line); }
.children li {
  display: grid; grid-template-columns: minmax(0, 1fr) 80px 130px auto; gap: 12px; align-items: center;
  padding: 10px 0; border-bottom: 1px dashed var(--line);
}
.children li:last-child { border-bottom: 0; }
.children li.is-archived { color: var(--muted); }
.child-name { font-weight: 700; }
.child-fee { text-align: right; font-variant-numeric: tabular-nums; }
.children .no-kids { display: block; padding: 12px 0; color: var(--muted); }

/* ---------- Empty and loading ---------- */
.empty { padding: 44px 24px; text-align: center; background: var(--surface); border: 1px dashed var(--line-strong); border-radius: var(--radius); }
.empty h2 { margin-bottom: 6px; }
.empty p { color: var(--muted); margin: 0 auto 18px; max-width: 48ch; }
.loading { color: var(--muted); padding: 40px 0; }
.error-panel { padding: 20px 22px; border-radius: var(--radius); background: var(--red-soft); color: var(--red); font-weight: 700; }

/* ---------- Modal ---------- */
#modal-root { position: fixed; inset: 0; z-index: 50; display: none; }
#modal-root.open { display: grid; place-items: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(23, 34, 56, 0.45); }
.modal {
  position: relative; background: var(--surface); border-radius: 12px;
  width: min(520px, 100%); max-height: calc(100vh - 32px); overflow: auto;
  box-shadow: 0 24px 60px rgba(23, 34, 56, 0.28);
  animation: rise 0.16s ease-out;
}
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 20px 22px 4px; }
.modal-body { padding: 14px 22px 2px; }
.modal-body .form-error { margin-top: 4px; }
.modal-foot { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 22px 22px; align-items: center; }
.spacer { flex: 1; }
.icon-btn { background: none; border: 0; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); padding: 2px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--paper); color: var(--ink); }
.summary-box { background: var(--paper); border-radius: var(--radius-sm); padding: 12px 14px; margin: 0 0 14px; }
.summary-box ul { list-style: none; margin: 0; padding: 0; }
.summary-box li { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; font-variant-numeric: tabular-nums; }
.summary-box li.total { border-top: 1px solid var(--line-strong); margin-top: 6px; padding-top: 8px; font-weight: 700; }
.result-list { margin: 6px 0 12px; padding-left: 18px; }
.result-list li { margin: 2px 0; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast {
  background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 8px;
  font-weight: 700; font-size: 0.9rem; max-width: 380px;
  box-shadow: 0 10px 30px rgba(23, 34, 56, 0.25); animation: rise 0.16s ease-out;
}
.toast.error { background: var(--red); }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 32px 30px; box-shadow: inset 0 5px 0 var(--hl); }
.login-card h1 { font-size: 1.7rem; }
.login-card > p { color: var(--muted); margin: 8px 0 22px; }

/* ---------- Settings ---------- */
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; align-items: start; }
.settings-grid .panel h2 { margin-bottom: 16px; }
.status-line { display: flex; gap: 10px; align-items: center; margin: 0 0 12px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--red); flex: none; }
.dot.ok { background: var(--green); }
.dot.warn { background: #D99A00; }
.save-bar { margin-top: 20px; display: flex; gap: 12px; align-items: center; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .dash-grid { grid-template-columns: 1fr; }
  .ledger { grid-template-columns: 1fr; }
  .ledger-cols { grid-template-columns: repeat(2, 1fr); }
  .ledger-cols > div { border-left: 0; border-top: 1px solid var(--line); }
  .ledger-cols > div:nth-child(even) { border-left: 1px solid var(--line); }
  .settings-grid { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky; top: 0; z-index: 20; height: auto;
    flex-direction: row; align-items: center; gap: 12px;
    padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--line);
    overflow-x: auto;
  }
  .brand { display: none; }
  .nav { flex-direction: row; }
  .nav a { white-space: nowrap; padding: 8px 10px; }
  .nav a[aria-current="page"] { box-shadow: inset 0 -4px 0 var(--hl); }
  .sign-out { margin: 0 0 0 auto; white-space: nowrap; }
  .main { padding: 26px 16px 56px; }
  h1 { font-size: 1.7rem; }
  .children li { grid-template-columns: minmax(0, 1fr) auto; }
  .children li .actions { grid-column: 1 / -1; grid-row: 3; justify-content: flex-start; }
  .children li .child-age { grid-column: 1; grid-row: 2; font-size: 0.85rem; }
  .field-row { grid-template-columns: 1fr; }
  .family-total { text-align: left; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- Logo upload (Settings) ---------- */
.logo-box { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-top: 6px; }
.logo-frame {
  width: 220px; height: 84px; display: grid; place-items: center; padding: 10px;
  background: #fff; border: 1px dashed var(--line-strong); border-radius: 8px;
}
.logo-frame { box-sizing: border-box; overflow: hidden; }
.logo-preview { display: block; max-width: 196px; max-height: 60px; width: auto; height: auto; object-fit: contain; }
.logo-empty { color: var(--muted); font-size: 0.85rem; }
.logo-actions { display: flex; align-items: center; gap: 14px; }
.logo-actions label.btn { cursor: pointer; }
.logo-actions label.btn:focus-within { outline: 3px solid var(--hl); outline-offset: 2px; }
