:root {
  color-scheme: only light;
  --ink: #24160f;
  --cream: #fff6e4;
  --card: #fffdf8;
  --line: #e7c39a;
  --mic: #c9182b;
  --mic-deep: #8e0f1d;
  --listen: #ffd23f;
  --good: #1f7a45;
  --muted: #6b625b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  min-height: 100dvh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding:
    calc(0.7rem + env(safe-area-inset-top))
    calc(1rem + env(safe-area-inset-right))
    calc(0.8rem + env(safe-area-inset-bottom))
    calc(1rem + env(safe-area-inset-left));
  background:
    radial-gradient(circle at 20% 0%, #ffe7a8 0%, transparent 42%),
    radial-gradient(circle at 100% 100%, #ffd0b8 0%, transparent 46%),
    var(--cream);
  color: var(--ink);
  font-family: ui-rounded, "Avenir Next", "Trebuchet MS", sans-serif;
  font-size: 1.35rem;
  line-height: 1.25;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button, input, select { font: inherit; color: inherit; }

button {
  touch-action: manipulation;
  cursor: pointer;
}

[hidden] { display: none !important; }

.top {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  gap: 0.8rem;
  align-items: center;
  padding-right: 2.4rem;
  min-height: 0;
}

.character {
  width: 5.6rem;
  height: 5.6rem;
  flex: none;
}

.character canvas,
.character-fallback,
.character-fallback svg {
  width: 100%;
  height: 100%;
  display: block;
}

.character-fallback {
  border-radius: 50%;
  box-shadow: 0 0.35rem 0 #e7a15a;
}

.character.is-talking .mouth {
  transform-box: fill-box;
  transform-origin: center;
  animation: character-talk 0.28s ease-in-out infinite;
}

#face {
  width: 6.4rem;
  height: 6.4rem;
  border: 4px solid #fff;
  border-radius: 50%;
  padding: 0;
  position: relative;
  background: radial-gradient(circle at 50% 38%, #ffe38a, #ffb347 58%, #ff8a3d);
  box-shadow: 0 0.45rem 0 #e7a15a;
  flex: none;
}

#face:active { transform: translateY(0.2rem); box-shadow: 0 0.15rem 0 #e7a15a; }

.eye {
  position: absolute;
  top: 34%;
  width: 0.72rem;
  height: 0.95rem;
  background: var(--ink);
  border-radius: 50%;
}

.eye.left { left: 27%; }
.eye.right { right: 27%; }

.smile {
  position: absolute;
  left: 50%;
  top: 54%;
  width: 2.15rem;
  height: 1.05rem;
  border-bottom: 0.28rem solid var(--ink);
  border-radius: 0 0 2rem 2rem;
  transform: translateX(-50%);
}

body.speaking .smile {
  height: 1.35rem;
  border: 0.28rem solid var(--ink);
  border-radius: 50%;
  background: #ff6b6b;
  animation: talk 0.28s ease-in-out infinite;
}

.kicker {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: 0.01em;
}

.reply {
  margin: 0.25rem 0 0;
  font-size: clamp(1.8rem, 4.4vw, 3.15rem);
  font-weight: 800;
  line-height: 1.15;
  max-height: 26dvh;
  overflow: auto;
}

.hear-hint {
  margin: 0.35rem 0 0;
  font-size: 1.15rem;
  font-weight: 700;
}

#grownup {
  position: fixed;
  z-index: 5;
  top: calc(0.25rem + env(safe-area-inset-top));
  right: calc(0.25rem + env(safe-area-inset-right));
  width: 2.75rem;
  height: 2.75rem;
  border: 0;
  padding: 0;
  background: transparent;
  display: grid;
  place-items: center;
}

#grownup span {
  width: 0.72rem;
  height: 0.72rem;
  border-radius: 50%;
  background: rgba(36, 22, 15, 0.38);
}

.note {
  margin: 0;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 1rem;
  padding: 0.55rem 0.8rem;
  font-weight: 800;
  font-size: 1.05rem;
}

.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(7rem, 1fr) auto auto;
  justify-items: center;
  align-items: center;
  align-content: stretch;
  gap: 0.45rem;
}

.board {
  width: min(100%, 40rem);
  max-height: 100%;
  min-height: 7rem;
  overflow: auto;
  background: var(--card);
  border: 4px solid #fff;
  border-radius: 1.5rem;
  box-shadow: 0 0.4rem 0 var(--line);
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 0.35rem;
  padding: 0.7rem 1rem;
}

.story-art { margin: 0; font-size: 3.2rem; line-height: 1; }
.story-title, .shape-name, .spell-word { margin: 0; font-size: 1.7rem; font-weight: 800; }
.shape-name, .spell-word { text-transform: capitalize; }

.trace-wrap {
  position: relative;
  width: min(18rem, 72vw);
  height: 7.5rem;
}

.trace-letter {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 5.5rem;
  font-weight: 800;
  color: rgba(36, 22, 15, 0.2);
  pointer-events: none;
}

