/* ============================================================
   Debt Record — design system
   Soft mint canvas, white cards, deep teal ink.
   Self-contained: no CDN, no external fonts, no build step.
   ============================================================ */

/* ---------- Tokens ---------------------------------------- */

:root {
  color-scheme: light;

  /* Planes & ink */
  --plane:            #eef4f3;
  --surface:          #ffffff;
  --surface-raised:   #ffffff;
  --surface-sunken:   #f1f5f4;
  --track:            #edf1f0;

  --ink:              #0d2b36;
  --ink-secondary:    #56717a;
  --ink-muted:        #92a6ac;
  --hairline:         rgba(13, 43, 54, 0.07);
  --hairline-strong:  rgba(13, 43, 54, 0.14);
  --gridline:         #e7edec;
  --baseline:         #cbd8d7;

  /* Accent — the teal that runs through the app */
  --accent:           #0d9488;
  --accent-hover:     #0b8177;
  --accent-ink:       #ffffff;
  --accent-wash:      rgba(13, 148, 136, 0.10);
  --accent-ring:      rgba(13, 148, 136, 0.30);

  /* Deep header plane */
  --deep-1:           #17414f;
  --deep-2:           #0e2c38;
  --deep-ink:         #ffffff;
  --deep-ink-soft:    rgba(255, 255, 255, 0.66);

  /* Data series — validated red<->teal pair (see charts.js) */
  --series-debt:      #d92d20;
  --series-payment:   #0d9488;
  --series-debt-wash:    rgba(217, 45, 32, 0.09);
  --series-payment-wash: rgba(13, 148, 136, 0.09);

  /* Status */
  --good:             #0d9488;
  --good-text:        #0b7a70;
  --warning:          #d97706;
  --critical:         #d92d20;

  /* Elevation — soft and low-contrast, never a hard border */
  --shadow-sm: 0 1px 3px rgba(13, 43, 54, 0.05);
  --shadow-md: 0 2px 14px rgba(13, 43, 54, 0.07);
  --shadow-lg: 0 18px 46px rgba(13, 43, 54, 0.18);
  --shadow-accent: 0 8px 22px rgba(13, 148, 136, 0.32);

  /* Geometry — generous, rounded */
  --r-sm: 12px;
  --r-md: 16px;
  --r-lg: 22px;
  --r-xl: 28px;
  --r-full: 999px;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;

  --header-h: 62px;
  --nav-h: 66px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;

    --plane:           #0b1416;
    --surface:         #16211f;
    --surface-raised:  #1d2a28;
    --surface-sunken:  #121c1b;
    --track:           #1e2b29;

    --ink:             #eaf3f1;
    --ink-secondary:   #a8bcbb;
    --ink-muted:       #7d9194;
    --hairline:        rgba(255, 255, 255, 0.08);
    --hairline-strong: rgba(255, 255, 255, 0.16);
    --gridline:        #23302e;
    --baseline:        #33413f;

    --accent:          #14b8a6;
    --accent-hover:    #2dd4bf;
    --accent-ink:      #06201d;
    --accent-wash:     rgba(20, 184, 166, 0.14);
    --accent-ring:     rgba(20, 184, 166, 0.40);

    --deep-1:          #1a4b58;
    --deep-2:          #0c242c;

    --series-debt:     #e2564d;
    --series-payment:  #0fa892;
    --series-debt-wash:    rgba(226, 86, 77, 0.13);
    --series-payment-wash: rgba(15, 168, 146, 0.13);

    --good:            #0fa892;
    --good-text:       #2dd4bf;
    --critical:        #e2564d;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.45);
    --shadow-md: 0 2px 16px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 20px 52px rgba(0, 0, 0, 0.68);
    --shadow-accent: 0 8px 22px rgba(20, 184, 166, 0.28);
  }
}

