:root {
  --ink: #1d2430;
  --ink-2: #4a5464;
  --muted: #7a8494;
  --line: #e3e0d9;
  --line-2: #efece6;
  --paper: #faf8f4;
  --card: #ffffff;
  --gold: #a8812f;
  --gold-soft: #f4ecd9;
  --neg: #a23b2c;
  --pos: #2f6b4f;
  --warn-bg: #fff4dc;
  --warn: #8a5a00;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(29,36,48,.06), 0 4px 16px rgba(29,36,48,.05);
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; font-size: 16px; }
/* Full Mac screen: everything sized in rem grows with this */
@media (min-width: 1100px) { html { font-size: 17px; } }
@media (min-width: 1650px) { html { font-size: 18px; } }
@media (min-width: 1900px) { html { font-size: 19px; } }
body { margin: 0; background: var(--paper); color: var(--ink); font: 1rem/1.5 var(--font); }
a { color: inherit; }
h1, h2 { font-family: var(--serif); font-weight: 600; letter-spacing: -.01em; margin: 0 0 .3em; }
h1 { font-size: 1.9rem; }
h2 { font-size: 1.2rem; margin-top: 1.5em; }
code { font-family: var(--mono); font-size: .85em; background: var(--line-2); padding: .1em .35em; border-radius: 4px; word-break: break-all; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: .85rem; }
.neg { color: var(--neg); }
.pos { color: var(--pos); }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 24px; padding: 10px 24px; background: var(--ink); color: #f3efe6; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-family: var(--serif); font-size: 1.15rem; white-space: nowrap; }
.brand em { font-style: italic; color: #d9bd7a; }
.mark { display: inline-grid; place-items: center; width: 30px; height: 30px; border: 1.5px solid #d9bd7a; color: #d9bd7a; border-radius: 6px; font: 600 .78rem/1 var(--serif); letter-spacing: .04em; }
.mark.big { width: 44px; height: 44px; font-size: 1rem; color: var(--gold); border-color: var(--gold); }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a { text-decoration: none; padding: .4rem .9rem; font-size: 1.02rem; border-radius: 6px; color: #c9cdd4; }
.topbar nav a:hover { color: #fff; background: rgba(255,255,255,.06); }
.topbar nav a.on { color: #fff; background: rgba(217,189,122,.16); box-shadow: inset 0 -2px 0 #d9bd7a; }
.who { display: flex; align-items: center; gap: 12px; color: #c9cdd4; font-size: .95rem; margin: 0; }
.who .link { color: #d9bd7a; }

/* ---------- layout ---------- */
.wrap { max-width: 1100px; margin: 0 auto; padding: 28px 24px 64px; }
.wrap.wide { max-width: 1760px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; box-shadow: var(--shadow); }
.panel.narrow { max-width: 520px; margin: 40px auto; }
.panel.ok { border-left: 4px solid var(--pos); }
.panel summary { cursor: pointer; }
.panel[open] summary { margin-bottom: 14px; }
.flash { background: var(--gold-soft); border: 1px solid #e6d3a8; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.error { color: var(--neg); background: #fbecea; padding: 8px 12px; border-radius: 6px; }

/* ---------- forms ---------- */
label { display: block; font-size: .85rem; color: var(--ink-2); font-weight: 500; }
input, select, textarea { font: inherit; color: var(--ink); background: #fff; border: 1px solid #cfcac0; border-radius: 7px; padding: 8px 10px; width: 100%; margin-top: 4px; }
input:focus, select:focus, textarea:focus, button:focus-visible, tr[tabindex="0"]:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 400; color: var(--ink); }
label.check input { width: auto; margin: 0; }
.hint { font-weight: 400; color: var(--muted); font-size: .78rem; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: 500 .9rem var(--font); padding: 8px 14px; border-radius: 7px; border: 1px solid #cfcac0; background: #fff; color: var(--ink); text-decoration: none; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: #2c3546; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; padding: 11px; margin-top: 8px; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--gold); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.link.danger { color: var(--neg); }

/* ---------- auth ---------- */
body.auth { background: radial-gradient(circle at 20% 0%, #2a3242 0, var(--ink) 60%); min-height: 100vh; }
.auth-wrap { display: grid; place-items: center; min-height: 100vh; padding: 24px; }
.auth-card { background: var(--card); width: 100%; max-width: 380px; border-radius: 14px; padding: 30px 28px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.auth-card form label { margin-top: 14px; }
.auth-card label.remember { margin-top: 12px; }
.auth-card .remember + .hint { margin: 2px 0 0 26px; }
.auth-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.auth-head h1 { font-size: 1.35rem; line-height: 1.15; margin: 0; }
.auth-head em { color: var(--gold); }
input.code { font: 600 1.5rem var(--mono); letter-spacing: .3em; text-align: center; }
.qr { display: grid; place-items: center; margin: 8px 0; }
.qr svg { width: 200px; height: 200px; }
.secret { display: inline-block; margin-top: 4px; font-size: .95rem; letter-spacing: .05em; }
.steps { padding-left: 20px; margin: 0 0 8px; }
.steps li { margin-bottom: 6px; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 20px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; text-decoration: none; box-shadow: var(--shadow); }
.kpi span { display: block; font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.kpi strong { display: block; font: 600 1.9rem/1.2 var(--serif); font-variant-numeric: tabular-nums; margin-top: 4px; }
.kpi.neg strong { color: var(--neg); }
.kpi.pos strong { color: var(--pos); }
.kpi.warn { background: var(--warn-bg); border-color: #efd9a4; }
.kpi.warn strong { color: var(--warn); }
.kpi em { font-style: normal; font-size: .8rem; color: var(--warn); }
.range { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.range label { font-size: .78rem; }
.range select { width: auto; }
.years { display: inline-flex; background: #fff; border: 1px solid #cfcac0; border-radius: 8px; overflow: hidden; }
.years a { padding: .45rem 1rem; text-decoration: none; color: var(--ink-2); border-left: 1px solid #e3ded4; font-weight: 500; }
.years a:first-child { border-left: 0; }
.years a:hover { background: #f7f4ee; color: var(--ink); }
.years a.on { background: var(--ink); color: #fff; }

/* ---------- report table ---------- */
.table-scroll { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; }
.report { font-variant-numeric: tabular-nums; font-size: .95rem; }
.report th, .report td { padding: .5rem .5rem; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line-2); }
.report thead th { position: sticky; top: 0; background: #f4f1ea; font-weight: 600; color: var(--ink-2); font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; }
.report .sticky { position: sticky; left: 0; background: var(--card); text-align: left; z-index: 1; min-width: 11.5rem; }
.report thead .sticky { background: #f4f1ea; z-index: 2; }
.report td a { text-decoration: none; }
.report td a:hover { text-decoration: underline; }
.report .tot { background: #faf8f3; font-weight: 600; }
.grp-row th, .grp-row td { font-weight: 600; border-top: 1px solid var(--line); }
.toggle { background: none; border: 0; font: inherit; color: inherit; cursor: pointer; padding: 0; display: flex; align-items: center; gap: 6px; }
.toggle::before { content: '▸'; color: var(--gold); width: 10px; transition: transform .15s; }
.toggle[aria-expanded="true"]::before { transform: rotate(90deg); }
.cat-row th { font-weight: 400; padding-left: 28px; color: var(--ink-2); }
.cat-row td { color: var(--ink-2); }
.report tfoot th, .report tfoot td { background: #f7f4ee; border-top: 1px solid var(--line); }
.report tfoot .sticky { background: #f7f4ee; }
.report tfoot tr.strong th, .report tfoot tr.strong td { font-weight: 700; border-top: 2px solid var(--ink); }

/* ---------- transactions ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.filters input[type=search] { flex: 1 1 260px; margin: 0; }
.filters select { width: auto; margin: 0; max-width: 16rem; }
.txns { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); border-collapse: separate; border-spacing: 0; }
/* Frozen header on Transactions: menu bar + title/filters + column headings stay put */
.sticky-head { position: sticky; top: var(--topbar-h, 56px); z-index: 15; background: var(--paper); margin: -28px -24px 0; padding: 20px 24px 12px; box-shadow: 0 6px 8px -8px rgba(29,36,48,.25); }
.sticky-head .page-head { margin-bottom: 14px; }
.sticky-head .filters { margin-bottom: 0; }
.txns thead th { position: sticky; top: var(--stack-h, 200px); z-index: 10; box-shadow: 0 1px 0 var(--line); }
.txns thead th:first-child { border-top-left-radius: var(--radius); }
.txns thead th:last-child { border-top-right-radius: var(--radius); }
.head-actions { display: flex; gap: 8px; align-items: center; }
.sticky-head + .txns { margin-top: 14px; }
.txns th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: #f4f1ea; padding: 9px 12px; }
.txns td { padding: .7rem .9rem; border-top: 1px solid var(--line-2); vertical-align: top; }
.txns .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }
.txns .date { white-space: nowrap; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.txns .desc small { display: block; color: var(--muted); font-size: .82rem; word-break: break-word; }
.txns .desc .note { color: var(--ink-2); font-style: italic; }
.txns .desc .note:empty { display: none; }
.chk { font-size: .75rem; background: var(--line-2); padding: 1px 6px; border-radius: 4px; color: var(--ink-2); }
.txns.editable tbody tr { cursor: pointer; }
.txns.editable tbody tr:hover { background: #fbf8f1; }
.txns tr.saved { animation: flashrow 1.2s; }
@keyframes flashrow { from { background: var(--gold-soft); } to { background: transparent; } }
.pill { display: inline-block; font-size: .78rem; padding: 2px 8px; border-radius: 999px; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.pill.g-Income { background: #e5f0ea; color: var(--pos); }
.pill.g-COGS { background: #f1ebde; color: #6d5523; }
.pill.g-Debt { background: #f4e4e1; color: #8a3a2e; }
.pill.g-Personalq { background: #ece6f3; color: #5b4380; }
.pill.g-Unknown { background: #fff0cf; color: var(--warn); }
.pill.g-OwnerFinancing { background: #e2ebf3; color: #2d5878; }
.flag { display: block; font-size: .72rem; color: var(--warn); margin-top: 3px; }
.empty { text-align: center; color: var(--muted); padding: 30px !important; }

/* ---------- edit sheet ---------- */
dialog.sheet { border: 0; border-radius: 14px; padding: 0; width: min(36rem, 100vw - 24px); box-shadow: 0 24px 80px rgba(0,0,0,.3); color: var(--ink); }
dialog.sheet::backdrop { background: rgba(20,25,33,.45); }
.sheet form { padding: 0 20px 18px; }
.sheet header { display: flex; justify-content: space-between; align-items: center; padding: 16px 0 8px; }
.sheet header h2 { margin: 0; }
.sheet .x { background: none; border: 0; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); }
.sheet label { margin-top: 12px; }
.facts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; background: #f7f4ee; border-radius: 8px; padding: 10px 12px; }
.facts span { display: block; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.facts .full { grid-column: 1 / -1; }
.facts code { background: none; padding: 0; font-size: .8rem; }
fieldset.rule { border: 1px dashed #d8d2c6; border-radius: 8px; margin: 14px 0 0; padding: 10px 12px; }
.sheet footer { display: flex; align-items: center; gap: 8px; margin-top: 16px; }
.spacer { flex: 1; }

/* ---------- simple tables (rules, imports) ---------- */
.simple { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); font-size: .95rem; box-shadow: var(--shadow); }
.simple th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: #f4f1ea; padding: 8px 10px; }
.simple td { padding: 8px 10px; border-top: 1px solid var(--line-2); vertical-align: top; }
.simple .num { text-align: right; font-variant-numeric: tabular-nums; }
.rules tr.off { opacity: .45; }
.rules tr.bad { background: #fbecea; }
.form-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 14px; }
.form-foot form { margin: 0; }
.rule-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px 14px; align-items: end; }
.rule-form .grow { grid-column: 1 / -1; }
form.inline { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
form.inline label { flex: 1 1 200px; }
.upload { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.upload input { flex: 1 1 260px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,.25); z-index: 100; max-width: calc(100vw - 32px); }

/* ---------- phones ---------- */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 10px 16px; }
  .brand-text { display: none; }
  .topbar nav { order: 3; flex-basis: 100%; overflow-x: auto; }
  .who { margin-left: auto; }
  .wrap { padding: 18px 16px 48px; }
  h1 { font-size: 1.45rem; }
  .filters select { flex: 1 1 45%; max-width: none; }
  .txns thead { display: none; }
  .sticky-head { position: static; margin: 0; padding: 0; box-shadow: none; background: none; }
  .txns, .txns tbody, .txns tr, .txns td { display: block; width: 100%; }
  .txns tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 12px 14px; border-top: 1px solid var(--line-2); }
  .txns td { border: 0; padding: 0; }
  .txns .date { grid-column: 1; grid-row: 1; font-size: .78rem; }
  .txns .num { grid-column: 2; grid-row: 1 / 3; align-self: center; }
  .txns .desc { grid-column: 1; grid-row: 2; }
  .txns .cat { grid-column: 1 / -1; grid-row: 3; margin-top: 4px; }
  .txns .cat .flag { display: inline; margin-left: 8px; }
  .report .sticky { min-width: 160px; }
  .sheet footer { flex-wrap: wrap; }
}
