:root {
  /* Layout: one ledger column (max 760px). Month masthead, totals strip closed
     with an accounting double rule, composer, tabs, panel. Figures in mono. */
  --paper: #f2f3f8;
  --surface: #ffffff;
  --ink: #161a2e;
  --muted: #5a5f7a;
  --rule: #d9dbe7;
  --accent: #3340a8;
  --on-accent: #ffffff;
  --accent-soft: #e6e8f8;
  --good: #1c7448;
  --warn: #8f5300;
  --crit: #b3261e;
  --series-1: #4a5bd0;
  --series-2: #b0690f;
  --font-display: 'Instrument Serif', 'Iowan Old Style', Georgia, serif;
  --font-body: 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-num: 'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #11121b;
    --surface: #1a1c29;
    --ink: #e9eaf4;
    --muted: #9da1ba;
    --rule: #2d3045;
    --accent: #9aa5ff;
    --on-accent: #11121b;
    --accent-soft: #252a4d;
    --good: #62c794;
    --warn: #e9b15c;
    --crit: #ff8f85;
    --series-1: #7280f0;
    --series-2: #c2852a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #11121b;
  --surface: #1a1c29;
  --ink: #e9eaf4;
  --muted: #9da1ba;
  --rule: #2d3045;
  --accent: #9aa5ff;
  --on-accent: #11121b;
  --accent-soft: #252a4d;
  --good: #62c794;
  --warn: #e9b15c;
  --crit: #ff8f85;
  --series-1: #7280f0;
  --series-2: #c2852a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.45;
}
.page {
  max-width: 760px;
  margin: 0 auto;
  /* Installed as an app, the page reaches under the notch and the home bar. */
  padding: max(20px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(48px, calc(env(safe-area-inset-bottom) + 24px)) max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 20px;
}
h1, h2, h3, p { margin: 0; }
h1, h2, h3 { text-wrap: balance; }
h2 {
  font-family: var(--font-body);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}
.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Masthead */
.mast { display: flex; flex-direction: column; gap: 6px; }
.eyebrow {
  font-size: 0.78rem; font-weight: 600; letter-spacing: 0.09em;
  text-transform: uppercase; color: var(--accent);
}
.month-nav { display: flex; align-items: center; gap: 12px; }
.month-nav h1 {
  flex: 1; min-width: 0;
  font-family: var(--font-display);
  font-weight: 400; font-style: italic;
  font-size: clamp(2.5rem, 11vw, 3.9rem);
  line-height: 1;
  display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 12px;
}
.month-nav h1 .yr {
  font-family: var(--font-num); font-style: normal;
  font-size: 0.95rem; color: var(--muted);
}
.round {
  width: 44px; height: 44px; flex: none;
  border-radius: 50%;
  border: 1px solid var(--rule);
  background: var(--surface); color: var(--ink);
  font-size: 1.3rem; line-height: 1; cursor: pointer;
}
.round:hover { border-color: var(--ink); }
.linkish {
  align-self: flex-start;
  background: none; border: 0; padding: 4px 0;
  color: var(--accent); font: inherit; font-size: 0.9rem;
  text-decoration: underline; text-underline-offset: 3px; cursor: pointer;
}

/* Totals */
.totals {
  display: grid; grid-template-columns: 1fr 1fr; gap: 14px 20px;
  padding: 16px 0 0;
  border-top: 1px solid var(--ink);
}
.tot { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tot .lab { font-size: 0.78rem; font-weight: 600; letter-spacing: 0.09em; text-transform: uppercase; color: var(--muted); }
.tot .num { font-size: clamp(1.15rem, 5.2vw, 1.5rem); font-weight: 500; }
.tot.bal { grid-column: 1 / -1; padding-top: 12px; border-top: 1px solid var(--rule); }
.tot.bal .num {
  font-size: clamp(1.7rem, 8.5vw, 2.5rem);
  align-self: flex-start;
  padding-bottom: 4px;
  border-bottom: 4px double var(--ink);
}
.tot.bal .num.neg { color: var(--crit); border-bottom-color: var(--crit); }
.tot .sub { font-size: 0.9rem; color: var(--muted); margin-top: 6px; }
@media (min-width: 620px) {
  .totals { grid-template-columns: 1fr 1fr 1.5fr; }
  .tot.bal { grid-column: auto; padding-top: 0; border-top: 0; padding-left: 20px; border-left: 1px solid var(--rule); }
}

/* Notice + status */
.notice {
  padding: 12px 14px; border-radius: 8px;
  border: 1px solid var(--warn); color: var(--ink);
  background: var(--surface); font-size: 0.93rem;
  border-left-width: 5px;
}
.status { min-height: 1.4em; font-size: 0.9rem; color: var(--good); font-weight: 500; }
.status.err { color: var(--crit); }

/* Buttons and fields */
.btn {
  min-height: 44px; padding: 10px 16px;
  border: 1px solid var(--rule); border-radius: 8px;
  background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 500; cursor: pointer;
}
.btn:hover { border-color: var(--ink); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--crit); }
.btn.danger.armed { background: var(--crit); border-color: var(--crit); color: var(--surface); }
.btn.small { min-height: 40px; padding: 6px 12px; font-size: 0.9rem; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); min-width: 0; }
input[type="text"], input[type="search"], input[type="date"], input[type="month"], input[type="password"], input:not([type]), select {
  width: 100%; min-height: 44px; padding: 9px 12px;
  border: 1px solid var(--rule); border-radius: 8px;
  background: var(--surface); color: var(--ink);
  font: inherit; font-size: 16px;
}
input.amount { font-family: var(--font-num); font-size: 1.25rem; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.error { color: var(--crit); font-size: 0.93rem; font-weight: 500; }
.hint { font-size: 0.9rem; color: var(--muted); max-width: 65ch; }

/* Composer */
#tx-form {
  display: flex; flex-direction: column; gap: 14px;
  padding: 16px; border: 1px solid var(--rule); border-radius: 12px;
  background: var(--surface);
}
#tx-form[hidden] { display: none; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--rule); border-radius: 8px; overflow: hidden; }
.seg label { position: relative; }
.seg input, .chips input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.seg span { display: block; text-align: center; padding: 10px; font-weight: 500; }
.seg input:checked + span { background: var(--ink); color: var(--paper); }
.seg input:focus-visible + span, .chips input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
fieldset.chips { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset.chips legend { padding: 0; margin-bottom: 6px; font-size: 0.85rem; color: var(--muted); }
.chip-wrap { display: flex; flex-wrap: wrap; gap: 8px; }
.chips label { position: relative; }
.chips span {
  display: block; padding: 9px 14px; border-radius: 999px;
  border: 1px solid var(--rule); background: var(--paper); font-size: 0.95rem;
}
.chips input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

/* Tabs */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--rule); overflow-x: auto; }
.tabs a {
  flex: 1 0 auto; text-align: center;
  padding: 12px 6px 10px; color: var(--muted); text-decoration: none;
  font-size: 0.95rem; font-weight: 500; white-space: nowrap; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs a:hover { color: var(--ink); }
.tabs a[aria-current="page"] { color: var(--ink); border-bottom-color: var(--accent); }

#panel { display: flex; flex-direction: column; gap: 18px; }
.block { display: flex; flex-direction: column; gap: 10px; }
.empty { padding: 22px 16px; border: 1px dashed var(--rule); border-radius: 12px; color: var(--muted); }

/* Category spending rows */
ul.plain { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.cat-row { display: flex; flex-direction: column; gap: 6px; padding: 12px 0; border-bottom: 1px solid var(--rule); }
.cat-head, .cat-foot { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.cat-name { font-weight: 500; min-width: 0; overflow-wrap: anywhere; }
.bar { height: 8px; border-radius: 4px; background: var(--rule); overflow: hidden; }
.bar i { display: block; height: 100%; width: var(--p, 0%); background: var(--ink); border-radius: 4px; transition: width 0.3s ease; }
.bar.plain i { background: var(--muted); }
.bar.warn i { background: var(--warn); }
.bar.crit i { background: var(--crit); }
.cat-foot { font-size: 0.85rem; color: var(--muted); }
.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; border: 1px solid var(--rule); font-size: 0.82rem; font-weight: 500; color: var(--muted); }
.pill.warn { border-color: var(--warn); color: var(--warn); }
.pill.crit { border-color: var(--crit); background: var(--crit); color: var(--surface); }
.sum-row { display: flex; justify-content: space-between; gap: 12px; padding: 12px 0 0; font-weight: 500; }
.simple-row { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--rule); }
.simple-row .num.inc, .tx .num.inc { color: var(--good); }

/* Trend */
.trend-top { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; font-size: 0.82rem; color: var(--muted); }
.legend { display: flex; gap: 14px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; }
.legend .l-inc i { background: var(--good); }
.legend .l-exp i { background: var(--ink); }
.trend { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; border-top: 1px dashed var(--rule); }
.tcol { display: flex; flex-direction: column; gap: 6px; padding: 0 0 6px; border: 0; background: none; color: var(--muted); font: inherit; font-size: 0.85rem; cursor: pointer; border-radius: 6px; }
.tcol:hover { color: var(--ink); }
.tbars { height: 110px; display: flex; align-items: flex-end; justify-content: center; gap: 3px; border-bottom: 1px solid var(--ink); }
.tbars i { display: block; width: min(16px, 30%); border-radius: 3px 3px 0 0; }
.tbars .b-inc { background: var(--good); }
.tbars .b-exp { background: var(--ink); }
.tcol.cur { color: var(--ink); font-weight: 600; }
.tcol.cur .tlab { text-decoration: underline; text-decoration-color: var(--accent); text-decoration-thickness: 2px; text-underline-offset: 4px; }

/* Transactions */
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.filters .wide { grid-column: 1 / -1; }
.found-sum { font-size: 0.9rem; color: var(--muted); padding-top: 4px; }
.day { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 16px 0 6px; border-bottom: 1px solid var(--ink); font-size: 0.85rem; color: var(--muted); }
.day strong { color: var(--ink); font-weight: 600; text-transform: capitalize; }
.tx {
  width: 100%; display: flex; justify-content: space-between; align-items: baseline; gap: 12px;
  padding: 12px 0; min-height: 48px; border: 0; border-bottom: 1px solid var(--rule);
  background: none; color: var(--ink); font: inherit; text-align: left; cursor: pointer;
}
.tx:hover { background: var(--accent-soft); }
.tx .what { min-width: 0; display: flex; flex-direction: column; }
.tx .what b { font-weight: 500; overflow-wrap: anywhere; }
.tx .what small { color: var(--muted); font-size: 0.85rem; overflow-wrap: anywhere; }

/* Category editor */
.cat-edit li { display: grid; grid-template-columns: 1fr auto; gap: 8px 10px; align-items: end; padding: 12px 0; border-bottom: 1px solid var(--rule); }
.cat-edit .c-bud { grid-column: 1 / -1; }
.cat-edit .c-bud input { font-family: var(--font-num); }
@media (min-width: 620px) {
  .cat-edit li { grid-template-columns: 1.4fr 1fr auto; }
  .cat-edit .c-bud { grid-column: auto; }
  .cat-edit li.no-bud { grid-template-columns: 1fr auto; }
}
#cat-form { display: grid; grid-template-columns: 1fr; gap: 10px; padding: 16px; border: 1px solid var(--rule); border-radius: 12px; background: var(--surface); }
@media (min-width: 620px) { #cat-form { grid-template-columns: 1.6fr 1fr auto; align-items: end; } }
details summary { cursor: pointer; padding: 10px 0; color: var(--muted); font-weight: 500; }
.arch-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--rule); }

