/* Ledger — one stylesheet, no build step, no CDN.
   Financial data should stay on your machine; that includes the fonts. */

:root {
  color-scheme: light dark;

  --bg: #fbfaf8;
  --surface: #ffffff;
  --surface-2: #f4f2ee;
  --border: #e3ded6;
  --border-strong: #cfc7ba;

  --text: #1c1917;
  --text-dim: #6b635a;
  --text-faint: #948a7e;

  --accent: #1f6f5c;
  --accent-soft: #e4f0ec;

  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --warn: #9a6410;
  --warn-soft: #fdf1de;
  --good: #1f6f5c;
  --good-soft: #e4f0ec;

  --radius: 10px;
  --shadow: 0 1px 2px rgb(28 25 23 / 6%), 0 4px 12px rgb(28 25 23 / 4%);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17150f;
    --surface: #201d16;
    --surface-2: #2a261d;
    --border: #353025;
    --border-strong: #4a4335;

    --text: #f0ece3;
    --text-dim: #a89f8f;
    --text-faint: #7d7466;

    --accent: #6ec2a8;
    --accent-soft: #1d3a32;

    --danger: #f2857c;
    --danger-soft: #3a1f1c;
    --warn: #e0ac5c;
    --warn-soft: #37291459;
    --good: #6ec2a8;
    --good-soft: #1d3a32;

    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 14px rgb(0 0 0 / 22%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------------------------------------------------------------- layout */

header.top {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.top-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 28px;
  height: 58px;
}

.brand {
  font-weight: 640;
  letter-spacing: -0.02em;
  font-size: 16px;
  color: var(--text);
}
.brand:hover { text-decoration: none; }

nav.main { display: flex; gap: 2px; margin-right: auto; }

nav.main a {
  padding: 6px 11px;
  border-radius: 7px;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 500;
}
nav.main a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
nav.main a.active { background: var(--surface-2); color: var(--text); }

.badge-count {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 20px;
  background: var(--warn-soft);
  color: var(--warn);
  font-size: 11px;
  font-weight: 640;
  font-variant-numeric: tabular-nums;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 24px 80px;
}

/* ----------------------------------------------------------- month picker */

.month-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
}

.month-nav h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 640;
  letter-spacing: -0.02em;
}

.step {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
}
.step:hover { border-color: var(--border-strong); color: var(--text); text-decoration: none; }

.today-link { font-size: 13px; color: var(--text-faint); margin-left: 2px; }

/* ------------------------------------------------------------------ cards */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 15px 18px;
  border-bottom: 1px solid var(--border);
}

.card-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 640;
  letter-spacing: 0.045em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.card-body { padding: 18px; }

.grid { display: grid; gap: 16px; }
.grid.stats { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid.halves { grid-template-columns: 1.35fr 1fr; align-items: start; }

@media (max-width: 880px) {
  .grid.halves { grid-template-columns: 1fr; }
  .top-inner { gap: 14px; overflow-x: auto; }
}

/* ------------------------------------------------------------- stat tiles */

.stat { padding: 16px 18px; }

.stat .label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.045em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.stat .value {
  margin-top: 5px;
  font-size: 25px;
  font-weight: 620;
  letter-spacing: -0.025em;
  font-variant-numeric: tabular-nums;
}

.stat .sub { margin-top: 3px; font-size: 13px; color: var(--text-dim); }

.pos { color: var(--good); }
.neg { color: var(--danger); }

/* ------------------------------------------------------------------- bars */

.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 14px; padding: 9px 0; }
.bar-row + .bar-row { border-top: 1px solid var(--border); }

.bar-label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 14px;
  min-width: 0;
}
.bar-label .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.bar-amount {
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  font-weight: 560;
  white-space: nowrap;
}

.bar-track {
  grid-column: 1 / -1;
  position: relative;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}

.bar-fill { height: 100%; border-radius: 3px; background: var(--accent); }
.bar-fill.over { background: var(--danger); }
.bar-fill.warn { background: var(--warn); }

/* The budget marker: a hairline showing where the cap sits on the bar. */
.bar-cap {
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 2px;
  background: var(--text-faint);
  opacity: 0.65;
}

.tag {
  padding: 1px 7px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 620;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.tag.over { background: var(--danger-soft); color: var(--danger); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.good { background: var(--good-soft); color: var(--good); }
.tag.flat { background: var(--surface-2); color: var(--text-dim); }

/* ------------------------------------------------------------ trend chart */

.trend { display: flex; align-items: flex-end; gap: 14px; height: 132px; padding-top: 6px; }
.trend-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; }
.trend-bars { flex: 1; display: flex; align-items: flex-end; gap: 3px; width: 100%; }
.trend-bar { flex: 1; border-radius: 3px 3px 0 0; min-height: 2px; }
.trend-bar.income { background: var(--accent); opacity: 0.85; }
.trend-bar.spend { background: var(--border-strong); }
.trend-label { font-size: 11px; color: var(--text-faint); font-weight: 560; }

.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-dim); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.swatch { width: 9px; height: 9px; border-radius: 2px; }

