/* ============================================================
   Poliglota — sistema de diseño
   Escala tipográfica y tamaños táctiles cambian según [data-age]
   ============================================================ */

:root {
  --brand:      #5b4bff;
  --brand-dark: #4437d6;
  --brand-soft: #ece9ff;
  --accent:     #ff8a3d;
  --good:       #17a673;
  --good-soft:  #dff5ec;
  --bad:        #e8455f;
  --bad-soft:   #fde5e9;
  --gold:       #f5b301;

  --bg:         #f6f5fb;
  --surface:    #ffffff;
  --surface-2:  #f0eefa;
  --line:       #e2dff0;
  --text:       #1c1a2e;
  --text-soft:  #5d5878;

  --r-sm: 10px;
  --r:    18px;
  --r-lg: 28px;
  --r-pill: 999px;

  --shadow:    0 2px 0 var(--line), 0 8px 24px rgba(28,26,46,.07);
  --shadow-lg: 0 12px 40px rgba(28,26,46,.16);

  --tap: 56px;                /* altura táctil mínima */
  --fs:  17px;                /* base */
  --fs-title: 26px;
  --emoji: 56px;
  --gap: 14px;
  --maxw: 720px;

  --font: "Segoe UI Variable Display", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-round: "Baloo 2", "Comic Sans MS", "Segoe UI Variable Display", system-ui, sans-serif;
}

/* ---- Modos de edad: todo el layout se re-escala ---- */
body[data-age="baby"]   { --fs: 21px; --fs-title: 34px; --tap: 96px;  --emoji: 92px; --r: 28px; --r-lg: 36px; --gap: 18px; }
body[data-age="kid"]    { --fs: 19px; --fs-title: 30px; --tap: 76px;  --emoji: 72px; --r: 24px; --gap: 16px; }
body[data-age="junior"] { --fs: 18px; --fs-title: 28px; --tap: 64px;  --emoji: 60px; }
body[data-age="teen"],
body[data-age="adult"]  { --fs: 17px; --fs-title: 26px; --tap: 56px;  --emoji: 52px; }
body[data-age="senior"] { --fs: 21px; --fs-title: 32px; --tap: 72px;  --emoji: 64px; }

body[data-age="baby"], body[data-age="kid"] { --font: var(--font-round); }

/* ---- Tema oscuro ---- */
@media (prefers-color-scheme: dark) {
  body[data-theme="auto"] { color-scheme: dark; }
  body[data-theme="auto"] {
    --bg:#131223; --surface:#1d1b30; --surface-2:#262341; --line:#332f52;
    --text:#f2f1fa; --text-soft:#a9a4c9; --brand-soft:#2a2550;
    --good-soft:#123a2d; --bad-soft:#3d1723;
    --shadow: 0 2px 0 var(--line), 0 8px 24px rgba(0,0,0,.35);
  }
}
body[data-theme="dark"] {
  color-scheme: dark;
  --bg:#131223; --surface:#1d1b30; --surface-2:#262341; --line:#332f52;
  --text:#f2f1fa; --text-soft:#a9a4c9; --brand-soft:#2a2550;
  --good-soft:#123a2d; --bad-soft:#3d1723;
  --shadow: 0 2px 0 var(--line), 0 8px 24px rgba(0,0,0,.35);
}

/* ---- Accesibilidad: tipografía amigable a la dislexia ---- */
body[data-font="dys"] { --font: "Verdana", "Tahoma", sans-serif; letter-spacing: .02em; word-spacing: .12em; }

* { box-sizing: border-box; }

/* IMPRESCINDIBLE: el atributo `hidden` debe ganar SIEMPRE.
   Los estilos de esta hoja tienen prioridad sobre los del navegador, así que
   una regla como `.overlay { display: flex }` dejaría visible un elemento
   marcado con `hidden`. Sin esto, la capa oscura del overlay se queda
   permanentemente encima: la pantalla se ve borrosa y no se puede tocar nada. */
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs);
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;   /* sin destello gris al tocar en iOS */
  -webkit-text-size-adjust: 100%;             /* iOS no debe agrandar el texto al girar */
  text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

.app { min-height: 100%; display: flex; flex-direction: column; }
/* 100dvh tiene en cuenta la barra de Safari en iPhone, que aparece y
   desaparece al desplazarse. Se deja 100% antes como respaldo. */
@supports (height: 100dvh) {
  .app { min-height: 100dvh; }
}

