:root {
  --accent: #7c6fee;
  --accent-dark: #5e54d2;
  --bg: #f5f4fb;
  --surface: #ffffff;
  --text: #1c1b29;
  --text-dim: #6b6880;
  --border: #e6e3f5;
  --danger: #e2574c;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131220;
    --surface: #1d1c2e;
    --text: #f1f0fa;
    --text-dim: #a09dbd;
    --border: #322f4a;
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}
body { min-height: 100vh; }
#app { padding-bottom: calc(72px + var(--safe-bottom)); min-height: 100vh; }

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

.container { padding: 16px; max-width: 640px; margin: 0 auto; }

/* --- Top bar --- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(12px + var(--safe-top)) 16px 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.child-switcher {
  display: flex; align-items: center; gap: 10px;
  background: none; border: none; color: inherit;
  font-size: 17px; font-weight: 700; padding: 6px 8px; border-radius: 12px;
}
.child-switcher:active { background: var(--bg); }
.child-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; color: #fff; flex-shrink: 0;
}
.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  background: var(--bg); border: none; border-radius: 50%;
  width: 38px; height: 38px; display: flex; align-items: center; justify-content: center;
  font-size: 18px; color: var(--text);
}

/* --- Bottom nav --- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  padding-bottom: var(--safe-bottom);
}
.bottom-nav a {
  flex: 1; text-decoration: none; text-align: center;
  padding: 8px 4px 6px; font-size: 11px; color: var(--text-dim);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.bottom-nav a .nav-icon { font-size: 21px; }
.bottom-nav a.active { color: var(--accent); font-weight: 700; }

/* --- Cards / sections --- */
.card {
  background: var(--surface); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px;
  box-shadow: 0 1px 3px rgba(20,16,60,0.06);
}
.section-title {
  font-size: 13px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--text-dim); margin: 20px 4px 10px;
}
.section-title:first-child { margin-top: 4px; }

/* --- Quick-add grid --- */
.quick-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
}
@media (max-width: 380px) { .quick-grid { grid-template-columns: repeat(3, 1fr); } }
.quick-btn {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px 4px 10px; display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; color: var(--text);
}
.quick-btn:active { background: var(--bg); }
.quick-btn .qb-emoji { font-size: 26px; }

/* --- Summary rows --- */
.summary-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 4px; border-bottom: 1px solid var(--border);
}
.summary-row:last-child { border-bottom: none; }
.summary-emoji {
  width: 40px; height: 40px; border-radius: 12px; background: var(--bg);
  display: flex; align-items: center; justify-content: center; font-size: 20px; flex-shrink: 0;
}
.summary-main { flex: 1; min-width: 0; }
.summary-title { font-weight: 700; font-size: 14px; }
.summary-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 1px; }
.summary-count {
  font-size: 12px; font-weight: 700; color: var(--accent);
  background: rgba(124,111,238,0.12); padding: 3px 9px; border-radius: 999px; flex-shrink: 0;
}

/* --- Stats grid --- */
.stats-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
}
.stat-tile {
  background: var(--surface); border-radius: 14px; padding: 12px 8px; text-align: center;
  border: 1px solid var(--border);
}
.stat-value { font-size: 18px; font-weight: 800; }
.stat-label { font-size: 10.5px; color: var(--text-dim); margin-top: 2px; }
.period-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.period-tab {
  border: 1px solid var(--border); background: var(--surface); color: var(--text-dim);
  border-radius: 999px; padding: 5px 12px; font-size: 12px; font-weight: 700;
}
.period-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* --- History list --- */
.history-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}
.history-item:last-child { border-bottom: none; }
.history-main { flex: 1; min-width: 0; }
.history-time { font-weight: 700; font-size: 13.5px; }
.history-detail { font-size: 12.5px; color: var(--text-dim); margin-top: 1px; white-space: pre-line; }
.history-actions { display: flex; gap: 6px; }
.mini-btn {
  border: none; background: var(--bg); border-radius: 8px; width: 30px; height: 30px;
  font-size: 14px; color: var(--text-dim);
}

