/* Family Finance: mobile-first, no external fonts or frameworks. */

:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --line: #e3e6eb;
  --brand: #0f766e;
  --brand-text: #ffffff;
  --income: #15803d;
  --expense: #c2410c;
  --danger: #b91c1c;
  --track: #e6e9ee;
  --radius: 14px;
  --gap: 12px;
  --tabbar-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e8eaee;
    --muted: #9aa2b0;
    --line: #2a2f38;
    --brand: #14b8a6;
    --brand-text: #041513;
    --income: #4ade80;
    --expense: #fb923c;
    --danger: #f87171;
    --track: #2a2f38;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
body.with-tabbar { padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px); }

a { color: var(--brand); text-decoration: none; }
h1, h2 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1rem; }

/* ----- Frame ----------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 12px;
  background: var(--bg);
}

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px;
  display: grid;
  gap: var(--gap);
}

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  align-items: center;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabbar a {
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 8px 0;
  font-size: 0.7rem;
  color: var(--muted);
}
.tabbar a[aria-current="page"] { color: var(--brand); }
.tabbar svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tabbar .tab-add {
  justify-self: center;
  width: 52px;
  height: 52px;
  place-content: center;
  border-radius: 50%;
  background: var(--brand);
  color: var(--brand-text);
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.2);
}
.tabbar .tab-add svg { stroke-width: 2.4; }

/* ----- Building blocks ------------------------------------------------- */

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}
.card-head a { font-size: 0.9rem; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--line); }

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  padding: 8px 0;
  color: inherit;
}
.row-main { display: grid; min-width: 0; }
.row-title { font-weight: 500; }
.row-sub {
  color: var(--muted);
  font-size: 0.85rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.row.total { font-weight: 600; }
.row.muted { opacity: 0.6; }
.chevron { color: var(--muted); font-size: 1.4rem; }

.amount { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }
.income { color: var(--income); }
.expense { color: var(--expense); }

.empty { color: var(--muted); margin: 8px 0; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 0; }

.month-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.month-label { font-weight: 600; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.6rem;
  color: var(--text);
  background: var(--surface);
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}
.stat {
  display: grid;
  gap: 2px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 16px;
}
.stat span { color: var(--muted); font-size: 0.85rem; }
.stat strong { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.stat.wide { grid-column: 1 / -1; }

/* ----- Budgets --------------------------------------------------------- */

.budget { padding: 8px 0; }
.budget + .budget { border-top: 1px solid var(--line); }
.budget-head { display: flex; justify-content: space-between; gap: 8px; }
.budget-status { color: var(--muted); font-size: 0.85rem; }
.budget.over .budget-status { color: var(--danger); font-weight: 600; }

progress {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 8px;
  margin: 6px 0 4px;
  border: 0;
  border-radius: 4px;
  overflow: hidden;
  background: var(--track);
  color: var(--brand);
}
progress::-webkit-progress-bar { background: var(--track); }
progress::-webkit-progress-value { background: var(--brand); border-radius: 4px; }
progress::-moz-progress-bar { background: var(--brand); border-radius: 4px; }
.over progress { color: var(--danger); }
.over progress::-webkit-progress-value { background: var(--danger); }
.over progress::-moz-progress-bar { background: var(--danger); }

.budget-input { padding: 6px 0; }
.budget-input + .budget-input { border-top: 1px solid var(--line); }
.budget-input .budget { padding: 0; }

/* ----- Forms ----------------------------------------------------------- */

.form { display: grid; gap: 14px; }
.form.narrow { max-width: 420px; width: 100%; justify-self: center; margin-top: 8vh; }

label { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 10px; color: var(--text); }

input, select, button { font: inherit; color: inherit; }

input:not([type="checkbox"]), select {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  /* 16px+ stops iOS from zooming into focused fields. */
  font-size: 1rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
}
input[data-money] { font-variant-numeric: tabular-nums; }
input[name="amount"] { font-size: 1.5rem; font-weight: 600; }
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}
input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--brand); }

.btn {
  min-height: 48px;
  padding: 10px 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-text); }
.btn.danger { color: var(--danger); border-color: currentColor; background: transparent; }
.btn:disabled { opacity: 0.6; cursor: progress; }

.danger-zone, .secondary-action { display: grid; gap: 8px; text-align: center; }

.alert {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.error-page { min-height: 70vh; place-content: center; text-align: center; }

/* ----- Wider screens --------------------------------------------------- */

@media (min-width: 720px) {
  .stats { grid-template-columns: repeat(3, 1fr); }
  .stat.wide { grid-column: auto; }
  .tabbar { max-width: 640px; margin: 0 auto; border: 1px solid var(--line); border-radius: 16px 16px 0 0; }
}

@media (prefers-reduced-motion: no-preference) {
  .btn, .row, .icon-btn { transition: opacity 0.15s; }
  .btn:active, .row:active, .icon-btn:active { opacity: 0.7; }
}
