:root {
  --bg: #f6f3ee;
  --surface: #fffdf9;
  --surface-2: #f0ece4;
  --ink: #1f1d1a;
  --ink-2: #5d5850;
  --ink-3: #8a847a;
  --line: #e3ddd2;
  --accent: #2f5d50;
  --accent-ink: #ffffff;
  --accent-soft: #e2ece7;
  --owes: #9a4a1f;
  --owes-soft: #f6e6db;
  --credit: #2f5d50;
  --credit-soft: #e2ece7;
  --danger: #a33a2c;
  --radius: 14px;
  --shadow: 0 1px 0 rgba(31, 29, 26, .04), 0 6px 24px -12px rgba(31, 29, 26, .18);
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171513;
    --surface: #211e1b;
    --surface-2: #2a2622;
    --ink: #f1ece4;
    --ink-2: #bdb5a9;
    --ink-3: #8c857a;
    --line: #36312b;
    --accent: #7fb7a3;
    --accent-ink: #10201b;
    --accent-soft: #233630;
    --owes: #e9a77f;
    --owes-soft: #3a2a20;
    --credit: #7fb7a3;
    --credit-soft: #233630;
    --danger: #e98a7c;
    --shadow: 0 1px 0 rgba(0,0,0,.3), 0 8px 28px -14px rgba(0,0,0,.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
}
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-family: var(--serif); font-size: 28px; }
h2 { font-size: 16px; }
h3 { font-size: 13px; margin: 18px 0 8px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); }
h3 .muted { text-transform: none; letter-spacing: 0; font-weight: 400; }
p { margin: 0; }
.muted { color: var(--ink-2); }
.small-text { font-size: 13px; margin-bottom: 8px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.wrap { max-width: 1040px; margin: 0 auto; padding: 0 16px 64px; }
.loading, .empty { padding: 48px 0; text-align: center; color: var(--ink-2); }

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px max(16px, calc((100vw - 1040px) / 2 + 16px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand-name { font-family: var(--serif); font-size: 19px; }
.brand-mark { width: 26px; height: 26px; border-radius: 8px; background: url('/icon.svg') center / contain no-repeat; }
.brand-mark.big { width: 48px; height: 48px; border-radius: 12px; }
.topbar-actions { display: flex; align-items: center; gap: 4px; }
.save-status { font-size: 12px; color: var(--ink-3); margin-right: 6px; white-space: nowrap; }
.save-status[data-kind="warn"] { color: var(--owes); }

/* ---- buttons ---- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 500; padding: 9px 14px; border-radius: 10px; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px; line-height: 1.2;
}
.btn:hover { border-color: var(--ink-3); }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .month-tab:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); padding: 8px 10px; }
.btn.ghost:hover { color: var(--ink); background: var(--surface-2); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.block { width: 100%; justify-content: center; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--ink-3);
  width: 28px; height: 28px; border-radius: 8px; font-size: 18px; line-height: 1; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--danger); }

/* ---- inputs ---- */
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; min-width: 0;
}
input:hover, select:hover, textarea:hover { border-color: var(--ink-3); }
input.text { width: 100%; }
input.num { width: 100%; text-align: right; }
input.tiny { width: 58px; padding: 5px 6px; text-align: right; }
input[type="date"] { padding: 6px 8px; }
textarea { width: 100%; resize: vertical; }
select { padding-right: 26px; }
.money-input { position: relative; display: inline-flex; align-items: center; width: 100%; }
.money-input > span { position: absolute; left: 9px; color: var(--ink-3); pointer-events: none; }
.money-input input { padding-left: 20px; }
.mini { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--ink-2); margin-top: 6px; flex-wrap: wrap; }
.mini input[type="date"] { font-size: 12px; padding: 3px 6px; }
.grow { flex: 1 1 160px; }

/* ---- hero ---- */
.hero {
  margin: 20px 0 16px; padding: 22px 24px; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--line);
  display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  border-left: 6px solid var(--ink-3);
}
.hero[data-state="owes"] { border-left-color: var(--owes); }
.hero[data-state="credit"], .hero[data-state="even"] { border-left-color: var(--credit); }
.hero-who { font-weight: 600; color: var(--ink-2); }
.hero-amount { font-family: var(--serif); font-size: clamp(40px, 9vw, 56px); line-height: 1.05; font-weight: 600; letter-spacing: -.02em; }
.hero[data-state="owes"] .hero-amount { color: var(--owes); }
.hero[data-state="credit"] .hero-amount { color: var(--credit); }
.hero-sub { font-size: 13px; margin-top: 4px; }
.hero-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---- month tabs ---- */
.months {
  display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 10px; margin: 0 -2px 8px;
  scrollbar-width: thin; scroll-snap-type: x proximity;
}
.month-tab {
  appearance: none; flex: 0 0 auto; scroll-snap-align: start;
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 9px 14px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit;
}
.month-tab:hover { border-color: var(--ink-3); }
.month-tab[aria-current="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.month-tab[aria-current="true"] .month-bal { color: inherit; opacity: .75; }
.month-tab.add { border-style: dashed; background: transparent; color: var(--accent); }
.month-name { font-weight: 600; font-size: 14px; white-space: nowrap; }
.month-bal { font-size: 12px; color: var(--ink-2); }

/* ---- period ---- */
.period-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin: 14px 0 14px; }
.title-input {
  font: inherit; font-family: var(--serif); font-size: 28px; font-weight: 600; letter-spacing: -.01em;
  border-color: transparent; background: transparent; padding: 2px 6px; margin-left: -7px; width: min(100%, 30ch);
}
.title-input:hover { border-color: var(--line); }
.dates { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; color: var(--ink-2); margin-top: 4px; }
.dates input { border-color: transparent; background: transparent; color: var(--ink-2); }
.dates input:hover { border-color: var(--line); }
.badge { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 99px; background: var(--owes-soft); color: var(--owes); }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px 18px 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head p { font-size: 13px; }

