:root {
  color-scheme: light dark;
  --bg: #f4f4f2;
  --surface: #ffffff;
  --surface-2: #f0f0ee;
  --border: #e2e2df;
  --text: #121212;
  --text-2: #5a5955;
  --text-3: #8a8985;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c93a39;
  --good: #1f7a33;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4;
  --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141416;
    --surface: #1e1e21;
    --surface-2: #27272b;
    --border: #333338;
    --text: #f4f4f2;
    --text-2: #bdbcb5;
    --text-3: #8c8b86;
    --accent: #3987e5;
    --danger: #e66767;
    --good: #4fbf6a;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
    --s6: #008300; --s7: #9085e9; --s8: #e66767;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 20px; } h3 { font-size: 15px; margin-bottom: 10px; }
p { margin: 0 0 8px; }
.muted { color: var(--text-2); } .small-text { font-size: 13px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
[hidden] { display: none !important; }

/* ---- brand ---- */
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 24px; } .brand p { color: var(--text-2); margin: 0; }
.brand.small strong { font-size: 15px; }
.flag { width: 28px; height: 28px; border-radius: 7px; flex: none;
  background-color: #141416;
  background-image: linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%),
                    linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%);
  background-size: 10px 10px; background-position: 0 0, 5px 5px; }
.brand.small .flag { width: 22px; height: 22px; background-size: 8px 8px; background-position: 0 0, 4px 4px; }

/* ---- login ---- */
.screen { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 420px; background: var(--surface); border-radius: 20px; padding: 28px; box-shadow: var(--shadow); }
.login-card .brand { margin-bottom: 24px; }
.member-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.member-btn { display: flex; align-items: center; gap: 10px; padding: 14px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; }
.member-btn:hover { border-color: var(--accent); }
#login-pin { font-size: 28px; letter-spacing: .4em; text-align: center; }

/* ---- layout ---- */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--border); }
.period { display: flex; align-items: center; gap: 2px; }
.period-label { white-space: nowrap; font: inherit; font-weight: 650; background: none; border: 0; color: var(--text); padding: 6px 10px; border-radius: 8px; cursor: pointer; min-width: 130px; }
.period-label:hover { background: var(--surface-2); }
.icon-btn { width: 34px; height: 34px; border-radius: 8px; border: 0; background: none; color: var(--text); font-size: 22px; cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
.who { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
main { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(80px + env(safe-area-inset-bottom)); }
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 14px; flex-wrap: wrap; }
.count { color: var(--text-3); font-weight: 500; font-size: 15px; }
.tabbar { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom); z-index: 5; }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 8px; border: 0; background: none;
  color: var(--text-3); font: inherit; font-size: 12px; font-weight: 600; cursor: pointer; }
.tab.active { color: var(--accent); } .tab-ico { font-size: 20px; line-height: 1; }
@media (min-width: 720px) { .tabbar { position: sticky; top: 56px; max-width: 760px; margin: 0 auto; border: 1px solid var(--border); border-radius: 12px; }
  main { padding-bottom: 40px; } body { display: flex; flex-direction: column; } #app { display: contents; } }

/* ---- cards, tiles ---- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.card-head h3 { margin: 0; }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.tile-label { font-size: 12px; color: var(--text-2); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.tile-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
@media (max-width: 420px) { .tile-value { font-size: 18px; } }
.chart-wrap { position: relative; width: 100%; margin-bottom: 8px; }

/* ---- lists ---- */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; margin-bottom: 8px; }
.list.compact li { padding: 8px 10px; background: var(--surface-2); border-color: transparent; }
.list li.clickable { cursor: pointer; } .list li.clickable:hover { border-color: var(--accent); }
.avatar { width: 34px; height: 34px; border-radius: 50%; flex: none; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 14px; }
.avatar.sm { width: 24px; height: 24px; font-size: 11px; }
.item-main { flex: 1; min-width: 0; }
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-sub { color: var(--text-2); font-size: 13px; display: flex; gap: 8px; flex-wrap: wrap; }
.chip { background: var(--surface-2); border-radius: 999px; padding: 0 8px; font-size: 12px; font-weight: 600; color: var(--text-2); }
.amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.receipt-link { flex: none; text-decoration: none; font-size: 18px; padding: 4px; border-radius: 6px; }
.receipt-link:hover { background: var(--surface-2); }
.empty { color: var(--text-2); text-align: center; padding: 28px 0; }

/* ---- table ---- */
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; }
.table th { font-size: 12px; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.table .num { text-align: right; }
.table tr:last-child td { border-bottom: 0; }
.owed { color: var(--good); font-weight: 600; } .owes { color: var(--danger); font-weight: 600; }
.member-cell { display: flex; align-items: center; gap: 8px; }

/* ---- transfers ---- */
.transfer { display: flex; align-items: center; gap: 10px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 8px; background: var(--surface-2); }
.transfer .arrow { color: var(--text-3); }
.transfer .amount { margin-left: auto; font-size: 17px; }
.settled { display: flex; align-items: center; gap: 10px; padding: 14px; border-radius: 12px; background: color-mix(in srgb, var(--good) 12%, transparent); color: var(--good); font-weight: 600; }

/* ---- category bars ---- */
.cat-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin-bottom: 10px; align-items: center; }
.cat-row .bar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.cat-row .bar > span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

/* ---- notes & badges ---- */
.note { font-size: 13px; color: var(--text-2); background: var(--surface-2); border-radius: 10px; padding: 8px 12px; margin: 0; }
.chip.repeat { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.paused { opacity: .6; }

/* ---- receipt picker ---- */
.file-label { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--text-2); }
.receipt-actions .btn { flex: 1; }
.receipt-preview { display: flex; align-items: center; gap: 10px; }
.receipt-preview img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.receipt-preview .btn { margin-left: auto; }

/* ---- forms ---- */
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; } .row > label { flex: 1; } .row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; color: var(--text-2); }
label.check { flex-direction: row; align-items: center; gap: 8px; }
label.check input { width: auto; margin: 0; }
input, select { font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="file"] { padding: 8px; }
.file-hint { font-weight: 400; color: var(--text-3); font-size: 12px; }
.btn { font: inherit; font-weight: 600; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 10px;
  padding: 10px 16px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.tiny { padding: 4px 8px; font-size: 12px; border-radius: 8px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg-btn { font: inherit; font-size: 13px; font-weight: 600; border: 0; background: none; color: var(--text-2); padding: 6px 12px; border-radius: 8px; cursor: pointer; }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.filters { margin-bottom: 12px; } .filters select { width: auto; }
.error { color: var(--danger); font-size: 13px; font-weight: 600; }

dialog { border: 0; border-radius: 18px; padding: 20px; width: min(520px, calc(100vw - 24px)); background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { font-size: 18px; margin-bottom: 4px; }
@media (max-width: 520px) { dialog { margin: auto 12px 12px; width: calc(100vw - 24px); } }

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

@media (max-width: 520px) { .brand.small strong { display: none; } .who-name { display: none; } }
