:root {
  --bg: #f4f1ea;
  --surface: #fffdf9;
  --sunken: #ebe7de;
  --ink: #18211d;
  --muted: #66706a;
  --line: #e2ddd2;
  --accent: #145c3c;
  --accent-soft: #dcebe2;
  --on-accent: #ffffff;
  --pos: #13704a;
  --neg: #b03a2e;
  --neg-soft: #f6e1dd;
  --shadow: 0 1px 2px rgba(24, 33, 29, .06), 0 8px 24px rgba(24, 33, 29, .08);
  --radius: 14px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tabs-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121715;
    --surface: #1a211e;
    --sunken: #222a27;
    --ink: #eef1ec;
    --muted: #98a29c;
    --line: #2c3531;
    --accent: #4fb585;
    --accent-soft: #1f3a2d;
    --on-accent: #0c1712;
    --pos: #5cc493;
    --neg: #ee8a7d;
    --neg-soft: #3a2422;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; }

#app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) 16px calc(var(--tabs-h) + 96px + env(safe-area-inset-bottom));
}
#app.auth { padding-bottom: 48px; display: flex; flex-direction: column; justify-content: center; }

/* ---------- Auth screens ---------- */
.auth-card { padding: 8px 4px; }
.wordmark { font: 600 13px/1 var(--sans); letter-spacing: .16em; text-transform: uppercase; color: var(--accent); }
.auth-card h1 { font: 500 40px/1.05 var(--serif); margin: 10px 0 8px; letter-spacing: -.01em; }
.auth-card p.lead { color: var(--muted); margin: 0 0 28px; }
.auth-card form { display: grid; gap: 14px; }
.linkish { background: none; border: 0; padding: 6px 0; color: var(--accent); font-weight: 600; text-align: left; }
.back { background: none; border: 0; padding: 8px 0; color: var(--muted); margin-bottom: 18px; }
.people-pick { display: grid; gap: 10px; margin-bottom: 18px; }
.person-btn {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 18px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface);
  font-size: 18px; font-weight: 600; text-align: left;
}
.person-btn small { font-size: 13px; font-weight: 500; color: var(--muted); }
.person-btn:active { transform: scale(.99); }

