:root {
  --bg: #FAF9F5;
  --card: #FFFFFF;
  --soft: #F3F1EA;
  --line: #E8E5DA;
  --text: #1F1E1D;
  --muted: #77756E;
  --accent: #D97757;
  --accent-strong: #C6613F;
  --accent-soft: #F8E9E2;
  --on-accent: #FFFFFF;
  --danger: #B5483A;
  --kx: #D97757;
  --wy: #788C5D;
  --ww: #6A9BCC;
  --shadow: 0 1px 2px rgba(60, 50, 30, .04), 0 4px 14px rgba(60, 50, 30, .04);
  --serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --ease: cubic-bezier(.2, .7, .2, 1);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624;
    --card: #2F2F2D;
    --soft: #383836;
    --line: #42413D;
    --text: #F5F4EE;
    --muted: #A9A79E;
    --accent: #D97757;
    --accent-strong: #E38B6D;
    --accent-soft: #46322A;
    --danger: #E58A7B;
    --shadow: 0 1px 2px rgba(0, 0, 0, .18), 0 4px 14px rgba(0, 0, 0, .14);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
h1, h2 { font-family: var(--serif); font-weight: 500; margin: 0; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.0625rem; margin-bottom: 12px; }
p { margin: 0; }
.muted { color: var(--muted); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  transition: background .15s, border-color .15s, transform .08s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); width: 100%; }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.primary:disabled { opacity: .6; cursor: default; }
.btn.small { min-height: 32px; padding: 0 11px; font-size: .8125rem; border-radius: 8px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.danger { color: var(--danger); width: 100%; background: transparent; border-color: transparent; min-height: 38px; }
.btn.danger.armed { background: var(--danger); border-color: var(--danger); color: #fff; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: block; margin-bottom: 12px; }
.field > span, .label { display: block; font-size: .75rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
input[type="text"], input[type="password"], input[type="date"] {
  width: 100%; min-height: 44px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text);
  font: inherit; font-size: 16px; /* 16px stops iPhone zooming in */
  -webkit-appearance: none; appearance: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.amount-wrap { position: relative; }
.amount-wrap .prefix {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-weight: 600; pointer-events: none;
}
.amount-wrap input { padding-left: 42px; font-size: 1.25rem; font-weight: 600; min-height: 50px; font-variant-numeric: tabular-nums; }

.chips { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 12px; }
.chip {
  min-height: 40px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--muted);
  font: inherit; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  transition: background .15s, border-color .15s, color .15s, transform .08s;
}
.chip:active { transform: scale(.97); }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); transition: background .15s; }
.chip[aria-pressed="true"], .chip[aria-checked="true"] {
  background: var(--accent-soft); border-color: var(--accent); color: var(--text);
}
.chip[aria-pressed="true"] .dot, .chip[aria-checked="true"] .dot { background: var(--p, var(--accent)); }
.hint { font-size: .75rem; color: var(--muted); margin: -4px 0 12px; }
.error { color: var(--danger); font-size: .8125rem; margin-bottom: 10px; }
.form-actions { display: grid; gap: 4px; margin-top: 4px; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 340px; text-align: center; padding: 26px 20px 20px; animation: rise .45s var(--ease) both; }
.login-card .field { text-align: left; margin-top: 18px; }
.login-icon { border-radius: 12px; margin-bottom: 10px; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 16px; padding-top: calc(8px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 1rem; font-weight: 500; white-space: nowrap; }
.brand img { border-radius: 7px; }
.topbar-actions { display: flex; gap: 2px; }

.layout { max-width: 520px; margin: 0 auto; padding: 14px 16px calc(84px + env(safe-area-inset-bottom)); }
.tab { display: none; }
.tab.active { display: grid; gap: 12px; }
.right { display: contents; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: 0; background: none; color: var(--muted); font: inherit; font-size: .6875rem; font-weight: 600;
  padding: 8px 0 7px; display: grid; justify-items: center; gap: 1px; cursor: pointer;
  transition: color .15s;
}
.tabbar svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; transition: transform .2s var(--ease); }
.tabbar button.active { color: var(--accent-strong); }
.tabbar button.active svg { transform: translateY(-1px) scale(1.08); }

@media (min-width: 900px) {
  .layout { max-width: 960px; display: grid; grid-template-columns: 340px 1fr; gap: 20px; align-items: start; padding: 20px; }
  .tab, .tab.active { display: grid; gap: 12px; }
  #tab-add { position: sticky; top: 68px; }
  .right { display: grid; gap: 12px; min-width: 0; }
  #tab-bills .monthnav { display: none; }
  .tabbar { display: none; }
}

/* ---------- dashboard ---------- */
.monthnav { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.monthnav .title { font-family: var(--serif); font-size: 1.0625rem; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font-size: 1.125rem; line-height: 1; cursor: pointer;
  transition: background .15s, transform .08s;
}
.icon-btn:active { transform: scale(.94); }
.icon-btn:disabled { opacity: .35; cursor: default; }

.eyebrow { font-size: .6875rem; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.big { font-family: var(--serif); font-size: 1.875rem; line-height: 1.15; margin: 2px 0 1px; font-variant-numeric: tabular-nums; }
.rows { display: grid; gap: 10px; margin-top: 14px; }
.person-row { display: grid; grid-template-columns: 32px 1fr auto; align-items: center; gap: 10px; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center;
  background: var(--p); color: #fff; font-size: .6875rem; font-weight: 700;
}
.bar { height: 6px; border-radius: 99px; background: var(--soft); overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--p); transform-origin: left; animation: growX .6s var(--ease) both; }
.person-row .sub { font-size: .75rem; color: var(--muted); }
.person-row .amt { font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }

.rows.owe { gap: 6px; margin-top: 0; }
.owe-row {
  display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 10px;
  padding: 9px 10px 9px 12px; border-radius: 10px; background: var(--soft);
  animation: fade .35s ease both;
}
.owe-row .who { display: flex; align-items: center; gap: 5px; min-width: 0; }
.owe-row .arrow { color: var(--muted); }
.owe-row .amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tag { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px 2px 7px; border-radius: 99px; background: var(--card); border: 1px solid var(--line); font-size: .8125rem; font-weight: 600; }
.tag .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--p); }
.nets { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 12px; font-size: .75rem; color: var(--muted); }
.nets b { color: var(--text); font-variant-numeric: tabular-nums; }
.nets + .hint { margin: 4px 0 0; }
.empty { color: var(--muted); padding: 4px 0 2px; }