/* Data tab */
dl.paths { margin: 0; display: grid; grid-template-columns: 1fr; gap: 10px; }
dl.paths dt { font-family: var(--font-num); font-size: 0.85rem; overflow-wrap: anywhere; }
dl.paths dd { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }

/* Settings, review inbox, recurring */
.subtabs { display: flex; flex-wrap: wrap; gap: 8px; }
.subtabs[hidden] { display: none; }
.subtabs a { padding: 7px 13px; border: 1px solid var(--rule); border-radius: 999px; background: var(--surface); color: var(--muted); text-decoration: none; font-size: 0.9rem; font-weight: 500; }
.subtabs a:hover { color: var(--ink); border-color: var(--ink); }
.subtabs a[aria-current="page"] { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.reviewbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--warn); border-left-width: 5px; border-radius: 8px; background: var(--surface); color: var(--ink); text-decoration: none; font-weight: 500; }
.reviewbar[hidden] { display: none; }
.reviewbar::after { content: 'Revisar \203A'; color: var(--accent); white-space: nowrap; }
.reviewbar.backupbar { border-color: var(--crit); }
.reviewbar.backupbar::after { content: 'Ver \203A'; }
.pill.ok { border-color: var(--good); color: var(--good); }
dl.facts { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 0.93rem; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }
.card-form { display: flex; flex-direction: column; gap: 14px; padding: 16px; border: 1px solid var(--rule); border-radius: 12px; background: var(--surface); }
.inline-form { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; }
.rv-actions { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; }
.token-box { display: flex; flex-direction: column; gap: 10px; padding: 14px; border: 1px solid var(--good); border-left-width: 5px; border-radius: 8px; background: var(--surface); }
.token-box input { font-family: var(--font-num); font-size: 0.85rem; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.check input { width: 20px; height: 20px; margin: 0; }
.urow .pill { margin-left: 6px; }
.locked .subtabs, .locked .reviewbar { display: none !important; }

/* Net worth, loans, monthly closing */
a.tx { text-decoration: none; }
.grp { font-size: 0.85rem; font-weight: 500; color: var(--muted); padding-top: 10px; }
.lead { font-family: var(--font-display); font-size: clamp(1.45rem, 6vw, 1.9rem); line-height: 1.2; text-wrap: balance; }
.reviewbar.closebar { border-color: var(--accent); }
.reviewbar.closebar::after { content: 'Ver \203A'; }
.sim { display: flex; flex-direction: column; gap: 10px; padding: 14px; border: 1px solid var(--rule); border-radius: 12px; background: var(--surface); }

/* Portfolio */
.pill.down { border-color: var(--crit); color: var(--crit); }
.pf-legend { flex-wrap: wrap; gap: 6px 18px; color: var(--ink); font-size: 0.88rem; }
.pf-legend i { width: 16px; height: 0; border-top: 2px solid; border-radius: 0; vertical-align: middle; }
.pf-legend .k1, .pf-tip .k1 { border-color: var(--series-1); }
.pf-legend .k2, .pf-tip .k2 { border-color: var(--series-2); }
.pf-legend b { font-weight: 500; margin-left: 4px; }
.pf-chart { display: grid; grid-template-columns: auto 1fr; gap: 4px 8px; }
.pf-y { display: flex; flex-direction: column; justify-content: space-between; text-align: right; font-size: 0.72rem; color: var(--muted); }
.pf-y span { line-height: 1; }
.pf-plot { position: relative; height: 150px; touch-action: pan-y; border-radius: 4px; cursor: crosshair; }
.pf-plot svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.pf-plot .g { stroke: var(--rule); stroke-width: 1; vector-effect: non-scaling-stroke; }
.pf-plot polyline { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.pf-plot .s1 { stroke: var(--series-1); }
.pf-plot .s2 { stroke: var(--series-2); }
.pf-dot { position: absolute; width: 8px; height: 8px; margin: -6px 0 0 -6px; border-radius: 50%; border: 2px solid var(--paper); box-sizing: content-box; pointer-events: none; }
.pf-dot.d1 { background: var(--series-1); }
.pf-dot.d2 { background: var(--series-2); }
.pf-dot[hidden], .pf-hair[hidden], .pf-tip[hidden] { display: none; }
.pf-hair { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--muted); pointer-events: none; }
.pf-tip { position: absolute; top: 4px; left: 0; z-index: 2; max-width: 100%; display: flex; flex-direction: column; gap: 3px; padding: 8px 10px; border: 1px solid var(--rule); border-radius: 8px; background: var(--surface); color: var(--ink); font-size: 0.82rem; white-space: nowrap; pointer-events: none; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12); }
.pf-tip .when { color: var(--muted); }
.pf-tip .row { display: flex; align-items: center; gap: 6px; }
.pf-tip i { width: 12px; border-top: 2px solid; }
.pf-tip b { font-family: var(--font-num); font-weight: 500; }
.pf-tip .lbl { color: var(--muted); }
.pf-x { grid-column: 2; display: flex; justify-content: space-between; font-size: 0.72rem; color: var(--muted); }
.fund-body { display: flex; flex-direction: column; gap: 12px; padding: 4px 0 8px; }
.fund .tx { border-bottom: 0; }

