/* Budget Flow - a bound ledger at night. Deep forest page, fine ruled lines, a red margin rule down the binding,
 * warm gold for income and the primary action, muted category hues for the ribbons, tabular figures throughout. */
:root, :root[data-theme="dark"] {
  --bg: #0b1f19; --bg2: #0f2820; --panel: #12291f; --panel2: #163126; --ink: #ece6d6; --muted: #a7b3a3; --faint: #6f7f72;
  --rule: rgba(236, 230, 214, 0.075); --rule2: rgba(236, 230, 214, 0.16); --margin: rgba(196, 92, 80, 0.55);
  --gold: #d9a441; --gold-ink: #1d1606; --focus: #f2d27a;
  --c-income: #d9a441; --c-needs: #5fa3a4; --c-wants: #b582a8; --c-savings: #84b46f; --c-debt: #cf7a55; --c-left: #e6c66c; --c-short: #e8665f; --c-move: #f2d27a;
  --ok: #8cc47a; --warn: #e3b05a; --bad: #ec7a70;
  --shadow: 0 1px 0 rgba(0,0,0,.25), 0 8px 24px rgba(0,0,0,.25);
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f4efe1; --bg2: #ece5d2; --panel: #fbf8ef; --panel2: #f3eddc; --ink: #1d2a23; --muted: #55635a; --faint: #8a958c;
  --rule: rgba(29, 62, 50, 0.09); --rule2: rgba(29, 62, 50, 0.2); --margin: rgba(178, 64, 52, 0.45);
  --gold: #a8710f; --gold-ink: #fffaf0; --focus: #8a5a00;
  --c-income: #b9821d; --c-needs: #2f7f80; --c-wants: #93527f; --c-savings: #4f8a3b; --c-debt: #b2552f; --c-left: #b8902a; --c-short: #c2392f; --c-move: #8a5a00;
  --ok: #3f7d2f; --warn: #9a6a12; --bad: #b3362c;
  --shadow: 0 1px 0 rgba(0,0,0,.04), 0 6px 18px rgba(60,50,20,.08);
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.5 "Source Sans 3", "Source Sans Pro", "Segoe UI", "Gill Sans", "Optima", system-ui, sans-serif; }
.num, input[type="number"], .figval, .figafter { font-variant-numeric: tabular-nums lining-nums; }
a { color: var(--gold); }
h1, h2, h3, h4 { font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif; font-weight: 600; letter-spacing: .005em; margin: 0; }
h2 { font-size: 1.28rem; } h3 { font-size: 1.02rem; margin-top: 1rem; } h4 { font-size: .82rem; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); font-family: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: .88rem; } .note { color: var(--muted); font-size: .9rem; margin: .25rem 0; }
.warn { color: var(--warn); } .note.warn { color: var(--warn); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Header */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; justify-content: space-between; align-items: center; gap: .75rem; padding: .55rem 1rem; background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(6px); border-bottom: 1px solid var(--rule2); }
.brand { display: flex; align-items: center; gap: .55rem; min-width: 0; }
.backlink { color: var(--muted); text-decoration: none; font-size: .9rem; white-space: nowrap; }
.wordmark { font-family: "Iowan Old Style", Palatino, Georgia, serif; font-size: 1.12rem; letter-spacing: .02em; }
.topactions { display: flex; align-items: center; gap: .45rem; flex-wrap: wrap; justify-content: flex-end; }
.credits { color: var(--gold); font-size: .9rem; } .topup { font-size: .9rem; }
.ghost { background: transparent; border: 1px solid var(--rule2); border-radius: 999px; padding: .28rem .8rem; cursor: pointer; }
.ghost:hover { border-color: var(--gold); }

/* The bound book: a ruled page with a red margin rule along the binding */
.book { max-width: 1320px; margin: 0 auto; padding: 1.25rem 1.25rem 2rem 3.4rem; position: relative; }
.book::before { content: ""; position: absolute; top: 0; bottom: 0; left: 2.1rem; border-left: 1px solid var(--margin); box-shadow: 3px 0 0 -2px var(--margin); }
section { margin: 0 0 2.2rem; }
.bandhead { display: flex; align-items: baseline; gap: .8rem; flex-wrap: wrap; margin-bottom: .5rem; border-bottom: 1px solid var(--rule2); padding-bottom: .35rem; }
.linkbtn { background: none; border: 0; color: var(--gold); cursor: pointer; padding: 0; font-size: .92rem; }

