:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --border: #334155;
  --accent: #6366f1;
  --accent-2: #8b5cf6;
  --good: #22c55e;
  --bad: #ef4444;
  --warn: #f59e0b;
  --radius: 8px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 14px; line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* SVG icon system. Pulled from the Lucide icon set as inline symbols
   (see partials/icons.html) - no third-party CDN, fully self-hosted.
   Stroke uses currentColor so an icon inherits the surrounding text colour. */
.icon {
  width: 16px;
  height: 16px;
  display: inline-block;
  vertical-align: -3px;
  flex-shrink: 0;
}
.icon-lg { width: 20px; height: 20px; vertical-align: -4px; }
.icon-sm { width: 13px; height: 13px; vertical-align: -2px; }

.topnav {
  /* Bar spans the page so the gradient + drop-shadow read full-width;
     the inner wrapper centers the content to match the body container. */
  background: linear-gradient(180deg, var(--panel) 0%, color-mix(in srgb, var(--panel) 92%, black) 100%);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  box-shadow: 0 1px 0 rgba(255,255,255,0.02), 0 4px 12px rgba(0,0,0,0.25);
}
.topnav-inner {
  max-width: 1400px;     /* matches .container so the brand sits in line */
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: stretch;
}
.topnav .brand {
  display: flex; align-items: center; gap: 8px;
  font-size: 16px;
  padding: 0 18px 0 0;
  margin-right: 12px;
  border-right: 1px solid var(--border);
  color: var(--text);
}
.topnav .brand svg { color: var(--accent); }
.topnav ul {
  display: flex; gap: 2px; list-style: none; padding: 0; margin: 0;
  flex: 1; flex-wrap: wrap;
}
.topnav li { display: flex; }
.topnav a {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 14px 14px;
  color: var(--muted);
  font-size: 13px;
  position: relative;
  transition: color 0.15s, background 0.15s;
}
.topnav a:hover {
  color: var(--text);
  text-decoration: none;
  background: rgba(255,255,255,0.04);
}
.topnav a.on {
  color: var(--text);
  background: rgba(99, 102, 241, 0.08);
}
.topnav a.on::after {
  content: '';
  position: absolute;
  left: 12px; right: 12px; bottom: 0;
  height: 2px;
  background: var(--accent);
  border-radius: 2px 2px 0 0;
}
.topnav a .icon { color: var(--muted); transition: color 0.15s; }
.topnav a:hover .icon,
.topnav a.on .icon { color: var(--accent); }
@media (max-width: 900px) {
  .topnav a span { display: none; }   /* icons-only on narrow screens */
  .topnav a { padding: 14px 12px; }
}

.container { max-width: 1400px; margin: 0 auto; padding: 24px; }

/* Auth pages: standalone shell, no topnav, single centred card. */
.auth-body { display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.auth-shell { width: 100%; max-width: 420px; padding: 24px; }
.auth-card { padding: 28px; }
.auth-card h1 { margin-top: 0; font-size: 22px; }
.auth-card h2 { font-size: 16px; }
.auth-card a { color: var(--accent); }

h1, h2, h3 { margin: 0 0 12px 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 18px; color: var(--text); }
h3 { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }

/* --- Layout primitives -----------------------------------------------
   Every page uses two core building blocks:
     .card  - a panel of content with consistent border/padding/margin
     .grid  - a CSS Grid container that lays out cards or KPIs in columns
   Cards/KPIs inside a grid are equal-height by default. Use .grid.masonry
   when neighbours have wildly different row counts and you want columnar
   packing instead.
*/

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 18px;
}
.card h2 { margin-top: 0; }
.card h2:not(:first-child) { margin-top: 18px; }

/* Accent variants - use a coloured left border to flag a card visually
   (income, danger, warning, info). Replaces a bunch of repeated inline
   styles across templates. */
.card.accent-good { border-left: 3px solid var(--good); }
.card.accent-bad  { border-left: 3px solid var(--bad);  }
.card.accent-warn { border-left: 3px solid var(--warn); }
.card.accent-info { border-left: 3px solid var(--accent); }

.grid {
  display: grid;
  gap: 16px;
  align-items: stretch;     /* cells match the tallest in the row */
  margin-bottom: 18px;      /* consistent gap between sections */
}
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) {
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr; }
}

/* Cards/KPIs as direct children of a grid fill their cell so neighbours
   visibly share the same height. Internal layout is flex-column so any
   trailing form/footer can be pushed to the bottom via margin-top:auto. */