/* --- Forms --- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 700; color: var(--text-dim); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 15px; font-family: inherit;
}
.field textarea { resize: vertical; min-height: 70px; }
.seg {
  display: flex; gap: 6px; flex-wrap: wrap;
}
.seg button {
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  border-radius: 10px; padding: 8px 12px; font-size: 13px; font-weight: 600;
}
.seg button.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: 12px; padding: 13px 18px; font-size: 15px; font-weight: 700;
  background: var(--accent); color: #fff; width: 100%;
}
.btn:active { background: var(--accent-dark); }
.btn.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--danger); }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { width: auto; flex: 1; }

/* --- Modal --- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10,8,30,0.45); z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-sheet {
  background: var(--surface); width: 100%; max-width: 640px;
  border-radius: 20px 20px 0 0; padding: 18px 18px calc(18px + var(--safe-bottom));
  max-height: 88vh; overflow-y: auto;
  animation: slideUp 0.18s ease-out;
}
@keyframes slideUp { from { transform: translateY(24px); opacity: 0.6; } to { transform: translateY(0); opacity: 1; } }
.modal-handle { width: 40px; height: 4px; background: var(--border); border-radius: 999px; margin: 0 auto 14px; }
.modal-title { font-size: 18px; font-weight: 800; margin-bottom: 14px; }

/* --- Toast --- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(88px + var(--safe-bottom)); z-index: 200;
  background: #1c1b29; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 13.5px; font-weight: 600; box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  animation: fadeUp 2.4s ease forwards;
}
@keyframes fadeUp {
  0% { opacity: 0; transform: translate(-50%, 8px); }
  10% { opacity: 1; transform: translate(-50%, 0); }
  85% { opacity: 1; }
  100% { opacity: 0; }
}

/* --- Empty state --- */
.empty { text-align: center; padding: 40px 20px; color: var(--text-dim); }
.empty .empty-emoji { font-size: 40px; margin-bottom: 10px; }

/* --- Children list --- */
.child-row {
  display: flex; align-items: center; gap: 12px; padding: 12px 6px;
  border-bottom: 1px solid var(--border);
}
.child-row:last-child { border-bottom: none; }
.color-swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.color-swatch {
  width: 32px; height: 32px; border-radius: 50%; border: 2px solid transparent;
}
.color-swatch.active { border-color: var(--text); }
.emoji-swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.emoji-swatch {
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); font-size: 19px; display: flex; align-items: center; justify-content: center;
}
.emoji-swatch.active { border-color: var(--accent); background: rgba(124,111,238,0.12); }

/* --- Growth chart --- */
.chart-wrap { overflow-x: auto; }
canvas#growth-chart { width: 100%; height: 220px; display: block; }

.link-row { text-align: center; margin-top: 6px; }
.link-btn { background: none; border: none; color: var(--accent); font-weight: 700; font-size: 13.5px; padding: 8px; }

.install-banner {
  background: var(--surface); border: 1px dashed var(--accent);
  border-radius: 14px; padding: 12px 14px; margin-bottom: 14px; font-size: 13px;
}
.install-banner b { color: var(--accent); }

/* --- Auth screens --- */
.auth-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px 16px calc(24px + var(--safe-bottom)); padding-top: calc(24px + var(--safe-top));
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px;
  padding: 28px 24px; box-shadow: 0 4px 24px rgba(20,16,60,0.08);
}
.auth-logo { font-size: 40px; text-align: center; margin-bottom: 6px; }
.auth-card h1 { text-align: center; font-size: 20px; margin: 0 0 4px; }
.auth-sub { text-align: center; color: var(--text-dim); font-size: 13px; margin: 0 0 22px; }
.auth-hint { text-align: center; color: var(--text-dim); font-size: 12px; margin: 16px 0 0; }
.auth-error {
  background: rgba(226,87,76,0.12); color: var(--danger); border-radius: 10px;
  padding: 9px 12px; font-size: 13px; margin-bottom: 14px; font-weight: 600;
}
.account-row { display: flex; align-items: center; gap: 12px; padding: 10px 4px; }
.divider { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