/* Title page */
.titlepage h1 { font-size: clamp(1.8rem, 4vw, 2.7rem); line-height: 1.12; max-width: 22ch; }
.lede { color: var(--muted); max-width: 66ch; font-size: 1.05rem; }
.templates { display: flex; gap: .45rem; flex-wrap: wrap; align-items: center; margin: .9rem 0; }
.templates .label { color: var(--muted); font-size: .85rem; text-transform: uppercase; letter-spacing: .1em; margin-right: .2rem; }
.chip { background: var(--panel); border: 1px solid var(--rule2); border-radius: 999px; padding: .32rem .85rem; cursor: pointer; }
.chip:hover { border-color: var(--gold); color: var(--gold); }
.chip.quiet { background: transparent; color: var(--muted); }
.meta { display: grid; grid-template-columns: minmax(220px, 2fr) repeat(3, minmax(120px, 1fr)); gap: .75rem; }
.field { display: flex; flex-direction: column; gap: .2rem; font-size: .85rem; color: var(--muted); }
.field em { font-style: normal; color: var(--faint); }
.field.wide { width: 100%; }
input[type="text"], input[type="number"], input[type="month"], select, textarea { background: var(--panel); border: 1px solid var(--rule2); border-radius: 6px; padding: .42rem .55rem; color: var(--ink); min-width: 0; width: 100%; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--gold); outline: none; box-shadow: 0 0 0 2px color-mix(in srgb, var(--gold) 30%, transparent); }

/* The flow */
.flowband { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 1.4rem; align-items: start; }
.flowmain { min-width: 0; }
.sankeywrap { margin: 0; background: var(--panel); border: 1px solid var(--rule2); border-radius: 10px; box-shadow: var(--shadow); overflow-x: auto; overflow-y: hidden;
  background-image: repeating-linear-gradient(to bottom, transparent 0 27px, var(--rule) 27px 28px); }
.sankey { display: block; max-width: none; }
.sankeywrap.vertical .sankey { width: 100%; height: auto; }
#flowNote { font-variant-numeric: tabular-nums; }
.summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; margin-top: .8rem; border: 1px solid var(--rule2); border-radius: 8px; overflow: hidden; }
.fig { padding: .6rem .8rem; border-right: 1px solid var(--rule2); display: flex; flex-direction: column; gap: .05rem; background: var(--panel); }
.fig:last-child { border-right: 0; }
.figlabel { font-size: .74rem; color: var(--muted); text-transform: uppercase; letter-spacing: .1em; }
.figval { font-size: 1.15rem; font-weight: 700; }
.fig.neg .figval { color: var(--bad); } .fig.pos .figval { color: var(--ink); }
.figafter { font-size: .92rem; color: var(--c-move); } .figafter.up { color: var(--ok); } .figafter.down { color: var(--warn); }
.ratios { margin-top: .7rem; }
.ratiobar { display: flex; height: 10px; border-radius: 99px; overflow: hidden; background: var(--rule2); }
.ratiobar .seg { height: 100%; }
.ratiokey { list-style: none; padding: 0; margin: .4rem 0 0; display: flex; flex-wrap: wrap; gap: .3rem 1.1rem; font-size: .88rem; color: var(--muted); }
.ratiokey b { color: var(--ink); } .ratiokey b.off { color: var(--warn); }
.sw { display: inline-block; width: .7rem; height: .7rem; border-radius: 2px; margin-right: .35rem; vertical-align: -1px; }
.g-needs .sw, .seg.g-needs { background: var(--c-needs); } .g-wants .sw, .seg.g-wants { background: var(--c-wants); }
.g-savings .sw, .seg.g-savings { background: var(--c-savings); } .g-debt .sw, .seg.g-debt { background: var(--c-debt); }
.freeexports, .exports { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .8rem; }
.mini { background: transparent; border: 1px solid var(--rule2); border-radius: 6px; padding: .3rem .7rem; cursor: pointer; font-size: .88rem; }
.mini:hover { border-color: var(--gold); color: var(--gold); }
.mini.strong { border-color: var(--gold); color: var(--gold); }