/* ---------- Form controls ---------- */
.field { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
.hint { font-size: 13px; font-weight: 400; color: var(--muted); }
input[type=text], input[type=password], input[type=date], input[type=tel], input:not([type]), select, textarea {
  width: 100%;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 17px;
  color: var(--ink);
  outline: none;
  appearance: none;
  -webkit-appearance: none;
}
textarea { resize: vertical; min-height: 120px; }
select { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 20px) 55%, calc(100% - 14px) 55%; background-size: 6px 6px; background-repeat: no-repeat; padding-right: 38px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.pin-input { font-size: 28px !important; letter-spacing: .5em; text-align: center; font-variant-numeric: tabular-nums; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 14px 18px; border-radius: 12px; border: 1px solid transparent;
  background: var(--accent); color: var(--on-accent);
  font-weight: 650; font-size: 16px;
}
.btn:disabled { opacity: .55; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.soft { background: var(--accent-soft); color: var(--accent); }
.btn.danger { background: var(--neg); color: #fff; }
.btn.small { padding: 8px 12px; font-size: 14px; border-radius: 10px; }
.btn:active { transform: translateY(1px); }

.error { color: var(--neg); background: var(--neg-soft); padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.error:empty { display: none; }

/* ---------- Main layout ---------- */
.top { display: flex; justify-content: space-between; align-items: flex-end; padding: 20px 2px 14px; }
.top h1 { margin: 4px 0 0; font: 500 32px/1.05 var(--serif); letter-spacing: -.01em; }
.me-chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 7px 14px; font-size: 14px; font-weight: 600;
}

.summary {
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px;
  padding: 18px 18px 16px; margin-bottom: 22px;
}
.summary .label { font-size: 14px; color: var(--muted); }
.summary .big { font: 500 36px/1.1 var(--serif); margin: 2px 0 10px; }
.summary .big.pos { color: var(--pos); }
.summary .big.neg { color: var(--neg); }
.summary .meta { display: flex; gap: 18px; font-size: 14px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 10px; }
.summary .meta b { color: var(--ink); font-weight: 650; }

h2.section { font-size: 13px; font-weight: 650; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 26px 2px 10px; }
h2.section:first-child { margin-top: 4px; }

.day { font-size: 13px; font-weight: 650; color: var(--muted); margin: 20px 2px 8px; }
.day:first-child { margin-top: 0; }
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; padding: 14px 16px; border: 0; background: none; text-align: left;
  border-top: 1px solid var(--line);
}
.list > .row:first-child { border-top: 0; }
button.row:active { background: var(--sunken); }
.row-main { min-width: 0; }
.row-title { font-weight: 620; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 13.5px; color: var(--muted); }
.row-side { text-align: right; flex-shrink: 0; }
.row-amount { font-weight: 650; font-variant-numeric: tabular-nums; }
.row-mine { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--muted); }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.tag { display: inline-block; font-size: 11.5px; font-weight: 650; padding: 1px 7px; border-radius: 999px; background: var(--sunken); color: var(--muted); vertical-align: 1px; margin-left: 4px; }
.row.payment .row-title { font-weight: 560; }

.empty { text-align: center; color: var(--muted); padding: 40px 12px; }
.empty b { display: block; color: var(--ink); font: 500 22px/1.2 var(--serif); margin-bottom: 6px; }

/* Balances */
.bal { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(56px, 1fr) auto; align-items: center; gap: 12px; padding: 13px 16px; border-top: 1px solid var(--line); }
.list > .bal:first-child { border-top: 0; }
.bal .name { font-weight: 620; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { position: relative; height: 8px; background: var(--sunken); border-radius: 4px; }
.bar::after { content: ""; position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--line); }
.bar span { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.bar span.pos { background: var(--pos); left: 50%; }
.bar span.neg { background: var(--neg); right: 50%; }
.bal .val { text-align: right; font-size: 14px; font-variant-numeric: tabular-nums; min-width: 92px; }
.bal .val small { display: block; color: var(--muted); font-size: 12px; }
.plan-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 13px 16px; border-top: 1px solid var(--line); }
.list > .plan-row:first-child { border-top: 0; }
.plan-row.mine { background: var(--accent-soft); }
.plan-row .who { font-weight: 560; }
.plan-row .who b { font-variant-numeric: tabular-nums; }

/* Trip tab */
.invite { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.invite code { display: block; font-size: 14px; word-break: break-all; background: var(--sunken); padding: 10px 12px; border-radius: 10px; margin: 8px 0 12px; }
.invite .btns { display: flex; gap: 8px; flex-wrap: wrap; }
.add-person { display: flex; gap: 8px; margin-top: 10px; }
.add-person input { flex: 1; }
.activity { list-style: none; padding: 0; margin: 0; }
.activity li { padding: 10px 16px; border-top: 1px solid var(--line); font-size: 14px; }
.activity li:first-child { border-top: 0; }
.activity time { display: block; color: var(--muted); font-size: 12px; }
.footer-actions { display: grid; gap: 10px; margin-top: 26px; }

/* Bottom tabs + add button */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs .inner { display: flex; width: 100%; max-width: 520px; }
.tabs button {
  flex: 1; height: var(--tabs-h); border: 0; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 12px; font-weight: 600; color: var(--muted);
}
.tabs button svg { width: 22px; height: 22px; }
.tabs button[aria-current="page"] { color: var(--accent); }
.fab {
  position: fixed; z-index: 21;
  right: max(16px, calc(50vw - 260px + 16px));
  bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom));
  border: 0; border-radius: 999px; padding: 15px 22px 15px 18px;
  background: var(--accent); color: var(--on-accent); font-weight: 700; font-size: 16px;
  box-shadow: var(--shadow); display: flex; align-items: center; gap: 8px;
}
.fab svg { width: 20px; height: 20px; }

