:root {
  --green-900: #14401f;
  --green-800: #1a5c2a;
  --green-700: #226e34;
  --green-600: #2d8b45;
  --green-pale: #e8f5ec;
  --success-text: #226e34;
  --amber: #d97706;
  --amber-600: #ea8a0c;
  --amber-700: #b45309;
  --amber-pale: #fef3c7;
  --red: #dc2626;
  --red-pale: #fee2e2;
  --danger-text: #b91c1c;
  --field-border: #79889d;
  --blue: #2563eb;
  --blue-pale: #dbeafe;
  --ink: #0f172a;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-300: #cbd5e1;
  --gray-400: #94a3b8;
  --gray-500: #64748b;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1e293b;
  --white: #ffffff;
  --card-bg: #ffffff;
  --body-bg: #f8fafc;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(15,23,42,0.06);
  --shadow: 0 1px 3px rgba(15,23,42,0.08), 0 1px 2px rgba(15,23,42,0.04);
  --shadow-md: 0 4px 12px rgba(15,23,42,0.08);
  --shadow-lg: 0 10px 30px rgba(15,23,42,0.12);
  --sidebar-w: 248px;
}

[data-theme="dark"] {
  --ink: #e7ecf3;
  --gray-50: #0f1722;
  --gray-100: #1a2433;
  --gray-200: #243044;
  --gray-300: #334155;
  --gray-400: #64748b;
  --gray-500: #94a3b8;
  --gray-600: #b6c2d3;
  --gray-700: #cbd5e1;
  --white: #16202e;
  --card-bg: #16202e;
  --body-bg: #0b1220;
  --field-border: #44566f;
  --green-pale: #16321f;
  --amber-pale: #3a2c10;
  --red-pale: #3a1d1d;
  --blue-pale: #16263f;
  --success-text: #5fd07e;
  --danger-text: #f87171;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px; line-height: 1.55;
  color: var(--ink); background: var(--body-bg);
  -webkit-font-smoothing: antialiased;
}

a:focus-visible, button:focus-visible, input:focus-visible,
select:focus-visible, textarea:focus-visible, [tabindex]:focus-visible {
  outline: 3px solid var(--blue); outline-offset: 2px; border-radius: 6px;
}
.sidebar a:focus-visible, .sidebar button:focus-visible { outline-color: #fff; outline-offset: 3px; }
.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; }
.skip-link { position: absolute; left: .5rem; top: -3rem; background: var(--green-800); color: #fff; padding: .6rem 1rem; border-radius: var(--radius); z-index: 300; font-weight: 600; text-decoration: none; transition: top .15s; }
.skip-link:focus { top: .5rem; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; } }