/* charge table */
.table { display: flex; flex-direction: column; }
.row {
  display: grid; grid-template-columns: minmax(150px, 1.5fr) 118px minmax(160px, 1.3fr) 96px 96px 30px;
  gap: 10px; align-items: start; padding: 10px 0; border-top: 1px solid var(--line);
}
.row.head { border-top: 0; padding: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); font-weight: 600; }
.row.head > div:nth-child(n+4) { text-align: right; }
.row.total { font-weight: 600; align-items: center; border-top: 2px solid var(--line); }
.cell { min-width: 0; }
.cell.item { display: flex; flex-direction: column; align-items: flex-start; }
.cell.item input.text { align-self: stretch; }
.cell.share { text-align: right; padding-top: 8px; }
.row.total .cell.share, .row.total .cell.amt { padding-top: 0; text-align: right; }
.cell.share.b { color: var(--owes); font-weight: 600; }
.cell.split select { width: 100%; }
.cell.split .frac { display: block; font-size: 12px; margin-top: 4px; }
.cell.x { padding-top: 4px; }
.cell-label { display: none; }

.two-col { display: grid; grid-template-columns: 1fr 1.25fr; gap: 14px; align-items: start; }
.two-col .card { margin-bottom: 14px; }

.kv { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 7px 0; border-top: 1px solid var(--line); }
.kv:first-of-type { border-top: 0; }
.kv .money-input { width: 140px; }
.kv strong { font-weight: 600; }
.kv.sub { margin-top: 14px; }
.kv.grand { border-top: 2px solid var(--ink); margin-top: 4px; padding-top: 10px; font-weight: 600; }
.kv.grand strong { font-family: var(--serif); font-size: 24px; color: var(--owes); }

.pill { margin-top: 12px; display: inline-block; padding: 5px 10px; border-radius: 99px; font-size: 13px; font-weight: 600; background: var(--surface-2); color: var(--ink-2); }
.pill[data-state="even"] { background: var(--credit-soft); color: var(--credit); }
.pill[data-state="credit"] { background: var(--accent-soft); color: var(--accent); }
.pill[data-state="owes"] { background: var(--owes-soft); color: var(--owes); }

.line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 6px 0; }
.line .money-input { width: 120px; }
.line.room .text { width: 120px; flex: 0 0 auto; }
.line .dims { display: inline-flex; align-items: center; gap: 4px; color: var(--ink-3); flex-wrap: wrap; }
.line .sqft { font-size: 13px; min-width: 80px; }
.balance h3 + .btn, .balance .line + .btn { margin-top: 2px; }

.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-2); }

.danger-zone { text-align: right; margin-top: 4px; }