/* The in-app toggle must beat the OS setting in both directions. */
:root[data-theme="dark"] {
  color-scheme: dark;

  --plane:           #0b1416;
  --surface:         #16211f;
  --surface-raised:  #1d2a28;
  --surface-sunken:  #121c1b;
  --track:           #1e2b29;

  --ink:             #eaf3f1;
  --ink-secondary:   #a8bcbb;
  --ink-muted:       #7d9194;
  --hairline:        rgba(255, 255, 255, 0.08);
  --hairline-strong: rgba(255, 255, 255, 0.16);
  --gridline:        #23302e;
  --baseline:        #33413f;

  --accent:          #14b8a6;
  --accent-hover:    #2dd4bf;
  --accent-ink:      #06201d;
  --accent-wash:     rgba(20, 184, 166, 0.14);
  --accent-ring:     rgba(20, 184, 166, 0.40);

  --deep-1:          #1a4b58;
  --deep-2:          #0c242c;

  --series-debt:     #e2564d;
  --series-payment:  #0fa892;
  --series-debt-wash:    rgba(226, 86, 77, 0.13);
  --series-payment-wash: rgba(15, 168, 146, 0.13);

  --good:            #0fa892;
  --good-text:       #2dd4bf;
  --critical:        #e2564d;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.45);
  --shadow-md: 0 2px 16px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 20px 52px rgba(0, 0, 0, 0.68);
  --shadow-accent: 0 8px 22px rgba(20, 184, 166, 0.28);
}

/* ---------- Reset ----------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--plane);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, h4, p { margin: 0; }
button { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

.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;
}

.num { font-variant-numeric: tabular-nums; }

/* ---------- App frame ------------------------------------- */

.shell {
  max-width: 1140px;
  margin: 0 auto;
  padding: 0 16px calc(var(--nav-h) + 40px);
}

@media (min-width: 900px) {
  .shell { padding: 0 24px 56px; }
}

/* ---------- Top bar -------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: color-mix(in srgb, var(--plane) 86%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
}

.topbar__inner {
  max-width: 1140px;
  margin: 0 auto;
  height: var(--header-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}

@media (min-width: 900px) { .topbar__inner { padding: 0 24px; } }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-right: auto;
  min-width: 0;
}

.brand__mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  display: block;
  flex: none;
}

.brand__name {
  font-size: 15.5px;
  font-weight: 680;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.brand__sub {
  font-size: 12px;
  color: var(--ink-muted);
  white-space: nowrap;
}

@media (max-width: 560px) { .brand__sub { display: none; } }

/* ---------- Buttons -------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: var(--r-full);
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-weight: 620;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
  white-space: nowrap;
}

.btn:hover { background: var(--surface-sunken); }
.btn:active { transform: scale(0.975); }

.btn--primary {
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: var(--shadow-accent);
}
.btn--primary:hover { background: var(--accent-hover); }

.btn--deep {
  background: linear-gradient(140deg, var(--deep-1), var(--deep-2));
  color: #fff;
  box-shadow: var(--shadow-md);
}
.btn--deep:hover { filter: brightness(1.12); }

.btn--ghost { background: transparent; box-shadow: none; }
.btn--ghost:hover { background: var(--track); }

.btn--danger { color: var(--critical); }
.btn--danger:hover { background: var(--series-debt-wash); }

.btn--block { width: 100%; }
.btn--lg { height: 48px; padding: 0 22px; font-size: 15px; }

.icon-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  border: 0;
  border-radius: var(--r-full);
  background: var(--surface);
  color: var(--ink-secondary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
}
.icon-btn:hover { background: var(--surface-sunken); color: var(--ink); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 18px; height: 18px; }

.icon-btn--flat { background: transparent; box-shadow: none; }
.icon-btn--flat:hover { background: var(--track); }

/* Pin toggle */
.pin-btn { color: var(--ink-muted); }
.pin-btn[aria-pressed="true"] { color: var(--accent); background: var(--accent-wash); }
.pin-btn[aria-pressed="true"] svg { fill: currentColor; }

/* ---------- Segmented pill -------------------------------- */

.segmented {
  display: inline-flex;
  padding: 4px;
  gap: 2px;
  background: var(--track);
  border-radius: var(--r-full);
}

.segmented__item {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-secondary);
  padding: 8px 16px;
  border-radius: var(--r-full);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, box-shadow 0.18s ease;
  white-space: nowrap;
}

.segmented__item:hover { color: var(--ink); }

.segmented__item[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}

.segmented--wide { display: flex; width: 100%; }
.segmented--wide .segmented__item { flex: 1; text-align: center; }

/* ---------- Cards ---------------------------------------- */

.card {
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-md);
}

.card__head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 20px 22px 0;
}

.card__title {
  font-size: 15px;
  font-weight: 680;
  letter-spacing: -0.015em;
}

.card__sub {
  font-size: 12.5px;
  color: var(--ink-muted);
  margin-top: 3px;
}

