:root {
  --navy: #0B1F3A;
  --navy-2: #16305A;
  --navy-soft: #EAEFF6;
  --emerald: #059669;
  --emerald-hover: #047857;
  --emerald-ink: #065F46;
  --emerald-soft: #E7F6EF;
  --red: #DC2626;
  --red-soft: #FDEDED;
  --bg: #F5F7FB;
  --surface: #FFFFFF;
  --surface-2: #F8FAFC;
  --text: #0B1F3A;
  --text-muted: #5B6B82;
  --border: rgba(11, 31, 58, 0.09);
  --border-strong: rgba(11, 31, 58, 0.16);
  --accent: var(--emerald);
  --accent-hover: var(--emerald-hover);
  --accent-soft: var(--emerald-soft);
  --success: var(--emerald);
  --warning: #B45309;
  --danger: var(--red);
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(11, 31, 58, 0.05);
  --shadow-md: 0 1px 2px rgba(11, 31, 58, 0.04), 0 8px 24px -12px rgba(11, 31, 58, 0.14);
  --shadow-lg: 0 24px 60px -20px rgba(11, 31, 58, 0.35);
  --font-display: "Plus Jakarta Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-body: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --ease: 180ms ease;
}

*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 84px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.6 var(--font-body); -webkit-font-smoothing: antialiased; overflow-x: hidden;
}
h1, h2, h3 { font-family: var(--font-display); margin: 0; color: var(--navy); text-wrap: balance; }
h1 { font-size: clamp(2rem, 4.5vw, 3rem); letter-spacing: -0.025em; line-height: 1.1; font-weight: 800; }
h2 { font-size: clamp(1.125rem, 2vw, 1.375rem); letter-spacing: -0.02em; font-weight: 700; }
h3 { font-size: 1.125rem; letter-spacing: -0.015em; }
p { margin: 0; text-wrap: pretty; }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-muted); }
.small { font-size: 12.5px; }
.wrap { width: min(1240px, 100% - 40px); margin-inline: auto; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: rgba(255, 255, 255, 0.82); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner { display: flex; align-items: center; gap: 24px; height: 64px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font: 800 1.125rem var(--font-display); letter-spacing: -0.02em; color: var(--navy); }
.brand-mark { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; background: var(--navy); color: #34D399; }
.nav { display: flex; gap: 4px; margin-right: auto; }
.nav a { text-decoration: none; padding: 8px 12px; border-radius: 8px; color: var(--text-muted); font-weight: 500; transition: background var(--ease), color var(--ease); }
.nav a:hover { background: var(--navy-soft); color: var(--navy); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px; border-radius: var(--radius-sm); border: 1px solid transparent;
  font-weight: 600; font-size: 14px; cursor: pointer; white-space: nowrap;
  transition: background var(--ease), border-color var(--ease), transform var(--ease), box-shadow var(--ease);
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-primary { background: var(--emerald); color: #fff; box-shadow: 0 6px 16px -8px rgba(5, 150, 105, 0.7); margin-left: auto; }
.btn-primary:hover:not(:disabled) { background: var(--emerald-hover); }
.btn-ghost { background: var(--surface); border-color: var(--border-strong); color: var(--navy); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); border-color: var(--navy-2); }
.link { background: none; border: 0; padding: 4px 0; color: var(--navy); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.link.armed { color: var(--red); }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 9px;
  border: 1px solid transparent; background: transparent; color: var(--text-muted); cursor: pointer;
  transition: background var(--ease), color var(--ease);
}
.icon-btn:hover { background: var(--navy-soft); color: var(--navy); }
.icon-btn.danger:hover { background: var(--red-soft); color: var(--red); }

/* Hero */
.hero { display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; flex-wrap: wrap; padding: 40px 0 24px; }
.eyebrow {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; margin-bottom: 12px;
  border-radius: 999px; background: var(--emerald-soft); color: var(--emerald-ink);
  font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
}
.lede { margin-top: 10px; color: var(--text-muted); font-variant-numeric: tabular-nums; max-width: 65ch; }
.filters { display: flex; flex-direction: column; gap: 10px; align-items: flex-end; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.chip {
  height: 34px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--border-strong);
  background: var(--surface); font-size: 13px; font-weight: 500; cursor: pointer; color: var(--text-muted);
  transition: all var(--ease);
}
.chip:hover { color: var(--navy); border-color: var(--navy-2); }
.chip.active { background: var(--navy); border-color: var(--navy); color: #fff; }
.dates { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.dates label { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-muted); font-weight: 500; }

input[type="date"], input[type="number"], input[type="text"], input[type="search"], select {
  height: 40px; padding: 0 12px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); min-width: 0; transition: border-color var(--ease), box-shadow var(--ease);
}
input:focus, select:focus { outline: none; border-color: var(--emerald); box-shadow: 0 0 0 3px rgba(5, 150, 105, 0.16); }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-md); }
.cards { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 20px; }
.kpi { position: relative; padding: 20px 20px 18px; overflow: hidden; transition: transform var(--ease), box-shadow var(--ease); }
.kpi::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; background: var(--emerald); opacity: 0.9; }
.kpi:nth-child(2)::before { background: var(--navy); }
.kpi:hover { transform: translateY(-2px); box-shadow: var(--shadow-md), 0 12px 28px -18px rgba(11, 31, 58, 0.35); }
.kpi-head { display: flex; justify-content: space-between; align-items: center; color: var(--text-muted); font-size: 13.5px; font-weight: 600; }
.ico { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; font-style: normal; }
.ico-green { background: var(--emerald-soft); color: var(--emerald); }
.ico-navy { background: var(--navy-soft); color: var(--navy); }
.ico-red { background: var(--red-soft); color: var(--red); }
.kpi-value { margin-top: 10px; font: 800 clamp(1.5rem, 2.4vw, 1.875rem)/1.15 var(--font-display); letter-spacing: -0.02em; color: var(--navy); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi-sub { margin-top: 6px; font-size: 13px; color: var(--text-muted); font-variant-numeric: tabular-nums; min-height: 20px; }
.trend-up { color: var(--emerald-ink); font-weight: 600; }
.trend-down { color: var(--red); font-weight: 600; }
#netCard.is-loss::before, #lossCard.has-loss::before { background: var(--red); }
#netCard.is-loss .kpi-value, #lossCard.has-loss .kpi-value { color: var(--red); }
#netCard.is-loss .ico { background: var(--red-soft); color: var(--red); }
#lossCard:not(.has-loss)::before { background: var(--border-strong); }
#lossCard:not(.has-loss) .kpi-value { color: var(--text-muted); }

/* Panels */
.panel { padding: 22px 22px 18px; margin-bottom: 20px; }
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.panel-head .muted { font-size: 13.5px; margin-top: 2px; }
.seg { display: inline-flex; padding: 3px; border-radius: 11px; background: var(--surface-2); border: 1px solid var(--border); }
.seg button { height: 32px; padding: 0 14px; border: 0; border-radius: 8px; background: transparent; color: var(--text-muted); font-weight: 600; font-size: 13px; cursor: pointer; transition: all var(--ease); }
.seg button:hover { color: var(--navy); }
.seg button.active { background: var(--surface); color: var(--navy); box-shadow: var(--shadow-sm), 0 0 0 1px var(--border); }
.legend { display: flex; flex-wrap: wrap; gap: 16px; font-size: 12.5px; color: var(--text-muted); margin-bottom: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.dot-green { background: var(--emerald); }
.dot-navy { background: var(--navy); }
.dot-line { width: 16px; height: 3px; border-radius: 2px; background: var(--emerald-ink); }
.dot-red { width: 10px; height: 10px; border-radius: 50%; background: var(--red); }

/* Chart */
.chart-box { position: relative; overflow-x: auto; overflow-y: hidden; min-height: 320px; }
.chart-box svg { display: block; touch-action: pan-x; }
.chart-empty { display: grid; place-items: center; height: 320px; color: var(--text-muted); }
.chart-box .grid { stroke: var(--border); stroke-width: 1; }
.chart-box .axis-zero { stroke: rgba(11, 31, 58, 0.32); stroke-width: 1; }
.chart-box .tick, .chart-box .xlab { fill: var(--text-muted); font: 500 11px var(--font-body); font-variant-numeric: tabular-nums; }
.chart-box .bar-inc { fill: var(--emerald); }
.chart-box .bar-exp { fill: var(--navy); }
.chart-box .hover-band { fill: rgba(11, 31, 58, 0.045); transition: x 120ms ease; }
.chart-box .net-line { fill: none; stroke: var(--emerald-ink); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-box .net-dot { fill: #fff; stroke: var(--emerald-ink); stroke-width: 2; }
.chart-box .net-dot.loss { fill: var(--red); stroke: #fff; }
.chart-tip {
  position: absolute; top: 8px; transform: translateX(-50%); min-width: 190px; pointer-events: none;
  padding: 10px 12px; border-radius: 12px; background: var(--navy); color: #fff; font-size: 12.5px;
  box-shadow: var(--shadow-lg); font-variant-numeric: tabular-nums;
}
.chart-tip strong { display: block; margin-bottom: 6px; font: 600 12.5px var(--font-display); }
.chart-tip div { display: flex; align-items: center; gap: 6px; }
.chart-tip div span { margin-left: auto; font-weight: 600; }
.chart-tip .dot-navy { background: #8FA3C2; }
.chart-tip .tip-net { margin-top: 6px; padding-top: 6px; border-top: 1px solid rgba(255, 255, 255, 0.15); }
.chart-tip .tip-net.pos span { color: #6EE7B7; }
.chart-tip .tip-net.neg span { color: #FCA5A5; }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-md); max-height: 560px; overflow-y: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
thead th {
  position: sticky; top: 0; z-index: 1; background: var(--surface-2); text-align: left;
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); font-weight: 600;
  padding: 11px 14px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
tbody td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { transition: background var(--ease); }
tbody tr:hover { background: var(--surface-2); }
tr.is-today td:first-child { box-shadow: inset 3px 0 0 var(--emerald); }
.num { text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.pos { color: var(--emerald-ink); font-weight: 600; }
.neg { color: var(--red); font-weight: 600; }
.amt-exp { color: var(--navy); font-weight: 600; }
.d-main { display: block; font-weight: 500; white-space: nowrap; }
.d-sub { display: block; font-size: 12px; color: var(--text-muted); }
.bar-col { width: 28%; min-width: 150px; }
.split { display: flex; height: 8px; min-width: 120px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.s-inc { background: var(--emerald); }
.s-exp { background: var(--navy); }
.desc { min-width: 180px; max-width: 340px; }
.cat { display: inline-block; padding: 2px 9px; border-radius: 7px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12.5px; white-space: nowrap; }
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-income { background: var(--emerald-soft); color: var(--emerald-ink); }
.badge-expense { background: var(--navy-soft); color: var(--navy); }
.act-col { width: 88px; text-align: right; white-space: nowrap; }
.note { text-align: center; color: var(--text-muted); font-size: 13px; }
.tools { display: flex; gap: 8px; flex-wrap: wrap; }
.tools input[type="search"] { width: 260px; }
.empty { padding: 36px 16px; text-align: center; color: var(--text-muted); }

.foot { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; padding: 8px 0 40px; font-size: 13px; color: var(--text-muted); }

/* Dialog */
dialog {
  width: min(480px, calc(100vw - 32px)); padding: 0; border: 0; border-radius: var(--radius-lg);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
dialog[open] { animation: pop 200ms ease; }
dialog::backdrop { background: rgba(11, 31, 58, 0.45); backdrop-filter: blur(3px); }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.98); } to { opacity: 1; transform: none; } }
dialog form { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; }
.type-toggle { display: grid; grid-template-columns: 1fr 1fr; padding: 3px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--border); }
.type-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.type-toggle span { display: grid; place-items: center; height: 38px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--text-muted); cursor: pointer; transition: all var(--ease); }
.type-toggle input:checked + span { background: var(--surface); box-shadow: var(--shadow-sm), 0 0 0 1px var(--border); }
.type-toggle input[value="income"]:checked + span { color: var(--emerald-ink); }
.type-toggle input[value="expense"]:checked + span { color: var(--navy); }
.type-toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--navy); }
.field input, .field select { width: 100%; font-weight: 400; }
.field input[aria-invalid="true"] { border-color: var(--red); }
.form-error { padding: 10px 12px; border-radius: var(--radius-sm); background: var(--red-soft); color: var(--red); font-size: 13.5px; font-weight: 500; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dlg-foot .btn-primary { margin-left: 0; }

/* Toasts */
.toasts { position: fixed; right: 20px; bottom: 20px; z-index: 50; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 40px); }
.toast {
  display: flex; align-items: center; gap: 12px; min-width: 260px; max-width: 420px;
  padding: 12px 12px 12px 14px; border-radius: 12px; background: var(--navy); color: #fff;
  box-shadow: var(--shadow-lg); font-size: 14px; border-left: 4px solid #8FA3C2; animation: pop 200ms ease;
  transition: opacity 200ms ease, transform 200ms ease;
}
.toast span { flex: 1; overflow-wrap: anywhere; }
.toast.out { opacity: 0; transform: translateY(6px); }
.toast-success { border-left-color: #34D399; }
.toast-error { border-left-color: #F87171; }
.toast-act { background: rgba(255, 255, 255, 0.12); border: 0; color: #fff; font-weight: 600; padding: 6px 10px; border-radius: 8px; cursor: pointer; }
.toast-act:hover { background: rgba(255, 255, 255, 0.22); }
.toast .icon-btn { width: 28px; height: 28px; color: rgba(255, 255, 255, 0.7); }
.toast .icon-btn:hover { background: rgba(255, 255, 255, 0.12); color: #fff; }

/* Responsive */
@media (max-width: 1024px) { .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 860px) {
  .nav { display: none; }
  .hero { flex-direction: column; align-items: stretch; padding-top: 28px; }
  .filters, .chips, .dates { align-items: stretch; justify-content: flex-start; }
  .tools, .tools input[type="search"] { width: 100%; }
  .tools input[type="search"] { flex: 1 1 100%; }
}
@media (max-width: 560px) {
  .wrap { width: calc(100% - 28px); }
  .cards { grid-template-columns: 1fr; gap: 12px; }
  .panel { padding: 16px 14px 14px; }
  .dates label { flex: 1 1 140px; }
  .dates input { flex: 1; width: 100%; }
  .grid2 { grid-template-columns: 1fr; }
  .toasts { left: 14px; right: 14px; bottom: 14px; max-width: none; }
  .toast { min-width: 0; max-width: none; }
}
@media (max-width: 480px) {
  .topbar .btn-primary span { display: none; }
  .topbar .btn-primary { width: 44px; height: 44px; padding: 0; }
  .chip, .seg button { height: 38px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* Auth */
body:not(.authed) .topbar, body:not(.authed) main { display: none; }
body.authed .auth { display: none; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: radial-gradient(1200px 500px at 50% -10%, var(--navy-soft), transparent 70%), var(--bg); }
.auth [hidden] { display: none !important; }
.auth-card { width: min(440px, 100%); padding: 32px 28px 26px; }
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 22px; font: 800 1.125rem var(--font-display); letter-spacing: -0.02em; color: var(--navy); }
.auth-card h1 { font-size: clamp(1.5rem, 4vw, 1.875rem); }
.auth-card h2 { font-size: 1.5rem; margin-top: 4px; }
.auth-card .muted { margin-top: 8px; font-size: 14px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 22px; }
.pw-wrap { position: relative; display: block; }
.pw-wrap input { padding-right: 64px; }
.pw-toggle { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); height: 30px; padding: 0 10px; border: 0; border-radius: 8px; background: transparent; color: var(--text-muted); font-weight: 600; font-size: 13px; cursor: pointer; }
.pw-toggle:hover { background: var(--navy-soft); color: var(--navy); }
.auth-msg { padding: 10px 12px; border-radius: var(--radius-sm); font-size: 13.5px; font-weight: 500; margin-top: 12px; }
.auth-card form .auth-msg { margin-top: 0; }
.auth-msg.error { background: var(--red-soft); color: var(--red); }
.auth-msg.success { background: var(--emerald-soft); color: var(--emerald-ink); }
.auth-msg.info { background: var(--navy-soft); color: var(--navy); }
.auth-submit { width: 100%; height: 44px; margin-left: 0; margin-top: 4px; }
.auth-switch { margin-top: 18px; text-align: center; font-size: 14px; color: var(--text-muted); }
.verify-ico { display: grid; place-items: center; width: 56px; height: 56px; border-radius: 16px; background: var(--emerald-soft); color: var(--emerald); margin-bottom: 14px; }
#authVerifyView .auth-submit { margin-top: 18px; }
.user-chip { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; color: var(--text-muted); }
.topbar .btn-primary { margin-left: 0; }
.topbar-inner .btn-primary { margin-left: auto; }
@media (max-width: 860px) { .user-chip { display: none; } }