:root {
  --bg: #f5f4ef;
  --surface: #ffffff;
  --surface-2: #efede6;
  --text: #1c2420;
  --muted: #66706b;
  --line: #e2dfd6;
  --brand: #1d6b52;
  --brand-ink: #ffffff;
  --brand-soft: #dcefe6;
  --me: #1d6b52;
  --me-soft: #dcefe6;
  --partner: #8a4fb8;
  --partner-soft: #efe3f8;
  --both: #b5701d;
  --both-soft: #f8ead6;
  --danger: #b3362c;
  --danger-soft: #fbe5e2;
  --warn-soft: #fff3d1;
  --warn-ink: #7a5a00;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 14px rgb(0 0 0 / .05);
  --tabbar-h: 68px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1714;
    --surface: #17211d;
    --surface-2: #1f2b26;
    --text: #e7ece9;
    --muted: #93a29b;
    --line: #2a3832;
    --brand: #4fbf93;
    --brand-ink: #08130f;
    --brand-soft: #18362b;
    --me: #5fcf9f;
    --me-soft: #17362a;
    --partner: #c79af0;
    --partner-soft: #2f2240;
    --both: #e8a95a;
    --both-soft: #3a2b18;
    --danger: #f08a80;
    --danger-soft: #3d1f1c;
    --warn-soft: #3a3115;
    --warn-ink: #f1d47a;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1714; --surface: #17211d; --surface-2: #1f2b26; --text: #e7ece9; --muted: #93a29b;
  --line: #2a3832; --brand: #4fbf93; --brand-ink: #08130f; --brand-soft: #18362b;
  --me: #5fcf9f; --me-soft: #17362a; --partner: #c79af0; --partner-soft: #2f2240;
  --both: #e8a95a; --both-soft: #3a2b18; --danger: #f08a80; --danger-soft: #3d1f1c;
  --warn-soft: #3a3115; --warn-ink: #f1d47a; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
body.page-login { padding-bottom: 32px; }
a { color: var(--brand); }
img { max-width: 100%; }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.num { font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }

.container { max-width: 640px; margin: 0 auto; padding: 16px; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  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: 8px; color: var(--text); text-decoration: none; font-weight: 700; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .75rem; font-weight: 600;
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.tab.active { color: var(--brand); }
.scan-bubble {
  display: grid; place-items: center; width: 52px; height: 52px; margin-top: -22px;
  border-radius: 50%; background: var(--brand); color: var(--brand-ink); box-shadow: var(--shadow);
  border: 4px solid var(--bg);
}
.tab-scan.active { color: var(--brand); }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.card-tight { padding: 0; overflow: hidden; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 650;
  cursor: pointer; text-decoration: none; touch-action: manipulation;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn-danger { color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--brand); }
.btn-block { width: 100%; }
.btn-lg { min-height: 56px; font-size: 1.05rem; }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: .85rem; border-radius: 10px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1; }

label { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
.field { margin-bottom: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-2 > .field { margin-bottom: 0; }

/* Balance hero */
.balance { text-align: center; padding: 22px 16px; }
.balance-amount { font-size: 2.4rem; font-weight: 800; letter-spacing: -.02em; margin: 4px 0; }
.balance-label { color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.05rem; }
.stat span { font-size: .75rem; color: var(--muted); }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px;
  border-top: 1px solid var(--line); color: inherit; text-decoration: none;
}
.list-item:first-child { border-top: 0; }
.list-main { flex: 1; min-width: 0; }
.list-title { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-sub { font-size: .8rem; color: var(--muted); }
.list-amount { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 24px 16px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .72rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.pill-me { background: var(--me-soft); color: var(--me); }
.pill-partner { background: var(--partner-soft); color: var(--partner); }

/* Capture */
.capture { text-align: center; padding: 28px 16px; }
.capture-icon { width: 64px; height: 64px; margin: 0 auto 10px; color: var(--brand); }
.thumbs { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 10px; }
.thumb { position: relative; flex: 0 0 auto; }
.thumb img { width: 84px; height: 112px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); display: block; }
.thumb button {
  position: absolute; top: -6px; right: -6px; width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: var(--text); color: var(--bg); font-weight: 800; cursor: pointer; line-height: 1;
}
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.spinner {
  width: 42px; height: 42px; margin: 6px auto 12px; border-radius: 50%;
  border: 4px solid var(--brand-soft); border-top-color: var(--brand); animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* Item assignment */
.bulk { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--surface-2); }
.bulk span { font-size: .85rem; color: var(--muted); font-weight: 600; margin-right: auto; }
.item { padding: 12px 16px; border-top: 1px solid var(--line); }
.item:first-of-type { border-top: 0; }
.item-row { display: flex; gap: 8px; align-items: center; }
.item-desc { flex: 1; min-width: 0; border-color: transparent; background: transparent; padding-left: 4px; font-weight: 600; }
.item-desc:hover { border-color: var(--line); }
.item-amount { width: 96px; flex: 0 0 96px; text-align: right; font-variant-numeric: tabular-nums; }
.item-raw { font-size: .75rem; color: var(--muted); margin: 2px 4px 8px; font-family: ui-monospace, monospace; }
.item-del { flex: 0 0 auto; width: 36px; min-height: 36px; padding: 0; border: 0; background: transparent; color: var(--muted); font-size: 1.3rem; cursor: pointer; border-radius: 8px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 8px; }
.seg button {
  min-height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  color: var(--muted); font: inherit; font-size: .85rem; font-weight: 650; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 6px; touch-action: manipulation;
}
.seg button[aria-pressed="true"][data-who="me"] { background: var(--me-soft); border-color: var(--me); color: var(--me); }
.seg button[aria-pressed="true"][data-who="partner"] { background: var(--partner-soft); border-color: var(--partner); color: var(--partner); }
.seg button[aria-pressed="true"][data-who="split"] { background: var(--both-soft); border-color: var(--both); color: var(--both); }
.item[data-who="me"] { box-shadow: inset 4px 0 0 var(--me); }
.item[data-who="partner"] { box-shadow: inset 4px 0 0 var(--partner); }
.item[data-who="split"] { box-shadow: inset 4px 0 0 var(--both); }
.split-row { display: grid; grid-template-columns: auto 1fr 1fr; gap: 8px; align-items: end; margin-top: 8px; }
.split-row [data-even] { min-height: 40px; }
.split-row [data-even][aria-pressed="true"] { background: var(--both-soft); border-color: var(--both); color: var(--both); }
.split-field { margin: 0; font-size: .75rem; }
.split-field span { display: block; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.split-field input { min-height: 40px; text-align: right; font-variant-numeric: tabular-nums; }

.totals .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 6px 0; }
.totals .row input { width: 110px; text-align: right; min-height: 40px; }
.totals .row.total { font-weight: 800; border-top: 1px solid var(--line); margin-top: 6px; padding-top: 10px; }
.alert { padding: 10px 12px; border-radius: 10px; font-size: .9rem; margin-bottom: 12px; }
.alert-warn { background: var(--warn-soft); color: var(--warn-ink); }
.alert-error { background: var(--danger-soft); color: var(--danger); }

.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.summary div { border-radius: 12px; padding: 10px 12px; }
.summary .me { background: var(--me-soft); color: var(--me); }
.summary .partner { background: var(--partner-soft); color: var(--partner); }
.summary b { display: block; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.summary span { font-size: .8rem; font-weight: 600; }

.sticky-actions {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px);
  padding-top: 14px; background: linear-gradient(transparent, var(--bg) 14px);
}

.receipt-photos { display: flex; gap: 8px; overflow-x: auto; }
.receipt-photos img { height: 160px; border-radius: 10px; border: 1px solid var(--line); }

.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; background: var(--brand); border-radius: 99px; }


.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 18px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-weight: 600; font-size: .9rem; transition: .2s; max-width: calc(100% - 32px); text-align: center; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog {
  border: 0; border-radius: 18px; padding: 20px; width: min(420px, calc(100% - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