/* Levers */
.leverpanel { background: var(--panel); border: 1px solid var(--rule2); border-radius: 10px; padding: .8rem .9rem; position: sticky; top: 3.6rem; max-height: calc(100vh - 4.6rem); overflow-y: auto; }
.lvgroup { margin: .7rem 0 .2rem; }
.lever { padding: .35rem 0 .25rem; border-bottom: 1px solid var(--rule); }
.lvtop { display: flex; justify-content: space-between; gap: .5rem; font-size: .9rem; }
.lvname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lvval { color: var(--muted); white-space: nowrap; } .lvval.moved { color: var(--c-move); font-weight: 600; }
input[type="range"] { width: 100%; margin: .2rem 0 0; accent-color: var(--gold); height: 1.4rem; cursor: pointer; }
.lever.g-income input[type="range"] { accent-color: var(--c-income); } .lever.g-needs input[type="range"] { accent-color: var(--c-needs); }
.lever.g-wants input[type="range"] { accent-color: var(--c-wants); } .lever.g-savings input[type="range"] { accent-color: var(--c-savings); } .lever.g-debt input[type="range"] { accent-color: var(--c-debt); }

/* The ledger: ruled pages, one per category */
.ledger { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: .9rem; align-items: start; }
.block { background: var(--panel); border: 1px solid var(--rule2); border-radius: 8px; padding: .55rem .7rem .6rem; border-top: 3px solid var(--faint);
  background-image: repeating-linear-gradient(to bottom, transparent 0 37px, var(--rule) 37px 38px); background-position: 0 6px; }
.block.income { border-top-color: var(--c-income); } .block.g-needs { border-top-color: var(--c-needs); } .block.g-wants { border-top-color: var(--c-wants); }
.block.g-savings { border-top-color: var(--c-savings); } .block.g-debt { border-top-color: var(--c-debt); } .block.debts { border-top-color: var(--c-debt); border-top-style: double; border-top-width: 4px; grid-column: 1 / -1; }
.blockhead { display: flex; gap: .4rem; align-items: center; margin-bottom: .3rem; }
.blockhead h3 { margin: 0; flex: 1; }
.blockhead .catname { flex: 1; font-weight: 600; background: transparent; border-color: transparent; padding-left: .2rem; }
.blockhead .catname:hover { border-color: var(--rule2); }
.blockhead select { width: auto; font-size: .85rem; padding: .25rem .3rem; }
.blocktotal { font-weight: 700; white-space: nowrap; } .blocktotal.moved { color: var(--c-move); }
.blockhead .hint { font-size: .8rem; color: var(--muted); }
.row { display: grid; grid-template-columns: minmax(0, 1fr) 7.2rem auto; gap: .35rem; align-items: center; padding: .14rem 0; }
.block.income .row { grid-template-columns: minmax(0, 1fr) 7.2rem auto auto; }
.row.debt { grid-template-columns: minmax(0, 1fr) 7.2rem 5rem auto; }
.row input { background: transparent; border-color: transparent; border-bottom: 1px dotted var(--rule2); border-radius: 0; padding: .28rem .2rem; }
.row input:hover { border-bottom-color: var(--muted); }
.row input.amt, .row input.apr { text-align: right; }
.irr { font-size: .8rem; color: var(--muted); white-space: nowrap; display: flex; gap: .2rem; align-items: center; }
.rm { background: none; border: 0; color: var(--faint); cursor: pointer; font-size: 1.1rem; line-height: 1; padding: .1rem .3rem; }
.rm:hover { color: var(--bad); }
.addline { background: none; border: 1px dashed var(--rule2); color: var(--muted); border-radius: 6px; padding: .2rem .6rem; cursor: pointer; font-size: .85rem; margin-top: .3rem; }
.addline:hover { color: var(--gold); border-color: var(--gold); }
.ledger > .addline { align-self: stretch; min-height: 3rem; }