#trace {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.erase {
  min-height: 2.6rem;
  padding: 0.2rem 0.9rem;
  border-radius: 0.8rem;
  border: 3px solid var(--ink);
  background: #fff;
  font-weight: 800;
}

.giant-word, .numeral { margin: 0; font-size: clamp(3rem, 8vw, 4.5rem); font-weight: 800; }

.tiles, .objects, .sum { display: flex; flex-wrap: wrap; gap: 0.45rem; justify-content: center; align-items: center; }
.tiles span {
  min-width: 3rem;
  min-height: 3rem;
  display: grid;
  place-items: center;
  padding: 0.2rem;
  background: #fff1b8;
  border: 3px solid #fff;
  border-radius: 0.8rem;
  font-size: 2rem;
  font-weight: 800;
}
.objects { max-width: 24rem; font-size: 1.8rem; }
.plus { font-size: 2.4rem; font-weight: 800; }

.shape { width: 5.5rem; height: 5.5rem; }
.shape-circle { border-radius: 50%; background: #2f6fd6; }
.shape-square { background: #2f9e44; }
.shape-rectangle { width: 8rem; height: 4.4rem; background: #e07a12; }
.shape-triangle {
  width: 0;
  height: 0;
  background: transparent;
  border-left: 2.8rem solid transparent;
  border-right: 2.8rem solid transparent;
  border-bottom: 4.8rem solid #8a3db8;
}
.shape-star, .shape-heart { width: auto; height: auto; background: none; font-size: 4.5rem; line-height: 1; }

.next {
  min-height: 4rem;
  border: 4px solid #fff;
  border-radius: 1.2rem;
  background: #1f7a45;
  color: #fff;
  font-size: 1.6rem;
  font-weight: 800;
  box-shadow: 0 0.4rem 0 #145c32;
}
.next:active { transform: translateY(0.15rem); box-shadow: 0 0.15rem 0 #145c32; }
.next:disabled { opacity: 0.55; }

.picks { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.pick {
  min-height: 2.8rem;
  padding: 0.25rem 0.85rem;
  border-radius: 999px;
  border: 3px solid var(--ink);
  background: #fff;
  font-weight: 800;
}
.pick[aria-pressed="true"] { background: var(--ink); color: #fff; }

#mic {
  width: 8.6rem;
  height: 8.6rem;
  border-radius: 50%;
  border: 4px solid #fff;
  background: var(--mic);
  color: #fff;
  box-shadow: 0 0.5rem 0 var(--mic-deep);
  display: grid;
  place-items: center;
  padding: 0;
}

#mic svg { width: 46%; height: 46%; }
#mic .icon-mic rect { fill: currentColor; }
#mic .icon-stop { display: none; }
#mic .icon-stop rect { fill: currentColor; }
#mic.listening {
  background: var(--listen);
  color: var(--ink);
  box-shadow: 0 0.5rem 0 #c49212;
  animation: pulse 1s ease-in-out infinite;
}
#mic.listening .icon-mic { display: none; }
#mic.listening .icon-stop { display: block; }
#mic:active { transform: translateY(0.22rem); box-shadow: 0 0.15rem 0 var(--mic-deep); }
#mic:disabled { opacity: 0.55; }

#mic-label {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 800;
  text-align: center;
}

.topics {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.65rem;
}

.topic {
  min-height: 6.3rem;
  border: 3px solid #fff;
  border-radius: 1.3rem;
  background: var(--card);
  box-shadow: 0 0.4rem 0 var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  padding: 0.45rem;
}

.topic:active { transform: translateY(0.2rem); box-shadow: 0 0.12rem 0 var(--line); }
.topic:disabled { opacity: 0.55; }
.topic .emoji { font-size: 2rem; font-weight: 800; line-height: 1; }
.topic .label { font-size: 1.05rem; font-weight: 800; }

.topic[data-topic="colors"] { background: #ffe1c4; }
.topic[data-topic="animals"] { background: #d9f5d6; }
.topic[data-topic="counting"] { background: #d9e8ff; }
.topic[data-topic="letters"] { background: #f3dffc; }
.topic[data-topic="feelings"] { background: #ffd9ea; }
.topic[data-topic="family"] { background: #fff1b8; }
.topic[data-topic="bible"] { background: #d7f6ef; }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(28, 16, 10, 0.72);
  display: grid;
  place-items: center;
  padding: 1rem;
}

.sheet {
  width: min(42rem, 100%);
  max-height: min(92dvh, 52rem);
  overflow: auto;
  background: var(--card);
  color: var(--ink);
  border-radius: 1.5rem;
  padding: 1.15rem 1.15rem 1.3rem;
  user-select: text;
  -webkit-user-select: text;
}

.sheet h2, .sheet h3 { margin: 0.2rem 0 0.6rem; }
.sheet h3 { margin-top: 1rem; }

.dots {
  margin: 0.2rem 0;
  font-size: 2rem;
  letter-spacing: 0.4rem;
  text-align: center;
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 4.6rem);
  gap: 0.55rem;
  justify-content: center;
  margin: 0.8rem 0;
}

.key, .text-btn, .settings button, .done {
  min-height: 3.1rem;
  border-radius: 0.9rem;
  border: 3px solid var(--ink);
  background: #fff;
  font-weight: 800;
}

.key { font-size: 1.7rem; }

.settings {
  display: grid;
  gap: 0.55rem;
}

.settings input, .settings select {
  width: 100%;
  font-size: 1.15rem;
  padding: 0.65rem 0.75rem;
  border-radius: 0.8rem;
  border: 3px solid var(--ink);
  background: #fff;
}

.row { display: flex; flex-wrap: wrap; gap: 0.55rem; }
.row button { flex: 1; }

#voice-toggle.on { background: var(--good); color: #fff; border-color: var(--good); }
#voice-toggle.off { background: var(--muted); color: #fff; border-color: var(--muted); }

.danger { background: #8d1d2c; color: #fff; border-color: #8d1d2c; }
.done { background: var(--ink); color: #fff; margin-top: 0.4rem; }

.transcript {
  max-height: 12rem;
  overflow: auto;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 0.8rem;
  padding: 0.55rem 0.7rem;
}

.transcript p { margin: 0.35rem 0; font-size: 1.05rem; }
.transcript .user { color: #8d1d2c; }
.transcript .assistant { color: #1d4e89; }

.fine { margin: 0.4rem 0 0; font-size: 0.95rem; color: var(--muted); }

#gate-msg.bad { color: #8d1d2c; font-weight: 800; }

:focus-visible {
  outline: 4px solid var(--ink);
  outline-offset: 3px;
}

@media (max-width: 900px) {
  .topics { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  #face { width: 5.2rem; height: 5.2rem; }
  .character { width: 4.4rem; height: 4.4rem; }
  #mic { width: 7.4rem; height: 7.4rem; }
}

@media (max-height: 740px) {
  .topic { min-height: 4.8rem; }
  .topic .emoji { font-size: 1.6rem; }
  #mic { width: 6.4rem; height: 6.4rem; }
  .reply { font-size: clamp(1.45rem, 3.5vw, 2.2rem); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
  }
}

@keyframes pulse {
  50% { transform: scale(1.04); }
}

@keyframes talk {
  50% { transform: translateX(-50%) scaleY(0.72); }
}

@keyframes character-talk {
  50% { transform: scaleY(0.55); }
}

.next {
  min-height: 3.5rem;
  border: 3px solid #fff;
  border-radius: 999px;
  background: var(--good);
  color: #fff;
  font-weight: 800;
  font-size: 1.45rem;
  box-shadow: 0 0.35rem 0 #145232;
}

.next:disabled { opacity: 0.55; }

.picks { display: flex; flex-wrap: wrap; gap: 0.45rem; margin: 0.3rem 0 0.2rem; }

.pick[aria-pressed="true"] { background: var(--listen); color: var(--ink); }

.trace-wrap {
  position: relative;
  width: min(100%, 18rem);
  height: 9rem;
  min-height: 9rem;
  flex: 0 0 9rem;
}
.trace-letter {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 6.5rem;
  font-weight: 800;
  color: #f3c7b0;
  pointer-events: none;
}
#trace { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.erase {
  min-height: 2.6rem;
  border-radius: 999px;
  border: 3px solid var(--ink);
  background: #fff;
  font-weight: 800;
}
.giant-word, .numeral { margin: 0; font-size: clamp(3.2rem, 12vw, 6rem); font-weight: 800; line-height: 0.9; }
.story-art { margin: 0; font-size: 3.4rem; line-height: 1; }
.story-title, .shape-name, .spell-word { margin: 0; font-weight: 800; }
.tiles { display: flex; gap: 0.4rem; }
.tiles span {
  min-width: 2.8rem;
  min-height: 3rem;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 0.7rem;
  background: #fff1b8;
  font-weight: 800;
  font-size: 1.6rem;
}
.objects, .sum { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 0.2rem; }
.plus { font-size: 2rem; font-weight: 800; }
.shape-circle { width: 5.5rem; height: 5.5rem; border-radius: 50%; background: #3aa0c8; }
.shape-square { width: 5.2rem; height: 5.2rem; background: #ffd23f; border-radius: 0.6rem; }
.shape-rectangle { width: 8rem; height: 4.2rem; background: #1f7a45; border-radius: 0.6rem; }
.shape-triangle {
  width: 0;
  height: 0;
  border-left: 3rem solid transparent;
  border-right: 3rem solid transparent;
  border-bottom: 5.2rem solid #c9182b;
}
.shape-star {
  width: 5.5rem;
  height: 5.5rem;
  background: #ffd23f;
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
.shape-heart { font-size: 4.5rem; line-height: 1; }
