:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1c2330;
  --muted: #6b7484;
  --line: #e3e6eb;
  --accent: #2f6fde;
  --accent-soft: #e6eefc;
  --ok: #1f8a4c;
  --warn-bg: #fff6e0;
  --warn-line: #f0c96a;
  --error-bg: #fdecec;
  --error-line: #e7a3a3;
  --danger: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161d;
    --card: #1b212b;
    --text: #e6e9ef;
    --muted: #97a0ae;
    --line: #2c3440;
    --accent: #5b8ff0;
    --accent-soft: #233552;
    --ok: #4cc07f;
    --warn-bg: #3a2f14;
    --warn-line: #7a6326;
    --error-bg: #3d1f1f;
    --error-line: #7a3a3a;
    --danger: #e0685c;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.4rem; margin: 0.6rem 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 0.8rem; display: flex; gap: 0.8rem; align-items: baseline; }
main { max-width: 1040px; margin: 0 auto; padding: 1rem 16px 3rem; }

.topbar {
  display: flex; align-items: center; gap: 1.2rem; flex-wrap: wrap;
  padding: 0.7rem 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--text); }
.topbar nav { display: flex; gap: 1rem; flex: 1; }
.topbar nav a { color: var(--muted); }
.topbar nav a:hover { color: var(--text); }
.sync { margin: 0; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  padding: 1rem 1.1rem; margin-bottom: 1rem;
}
.card.center { text-align: center; padding: 2rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.grid2 > .card { margin-bottom: 0; }
.grid2 + * { margin-top: 1rem; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 0.9rem 1.1rem; }
.stat .label, .label { color: var(--muted); font-size: 0.85rem; margin-bottom: 0.3rem; }
.stat .value { font-size: 1.5rem; font-weight: 650; }
.amount { font-variant-numeric: tabular-nums; }
.usd { font-size: 0.85em; color: var(--muted); font-variant-numeric: tabular-nums; }
.big { font-size: 1.8rem; font-weight: 650; margin: 0 0 0.6rem; }
.struck { text-decoration: line-through; color: var(--muted); }

.chart-box { position: relative; height: 240px; }

.bar-row { margin-bottom: 0.6rem; }
.bar-label { display: flex; justify-content: space-between; gap: 0.5rem; font-size: 0.92rem; }
.bar { height: 6px; background: var(--accent-soft); border-radius: 3px; margin-top: 3px; }
.bar > div { height: 100%; background: var(--accent); border-radius: 3px; }

table { width: 100%; border-collapse: collapse; }
.tx th { text-align: left; color: var(--muted); font-weight: 500; font-size: 0.85rem; padding: 0.3rem 0.4rem; }
.tx td { padding: 0.55rem 0.4rem; border-top: 1px solid var(--line); vertical-align: top; }
.tx tr.excluded td { opacity: 0.55; }
.tx select { max-width: 13rem; }
.kv th { text-align: left; color: var(--muted); font-weight: 500; padding: 0.35rem 1rem 0.35rem 0; width: 34%; vertical-align: top; }
.kv td { padding: 0.35rem 0; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.btn {
  display: inline-block; border: 1px solid var(--accent); background: var(--accent); color: #fff;
  border-radius: 7px; padding: 0.45rem 0.9rem; font: inherit; cursor: pointer;
}
.btn:hover { text-decoration: none; filter: brightness(1.07); }
.btn.small { padding: 0.25rem 0.65rem; font-size: 0.85rem; }
.btn.secondary { background: transparent; color: var(--accent); }
.btn.danger { background: var(--danger); border-color: var(--danger); }
button.link { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1.1rem; padding: 0 0.3rem; }
button.link:hover { color: var(--text); }

input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 6px; padding: 0.35rem 0.5rem;
}
label { display: inline-flex; flex-direction: column; gap: 0.2rem; font-size: 0.88rem; color: var(--muted); }
label > input, label > select { color: var(--text); }
.choice { flex-direction: row; align-items: center; gap: 0.45rem; color: var(--text); font-size: 0.95rem;
  border: 1px solid var(--line); border-radius: 8px; padding: 0.45rem 0.8rem; cursor: pointer; }
.choices { display: flex; flex-wrap: wrap; gap: 0.6rem; margin: 0.6rem 0; }
.form-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.8rem; align-items: end; }
.form-grid .wide { grid-column: span 2; }
.row { display: flex; gap: 0.8rem; align-items: end; flex-wrap: wrap; margin-bottom: 0.9rem; }
.row .wide { flex: 1; min-width: 14rem; }
.actions { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center; }
form.inline { display: inline; margin: 0 0 0 0.5rem; }
.title-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.5rem; }

