:root {
  --paper: #f4efe4;
  --card: #fffdf8;
  --ink: #1f2a24;
  --muted: #6b7068;
  --line: #e2dacb;
  --accent: #2f5d46;
  --accent-ink: #fff;
  --warn: #b5532f;
  --pos: #2f6f4a;
  --hl: #f3e3b5;
  --shadow: 0 1px 2px rgba(31, 42, 36, .06), 0 8px 24px rgba(31, 42, 36, .06);
  color-scheme: light;
}
/* Dark: follows the system unless she picks one with the toggle (data-theme on <html>). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #151a17; --card: #1d2420; --ink: #ece6d8; --muted: #9aa096; --line: #2f3832;
    --accent: #7fb894; --accent-ink: #10160f; --warn: #e08a66; --pos: #8fd0a5; --hl: #3a3420;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #151a17; --card: #1d2420; --ink: #ece6d8; --muted: #9aa096; --line: #2f3832;
  --accent: #7fb894; --accent-ink: #10160f; --warn: #e08a66; --pos: #8fd0a5; --hl: #3a3420;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  color-scheme: dark;
}
.theme-btn { width: 40px; padding: 8px 0; text-align: center; }

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 15px/1.45 "IBM Plex Sans", system-ui, sans-serif;
}
h1, h2, h3 { font-family: Fraunces, Georgia, serif; font-weight: 650; margin: 0; letter-spacing: -.01em; }
button, select, input { font: inherit; color: inherit; }

.btn {
  border: 1px solid var(--line); background: var(--card); padding: 8px 14px; border-radius: 8px;
  cursor: pointer; font-weight: 500; list-style: none;
}
.btn::-webkit-details-marker { display: none; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 4px; font-weight: 600; }
select, input[type=search], input[type=text] {
  border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 7px 10px;
}

/* Header */
.top {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 24px; border-bottom: 1px solid var(--line); background: var(--card);
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand h1 { font-size: 22px; }
.brand p { margin: 0; color: var(--muted); font-size: 13px; }
.mark {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-weight: 700;
}
.top-actions { display: flex; gap: 8px; align-items: center; }
.menu { position: relative; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 10; min-width: 220px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  padding: 6px; display: grid;
}
.menu-pop button { background: none; border: 0; text-align: left; padding: 9px 10px; border-radius: 6px; cursor: pointer; }
.menu-pop button:hover { background: var(--paper); }

/* Layout */
.layout { display: grid; grid-template-columns: 290px 1fr; min-height: calc(100vh - 68px); }
.side { border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; gap: 10px; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 0 8px; }
.side-head h2 { font-size: 17px; }
.nav-item {
  display: grid; grid-template-columns: 10px 1fr auto; gap: 10px; align-items: center; width: 100%;
  background: none; border: 0; border-radius: 8px; padding: 6px 10px; cursor: pointer; text-align: left; font-size: 14px;
}
.nav-item:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
.nav-item.active { background: var(--accent); color: var(--accent-ink); }
.nav-item.active .sum, .nav-item.active .name small { color: inherit; opacity: .85; }
.nav-item .dot { width: 9px; height: 9px; border-radius: 50%; }
.nav-item.active .dot { box-shadow: 0 0 0 2px var(--accent-ink); }
.nav-item .sum { font-family: "IBM Plex Mono", monospace; font-size: 12.5px; color: var(--muted); }
.nav-item .name small { color: var(--muted); }
.nav-item.flag { background: color-mix(in srgb, var(--warn) 13%, transparent); font-weight: 600; }
.nav-item.flag .sum { color: var(--warn); font-weight: 700; }
.nav-item.flag.active { background: var(--accent); }
.nav-item.flag.active .sum { color: inherit; }
.nav-item.done .sum { color: var(--pos); }
.grand {
  margin-top: auto; display: flex; justify-content: space-between; align-items: baseline;
  padding: 14px 10px 4px; border-top: 2px solid var(--ink);
}
.grand strong { font-family: "IBM Plex Mono", monospace; font-size: 18px; }

.main { padding: 20px 24px 40px; min-width: 0; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar h2 { font-size: 24px; }
#search { width: min(320px, 100%); }

.bulk {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 10px 12px; margin-bottom: 12px;
  background: var(--hl); border-radius: 10px;
}
.bulk[hidden] { display: none; }

/* Table */
table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: color-mix(in srgb, var(--paper) 60%, transparent); }
tbody tr.sel { background: var(--hl); }
.chk { width: 36px; }
.num { text-align: right; font-family: "IBM Plex Mono", monospace; white-space: nowrap; }
td.date { white-space: nowrap; color: var(--muted); font-family: "IBM Plex Mono", monospace; font-size: 13px; }
td.desc { word-break: break-word; }
td.desc small { display: block; color: var(--muted); font-size: 12px; }
.amt-in { color: var(--pos); }
td select { width: 100%; max-width: 230px; }
td select.unset { color: var(--muted); border-style: dashed; }
td.grp select { max-width: 190px; font-size: 13px; }
td.grp select.auto { color: var(--muted); background: transparent; }
.grp-in { color: var(--pos); font-size: 13px; font-weight: 500; }
.toast-btn {
  margin-left: 10px; background: none; border: 1px solid currentColor; color: inherit;
  border-radius: 6px; padding: 3px 10px; font-weight: 600; cursor: pointer;
}
.foot { color: var(--muted); font-size: 13px; }

.empty { display: grid; place-items: center; padding: 60px 0; }
.empty[hidden] { display: none; }
.empty-card {
  text-align: center; max-width: 440px; padding: 40px 32px; border: 2px dashed var(--line); border-radius: 16px; background: var(--card);
}
.empty-card p { color: var(--muted); }

.drop-overlay {
  position: fixed; inset: 0; background: color-mix(in srgb, var(--accent) 22%, transparent); z-index: 50;
  display: grid; place-items: center; pointer-events: none;
}
.drop-overlay[hidden] { display: none; }
.drop-overlay span { background: var(--card); padding: 18px 28px; border-radius: 12px; font: 600 20px Fraunces, serif; box-shadow: var(--shadow); }

/* Dialogs */
dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 48px);
  background: var(--card); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(20, 26, 22, .45); }
