:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #eef1ee;
  --text: #17201d;
  --muted: #61706b;
  --border: #dbe1dd;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d8efec;
  --pos: #11795a;
  --neg: #c0392b;
  --danger: #b42318;
  --shadow: 0 1px 2px rgba(16, 24, 20, .06), 0 8px 24px rgba(16, 24, 20, .08);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --surface: #171e1c;
    --surface-2: #1f2825;
    --text: #e6ece9;
    --muted: #92a19c;
    --border: #2b3632;
    --accent: #2bb3a3;
    --accent-ink: #062320;
    --accent-soft: #163a35;
    --pos: #43c59a;
    --neg: #f07a6c;
    --danger: #f07a6c;
    --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(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; margin: 24px 0 10px; }
.muted { color: var(--muted); font-style: normal; }
.small { font-size: .85rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Buttons and inputs */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 10px;
  padding: 10px 16px; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px;
}
.btn.small { padding: 6px 10px; min-height: 34px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.ghost { background: transparent; color: var(--text); }
.btn.ghost:hover, .btn.secondary:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.link-btn { background: none; border: 0; color: var(--accent); font: inherit; font-size: .85rem; cursor: pointer; padding: 2px 0; }
.icon-btn { background: none; border: 0; font-size: 1.6rem; line-height: 1; color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; min-height: 42px;
}
textarea { resize: vertical; min-height: 0; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field { display: block; margin: 0 0 14px; border: 0; padding: 0; min-width: 0; }
.field > span, .field > legend { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; padding: 0; }
.row { display: flex; gap: 10px; }
.row .field { min-width: 0; }
.grow { flex: 1; }
.form-error { color: var(--danger); font-size: .88rem; margin: 4px 0 0; min-height: 1em; }
.form-error:empty { display: none; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border-radius: 18px;
  padding: 32px 24px; box-shadow: var(--shadow); text-align: center;
}
.login-card h1 { font-size: 1.5rem; margin-top: 10px; }
.login-card p { margin: 6px 0 20px; }
.login-card .field { text-align: left; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; padding: 8px 16px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; }
.tabs { display: flex; gap: 4px; margin: 0 auto; }
.tabs button {
  background: none; border: 0; font: inherit; font-weight: 600; color: var(--muted);
  padding: 8px 12px; border-radius: 8px; cursor: pointer;
}
.tabs button[aria-selected="true"] { color: var(--accent); background: var(--accent-soft); }
.top-actions { display: flex; gap: 4px; }
@media (max-width: 640px) {
  .topbar { padding: 8px 12px; }
  .tabs { order: 3; width: 100%; justify-content: space-between; }
  .tabs button { flex: 1; }
  .top-actions { margin-left: auto; }
}

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

/* Stats */
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
@media (max-width: 480px) { .stats { gap: 6px; } .stat { padding: 10px; } .stat .value { font-size: 1.02rem; } }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat .label { font-size: .78rem; color: var(--muted); font-weight: 600; }
.stat .value { font-size: 1.25rem; font-weight: 700; margin-top: 2px; }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.toolbar input { flex: 2; }
.toolbar select { flex: 1; }

/* Lists */
.entry-list, .balance-list, .settle-list, .people-list, .split-list { list-style: none; margin: 0; padding: 0; }
.month-head { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 4px 6px; }
.entry {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 8px; cursor: pointer;
  width: 100%; text-align: left; font: inherit; color: inherit;
}
.entry:hover { border-color: var(--accent); }
.entry .date { width: 38px; text-align: center; flex: none; color: var(--muted); font-size: .75rem; line-height: 1.1; }
.entry .date b { display: block; font-size: 1.1rem; color: var(--text); }
.entry .main { flex: 1; min-width: 0; }
.entry .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .sub { color: var(--muted); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .amt { text-align: right; font-weight: 700; }
.entry.payment .title { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

.balance-list li, .settle-list li, .people-list li {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 8px;
}
.balance-list .name, .people-list .name { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.balance-list .bar { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; position: relative; }
.balance-list .bar i { position: absolute; top: 0; bottom: 0; border-radius: 3px; }
.balance-list .bar i.pos { background: var(--pos); left: 50%; }
.balance-list .bar i.neg { background: var(--neg); right: 50%; }
.settle-list .text { flex: 1; min-width: 0; }
.people-list .tag { font-size: .75rem; color: var(--muted); background: var(--surface-2); padding: 2px 8px; border-radius: 99px; }
.people-list li.archived .name { color: var(--muted); }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }

/* Floating actions */
.fab-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; justify-content: center; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}
.fab-bar .btn { box-shadow: var(--shadow); }

/* Dialogs */
dialog.sheet {
  border: 0; padding: 0; border-radius: 18px; width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 24px); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.sheet::backdrop { background: rgba(10, 16, 14, .45); }
dialog.sheet form { display: flex; flex-direction: column; max-height: calc(100vh - 24px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 8px; }
.sheet-body { padding: 8px 18px; overflow-y: auto; flex: 1; }
.sheet-foot { display: flex; gap: 8px; align-items: center; padding: 12px 18px 16px; border-top: 1px solid var(--border); }
.spacer { flex: 1; }
@media (max-width: 640px) {
  dialog.sheet { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 18px 18px 0 0; max-height: 92vh; }
  dialog.sheet form { max-height: 92vh; }
  .sheet-foot { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}

.segmented { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; min-height: 0; }
.segmented span { display: block; text-align: center; padding: 7px 4px; border-radius: 8px; font-weight: 600; font-size: .88rem; color: var(--muted); }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.split-tools { display: flex; gap: 14px; justify-content: flex-end; margin: 6px 2px; }
.split-list li { display: flex; align-items: center; gap: 10px; padding: 6px 2px; border-bottom: 1px solid var(--border); }
.split-list li:last-child { border-bottom: 0; }
.split-list label.who { flex: 1; display: flex; align-items: center; gap: 10px; cursor: pointer; min-width: 0; }
.split-list label.who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.split-list input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); flex: none; }
.split-list input.val { width: 92px; text-align: right; min-height: 36px; padding: 6px 8px; }
.split-list .owed { width: 84px; text-align: right; color: var(--muted); font-size: .88rem; }
.split-status { font-size: .85rem; margin: 8px 2px 0; color: var(--muted); }
.split-status.bad { color: var(--danger); }
.split-status.ok { color: var(--pos); }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; font-weight: 600;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
