:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --text: #1f2124;
  --muted: #6a6d73;
  --border: #e3e0d8;
  --accent: #2f6f5e;
  --accent-hover: #255a4c;
  --accent-weak: #e3efe9;
  --on-accent: #ffffff;
  --danger: #b4361a;
  --danger-weak: #fbe9e4;
  --warn: #a15c07;
  --warn-weak: #fdf0dc;
  --ok: #2f7d3a;
  --spent: #3b6fd8;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 2px 8px rgb(0 0 0 / 0.04);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141517;
    --surface: #1c1d20;
    --surface-2: #25272b;
    --text: #e9e9ea;
    --muted: #9a9ca2;
    --border: #2f3136;
    --accent: #5fb39b;
    --accent-hover: #74c3ac;
    --accent-weak: #1d332c;
    --on-accent: #0d1f19;
    --danger: #f08a6e;
    --danger-weak: #3a221c;
    --warn: #f0b45e;
    --warn-weak: #392b17;
    --ok: #7ccf88;
    --spent: #7fa6f5;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.6rem; font-weight: 650; }
h2 { font-size: 1.1rem; font-weight: 600; }
h3 { font-size: 0.95rem; font-weight: 600; }
[hidden] { display: none !important; }
.loading { padding: 3rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- Shell ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 4px;
  padding: 20px 12px; border-right: 1px solid var(--border); background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 1.1rem; }
.brand img { width: 30px; height: 30px; }
.brand small { display: block; font-weight: 400; font-size: 0.8rem; color: var(--muted); }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 9px 12px;
  border-radius: 9px; color: var(--text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-weak); color: var(--accent); }
.nav svg { width: 20px; height: 20px; flex: none; }
.sidebar .spacer { flex: 1; }
.me-link { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; color: var(--text); text-decoration: none; }
.me-link:hover { background: var(--surface-2); }

main { padding: 28px 32px 64px; max-width: 1100px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: auto 0 0 0; height: auto; z-index: 20;
    flex-direction: row; justify-content: space-around; gap: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border);
  }
  .brand, .sidebar .spacer { display: none; }
  .nav { display: contents; }
  .nav a, .me-link { flex-direction: column; gap: 2px; padding: 6px 4px; font-size: 0.7rem; flex: 1; justify-content: center; text-align: center; }
  .me-link .avatar { width: 22px; height: 22px; font-size: 0.65rem; }
  .me-link .me-name { font-size: 0.7rem; }
  main { padding: 20px 16px 96px; }
  h1 { font-size: 1.35rem; }
}

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 550; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 4px 10px; font-size: 0.85rem; border-radius: 7px; }
.btn:disabled { opacity: 0.55; cursor: default; }
.icon-btn { padding: 5px; border: 0; background: transparent; border-radius: 7px; cursor: pointer; color: var(--muted); display: inline-flex; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

label.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; font-weight: 550; font-size: 0.9rem; }
label.field .hint, .field > .hint { font-weight: 400; color: var(--muted); font-size: 0.8rem; }
input[type=text], input[type=url], input[type=email], input[type=password], input[type=date], input[type=number], input[type=month], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface);
  font-weight: 400;
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
label.field.check-row, .check-row { display: flex; flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.check-grid { display: flex; flex-wrap: wrap; gap: 6px 14px; font-weight: 400; }
.check-grid label { display: flex; align-items: center; gap: 6px; }
fieldset.field { border: 0; padding: 0; margin: 0 0 14px; }
fieldset.field legend { padding: 0; margin-bottom: 4px; font-weight: 550; font-size: 0.9rem; }
fieldset.field > .hint { display: block; margin-bottom: 8px; }
.swatch { position: relative; cursor: pointer; }
.swatch input { position: absolute; opacity: 0; }
.swatch .avatar { width: 28px; height: 28px; }
.swatch input:checked + .avatar { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text); }
.swatch input:focus-visible + .avatar { outline: 2px solid var(--accent); outline-offset: 4px; }
.form-error { color: var(--danger); background: var(--danger-weak); padding: 8px 12px; border-radius: 8px; margin: 0 0 14px; font-size: 0.9rem; }

