:root {
  --bg: #f5f7f6; --card: #ffffff; --ink: #1c2421; --muted: #6b7873; --line: #dde3e0;
  --brand: #1f7a5a; --brand-ink: #ffffff; --bad: #c0392b; --warn-bg: #fdecea; --sel-bg: #e3f2ec;
  --unpaid-bg: #fff3a3; --unpaid-ink: #5c4a00; --paid-bg: #d4efdf; --paid-ink: #14532d;
  --chart-track: #eef2f0; --chart-plan: #b9d9cb; --chart-real: #1f7a5a; --chart-over: #c0392b;
  --chart-line: #6b7873; --chart-income: #b7791f; --chart-cur: #fff3a3;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #121715; --card: #1b221f; --ink: #e8eeeb; --muted: #93a09a; --line: #2c3631;
          --brand: #3fae86; --brand-ink: #0b1511; --bad: #ff7b6b; --warn-bg: #3a1f1c; --sel-bg: #183229;
          --unpaid-bg: #5a4b0c; --unpaid-ink: #ffe680; --paid-bg: #1d4a33; --paid-ink: #a7e8c3;
          --chart-track: #222b27; --chart-plan: #2f5a48; --chart-real: #5cc79f; --chart-over: #ff7b6b;
          --chart-line: #c9d3ce; --chart-income: #f0b44c; --chart-cur: #3d3510; }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 8px 12px; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
button.mini { padding: 0 6px; border-radius: 6px; margin-left: 4px; }
input, select { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px; min-width: 0; }

.top { display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 8px; }
.top { gap: 8px; }
.top > div:first-child { min-width: 0; }
.top h1 { font-size: 1.15rem; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hright { flex: none; }
.ver { display: block; font-size: .7rem; color: var(--muted); }
.sync { font-size: .78rem; color: var(--muted); }
.hright { display: flex; align-items: center; gap: 8px; }
.hright select { padding: 4px 6px; font-size: .85rem; }
.banner { margin: 0 12px 8px; padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--bad); font-size: .88rem; }
main.ro input, main.ro select, main.ro button { pointer-events: none; opacity: .6; }
.sync.error { color: var(--bad); }
/* Account icon: the signed-in email is its tooltip and is shown when it is tapped. */
.acct { display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px; padding: 0;
  border-radius: 50%; color: var(--muted); }
.acct-mail { word-break: break-all; }

.tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; gap: 6px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: var(--card); border-top: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; background: transparent; padding: 10px 0; color: var(--muted); font-weight: 600; }
.tabs button.on { background: var(--brand); color: var(--brand-ink); }
main { padding: 4px 12px 96px; max-width: 760px; margin: 0 auto; }
main.wide { max-width: none; }

