/* =====================================================================
   Folio — "The Ledger" design system
   Drop-in stylesheet for the vanilla-JS PWA (replaces css/app.css visuals).
   Mobile-first, single column, max-width 560px.
   Every color, size and radius here is the source of truth; README.md
   explains where each class is used.
   ===================================================================== */

/* ---------- Fonts (self-host; see README §5.2) ---------- */
@font-face { font-family: "Geist"; src: url("../fonts/Geist-Variable.woff2") format("woff2"); font-weight: 100 900; font-display: swap; }
@font-face { font-family: "Geist Mono"; src: url("../fonts/GeistMono-Variable.woff2") format("woff2"); font-weight: 100 900; font-display: swap; }
@font-face { font-family: "Instrument Serif"; src: url("../fonts/InstrumentSerif-Regular.woff2") format("woff2"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "Instrument Serif"; src: url("../fonts/InstrumentSerif-Italic.woff2") format("woff2"); font-weight: 400; font-style: italic; font-display: swap; }

/* ---------- Tokens ---------- */
:root {
  /* surfaces */
  --stone:        #EDEEE9;  /* app background */
  --stone-2:      #E0E2DC;  /* segmented-control track */
  --sheet:        #FFFFFF;  /* cards */
  --track:        #DCDED7;  /* bar tracks on stone */
  --rule:         #ECEDE8;  /* row dividers inside cards */
  --rule-strong:  #C9CDC4;  /* dashed borders, header rules, pending outlines */

  /* ink */
  --ink:          #111714;  /* primary text, nav capsule, avatar */
  --ink-2:        #4C5550;  /* secondary text */
  --ink-3:        #5F6762;  /* captions, tertiary */

  /* semantic: performance */
  --up:           #0A6B45;  /* ahead / gain */
  --up-bg:        #E3F1EA;
  --down:         #AE3420;  /* behind / loss */
  --down-bg:      #F8E6E1;
  --spy:          #8A918C;  /* benchmark, always */
  --muted-bar:    #B9BEB7;  /* income month under plan */

  /* semantic: status */
  --amber-bg:     #F2E7C9;  /* demo banner, reconstruction, warnings */
  --amber-ink:    #6B4E0E;

  /* night (lock screen + nav capsule) */
  --night:        #111714;
  --night-2:      #1E2622;  /* keypad keys */
  --night-3:      #2C3631;  /* keypad key pressed */
  --night-ink:    #F3F4EF;
  --night-ink-2:  #9AA59F;
  --night-ring:   #6E7A73;  /* empty PIN dot ring */
  --night-error:  #FF8A73;

  /* sleeve palette (assign in sleeve order; cycle if more than 5) */
  --sleeve-1: #2F5D8A;  /* slate blue */
  --sleeve-2: #6E9CC4;  /* steel blue (no greens or reds: those mean ahead/behind) */
  --sleeve-3: #8C6E9E;  /* mauve */
  --sleeve-4: #B08433;  /* ochre */
  --sleeve-5: #8A918C;  /* gray */

  /* type */
  --font-sans:  "Geist", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-serif: "Instrument Serif", Georgia, "Times New Roman", serif;
  --font-mono:  "Geist Mono", ui-monospace, "SF Mono", Menlo, monospace;

  /* shape */
  --r-card: 22px;
  --r-pill: 999px;
  --r-tile: 8px;
  --r-dot: 3px;

  /* elevation */
  --sh-card: 0 1px 2px rgba(17, 23, 20, .06);
  --sh-seg:  0 1px 3px rgba(17, 23, 20, .12);
  --sh-nav:  0 8px 24px rgba(17, 23, 20, .25);

  /* layout */
  --gutter: 18px;
  --nav-h: 64px;
  --nav-bottom: calc(22px + env(safe-area-inset-bottom, 0px));

  /* motion */
  --ease: cubic-bezier(.2, .7, .2, 1);
  --dur-1: 120ms;
  --dur-2: 220ms;
}

/* Color-blind-safe mode: add class "cb-safe" to <body> */
body.cb-safe {
  --up: #1D5FD1;  --up-bg: #E4ECFA;
  --down: #9E4708; --down-bg: #F7E8DC;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--stone);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;     /* every figure aligns */
  -webkit-font-smoothing: antialiased;
}
a { color: var(--up); }
body.ledger a:hover { color: var(--ink); }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
body.ledger :focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; border-radius: 6px; }

.up   { color: var(--up); }
body.ledger .down { color: var(--down); }
.flat { color: var(--ink-2); }