.flash { padding: 0.6rem 0.9rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid var(--line); background: var(--card); }
.flash.ok { border-color: var(--ok); }
.flash.warn, .flash.ask { background: var(--warn-bg); border-color: var(--warn-line); }
.flash.error { background: var(--error-bg); border-color: var(--error-line); }

.badge { font-size: 0.75rem; padding: 0.1rem 0.5rem; border-radius: 999px; margin-left: 0.4rem; border: 1px solid var(--line); }
.badge.active { color: var(--ok); border-color: var(--ok); }
.badge.needs_reauth { color: var(--danger); border-color: var(--danger); }
.account-head { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
details summary { cursor: pointer; color: var(--muted); }
details form { margin-top: 0.6rem; display: flex; gap: 0.8rem; align-items: center; flex-wrap: wrap; }
details label { flex-direction: row; gap: 0.35rem; align-items: center; }

.email-meta { margin-bottom: 0.4rem; }
.email-body {
  white-space: pre-wrap; font: 0.82rem/1.4 ui-monospace, Consolas, monospace; background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 0.7rem; max-height: 340px; overflow: auto; margin: 0;
}

@media (max-width: 720px) {
  .stats, .grid2 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .tx thead { display: none; }
  .tx td:first-child { font-size: 0.8rem; }
  .tx select { max-width: 9rem; }
}

.tx tr.ask-row td { border-top: none; padding-top: 0; }
.tx tr.ask-row .flash { margin: 0; }
.tx tr[id] { scroll-margin-top: 90px; }

/* --- App (PWA) and phone layout ------------------------------------------- */
.topbar { padding-top: calc(0.7rem + env(safe-area-inset-top)); position: sticky; top: 0; z-index: 5; }
.sync-icon { display: none; font-size: 1.1rem; line-height: 1; }
.btn.block { width: 100%; }
.center-text { text-align: center; }
.login img { border-radius: 16px; }
.login h1 { margin-top: 0.8rem; }
.amount-input { display: flex; gap: 0.4rem; }
.amount-input select { flex: none; }
.amount-input input { flex: 1; min-width: 0; font-size: 1.2rem; font-weight: 600; }
.form-grid label > input, .form-grid label > select { width: 100%; }
.tabbar { display: none; }

@media (max-width: 720px) {
  body { font-size: 16px; }  /* 16px inputs: iPhone doesn't zoom in on focus */
  .topbar .desktop-nav, .desktop-only { display: none; }
  .sync-label { display: none; }
  .sync-icon { display: inline; }
  .topbar .brand { flex: 1; }
  main { padding-bottom: calc(5.5rem + env(safe-area-inset-bottom)); }
  h1 { font-size: 1.25rem; }
  .stats { grid-template-columns: 1fr 1fr; gap: 0.6rem; }
  .stats .stat:last-child { grid-column: 1 / -1; order: -1; }  /* this month first, full width */
  .stat { padding: 0.7rem 0.9rem; }
  .stat .value { font-size: 1.35rem; }
  .card { padding: 0.9rem; }
  .chart-box { height: 200px; }

  .tabbar {
    display: grid; grid-template-columns: repeat(4, 1fr);
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 0.35rem 0 calc(0.35rem + env(safe-area-inset-bottom));
  }
  .tabbar a {
    display: flex; flex-direction: column; align-items: center; gap: 0.15rem;
    color: var(--muted); font-size: 0.72rem; padding: 0.2rem 0; text-decoration: none;
  }
  .tabbar a.on { color: var(--accent); }
  .tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
  .tabbar a.add svg { background: var(--accent); color: #fff; border-radius: 50%; padding: 4px; }

  /* Transactions as stacked rows: merchant + amount, then date + card, then category. */
  .tx, .tx tbody { display: block; }
  .tx tbody tr:not(.ask-row) {
    display: grid; grid-template-columns: 1fr auto; column-gap: 0.6rem;
    grid-template-areas: "merchant amount" "date act" "cat cat";
    border-top: 1px solid var(--line); padding: 0.55rem 0;
  }
  .tx tbody tr.recent { grid-template-areas: "merchant amount" "date date"; }
  .tx tbody tr:first-child { border-top: none; }
  .tx td { border-top: none; padding: 0; }
  .tx td:first-child { font-size: 0.8rem; }
  .tx .c-merchant { grid-area: merchant; }
  .tx .c-amount { grid-area: amount; font-weight: 600; }
  .tx .c-date { grid-area: date; font-size: 0.8rem; }
  .tx .c-act { grid-area: act; text-align: right; }
  .tx .c-cat { grid-area: cat; margin-top: 0.3rem; }
  .tx .c-cat select { max-width: 100%; width: 100%; }
  .tx tr.ask-row { display: block; }
  .tx tr.ask-row td { display: block; }
  .form-grid .wide { grid-column: 1 / -1; }
}