.pbar { display: flex; gap: 8px; margin: 0 0 10px; }
/* Period, totals and Plan / Real stay at the top while the rows scroll (Setup > Display). */
.phead.pinned { position: sticky; top: 0; z-index: 5; background: var(--bg); padding-top: 6px; }
.phead.pinned .lhead { position: static; }
.pbar select { flex: 1; font-weight: 600; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; font-size: .8rem; }
.card h3 { margin: 0 0 4px; font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.card div { display: flex; justify-content: space-between; gap: 4px; }
.card b { font-variant-numeric: tabular-nums; }
.neg { color: var(--bad); }

.search { width: 100%; margin: 6px 0 4px; }
/* Not pinned: only the Plan / Real header sticks, as before; it needs main as its sticky container. */
.phead:not(.pinned) { display: contents; }
.phead:not(.pinned) .pbar { margin-top: 6px; }
.lhead, .row { display: grid; grid-template-columns: 1fr 92px 92px 32px; gap: 6px; align-items: center; }
.lhead { position: sticky; top: 0; background: var(--bg); padding: 4px 0; font-size: .75rem; color: var(--muted);
  text-transform: uppercase; z-index: 2; }
.lhead span:not(:first-child) { text-align: right; padding-right: 8px; }
h2.sec { font-size: .85rem; margin: 14px 0 4px; color: var(--brand); text-transform: uppercase; letter-spacing: .04em; }
.row { padding: 3px 0; border-bottom: 1px solid var(--line); }
.row .nm { overflow: hidden; text-overflow: ellipsis; }
.row input.num { text-align: right; width: 100%; font-variant-numeric: tabular-nums; padding: 7px 8px; }
.row.over { background: var(--warn-bg); border-radius: 8px; }
.row.over input[data-f="r"] { color: var(--bad); font-weight: 600; }
/* Expense amounts not paid yet: yellow Real field, as in the Excel sheet; the mark toggles it. */
input.num.unpaid { background: var(--unpaid-bg); color: var(--unpaid-ink); border-color: var(--unpaid-bg); }
.row button.pd { width: 32px; height: 32px; padding: 0; border-radius: 50%; color: var(--muted); font-weight: 700; }
/* Selected rows (Period view): tap the name; the bar above the tabs shows their totals. */
.row .nm[data-act="sel"] { cursor: pointer; border-radius: 6px; }
.row.sel { background: var(--sel-bg); border-radius: 8px; box-shadow: inset 3px 0 0 var(--brand); }
.row.sel .nm { font-weight: 600; padding-left: 6px; }
.selbar { position: fixed; left: 0; right: 0; bottom: calc(58px + env(safe-area-inset-bottom)); z-index: 9;
  display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; padding: 8px 12px;
  background: var(--card); border-top: 2px solid var(--brand); box-shadow: 0 -2px 8px rgba(0, 0, 0, .12); font-size: .9rem; }
.selbar[hidden] { display: none; }
.selbar small { color: var(--muted); }
.selbar .seln { font-weight: 700; color: var(--brand); }
.selbar .selbtns { display: flex; gap: 6px; margin-left: auto; }
.selbar button { padding: 6px 10px; }
body.hassel main { padding-bottom: 170px; }
.row button.pd.unpaid { background: var(--unpaid-bg); color: var(--unpaid-ink); border-color: var(--unpaid-bg); }
/* Payroll check at the end of the Period list. */
.paycheck { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px 10px; margin-bottom: 8px; }
.paycheck > div { display: grid; grid-template-columns: 1fr 130px; gap: 6px; align-items: center; min-height: 40px; border-bottom: 1px solid var(--line); }
.paycheck > div:last-child { border-bottom: 0; }
.paycheck b { text-align: right; font-variant-numeric: tabular-nums; padding-right: 8px; }
.paycheck input { text-align: right; width: 100%; font-variant-numeric: tabular-nums; }

.gridwrap { overflow: auto; max-height: calc(100vh - 170px); border: 1px solid var(--line); border-radius: 10px; background: var(--card); }
table.grid { border-collapse: separate; border-spacing: 0; font-size: .82rem; }
table.grid th, table.grid td { padding: 3px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--card); }
table.grid thead th { position: sticky; top: 0; z-index: 3; text-align: center; }
table.grid thead tr:nth-child(2) th { top: 29px; font-weight: 400; color: var(--muted); }
table.grid th[data-cur] { background: var(--brand); color: var(--brand-ink); }
table.grid .sticky { position: sticky; left: 0; z-index: 2; text-align: left; min-width: 150px; max-width: 150px; overflow: hidden; text-overflow: ellipsis; }
table.grid thead .sticky { z-index: 4; }
table.grid input { width: 84px; text-align: right; padding: 4px 6px; border-radius: 6px; }
table.grid td.tot { text-align: right; font-variant-numeric: tabular-nums; padding-right: 8px; }
table.grid tr.secrow td { background: var(--bg); color: var(--brand); font-weight: 700; text-transform: uppercase; font-size: .72rem; }
/* Footer rows are 29px tall, the step of the sticky offsets below, so they never cover the last row. */
table.grid tfoot td, table.grid tfoot th { position: sticky; bottom: 0; height: 29px; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
table.grid tfoot tr:nth-child(1) td, table.grid tfoot tr:nth-child(1) th { bottom: 58px; }
table.grid tfoot tr:nth-child(2) td, table.grid tfoot tr:nth-child(2) th { bottom: 29px; }
table.grid tfoot th.sticky { text-align: left; z-index: 4; }

/* Setup sub-menu: chips that wrap to a second line on a narrow phone, so all are visible. */
.subtabs { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 0 2px; margin: 0 0 4px; }
.subtabs button { flex: none; border-radius: 999px; padding: 6px 14px; font-size: .88rem; color: var(--muted); font-weight: 600; }
.subtabs button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

.panel { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 10px 0; }
.panel h2 { margin: 0 0 8px; font-size: 1rem; }
.panel p { margin: 4px 0 8px; color: var(--muted); font-size: .88rem; }
.catrow { display: grid; grid-template-columns: 1fr 96px auto auto auto; gap: 4px; margin-bottom: 4px; }
.catrow .catinfo { grid-column: 1 / -1; text-align: left; padding: 4px 10px; margin-bottom: 6px; font-size: .85rem; }
.catrow .catinfo small { margin-left: 6px; }
.prow { display: grid; grid-template-columns: 1fr auto auto; gap: 6px; align-items: center; margin-bottom: 4px; }
.lrow { display: grid; grid-template-columns: 1fr 140px auto; gap: 4px; margin-bottom: 4px; }
.lrow.two { grid-template-columns: 1fr auto; }
small.rl { display: block; font-size: .72rem; color: var(--muted); font-weight: 400; overflow: hidden; text-overflow: ellipsis; }
.catinfo small.rl, .prow small.rl { display: inline; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; }
.check { display: flex; align-items: flex-start; gap: 10px; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; padding: 0; flex: none; accent-color: var(--brand); }

.login { max-width: 360px; margin: 18vh auto 0; padding: 0 20px; text-align: center; }
.login button { display: inline-flex; align-items: center; gap: 10px; padding: 12px 18px; }
.err { color: var(--bad); }
.dlg { border: 1px solid var(--line); border-radius: 14px; padding: 18px 16px 14px; width: min(92vw, 420px);
  background: var(--card); color: var(--ink); box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.dlg:focus { outline: none; }
.dlg::backdrop { background: rgba(0,0,0,.45); }
.dlg h2 { margin: 0 0 8px; font-size: 1.05rem; }
.dlg p { margin: 0 0 16px; line-height: 1.45; }
.dlg-btns { display: flex; justify-content: flex-end; gap: 8px; }
.dlg-btns.stack { flex-direction: column; }
.dlg-btns button { min-width: 88px; padding: 10px 14px; }
.dlg-btns.stack button { text-align: left; display: flex; flex-direction: column; gap: 2px; }
.dlg-btns.stack button span { font-weight: 600; }
.dlg-btns small { color: var(--muted); font-size: .8rem; }
/* Wider dialogs with forms: category details, new year, values changed on both devices. */
.dlg.wide { width: min(96vw, 640px); max-height: 92vh; overflow: auto; }
.dlg h3 { margin: 14px 0 4px; font-size: .92rem; }
.dlg .hint { margin: 0 0 8px; color: var(--muted); font-size: .82rem; }
.frow { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 8px; }
.frow label { display: flex; flex-direction: column; gap: 4px; font-size: .82rem; color: var(--muted); flex: 1 1 180px; }
.frow.apply { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--line); }
.rhead, .rrow { display: grid; grid-template-columns: 138px 1fr 110px 90px auto; gap: 4px; align-items: center; margin-bottom: 4px; }
.rhead { font-size: .72rem; color: var(--muted); text-transform: uppercase; }
.rules + button { margin: 4px 0 6px; }
.hols { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 6px; font-size: .85rem; }
.review { display: flex; flex-direction: column; gap: 4px; }
.rvrow { display: grid; grid-template-columns: 1fr 90px 130px; gap: 6px; align-items: center; font-size: .88rem; padding: 3px 0; border-bottom: 1px solid var(--line); }
.rvrow input { text-align: right; }
.clist { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.citem .clabel { font-weight: 600; font-size: .88rem; margin-bottom: 4px; }
.cchoices { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.cchoices button { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; }
.cchoices button small { color: var(--muted); font-size: .75rem; }
.cchoices button.on { border-color: var(--brand); box-shadow: inset 0 0 0 2px var(--brand); }
.dlist { margin: 0 0 14px; padding-left: 18px; font-size: .88rem; }
@media (max-width: 520px) {
  .rhead { display: none; }
  .rrow { grid-template-columns: 1fr 1fr auto; }
  .rrow > :nth-child(3), .rrow > :nth-child(4) { grid-column: span 1; }
  .rvrow { grid-template-columns: 1fr 80px; }
  .rvrow select { grid-column: 1 / -1; }
}

/* Due-date check above the Year grid. */
.checks { margin: 6px 0 10px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: .82rem; }
.checks summary { cursor: pointer; font-weight: 600; }
.checkwrap { overflow: auto; max-height: 40vh; margin-top: 6px; }
.checks table { border-collapse: collapse; width: 100%; }
.checks th, .checks td { text-align: left; vertical-align: top; padding: 4px 6px; border-top: 1px solid var(--line); }
.checks th { white-space: nowrap; font-weight: 600; }
.checks .strip { white-space: nowrap; }
.checks .strip span { display: inline-block; margin-right: 6px; }
.checks .ok { color: var(--muted); }
.checks .bad, .checks .err { color: var(--bad); }
.checks .info { color: var(--muted); }

/* Cards tab: card charges outside the budget. Yellow = money moved to living expenses (as in the
   sheet), green = the card is paid. */
.muted { color: var(--muted); }
.chips { margin-top: 4px; }
.ctots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 6px 0 8px; }
.ctot { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; border-radius: 12px; padding: 8px 10px; }
.ctot small { font-size: .74rem; color: var(--muted); font-weight: 600; }
.ctot b { font-variant-numeric: tabular-nums; font-size: 1rem; }
.ctot.warn b { color: var(--unpaid-ink); }
.ctot.warn { background: var(--unpaid-bg); border-color: var(--unpaid-bg); }
.ctot.warn small { color: var(--unpaid-ink); }
.ctot.on { box-shadow: inset 0 0 0 2px var(--brand); border-color: var(--brand); }
.chbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 4px; }
.chbar button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.chrow { display: grid; grid-template-columns: 1fr auto 36px 36px; gap: 6px; align-items: center; padding: 3px 0; border-bottom: 1px solid var(--line); }
.chrow.paid .chmain span, .chrow.paid .amt { color: var(--muted); }
.chmain { border: 0; background: transparent; padding: 4px 0; text-align: left; min-width: 0; display: flex; flex-direction: column; }
.chmain span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chmain small { font-size: .72rem; color: var(--muted); }
.chrow .amt { font-variant-numeric: tabular-nums; text-align: right; }
.mk { width: 36px; height: 36px; padding: 0; border-radius: 50%; color: var(--muted); font-weight: 700; }
.mk.mv { border-style: dashed; border-color: var(--unpaid-ink); color: var(--unpaid-ink); }
.mk.mv.on { background: var(--unpaid-bg); border: 1px solid var(--unpaid-bg); }
.mk.pdc.on { background: var(--paid-bg); color: var(--paid-ink); border-color: var(--paid-bg); }
.chform { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.chform label:not(.check) { display: flex; flex-direction: column; gap: 4px; font-size: .82rem; color: var(--muted); }
.chform input:not([type=checkbox]), .chform select { width: 100%; color: var(--ink); font-size: 1rem; }
.chform .full { grid-column: 1 / -1; }
.chform .hint { margin: -6px 0 0 30px; }
.chform p.err { margin: 0; min-height: 0; }
/* Receipt buttons and the image while editing; values read from it are highlighted until changed. */
.rcpt { display: flex; gap: 8px; }
.rcpt .btn { flex: 1; text-align: center; border: 1px solid var(--line); border-radius: 10px; padding: 10px 8px; cursor: pointer; color: var(--ink); font-size: .92rem; background: var(--bg); }
.chform p#rstat { margin: -4px 0 0; min-height: 0; }
.chform p#rstat:empty { display: none; }
.rimg { max-height: 220px; width: 100%; object-fit: contain; border-radius: 8px; border: 1px solid var(--line); }
.chform input.check { background: var(--unpaid-bg); color: var(--unpaid-ink); border-color: var(--unpaid-ink); }
@media (max-width: 400px) { .ctot { padding: 6px 7px; } .ctot b { font-size: .9rem; } .ctots { gap: 6px; } }

/* Insights tab: horizontal bars, Plan as a light bar and Real on top (insights.js). */
.ispan { margin: -4px 0 8px; font-size: .82rem; }
.ihint { margin: 0 0 6px; font-size: .8rem; }
.ihead { margin: 12px 0 0; }
.ichart { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.irow { display: flex; flex-direction: column; gap: 3px; width: 100%; text-align: left; padding: 8px 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; }
button.irow:hover { border-color: var(--brand); }
.irow-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; min-width: 0; }
.iname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.irow b, .irow-sub { font-variant-numeric: tabular-nums; }
.irow-sub { display: flex; justify-content: space-between; gap: 8px; font-size: .76rem; color: var(--muted); }
.ibar { display: block; width: 100%; height: 12px; border-radius: 3px; }
.ib-track { fill: var(--chart-track); }
.ib-plan { fill: var(--chart-plan); }
.ib-real { fill: var(--chart-real); }
.ib-real.over { fill: var(--chart-over); }
/* Spending per payday: bars, Plan line, income line; today's payday has a band behind it. */
.itrend { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; }
.ilegend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: .76rem; color: var(--muted); }
.ileg { display: inline-flex; align-items: center; gap: 5px; }
.ileg i { display: inline-block; width: 14px; height: 10px; border-radius: 2px; }
.ileg i.sw-real { background: var(--chart-real); }
.ileg i.sw-plan { height: 2px; background: var(--chart-line); }
.ileg i.sw-inc { height: 2px; background: var(--chart-income); }
.iscale { display: block; margin-top: 6px; font-size: .7rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.itsvg { display: block; width: 100%; height: 160px; border-bottom: 1px solid var(--line); }
.it-cur { fill: var(--chart-cur); }
.it-real { fill: var(--chart-real); }
.it-plan, .it-inc { fill: none; stroke-width: 2; stroke-linejoin: round; }
.it-plan { stroke: var(--chart-line); }
.it-inc { stroke: var(--chart-income); }
.im-tr { fill: var(--chart-income); }
.im-ot { fill: var(--chart-real); }
.ileg i.sw-ot { background: var(--chart-real); }
.ileg i.sw-tr { background: var(--chart-income); }
.imonths { display: grid; grid-template-columns: repeat(12, 1fr); text-align: center; font-size: .66rem; color: var(--muted); margin-top: 2px; }
.imonths .on { color: var(--ink); font-weight: 700; }
.ilines { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 2px 10px; margin-bottom: 8px; font-size: .88rem; }
.iline { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
.iline:first-child { border-top: 0; }
.iline > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iline > span small { display: block; font-size: .74rem; color: var(--muted); }
.iline b { font-variant-numeric: tabular-nums; white-space: nowrap; }
.iline b small { color: var(--muted); font-weight: 400; }
.iaxis { display: flex; justify-content: space-between; font-size: .7rem; color: var(--muted); margin-top: 2px; }
.icur { margin: 6px 0 0; font-size: .8rem; font-variant-numeric: tabular-nums; }
.ilist { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; font-size: .88rem; }
.ilist summary { cursor: pointer; font-weight: 600; }
.ilist div { display: flex; justify-content: space-between; gap: 8px; padding: 4px 0; border-top: 1px solid var(--line); }
.ilist div span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ilist b { font-variant-numeric: tabular-nums; }

button.danger { background: var(--bad); color: #fff; border-color: var(--bad); font-weight: 600; }

/* Phones: smaller cards so the pinned block leaves more room for rows. */
@media (max-width: 759px) {
  .lhead, .row { grid-template-columns: 1fr 82px 82px 32px; gap: 5px; }
  .phead.pinned .pbar { margin-bottom: 6px; }
  .phead.pinned .cards { gap: 6px; margin-bottom: 4px; }
  .phead.pinned .card { padding: 5px 8px; font-size: .76rem; }
  .phead.pinned .card h3 { margin-bottom: 2px; font-size: .7rem; }
}

@media (min-width: 760px) {
  .tabs { position: static; max-width: 760px; margin: 0 auto 6px; padding: 0 12px; border: 0; background: transparent; }
  .tabs button { border: 1px solid var(--line); }
  main { padding-bottom: 40px; }
  .selbar { bottom: 0; max-width: 736px; margin: 0 auto; border-radius: 10px 10px 0 0; }
  body.hassel main { padding-bottom: 90px; }
  .gridwrap { max-height: calc(100vh - 150px); }
}