.card__tools { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.card__body { padding: 16px 22px 22px; }

/* ---------- Section headers ------------------------------- */

.section {
  margin: 26px 0 12px;
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.section__title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.015em;
  color: var(--ink);
}

.section__title--eyebrow {
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-muted);
}

.section__note { font-size: 12.5px; color: var(--ink-muted); }
.section__end { margin-left: auto; }

/* ---------- Summary (the hero) ---------------------------- */

.summary { padding: 20px 24px 22px; }

.summary__label {
  font-size: 13px;
  color: var(--ink-muted);
  font-weight: 550;
}

.summary__value {
  font-size: 46px;
  line-height: 1.02;
  font-weight: 760;
  letter-spacing: -0.035em;
  margin-top: 6px;
}

.summary__unit {
  font-size: 19px;
  font-weight: 620;
  color: var(--ink-muted);
  letter-spacing: -0.01em;
  margin-left: 8px;
}

.summary__caption {
  font-size: 13px;
  color: var(--ink-secondary);
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
}

.summary__split {
  display: flex;
  gap: 10px;
  margin-top: 18px;
  flex-wrap: wrap;
}

.split-stat {
  flex: 1 1 140px;
  background: var(--surface-sunken);
  border-radius: var(--r-md);
  padding: 12px 14px;
  min-width: 0;
}