.dlg-head { padding: 20px 22px 6px; }
.dlg-head p { color: var(--muted); margin: 6px 0 0; font-size: 14px; }
.dlg-body { padding: 12px 22px; overflow: auto; max-height: 60vh; }
.dlg-foot { padding: 14px 22px 20px; display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); }
.map-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 14px; }
.map-grid label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.map-grid select { width: 100%; }
.radio-row { display: flex; gap: 16px; flex-wrap: wrap; margin: 4px 0 12px; font-size: 14px; }
.preview { width: 100%; font-size: 13px; box-shadow: none; border: 1px solid var(--line); }
.preview td, .preview th { padding: 6px 8px; }
.tag { font-size: 11px; padding: 2px 7px; border-radius: 99px; background: var(--paper); color: var(--muted); font-weight: 600; }
.tag.in { color: var(--pos); }

.cat-row { display: grid; grid-template-columns: 34px 1fr auto auto; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); }
.cat-row input[type=color] { width: 30px; height: 30px; border: 0; padding: 0; background: none; cursor: pointer; }
.cat-row input[type=text] { width: 100%; }
.cat-row .ctl { display: flex; gap: 2px; }
.cat-row .ctl button { background: none; border: 1px solid transparent; border-radius: 6px; padding: 4px 7px; cursor: pointer; color: var(--muted); }
.cat-row .ctl button:hover { border-color: var(--line); color: var(--ink); }
.cat-row .ctl button.danger.armed { color: #fff; background: var(--warn); }
.cat-row label { font-size: 12px; color: var(--muted); white-space: nowrap; display: flex; gap: 4px; align-items: center; }

/* Receipts */
dialog.wide { width: min(940px, calc(100vw - 32px)); }
.rcpt { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 20px; align-items: start; max-height: 66vh; }
.rcpt img { width: 100%; max-height: 62vh; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
.rcpt-fields { display: grid; gap: 10px; }
.rcpt-fields label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.rcpt-fields input, .rcpt-fields select { width: 100%; font-weight: 400; }
.rcpt-fields input[type=date], .rcpt-fields input[type=number] {
  border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 7px 10px;
}
.rcpt-fields .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.match { border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px 10px; margin: 4px 0 0; display: grid; gap: 6px; }
.match legend { font-size: 13px; font-weight: 600; padding: 0 4px; }
.match .opt { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; font-weight: 400; }
.match .opt input { width: auto; margin-top: 3px; }
.status { min-height: 1.4em; }
.rcpt-info { margin: 0; display: grid; gap: 10px; }
.rcpt-info dt { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.rcpt-info dd { margin: 2px 0 0; }
.dlg-foot .btn.armed { background: var(--warn); border-color: var(--warn); color: #fff; }
.chip {
  display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 99px; font-size: 11px; font-weight: 600;
  border: 1px solid var(--accent); color: var(--accent); background: none; cursor: pointer; vertical-align: 1px;
}
.chip:hover { background: var(--accent); color: var(--accent-ink); }
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60; max-width: calc(100vw - 32px);
  background: var(--ink); color: var(--paper); padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow); font-size: 14px;
}
.toast[hidden] { display: none; }

/* Sidebar = three cards: Overview (plain), Spending groups (auto), Your categories (hers) */
.side { gap: 12px; }
#catNav { display: grid; gap: 12px; align-content: start; }
.nav-card { border: 1px solid var(--line); border-radius: 14px; padding: 8px; background: var(--card); }
.nav-card.auto { background: transparent; border-style: dashed; }
.nav-card.mine { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); box-shadow: inset 3px 0 0 var(--accent); }
.nav-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 6px 6px; }
.nav-toggle {
  display: flex; align-items: center; gap: 7px; background: none; border: 0; padding: 2px 0; cursor: pointer;
  font: 700 11.5px "IBM Plex Sans", sans-serif; letter-spacing: .09em; text-transform: uppercase; color: var(--muted);
  text-align: left; min-width: 0; white-space: nowrap;
}
.nav-toggle.static { cursor: default; }
.nav-toggle:hover:not(.static) { color: var(--ink); }
.nav-toggle .chev { width: 12px; height: 12px; flex: none; transition: transform .18s ease; }
.nav-card.shut .chev { transform: rotate(-90deg); }
.nav-card.shut .nav-body { display: none; }
.nav-card.shut .nav-head { padding-bottom: 4px; }
.nav-card.mine .nav-toggle { color: var(--accent); }
.nav-badge {
  font: 700 10px "IBM Plex Sans", sans-serif; letter-spacing: .06em; text-transform: uppercase; font-style: normal;
  padding: 2px 7px; border-radius: 99px; background: color-mix(in srgb, var(--muted) 18%, transparent); color: var(--muted);
}
.nav-card.mine .nav-badge { background: var(--accent); color: var(--accent-ink); }
.nav-head .link { font: 600 13px "IBM Plex Sans", sans-serif; padding: 2px 6px; }
.nav-body { display: grid; gap: 1px; }
.nav-rule { height: 1px; background: var(--line); margin: 5px 8px; }
.stats { display: flex; gap: 28px; margin: -2px 0 14px; padding: 12px 16px; background: var(--card); border-radius: 12px; box-shadow: var(--shadow); }
.stats[hidden] { display: none; }
.stats span { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.stats strong { font: 500 22px "IBM Plex Mono", monospace; }

/* Phone-only pieces, hidden on desktop */
.nav-btn, .scrim, .fab, .sm, .snap-only { display: none; }
.empty-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* Touch devices (phones, tablets) get the camera shortcut. */
@media (pointer: coarse), (max-width: 760px) {
  .snap-only { display: inline-flex; }
  .fab {
    display: inline-flex; align-items: center; gap: 8px; position: fixed; z-index: 40;
    right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
    padding: 14px 20px; border: 0; border-radius: 999px; cursor: pointer;
    background: var(--accent); color: var(--accent-ink); font: 600 16px "IBM Plex Sans", sans-serif;
    box-shadow: 0 6px 20px rgba(20, 30, 24, .28);
  }
  .fab svg { width: 22px; height: 22px; }
  body.has-selection .fab, body.drawer-open .fab { display: none; }
  .toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
}

/* Phone */
@media (max-width: 760px) {
  body { font-size: 15px; }
  .top { padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top)); gap: 8px; }
  .brand p { display: none; }
  .brand h1 { font-size: 18px; }
  .mark { width: 30px; height: 30px; border-radius: 8px; }
  .top-actions { gap: 6px; }
  .top-actions .btn, .top-actions select { padding: 7px 10px; }
  #yearSel { max-width: 120px; }
  .lg { display: none; }
  .sm { display: inline; }

  /* Sidebar becomes a slide-out menu */
  .layout { display: block; }
  .side {
    position: fixed; z-index: 70; top: 0; bottom: 0; left: 0; width: min(330px, 86vw);
    background: var(--paper); border-right: 1px solid var(--line); overflow-y: auto;
    padding: calc(18px + env(safe-area-inset-top)) 14px calc(18px + env(safe-area-inset-bottom));
    transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow);
  }
  .drawer-open .side { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; z-index: 65; background: rgba(15, 20, 17, .45); opacity: 0; pointer-events: none; transition: opacity .2s; }
  .drawer-open .scrim { opacity: 1; pointer-events: auto; }
  .drawer-open { overflow: hidden; }
  .nav-item { padding: 10px 10px; }

  .main { padding: 14px 16px calc(96px + env(safe-area-inset-bottom)); }
  .toolbar { margin-bottom: 12px; gap: 10px; }
  .toolbar h2 { display: none; }
  .nav-btn {
    display: inline-flex; align-items: center; gap: 8px; max-width: 100%; font-weight: 600;
    font-family: Fraunces, Georgia, serif; font-size: 17px; padding: 8px 12px;
  }
  .nav-btn svg { width: 18px; height: 18px; flex: none; }
  .nav-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #search { width: 100%; }
  input, select, textarea { font-size: 16px; } /* below 16px iOS zooms in on focus */

  /* Transactions as cards */
  table, tbody { display: block; }
  thead { display: none; }
  tbody tr { display: grid; grid-template-columns: 26px 1fr 1fr; gap: 4px 10px; padding: 12px; border-bottom: 1px solid var(--line); }
  td { border: 0; padding: 0; }
  td.chk { grid-row: 1 / span 2; padding-top: 2px; }
  td.date { grid-column: 2; }
  td.num { grid-column: 3; grid-row: 1; font-size: 16px; }
  td.desc { grid-column: 2 / 4; }
  td.desc .dtext { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.35; }
  td.grp { grid-column: 2; margin-top: 4px; }
  td.cat { grid-column: 3; margin-top: 4px; }
  td select, td.grp select { max-width: none; width: 100%; font-size: 14px; padding: 8px; }
  .grp-in { display: block; padding-top: 9px; }

  .bulk {
    position: fixed; z-index: 45; left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom));
    margin: 0; box-shadow: 0 8px 28px rgba(0, 0, 0, .25); border-radius: 14px;
  }
  .bulk select { flex: 1; min-width: 0; }

  /* Dialogs become full-screen sheets with the buttons always in reach */
  dialog, dialog.wide { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  dialog[open] { display: flex; flex-direction: column; }
  dialog > form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
  .dlg-head { padding: calc(16px + env(safe-area-inset-top)) 16px 6px; }
  .dlg-body { flex: 1; min-height: 0; max-height: none; padding: 10px 16px; }
  .dlg-foot { padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); background: var(--card); }
  .dlg-foot .btn { flex: 1; padding: 12px; font-size: 16px; }
  .dlg-foot > span { display: flex; gap: 8px; flex: 1; }

  .rcpt { grid-template-columns: 1fr; max-height: none; gap: 14px; }
  .rcpt img { max-height: 30vh; }
  .rcpt-fields input, .rcpt-fields select { padding: 10px; }

  .cat-row { grid-template-columns: 34px 1fr; }
  .cat-row label, .cat-row .ctl { grid-column: 2; }
  .grand { position: sticky; bottom: calc(-18px - env(safe-area-inset-bottom)); background: var(--paper); padding-bottom: 8px; }
}

