/* ============ Design tokens ============ */
:root {
  --bg: #f6f4f1;
  --bg-elevated: #ffffff;
  --card: #ffffff;
  --card-border: rgba(20, 20, 25, 0.06);
  --text: #201d1a;
  --text-dim: #706a63;
  --text-faint: #a39d95;
  --shadow-sm: 0 1px 2px rgba(30, 24, 18, 0.06), 0 1px 1px rgba(30, 24, 18, 0.04);
  --shadow-md: 0 8px 24px rgba(30, 24, 18, 0.08), 0 2px 6px rgba(30, 24, 18, 0.05);
  --shadow-lg: 0 20px 48px rgba(30, 24, 18, 0.16), 0 6px 16px rgba(30, 24, 18, 0.08);
  --radius-lg: 28px;
  --radius-md: 18px;
  --radius-sm: 12px;
  --radius-pill: 999px;
  --nav-h: 76px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --coral: #ff6b57;
  --coral-soft: #ffe4de;
  --coral-dark: #d94a37;
  --teal: #0eb8a6;
  --teal-soft: #d7f5f0;
  --teal-dark: #0a8d80;
  --violet: #8b6bf2;
  --violet-soft: #e9e1fd;
  --violet-dark: #6a48d6;
  --amber: #f5a524;
  --amber-soft: #fdecc9;
  --amber-dark: #c9820e;
  --sky: #3aa9e0;
  --sky-soft: #dcf1fb;
  --sky-dark: #227bab;
  --rose: #f2568f;
  --rose-soft: #fde0eb;
  --rose-dark: #c53870;

  --good: #2fb473;
  --bad: #e5533f;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131211;
    --bg-elevated: #1c1a18;
    --card: #201e1b;
    --card-border: rgba(255, 255, 255, 0.07);
    --text: #f3efe9;
    --text-dim: #b3aba1;
    --text-faint: #7c766e;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 20px 48px rgba(0, 0, 0, 0.55);
  }
}
:root[data-theme="dark"] {
  --bg: #131211;
  --bg-elevated: #1c1a18;
  --card: #201e1b;
  --card-border: rgba(255, 255, 255, 0.07);
  --text: #f3efe9;
  --text-dim: #b3aba1;
  --text-faint: #7c766e;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 20px 48px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
  padding-top: var(--safe-top);
}
img { max-width: 100%; }
[hidden] { display: none !important; }
button { font-family: inherit; }

.app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100%;
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
}

/* ============ Header ============ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 8px;
}
.topbar h1 {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin: 0;
}
.topbar .subtitle {
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 500;
  margin-top: 2px;
}
.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-pill);
  border: none;
  background: var(--card);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg { width: 20px; height: 20px; }

/* ============ Screens ============ */
.screen { padding: 4px 16px 8px; }
.screen[hidden] { display: none; }

/* ============ Kid card ============ */
.kid-card {
  --accent: var(--coral);
  --accent-soft: var(--coral-soft);
  --accent-dark: var(--coral-dark);
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-lg);
  padding: 22px 20px 20px;
  margin-bottom: 18px;
  box-shadow: var(--shadow-md);
  position: relative;
  overflow: hidden;
  transition: box-shadow 0.2s ease;
}
.kid-card[data-color="coral"] { --accent: var(--coral); --accent-soft: var(--coral-soft); --accent-dark: var(--coral-dark); }
.kid-card[data-color="teal"] { --accent: var(--teal); --accent-soft: var(--teal-soft); --accent-dark: var(--teal-dark); }
.kid-card[data-color="violet"] { --accent: var(--violet); --accent-soft: var(--violet-soft); --accent-dark: var(--violet-dark); }
.kid-card[data-color="amber"] { --accent: var(--amber); --accent-soft: var(--amber-soft); --accent-dark: var(--amber-dark); }
.kid-card[data-color="sky"] { --accent: var(--sky); --accent-soft: var(--sky-soft); --accent-dark: var(--sky-dark); }
.kid-card[data-color="rose"] { --accent: var(--rose); --accent-soft: var(--rose-soft); --accent-dark: var(--rose-dark); }

.kid-card.shake { animation: shake 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97); }
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-5px); }
  40%, 60% { transform: translateX(5px); }
}

.kid-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.kid-name {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: -0.01em;
}
.kid-badge {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
  margin-right: 8px;
  box-shadow: 0 0 0 4px var(--accent-soft);
}
.kid-name-row { display: flex; align-items: center; }

