:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #0284c7;
  --accent-strong: #0369a1;
  --household: #7c3aed;
  --construction: #ea580c;
  --other: #64748b;
  --danger: #dc2626;
  --good: #16a34a;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #131c2e;
    --text: #e5e9f2;
    --muted: #93a2b8;
    --border: #23304a;
    --accent: #38bdf8;
    --accent-strong: #7dd3fc;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
#app { max-width: 560px; margin: 0 auto; min-height: 100vh; display: flex; flex-direction: column; padding-bottom: 72px; }

a { color: inherit; }
button { font-family: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 14px 16px; display: flex; align-items: center; justify-content: space-between;
}
.topbar h1 { font-size: 17px; margin: 0; }
.topbar .month-nav { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.topbar .month-nav button { background: none; border: none; color: var(--text); font-size: 18px; padding: 4px 8px; cursor: pointer; border-radius: 8px; }
.topbar .month-nav button:active { background: var(--bg); }

.content { padding: 16px; flex: 1; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px;
}
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 12px; }

.totals-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.total-tile { border-radius: 10px; padding: 10px 8px; text-align: center; background: var(--bg); }
.total-tile .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.total-tile .amount { font-size: 14px; font-weight: 700; margin-top: 4px; word-break: break-word; }
.total-tile.household { border-top: 3px solid var(--household); }
.total-tile.construction { border-top: 3px solid var(--construction); }
.total-tile.other { border-top: 3px solid var(--other); }

.balance-row { display: flex; justify-content: space-between; align-items: baseline; margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--border); }
.balance-row .amt.positive { color: var(--good); }
.balance-row .amt.negative { color: var(--danger); }

.progress-outer { background: var(--bg); border-radius: 8px; height: 10px; overflow: hidden; margin: 10px 0; }
.progress-inner { height: 100%; background: var(--accent); }
.progress-inner.over { background: var(--danger); }
.budget-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }

.bar-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 13px; }
.bar-row .name { width: 96px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; background: var(--bg); border-radius: 6px; height: 8px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); }
.bar-amount { width: 92px; text-align: right; flex-shrink: 0; font-variant-numeric: tabular-nums; }

.expense-list { display: flex; flex-direction: column; gap: 8px; }
.expense-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.expense-item .left { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.expense-item .cat { font-weight: 600; font-size: 14px; }
.expense-item .meta { font-size: 12px; color: var(--muted); }
.expense-item .amount { font-weight: 700; font-size: 14px; white-space: nowrap; margin-left: 10px; }
.expense-item .tag { display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 20px; margin-right: 6px; color: #fff; }
.tag.household { background: var(--household); }
.tag.construction { background: var(--construction); }
.tag.other { background: var(--other); }

.empty-state { text-align: center; color: var(--muted); padding: 30px 10px; font-size: 14px; }

form.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12px; color: var(--muted); }
.field input, .field select, .field textarea {
  padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 15px; width: 100%;
}
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.group-picker { display: flex; gap: 8px; }
.group-picker button {
  flex: 1; padding: 10px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); cursor: pointer; font-size: 13px;
}
.group-picker button.active.household { background: var(--household); color: #fff; border-color: var(--household); }
.group-picker button.active.construction { background: var(--construction); color: #fff; border-color: var(--construction); }
.group-picker button.active.other { background: var(--other); color: #fff; border-color: var(--other); }

.btn {
  padding: 13px; border-radius: 10px; border: none; background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 600; cursor: pointer; text-align: center;
}
.btn.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--danger); color: #fff; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

.filters { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.filters select, .filters input { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 13px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; max-width: 560px; margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; text-align: center; padding: 10px 4px 8px; text-decoration: none; color: var(--muted);
  font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tabbar a.active { color: var(--accent); }
.tabbar .icon { font-size: 19px; }

.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card { width: 100%; max-width: 360px; }
.login-card h1 { font-size: 20px; margin-bottom: 4px; }
.login-card p.sub { color: var(--muted); font-size: 13px; margin-top: 0; margin-bottom: 20px; }

.error-banner { background: #fee2e2; color: #991b1b; padding: 10px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
@media (prefers-color-scheme: dark) {
  .error-banner { background: #3f1d1d; color: #fca5a5; }
}
.toast {
  position: fixed; bottom: 84px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 20px;
  font-size: 13px; z-index: 20; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.toast.show { opacity: 1; }

.fab {
  position: fixed; right: 18px; bottom: 88px; width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 28px; border: none; box-shadow: 0 6px 16px rgba(0,0,0,.25);
  cursor: pointer; z-index: 6; display: flex; align-items: center; justify-content: center;
  max-width: 560px;
}

.install-banner {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 76px;
  width: calc(100% - 32px); max-width: 528px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
  padding: 14px; z-index: 25; font-size: 13px;
  display: flex; flex-direction: column; gap: 10px;
}
.install-banner-actions { display: flex; gap: 8px; justify-content: flex-end; }
.install-banner-actions .btn { padding: 8px 14px; font-size: 13px; flex: none; }