.grid > .card,
.grid > .kpi {
  height: 100%;
  display: flex;
  flex-direction: column;
  margin-bottom: 0;          /* the parent .grid handles vertical spacing */
}
.grid > .card > form:last-child,
.grid > .kpi  > form:last-child,
.grid > .kpi  > .kpi-actions,
.grid > .card > .card-footer {
  margin-top: auto;
}

/* Masonry packing - opt-in for grids whose cards have very different
   heights (e.g. monthly outgoings breakdown). Drops grid layout in
   favour of CSS columns so cards keep natural height and pack densely. */
.grid.masonry,
.masonry-2 {            /* legacy alias, kept while templates migrate */
  display: block;
  column-count: 2;
  column-gap: 16px;
  align-items: initial;
  margin-bottom: 18px;  /* match .grid's gap so the next section doesn't snap up */
}
.grid.masonry > .card,
.masonry-2 > .card {
  break-inside: avoid;
  display: block;
  height: auto;
  width: 100%;
  margin-bottom: 16px;
}
@media (max-width: 900px) {
  .grid.masonry, .masonry-2 { column-count: 1; }
}

/* KPIs - small summary tiles. A KPI used outside a grid stacks naturally;
   inside a grid it inherits the equal-height/flex-column rules above. */
.kpi {
  padding: 14px;
  background: var(--panel-2);
  border-radius: var(--radius);
  min-height: 90px;
}
.kpi .label  { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.kpi .value  { font-size: 24px; font-weight: 600; margin-top: 4px; }
.kpi .delta  { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.good .value { color: var(--good); }
.kpi.bad  .value { color: var(--bad); }
.kpi.warn .value { color: var(--warn); }

/* Legacy section modifiers kept for back-compat. With the new grid
   defaults they're effectively no-ops; .kpi-row gets a slightly bigger
   bottom margin since it's typically followed by a major content card. */
.grid.kpi-row { margin-bottom: 24px; }
.grid.dash-section { margin-bottom: 18px; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); font-size: 13px; }
th { color: var(--muted); font-weight: 500; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:hover td { background: rgba(255,255,255,0.02); }

.tag { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 11px; background: var(--panel-2); border: 1px solid var(--border); }
.tag.income { background: rgba(34, 197, 94, 0.15); color: var(--good); border-color: var(--good); }
.tag.expense { background: rgba(239, 68, 68, 0.10); color: var(--bad); }
.tag.debt { background: rgba(245, 158, 11, 0.10); color: var(--warn); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }

.bar {
  height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden;
}
.bar > span { display: block; height: 100%; background: var(--accent); }

input, select, textarea, button {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px;
  font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
/* Hide native number-input spinners - the up/down arrows render as a tiny
   visual chunk on the right of the field which is unusable at our font size */
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type=number] { -moz-appearance: textfield; appearance: textfield; }
button { cursor: pointer; background: var(--accent); border-color: var(--accent); color: white; }
button:hover { background: var(--accent-2); border-color: var(--accent-2); }
button.ghost { background: transparent; color: var(--muted); border-color: var(--border); }
button.ghost:hover { color: var(--text); border-color: var(--text); background: var(--panel-2); }
button.danger { background: var(--bad); border-color: var(--bad); }
button.icon-btn {
  padding: 5px 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
button.icon-btn .icon { vertical-align: 0; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

form.inline { display: flex; gap: 6px; flex-wrap: wrap; }
form.inline input, form.inline select { padding: 5px 8px; font-size: 12px; }
form.inline button { padding: 5px 10px; font-size: 12px; }

.muted { color: var(--muted); }
.good { color: var(--good); }
.bad { color: var(--bad); }
.warn { color: var(--warn); }
.mono { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
.right { text-align: right; }
.center { text-align: center; }

.pillbar { display: flex; gap: 4px; }
.pillbar .pill { display: flex; align-items: center; padding: 4px 10px; background: var(--panel-2); border-radius: 12px; font-size: 12px; }

.chart-wrap { position: relative; height: 280px; }
.chart-wrap.tall { height: 360px; }

.recommendation {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px; background: var(--panel-2); border-radius: var(--radius);
  border-left: 3px solid var(--warn); margin-bottom: 8px;
}
.recommendation .rec-savings { color: var(--good); font-weight: 600; white-space: nowrap; }
.recommendation .rec-body { flex: 1; }
.recommendation .rec-body strong { display: block; }
.recommendation .rec-body small { color: var(--muted); }

.chat .msg { padding: 12px 14px; border-radius: 8px; margin-bottom: 10px; max-width: 85%; }
.chat .msg.user { background: var(--accent); margin-left: auto; color: white; }
.chat .msg.assistant { background: var(--panel-2); }

/* Markdown content rendered into chat bubbles */
.msg-content { line-height: 1.55; }
.msg-content > *:first-child { margin-top: 0; }
.msg-content > *:last-child { margin-bottom: 0; }
.msg-content p { margin: 0 0 8px 0; }
.msg-content h1 { font-size: 18px; margin: 14px 0 6px; }
.msg-content h2 { font-size: 16px; margin: 12px 0 6px; }
.msg-content h3 { font-size: 14px; margin: 10px 0 4px; color: var(--text); text-transform: none; letter-spacing: 0; }
.msg-content ul, .msg-content ol { margin: 6px 0 8px 0; padding-left: 22px; }
.msg-content li { margin-bottom: 3px; }
.msg-content code {
  background: rgba(0,0,0,0.25);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
}
.msg-content pre {
  background: rgba(0,0,0,0.3);
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 12px;
}
.msg-content pre code { background: transparent; padding: 0; font-size: inherit; }
.msg-content strong { color: var(--text); }
.msg-content em { color: var(--text); }
.msg.user .msg-content,
.msg.user .msg-content strong,
.msg.user .msg-content em { color: white; }

/* Blinking cursor at the tail of a streaming response */
.cursor {
  display: inline-block;
  width: 8px;
  height: 14px;
  background: var(--accent);
  vertical-align: -2px;
  margin-left: 2px;
  animation: cursorBlink 1s steps(2, start) infinite;
}
@keyframes cursorBlink { to { opacity: 0; } }
.msg-content.streaming { min-height: 1.2em; }

.scenario-row { display: grid; grid-template-columns: 2fr 1fr 1fr auto; gap: 8px; margin-bottom: 6px; }
.scenario-row input, .scenario-row select { width: 100%; }

/* Modal: a backdrop + centered card, hidden by default. Toggle visibility
   by adding/removing .open on the .modal element. Click backdrop or .close
   to dismiss. Lightweight, no library. */
.modal { position: fixed; inset: 0; display: none; z-index: 100; }
.modal.open { display: block; }
.modal .backdrop {
  position: absolute; inset: 0; background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}
.modal .modal-card {
  position: relative; max-width: 600px; width: calc(100% - 32px);
  margin: 80px auto 0; background: var(--panel);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 22px; max-height: calc(100vh - 120px); overflow: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}
.modal .modal-card .close {
  position: absolute; top: 10px; right: 12px;
  background: transparent; border: none; color: var(--muted);
  font-size: 22px; cursor: pointer; padding: 4px 8px;
}
.modal .modal-card .close:hover { color: var(--text); background: var(--panel-2); border-radius: 4px; }

/* Make a KPI card behave like a button when it has data-modal */
.kpi[data-modal] { cursor: pointer; transition: background 0.15s; }
.kpi[data-modal]:hover { background: var(--panel); }

/* Click-to-edit affordance for account rows on /debt: subtle hover hint */
tr[hx-get*="/edit"]:hover > td { background: rgba(255,255,255,0.04); }

/* Top-of-page action buttons (Start month / Generate recurring / Clear).
   Without this they pick up different padding from <a> wrappers vs forms,
   so they end up at slightly different sizes and shapes. */
.action-btn {
  padding: 8px 14px;
  font-size: 13px;
  line-height: 1.2;
  min-height: 34px;
}
.btn-link { display: inline-flex; }
.btn-link, .btn-row form { margin: 0; }
.btn-row form button.action-btn { display: inline-flex; align-items: center; }

/* "Jump to row" pulse - highlights a transaction briefly when the user
   clicks its category line in a summary box. */
@keyframes rowFlash {
  0%   { background: rgba(99,102,241,0.35); }
  100% { background: transparent; }
}
.row-flash > td { animation: rowFlash 1.5s ease-out; }
.cat-jump { transition: background 0.15s; }
.cat-jump:hover { background: rgba(255,255,255,0.05); }
.cat-jump:hover td { background: rgba(255,255,255,0.05); }
tr.cat-jump:hover > td { background: rgba(255,255,255,0.05); }
