/* ── Tokens ───────────────────────────────────────────────────────────
   Light palette is complete on bare :root. Dark redefines only tokens,
   once for prefers-color-scheme and once for the explicit stamp. */
:root {
  --backdrop:   #dfe3de;
  --ground:     #eff1ed;
  --surface:    #ffffff;
  --surface-2:  #f4f6f2;
  --surface-3:  #e9ece7;

  --ink:        #14181a;
  --ink-2:      #5d666b;
  --ink-3:      #929a9e;
  --rule:       #dfe3dd;
  --rule-soft:  #ebeee9;

  --kid-blue:        #1f5fc0;
  --kid-blue-wash:   #e8effb;
  --kid-green:       #0d7a61;
  --kid-green-wash:  #e3f2ed;
  --kid-amber:       #8a5a06;
  --kid-amber-wash:  #fbf0da;
  --kid-purple:      #6a3fb5;
  --kid-purple-wash: #efe9fb;
  --on-accent:  #ffffff;

  --neg:        #ad3a2b;

  --shadow: 0 1px 2px rgba(20,24,26,.05), 0 8px 24px rgba(20,24,26,.06);
  --shadow-sheet: 0 -8px 40px rgba(20,24,26,.18);

  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-num: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, sans-serif;

  --r-card: 20px;
  --r-btn: 16px;
  --dur: 220ms;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --backdrop:  #0a0c0d;
    --ground:    #101315;
    --surface:   #191d20;
    --surface-2: #21262a;
    --surface-3: #2a3035;

    --ink:       #edf0f1;
    --ink-2:     #99a2a7;
    --ink-3:     #6c757a;
    --rule:      #2b3237;
    --rule-soft: #232a2e;

    --kid-blue:       #74a8f7;
    --kid-blue-wash:  #16233a;
    --kid-green:      #43c3a2;
    --kid-green-wash: #102b26;
    --kid-amber:      #e0a94a;
    --kid-amber-wash: #33260f;
    --kid-purple:     #b191f0;
    --kid-purple-wash:#241a3a;
    --on-accent: #0b0e10;

    --neg:       #ec7d68;

    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.32);
    --shadow-sheet: 0 -8px 40px rgba(0,0,0,.5);
  }
}

:root[data-theme="dark"] {
  --backdrop:  #0a0c0d;
  --ground:    #101315;
  --surface:   #191d20;
  --surface-2: #21262a;
  --surface-3: #2a3035;

  --ink:       #edf0f1;
  --ink-2:     #99a2a7;
  --ink-3:     #6c757a;
  --rule:      #2b3237;
  --rule-soft: #232a2e;

  --kid-blue:       #74a8f7;
  --kid-blue-wash:  #16233a;
  --kid-green:      #43c3a2;
  --kid-green-wash: #102b26;
  --kid-amber:      #e0a94a;
  --kid-amber-wash: #33260f;
  --kid-purple:     #b191f0;
  --kid-purple-wash:#241a3a;
  --on-accent: #0b0e10;

  --neg:       #ec7d68;

  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.32);
  --shadow-sheet: 0 -8px 40px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--backdrop);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button {
  font: inherit; color: inherit; border: 0; background: none; cursor: pointer;
  -webkit-touch-callout: none; -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
}

:focus-visible { outline: 2px solid var(--kid-blue); outline-offset: 2px; border-radius: 6px; }

[hidden] { display: none !important; }

/* ── Stage & phone frame ──────────────────────────────────────────── */
.stage { min-height: 100dvh; display: flex; justify-content: center; align-items: stretch; }

.phone {
  position: relative; display: flex; flex-direction: column;
  width: 100%; max-width: 430px; min-height: 100dvh;
  background: var(--ground); overflow: hidden;
}

@media (min-width: 620px) {
  .stage { align-items: center; padding: 28px 20px; }
  .phone {
    min-height: 0; height: min(900px, calc(100dvh - 56px));
    border-radius: 38px; border: 1px solid var(--rule);
    box-shadow: 0 30px 70px rgba(0,0,0,.22);
  }
}

/* ── Sign in ──────────────────────────────────────────────────────── */
.signin {
  flex: 1; display: flex; flex-direction: column; justify-content: center;
  gap: 10px; padding: 32px 24px calc(32px + env(safe-area-inset-bottom));
  text-align: center;
}
.signin h1 { margin: 0; font-size: 30px; letter-spacing: -0.03em; }
.signin p { margin: 0 0 20px; color: var(--ink-2); font-size: 15px; }
.signin button {
  height: 56px; border-radius: var(--r-btn);
  background: var(--kid-blue); color: var(--on-accent); font-weight: 700; font-size: 17px;
}
.signin button:disabled { opacity: .45; }
.signin .msg { min-height: 1.4em; font-size: 14px; color: var(--ink-2); }
.signin .msg.err { color: var(--neg); }

