:root {
  --bg: #fff6e6;
  --bg-deep: #ffe7bf;
  --skin: #ffb938;
  --skin-dark: #f29a1d;
  --belly: #ffe2a3;
  --tuft: #4caf50;
  --ink: #3b2414;
  --mouth: #7a1f1f;
  --tongue: #ef6f6c;
  --cheek: #ff8f8f;
  --ring: #ffcf70;
  --panel: #ffffff;
  --muted: #7b6a5c;
  --line: #eadfce;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(circle at 50% 45%, var(--bg) 0%, var(--bg-deep) 100%);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Noto Sans Devanagari", sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

#stage { height: 100dvh; display: grid; place-items: center; padding: 16px; }

#mascot {
  border: 0; background: none; padding: 0; cursor: pointer;
  width: min(78vw, 62dvh, 460px);
  touch-action: manipulation;
}
#mascot svg { width: 100%; display: block; overflow: visible; }

.shadow { fill: rgba(59, 36, 20, 0.12); }
.skin { fill: var(--skin); stroke: var(--skin-dark); stroke-width: 4; }
.belly { fill: var(--belly); }
.tuft { fill: var(--tuft); }
.white { fill: #fff; }
.pupil circle { fill: var(--ink); }
.pupil .glint { fill: #fff; }
.cheek { fill: var(--cheek); opacity: 0.45; }
#smile, #closed-eyes path { fill: none; stroke: var(--ink); stroke-width: 6; stroke-linecap: round; }
#mouth .inside { fill: var(--mouth); }
#mouth .tongue { fill: var(--tongue); }
#ring { fill: var(--ring); opacity: 0; transform-origin: 150px 170px; transition: opacity 0.3s; }
#zzz text { font: 700 30px system-ui, sans-serif; fill: var(--muted); opacity: 0; }

#body { transform-origin: 150px 288px; }
#eyes .eye > * { transform-box: fill-box; transform-origin: center; transition: transform 0.08s; }
#eyes.blink .white, #eyes.blink .pupil { transform: scaleY(0.08); }

/* State: asleep. Tap wakes it. */
[data-state="sleep"] #eyes, [data-state="sleep"] #mouth { display: none; }
[data-state="sleep"] #body { animation: breathe 3.2s ease-in-out infinite; }
[data-state="sleep"] #zzz text { animation: zzz 2.6s ease-in-out infinite; }
[data-state="sleep"] #zzz text + text { animation-delay: 1.3s; }
#closed-eyes { display: none; }
[data-state="sleep"] #closed-eyes { display: inline; }

/* State: connecting / thinking. */
[data-state="waking"] #body, [data-state="thinking"] #body { animation: sway 1.4s ease-in-out infinite; }
[data-state="thinking"] .pupil { transform: translate(6px, -9px); }

/* State: listening. The ring grows with the child's voice. */
[data-state="listening"] #ring { opacity: 0.55; }
[data-state="listening"] .pupil { transform: translateY(2px) scale(1.08); }

/* Mouth: a smile at rest, an open mouth while speaking. */
#mouth { display: none; }
[data-state="speaking"] #mouth { display: inline; }
[data-state="speaking"] #smile { display: none; }
[data-state="speaking"] #body { animation: bob 0.9s ease-in-out infinite; }

@keyframes breathe { 50% { transform: scale(1.03, 0.97); } }
@keyframes sway { 25% { transform: rotate(-3deg); } 75% { transform: rotate(3deg); } }
@keyframes bob { 50% { transform: translateY(-4px); } }
@keyframes zzz { 0% { opacity: 0; transform: translateY(8px); } 40% { opacity: 1; } 100% { opacity: 0; transform: translateY(-14px); } }

/* Parent controls: a faint corner button that opens on a one-second hold. */
#parent-open {
  position: fixed; top: max(12px, env(safe-area-inset-top)); right: 12px;
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: rgba(59, 36, 20, 0.06); color: var(--muted); font-size: 20px; opacity: 0.6;
}
#parent-open.holding { background: rgba(59, 36, 20, 0.2); transition: background 1s; }

#parent {
  width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  border: 0; border-radius: 16px; padding: 20px; background: var(--panel); color: var(--ink);
  user-select: text; -webkit-user-select: text;
}
#parent::backdrop { background: rgba(30, 18, 8, 0.4); }
#parent .head { display: flex; justify-content: space-between; align-items: center; margin: 0; }
#parent .head button { border: 0; background: none; font-size: 20px; cursor: pointer; }
#parent h2 { margin: 0; font-size: 20px; }
#parent h3 { margin: 20px 0 8px; font-size: 15px; }
#parent fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 16px 0 12px; padding: 8px 12px; }
#parent fieldset label { display: block; padding: 6px 0; }
#parent fieldset span { color: var(--muted); font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; }
#parent select, #stop { font: inherit; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: #fff; }
#log { margin: 0; padding: 0 0 0 4px; list-style: none; font-size: 15px; line-height: 1.5; }
#log li { padding: 6px 0; border-bottom: 1px solid var(--line); }
#log .who { font-weight: 600; margin-right: 6px; }
#log .meta { color: var(--muted); font-size: 12px; margin-left: 6px; white-space: nowrap; }
#log .sys { color: var(--muted); font-size: 13px; }
#log .err { color: #b3261e; font-size: 13px; }

/* It heard a sound while talking and got quieter to listen: the ring shows faintly. */
[data-heard] #ring { opacity: 0.35; }