/* ---------- Screen shell ---------- */
body.ledger .screen {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top, 0px)) var(--gutter) calc(var(--nav-h) + var(--nav-bottom) + 34px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.screen--home { gap: 18px; }

body.ledger .topbar { display: flex; justify-content: space-between; align-items: center; min-height: 44px; }
.wordmark { font-family: var(--font-serif); font-size: 30px; line-height: 1; font-weight: 400; }
body.ledger .screen-title { font-family: var(--font-serif); font-size: 40px; line-height: 1; font-weight: 400; margin: 0; }
.avatar {
  width: 36px; height: 36px; border-radius: 18px;
  background: var(--ink); color: var(--night-ink);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; letter-spacing: .02em;
  position: relative;
}
.avatar::after { content: ""; position: absolute; inset: -4px; } /* 44px hit area */

body.ledger .topbar-actions { display: flex; gap: 8px; align-items: center; }
.ask-btn {
  width: 36px; height: 36px; border-radius: 18px; background: var(--sheet); box-shadow: var(--sh-card); color: var(--ink);
  display: inline-flex; align-items: center; justify-content: center; position: relative;
}
.ask-btn svg { width: 18px; height: 18px; }
body.ledger .ask-btn::after { content: ""; position: absolute; inset: -4px; }

.back-btn {
  width: 36px; height: 36px; border-radius: 18px; background: var(--sheet); box-shadow: var(--sh-card);
  display: inline-flex; align-items: center; justify-content: center; position: relative;
}
.back-btn::after { content: ""; position: absolute; inset: -4px; }

body.ledger .mono-label {
  font-family: var(--font-mono); font-size: 11px; font-weight: 600;
  letter-spacing: .1em; text-transform: uppercase; color: var(--ink-2);
}
.caption { font-size: 12px; line-height: 1.5; color: var(--ink-3); }
body.ledger .section-title { font-family: var(--font-serif); font-size: 22px; line-height: 1; margin: 6px 2px 0; font-weight: 400; }

/* ---------- Segmented control (window picker, Sleeves/Funds, Real/Paper) ---------- */
/* Keep existing <button> elements and the existing "on" class. */
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--stone-2); border-radius: var(--r-pill); padding: 3px;
}
.seg > button {
  height: 36px; border-radius: var(--r-pill);
  font-size: 13px; font-weight: 600; color: var(--ink-2);
  transition: background var(--dur-2) var(--ease), color var(--dur-2) var(--ease), box-shadow var(--dur-2) var(--ease);
  position: relative;
}
.seg > button::after { content: ""; position: absolute; inset: -4px 0; } /* 44px tall hit area */
body.ledger .seg > button.on { background: var(--sheet); color: var(--ink); box-shadow: var(--sh-seg); }

/* Since-month chip with the native <select> laid over it (keep the select + its onchange) */
.since-chip {
  position: relative; align-self: flex-start;
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 12px; border-radius: var(--r-pill);
  background: var(--sheet); box-shadow: var(--sh-card);
  font-size: 13px; font-weight: 600;
}
.since-chip select { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; font-size: 16px; /* prevents iOS zoom */ }

/* ---------- Home hero ---------- */
body.ledger .hero { display: flex; flex-direction: column; gap: 6px; padding-top: 6px; }
.hero-figure { font-size: clamp(56px, 21vw, 80px); line-height: .95; font-weight: 600; letter-spacing: -.05em; }
body.ledger .hero-verdict { font-family: var(--font-serif); font-style: italic; font-size: 24px; line-height: 1.1; text-wrap: balance; }

/* Race bars: You vs S&P 500 */
.race { display: flex; flex-direction: column; gap: 10px; }
body.ledger .race-row { display: grid; grid-template-columns: 62px minmax(0, 1fr) 70px; gap: 10px; align-items: center; }
.race-name { font-size: 13px; font-weight: 700; }
body.ledger .race-row--spy .race-name, body.ledger .race-row--spy .race-val { font-weight: 500; color: var(--ink-2); }
.race-track { height: 8px; border-radius: 4px; background: var(--track); overflow: hidden; }
/* Fill is positioned with inline margin-left + width (see README §7.2) so negative returns diverge from a zero point */
body.ledger .race-fill { height: 8px; border-radius: 4px; background: var(--up); transition: width var(--dur-2) var(--ease), margin-left var(--dur-2) var(--ease); }
.race-fill.down { background: var(--down); }
body.ledger .race-row--spy .race-fill { background: var(--spy); }
.race-val { font-size: 15px; font-weight: 700; text-align: right; }