/* ---------- Cards & lists ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 16px 20px 6px; }
.card-head a { font-size: 0.85rem; text-decoration: none; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); align-items: start; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.section-title { margin: 22px 0 8px; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 650; }
.section-title.overdue { color: var(--danger); }

.list { list-style: none; margin: 0; padding: 4px 0; }
.item { display: flex; align-items: center; gap: 12px; padding: 11px 20px; border-top: 1px solid var(--border); }
.list .item:first-child { border-top: 0; }
.item-main { flex: 1; min-width: 0; }
.item-title { font-weight: 550; overflow-wrap: anywhere; }
.item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; color: var(--muted); font-size: 0.83rem; margin-top: 3px; }
.item-actions { display: flex; align-items: center; gap: 2px; flex: none; }
.item.done .item-title { text-decoration: line-through; color: var(--muted); }
.empty { padding: 22px 20px; color: var(--muted); text-align: center; }

.checkbox {
  appearance: none; width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border);
  flex: none; cursor: pointer; display: grid; place-items: center; margin: 0; background: var(--surface);
}
.checkbox:hover { border-color: var(--accent); }
.checkbox:checked { background: var(--accent); border-color: var(--accent); }
.checkbox:checked::after { content: ""; width: 6px; height: 11px; border: solid var(--on-accent); border-width: 0 2px 2px 0; transform: translateY(-1px) rotate(45deg); }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge.overdue { background: var(--danger-weak); color: var(--danger); }
.badge.today { background: var(--warn-weak); color: var(--warn); }
.badge.soon { background: var(--accent-weak); color: var(--accent); }
.tag { font-size: 0.78rem; padding: 1px 8px; border-radius: 6px; border: 1px solid var(--border); color: var(--muted); }

.avatar {
  width: 26px; height: 26px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  color: #fff; font-size: 0.72rem; font-weight: 700; background: var(--avatar, #888); letter-spacing: 0.02em;
}
.avatar.lg { width: 40px; height: 40px; font-size: 0.95rem; }
.who { display: inline-flex; align-items: center; gap: 6px; color: var(--text); }
.who .avatar { width: 20px; height: 20px; font-size: 0.6rem; }

.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 10px; width: fit-content; max-width: 100%; overflow-x: auto; }
.tabs button { border: 0; background: transparent; padding: 6px 14px; border-radius: 7px; cursor: pointer; font-weight: 550; color: var(--muted); white-space: nowrap; }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar select { width: auto; }

/* ---------- Stats & charts ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 0.82rem; }
.stat .value { font-size: 1.45rem; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .value.neg { color: var(--danger); }
.stat .value.pos { color: var(--ok); }

.meter { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; width: var(--pct, 0%); }
.meter.warn > span { background: var(--warn); }
.meter.over > span { background: var(--danger); }
.budget-row { padding: 12px 20px; border-top: 1px solid var(--border); }
.budget-row:first-child { border-top: 0; }
.budget-row .top { display: flex; justify-content: space-between; gap: 12px; font-size: 0.9rem; }

.bars { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; align-items: end; height: 160px; padding: 10px 20px 0; }
.bar-group { display: flex; gap: 3px; align-items: end; height: 100%; justify-content: center; }
.bar { width: 14px; border-radius: 4px 4px 0 0; height: var(--h, 0%); min-height: 2px; }
.bar.income { background: var(--ok); }
.bar.expense { background: var(--spent); }
.bar-labels { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; padding: 6px 20px 14px; text-align: center; font-size: 0.78rem; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 0.8rem; color: var(--muted); }
.legend i.bar { height: 10px; min-height: 0; width: 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.amount.expense { color: var(--text); }
.amount.income { color: var(--ok); }

.person-head { display: flex; align-items: center; gap: 10px; margin: 24px 0 10px; }
.person-head h2 { font-size: 1.05rem; }
.item-title a { color: inherit; text-decoration-color: var(--border); text-underline-offset: 3px; }
.item-title a:hover { color: var(--accent); }
.leader { display: flex; align-items: center; gap: 10px; padding: 8px 20px; }
.leader .count { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }

/* ---------- Dialogs ---------- */
dialog.modal {
  border: 1px solid var(--border); border-radius: 16px; padding: 0; width: min(480px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
}
dialog.modal::backdrop { background: rgb(0 0 0 / 0.4); }
dialog.modal form, dialog.modal .dialog-body { padding: 22px 22px 18px; max-height: calc(100vh - 64px); overflow-y: auto; }
dialog.modal h2 { margin-bottom: 18px; }
dialog.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
dialog.modal .actions .left { margin-right: auto; }

/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth .card { width: min(420px, 100%); padding: 28px 26px; }
.auth .brand { padding: 0 0 6px; font-size: 1.3rem; }
.auth p.lead { color: var(--muted); margin: 0 0 22px; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 550; box-shadow: 0 6px 20px rgb(0 0 0 / 0.2); animation: toast-in 0.18s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@media (max-width: 760px) { #toasts { bottom: 86px; } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }
