:root {
  --bg: #f2f2f7; --card: #fff; --ink: #1c1c1e; --muted: rgba(60,60,67,0.6);
  --muted-2: rgba(60,60,67,0.45); --line: rgba(60,60,67,0.12);
  --accent: #2563eb; --accent-ink: #fff; --danger: #ff3b30; --pos: #16a34a;
  --chip-bg: #fff; --chip-active-bg: #2563eb; --sidebar-active-bg: #eef2ff;
}
@media (prefers-color-scheme: dark) {
  :root { --bg:#0f1115; --card:#1a1d24; --ink:#e8e8ea; --muted:#9aa0aa;
          --muted-2:#6b7280; --line:#2a2f3a; --accent:#60a5fa; --accent-ink:#0f1115;
          --danger:#f87171; --pos:#4ade80; --chip-bg:#242832; --sidebar-active-bg:#1c2333; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 16px/1.4 -apple-system, system-ui, sans-serif;
       background: var(--bg); color: var(--ink); }
button, input, select { font-family: inherit; }

/* ---- layout shell: sidebar (desktop) / bottom nav (mobile) ---- */
.shell { display: flex; min-height: 100vh; }
.sidebar { width: 216px; flex-shrink: 0; background: var(--card);
           border-right: 1px solid var(--line); display: flex; flex-direction: column;
           padding: 20px 12px; position: sticky; top: 0; height: 100vh; }
.brand { font-size: 18px; font-weight: 700; padding: 2px 10px 22px; }
.side-nav { display: flex; flex-direction: column; }
.side-nav button { display: flex; align-items: center; gap: 10px; padding: 9px 10px;
  border-radius: 8px; border: none; background: none; color: var(--ink); font-size: 14px;
  font-weight: 600; text-align: left; margin-bottom: 2px; cursor: pointer; }
.side-nav button svg { stroke: currentColor; fill: currentColor; flex-shrink: 0; }
.side-nav button.active { background: var(--sidebar-active-bg); color: var(--accent); }
.sidebar-spacer { flex: 1; }
.btn-primary { padding: 11px; border-radius: 8px; border: none; background: var(--accent);
  color: var(--accent-ink); font-size: 14px; font-weight: 600; cursor: pointer; }
.export-link { display: block; text-align: center; margin-top: 8px; padding: 8px;
  color: var(--accent); font-size: 13px; text-decoration: none; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { padding: 22px 32px 4px; flex-shrink: 0; }
.topbar h1 { font-size: 24px; font-weight: 700; margin: 0 0 12px; }
main#content { flex: 1; overflow-y: auto; padding: 0 32px 32px; }
.muted { color: var(--muted); }

#fab-desktop { display: none; }
#bottom-nav { display: none; }
/* Desktop reaches export through the sidebar link; the topbar copy is the
   phone's only route to it, since the bottom nav is view tabs only. */
#export-mobile { display: none; }

/* The mobile add-expense bubble. Only the media queries below toggle `display`;
   everything that makes it look and sit like a FAB lives here. It has to clear
   #bottom-nav on both axes -- z-index above the nav's 10, and a bottom offset
   past the nav's height plus the home-indicator inset. */
#fab { display: none; position: fixed; z-index: 11;
  right: 18px; bottom: calc(env(safe-area-inset-bottom, 0px) + 76px);
  width: 56px; height: 56px; border-radius: 50%; border: none;
  align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-ink);
  font-size: 26px; line-height: 1; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,0.28);
  -webkit-tap-highlight-color: transparent; }
#fab:active { transform: scale(0.94); }
@media (prefers-reduced-motion: reduce) { #fab:active { transform: none; } }

/* ---- mobile layout ---- */
@media (max-width: 860px) {
  .shell { display: block; }
  .sidebar { display: none; }
  .main { display: block; }
  .topbar { padding: 20px 16px 0; display: flex; align-items: baseline;
    justify-content: space-between; gap: 12px; }
  .topbar h1 { font-size: 22px; }

  #export-mobile { display: inline-flex; align-items: center; gap: 5px;
    flex-shrink: 0; margin-bottom: 12px; padding: 6px 11px;
    border-radius: 8px; background: var(--card); border: 1px solid var(--line);
    color: var(--accent); font-size: 13px; font-weight: 600; text-decoration: none;
    -webkit-tap-highlight-color: transparent; }
  #export-mobile svg { stroke: currentColor; fill: none; }
  main#content { padding: 0 16px 110px; }

  #bottom-nav { display: flex; position: fixed; left: 0; right: 0; bottom: 0;
    padding: 8px 6px calc(env(safe-area-inset-bottom, 0px) + 10px);
    background: rgba(255,255,255,0.92); backdrop-filter: blur(12px) saturate(180%);
    border-top: 0.5px solid var(--line); z-index: 10; }
  @media (prefers-color-scheme: dark) {
    #bottom-nav { background: rgba(26,29,36,0.92); }
  }
  #bottom-nav button { flex: 1; display: flex; flex-direction: column; align-items: center;
    gap: 2px; border: none; background: none; color: var(--muted); cursor: pointer; }
  #bottom-nav button svg { stroke: currentColor; fill: currentColor; }
  #bottom-nav button span { font-size: 10px; font-weight: 500; }
  #bottom-nav button.active { color: var(--accent); }

  #fab { display: flex; }
}
@media (min-width: 861px) {
  #fab { display: none; }
  #fab-desktop { display: block; }
}

/* ---- avatar + list cards ---- */
.avatar { width: 38px; height: 38px; border-radius: 50%; color: #fff; display: flex;
  align-items: center; justify-content: center; font-size: 15px; font-weight: 600;
  flex-shrink: 0; position: relative; }
.avatar.sm { width: 26px; height: 26px; font-size: 11px; }
.avatar .review-dot { position: absolute; top: -2px; right: -2px; width: 11px; height: 11px;
  border-radius: 50%; background: var(--danger); border: 2px solid var(--bg); }

.row { display: flex; align-items: center; gap: 12px; background: var(--card);
  border-radius: 14px; padding: 12px 14px; margin-bottom: 8px; cursor: pointer; }
.row:active { opacity: 0.7; }
.row .meta { flex: 1; min-width: 0; }
.row .merchant { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; }
.row .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; }
.row .amt { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums;
  white-space: nowrap; }
.amt.neg { color: var(--pos); }
.amt.pending { color: var(--muted); }
.flag { color: var(--danger); font-size: 11px; font-weight: 600; }

/* desktop table-style list */
@media (min-width: 861px) {
  .list-card { background: var(--card); border: 1px solid var(--line); border-radius: 10px;
    overflow: hidden; }
  .list-head { display: grid; grid-template-columns: 100px 1fr 200px 120px; gap: 12px;
    padding: 10px 18px; background: var(--bg); border-bottom: 1px solid var(--line);
    font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase;
    letter-spacing: 0.03em; }
  .list-head > div:last-child { text-align: right; }
  .row.table-row { grid-template-columns: 100px 1fr 200px 120px; display: grid; align-items: center;
    background: none; border-radius: 0; border-bottom: 1px solid var(--line); margin-bottom: 0;
    padding: 12px 18px; }
  .row.table-row:last-child { border-bottom: none; }
  .row.table-row .amt { text-align: right; }
}

/* ---- filter chips ---- */
.filter-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px;
  flex-wrap: wrap; }
.chip { flex-shrink: 0; display: inline-flex; align-items: center; gap: 4px;
  padding: 7px 12px; border-radius: 20px; border: none; background: var(--chip-bg);
  color: var(--ink); font-size: 14px; font-weight: 500; white-space: nowrap; cursor: pointer; }