/* ------------------------------------------------------------ benchmark */

.bench-row { display: grid; grid-template-columns: 74px 1fr auto; gap: 12px; align-items: center; padding: 8px 0; }
.bench-name { font-size: 13px; font-weight: 560; text-transform: capitalize; }
.bench-track { position: relative; height: 8px; border-radius: 4px; background: var(--surface-2); }
.bench-fill { height: 100%; border-radius: 4px; background: var(--accent); }
.bench-fill.over { background: var(--warn); }
.bench-fill.under { background: var(--danger); }
.bench-target { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text-faint); }
.bench-value { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--text-dim); white-space: nowrap; }

/* ------------------------------------------------------------- alert list */

.alert {
  display: flex;
  gap: 11px;
  padding: 12px 0;
  align-items: flex-start;
}
.alert + .alert { border-top: 1px solid var(--border); }

.dot { width: 7px; height: 7px; border-radius: 50%; margin-top: 7px; flex: none; }
.dot.high { background: var(--danger); }
.dot.medium { background: var(--warn); }
.dot.low { background: var(--text-faint); }
.dot.good { background: var(--good); }

.alert .t { font-size: 14px; font-weight: 580; }
.alert .d { font-size: 13px; color: var(--text-dim); margin-top: 2px; }

/* ----------------------------------------------------------------- tables */

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }

th {
  text-align: left;
  padding: 9px 12px;
  font-size: 11px;
  font-weight: 640;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }

.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 540; }
.dim { color: var(--text-dim); }
.faint { color: var(--text-faint); font-size: 13px; }
.mono { font-family: var(--mono); font-size: 12px; }
.narration { max-width: 380px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ----------------------------------------------------------------- forms */

input, select, button, textarea {
  font: inherit;
  color: inherit;
}

input[type="text"], input[type="date"], input[type="number"],
input[type="search"], select, textarea {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  min-width: 0;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: -1px;
  border-color: var(--accent);
}

.btn {
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  font-size: 14px;
  font-weight: 540;
}
.btn:hover { border-color: var(--border-strong); }

.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }

@media (prefers-color-scheme: dark) {
  .btn.primary { color: #12241f; }
}

/* Quick add: a form you use dozens of times in a row, so the targets are big
   and the amount field is the first thing your thumb lands on. */
.quick-add {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.quick-add .field { display: flex; flex-direction: column; gap: 5px; }
.quick-add .field.wide { grid-column: 1 / -1; }
.quick-add .field > span {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--text-dim);
}
.quick-add input, .quick-add select { padding: 10px 12px; font-size: 15px; }
.quick-add .amount-input {
  font-size: 26px;
  font-weight: 620;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  padding: 10px 12px;
}

@media (max-width: 620px) {
  .quick-add { grid-template-columns: 1fr; }
}

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 16px; }
.filters input[type="search"] { flex: 1; min-width: 180px; }

.cat-select {
  padding: 4px 7px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  font-size: 13px;
  max-width: 190px;
}
.cat-select:hover { border-color: var(--border); background: var(--surface); }
.cat-select.unreviewed { border-color: var(--warn); background: var(--warn-soft); }

.budget-input { width: 118px; text-align: right; font-variant-numeric: tabular-nums; }

.last4-input {
  width: 92px;
  font-family: var(--mono);
  font-size: 13px;
  letter-spacing: 0.06em;
}
/* An account with no masked digits cannot receive alerts, so say so visually
   rather than leaving it looking merely blank. */
.last4-input.unset {
  border-color: var(--warn);
  background: var(--warn-soft);
}
.last4-input.unset::placeholder { color: var(--warn); opacity: 0.85; }

.opening-input {
  width: 128px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------------ misc */

.empty { padding: 46px 24px; text-align: center; color: var(--text-dim); }
.empty h3 { margin: 0 0 6px; font-size: 16px; color: var(--text); }
.empty p { margin: 0 auto; max-width: 460px; font-size: 14px; }
.empty code {
  display: inline-block;
  margin-top: 12px;
  padding: 6px 11px;
  border-radius: 7px;
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 13px;
}

.note {
  padding: 11px 14px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-dim);
}
.note.error { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.note.ok { background: var(--good-soft); border-color: var(--good); color: var(--good); }

.section-gap { margin-top: 20px; }

.sparkline { width: 100%; height: 58px; display: block; }
.sparkline path { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }

#toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translate(-50%, 14px);
  padding: 9px 16px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  font-weight: 540;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s, transform 0.16s;
  z-index: 60;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Destructive controls: present, but never the thing your eye lands on. */
.danger-card { border-color: color-mix(in srgb, var(--danger) 28%, var(--border)); }

.danger-summary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 15px 18px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 640;
  letter-spacing: 0.045em;
  text-transform: uppercase;
  color: var(--danger);
  list-style: none;
}
.danger-summary::-webkit-details-marker { display: none; }
/* The marker belongs to the title, not as a third flex item — otherwise
   space-between pushes the title into the middle of the row. */