.timer-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-weight: 700;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.timer-pill .dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent-dark);
}
.timer-pill.running .dot { animation: pulse-dot 1.1s ease-in-out infinite; }
@keyframes pulse-dot { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

.ring-wrap {
  display: flex;
  justify-content: center;
  margin: 6px 0 18px;
  position: relative;
}
.ring-svg { transform: rotate(-90deg); }
.ring-track { fill: none; stroke: var(--accent-soft); stroke-width: 14; }
.ring-fill {
  fill: none;
  stroke: var(--accent);
  stroke-width: 14;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.55s cubic-bezier(0.22, 1, 0.36, 1);
}
.ring-fill.at-cap { stroke: var(--good); }
.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.ring-minutes {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  transition: transform 0.25s ease;
}
.ring-minutes.bump { transform: scale(1.12); }
.ring-cap-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-dim);
  margin-top: 5px;
}

.stepper-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin-bottom: 16px;
}
.step-btn {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-pill);
  border: none;
  font-size: 30px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  color: #fff;
}
.step-btn:active { transform: scale(0.9); box-shadow: var(--shadow-sm); }
.step-btn.plus { background: linear-gradient(155deg, var(--accent), var(--accent-dark)); }
.step-btn.minus { background: var(--card); color: var(--text-dim); border: 1.5px solid var(--card-border); }
.step-btn.minus:active { color: var(--bad); }
.step-label { font-size: 12px; font-weight: 700; color: var(--text-faint); text-align: center; width: 64px; margin-top: 4px; }
.step-col { display: flex; flex-direction: column; align-items: center; }

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
}
.chip {
  border: 1.5px solid var(--card-border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: var(--radius-pill);
  padding: 9px 14px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease;
  white-space: nowrap;
}
.chip:active { transform: scale(0.95); }
.chip.add { color: var(--good); }
.chip.subtract { color: var(--bad); }
.chip.custom { border-style: dashed; }

.timer-strip {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
  border: 1px solid var(--card-border);
  display: flex;
  align-items: center;
  gap: 12px;
}
.timer-time {
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  min-width: 76px;
}
.timer-controls { display: flex; gap: 8px; margin-left: auto; }
.timer-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.timer-btn.secondary { background: var(--card); color: var(--text-dim); border: 1.5px solid var(--card-border); }
.timer-btn:active { transform: scale(0.92); }
.timer-btn svg { width: 16px; height: 16px; }
.timer-label { font-size: 12.5px; font-weight: 700; color: var(--text-dim); }

/* ============ Confetti ============ */
.confetti-canvas { position: fixed; inset: 0; pointer-events: none; z-index: 500; }

/* ============ Bottom nav ============ */
.bottom-nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-elevated);
  border-top: 1px solid var(--card-border);
  display: flex;
  z-index: 100;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.05);
}
.nav-btn {
  flex: 1;
  border: none;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--text-faint);
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
}
.nav-btn svg { width: 24px; height: 24px; }
.nav-btn.active { color: var(--coral); }
.nav-btn:active { opacity: 0.6; }

/* ============ Weekly ============ */
.week-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-lg);
  padding: 20px 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-md);
}
.week-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.week-name { font-size: 17px; font-weight: 800; display: flex; align-items: center; }
.week-total { font-size: 13px; color: var(--text-dim); font-weight: 600; }
.bars {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  height: 120px;
  gap: 8px;
}
.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
}
.bar-stack {
  width: 100%;
  max-width: 30px;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-start;
  height: 100%;
  border-radius: 7px;
  overflow: hidden;
  background: var(--bg-elevated);
}
.bar-earn { width: 100%; background: var(--accent, var(--good)); }
.bar-lost { width: 100%; background: var(--bad); opacity: 0.65; }
.bar-day-label { font-size: 11px; color: var(--text-faint); font-weight: 700; margin-top: 8px; }
.bar-day-label.today { color: var(--text); }