.chip:active { opacity: 0.6; }
.chip.active { background: var(--chip-active-bg); color: #fff; }
.chip.danger.active { background: var(--danger); }
.chip.link { background: none; color: var(--accent); padding: 7px 6px; }
.search-box { display: flex; align-items: center; gap: 8px; background: var(--card);
  border-radius: 12px; padding: 9px 12px; margin-bottom: 10px; }
.search-box svg { flex-shrink: 0; stroke: var(--muted); }
.search-box input { border: none; outline: none; flex: 1; font-size: 15px;
  background: transparent; color: var(--ink); }
@media (min-width: 861px) {
  .search-box { flex: 0 0 240px; padding: 8px 12px; margin-bottom: 0; }
  .chip { border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; font-size: 13px; }
  .chip.active { border-color: var(--chip-active-bg); }
}

/* ---- summary rows ---- */
.summary-total { display: flex; justify-content: space-between; align-items: center;
  background: var(--card); border-radius: 14px; padding: 16px; margin-bottom: 12px; }
.summary-total .label { font-size: 15px; color: var(--muted); }
.summary-total .value { font-size: 20px; font-weight: 700; }
.summary-row { background: var(--card); border-radius: 14px; padding: 12px 14px; margin-bottom: 8px; }
.summary-row.sub { margin-left: 16px; }
.summary-row .head { display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 6px; }
.summary-row .name { font-size: 15px; font-weight: 600; }
.summary-row .name.leaf { font-weight: 400; }
.summary-row .count { color: var(--muted-2); font-weight: 400; }
.summary-row .value { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.expansion { margin: 0 0 8px 16px; }
.summary-row.sub + .expansion { margin-left: 32px; }
.expansion-empty { margin: 0 0 8px 16px; font-size: 13px; }
.bar-track { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 3px; }

/* ---- merchants ---- */
.scope-toggle { display: flex; background: var(--line); border-radius: 10px; padding: 2px;
  margin-bottom: 12px; width: fit-content; min-width: 220px; }
.scope-btn { flex: 1; padding: 7px 14px; border-radius: 8px; border: none; font-size: 14px;
  font-weight: 600; background: transparent; color: var(--muted); cursor: pointer; }
.scope-btn.active { background: var(--card); color: var(--accent); }
.row + .expansion { margin-left: 16px; }

/* ---- duplicates ---- */
.dup-group { background: var(--card); border-radius: 14px; padding: 12px 14px; margin-bottom: 12px; }
.dup-member { display: flex; align-items: center; gap: 10px; padding: 8px 0;
  border-bottom: 1px solid var(--line); cursor: pointer; }
.dup-member:last-of-type { border-bottom: none; }
.dup-member input { display: none; }
.dup-dot { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line);
  background: var(--card); flex-shrink: 0; }
.dup-member input:checked + .dup-dot { border-color: var(--accent); background: var(--accent); }
.dup-member .meta { flex: 1; min-width: 0; }
.dup-member .merchant { font-size: 15px; font-weight: 600; display: block; }
.dup-member .sub { font-size: 12px; color: var(--muted); }
.dup-merge, .dup-dismiss { width: 100%; margin-top: 10px; padding: 10px; border-radius: 10px;
  border: none; font-size: 14px; font-weight: 600; cursor: pointer; }
.dup-merge { background: var(--accent); color: #fff; }
.dup-dismiss { background: var(--line); color: var(--ink); margin-top: 6px; }
.dup-merge:disabled, .dup-dismiss:disabled { opacity: 0.6; cursor: default; }
@media (min-width: 861px) {
  #content .dup-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
  .dup-group { border: 1px solid var(--line); }
}

/* ---- trends ---- */
.chart-scroll { overflow-x: auto; background: var(--card); border-radius: 14px;
  padding: 20px 16px 10px; }
.chart-scroll svg text { font-family: inherit; fill: var(--muted); }

/* ---- sheets / modals ---- */
@keyframes sheetUp { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes popIn { from { transform: scale(0.96); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 30;
  animation: fadeIn 0.15s; display: flex; align-items: flex-end; justify-content: center; }
.overlay.hidden { display: none; }
/* month/category pickers can be opened from inside the add/edit sheet, so they
   need to stack above it regardless of DOM order. */
#month-sheet, #cat-sheet { z-index: 31; }
.sheet { background: var(--card); width: 100%; max-width: 520px; border-radius: 20px 20px 0 0;
  padding: 14px 16px 30px; max-height: 85vh; overflow-y: auto; animation: sheetUp 0.2s ease-out; }
.grabber { width: 36px; height: 5px; background: var(--line); border-radius: 3px; margin: 0 auto 14px; }
/* on desktop the sheet is a centered dialog, not an iOS-style bottom sheet --
   matches the browser-chrome mock, which used position:center + popIn there
   while the phone mock stayed bottom-anchored. */
@media (min-width: 861px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,0.25); animation: popIn 0.15s ease-out; }
  .grabber { display: none; }
}
.sheet h2, .sheet-title { font-size: 19px; font-weight: 700; margin: 0 0 12px; }
.sheet label { display: block; margin: 10px 0 4px; font-size: 13px; color: var(--muted); }
.sheet input:not([type=hidden]), .sheet select, .picker-btn { width: 100%; padding: 11px;
  font-size: 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg);
  color: var(--ink); }
.picker-btn { text-align: left; cursor: pointer; }
.item-row { display: flex; gap: 6px; margin-top: 6px; }
.item-row input { flex: 1; }
.item-remove { width: 38px; flex-shrink: 0; border: none; background: var(--line);
  border-radius: 10px; color: var(--muted); font-size: 18px; cursor: pointer; }
.linkish { background: none; border: none; color: var(--accent); padding: 6px 0; cursor: pointer;
  font-size: 14px; }
.chip-row { display: flex; gap: 6px; overflow-x: auto; margin-top: 8px; }
.chip-row.hidden { display: none; }
.recent-chip { flex-shrink: 0; padding: 6px 10px; border-radius: 14px; border: none;
  background: var(--line); color: var(--ink); font-size: 13px; cursor: pointer; }
.form-actions { display: flex; gap: 10px; margin-top: 16px; }
.form-actions button { flex: 1; padding: 13px; border-radius: 12px; border: none;
  font-size: 16px; font-weight: 600; cursor: pointer; }
#f-save { background: var(--accent); color: var(--accent-ink); }
.ghost { background: var(--line); color: var(--ink); }
.danger { background: var(--danger); color: #fff; }
.error { color: var(--danger); margin-top: 10px; font-size: 14px; }

/* month / category picker sheets */
.pick-row { display: flex; justify-content: space-between; align-items: center;
  padding: 13px 4px; border-bottom: 1px solid var(--line); cursor: pointer; }
.pick-row:last-child { border-bottom: none; }
.pick-row .name { font-size: 16px; }
.pick-row .count { font-size: 14px; color: var(--muted-2); }
.pick-default { font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .05em; color: var(--accent); background: var(--sidebar-active-bg);
  border-radius: 5px; padding: 3px 7px; flex-shrink: 0; }
.cat-pick-row { display: flex; align-items: center; gap: 10px; padding: 12px 4px;
  border-bottom: 1px solid var(--line); cursor: pointer; }
.cat-pick-row:last-child { border-bottom: none; }
.cat-dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; }
.cat-pick-row .name { flex: 1; font-size: 16px; }
.cat-pick-row svg { flex-shrink: 0; stroke: var(--muted-2); }
.cat-back { border: none; background: none; color: var(--accent); font-size: 16px;
  padding: 0; cursor: pointer; }
.cat-sub-header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }

/* ---- detail sheet ---- */
.detail-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.detail-head .name { font-size: 19px; font-weight: 700; }
.detail-head .date { font-size: 14px; color: var(--muted); }
.detail-head .amt { margin-left: auto; font-size: 20px; font-weight: 700; }
#detail-body { margin: 0; background: var(--bg); border-radius: 12px; padding: 4px 14px; }
#detail-body dt { color: var(--muted); font-size: 13px; margin-top: 10px; padding-top: 10px;
  border-top: 1px solid var(--line); }
#detail-body dt:first-child { margin-top: 0; padding-top: 10px; border-top: none; }
#detail-body dd { margin: 2px 0 10px; white-space: pre-wrap; word-break: break-word; }
.review-note { color: var(--danger); font-size: 13px; font-weight: 600; padding: 10px 0 0; }

/* `.sheet label { display: block; }` above is more specific than `.hidden { display: none; }`
   for a <label> in this form -- restore the hide explicitly. */
.sheet label.hidden { display: none; }
.hidden { display: none !important; }