/* ── App shell ─────────────────────────────────────────────── */
#app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: linear-gradient(180deg, var(--green-900) 0%, var(--green-800) 100%);
  color: rgba(255,255,255,.92);
  display: flex; flex-direction: column;
  position: fixed; top: 0; bottom: 0; left: 0;
  padding: 1.1rem .85rem; z-index: 100; box-shadow: var(--shadow-lg);
}
.brand { display: flex; align-items: center; gap: .7rem; padding: .25rem .5rem 1.1rem; }
.brand-mark {
  width: 40px; height: 40px; background: var(--amber); border-radius: 10px;
  display: grid; place-items: center; font-weight: 800; font-size: .95rem; color: #fff;
  box-shadow: 0 2px 8px rgba(217,119,6,.4); flex-shrink: 0;
}
.brand-name { font-weight: 700; font-size: .98rem; line-height: 1.1; color: #fff; }
.brand-sub { font-size: .74rem; color: rgba(255,255,255,.8); }

.sidebar-sync {
  display: flex; align-items: center; justify-content: center; gap: .55rem;
  margin: 0 .25rem 1.1rem; padding: .7rem .9rem;
  background: var(--amber); color: #fff; border: none; border-radius: var(--radius);
  font-size: .92rem; font-weight: 600; cursor: pointer;
  transition: background .15s, transform .1s; box-shadow: 0 2px 10px rgba(217,119,6,.35);
}
.sidebar-sync:hover { background: var(--amber-600); }
.sidebar-sync:active { transform: scale(.98); }
.sidebar-sync:disabled { opacity: .6; cursor: default; }

.sidebar-nav { display: flex; flex-direction: column; gap: .2rem; }
.nav-link {
  display: flex; align-items: center; gap: .75rem; padding: .65rem .85rem;
  border-radius: var(--radius); color: rgba(255,255,255,.88); text-decoration: none;
  font-size: .92rem; font-weight: 500; border: none; background: none; cursor: pointer;
  width: 100%; text-align: left; transition: background .15s, color .15s;
}
.nav-link .ico { font-size: 1.15rem; width: 1.4rem; text-align: center; flex-shrink: 0; }
.nav-link:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav-link.active { background: rgba(255,255,255,.16); color: #fff; font-weight: 600; }
.nav-badge { margin-left: auto; background: var(--amber); color: #fff; font-size: .72rem; font-weight: 700; padding: .05rem .45rem; border-radius: 999px; }
.nav-badge:empty { display: none; }

.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: .2rem; padding-top: 1rem; }
.user-chip { font-size: .8rem; color: rgba(255,255,255,.75); padding: .4rem .85rem; }
.nav-link.logout { color: rgba(255,255,255,.8); }

/* ── Content ───────────────────────────────────────────────── */
.content { flex: 1; margin-left: var(--sidebar-w); padding: 1.6rem 2rem 4rem; max-width: 1100px; }
.page { display: none; }
.page.active { display: block; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.4rem; }
.page-head h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -.01em; }
.page-head .sub { color: var(--gray-500); font-size: .9rem; margin-top: .15rem; }

.year-picker { display: flex; align-items: center; gap: .5rem; }
.year-label { font-weight: 700; font-size: 1.05rem; min-width: 3.2rem; text-align: center; }

/* ── Buttons ───────────────────────────────────────────────── */
.btn { display: inline-flex; align-items: center; gap: .4rem; padding: .6rem 1rem; border-radius: var(--radius); font-size: .9rem; font-weight: 600; border: 1px solid transparent; cursor: pointer; transition: background .15s, border-color .15s, transform .1s; text-decoration: none; }
.btn:active { transform: scale(.98); }
.btn-small { padding: .4rem .7rem; font-size: .82rem; }
.btn-primary { background: var(--green-700); color: #fff; }
.btn-primary:hover { background: var(--green-600); }
.btn-secondary { background: var(--card-bg); color: var(--ink); border-color: var(--gray-300); }
.btn-secondary:hover { background: var(--gray-100); }
.btn-amber { background: var(--amber); color: #fff; }
.btn-amber:hover { background: var(--amber-600); }
.btn-danger { background: var(--red-pale); color: var(--danger-text); }
.btn-danger:hover { background: #fbcaca; }
[data-theme="dark"] .btn-danger:hover { background: #4d2424; }

/* ── Cards ─────────────────────────────────────────────────── */
.card { background: var(--card-bg); border: 1px solid var(--gray-200); border-radius: var(--radius-lg); padding: 1.25rem; box-shadow: var(--shadow-sm); margin-bottom: 1.25rem; }
.card.flush { padding: 0; }
.section-title { font-size: .95rem; font-weight: 700; margin-bottom: .85rem; display: flex; align-items: center; gap: .5rem; }
.section-title span { font-weight: 400; color: var(--gray-500); font-size: .82rem; }

/* ── Stat tiles ────────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stat-tile { background: var(--card-bg); border: 1px solid var(--gray-200); border-radius: var(--radius-lg); padding: 1.1rem 1.25rem; box-shadow: var(--shadow-sm); }
.stat-tile .label { font-size: .8rem; color: var(--gray-500); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat-tile .value { font-size: 1.7rem; font-weight: 800; margin-top: .35rem; letter-spacing: -.02em; }
.stat-tile.income .value { color: var(--success-text); }
.stat-tile.expense .value { color: var(--danger-text); }
.stat-tile.net .value { color: var(--ink); }
.stat-tile.review { cursor: pointer; }
.stat-tile.review .value { color: var(--amber-700); }
.stat-tile .tile-note { font-size: .76rem; color: var(--gray-500); margin-top: .2rem; }

/* ── Account chips ─────────────────────────────────────────── */
.accounts-row { display: flex; flex-wrap: wrap; gap: .75rem; margin-bottom: 1.25rem; }
.account-chip { background: var(--card-bg); border: 1px solid var(--gray-200); border-radius: var(--radius); padding: .65rem .9rem; min-width: 160px; box-shadow: var(--shadow-sm); }
.account-chip .ac-name { font-size: .82rem; font-weight: 600; }
.account-chip .ac-mask { font-size: .74rem; color: var(--gray-500); }
.account-chip .ac-bal { font-size: 1.1rem; font-weight: 700; margin-top: .2rem; }

/* ── Tables ────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
thead th { text-align: left; font-size: .76rem; text-transform: uppercase; letter-spacing: .03em; color: var(--gray-500); font-weight: 700; padding: .7rem .8rem; border-bottom: 1px solid var(--gray-200); white-space: nowrap; }
tbody td { padding: .6rem .8rem; border-bottom: 1px solid var(--gray-100); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
.text-right { text-align: right; }
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover, th.sortable.active { color: var(--ink); }
.sort-arrow { font-size: .7rem; color: var(--green-700); }

/* segmented money in/out tabs */
.flow-tabs { display: inline-flex; gap: .25rem; background: var(--gray-100); border-radius: var(--radius); padding: .25rem; margin-bottom: 1rem; }
.flow-tabs button { border: none; background: none; padding: .4rem .9rem; border-radius: calc(var(--radius) - 3px); font-size: .85rem; font-weight: 600; color: var(--gray-600); cursor: pointer; transition: background .12s; }
.flow-tabs button.active { background: var(--card-bg); color: var(--ink); box-shadow: var(--shadow-sm); }
.flow-tabs button span { font-weight: 500; color: var(--gray-500); }

.toggle-row { display: flex; align-items: flex-start; gap: .6rem; font-size: .88rem; font-weight: 600; cursor: pointer; margin-bottom: 1rem; }
.toggle-row input { width: auto; margin-top: .25rem; flex-shrink: 0; }
.toggle-row .muted { display: block; font-weight: 400; font-size: .8rem; margin-top: .15rem; }
.amount-pos { color: var(--success-text); font-weight: 600; }
.amount-neg { color: var(--ink); }
.txn-name { font-weight: 600; }
.txn-sub { font-size: .76rem; color: var(--gray-500); }
.pending-tag { font-size: .68rem; background: var(--amber-pale); color: var(--amber-700); padding: .05rem .4rem; border-radius: 4px; margin-left: .4rem; }

/* ── Forms / controls ──────────────────────────────────────── */
.form-group { margin-bottom: 1rem; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
label { display: block; font-size: .82rem; font-weight: 600; margin-bottom: .35rem; color: var(--gray-700); }
input, select, textarea {
  width: 100%; padding: .55rem .7rem; font-size: .9rem; font-family: inherit;
  border: 1px solid var(--field-border); border-radius: var(--radius);
  background: var(--card-bg); color: var(--ink);
}
textarea { min-height: 80px; resize: vertical; }
.cat-select { padding: .35rem .5rem; font-size: .82rem; min-width: 170px; }
.cat-select.uncat { border-color: var(--amber); background: var(--amber-pale); }

.toolbar { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; margin-bottom: 1rem; }
.toolbar input[type="search"], .toolbar select { width: auto; min-width: 150px; }
.toolbar .grow { flex: 1; min-width: 180px; }

.biz-toggle { display: inline-flex; border: 1px solid var(--gray-300); border-radius: 999px; overflow: hidden; font-size: .72rem; cursor: pointer; }
.biz-toggle span { padding: .15rem .5rem; font-weight: 600; color: var(--gray-500); }
.biz-toggle span.on { background: var(--green-700); color: #fff; }
.biz-toggle.personal span.on { background: var(--gray-500); }

/* ── Schedule C breakdown ──────────────────────────────────── */
.line-row { display: flex; align-items: center; gap: .75rem; padding: .5rem 0; border-bottom: 1px solid var(--gray-100); }
.line-row:last-child { border-bottom: none; }
.line-tag { font-size: .7rem; font-weight: 700; color: var(--gray-500); background: var(--gray-100); border-radius: 5px; padding: .1rem .4rem; min-width: 2.2rem; text-align: center; }
.line-name { flex: 1; font-weight: 500; }
.line-amt { font-weight: 700; }
.line-bar { height: 6px; border-radius: 3px; background: var(--green-600); margin-top: .3rem; }

/* ── Misc ──────────────────────────────────────────────────── */
.empty-state { text-align: center; padding: 2.5rem 1rem; color: var(--gray-500); }
.empty-state .big { font-size: 2.5rem; margin-bottom: .5rem; }
.empty-state h3 { color: var(--ink); margin-bottom: .35rem; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.flex { display: flex; }
.gap-1 { gap: .6rem; }
.muted { color: var(--gray-500); }
.pill { font-size: .72rem; font-weight: 600; padding: .1rem .5rem; border-radius: 999px; }
.pill.ok { background: var(--green-pale); color: var(--success-text); }
.pill.warn { background: var(--amber-pale); color: var(--amber-700); }
.pill.err { background: var(--red-pale); color: var(--danger-text); }

.bank-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .7rem 0; border-bottom: 1px solid var(--gray-100); }
.bank-row:last-child { border-bottom: none; }
.rule-row { display: flex; align-items: center; gap: .6rem; padding: .5rem 0; border-bottom: 1px solid var(--gray-100); font-size: .85rem; }
.rule-row:last-child { border-bottom: none; }
.rule-match { font-weight: 600; }
.rule-arrow { color: var(--gray-400); }

/* ── Toast ─────────────────────────────────────────────────── */
#toast { position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%) translateY(2rem); background: var(--ink); color: var(--body-bg); padding: .7rem 1.2rem; border-radius: var(--radius); font-size: .88rem; font-weight: 600; box-shadow: var(--shadow-lg); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 400; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Modal ─────────────────────────────────────────────────── */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: none; align-items: flex-start; justify-content: center; padding: 2rem 1rem; z-index: 350; overflow-y: auto; }
.modal-backdrop.show { display: flex; }
.modal { background: var(--card-bg); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); padding: 1.5rem; width: 100%; max-width: 560px; }
.modal h2 { font-size: 1.2rem; margin-bottom: 1rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .6rem; margin-top: 1.25rem; }

.upload-zone { position: relative; border: 2px dashed var(--field-border); border-radius: var(--radius); padding: 1.6rem 1rem; text-align: center; cursor: pointer; transition: border-color .15s, background .15s; }
.upload-zone:hover, .upload-zone.drag { border-color: var(--green-600); background: var(--green-pale); }
.upload-zone input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.upload-zone .icon { font-size: 1.8rem; }

.add-account-row { display: flex; gap: .5rem; align-items: center; }
.add-account-row input { flex: 1; }
.add-account-row select { width: auto; }
.account-line { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .6rem 0; border-bottom: 1px solid var(--gray-100); }
.account-line:last-child { border-bottom: none; }
#imp-preview td, #imp-preview th { font-size: .82rem; }

/* ── Review cards ──────────────────────────────────────────── */
.review-bulk { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; background: var(--green-pale); border: 1px solid var(--green-600); border-radius: var(--radius); padding: .6rem .8rem; margin-bottom: 1rem; }
.review-item { display: grid; grid-template-columns: auto 1fr auto; gap: .75rem; align-items: center; padding: .7rem 0; border-bottom: 1px solid var(--gray-100); }
.review-item:last-child { border-bottom: none; }
.review-controls { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.remember-label { display: inline-flex; align-items: center; gap: .3rem; font-size: .76rem; color: var(--gray-600); font-weight: 500; cursor: pointer; }
.remember-label input { width: auto; }

/* ── Login ─────────────────────────────────────────────────── */
#login-page { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; background: linear-gradient(135deg, var(--green-900), var(--green-700)); }
.login-box { background: var(--card-bg); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); padding: 2rem; width: 100%; max-width: 380px; }
.brand-block { text-align: center; margin-bottom: 1.5rem; }
.brand-block .mark { width: 56px; height: 56px; background: var(--amber); border-radius: 14px; display: grid; place-items: center; font-weight: 800; color: #fff; margin: 0 auto .75rem; font-size: 1.1rem; }
.brand-block h1 { font-size: 1.25rem; }
.brand-block p { color: var(--gray-500); font-size: .85rem; }
.btn-full { width: 100%; justify-content: center; margin-top: .5rem; }
.login-error { color: var(--danger-text); font-size: .85rem; margin-top: .75rem; text-align: center; display: none; }

/* ── Taxes page ────────────────────────────────────────────── */
.callout { background: var(--amber-pale); border: 1px solid var(--amber); border-radius: var(--radius-lg); padding: .9rem 1.1rem; margin-bottom: 1.25rem; font-size: .95rem; color: var(--ink); }
.callout strong { color: var(--amber-700); font-size: 1.05rem; }
.kv-table td { padding: .55rem .2rem; border-bottom: 1px solid var(--gray-100); }
.kv-table tr:last-child td { border-bottom: none; }
.kv-table td:last-child { font-weight: 700; white-space: nowrap; }
.q-due { font-size: .78rem; color: var(--gray-500); }
.q-setaside { font-weight: 800; color: var(--amber-700); }

.mileage-add { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: .85rem; }
.mileage-add input[type="date"], .mileage-add input[type="number"] { width: auto; }
.mileage-add input[type="number"] { max-width: 110px; }
.mileage-total { font-size: .85rem; color: var(--gray-600); font-weight: 600; margin-bottom: .6rem; }
.mile-row { display: flex; align-items: center; gap: .75rem; padding: .55rem 0; border-bottom: 1px solid var(--gray-100); font-size: .88rem; }
.mile-row:last-child { border-bottom: none; }
.mile-date { font-variant-numeric: tabular-nums; color: var(--gray-600); white-space: nowrap; }
.mile-miles { font-weight: 700; white-space: nowrap; }
.mile-purpose { flex: 1; color: var(--gray-600); }
.mile-del { margin-left: auto; }

/* ── Category guide + inline hints ─────────────────────────── */
#guide-modal .modal { max-width: 640px; }
.guide-group { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--gray-500); margin: 1rem 0 .4rem; }
.guide-group:first-child { margin-top: 0; }
.guide-item { padding: .6rem 0; border-bottom: 1px solid var(--gray-100); }
.guide-item:last-child { border-bottom: none; }
.guide-head { display: flex; align-items: center; gap: .5rem; }
.guide-name { font-weight: 700; }
.guide-biz { margin-left: auto; font-size: .68rem; font-weight: 700; padding: .05rem .45rem; border-radius: 999px; }
.guide-biz.biz { background: var(--green-pale); color: var(--success-text); }
.guide-biz.pers { background: var(--gray-100); color: var(--gray-500); }
.guide-hint { font-size: .82rem; color: var(--gray-600); margin-top: .2rem; }
.cat-hint { font-size: .78rem; color: var(--gray-600); margin-top: .35rem; max-width: 60ch; }
.cat-hint:empty { display: none; }

/* ── Mobile ────────────────────────────────────────────────── */
@media (max-width: 760px) {
  .sidebar { position: fixed; bottom: 0; top: auto; left: 0; right: 0; width: 100%; flex-direction: row; padding: .4rem; gap: .2rem; height: 60px; }
  .brand, .sidebar-footer, .sidebar-sync .lbl, .user-chip { display: none; }
  .sidebar-sync { margin: 0; padding: .5rem; border-radius: var(--radius); }
  .sidebar-nav { flex-direction: row; flex: 1; justify-content: space-around; gap: 0; }
  .nav-link { flex-direction: column; gap: .1rem; padding: .3rem; font-size: .65rem; }
  .nav-link .lbl { font-size: .62rem; }
  .content { margin-left: 0; padding: 1rem 1rem 5rem; }
  .form-row { grid-template-columns: 1fr; }
}
