:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #17201c;
  --muted: #6b7a73;
  --line: #e3e8e5;
  --brand: #16a374;
  --brand-ink: #ffffff;
  --in: #11865f;
  --in-bg: #e3f6ee;
  --out: #d1453b;
  --out-bg: #fdeceb;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  --radius: 18px;
  --tabbar-h: 58px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1412;
    --card: #1a211e;
    --text: #edf2ef;
    --muted: #93a39b;
    --line: #2a332f;
    --brand: #22b985;
    --in: #3fcf98;
    --in-bg: #143126;
    --out: #ff7b70;
    --out-bg: #3a1c1a;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
}
main {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}
h1 { font-size: 28px; margin: 8px 0 14px; letter-spacing: -.3px; }
h2 { font-size: 18px; margin: 22px 0 10px; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* Cards */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.total {
  background: linear-gradient(160deg, #1bb07f, #0d7a55);
  color: #fff;
  padding: 20px;
}
.total .label { opacity: .85; font-size: 14px; }
.total .amount { font-size: 34px; font-weight: 700; margin-top: 2px; }

.kids { display: grid; gap: 10px; margin-top: 14px; }
.kid {
  display: flex; align-items: center; gap: 14px;
  text-decoration: none; color: inherit;
}
.kid:active { transform: scale(.99); }
.avatar {
  width: 48px; height: 48px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 26px; flex: none;
}
.kid .name { font-weight: 600; font-size: 17px; }
.kid .sub { font-size: 13px; color: var(--muted); }
.kid .bal { margin-inline-start: auto; font-weight: 700; font-size: 18px; }
.kid .chev { color: var(--muted); font-size: 20px; }

/* Buttons */
button, .btn {
  font: inherit; border: 0; cursor: pointer;
  border-radius: 14px; padding: 12px 16px;
  background: var(--line); color: var(--text);
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button:active, .btn:active { opacity: .8; }
.btn-primary { background: var(--brand); color: var(--brand-ink); font-weight: 600; }
.btn-in { background: var(--in); color: #fff; font-weight: 600; }
.btn-out { background: var(--out); color: #fff; font-weight: 600; }
.btn-ghost { background: transparent; color: var(--brand); padding: 8px 4px; }
.btn-danger { background: var(--out-bg); color: var(--out); }
.btn-block { width: 100%; }
.add-kid { margin-top: 14px; width: 100%; background: var(--card); color: var(--brand); border: 1.5px dashed var(--line); font-weight: 600; }

/* Child page */
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.hero { text-align: center; padding: 22px 16px; }
.hero .avatar { width: 72px; height: 72px; font-size: 40px; margin: 0 auto 8px; }
.hero .name { font-size: 22px; font-weight: 700; }
.hero .amount { font-size: 40px; font-weight: 800; margin: 4px 0 2px; }
.hero .stats { display: flex; justify-content: center; gap: 18px; font-size: 13px; color: var(--muted); margin-top: 6px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.actions button { padding: 16px; font-size: 17px; }

/* Transactions */
.tx-list { padding: 4px 0; }
.tx {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--line);
  width: 100%; background: none; border-radius: 0; text-align: start;
}
.tx:last-child { border-bottom: 0; }
.tx .ic {
  width: 36px; height: 36px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 800; font-size: 18px;
}
.tx.in .ic { background: var(--in-bg); color: var(--in); }
.tx.out .ic { background: var(--out-bg); color: var(--out); }
.tx .body { flex: 1; min-width: 0; }
.tx .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .meta { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .right { text-align: end; flex: none; }
.tx .amt { font-weight: 700; }
.tx.in .amt { color: var(--in); }
.tx.out .amt { color: var(--out); }
.tx .after { font-size: 12px; color: var(--muted); }
.tx.cancelled .title, .tx.cancelled .amt { text-decoration: line-through; opacity: .55; }
.badge { font-size: 11px; background: var(--line); color: var(--muted); padding: 1px 7px; border-radius: 99px; margin-inline-start: 6px; font-weight: 500; }
.month { padding: 10px 16px 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 30px 16px; }

/* Filters */
.filters { display: flex; gap: 8px; margin-bottom: 12px; }
.filters select { flex: 1; }

/* Forms */
label.f { display: block; margin: 12px 0 0; font-size: 14px; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); width: 100%;
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; margin-top: 4px; font-size: 16px; /* 16px prevents iOS zoom */
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
input.big { font-size: 30px; font-weight: 700; text-align: center; direction: ltr; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { padding: 7px 12px; border-radius: 99px; font-size: 14px; background: var(--bg); border: 1px solid var(--line); }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.emoji-pick, .color-pick { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.emoji-pick button { font-size: 24px; padding: 6px; width: 46px; height: 46px; background: var(--bg); border: 2px solid transparent; }
.emoji-pick button.on { border-color: var(--brand); }
.color-pick button { width: 36px; height: 36px; border-radius: 50%; padding: 0; border: 3px solid transparent; }
.color-pick button.on { border-color: var(--text); }
.err { color: var(--out); font-size: 14px; margin-top: 8px; min-height: 1em; }

/* Dialog (bottom sheet) */
dialog {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px;
  max-height: 92vh; border-radius: 22px 22px 0 0;
  background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog[open] { animation: up .22s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet { padding: 18px 16px calc(env(safe-area-inset-bottom) + 18px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h3 { margin: 0; font-size: 20px; }
.sheet .foot { display: grid; gap: 8px; margin-top: 18px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin-top: 12px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* Settings */
.list { padding: 0; overflow: hidden; }
.list > * { display: flex; width: 100%; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); background: none; border-radius: 0; text-align: start; color: var(--text); }
.list > *:last-child { border-bottom: 0; }
.note { font-size: 13px; color: var(--muted); margin: 8px 4px; }
.install { margin-bottom: 12px; font-size: 14px; display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); }
.install b { display: block; margin-bottom: 2px; }

/* Tab bar */
.tabbar {
  position: fixed; inset: auto 0 0 0;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
  z-index: 5;
}
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-decoration: none; color: var(--muted); font-size: 11px; gap: 2px; }
.tabbar a .ti { font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabbar a.on { color: var(--brand); font-weight: 600; }
.tabbar a.on .ti { filter: none; opacity: 1; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 99px;
  font-size: 15px; transition: .25s; z-index: 10; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Cloud sync + login */
.sync-line { font-size: 12px; color: var(--muted); min-height: 16px; margin-top: 2px; }
.sync-line[data-state="offline"], [data-sync][data-state="offline"] { color: var(--out); }
body.logged-out .tabbar { display: none; }
.login { text-align: center; padding-top: 8vh; }
.login img { border-radius: 22px; box-shadow: var(--shadow); }
.login h1 { margin-top: 16px; }
.login form { text-align: start; margin-top: 20px; display: grid; gap: 10px; }
.login .err { text-align: center; margin: 0; }
