:root {
  --navy: #0e2a5a;
  --navy-2: #1b3f7d;
  --yellow: #f9b934;
  --yellow-soft: #fff3cf;
  --red: #e8403a;
  --red-dark: #c42f2a;
  --green: #1f9d55;
  --green-soft: #e3f6eb;
  --cream: #fffaf1;
  --sky-1: #d9efff;
  --sky-2: #f4fbff;
  --ink: #1b2233;
  --muted: #5d6679;
  --line: #e7e1d6;
  --card: #ffffff;
  --display: "Baloo 2", "Trebuchet MS", system-ui, sans-serif;
  --body: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  --radius: 22px;
  --shadow: 0 10px 30px rgba(14, 42, 90, 0.12);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--body);
  color: var(--ink);
  background: var(--cream);
  display: flex;
  flex-direction: column;
}
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- Barra superior ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 24px;
  background: var(--navy);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 12px;
  background: var(--yellow);
  color: var(--navy);
  font: 800 24px/1 var(--display);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font: 700 20px/1.1 var(--display); }
.brand-text span { font-size: 13px; opacity: 0.8; }
.chapter {
  margin-left: auto;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: var(--yellow);
  font: 700 16px/1.2 var(--display);
}
.status { font-size: 12px; opacity: 0.7; white-space: nowrap; }

/* ---------- Distribución ---------- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

/* ---------- Escenario de Flynn ---------- */
.stage {
  position: relative;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 24px 24px 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% 22%, rgba(255, 255, 255, 0.9) 0 40px, transparent 41px),
    radial-gradient(circle at 82% 14%, rgba(255, 255, 255, 0.7) 0 26px, transparent 27px),
    linear-gradient(180deg, var(--sky-1), var(--sky-2) 70%, #e9f3e2 70.2%, #dcebd2);
}
.flynn-wrap {
  height: min(66vh, 560px);
  aspect-ratio: 337 / 481;
  position: relative;
  z-index: 1;
}
.flynn { position: relative; width: 100%; height: 100%; transform-origin: 55% 95%; }
.flynn img, .flynn svg { position: absolute; inset: 0; width: 100%; height: 100%; user-select: none; }
.flynn.is-speaking { animation: bob 0.5s ease-in-out infinite alternate; }
.flynn.is-listening { animation: lean 0.6s ease forwards; }
.flynn[data-mood="celebrate"] { animation: jump 0.8s cubic-bezier(.3, 1.6, .5, 1) 2; }
.flynn[data-mood="encourage"] { animation: tilt 1.2s ease; }

@keyframes bob { from { transform: translateY(0) rotate(0); } to { transform: translateY(-4px) rotate(-1deg); } }
@keyframes lean { to { transform: rotate(2.5deg) translateX(6px); } }
@keyframes jump { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-34px) rotate(-3deg); } }
@keyframes tilt { 0%, 100% { transform: rotate(0); } 30% { transform: rotate(4deg); } 60% { transform: rotate(-2deg); } }

.bubble {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  width: min(92%, 520px);
  padding: 16px 22px;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  font: 600 22px/1.35 var(--display);
  color: var(--navy);
  text-align: center;
  text-wrap: balance;
}
.bubble::after {
  content: "";
  position: absolute;
  bottom: -12px;
  left: 50%;
  border: 12px solid transparent;
  border-top-color: var(--card);
  border-bottom: 0;
  transform: translateX(-50%);
}
.bubble.is-thinking { color: var(--muted); }
mark {
  background: var(--yellow);
  color: var(--navy);
  padding: 0 6px;
  border-radius: 8px;
  font-weight: 800;
}

.listening {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 90%;
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--red);
  color: #fff;
  font: 700 18px/1.2 var(--display);
  box-shadow: var(--shadow);
}
#interim { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bars { display: flex; align-items: center; gap: 3px; height: 22px; }
.bars i { width: 4px; height: 8px; border-radius: 2px; background: #fff; animation: bar 0.9s ease-in-out infinite; }
.bars i:nth-child(2) { animation-delay: 0.15s; }
.bars i:nth-child(3) { animation-delay: 0.3s; }
.bars i:nth-child(4) { animation-delay: 0.45s; }
.bars i:nth-child(5) { animation-delay: 0.6s; }
@keyframes bar { 50% { height: 22px; } }

.burst { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
.burst span {
  position: absolute;
  left: 50%;
  top: 45%;
  font-size: 30px;
  animation: fly 1.1s ease-out forwards;
}
@keyframes fly {
  from { transform: translate(-50%, -50%) scale(0.4); opacity: 1; }
  to { transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y))) scale(1.2) rotate(var(--r)); opacity: 0; }
}

.toast {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  width: max-content;
  max-width: 90%;
  padding: 10px 16px;
  border-radius: 14px;
  background: var(--navy);
  color: #fff;
  font-size: 15px;
  box-shadow: var(--shadow);
}

/* ---------- Panel de conversación ---------- */
.panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 20px;
  border-left: 1px solid var(--line);
  background: var(--cream);
}