/* CSV import */
.importgrid { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 1.2rem; }
.drop { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .4rem; border: 2px dashed var(--rule2); border-radius: 10px; padding: 1.2rem; color: var(--muted); cursor: pointer; margin-bottom: .6rem; text-align: center; }
.drop.over { border-color: var(--gold); color: var(--gold); }
.drop input { max-width: 100%; }
.row-actions { display: flex; gap: .4rem; margin: .5rem 0; }
.buckets { width: 100%; border-collapse: collapse; font-size: .9rem; margin: .3rem 0 .6rem; }
.buckets th, .buckets td { text-align: left; padding: .25rem .4rem; border-bottom: 1px solid var(--rule); }
.buckets td.num { text-align: right; }
.buckets tr.inc td:first-child { color: var(--c-income); }
.rules { display: flex; flex-direction: column; gap: .3rem; }
.rule { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) 6.5rem auto; gap: .3rem; }
.rule input, .rule select { font-size: .85rem; padding: .3rem .4rem; }

/* Run */
.runband { display: grid; grid-template-columns: minmax(280px, 380px) minmax(0, 1fr); gap: 1.5rem; align-items: start; }
.runbox { background: var(--panel); border: 1px solid var(--rule2); border-radius: 10px; padding: .9rem 1rem; display: flex; flex-direction: column; gap: .5rem; }
.runhead, .reshead { display: flex; align-items: baseline; justify-content: space-between; gap: .6rem; }
.lanechip { font-size: .78rem; text-transform: uppercase; letter-spacing: .1em; border: 1px solid var(--gold); color: var(--gold); border-radius: 999px; padding: .08rem .55rem; }
.primary { background: var(--gold); color: var(--gold-ink); border: 0; border-radius: 8px; padding: .62rem 1rem; font-weight: 700; cursor: pointer; }
.primary.small { padding: .42rem .85rem; font-size: .92rem; }
.primary:hover:not(:disabled) { filter: brightness(1.08); }
.primary:disabled { opacity: .45; cursor: not-allowed; }
.notice { color: var(--muted); border-left: 2px solid var(--gold); padding-left: .5rem; }
.gate { color: var(--warn); margin: 0; }
.gateaction { font-weight: 600; }
.progress { border-top: 1px solid var(--rule2); padding-top: .5rem; }
.steps { list-style: none; padding: 0; margin: .4rem 0 0; font-size: .9rem; }
.step { color: var(--faint); } .step.active { color: var(--ink); } .step.done { color: var(--ok); }
.step-ic { display: inline-block; width: 1.1rem; }