.danger-summary > span:first-child::before { content: "▸ "; }
details[open] > .danger-summary > span:first-child::before { content: "▾ "; }
.danger-summary .faint { text-transform: none; letter-spacing: 0; font-weight: 500; }

.reset-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 13px 0;
}
.reset-row + .reset-row { border-top: 1px solid var(--border); }
.reset-row .t { font-size: 14px; font-weight: 580; }
.reset-row .d { font-size: 13px; color: var(--text-dim); margin-top: 2px; max-width: 62ch; }

.btn.danger { border-color: var(--danger); color: var(--danger); white-space: nowrap; }
.btn.danger:hover { background: var(--danger); color: var(--surface); }

details.raw summary { cursor: pointer; font-size: 13px; color: var(--text-dim); }
details.raw pre {
  margin: 8px 0 0;
  padding: 12px;
  border-radius: 8px;
  background: var(--surface-2);
  overflow-x: auto;
  font-size: 12px;
}

.name-input {
  width: 100%;
  min-width: 130px;
  border-color: transparent;
  background: transparent;
  font-weight: 500;
}
.name-input:hover { border-color: var(--border); background: var(--surface); }

/* Three answers to three different questions about the same month. */
.view-tabs {
  display: flex;
  gap: 2px;
  padding: 10px 18px 0;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.view-tab {
  padding: 7px 13px;
  border-radius: 8px 8px 0 0;
  font-size: 13px;
  font-weight: 560;
  color: var(--text-dim);
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.view-tab:hover { color: var(--text); text-decoration: none; background: var(--surface-2); }
.view-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  background: transparent;
}
.view-note {
  margin: 10px 18px 0;
  font-size: 13px;
  color: var(--text-faint);
}

/* ------------------------------------------------- phone (standalone PWA) */

/* Grid and flex items will not shrink below their content's min-content width
   unless told to. On a 375px iPhone the three view tabs — 383px of text that
   cannot wrap — dragged the entire page wider than the screen, so every
   swipe drifted sideways. */
.grid > * { min-width: 0; }
.view-tabs { min-width: 0; }

/* Belt and braces: nothing should ever scroll the page sideways. */
html, body { overflow-x: hidden; }

/* The tab strips scroll rather than wrap, so hide the scrollbar and give them
   the momentum scrolling a native app has. */
nav.main, .view-tabs, .top-inner, .filters {
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
nav.main::-webkit-scrollbar,
.view-tabs::-webkit-scrollbar,
.top-inner::-webkit-scrollbar { display: none; }

/* Added to the Home Screen, the page owns the whole display — including the
   area behind the notch and the home indicator. Without these insets the nav
   sits under the status bar and the last row under the home bar. */
@supports (padding: max(0px)) {
  header.top { padding-top: env(safe-area-inset-top); }
  .top-inner {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
  }
  main {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
    /* 80px is the desktop value; never shrink it, only grow for the bar. */
    padding-bottom: max(80px, env(safe-area-inset-bottom));
  }
}

@media (max-width: 640px) {
  /* Touch targets: 14px text in a 6px-tall box is a miss on a phone. */
  nav.main a { padding: 8px 12px; font-size: 15px; }
  .top-inner { height: 54px; gap: 12px; }
  .stat .value { font-size: 26px; }
}

/* ------------------------------------------------------------------ login */

.login-wrap {
  min-height: 70vh;
  display: grid;
  place-items: center;
}
.login-card { width: min(360px, 100%); padding: 28px; text-align: left; }
.login-card h1 { margin: 0 0 2px; font-size: 22px; letter-spacing: -0.02em; }
.login-form { display: grid; gap: 8px; margin-top: 20px; }
.login-form label { font-size: 13px; color: var(--text-dim); }
.login-form input {
  padding: 10px 12px;
  font-size: 16px;  /* below 16px, iOS Safari zooms the page on focus */
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.login-form button { margin-top: 6px; padding: 10px; font-size: 15px; }
.login-error {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--neg, #c2410c);
}

/* Naming a category from the review page. */
/* Wraps: the row sits in a table cell and the Save button was cut off. */
.new-cat { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center; }
.new-cat input { flex: 1 1 110px; min-width: 0; padding: 5px 8px; font-size: 14px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); }
.new-cat select { font-size: 13px; }

.key-reveal { font-size: 17px; letter-spacing: 0.06em; padding: 8px 0; user-select: all; }

.good-note { color: var(--pos, #1f6f5c); margin: 0 0 8px; }