.practice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--yellow-soft);
  border: 2px solid var(--yellow);
}
.practice.is-wrong { animation: shake 0.45s ease; }
.practice.is-right { background: var(--green-soft); border-color: var(--green); }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.practice-main { display: flex; align-items: center; gap: 14px; }
.practice-emoji { font-size: 40px; line-height: 1; }
.practice-label { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.practice-word { font: 800 32px/1.1 var(--display); color: var(--navy); }
.practice-side { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.attempts { display: flex; gap: 6px; }
.attempts i { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--navy-2); }
.attempts i.used { background: var(--red); border-color: var(--red); }

.btn-soft {
  border: 0;
  padding: 8px 12px;
  border-radius: 12px;
  background: #fff;
  color: var(--navy);
  font-weight: 700;
  box-shadow: 0 2px 0 rgba(14, 42, 90, 0.12);
}

.log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0;
  padding: 4px 2px;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.msg { display: flex; gap: 8px; align-items: flex-end; max-width: 88%; }
.msg p { margin: 0; padding: 10px 14px; border-radius: 18px; font-size: 17px; line-height: 1.4; }
.msg-flynn p { background: var(--card); border-bottom-left-radius: 6px; box-shadow: 0 1px 0 var(--line); }
.msg-kid { align-self: flex-end; }
.msg-kid p { background: var(--navy); color: #fff; border-bottom-right-radius: 6px; }
.face-crop { flex: none; width: 34px; height: 34px; border-radius: 50%; overflow: hidden; background: var(--yellow); }
.face-crop img { display: block; width: 130px; max-width: none; margin: -58px 0 0 -72px; }
.tag { display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 999px; font-size: 13px; font-weight: 800; }
.tag.ok { background: var(--green); color: #fff; }
.tag.bad { background: var(--yellow); color: var(--navy); }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1s infinite; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 50% { opacity: 0.25; } }

.words-title { font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; max-height: 92px; overflow-y: auto; }
.chip {
  border: 1.5px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
}
.chip:hover { border-color: var(--navy-2); }

.composer { display: flex; align-items: center; gap: 12px; }
.mic {
  flex: none;
  width: 68px; height: 68px;
  border: 0;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--red);
  box-shadow: 0 6px 0 var(--red-dark);
  transition: transform 0.1s;
}
.mic svg { width: 32px; height: 32px; fill: #fff; }
.mic:active { transform: translateY(3px); box-shadow: 0 3px 0 var(--red-dark); }
body[data-phase="listening"] .mic { animation: pulse 1.2s ease-in-out infinite; }
body[data-phase="thinking"] .mic { opacity: 0.5; }
.mic.nudge { animation: pulse 1.2s ease-in-out 3; }
@keyframes pulse { 0%, 100% { box-shadow: 0 6px 0 var(--red-dark), 0 0 0 0 rgba(232, 64, 58, 0.5); } 50% { box-shadow: 0 6px 0 var(--red-dark), 0 0 0 14px rgba(232, 64, 58, 0); } }
.field { flex: 1; min-width: 0; display: flex; align-items: center; background: #fff; border: 2px solid var(--line); border-radius: 18px; padding: 4px 4px 4px 14px; }
.field:focus-within { border-color: var(--navy-2); }
.field input { flex: 1; min-width: 0; border: 0; outline: none; font: 600 17px var(--body); background: transparent; padding: 10px 0; }
.send { border: 0; width: 44px; height: 44px; border-radius: 14px; background: var(--navy); display: grid; place-items: center; }
.send svg { width: 20px; height: 20px; fill: #fff; }

/* ---------- Pantalla de inicio ---------- */
.intro {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(14, 42, 90, 0.82);
}
.intro-card {
  width: min(420px, 100%);
  padding: 28px 24px;
  border-radius: 28px;
  background: var(--cream);
  text-align: center;
  box-shadow: var(--shadow);
}
.intro-flynn { height: 200px; display: flex; justify-content: center; }
.intro-flynn img { height: 100%; width: auto; }
.intro h1 { margin: 8px 0 4px; font: 800 34px/1.1 var(--display); color: var(--navy); }
.intro p { margin: 0 0 18px; font-size: 18px; color: var(--muted); }
.intro small { display: block; margin-top: 14px; color: var(--muted); }
.btn-primary {
  border: 0;
  padding: 14px 36px;
  border-radius: 18px;
  background: var(--red);
  color: #fff;
  font: 800 24px/1 var(--display);
  box-shadow: 0 6px 0 var(--red-dark);
}
.btn-primary:active { transform: translateY(3px); box-shadow: 0 3px 0 var(--red-dark); }

/* ---------- Celular ---------- */
@media (max-width: 860px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .status, .brand-text span { display: none; }
  .chapter { font-size: 14px; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .stage { padding: 16px 16px 0; min-height: 42vh; }
  .flynn-wrap { height: 34vh; margin-top: 96px; }
  .bubble { font-size: 18px; padding: 12px 16px; top: 12px; }
  .panel { border-left: 0; border-top: 1px solid var(--line); padding: 12px 16px; }
  .practice-word { font-size: 26px; }
  .chips { max-height: 64px; }
}

@media (prefers-reduced-motion: reduce) {
  .flynn, .bars i, .mic, .burst span { animation: none !important; }
}