/* ---------- Cards ---------- */
body.ledger .card { background: var(--sheet); border-radius: var(--r-card); padding: 16px; box-shadow: var(--sh-card); display: flex; flex-direction: column; gap: 12px; }
.card--list { padding: 2px 16px; gap: 0; }
body.ledger .card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.card-title { font-family: var(--font-serif); font-size: 22px; line-height: 1; font-weight: 400; margin: 0; }
body.ledger .card-meta { font-size: 12px; color: var(--ink-3); }
.card-total { font-size: 17px; font-weight: 600; }

/* ---------- Gap chart (SVG generated by gapChartSVG(), viewBox 0 0 300 120) ---------- */
body.ledger .gap-chart svg { display: block; width: 100%; height: 120px; overflow: visible; }
.gap-zero { stroke: var(--spy); stroke-width: 1; stroke-dasharray: 2 3; }
body.ledger .gap-area-up { fill: var(--up);   fill-opacity: .12; }
.gap-area-down { fill: var(--down); fill-opacity: .12; }
body.ledger .gap-line-up, body.ledger .gap-line-down { fill: none; stroke-width: 2.25; stroke-linejoin: round; stroke-linecap: round; }
.gap-line-up   { stroke: var(--up); }
body.ledger .gap-line-down { stroke: var(--down); }
.gap-end { stroke: var(--sheet); stroke-width: 2; }
body.ledger .gap-end.up { fill: var(--up); } .gap-end.down { fill: var(--down); }
.legend-zero { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--ink-3); }
body.ledger .legend-zero::before { content: ""; width: 14px; border-top: 1.5px dashed var(--spy); }
.gap-axis { display: flex; justify-content: space-between; font-family: var(--font-mono); font-size: 11px; color: var(--ink-3); }
body.ledger .chart-empty { height: 140px; display: flex; align-items: center; justify-content: center; text-align: center; font-size: 14px; color: var(--ink-3); }

/* ---------- Allocation ("Where it sits") ---------- */
.alloc-bar { display: flex; gap: 2px; height: 10px; border-radius: 5px; overflow: hidden; }
body.ledger .alloc-seg { height: 10px; min-width: 3px; }
.alloc-row {
  display: grid; grid-template-columns: 10px minmax(0, 1fr) auto 36px; gap: 10px; align-items: center;
  min-height: 44px; border-top: 1px solid var(--rule); width: 100%; text-align: left;
}
.sleeve-dot { width: 10px; height: 10px; border-radius: var(--r-dot); flex: none; }
body.ledger .alloc-name { font-size: 14px; font-weight: 600; }
.alloc-val { font-size: 14px; color: var(--ink-2); }
body.ledger .alloc-pct { font-size: 13px; color: var(--ink-3); text-align: right; }

/* ---------- Sleeve card (Holdings) ---------- */
/* Keep the existing <button class="sleeve-row tap" data-go="..."> as the element; add class "sleeve-card". */
.sleeve-card { background: var(--sheet); border-radius: var(--r-card); padding: 16px; box-shadow: var(--sh-card); display: flex; flex-direction: column; gap: 12px; width: 100%; text-align: left; transition: transform var(--dur-1) var(--ease); }
body.ledger .sleeve-card:active { transform: scale(.985); }
.sleeve-card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
body.ledger .sleeve-card-id { display: flex; gap: 10px; min-width: 0; }
.sleeve-card-id .sleeve-dot { margin-top: 5px; }
body.ledger .sleeve-name { font-size: 16px; font-weight: 600; }
.sleeve-desc { font-size: 13px; color: var(--ink-3); }
body.ledger .sleeve-card-bottom { display: flex; justify-content: space-between; align-items: baseline; padding-left: 20px; }
.sleeve-value { font-size: 22px; font-weight: 600; letter-spacing: -.02em; }
body.ledger .sleeve-twr { font-size: 15px; font-weight: 600; }

.chip { font-size: 12px; font-weight: 600; white-space: nowrap; padding: 4px 9px; border-radius: var(--r-pill); }
body.ledger .chip.up { background: var(--up-bg);    color: var(--up); }
.chip.down  { background: var(--down-bg);  color: var(--down); }
body.ledger .chip.amber { background: var(--amber-bg); color: var(--amber-ink); }

/* ---------- Ledger table (Funds, sleeve holdings) ---------- */
.ltable { width: 100%; border-collapse: collapse; }
body.ledger .ltable th { font-family: var(--font-mono); font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); text-align: left; padding: 10px 0 8px; }
.ltable td { border-top: 1px solid var(--rule); padding: 12px 0; font-size: 14px; vertical-align: middle; }
body.ledger .ltable .num { text-align: right; padding-left: 10px; white-space: nowrap; }
.ltable td.num { color: var(--ink-2); }
body.ledger .ltable td.num.strong { color: var(--ink); font-weight: 600; }
.ltable .name { font-weight: 600; }
body.ledger .mini-track { height: 3px; border-radius: 2px; background: var(--rule); margin-top: 6px; }
.mini-fill  { height: 3px; border-radius: 2px; background: var(--sleeve-1); }