.split-stat__label {
  font-size: 12px;
  color: var(--ink-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.split-stat__value {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-top: 3px;
}

.split-stat__value small {
  font-size: 11.5px;
  font-weight: 550;
  color: var(--ink-muted);
  margin-left: 4px;
  letter-spacing: 0;
}

@media (min-width: 760px) {
  .summary { display: flex; align-items: flex-end; gap: 32px; }
  /* Scoped to the summary card on purpose. The person sheet reuses
     .summary__split inside a COLUMN flex container, where a
     flex-basis of 420px would be read as a height and stretch the
     stat boxes down the sheet. */
  .summary > .summary__split { margin-top: 0; margin-left: auto; flex: 0 1 420px; }
  .summary__value { font-size: 54px; }
}

/* ---------- Stat tiles ----------------------------------- */

.tiles {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
}

.tile {
  background: var(--surface);
  border-radius: var(--r-lg);
  padding: 16px 17px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.tile__label {
  font-size: 12.5px;
  color: var(--ink-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 550;
}

.tile__value {
  font-size: 24px;
  font-weight: 720;
  letter-spacing: -0.025em;
  line-height: 1.15;
}

.tile__value small {
  font-size: 12px;
  font-weight: 550;
  color: var(--ink-muted);
  margin-left: 4px;
  letter-spacing: 0;
}

.tile__foot {
  font-size: 12px;
  color: var(--ink-muted);
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 17px;
}

.tile__spark { margin-top: 4px; height: 30px; }

.delta { display: inline-flex; align-items: center; gap: 3px; font-weight: 620; }
.delta--up   { color: var(--good-text); }
.delta--down { color: var(--critical); }
.delta--flat { color: var(--ink-muted); }

/* Coloured key beside plain text. */
.key-dot {
  width: 9px;
  height: 9px;
  border-radius: 3px;
  flex: none;
  display: inline-block;
}
.key-dot--debt    { background: var(--series-debt); }
.key-dot--payment { background: var(--series-payment); }
.key-dot--muted   { background: var(--baseline); }

/* ---------- Meter ---------------------------------------- */

.meter {
  height: 8px;
  border-radius: var(--r-full);
  background: var(--accent-wash);
  overflow: hidden;
}

.meter__fill {
  height: 100%;
  border-radius: var(--r-full);
  background: var(--accent);
  transition: width 0.55s cubic-bezier(0.16, 1, 0.3, 1);
}

/* ---------- Toolbar / filters ---------------------------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 16px 0;
}

.toolbar--tight { margin-top: -6px; }
.toolbar__spacer { margin-left: auto; }

/* The add-person button is redundant on phones — the nav's + button
   and the account sheet both lead to the same place. */
@media (max-width: 620px) {
  .desktop-only { display: none !important; }
}

.search {
  display: flex;
  align-items: center;
  gap: 9px;
  height: 42px;
  padding: 0 16px;
  background: var(--surface);
  border-radius: var(--r-full);
  box-shadow: var(--shadow-sm);
  flex: 1 1 180px;
  min-width: 0;
  max-width: 360px;
}

.search svg { width: 16px; height: 16px; color: var(--ink-muted); flex: none; }

.search input {
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  width: 100%;
  min-width: 0;
  outline: none;
}

.search input::placeholder { color: var(--ink-muted); }
.search input::-webkit-search-cancel-button { filter: grayscale(1) opacity(0.5); }

.count-pill {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-secondary);
  background: var(--track);
  border-radius: var(--r-full);
  padding: 6px 13px;
  white-space: nowrap;
}

/* Stays right-aligned even when it wraps onto its own line. */
.count-pill--end { margin-left: auto; }

/* ---------- Lists ---------------------------------------- */

.list { display: flex; flex-direction: column; gap: 8px; }

.row {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 12px 14px;
  background: var(--surface);
  border: 0;
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.15s ease;
  text-align: left;
  width: 100%;
  appearance: none;
  font: inherit;
  color: inherit;
}

.row:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }
.row:active { transform: scale(0.995); }

.row--static { cursor: default; }
.row--static:hover { transform: none; box-shadow: var(--shadow-sm); }

/* An archived person is present but visibly out of play. */
.row--archived { background: var(--surface-sunken); box-shadow: none; }
.row--archived .avatar { opacity: 0.55; }
.row--archived .row__title { color: var(--ink-secondary); }
.row--archived .row__title svg { color: var(--ink-muted); }

/* The install button only appears once the browser says the app
   is installable, and never on phones (the nav sheet covers it). */
.install-only { display: none; }
body.can-install .install-only { display: inline-grid; }

@media (max-width: 620px) {
  body.can-install .install-only,
  #exportButton { display: none; }
}

/* Checkbox + explanation, used by the export dialog. */
.switch-row {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 12px 14px;
  border-radius: var(--r-md);
  background: var(--surface-sunken);
  cursor: pointer;
}

.switch-row input {
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  accent-color: var(--accent);
  flex: none;
  cursor: pointer;
}

.switch-row > span { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.switch-row__title { font-size: 13.5px; font-weight: 600; }
.switch-row__note { font-size: 11.5px; color: var(--ink-muted); line-height: 1.4; }

/* Explains a scope the numbers depend on. */
.notice {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 12px 14px;
  margin-bottom: 4px;
  border-radius: var(--r-md);
  background: var(--accent-wash);
  color: var(--ink-secondary);
  font-size: 12.5px;
  line-height: 1.45;
}

.notice svg { width: 16px; height: 16px; flex: none; color: var(--accent); margin-top: 1px; }

.avatar {
  width: 44px;
  height: 44px;
  border-radius: var(--r-full);
  display: grid;
  place-items: center;
  font-size: 15px;
  font-weight: 680;
  color: #fff;
  flex: none;
  letter-spacing: -0.01em;
}

.avatar--lg { width: 64px; height: 64px; font-size: 22px; }
.avatar--sm { width: 34px; height: 34px; font-size: 12.5px; }

.row__main { flex: 1; min-width: 0; }

.row__title {
  font-size: 15px;
  font-weight: 640;
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}

.row__title svg { width: 13px; height: 13px; color: var(--accent); flex: none; }

.row__meta {
  font-size: 12.5px;
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}

.row__end {
  text-align: right;
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
}

/* Caption above the amount states the direction in words, so the
   colour is reinforcement rather than the only cue. */
.row__caption {
  font-size: 11px;
  color: var(--ink-muted);
  font-weight: 500;
}

.row__amount {
  font-size: 15.5px;
  font-weight: 700;
  letter-spacing: -0.015em;
  font-variant-numeric: tabular-nums;
}

.row__amount small { font-size: 11px; font-weight: 550; opacity: 0.7; margin-left: 3px; }

.amount--debt    { color: var(--series-debt); }
.amount--payment { color: var(--series-payment); }
.amount--zero    { color: var(--ink-muted); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  font-weight: 620;
  padding: 4px 10px;
  border-radius: var(--r-full);
  background: var(--track);
  color: var(--ink-secondary);
  white-space: nowrap;
}

.chip--debt    { background: var(--series-debt-wash);    color: var(--series-debt); }
.chip--payment { background: var(--series-payment-wash); color: var(--series-payment); }
.chip--settled { background: var(--track); color: var(--ink-secondary); }

.chip svg { width: 12px; height: 12px; flex: none; }

.entry-icon {
  width: 42px;
  height: 42px;
  border-radius: var(--r-full);
  display: grid;
  place-items: center;
  flex: none;
}
.entry-icon svg { width: 18px; height: 18px; }
.entry-icon--debt    { background: var(--series-debt-wash);    color: var(--series-debt); }
.entry-icon--payment { background: var(--series-payment-wash); color: var(--series-payment); }

/* ---------- Empty & skeleton ----------------------------- */

.empty {
  text-align: center;
  padding: 52px 24px;
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-sm);
}

.empty__art {
  width: 74px;
  height: 74px;
  margin: 0 auto 16px;
  border-radius: var(--r-full);
  background: var(--track);
  display: grid;
  place-items: center;
  color: var(--ink-muted);
}

.empty__art svg { width: 32px; height: 32px; }
.empty__title { font-size: 16px; font-weight: 680; letter-spacing: -0.015em; }
.empty__text { font-size: 13.5px; color: var(--ink-muted); margin-top: 5px; }
.empty__action { margin-top: 18px; }

.skeleton {
  background: var(--surface);
  border-radius: var(--r-lg);
  height: 70px;
  position: relative;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, var(--track), transparent);
  animation: sweep 1.3s ease-in-out infinite;
}

@keyframes sweep {
  from { transform: translateX(-100%); }
  to   { transform: translateX(100%); }
}

/* Refetch holds the previous render instead of flashing a skeleton. */
.is-refreshing { opacity: 0.55; transition: opacity 0.15s ease; }

/* ---------- Tabs / panels -------------------------------- */

.panel[hidden] { display: none; }

/* ---------- Charts --------------------------------------- */

.chart { width: 100%; display: block; overflow: visible; }
.chart text { font-family: var(--font); }

.chart__grid line   { stroke: var(--gridline); stroke-width: 1; shape-rendering: crispEdges; }
.chart__axis line   { stroke: var(--baseline); stroke-width: 1; shape-rendering: crispEdges; }
.chart__tick        { fill: var(--ink-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart__label       { fill: var(--ink-secondary); font-size: 11.5px; font-weight: 620; }
.chart__hit         { fill: transparent; cursor: pointer; }
.chart__crosshair line { stroke: var(--hairline-strong); stroke-width: 1; }
.chart__bar         { transition: opacity 0.12s ease; }
.chart__bar--dim    { opacity: 0.4; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  align-items: center;
  font-size: 12.5px;
  color: var(--ink-secondary);
}

.legend__item { display: inline-flex; align-items: center; gap: 6px; }
.legend__swatch { width: 12px; height: 9px; border-radius: 3px; flex: none; }
.legend__line   { width: 14px; height: 2px; border-radius: 2px; flex: none; }

.tooltip {
  position: fixed;
  z-index: 60;
  min-width: 148px;
  max-width: 250px;
  padding: 10px 12px;
  background: var(--surface-raised);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-lg);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s ease;
}

.tooltip[data-open="true"] { opacity: 1; }

.tooltip__head {
  font-size: 11.5px;
  color: var(--ink-muted);
  font-weight: 620;
  margin-bottom: 7px;
}

.tooltip__row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 12.5px;
}

.tooltip__row + .tooltip__row { margin-top: 4px; }
.tooltip__key { width: 14px; height: 2px; border-radius: 2px; flex: none; align-self: center; }
.tooltip__val { font-weight: 700; font-variant-numeric: tabular-nums; margin-left: auto; }
.tooltip__name { color: var(--ink-secondary); }

/* ---------- Data table (every chart's twin) -------------- */

.table-wrap { overflow-x: auto; }

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

table.data th, table.data td {
  padding: 9px 10px;
  text-align: right;
  border-bottom: 1px solid var(--hairline);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

table.data th {
  font-size: 11px;
  font-weight: 700;
  color: var(--ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

table.data th:first-child, table.data td:first-child { text-align: left; font-variant-numeric: normal; }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tfoot td { font-weight: 700; border-top: 1px solid var(--hairline-strong); border-bottom: 0; }

/* ---------- Forms ---------------------------------------- */

.field { display: flex; flex-direction: column; gap: 7px; }

.field__label { font-size: 12.5px; font-weight: 620; color: var(--ink-secondary); }
.field__hint { font-size: 11.5px; color: var(--ink-muted); line-height: 1.4; }

.input {
  height: 46px;
  padding: 0 14px;
  background: var(--surface-sunken);
  border: 1.5px solid transparent;
  border-radius: var(--r-md);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  width: 100%;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.input:focus {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 4px var(--accent-ring);
}

.input::placeholder { color: var(--ink-muted); }

.input--amount {
  font-size: 30px;
  font-weight: 740;
  letter-spacing: -0.03em;
  height: 66px;
  padding-right: 66px;
  font-variant-numeric: tabular-nums;
}

.input-affix { position: relative; }

.input-affix__unit {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  font-weight: 640;
  color: var(--ink-muted);
  pointer-events: none;
}

.field-error {
  font-size: 12.5px;
  color: var(--critical);
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 550;
}

.field-error svg { width: 14px; height: 14px; flex: none; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 460px) { .grid-2 { grid-template-columns: 1fr; } }

/* Type switch inside the entry dialog */
.type-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; position: relative; }

.type-switch__option {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 50px;
  border-radius: var(--r-md);
  background: var(--surface-sunken);
  font-size: 14.5px;
  font-weight: 640;
  color: var(--ink-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
  box-shadow: inset 0 0 0 1.5px transparent;
}

.type-switch__option svg { width: 17px; height: 17px; }
.type-switch input { position: absolute; opacity: 0; pointer-events: none; }

.type-switch input:checked + .type-switch__option--debt {
  box-shadow: inset 0 0 0 1.5px var(--series-debt);
  background: var(--series-debt-wash);
  color: var(--series-debt);
}
.type-switch input:checked + .type-switch__option--payment {
  box-shadow: inset 0 0 0 1.5px var(--series-payment);
  background: var(--series-payment-wash);
  color: var(--series-payment);
}
.type-switch input:focus-visible + .type-switch__option { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Colour picker */
.swatches { display: flex; flex-wrap: wrap; gap: 9px; }

.swatch {
  width: 32px;
  height: 32px;
  border-radius: var(--r-full);
  border: 0;
  cursor: pointer;
  position: relative;
  padding: 0;
  transition: transform 0.12s ease;
}

.swatch:hover { transform: scale(1.12); }

.swatch[aria-pressed="true"]::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: var(--r-full);
  border: 2.5px solid var(--ink);
}

/* ---------- Dialogs -------------------------------------- */

.scrim {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(9, 30, 38, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fade 0.16s ease;
}

.scrim[hidden] { display: none; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.dialog {
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 440px;
  max-height: min(90vh, 800px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: pop 0.22s cubic-bezier(0.16, 1, 0.3, 1);
  /* As a flex child its default min-width is `auto`, so a wide
     nowrap descendant would stretch it past the viewport. */
  min-width: 0;
}

.dialog--wide { max-width: 520px; }

@keyframes pop {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

.dialog__head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 22px 22px 16px;
}

.dialog__title { font-size: 19px; font-weight: 700; letter-spacing: -0.025em; }
.dialog__sub { font-size: 13px; color: var(--ink-muted); margin-top: 3px; }

/* The header and footer hold their height; only the body scrolls. */
.dialog__grab, .dialog__head, .dialog__foot { flex: none; }

.dialog__body {
  padding: 0 22px 22px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1 1 auto;
  min-height: 0;
}

.dialog__foot {
  display: flex;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--hairline);
}
.dialog__foot .btn { flex: 1; }

.dialog__grab { display: none; }

/* On phones the dialog becomes a bottom sheet. */
@media (max-width: 620px) {
  .scrim { align-items: flex-end; padding: 0; }

  .dialog {
    max-width: none;
    max-height: 94vh;
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    animation: sheet 0.28s cubic-bezier(0.16, 1, 0.3, 1);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .dialog__grab { display: block; padding: 10px 0 2px; position: relative; z-index: 2; }
  .dialog__grab span {
    display: block;
    width: 40px;
    height: 4px;
    border-radius: var(--r-full);
    background: var(--baseline);
    margin: 0 auto;
  }

  @keyframes sheet { from { transform: translateY(100%); } to { transform: none; } }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ---------- Person detail: the deep header --------------- */

.detail { display: flex; flex-direction: column; min-height: 0; }

.deep {
  position: relative;
  background: linear-gradient(145deg, var(--deep-1), var(--deep-2));
  color: var(--deep-ink);
  padding: 20px 22px 24px;
  overflow: hidden;
  /* Keep its natural height. As a flex item it would otherwise shrink
     to make room for a long ledger and, because it clips its overflow,
     silently cut off the action buttons along its bottom edge. */
  flex: none;
}

/* Faint diagonal texture, purely decorative. */
.deep::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(115deg, rgba(255,255,255,0.06) 1px, transparent 1px),
    linear-gradient(65deg, rgba(255,255,255,0.05) 1px, transparent 1px);
  background-size: 38px 38px, 52px 52px;
  pointer-events: none;
}

.deep > * { position: relative; z-index: 1; }

.deep__bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.deep__name {
  font-size: 15.5px;
  font-weight: 660;
  letter-spacing: -0.015em;
  flex: 1;
  min-width: 0;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.deep__btn {
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: var(--r-full);
  background: rgba(255, 255, 255, 0.12);
  color: var(--deep-ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
  transition: background 0.15s ease;
}

.deep__btn:hover { background: rgba(255, 255, 255, 0.22); }
.deep__btn[aria-pressed="true"] { background: #fff; color: var(--deep-2); }
.deep__btn[aria-pressed="true"] svg { fill: currentColor; }
.deep__btn svg { width: 16px; height: 16px; }

.deep__flag {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin: 0 auto 12px;
  padding: 5px 12px;
  width: fit-content;
  max-width: 100%;
  border-radius: var(--r-full);
  background: rgba(255, 255, 255, 0.14);
  color: var(--deep-ink);
  font-size: 11.5px;
  font-weight: 560;
  text-align: center;
}

.deep__flag svg { width: 13px; height: 13px; flex: none; }

.deep__label { font-size: 12.5px; color: var(--deep-ink-soft); text-align: center; }

.deep__value {
  font-size: 38px;
  font-weight: 760;
  letter-spacing: -0.035em;
  text-align: center;
  margin-top: 2px;
  line-height: 1.1;
}

.deep__value small { font-size: 16px; font-weight: 620; color: var(--deep-ink-soft); margin-left: 6px; letter-spacing: 0; }

.deep__meta {
  font-size: 12px;
  color: var(--deep-ink-soft);
  text-align: center;
  margin-top: 6px;
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.deep__actions {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin-top: 20px;
}

.deep__action {
  border: 0;
  background: transparent;
  color: var(--deep-ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  font-size: 11.5px;
  font-weight: 560;
  line-height: 1.25;
  width: 82px;
  padding: 0;
}

.deep__action span:first-child {
  width: 46px;
  height: 46px;
  border-radius: var(--r-full);
  background: rgba(255, 255, 255, 0.14);
  display: grid;
  place-items: center;
  transition: background 0.15s ease, transform 0.12s ease;
}

.deep__action:hover span:first-child { background: rgba(255, 255, 255, 0.26); }
.deep__action:active span:first-child { transform: scale(0.93); }
.deep__action svg { width: 19px; height: 19px; }

.detail__body {
  padding: 18px 22px 22px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 14px;
  /* Takes whatever height the header leaves and scrolls inside it. */
  flex: 1 1 auto;
  min-height: 0;
}

/* Blocks inside the sheet keep their own height; only the sheet's
   body scrolls. Without this a column flex parent stretches them. */
.detail__body > * { flex: none; }

/* On short screens the header is compacted rather than clipped, so the
   ledger always has room to scroll beneath it. */
@media (max-height: 700px) {
  .deep { padding: 14px 22px 16px; }
  .deep__bar { margin-bottom: 10px; }
  .deep__value { font-size: 30px; }
  .deep__flag { margin-bottom: 8px; }
  .deep__actions { gap: 14px; margin-top: 14px; }
  .deep__action span:first-child { width: 40px; height: 40px; }
  .deep__action svg { width: 17px; height: 17px; }
  .detail__body { padding-top: 14px; }
}

.ledger { display: flex; flex-direction: column; gap: 4px; }

.ledger__item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--hairline);
}

.ledger__item:last-child { border-bottom: 0; }
.ledger__main { flex: 1; min-width: 0; }
.ledger__title { font-size: 14px; font-weight: 620; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ledger__date { font-size: 11.5px; color: var(--ink-muted); margin-top: 1px; }
.ledger__end { text-align: right; flex: none; }
.ledger__amount { font-size: 14.5px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ledger__tools { display: flex; gap: 2px; }

.ledger__tools .icon-btn {
  width: 30px;
  height: 30px;
  background: transparent;
  box-shadow: none;
  color: var(--ink-muted);
}
.ledger__tools .icon-btn:hover { background: var(--track); color: var(--ink); }
.ledger__tools .icon-btn svg { width: 15px; height: 15px; }

/* ---------- Bottom nav (mobile) -------------------------- */

.bottomnav {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 40;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(16px) saturate(180%);
  border-top: 1px solid var(--hairline);
  display: flex;
  align-items: center;
}

@media (min-width: 900px) { .bottomnav { display: none; } }

.bottomnav__item {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 10px;
  font-weight: 620;
  cursor: pointer;
  height: 100%;
  padding: 0;
}

.bottomnav__item svg { width: 21px; height: 21px; }
.bottomnav__item[aria-selected="true"] { color: var(--accent); }

.bottomnav__add {
  flex: none;
  width: 56px;
  display: grid;
  place-items: center;
}

.bottomnav__add button {
  width: 50px;
  height: 50px;
  border: 0;
  border-radius: var(--r-full);
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: var(--shadow-accent);
  transition: transform 0.14s ease, background 0.14s ease;
}

.bottomnav__add button:hover { background: var(--accent-hover); }
.bottomnav__add button:active { transform: scale(0.93); }
.bottomnav__add svg { width: 22px; height: 22px; }

.tabs-desktop { display: none; }
@media (min-width: 900px) { .tabs-desktop { display: inline-flex; } }

/* Desktop-only floating add button */
.fab {
  display: none;
  position: fixed;
  right: 28px;
  bottom: 28px;
  z-index: 41;
  height: 52px;
  padding: 0 22px;
  border: 0;
  border-radius: var(--r-full);
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 15px;
  font-weight: 650;
  align-items: center;
  gap: 9px;
  box-shadow: var(--shadow-accent);
  cursor: pointer;
  transition: transform 0.14s ease, background 0.14s ease;
}

@media (min-width: 900px) { .fab { display: inline-flex; } }
.fab:hover { background: var(--accent-hover); transform: translateY(-2px); }
.fab:active { transform: scale(0.97); }
.fab svg { width: 19px; height: 19px; }

/* ---------- Toasts -------------------------------------- */

.toasts {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom));
  z-index: 70;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
  width: min(420px, calc(100vw - 32px));
}

@media (min-width: 900px) { .toasts { bottom: 28px; left: 28px; transform: none; align-items: flex-start; } }

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface-raised);
  border-radius: var(--r-full);
  box-shadow: var(--shadow-lg);
  font-size: 13.5px;
  font-weight: 580;
  animation: toastIn 0.24s cubic-bezier(0.16, 1, 0.3, 1);
  max-width: 100%;
}

.toast svg { width: 17px; height: 17px; flex: none; }
.toast--ok svg { color: var(--good); }
.toast--error svg { color: var(--critical); }
.toast[data-leaving="true"] { animation: toastOut 0.18s ease forwards; }

@keyframes toastIn  { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateY(8px); } }

/* ---------- Balance privacy ------------------------------ */

body.privacy [data-money],
html.privacy-boot body [data-money] {
  filter: blur(7px);
  user-select: none;
}

body.privacy .chart-host,
body.privacy table.data td,
body.privacy .tile__spark {
  filter: blur(6px);
  user-select: none;
}

/* ---------- Compact select ------------------------------- */

select.input,
.input--compact {
  height: 42px;
  font-size: 14px;
  padding: 0 14px;
}

select.input {
  appearance: none;
  padding-right: 34px;
  border-radius: var(--r-full);
  background-color: var(--surface);
  box-shadow: var(--shadow-sm);
  border-color: transparent;
  font-weight: 570;
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-muted) 50%),
                    linear-gradient(135deg, var(--ink-muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 19px, calc(100% - 13px) 19px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
  flex: 0 1 200px;
  min-width: 132px;
}

/* ---------- Stats layout --------------------------------- */

.charts-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr;
}

@media (min-width: 1000px) {
  .charts-grid { grid-template-columns: 1fr 1fr; }
}

.chart-host { min-height: 250px; }

/* ---------- Print / forced colors ------------------------ */

@media (forced-colors: active) {
  .card, .tile, .row, .dialog { border: 1px solid CanvasText; }
  .chart__bar { forced-color-adjust: none; }
}