/* ============ Settings ============ */
.section-title {
  font-size: 13px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin: 22px 4px 10px;
}
.settings-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--card-border);
  gap: 10px;
}
.settings-row:last-child { border-bottom: none; }
.settings-row label { font-weight: 600; font-size: 14.5px; }
.settings-row .hint { font-size: 12px; color: var(--text-faint); margin-top: 2px; }
input[type="text"], input[type="number"], input[type="password"] {
  font-family: inherit;
  font-size: 15px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--card-border);
  background: var(--bg-elevated);
  color: var(--text);
  width: 100%;
}
input:focus { outline: 2px solid var(--coral); outline-offset: 1px; }
input.field-sm { width: 76px; text-align: center; }
.swatch-row { display: flex; gap: 10px; padding: 12px 16px; }
.swatch {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
}
.swatch.selected { border-color: var(--text); }
.btn {
  border: none;
  border-radius: var(--radius-pill);
  padding: 13px 20px;
  font-weight: 700;
  font-size: 14.5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn.primary { background: var(--text); color: var(--bg); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.ghost { background: var(--bg-elevated); color: var(--text); border: 1.5px solid var(--card-border); }
.btn:active { transform: scale(0.97); }
.btn-row { display: flex; gap: 10px; padding: 14px 4px 6px; }
.btn.block { width: 100%; }

.reason-row { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--card-border); }
.reason-row:last-child { border-bottom: none; }
.reason-dir { font-size: 11px; font-weight: 800; padding: 4px 8px; border-radius: 8px; }
.reason-dir.add { color: var(--good); background: rgba(47,180,115,0.12); }
.reason-dir.subtract { color: var(--bad); background: rgba(229,83,63,0.12); }
.reason-row input { flex: 1; }
.reason-row input.field-sm { flex: none; }
.reason-dir { white-space: nowrap; }
.icon-btn.small { width: 34px; height: 34px; }
.icon-btn.small svg { width: 16px; height: 16px; }

/* ============ Login ============ */
.login-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  text-align: center;
}
.login-logo {
  width: 84px; height: 84px;
  border-radius: 26px;
  background: linear-gradient(155deg, var(--coral), var(--violet));
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 22px;
  box-shadow: var(--shadow-lg);
}
.login-logo svg { width: 42px; height: 42px; color: #fff; }
.login-screen h2 { font-size: 24px; font-weight: 800; margin: 0 0 6px; }
.login-screen p { color: var(--text-dim); margin: 0 0 28px; font-size: 14.5px; }
.pin-dots { display: flex; gap: 14px; margin-bottom: 26px; }
.pin-dot {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--text-faint);
  transition: background 0.15s ease, transform 0.15s ease, border-color 0.15s ease;
}
.pin-dot.filled { background: var(--coral); border-color: var(--coral); transform: scale(1.1); }
.pin-dots.error .pin-dot { border-color: var(--bad); }
.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  width: 100%;
  max-width: 280px;
}
.pin-key {
  aspect-ratio: 1;
  border-radius: 50%;
  border: none;
  background: var(--card);
  box-shadow: var(--shadow-sm);
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
}
.pin-key:active { transform: scale(0.92); background: var(--bg-elevated); }
.pin-key.func { font-size: 15px; color: var(--text-dim); font-weight: 700; }
.who-row { display: flex; gap: 12px; margin-bottom: 24px; width: 100%; max-width: 280px; }
.who-btn {
  flex: 1;
  padding: 13px 10px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--card-border);
  background: var(--card);
  font-weight: 700;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
}
.who-btn.selected { border-color: var(--coral); color: var(--coral); background: var(--coral-soft); }

/* ============ Modal ============ */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(10, 8, 6, 0.5);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 300;
  animation: fade-in 0.18s ease;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.modal-sheet {
  background: var(--bg);
  width: 100%;
  max-width: 640px;
  border-radius: 24px 24px 0 0;
  padding: 10px 20px calc(24px + var(--safe-bottom));
  max-height: 82vh;
  overflow-y: auto;
  animation: slide-up 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes slide-up { from { transform: translateY(24px); opacity: 0.6; } to { transform: translateY(0); opacity: 1; } }
.modal-handle { width: 40px; height: 5px; border-radius: 3px; background: var(--card-border); margin: 8px auto 16px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h3 { font-size: 18px; font-weight: 800; margin: 0; }

.log-entry {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--card-border);
}
.log-entry:last-child { border-bottom: none; }
.log-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.log-main { flex: 1; min-width: 0; }
.log-reason { font-weight: 700; font-size: 14px; }
.log-meta { font-size: 12px; color: var(--text-faint); margin-top: 1px; }
.log-delta { font-weight: 800; font-size: 14.5px; font-variant-numeric: tabular-nums; }
.log-delta.pos { color: var(--good); }
.log-delta.neg { color: var(--bad); }
.log-entry.undone { opacity: 0.4; text-decoration: line-through; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-faint); }
.empty-state svg { width: 48px; height: 48px; margin-bottom: 10px; opacity: 0.5; }

.toast {
  position: fixed;
  left: 50%; bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px);
  transform: translateX(-50%) translateY(10px);
  background: var(--text);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: var(--radius-pill);
  font-weight: 700;
  font-size: 13.5px;
  box-shadow: var(--shadow-lg);
  z-index: 400;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  max-width: 90%;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