/* ---------- Picks ---------- */
body.ledger .pick-row { display: grid; grid-template-columns: 52px minmax(0, 1fr) auto; gap: 12px; align-items: center; min-height: 68px; width: 100%; text-align: left; }
.pick-row + .pick-row { border-top: 1px solid var(--rule); }
body.ledger .ticker { height: 30px; border-radius: var(--r-tile); background: var(--ink); color: var(--night-ink); display: flex; align-items: center; justify-content: center; font-family: var(--font-mono); font-size: 11px; font-weight: 600; }
.ticker.outline { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 1.5px var(--ink); }
body.ledger .pick-name { font-size: 15px; font-weight: 600; }
.pick-sub { font-size: 12px; color: var(--ink-3); }
body.ledger .pick-ret { font-size: 16px; font-weight: 600; }
.week-row { display: flex; justify-content: space-between; align-items: center; min-height: 52px; }
body.ledger .week-row + .week-row { border-top: 1px solid var(--rule); }

/* Paper lab (virtual) */
.paper { border: 1.5px dashed var(--spy); border-radius: var(--r-card); padding: 16px; display: flex; flex-direction: column; gap: 14px; }
body.ledger .paper .pick-row { min-height: 56px; }
.paper .pick-row + .pick-row { border-top: 1px dashed var(--rule-strong); }
body.ledger .paper .pick-name { font-weight: 500; color: var(--ink-2); }
.stamp { font-family: var(--font-mono); font-size: 11px; font-weight: 600; letter-spacing: .14em; color: var(--ink); border: 1.5px solid var(--ink); border-radius: 4px; padding: 3px 7px; transform: rotate(-3deg); display: inline-block; }

/* ---------- Income ---------- */
body.ledger .bars { position: relative; height: 140px; display: grid; grid-template-columns: repeat(12, 1fr); gap: 6px; align-items: end; }
.bar { border-radius: 5px 5px 2px 2px; background: var(--muted-bar); transition: height var(--dur-2) var(--ease); }
body.ledger .bar.met { background: var(--up); }
.bar.pending { background: transparent; box-shadow: inset 0 0 0 1.5px var(--rule-strong); }
body.ledger .plan-line { position: absolute; left: 0; right: 0; border-top: 1.5px dashed var(--ink); z-index: 1; pointer-events: none; }
.bars-axis { display: grid; grid-template-columns: repeat(12, 1fr); gap: 6px; }
body.ledger .bars-axis span { font-family: var(--font-mono); font-size: 11px; text-align: center; color: var(--ink-3); }
.legend-plan { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--ink-3); }
body.ledger .legend-plan::before { content: ""; width: 14px; border-top: 1.5px dashed var(--ink); }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
body.ledger .pair > div + div { border-left: 1px solid var(--rule); padding-left: 14px; }
.pair-label { font-size: 12px; color: var(--ink-3); }
body.ledger .pair-value { font-size: 20px; font-weight: 600; letter-spacing: -.02em; }

/* ---------- List rows (More, archive) ---------- */
.list-row { display: flex; justify-content: space-between; align-items: center; min-height: 52px; width: 100%; text-align: left; font-size: 15px; font-weight: 500; }
body.ledger .list-row + .list-row { border-top: 1px solid var(--rule); }
.list-row .chev { color: var(--ink-3); }