/* percentages */
.pct-pair { display: inline-flex; gap: 10px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--ink-2); }
.pct-pair label { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.cell.split .pct-pair { margin-top: 6px; gap: 8px; flex-wrap: nowrap; }
.cell.split input.pct { width: 64px; }
input.pct { width: 86px; padding: 4px 6px; font-size: 13px; text-align: right; }
input.pct:disabled { opacity: .6; background: var(--surface-2); }
.room-split {
  display: flex; align-items: center; gap: 8px 14px; flex-wrap: wrap;
  padding: 10px 12px; margin: -2px 0 12px; border-radius: 10px; background: var(--surface-2); font-size: 14px;
}
.room-split > span:first-child { font-weight: 600; }
.room-split .small-text { margin: 0; }
.check-label { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.check-label input { width: 18px; height: 18px; accent-color: var(--accent); }
.working { font-size: 13px; margin-top: 10px; }

/* show the math */
.math { display: none; margin: 6px 0 0; font-size: 12.5px; color: var(--accent); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; line-height: 1.5; }
.show-math .math { display: block; }
.row-math { grid-column: 1 / -1; margin: 0; padding: 6px 10px; border-radius: 8px; background: var(--accent-soft); }

/* period totals */
.period-totals { display: flex; gap: 22px; }
.period-totals > div { display: flex; flex-direction: column; text-align: right; }
.period-totals strong { font-family: var(--serif); font-size: 26px; }
.period-totals strong.b { color: var(--owes); }

.kv.sum { border-top: 1px solid var(--ink-3); font-weight: 600; }
.kv.grand.a strong { color: var(--accent); }
.balance .pill { margin: 4px 0 8px; }

/* check the math */
.verify details > summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.verify details > summary::-webkit-details-marker { display: none; }
.verify-title { display: flex; flex-direction: column; gap: 2px; }
.verify-title h2::after { content: ' ▸'; color: var(--ink-3); font-weight: 400; }
.verify details[open] .verify-title h2::after { content: ' ▾'; }
.verify .pill { margin: 0; }
.checks { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-direction: column; }
.check { display: grid; grid-template-columns: 26px 1fr; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); align-items: start; }
.check-mark {
  width: 22px; height: 22px; border-radius: 99px; display: grid; place-items: center; font-size: 13px; font-weight: 700;
  background: var(--credit-soft); color: var(--credit);
}
.check[data-state="bad"] .check-mark { background: var(--owes-soft); color: var(--owes); }
.check strong { display: block; font-size: 14px; }
.check-detail { display: block; font-size: 13px; color: var(--ink-2); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; margin-top: 2px; word-break: break-word; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 11px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,.5); max-width: calc(100vw - 32px); z-index: 20;
}

/* login */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 18px;
  padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px; align-items: stretch; text-align: center;
}
.login-card .brand-mark { align-self: center; }
.login-card h1 { font-size: 26px; }
.login-card input { padding: 11px 12px; font-size: 16px; }
.error { color: var(--danger); min-height: 1.2em; font-size: 14px; }

/* ---- small screens ---- */
@media (max-width: 760px) {
  body { font-size: 16px; }
  .topbar .btn.ghost { padding: 7px 8px; font-size: 14px; }
  .hero { padding: 18px; }
  .hero-actions { width: 100%; }
  .hero-actions .btn { flex: 1; justify-content: center; }
  .two-col { grid-template-columns: 1fr; gap: 0; }
  .form-grid { grid-template-columns: 1fr; }
  .period-total { text-align: left; }

  .row.head { display: none; }
  .row {
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "item item" "amt amt" "split split" "a b" "math math";
    gap: 8px 12px; padding: 14px 0; position: relative;
  }
  .row .cell.item { grid-area: item; padding-right: 36px; }
  .row .cell.amt { grid-area: amt; }
  .row .cell.split { grid-area: split; }
  .row .cell.share { grid-area: a; text-align: left; padding-top: 0; display: flex; justify-content: space-between; background: var(--surface-2); padding: 6px 10px; border-radius: 8px; }
  .row .cell.share.b { grid-area: b; }
  .row .cell.x { position: absolute; top: 14px; right: 0; padding-top: 4px; }
  .row .row-math { grid-area: math; }
  .period-totals > div { text-align: left; }
  .row.total { grid-template-areas: "item item" "amt amt" "a b"; }
  .row.total .cell.split, .row.total .cell.x { display: none; }
  .row.total .cell.amt { display: flex; justify-content: space-between; }
  .row.total .cell.share { text-align: left; }
  .cell-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); font-weight: 600; margin-bottom: 3px; }
  .cell.share .cell-label, .row.total .cell-label { margin: 0; align-self: center; }
  .line .money-input { width: 110px; }
  .line.room .text { width: 100%; }
  input, select, textarea { font-size: 16px; } /* stop iOS zoom */
  input.tiny { width: 70px; }
}