/* ---------- Sheets ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 14, 12, .42);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .16s ease-out;
}
.sheet {
  width: 100%; max-width: 560px; max-height: 94vh; max-height: 94dvh; overflow-y: auto;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding: 0 18px calc(24px + env(safe-area-inset-bottom));
  animation: up .22s cubic-bezier(.2, .8, .3, 1);
  overscroll-behavior: contain;
}
.sheet-head {
  position: sticky; top: 0; z-index: 2; background: var(--bg);
  display: flex; align-items: center; justify-content: space-between; padding: 16px 0 12px;
}
.sheet-head h3 { margin: 0; font: 500 22px/1.2 var(--serif); }
.icon-btn { width: 38px; height: 38px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); display: grid; place-items: center; }
.icon-btn svg { width: 18px; height: 18px; }
.sheet form, .sheet .stack { display: grid; gap: 16px; }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }

.amount-field { display: flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 6px 16px; }
.amount-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.amount-field span { font: 500 34px/1 var(--serif); color: var(--muted); }
.amount-field input { border: 0 !important; box-shadow: none !important; padding: 8px 0 !important; background: none !important; font: 500 34px/1.1 var(--serif) !important; font-variant-numeric: tabular-nums; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; font-size: 15px; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--sunken); border-radius: 12px; padding: 3px; }
.seg button { border: 0; background: none; padding: 9px 4px; border-radius: 9px; font-weight: 600; font-size: 14.5px; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

.split { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.split-row { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-top: 1px solid var(--line); min-height: 56px; }
.split-row:first-child { border-top: 0; }
.split-row .nm { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.split-row .calc { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; min-width: 72px; text-align: right; }
.split-row.off .nm, .split-row.off .calc { opacity: .45; }
.split-row input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); margin: 0; }
.split-row .money-in { width: 110px; padding: 9px 10px; text-align: right; font-variant-numeric: tabular-nums; }
.stepper { display: flex; align-items: center; gap: 2px; }
.stepper button { width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 19px; line-height: 1; }
.stepper output { width: 36px; text-align: center; font-weight: 650; font-variant-numeric: tabular-nums; }
.split-foot { padding: 10px 14px; font-size: 14px; color: var(--muted); background: var(--sunken); display: flex; justify-content: space-between; gap: 10px; }
.split-foot.bad { color: var(--neg); background: var(--neg-soft); }
.split-tools { display: flex; gap: 14px; justify-content: flex-end; margin-top: -8px; }
.split-tools button { background: none; border: 0; color: var(--accent); font-weight: 600; font-size: 14px; padding: 4px 0; }

.receipt-box { display: flex; align-items: center; gap: 12px; }
.receipt-thumb { width: 64px; height: 64px; border-radius: 10px; object-fit: cover; border: 1px solid var(--line); background: var(--sunken); }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.detail-amount { font: 500 40px/1.1 var(--serif); margin: 0; font-variant-numeric: tabular-nums; }
.detail-meta { color: var(--muted); margin-top: 4px; }
.detail-note { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; white-space: pre-wrap; }
.detail-receipt { width: 100%; max-height: 360px; object-fit: contain; border-radius: 12px; background: var(--sunken); border: 1px solid var(--line); }
.detail-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.small-meta { font-size: 13px; color: var(--muted); }

.viewer { position: fixed; inset: 0; z-index: 70; background: #000; display: grid; place-items: center; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .icon-btn { position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; background: rgba(255,255,255,.12); border-color: transparent; color: #fff; }

/* Toasts */
#toasts { position: fixed; left: 0; right: 0; bottom: calc(var(--tabs-h) + 84px + env(safe-area-inset-bottom)); z-index: 80; display: grid; justify-items: center; gap: 8px; pointer-events: none; padding: 0 16px; }
.toast { pointer-events: auto; background: var(--ink); color: var(--bg); padding: 11px 14px 11px 16px; border-radius: 12px; font-size: 15px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 14px; max-width: 480px; animation: up .2s ease-out; }
.toast button { background: none; border: 0; color: inherit; font-weight: 700; text-decoration: underline; padding: 2px 0; }

.loading { display: grid; place-items: center; min-height: 60vh; color: var(--muted); }

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