.trend { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; align-items: end; height: 116px; }
.trend .colwrap { display: grid; grid-template-rows: 1fr auto auto; height: 100%; gap: 3px; text-align: center; cursor: pointer; border: 0; background: none; padding: 0; font: inherit; color: inherit; }
.trend .track { display: flex; align-items: flex-end; justify-content: center; }
.trend .col { width: 100%; max-width: 34px; min-height: 3px; border-radius: 6px 6px 2px 2px; background: var(--line); transform-origin: bottom; animation: growY .6s var(--ease) both; transition: background .15s; }
.trend .colwrap:hover .col { background: var(--muted); }
.trend .current .col, .trend .current:hover .col { background: var(--accent); }
.trend .val { font-size: .625rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.trend .lab { font-size: .6875rem; font-weight: 600; color: var(--muted); }
.trend .current .lab { color: var(--text); }

/* ---------- bills list ---------- */
.list-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 2px; }
.list-head h2 { margin: 0; }
.list-head .muted { font-size: .75rem; }
.bill {
  display: grid; grid-template-columns: 1fr auto; gap: 1px 12px; width: 100%;
  padding: 10px 0; border: 0; border-top: 1px solid var(--line);
  background: none; color: inherit; font: inherit; text-align: left; cursor: pointer;
  animation: fade .35s ease both;
}
.day + .bill { border-top: 0; }
.bill:last-child { padding-bottom: 2px; }
.bill .desc { font-weight: 600; overflow-wrap: anywhere; transition: color .15s; }
.bill:hover .desc { color: var(--accent-strong); }
.bill .amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bill .meta { grid-column: 1 / -1; font-size: .75rem; color: var(--muted); }
.bill.settle .desc, .bill.settle:hover .desc { color: var(--wy); }
.bill .remove { grid-column: 1 / -1; justify-self: end; font-size: .6875rem; color: var(--danger); font-weight: 600; }

.bill .stamp { font-size: .6875rem; font-variant-numeric: tabular-nums; }

/* date heading for each day in the list */
.day {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px;
  margin-top: 12px; padding: 7px 12px 7px 10px; border-radius: 10px; background: var(--soft);
  animation: fade .35s ease both;
}
.list-head + .day { margin-top: 8px; }
.day-num { font-family: var(--serif); font-size: 1.5rem; line-height: 1; min-width: 30px; text-align: center; color: var(--accent-strong); font-variant-numeric: tabular-nums; }
.day-name { font-weight: 700; }
.day-sub { font-size: .75rem; color: var(--muted); }
.day-total { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* paid / share table on the dashboard */
.ptable { display: grid; gap: 9px; margin-top: 14px; }
.phead, .prow { display: grid; grid-template-columns: 1fr 96px 88px; align-items: center; gap: 8px; }
.phead { font-size: .6875rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.phead span:not(:first-child), .prow .amt { text-align: right; }
.pname { display: grid; grid-template-columns: 32px 1fr; align-items: center; gap: 10px; min-width: 0; }
.pname .bar { margin-top: 0; }
.prow .amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.prow .amt.dim { font-weight: 500; color: var(--muted); }

/* ---------- dialogs & toast ---------- */
dialog {
  width: min(100% - 24px, 400px); max-height: calc(100dvh - 24px);
  border: 1px solid var(--line); border-radius: 16px; padding: 16px;
  background: var(--card); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .22);
}
dialog[open] { animation: pop .28s var(--ease) both; }
dialog::backdrop { background: rgba(30, 25, 20, .42); animation: fade .25s ease both; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.dialog-head h2 { margin: 0; }
.dialog-head .icon-btn { border: 0; background: var(--soft); width: 32px; height: 32px; }
.settle-who { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; font-weight: 600; }

.toast {
  position: fixed; left: 50%; bottom: calc(74px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 99px;
  font-size: .8125rem; font-weight: 600; z-index: 20; box-shadow: var(--shadow); white-space: nowrap;
  animation: toastIn .3s var(--ease) both;
}
@media (min-width: 900px) { .toast { bottom: 24px; } }

/* ---------- fade-in animation ---------- */
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.97); } }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } }
@keyframes growX { from { transform: scaleX(0); } }
@keyframes growY { from { transform: scaleY(0); } }

/* Cards fade up one after another when a screen opens */
.tab.active > .card, .tab.active > .monthnav,
.right > .tab > .card, .right > .tab > .monthnav { animation: rise .45s var(--ease) both; }
.tab > :nth-child(2) { animation-delay: .06s; }
.tab > :nth-child(3) { animation-delay: .12s; }
.tab > :nth-child(4) { animation-delay: .18s; }
@media (min-width: 900px) {
  #tab-add > .card { animation: rise .45s var(--ease) both; }
  #tab-bills > .card { animation-delay: .24s; }
}
.bill:nth-child(3) { animation-delay: .03s; }
.bill:nth-child(4) { animation-delay: .06s; }
.bill:nth-child(5) { animation-delay: .09s; }
.bill:nth-child(n+6) { animation-delay: .12s; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