/* ---------- Floating nav capsule ---------- */
/* Four tabs, icon + label on every tab, equal widths. Keep each tab's existing data-go / handler. Active tab gets "on". */
body.ledger .nav {
  position: fixed; left: var(--gutter); right: var(--gutter); bottom: var(--nav-bottom);
  max-width: calc(560px - 2 * var(--gutter)); margin: 0 auto;
  height: var(--nav-h); border-radius: 32px; background: var(--night); padding: 6px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; box-shadow: var(--sh-nav); z-index: 50;
}
.nav > button {
  border-radius: 26px; color: var(--night-ink-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  transition: background var(--dur-2) var(--ease), color var(--dur-2) var(--ease);
}
.nav > button svg { width: 20px; height: 20px; flex: none; }
body.ledger .nav > button .nav-label { font-size: 11px; font-weight: 500; }
.nav > button.on { background: var(--night-ink); color: var(--ink); }
body.ledger .nav > button.on .nav-label { font-weight: 700; }

/* ---------- Banners & flags ---------- */
/* Render as the first child of <body>, OUTSIDE .screen, so it sticks flush to the top. */
.demo-banner {
  position: sticky; top: 0; z-index: 60;
  padding: calc(10px + env(safe-area-inset-top, 0px)) var(--gutter) 10px;
  background: var(--amber-bg); display: flex; justify-content: space-between; align-items: center;
}
.demo-banner > * { max-width: 524px; }
body.ledger body.is-demo .screen { padding-top: 16px; }
.demo-banner .mono-label { color: var(--amber-ink); font-size: 12px; letter-spacing: .08em; }
body.ledger .demo-exit { height: 32px; padding: 0 14px; border-radius: var(--r-pill); background: var(--amber-ink); color: #FFFFFF; font-size: 13px; font-weight: 600; position: relative; }
.demo-exit::after { content: ""; position: absolute; inset: -6px; }

body.ledger .flag { display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: 16px; font-size: 14px; line-height: 1.4; }
.flag svg { width: 16px; height: 16px; flex: none; margin-top: 2px; }
body.ledger .flag.info { background: var(--stone-2); color: var(--ink); }
.flag.warn { background: var(--amber-bg); color: var(--amber-ink); }
body.ledger .flag.crit { background: var(--down-bg); color: var(--down); }

.recon { background: var(--amber-bg); color: var(--amber-ink); border-radius: var(--r-card); padding: 16px; display: flex; flex-direction: column; gap: 6px; }
body.ledger .recon-title { font-family: var(--font-serif); font-size: 22px; line-height: 1.1; }
.recon-body { font-size: 14px; line-height: 1.5; }

/* ---------- Lock screen ---------- */
body.ledger .lock {
  min-height: 100dvh; background: var(--night); color: var(--night-ink);
  display: flex; flex-direction: column; align-items: center;
  padding: calc(96px + env(safe-area-inset-top, 0px)) 32px calc(40px + env(safe-area-inset-bottom, 0px));
}
.lock-wordmark { font-family: var(--font-serif); font-size: 64px; line-height: 1; }
body.ledger .lock-sub { font-size: 15px; color: var(--night-ink-2); padding-top: 12px; }
.lock-error { font-size: 14px; color: var(--night-error); min-height: 20px; margin-top: 8px; }
body.ledger .pin-dots { display: flex; gap: 16px; padding: 24px 0 40px; }
.pin-dots .dot { width: 12px; height: 12px; border-radius: 6px; box-shadow: inset 0 0 0 1.5px var(--night-ring); transition: background var(--dur-1) var(--ease), box-shadow var(--dur-1) var(--ease); }
body.ledger .pin-dots .dot.filled { background: var(--night-ink); box-shadow: none; }
.pin-dots.error .dot { box-shadow: inset 0 0 0 1.5px var(--night-error); }
body.ledger .keypad { display: grid; grid-template-columns: repeat(3, 76px); gap: 16px 24px; }
.key { height: 76px; border-radius: 38px; background: var(--night-2); color: var(--night-ink); font-size: 30px; font-weight: 400; display: flex; align-items: center; justify-content: center; transition: background var(--dur-1) var(--ease), transform var(--dur-1) var(--ease); }
body.ledger .key:active { background: var(--night-3); transform: scale(.96); }
.key--text { background: transparent; font-size: 13px; font-weight: 600; color: var(--night-ink-2); line-height: 1.2; text-align: center; }
body.ledger .lock-foot { margin-top: auto; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.lock-foot .mono-label { color: var(--night-ink-2); }
body.ledger .lock-demo { min-height: 44px; display: flex; align-items: center; font-family: var(--font-serif); font-style: italic; font-size: 20px; color: var(--night-ink); }
.pass-field { width: 100%; max-width: 300px; height: 52px; border-radius: 26px; background: var(--night-2); color: var(--night-ink); border: 1.5px solid var(--night-ring); padding: 0 20px; font-size: 16px; }
body.ledger .pass-field:focus { outline: none; border-color: var(--night-ink); }
.btn-primary { height: 52px; padding: 0 28px; border-radius: var(--r-pill); background: var(--night-ink); color: var(--ink); font-size: 16px; font-weight: 600; }

@media (prefers-reduced-motion: no-preference) {
body.ledger .pin-dots.error { animation: shake 320ms var(--ease); }
}
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-8px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(3px); } }

/* ---------- Loading ---------- */
.skeleton { background: var(--stone-2); border-radius: 8px; }
@media (prefers-reduced-motion: no-preference) {
body.ledger .skeleton { animation: pulse 1.4s ease-in-out infinite; }
}
@keyframes pulse { 50% { opacity: .55; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; }
}