/* Lock screen: nothing of the app shows until the vault is unlocked. */
body.locked > :not(.lock):not(script) { display: none !important; }
.lock {
  min-height: 100dvh; display: grid; place-items: center; background: var(--paper);
  padding: calc(24px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
}
.lock[hidden] { display: none; }
.lock-card {
  width: min(440px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  padding: 32px 28px; box-shadow: var(--shadow); display: grid; gap: 14px; text-align: center;
}
.lock-card h1 { font-size: 26px; text-wrap: balance; }
.lock-card p { margin: 0; color: var(--muted); line-height: 1.5; text-wrap: pretty; }
.lock-mark {
  width: 52px; height: 52px; margin: 0 auto 4px; border-radius: 14px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 24px; font-weight: 700;
}
.lock-go { width: 100%; padding: 14px; font-size: 16px; }
.lock-go:disabled { opacity: .6; cursor: default; }
.lock-small { font-size: 13px; }
.lock-links { display: flex; justify-content: center; gap: 18px; flex-wrap: wrap; }
.lock-msg { min-height: 1.3em; font-size: 14px; }
.lock-msg[data-kind="err"] { color: var(--warn) !important; }
.lock-field { display: grid; gap: 6px; text-align: left; font-size: 13px; font-weight: 600; }
.lock-field input, .lock-card textarea, .pin-form input {
  width: 100%; border: 1px solid var(--line); background: var(--paper); color: var(--ink);
  border-radius: 10px; padding: 12px; font-size: 16px;
}
.lock-card textarea { font-family: "IBM Plex Mono", monospace; resize: vertical; }
.lock-card form { display: grid; gap: 12px; }
.pin-form input { text-align: center; letter-spacing: .5em; font-family: "IBM Plex Mono", monospace; font-size: 22px; }
.rec-code {
  display: block; padding: 14px; border-radius: 10px; background: var(--paper); border: 1px dashed var(--line);
  font: 500 15px/1.6 "IBM Plex Mono", monospace; letter-spacing: .04em; word-break: break-all; user-select: all;
}
.rec-actions { display: flex; gap: 10px; justify-content: center; }
.rec-ok { display: flex; gap: 8px; align-items: center; justify-content: center; font-size: 14px; }
#syncStatus[data-state="pending"], #syncStatus[data-state="offline"] { color: var(--warn); }
