:root {
  --bg: #f4f3ef; --card: #ffffff; --text: #1d1d1b; --muted: #6b6a65; --line: #e2e0d8;
  --accent: #1f6f5c; --accent-soft: #e1f2ec; --on-accent: #ffffff;
  --warn: #8a5a00; --warn-bg: #fdf1d8; --warn-fill: #d99a1e;
  --danger: #a32d2d; --danger-bg: #fcebeb; --pos: #2f6b12;
  --radius: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615; --card: #22221f; --text: #ecebe6; --muted: #a3a29b; --line: #3a3a36;
    --accent: #4fbf9f; --accent-soft: #163b31; --on-accent: #0f1f1a;
    --warn: #f3c46b; --warn-bg: #3d2d0c; --warn-fill: #e0a63a;
    --danger: #f19a9a; --danger-bg: #3f1717; --pos: #98d07a;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
h1 { font-size: 22px; font-weight: 600; margin: 0 0 12px; }
h2 { font-size: 17px; font-weight: 600; margin: 0 0 4px; }
.muted { color: var(--muted); } .small { font-size: 13px; }

.top { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; }
.brand { font-weight: 600; } .brand small { color: var(--muted); font-weight: 400; }
.who { font-size: 13px; color: var(--muted); text-decoration: none; }

main { max-width: 560px; margin: 0 auto; padding: 0 16px 16px; }
main.wide { max-width: 1100px; }
main.center { min-height: 70vh; display: flex; align-items: center; justify-content: center; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin-bottom: 14px; }
.card.scroll { overflow-x: auto; }
.login { max-width: 360px; text-align: center; }

label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 4px; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; margin-top: 8px; }
input, select {
  width: 100%; height: 44px; padding: 0 12px; font: inherit; color: var(--text);
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
}
input[type=checkbox] { width: 18px; height: 18px; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.amount-row { position: relative; }
.amount-row .dollar { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); font-size: 24px; color: var(--muted); }
.amount-row input { height: 58px; font-size: 28px; padding-left: 34px; font-variant-numeric: tabular-nums; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 44px; padding: 0 16px; font: inherit; font-weight: 500; color: var(--text);
  background: transparent; border: 1px solid var(--line); border-radius: var(--radius);
  text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary.warn { background: var(--warn-fill); border-color: var(--warn-fill); color: #241800; }
.btn.wide { width: 100%; margin-top: 10px; }
.btn:disabled { opacity: .6; }
.small-btn { height: 34px; padding: 0 10px; font-size: 13px; }

.meter { background: var(--bg); border-radius: var(--radius); padding: 12px; margin-top: 16px; }
.meter-head { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.bar { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin: 8px 0 4px; }
.bar > div { height: 100%; width: 0; background: var(--accent); border-radius: 4px; transition: width .2s; }
.bar > div.over { background: var(--warn-fill); }
.over-text { color: var(--warn); }

.alert { border-radius: var(--radius); padding: 10px 12px; font-size: 14px; margin: 12px 0 0; }
.alert.warning { background: var(--warn-bg); color: var(--warn); }
.alert.danger { background: var(--danger-bg); color: var(--danger); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
}

.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
@media (min-width: 720px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.stat span { display: block; font-size: 13px; color: var(--muted); }
.stat b { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat.neg b, td.neg { color: var(--danger); } .stat.pos b { color: var(--pos); }

.month-nav { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: 8px; margin-bottom: 12px; }
.month-nav h1 { margin: 0; text-align: center; font-size: 20px; }
.month-nav .btn { padding: 0; font-size: 22px; }
.pill { display: inline-block; font-size: 12px; font-weight: 500; padding: 2px 8px; border-radius: 999px; vertical-align: middle; margin-left: 6px; }
.pill.closed { background: var(--line); color: var(--muted); }
.pill.open { background: var(--accent-soft); color: var(--accent); }
.pill.future { background: var(--warn-bg); color: var(--warn); }

.vrow { padding: 10px 0; border-top: 1px solid var(--line); }
.vrow:first-of-type { border-top: 0; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list td { padding: 8px 0; border-top: 1px solid var(--line); }
table.list tr:first-child td { border-top: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.txlist { list-style: none; margin: 0; padding: 0; }
.txlist li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.txlist li:first-child { border-top: 0; }

table.grid { border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; width: 100%; }
table.grid th, table.grid td { padding: 8px 10px; text-align: right; border-top: 1px solid var(--line); }
table.grid thead th { border-top: 0; color: var(--muted); font-weight: 500; }
table.grid th:first-child { text-align: left; position: sticky; left: 0; background: var(--card); }
table.grid tr.strong td, table.grid tr.strong th { font-weight: 600; }
table.plan td, table.plan th { padding: 4px; }
table.plan input { height: 36px; min-width: 76px; text-align: right; padding: 0 8px; }
table.plan input.name { min-width: 170px; text-align: left; }
table.plan select { height: 36px; min-width: 124px; }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; }
.dot.closed { background: var(--muted); } .dot.open { background: var(--accent); } .dot.future { background: var(--warn-fill); }
.legend { display: flex; gap: 6px 14px; flex-wrap: wrap; align-items: center; }
.legend .dot { margin-left: 8px; } .legend .dot:first-child { margin-left: 0; }

.sticky-save {
  position: sticky; bottom: calc(68px + env(safe-area-inset-bottom)); display: flex; justify-content: flex-end;
  align-items: center; gap: 12px; padding: 10px 0; background: var(--bg);
}

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs a { display: flex; flex-direction: column; align-items: center; padding: 8px 0 10px; font-size: 12px; color: var(--muted); text-decoration: none; }
.tabs a .ico { font-size: 20px; line-height: 1.2; }
.tabs a.on { color: var(--accent); font-weight: 600; }

/* ---------- v2 additions ---------- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.top { gap: 10px; }
.top .brand { margin-right: auto; }
.yearpick select { height: 34px; width: auto; padding: 0 28px 0 10px; font-size: 14px; font-weight: 600; }
.btn.ghost { border-color: transparent; }
.chip { height: 30px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--card); color: var(--text); font: inherit; font-size: 13px; cursor: pointer; }

/* iOS Safari: date inputs ignore width and overflow their column */
.two > div { min-width: 0; }
input[type="date"] { -webkit-appearance: none; appearance: none; min-width: 0; max-width: 100%; display: block; text-align: left; }
input[type="date"]::-webkit-date-and-time-value { text-align: left; }

/* Month: tappable variable lines + purchase filter */
button.vrow { display: block; width: 100%; text-align: left; background: none; border: 0; border-top: 1px solid var(--line); color: inherit; font: inherit; padding: 10px 6px; cursor: pointer; border-radius: 0; }
button.vrow:first-of-type { border-top: 0; }
button.vrow.sel { background: var(--accent-soft); border-radius: 8px; }
.chev { color: var(--muted); }
.plan-note { font-style: italic; margin-top: 2px; }
.purch-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.txlist li[hidden] { display: none; }

/* Year: collapsible breakdown */
.table-tools { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin: 0 0 8px; flex-wrap: wrap; }
.yeargrid tbody.group tr.group-head th, .yeargrid tbody.group tr.group-head td { font-weight: 600; }
.expander { background: none; border: 0; padding: 0; font: inherit; font-weight: 600; color: var(--accent); cursor: pointer; display: inline-flex; gap: 6px; align-items: center; }
.caret { width: 12px; display: inline-block; }
.yeargrid tr.sub th { font-weight: 400; padding-left: 28px; color: var(--muted); }
.yeargrid tr.sub td { color: var(--muted); font-size: 13px; }
.yeargrid tr.sub[hidden] { display: none; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
td.has-note { position: relative; cursor: help; }
td.has-note::after, .note-mark::after { content: ""; position: absolute; top: 0; right: 0; border-style: solid; border-width: 0 7px 7px 0; border-color: transparent var(--warn-fill) transparent transparent; }
.note-mark { position: relative; display: inline-block; width: 12px; height: 12px; border: 1px solid var(--line); margin-left: 8px; }

/* Plan page: full width, no bottom tabs, toolbar on top */
body.plan-page { padding-bottom: 24px; }
main.full { max-width: none; padding: 0 12px 24px; }
.plan-bar { position: sticky; top: 0; z-index: 10; background: var(--bg); border-bottom: 1px solid var(--line); padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); }
.plan-bar-row { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; }
.plan-bar-row .spacer { flex: 1; }
.plan-bar .btn { height: 38px; padding: 0 12px; white-space: nowrap; }
.note-row { margin-top: 6px; }
.note-label { margin: 0; white-space: nowrap; max-width: 45%; overflow: hidden; text-overflow: ellipsis; font-size: 13px; color: var(--muted); }
#noteInput { height: 36px; font-size: 14px; }
#planMsg { white-space: nowrap; }
.btn.pulse { box-shadow: 0 0 0 3px var(--accent-soft); }
.rotate-hint { display: none; margin: 10px 0 0; font-size: 13px; color: var(--muted); text-align: center; }
@media (orientation: portrait) and (max-width: 600px) { .rotate-hint { display: block; } }
.plan-card { margin-top: 12px; padding: 12px; }
table.plan { font-size: 13px; }
table.plan th, table.plan td { padding: 3px 3px; }
table.plan thead th { font-size: 12px; }
table.plan input, table.plan select { height: 32px; font-size: 13px; border-radius: 6px; }
table.plan input.amt { width: 68px; min-width: 68px; text-align: right; padding: 0 6px; }
table.plan input.amt.cur { outline: 2px solid var(--accent); outline-offset: 0; }
table.plan input.amt.has-note { background-image: linear-gradient(225deg, var(--warn-fill) 7px, transparent 7px); }
table.plan select.kind { width: 92px; min-width: 92px; padding: 0 4px; }
table.plan select.type { width: 104px; min-width: 104px; padding: 0 4px; }
table.plan .namecell { display: flex; align-items: center; gap: 4px; }
table.plan input.name { width: 150px; min-width: 150px; text-align: left; padding: 0 8px; font-weight: 500; }
table.plan th:first-child { z-index: 2; }
table.plan tfoot td, table.plan tfoot th { font-weight: 600; border-top: 2px solid var(--line); }
.icon-btn { width: 26px; height: 26px; flex: none; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-size: 13px; cursor: pointer; }
.icon-btn.del:hover, .icon-btn.del:focus { background: var(--danger-bg); color: var(--danger); }
tr.flash td, tr.flash th { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: var(--accent-soft); } to { background: transparent; } }
.add-line { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.add-line input { flex: 1 1 180px; height: 38px; width: auto; }
.add-line select { flex: 0 0 auto; width: auto; height: 38px; }
.add-line .btn { height: 38px; }
dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--text); padding: 20px; width: min(92vw, 380px); }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
@media (max-width: 600px) {
  .plan-bar #newYearBtn { padding: 0 8px; }
  #planMsg { display: none; }
}

[hidden] { display: none !important; }
.note-row { flex-wrap: wrap; }
@media (max-width: 600px) {
  .plan-bar #newYearBtn .lbl { display: none; }
  .plan-bar .btn { padding: 0 10px; }
  .note-label { flex-basis: 100%; max-width: 100%; }
}
.lbl-short { display: none; }
@media (max-width: 600px) { .plan-bar #newYearBtn .lbl-short { display: inline; } }