.screen {
  flex: 1;
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 18px 16px 24px;
  padding-top: max(18px, env(safe-area-inset-top));
  /* En iPhone apaisado, el notch se come los laterales */
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

h1,h2,h3 { margin: 0 0 .4em; line-height: 1.2; font-weight: 800; letter-spacing: -.01em; }
h1 { font-size: var(--fs-title); }
h2 { font-size: calc(var(--fs) * 1.25); }
h3 { font-size: calc(var(--fs) * 1.05); }
p  { margin: 0 0 1em; }
.muted { color: var(--text-soft); }
.center { text-align: center; }
.small { font-size: .86em; }
.stack > * + * { margin-top: var(--gap); }
.row { display: flex; gap: 10px; align-items: center; }
.row-wrap { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.hide { display: none !important; }

/* ============================ Botones ============================ */
/* Reglas comunes a todo lo que se toca.
   En iOS, sin `touch-action` un doble toque rápido hace zoom, y sin
   `-webkit-touch-callout` una pulsación larga saca el menú de copiar:
   ambas cosas arruinan una aplicación de tocar botones. */
.btn, .choice, .chip, .token, .node, .baby-card, .tabbar button {
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.btn {
  -webkit-appearance: none;
  appearance: none;
  border: 0;
  font: inherit;
  font-weight: 700;
  color: #fff;
  background: var(--brand);
  min-height: var(--tap);
  padding: 0 22px;
  border-radius: var(--r-pill);
  cursor: pointer;
  box-shadow: 0 4px 0 var(--brand-dark);
  transition: transform .08s ease, box-shadow .08s ease, background .15s ease;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  text-align: center;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--brand-dark); }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.btn[disabled] { opacity: .45; cursor: not-allowed; box-shadow: none; transform: none; }
.btn.block { width: 100%; }
.btn.ghost { background: var(--surface); color: var(--text); box-shadow: 0 3px 0 var(--line); }
.btn.good  { background: var(--good); box-shadow: 0 4px 0 #0f7a54; }
.btn.bad   { background: var(--bad);  box-shadow: 0 4px 0 #b32c43; }
.btn.soft  { background: var(--brand-soft); color: var(--brand); box-shadow: none; }
.btn.sm    { min-height: 44px; padding: 0 16px; font-size: .9em; }
.btn.icon  { min-width: var(--tap); padding: 0 14px; }

/* ============================ Tarjetas ============================ */
.card {
  background: var(--surface);
  border-radius: var(--r);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card.flat { box-shadow: none; border: 2px solid var(--line); }

/* ============================ Barra superior ============================ */
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.topbar .btn.icon { flex: 0 0 auto; }
.progressbar {
  flex: 1; height: 16px; border-radius: var(--r-pill);
  background: var(--surface-2); overflow: hidden; box-shadow: inset 0 1px 2px rgba(0,0,0,.06);
}
.progressbar > i {
  display: block; height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--good), #45d6a0);
  border-radius: var(--r-pill);
  transition: width .35s cubic-bezier(.2,.9,.3,1);
}
.hearts { font-size: 1.1em; letter-spacing: 2px; }

/* ============================ Tab bar ============================ */
.tabbar {
  position: sticky; bottom: 0; z-index: 20;
  display: flex; justify-content: center; gap: 4px;
  background: var(--surface);
  border-top: 2px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tabbar button {
  flex: 1 1 0; max-width: 140px;
  background: none; border: 0; font: inherit; color: var(--text-soft);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px; border-radius: var(--r-sm); cursor: pointer; min-height: 54px;
  font-size: .72em; font-weight: 700;
}
.tabbar button .ico { font-size: 1.7em; line-height: 1; }
.tabbar button[aria-current="page"] { color: var(--brand); background: var(--brand-soft); }

/* ============================ Selector tipo tarjeta ============================ */
.choices { display: grid; gap: var(--gap); }
.choices.two { grid-template-columns: repeat(2, 1fr); }
.choices.auto { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }

.choice {
  position: relative;
  background: var(--surface);
  border: 3px solid var(--line);
  border-radius: var(--r);
  padding: 14px;
  min-height: var(--tap);
  font: inherit; font-weight: 700; color: var(--text);
  cursor: pointer; text-align: center;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  transition: transform .1s ease, border-color .15s ease, background .15s ease;
  box-shadow: 0 3px 0 var(--line);
}
.choice:active { transform: translateY(3px); box-shadow: none; }
.choice:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.choice .emoji { font-size: var(--emoji); line-height: 1; }
.choice[aria-pressed="true"] { border-color: var(--brand); background: var(--brand-soft); }
.choice.is-correct { border-color: var(--good); background: var(--good-soft); animation: pop .35s ease; }
.choice.is-wrong   { border-color: var(--bad);  background: var(--bad-soft);  animation: shake .35s ease; }
.choice.is-dim     { opacity: .4; }

/* ============================ Fichas de palabras ============================ */
.tokens { display: flex; flex-wrap: wrap; gap: 10px; }
.token {
  background: var(--surface); border: 2px solid var(--line); box-shadow: 0 3px 0 var(--line);
  border-radius: var(--r-sm); padding: 10px 14px; font: inherit; font-weight: 700; color: var(--text);
  cursor: pointer; min-height: 48px; min-width: 44px;
}
.token:active { transform: translateY(3px); box-shadow: none; }
.token.used { visibility: hidden; }
.answer-slot {
  min-height: calc(var(--tap) + 8px);
  border-bottom: 3px dashed var(--line);
  padding: 8px 4px; display: flex; flex-wrap: wrap; gap: 10px; align-content: flex-start;
}

/* ============================ Feedback ============================ */
.feedback {
  position: sticky; bottom: 0;
  margin: 18px -16px -24px; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  animation: slideUp .25s cubic-bezier(.2,.9,.3,1);
}
.feedback.ok  { background: var(--good-soft); }
.feedback.err { background: var(--bad-soft); }
.feedback h3 { display: flex; align-items: center; gap: 8px; }
.feedback.ok  h3 { color: var(--good); }
.feedback.err h3 { color: var(--bad); }

/* ============================ Ruta de lecciones ============================ */
.unit { margin-bottom: 26px; }
.unit-head {
  background: var(--brand); color: #fff; border-radius: var(--r);
  padding: 14px 16px; box-shadow: 0 4px 0 var(--brand-dark); margin-bottom: 16px;
}
.unit-head h2 { margin: 0; font-size: calc(var(--fs) * 1.15); }
.unit-head p  { margin: 2px 0 0; opacity: .85; font-size: .85em; }

.path { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.node-row { display: flex; justify-content: center; width: 100%; }
.node {
  width: calc(var(--tap) + 18px); height: calc(var(--tap) + 18px);
  border-radius: 50%; border: 0; cursor: pointer;
  background: var(--surface-2); color: var(--text-soft);
  box-shadow: 0 5px 0 var(--line);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.8em; position: relative; transition: transform .1s ease;
}
.node:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--line); }
.node.done   { background: var(--gold); color: #fff; box-shadow: 0 5px 0 #c58f00; }
.node.active { background: var(--brand); color: #fff; box-shadow: 0 5px 0 var(--brand-dark); animation: bob 2.2s ease-in-out infinite; }
.node.locked { opacity: .55; cursor: not-allowed; }
.node .label {
  position: absolute; bottom: -22px; left: 50%; transform: translateX(-50%);
  font-size: 11px; font-weight: 700; color: var(--text-soft); white-space: nowrap;
}
.node-row { margin-bottom: 22px; }

/* ============================ Estadísticas ============================ */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px,1fr)); gap: 10px; }
.stat { background: var(--surface); border-radius: var(--r); padding: 12px; text-align: center; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 1.5em; }
.stat span { font-size: .74em; color: var(--text-soft); font-weight: 700; text-transform: uppercase; letter-spacing: .03em; }

.ring { display: block; margin: 0 auto; }
.ring circle { fill: none; stroke-width: 12; stroke-linecap: round; }
.ring .bg  { stroke: var(--surface-2); }
.ring .fg  { stroke: var(--good); transition: stroke-dashoffset .6s cubic-bezier(.2,.9,.3,1); }

/* ============================ Modo bebé ============================ */
.baby-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--gap); }
.baby-card {
  aspect-ratio: 1 / 1;
  border: 0; border-radius: var(--r-lg); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 800; color: #23204a;
  box-shadow: 0 8px 0 rgba(0,0,0,.12);
  transition: transform .12s ease;
  padding: 8px;
}
.baby-card:active { transform: scale(.94) translateY(4px); box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.baby-card .emoji { font-size: min(22vw, 96px); line-height: 1; }
.baby-card .word  { font-size: min(5.2vw, 24px); }
.baby-card.speaking { animation: pulse .6s ease infinite alternate; }

/* Respaldo SOLO para navegadores sin `aspect-ratio` (Safari anterior a la 15).
   Debe ir en píxeles, nunca en «vw»: una medida relativa a la ventana crece
   con el monitor y en un ordenador de escritorio cada tarjeta acabaría
   ocupando la pantalla entera. */
@supports not (aspect-ratio: 1 / 1) {
  .baby-card { min-height: 150px; }
}

/* Fila desplazable: el degradado de la derecha avisa de que hay más contenido */
.chips-wrap { position: relative; }
.chips-wrap::after {
  content: ''; position: absolute; top: 0; right: 0; bottom: 10px; width: 32px;
  pointer-events: none; border-radius: 0 var(--r) var(--r) 0;
  background: linear-gradient(90deg, transparent, var(--bg));
}
.chips {
  display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 10px;
  scrollbar-width: thin; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity;
}
.chips > .chip { scroll-snap-align: start; }
.chip {
  flex: 0 0 auto; border: 2px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: var(--r-pill); padding: 10px 16px; font: inherit; font-weight: 700; cursor: pointer;
  min-height: 46px; display: inline-flex; align-items: center; gap: 6px;
}
.chip[aria-pressed="true"] { background: var(--brand); color: #fff; border-color: var(--brand); }

/* ============================ Formularios ============================ */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-weight: 700; margin-bottom: 6px; font-size: .9em; }
input[type="text"], select {
  width: 100%; min-height: var(--tap); color: var(--text);
  font-family: inherit; font-weight: inherit;
  /* iOS hace zoom automático si el texto de un campo baja de 16px */
  font-size: max(16px, var(--fs));
  background: var(--surface); border: 3px solid var(--line); border-radius: var(--r-sm);
  padding: 8px 14px;
  -webkit-appearance: none; appearance: none;
}
/* La flecha del desplegable desaparece al quitar la apariencia nativa */
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--text-soft) 50%),
                    linear-gradient(135deg, var(--text-soft) 50%, transparent 50%);
  background-position: right 20px center, right 14px center;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 40px;
}
input[type="text"]:focus, select:focus { outline: none; border-color: var(--brand); }
input[type="range"] { width: 100%; accent-color: var(--brand); height: var(--tap); }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: var(--tap); }
.switch input { width: 54px; height: 34px; accent-color: var(--brand); flex: 0 0 auto; cursor: pointer; }
.switch { cursor: pointer; }