/* Result */
.result { min-width: 0; }
.headline { font-family: "Iowan Old Style", Palatino, Georgia, serif; font-size: 1.15rem; margin: .4rem 0 .8rem; }
.moves { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .55rem; }
.move { background: var(--panel); border: 1px solid var(--rule2); border-left: 3px dashed var(--c-move); border-radius: 8px; padding: .55rem .75rem; }
.move.invalid { border-left-color: var(--bad); opacity: .8; }
.movehead { display: flex; flex-wrap: wrap; align-items: center; gap: .3rem .65rem; }
.rank { display: inline-grid; place-items: center; width: 1.55rem; height: 1.55rem; border-radius: 50%; background: var(--c-move); color: #1b1a14; font-weight: 700; font-size: .85rem; }
.take { font-size: .82rem; color: var(--muted); display: flex; gap: .2rem; align-items: center; }
.movecat { font-size: 1.02rem; }
.movefig { color: var(--muted); } .movedelta { font-weight: 700; } .movedelta.cut { color: var(--c-move); } .movedelta.raise { color: var(--ok); }
.why, .tradeoff { margin: .3rem 0 0; font-size: .94rem; } .tradeoff { color: var(--muted); }
.flag { margin: .3rem 0 0; color: var(--bad); font-size: .86rem; } .flag.soft { color: var(--warn); }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: .8rem 0; }
.cols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.2rem; }
.risks, .ratioread, .checks, .history, #notesList { list-style: none; padding: 0; margin: .3rem 0; }
.risk { border-bottom: 1px solid var(--rule); padding: .35rem 0; }
.risk p { margin: .15rem 0 0; font-size: .93rem; }
.lvl { font-size: .72rem; text-transform: uppercase; letter-spacing: .1em; border-radius: 4px; padding: .05rem .35rem; margin-right: .4rem; border: 1px solid currentColor; }
.lvl-high .lvl { color: var(--bad); } .lvl-medium .lvl { color: var(--warn); } .lvl-low .lvl { color: var(--muted); }
.kind { font-size: .82rem; color: var(--muted); }
.firstq { font-family: "Iowan Old Style", Palatino, Georgia, serif; font-size: 1.05rem; border-left: 3px solid var(--gold); padding-left: .6rem; margin: .3rem 0; }
.ratio { display: flex; flex-wrap: wrap; gap: .2rem .5rem; align-items: baseline; padding: .2rem 0; border-bottom: 1px solid var(--rule); font-size: .92rem; }
.ratio .rg { min-width: 7.5rem; } .ratio .verdict { font-size: .78rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.ratio.v-high .verdict, .ratio.v-low .verdict { color: var(--warn); } .ratio .rnote { color: var(--muted); font-size: .86rem; flex-basis: 100%; }
.check { padding: .2rem 0; font-size: .92rem; } .check .ck { display: inline-block; width: 1rem; font-weight: 700; }
.check.ok .ck { color: var(--ok); } .check.warn .ck { color: var(--warn); } .check.bad .ck { color: var(--bad); }
.none { color: var(--muted); font-style: italic; }
.raw { white-space: pre-wrap; background: var(--panel); padding: .7rem; border-radius: 8px; max-height: 360px; overflow: auto; font-size: .85rem; }

/* History */
.history { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .5rem; }
.histitem { width: 100%; text-align: left; background: var(--panel); border: 1px solid var(--rule2); border-radius: 8px; padding: .5rem .7rem; cursor: pointer; display: flex; flex-direction: column; gap: .1rem; }
.histitem:hover { border-color: var(--gold); }

.foot { max-width: 1320px; margin: 0 auto; padding: 1rem 1.25rem 2.5rem 3.4rem; color: var(--muted); font-size: .88rem; border-top: 1px solid var(--rule2); }
.toasts { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: .4rem; z-index: 50; max-width: min(420px, calc(100vw - 2rem)); }
.toast { background: var(--panel2); border: 1px solid var(--rule2); border-left: 3px solid var(--ok); padding: .55rem .75rem; border-radius: 8px; box-shadow: var(--shadow); transition: opacity .5s; font-size: .92rem; }
.toast.err { border-left-color: var(--bad); } .toast.gone { opacity: 0; }

@media (max-width: 1000px) {
  .flowband { grid-template-columns: minmax(0, 1fr); }
  .leverpanel { position: static; max-height: none; }
  .levers { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 0 1rem; }
  .runband { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  .book { padding: 1rem 16px 1.5rem 16px; }
  .book::before { display: none; }
  .foot { padding: 1rem 16px 2rem; }
  .meta { grid-template-columns: 1fr 1fr; }
  .meta .field:first-child { grid-column: 1 / -1; }
  .summary { grid-template-columns: 1fr 1fr; }
  .fig:nth-child(2) { border-right: 0; } .fig:nth-child(-n+2) { border-bottom: 1px solid var(--rule2); }
  .levers { display: block; }
  .ledger { grid-template-columns: minmax(0, 1fr); }
  .row { grid-template-columns: minmax(0, 1fr) 6rem auto; }
  .block.income .row { grid-template-columns: minmax(0, 1fr) 6rem auto; }
  .block.income .row .irr { grid-column: 1 / 2; }
  .row.debt { grid-template-columns: minmax(0, 1fr) 5.5rem 4rem auto; }
  .importgrid, .cols { grid-template-columns: minmax(0, 1fr); }
  .rule { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } .rule .rm { justify-self: end; }
  .backlink { display: none; }
}
