:root {
  --bg: #f4f6f6;
  --card: #ffffff;
  --text: #1c2526;
  --muted: #6b7778;
  --line: #e2e7e7;
  --accent: #0f766e;
  --accent-soft: #d5ecea;
  --danger: #b42318;
  --radius: 12px;
  --nav-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111617;
    --card: #1b2223;
    --text: #e6eded;
    --muted: #93a0a1;
    --line: #2b3536;
    --accent: #2bb3a6;
    --accent-soft: #1f3c3a;
    --danger: #f97066;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--accent); color: #fff;
}
.top h1 { margin: 0; font-size: 1.2rem; }
.who { display: flex; gap: 12px; align-items: center; font-size: .9rem; }
.who .link { color: #fff; }

main { max-width: 560px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin-bottom: 12px;
}
h2 { margin: 0 0 12px; font-size: 1.15rem; }
h3 { margin: 0 0 10px; font-size: 1rem; }

label { display: block; margin-bottom: 12px; font-size: .9rem; color: var(--muted); }
input, select {
  display: block; width: 100%; margin-top: 4px;
  font: inherit; font-size: 1.05rem; color: var(--text);
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg);
}
input[name="amount"] { font-size: 1.6rem; font-weight: 600; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; }

button {
  font: inherit; cursor: pointer; border-radius: 10px;
  padding: 10px 14px; border: 1px solid var(--line);
  background: var(--card); color: var(--text);
}
button:disabled { opacity: .6; }
button.primary { width: 100%; background: var(--accent); color: #fff; border: 0; padding: 14px; font-weight: 600; font-size: 1.05rem; }
button.danger { color: var(--danger); border-color: var(--danger); }
button.link { background: none; border: 0; padding: 0; text-decoration: underline; }

.row { display: flex; gap: 10px; margin-top: 10px; }
.row > * { flex: 1; }
.row label { margin-bottom: 0; }

.error { color: var(--danger); min-height: 1em; margin: 0 0 10px; }
.muted { color: var(--muted); }

.month-nav {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px; font-weight: 600;
}
.month-nav button { width: 44px; height: 44px; padding: 0; font-size: 1.4rem; line-height: 1; }

.sum { margin: 0 0 10px; font-weight: 600; }

.expenses { list-style: none; margin: 0; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.expenses li { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-top: 1px solid var(--line); }
.expenses li:first-child { border-top: 0; }
.expenses li.mine { cursor: pointer; }
.expenses li.mine:active { background: var(--accent-soft); }
.expenses .d { color: var(--muted); font-variant-numeric: tabular-nums; width: 3em; flex: none; }
.expenses .t { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.expenses .t small { display: block; color: var(--muted); font-size: .8rem; }
.expenses li.mine .t small { color: var(--accent); }
.expenses .a { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

.seg { display: flex; margin-bottom: 12px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; border: 0; border-radius: 0; background: transparent; }
.seg button.on { background: var(--accent); color: #fff; }

.totals { width: 100%; border-collapse: collapse; }
.totals td { padding: 8px 0; border-top: 1px solid var(--line); }
.totals tr:first-child td { border-top: 0; }
.totals td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.totals .grand td { font-weight: 700; border-top: 2px solid var(--text); }

.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 5.5em 1fr auto; gap: 8px; align-items: center; font-size: .9rem; }
.bl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bt { height: 18px; background: var(--accent-soft); border-radius: 4px; overflow: hidden; }
.bf { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.bv { font-variant-numeric: tabular-nums; min-width: 5.5em; text-align: right; }

.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.bottom button {
  flex: 1; border: 0; border-radius: 0; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 1.2rem; color: var(--muted);
}
.bottom button span { font-size: .75rem; }
.bottom button.on { color: var(--accent); font-weight: 600; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 999px; font-size: .9rem; z-index: 10;
  max-width: calc(100% - 32px);
}