/* ============================ Toast / overlay ============================ */
.toast {
  position: fixed; left: 50%; bottom: 88px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 12px 20px; border-radius: var(--r-pill);
  font-weight: 700; opacity: 0; pointer-events: none; transition: all .25s ease; z-index: 100;
  max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.overlay {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;         /* respaldo para Safari antiguo */
  inset: 0;
  background: rgba(20,18,40,.55);
  -webkit-backdrop-filter: blur(3px);           /* Safari exige el prefijo */
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; z-index: 90;
  padding: max(20px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}
.overlay > .card { max-width: 420px; width: 100%; box-shadow: var(--shadow-lg); }

/* ============================ Confeti ============================ */
.confetti {
  position: fixed; top: 0; right: 0; bottom: 0; left: 0; inset: 0;
  pointer-events: none; z-index: 95; overflow: hidden;
}
.confetti i {
  position: absolute; width: 10px; height: 16px; border-radius: 2px;
  animation: fall linear forwards;
}

/* ============================ Animaciones ============================ */
@keyframes pop   { 0%{transform:scale(1)} 45%{transform:scale(1.07)} 100%{transform:scale(1)} }
@keyframes shake { 0%,100%{transform:translateX(0)} 20%{transform:translateX(-8px)} 40%{transform:translateX(8px)} 60%{transform:translateX(-5px)} 80%{transform:translateX(5px)} }
@keyframes bob   { 0%,100%{transform:translateY(0)} 50%{transform:translateY(-6px)} }
@keyframes pulse { from{transform:scale(1)} to{transform:scale(1.06)} }
@keyframes slideUp { from{transform:translateY(100%)} to{transform:translateY(0)} }
@keyframes fadeIn  { from{opacity:0; transform:translateY(8px)} to{opacity:1; transform:none} }
@keyframes fall  { to { transform: translateY(105vh) rotate(720deg); opacity: .2; } }
.screen > * { animation: fadeIn .22s ease; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* ============================ Responsive ============================ */
@media (min-width: 620px) {
  .baby-grid { grid-template-columns: repeat(3, 1fr); }
  .choices.auto { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
}
@media (min-width: 900px) {
  .tabbar { border-radius: var(--r-lg) var(--r-lg) 0 0; max-width: var(--maxw); margin: 0 auto; border: 2px solid var(--line); border-bottom: 0; }
}