/* Year view */
@media (max-width: 440px) { .tabs a { font-size: 0.86rem; padding: 12px 3px 10px; } }
@media (max-width: 375px) { .tabs a { font-size: 0.82rem; padding: 12px 2px 10px; } }
.mlink { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; text-align: left; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.cat-foot { flex-wrap: wrap; }
.yfilter { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; }
.trend.year { grid-template-columns: repeat(12, 1fr); gap: 2px; }
.trend.year .tcol { font-size: 0.7rem; }
.trend.year .tbars { height: 120px; gap: 1px; }
.trend.year .tbars i { width: min(9px, 42%); border-radius: 2px 2px 0 0; }
.ytable-wrap { overflow-x: auto; }
table.ytable { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.ytable th { text-align: right; font-size: 0.72rem; font-weight: 600; letter-spacing: 0.09em; text-transform: uppercase; color: var(--muted); padding: 6px 0 6px 10px; border-bottom: 1px solid var(--ink); }
.ytable td { text-align: right; padding: 9px 0 9px 10px; border-bottom: 1px solid var(--rule); }
.ytable th:first-child, .ytable td:first-child { text-align: left; padding-left: 0; }
.ytable td.num { font-size: 0.8rem; white-space: nowrap; }
.ytable td .mlink { text-transform: capitalize; }
.ytable tr.none td { color: var(--muted); }
.ytable td.neg { color: var(--crit); }
.ytable tfoot td { font-weight: 600; border-bottom: 0; }
.ytable tfoot tr:first-child td { border-top: 1px solid var(--ink); border-bottom: 4px double var(--ink); }
.ytable tfoot tr + tr td { font-weight: 400; color: var(--muted); }

/* Login */
#login { display: none; flex-direction: column; gap: 14px; padding: 16px; border: 1px solid var(--rule); border-radius: 12px; background: var(--surface); max-width: 420px; }
.locked #login { display: flex; }
.locked .month-nav, .locked #go-today, .locked .totals, .locked #composer, .locked #status, .locked .tabs, .locked #panel { display: none; }

@media (prefers-reduced-motion: reduce) { .bar i { transition: none; } }