/* ── App bar ──────────────────────────────────────────────────────── */
.appbar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 10px;
  padding: calc(14px + env(safe-area-inset-top)) 18px 12px;
  background: var(--ground); border-bottom: 1px solid var(--rule-soft); z-index: 5;
}
.appbar h1 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -0.01em; flex: 1; }

.backbtn {
  display: flex; align-items: center; gap: 3px; margin-left: -6px;
  padding: 6px 8px 6px 4px; font-size: 16px; font-weight: 600;
  color: var(--ink-2); border-radius: 10px;
}
.backbtn:active { background: var(--surface-3); }

.iconbtn {
  width: 32px; height: 32px; display: grid; place-items: center; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--rule); font-size: 14px; color: var(--ink-2);
}
.iconbtn.is-busy { opacity: .5; }

.whoami {
  display: flex; align-items: center; gap: 7px; padding: 5px 11px 5px 6px;
  border-radius: 999px; background: var(--surface); border: 1px solid var(--rule);
  font-size: 13px; font-weight: 600; color: var(--ink-2);
}
.whoami .dot {
  width: 20px; height: 20px; display: grid; place-items: center; border-radius: 50%;
  background: var(--ink); color: var(--ground); font-size: 11px; font-weight: 700;
}

/* ── Scroll region ────────────────────────────────────────────────── */
.scroll {
  flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(28px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}

/* ── Kid card ─────────────────────────────────────────────────────── */
.card {
  background: var(--surface); border-radius: var(--r-card); box-shadow: var(--shadow);
  padding: 16px 16px 14px; display: flex; flex-direction: column; gap: 12px;
  margin-bottom: 14px;
}

.kid-head { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; padding: 0; }

.avatar {
  width: 34px; height: 34px; display: grid; place-items: center;
  border-radius: 11px; font-size: 19px; background: var(--wash);
}

.kid-name {
  flex: 1; font-size: 13px; font-weight: 700; letter-spacing: .09em;
  text-transform: uppercase; color: var(--ink-2);
}
.chevron { color: var(--ink-3); font-size: 17px; }

.balance {
  font-family: var(--font-num); font-size: 76px; line-height: .92; font-weight: 700;
  letter-spacing: -0.035em; font-variant-numeric: tabular-nums;
  color: var(--accent); text-align: center; padding: 2px 0 4px;
}
.balance.is-negative { color: var(--neg); }

.balance-unit {
  display: block; font-family: var(--font-ui); font-size: 11px; font-weight: 700;
  letter-spacing: .12em; text-transform: uppercase; color: var(--ink-3); margin-top: 6px;
}

.rewards-brief {
  display: flex; flex-direction: column; gap: 9px;
  padding: 11px 12px; border-radius: 14px; background: var(--surface-2);
}
.brief-row { display: flex; align-items: center; gap: 8px; }
.brief-label {
  flex: 0 0 auto; font-size: 10px; font-weight: 700; letter-spacing: .1em;
  text-transform: uppercase; color: var(--ink-3); width: 52px;
}
.chips { display: flex; flex-wrap: wrap; gap: 5px; flex: 1; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 4px 9px;
  border-radius: 999px; background: var(--wash); color: var(--accent);
  font-size: 12.5px; font-weight: 650; white-space: nowrap;
}
.chip.is-locked { background: transparent; border: 1px dashed var(--rule); color: var(--ink-2); font-weight: 600; }
.togo { font-size: 12.5px; font-weight: 650; color: var(--ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar { flex: 1; height: 5px; min-width: 40px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; background: var(--accent); transition: width var(--dur) ease; }
.brief-empty { font-size: 12.5px; color: var(--ink-3); }

.actions { display: flex; gap: 9px; }
.act {
  flex: 1; height: 56px; border-radius: var(--r-btn); font-size: 22px; font-weight: 700;
  display: grid; place-items: center; transition: transform 90ms ease;
}
.act:active { transform: scale(.96); }
.act:disabled { opacity: .5; }
.act-minus { background: var(--surface-2); color: var(--neg); flex: 0 0 92px; }
.act-plus { background: var(--accent); color: var(--on-accent); }

/* ── Sections & feed ──────────────────────────────────────────────── */
.section-head { display: flex; align-items: baseline; justify-content: space-between; padding: 6px 4px 0; }
.section-head h2 {
  margin: 0; font-size: 11px; font-weight: 700; letter-spacing: .11em;
  text-transform: uppercase; color: var(--ink-3);
}

.feed, .reward-list {
  background: var(--surface); border-radius: var(--r-card); box-shadow: var(--shadow); overflow: hidden;
}

.ev {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 12px 15px; border-top: 1px solid var(--rule-soft);
}
.ev:first-child { border-top: 0; }
.ev:active { background: var(--surface-2); }

.ev-delta {
  flex: 0 0 46px; font-family: var(--font-num); font-size: 20px; font-weight: 700;
  font-variant-numeric: tabular-nums; letter-spacing: -0.02em; color: var(--accent);
}
.ev-delta.is-neg { color: var(--neg); }

.ev-body { flex: 1; min-width: 0; }
.ev-title { font-size: 15px; font-weight: 600; line-height: 1.25; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ev-meta { font-size: 12.5px; color: var(--ink-3); margin-top: 2px; }
.ev-who { color: var(--ink-2); font-weight: 600; }

.day-sep {
  padding: 12px 15px 6px; font-size: 10.5px; font-weight: 700; letter-spacing: .1em;
  text-transform: uppercase; color: var(--ink-3);
  border-top: 1px solid var(--rule-soft); background: var(--surface-2);
}
.day-sep:first-child { border-top: 0; }

.empty { padding: 26px 18px; text-align: center; color: var(--ink-3); font-size: 14px; }

/* ── Detail ───────────────────────────────────────────────────────── */
.hero {
  background: var(--surface); border-radius: var(--r-card); box-shadow: var(--shadow);
  padding: 20px 16px 16px; display: flex; flex-direction: column; gap: 16px; align-items: center;
}
.hero .avatar { width: 52px; height: 52px; border-radius: 16px; font-size: 30px; }
.hero .balance { font-size: 96px; }
.hero .actions { width: 100%; }

.rw { display: flex; align-items: center; gap: 12px; padding: 13px 15px; border-top: 1px solid var(--rule-soft); }
.rw:first-child { border-top: 0; }
.rw-emoji { font-size: 24px; flex: 0 0 auto; }
.rw-body { flex: 1; min-width: 0; }
.rw-name { font-size: 15px; font-weight: 600; }
.rw-sub {
  display: flex; align-items: center; gap: 8px; margin-top: 5px;
  font-size: 12.5px; color: var(--ink-3); font-variant-numeric: tabular-nums;
}
.rw-cost { font-family: var(--font-num); font-size: 17px; font-weight: 700; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.redeem {
  padding: 8px 14px; border-radius: 999px; background: var(--accent);
  color: var(--on-accent); font-size: 13.5px; font-weight: 700; white-space: nowrap;
}
.redeem:disabled { opacity: .5; }

/* ── Sheets ───────────────────────────────────────────────────────── */
.backdrop {
  position: absolute; inset: 0; background: rgba(8,11,12,.45);
  opacity: 0; pointer-events: none; transition: opacity var(--dur) ease; z-index: 20;
}
.backdrop.is-open { opacity: 1; pointer-events: auto; }

.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 21;
  background: var(--surface); border-radius: 26px 26px 0 0; box-shadow: var(--shadow-sheet);
  transform: translateY(101%); transition: transform var(--dur) cubic-bezier(.32,.72,0,1);
  max-height: 88%; display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.is-open { transform: translateY(0); }

.grabber { width: 38px; height: 4px; border-radius: 999px; background: var(--rule); margin: 9px auto 0; flex: 0 0 auto; }

.sheet-title {
  padding: 13px 20px 11px; font-size: 16px; font-weight: 700; text-align: center;
  border-bottom: 1px solid var(--rule-soft); flex: 0 0 auto;
}
.sheet-title small { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink-3); margin-top: 3px; }

.sheet-body { overflow-y: auto; overscroll-behavior: contain; }

.preset {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  padding: 14px 20px; border-top: 1px solid var(--rule-soft);
}
.preset:first-child { border-top: 0; }
.preset:active { background: var(--surface-2); }
.preset-delta {
  flex: 0 0 52px; font-family: var(--font-num); font-size: 22px; font-weight: 700;
  font-variant-numeric: tabular-nums; letter-spacing: -0.02em; color: var(--accent);
}
.preset-delta.is-neg { color: var(--neg); }
.preset-label { font-size: 16px; font-weight: 550; }

.sheet-foot { border-top: 1px solid var(--rule); padding: 6px 0 8px; flex: 0 0 auto; }
.linkish { display: block; width: 100%; padding: 13px 20px; font-size: 15.5px; font-weight: 600; color: var(--ink-2); text-align: center; }
.linkish:active { background: var(--surface-2); }
.linkish.is-danger { color: var(--neg); }

/* ── Keypad ───────────────────────────────────────────────────────── */
.keypad-amount {
  font-family: var(--font-num); font-size: 62px; font-weight: 700; text-align: center;
  padding: 14px 0 4px; color: var(--accent); font-variant-numeric: tabular-nums; letter-spacing: -0.03em;
}
.keypad-amount.is-neg { color: var(--neg); }

.reason-input {
  display: block; width: calc(100% - 40px); margin: 4px 20px 12px; padding: 12px 14px;
  font-family: var(--font-ui);
  font-size: 17px; /* >=16px, or iOS zooms in and never zooms back out */
  color: var(--ink); background: var(--surface-2);
  border: 1px solid var(--rule); border-radius: 12px;
}
.reason-input::placeholder { color: var(--ink-3); }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 0 16px 12px; }
.key {
  height: 54px; border-radius: 14px; background: var(--surface-2);
  font-family: var(--font-num); font-size: 25px; font-weight: 600;
  font-variant-numeric: tabular-nums; transition: transform 90ms ease;
}
.key:active { transform: scale(.94); background: var(--surface-3); }

.confirm {
  margin: 0 16px 14px; height: 54px; border-radius: var(--r-btn);
  background: var(--accent); color: var(--on-accent); font-size: 17px; font-weight: 700;
}
.confirm.is-neg { background: var(--neg); color: #fff; }
.confirm:disabled { opacity: .35; }

/* ── Toast ────────────────────────────────────────────────────────── */
.toast {
  position: absolute; left: 12px; right: 12px;
  bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 30;
  background: var(--ink); color: var(--ground); border-radius: 16px;
  box-shadow: var(--shadow-sheet); overflow: hidden;
  transform: translateY(140%); transition: transform 260ms cubic-bezier(.32,.72,0,1);
}
.toast.is-open { transform: translateY(0); }
.toast.is-error { background: var(--neg); color: #fff; }

.toast-inner { display: flex; align-items: center; gap: 12px; padding: 13px 8px 13px 16px; }
.toast-text { flex: 1; font-size: 14.5px; font-weight: 600; }
.toast-text b { font-family: var(--font-num); font-weight: 700; }
.toast-undo {
  padding: 8px 14px; font-size: 14.5px; font-weight: 700; color: inherit;
  border-radius: 10px; text-decoration: underline; text-underline-offset: 3px;
}
.toast-bar { height: 3px; background: rgba(255,255,255,.25); }
.toast-bar > i { display: block; height: 100%; width: 100%; background: currentColor; transform-origin: left center; }
@keyframes drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }
.toast.is-open .toast-bar > i { animation: drain 10s linear forwards; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
  .toast.is-open .toast-bar > i { animation: drain 10s linear forwards !important; }
}

/* ── Settings ─────────────────────────────────────────────────────── */
.set-row {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 13px 15px; border-top: 1px solid var(--rule-soft);
}
.set-row:first-child { border-top: 0; }
.set-row:active { background: var(--surface-2); }
.set-row.is-retired { opacity: .45; }

.set-delta {
  flex: 0 0 46px; font-family: var(--font-num); font-size: 18px; font-weight: 700;
  font-variant-numeric: tabular-nums; color: var(--ink-2);
}
.set-delta.is-neg { color: var(--neg); }
.set-label { flex: 1; font-size: 15px; font-weight: 600; }
.set-note { font-size: 12.5px; color: var(--ink-3); }

.add-row {
  display: block; width: 100%; padding: 13px 15px; text-align: center;
  font-size: 15px; font-weight: 650; color: var(--kid-blue);
  border-top: 1px solid var(--rule-soft);
}
.add-row:active { background: var(--surface-2); }

.field-label {
  display: block; padding: 12px 20px 0; font-size: 11px; font-weight: 700;
  letter-spacing: .1em; text-transform: uppercase; color: var(--ink-3);
}

.swatches { display: flex; gap: 10px; padding: 8px 20px 4px; }
.swatch {
  width: 40px; height: 40px; border-radius: 12px; border: 2px solid transparent;
}
.swatch[aria-pressed="true"] { border-color: var(--ink); }
