/* =========================================================================
   SUPERFICIE MATH — styles.css
   Paleta verde/turquesa (superficie, tierra, tableros). CSS Grid +
   Flexbox + Media Queries. Sin medidas fijas de layout.
   ========================================================================= */

:root {
  --color-primary: #15803d;       /* verde */
  --color-primary-dark: #14532d;
  --color-primary-light: #34d399; /* turquesa/menta */
  --color-bg: #f2f8f4;            /* gris verdoso claro */
  --color-surface: #ffffff;
  --color-border: #dbe7de;
  --ink: #1e2b22;
  --ink-soft: #5b6b60;
  --ok: #16a34a;
  --error: #dc2626;
  --accent: #f4a300;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 8px 24px rgba(21, 128, 61, 0.12);
  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.08);
  --font-main: 'Nunito Sans', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --font-display: 'Baloo 2', var(--font-main);
}

[data-theme="dark"] {
  --color-primary: #22c55e;
  --color-primary-dark: #16a34a;
  --color-primary-light: #34d399;
  --color-bg: #0f1a13;
  --color-surface: #1a2a20;
  --color-border: #2f4536;
  --ink: #eef7f1;
  --ink-soft: #9db3a4;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
}

[data-anim="off"] *, [data-anim="off"] *::before, [data-anim="off"] *::after {
  animation-duration: 0.001s !important;
  transition-duration: 0.001s !important;
}

* { box-sizing: border-box; }

/* El atributo hidden lo pisaba cualquier regla con display propio: por eso la
   barra superior (display:flex) seguía viéndose después de "Salir", con el
   nombre del estudiante anterior. */
[hidden] { display: none !important; }

html, body {
  margin: 0; padding: 0;
  background: var(--color-bg);
  color: var(--ink);
  font-family: var(--font-main);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

body { min-height: 100vh; }

h1, h2, h3 { font-family: var(--font-display); margin: 0 0 0.4em; }

button, input, select { font-family: inherit; }

.app-shell {
  max-width: 960px;
  margin: 0 auto;
  padding: clamp(12px, 3vw, 28px);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---- pantallas ---------------------------------------------------- */

.screen { display: none; flex: 1; flex-direction: column; animation: fadeIn 0.25s ease; position: relative; }
.screen.active { display: flex; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* ---- barra de navegación superior ---------------------------------- */

.topnav {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; flex-wrap: wrap;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
  padding: 10px 16px;
  margin-bottom: 18px;
}
.topnav-user { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.topnav-user .stars { color: var(--accent); }
.topnav-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.topnav-actions button { font-size: 0.85rem; padding: 8px 10px; }

/* ---- pantalla principal -------------------------------------------- */

.hero { text-align: center; padding: clamp(16px, 6vw, 48px) 0 8px; }
.hero-title {
  font-size: clamp(2.2rem, 7.5vw, 3.6rem);
  font-weight: 800;
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-light));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  letter-spacing: 1px;
}
.hero-subtitle { color: var(--ink-soft); font-size: clamp(1rem, 3vw, 1.25rem); margin-bottom: 1.5em; }
.hero-ruler { font-size: 2.4rem; margin-bottom: 0.2em; }

.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-top: 12px;
}

/* ---- botones --------------------------------------------------------- */

.btn {
  border: none; border-radius: var(--radius-sm);
  padding: 14px 18px;
  font-size: 1rem; font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
  min-height: 44px;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--color-primary); color: #fff; box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--color-primary-dark); box-shadow: var(--shadow); }
.btn-secondary { background: var(--color-primary-light); color: #063321; }
.btn-ghost { background: transparent; color: var(--color-primary); border: 2px solid var(--color-border); }
.btn-ghost:hover { border-color: var(--color-primary); }
.btn-big { padding: 18px 20px; font-size: 1.05rem; width: 100%; }
.btn-danger { background: var(--error); color: #fff; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn-option { background: var(--color-surface); border: 2px solid var(--color-border); color: var(--ink); }
.btn-option.selected { border-color: var(--color-primary); background: rgba(21,128,61,0.1); }

.row-buttons { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }

/* ---- tarjetas / paneles ------------------------------------------------ */

.card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: clamp(16px, 4vw, 26px);
}

.form-card { max-width: 420px; margin: 0 auto; width: 100%; }
.form-card label { display: block; font-weight: 700; margin: 14px 0 6px; }
.form-card input {
  width: 100%; padding: 12px 14px; border-radius: var(--radius-sm);
  border: 2px solid var(--color-border); background: var(--color-bg); color: var(--ink);
  font-size: 1rem;
}
.form-card input:focus { outline: none; border-color: var(--color-primary); }
.error-box { color: var(--error); background: rgba(220,38,38,0.08); border-radius: var(--radius-sm); padding: 10px 12px; margin-top: 10px; font-weight: 600; }
.muted { color: var(--ink-soft); }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.chip { border: 2px solid var(--color-border); background: var(--color-surface); border-radius: 999px; padding: 8px 14px; cursor: pointer; font-weight: 600; }
.chip-score { color: var(--accent); font-weight: 700; }

/* ---- bienvenida ---------------------------------------------------- */

.welcome-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin: 18px 0; }
.welcome-stat { background: var(--color-bg); border-radius: var(--radius-sm); padding: 14px; text-align: center; }
.welcome-stat .big { font-size: 1.6rem; font-weight: 800; color: var(--color-primary); display: block; }
.medal { font-size: 1.5rem; margin-right: 4px; }

/* ---- niveles --------------------------------------------------------- */

.niveles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.nivel-card {
  text-align: left;
  background: var(--color-surface);
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  display: flex; flex-direction: column; gap: 6px;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}
.nivel-card:not([disabled]):hover { transform: translateY(-3px); box-shadow: var(--shadow); border-color: var(--color-primary-light); }
.nivel-card[disabled] { opacity: 0.55; cursor: not-allowed; }
.nivel-num { font-size: 0.8rem; color: var(--ink-soft); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.nivel-nombre { font-size: 1.15rem; font-weight: 800; }
.nivel-estado-badge { font-size: 0.85rem; }
.estado-completado { border-color: var(--ok); }
.estado-disponible { border-color: var(--color-primary-light); }

.mini-bar { display: block; height: 6px; background: var(--color-border); border-radius: 4px; overflow: hidden; }
.mini-bar-fill { display: block; height: 100%; background: var(--color-primary); }

#nivel-detalle { margin-top: 16px; }
.nivel-detalle-stats { color: var(--ink-soft); font-weight: 600; }

/* ---- ejercicio ------------------------------------------------------- */

.practica-banner {
  background: linear-gradient(135deg, var(--accent), #ffd166);
  color: #4a3200;
  font-weight: 800;
  text-align: center;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-bottom: 14px;
}

.ejercicio-header {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  justify-content: space-between; margin-bottom: 14px;
}
.ejercicio-header h2 { font-size: 1.2rem; }
.ejercicio-meta { display: flex; gap: 14px; flex-wrap: wrap; color: var(--ink-soft); font-weight: 700; }
.ejercicio-meta .puntos { color: var(--accent); }

.progress-track { background: var(--color-border); border-radius: 999px; height: 10px; overflow: hidden; }
.progress-fill { display: block; height: 100%; background: linear-gradient(90deg, var(--color-primary), var(--color-primary-light)); width: 0%; transition: width 0.4s ease; }

.ejercicio-area { display: flex; justify-content: center; align-items: center; margin: 16px 0; overflow-x: auto; }
.exercise-svg { width: 100%; max-width: 560px; height: auto; touch-action: none; user-select: none; }
.exercise-svg.small { max-width: 440px; }
/* Las figuras cuadriculadas comparten lienzo (ver FIG_W/FIG_H en ui.js); este
   tope evita que en pantallas grandes el cuadrado unidad salga gigante. */
.exercise-svg.figura { max-width: 470px; }

/* Niveles 1 y 2: la figura se muestra solo con su contorno, sin cuadrícula
   interna — los cuadros los tiene que imaginar (y contar) el estudiante. */
.figura-relleno { fill: #e3f5ea; stroke: none; }
[data-theme="dark"] .figura-relleno { fill: #1f3a2b; }
.figura-contorno { fill: none; stroke: var(--color-primary); stroke-width: 3.5; stroke-linecap: square; }
.unit-placed { fill: var(--color-primary-light); stroke: var(--color-primary-dark); stroke-width: 1.5; }
/* El cuadrado patrón va con esquinas rectas (el rx se quitó en el SVG): un
   cuadrado con puntas redondeadas no es el cuadrado unidad que se enseña. */
.unit-token { fill: var(--color-primary); stroke: var(--color-primary-dark); stroke-width: 2; cursor: grab; touch-action: none; }

.grid-cell { fill: var(--color-bg); stroke: var(--color-border); stroke-width: 1; }
.grid-cell-filled { fill: var(--color-primary-light); stroke: var(--color-surface); stroke-width: 1; opacity: 0.85; }
.grid-outline { fill: none; stroke: var(--color-primary); stroke-width: 2.5; }

.svg-caption { font-size: 12px; fill: var(--ink-soft); }
.svg-question { font-size: 16px; fill: var(--ink); font-weight: 700; }

.word-problem-text { font-size: 1.05rem; line-height: 1.4; text-align: center; }

/* Nivel 7: el ejercicio a un lado y la escalera de superficie al otro.
   En pantallas angostas se apilan. Cada escalón vale 100 (no 10 como en
   LONGIMATH), porque el área lleva dos dimensiones. */
.convert-wrap { display: flex; gap: 16px; align-items: center; justify-content: center; flex-wrap: wrap; }
.convert-pregunta { flex: 1 1 260px; min-width: 220px; display: flex; justify-content: center; }
.convert-pregunta .exercise-svg { max-width: 300px; }

.escalera {
  flex: 0 0 auto; width: 100%; max-width: 340px;
  background: var(--color-surface); border: 2px solid var(--color-border);
  border-radius: var(--radius-sm); padding: 8px 10px 10px;
}
.escalera:focus { outline: 3px solid var(--color-primary); outline-offset: 2px; }
.escalera-svg { display: block; width: 100%; height: auto; user-select: none; }
.escalera-info { font-size: 0.85rem; line-height: 1.35; margin: 6px 0 0; text-align: center; font-weight: 700; color: var(--ink); }
.escalera-info.baja { color: #b45309; }
.escalera-info.sube { color: #1d4ed8; }
[data-theme="dark"] .escalera-info.baja { color: #fbbf24; }
[data-theme="dark"] .escalera-info.sube { color: #93c5fd; }
.escalera-ayuda { font-size: 0.72rem; color: var(--ink-soft); text-align: center; margin: 4px 0 0; }
.btn-verificar { display: block; margin: 8px auto 0; padding: 8px 16px; font-size: 0.9rem; }
/* alto reservado: el aviso aparece y desaparece, y sin esto el panel daría
   un salto cada vez que se comprueba la escalera */
.escalera-aviso { font-size: 0.8rem; font-weight: 700; text-align: center; margin: 6px 0 0; min-height: 2.2em; }
.escalera-aviso.ok { color: var(--ok); }
.escalera-aviso.mal { color: var(--error); }
.escalera.verificada { border-color: var(--ok); }

.esc-linea { fill: none; stroke: #2563eb; stroke-width: 2.6; stroke-linejoin: round; }
.esc-unidad { font-size: 12.5px; font-weight: 800; fill: #dc2626; }
.esc-unidad.actual { fill: #ffffff; }
.esc-unidad.inicio { fill: #7f1d1d; text-decoration: underline; }
.esc-marca { fill: #2563eb; }
.esc-hit { cursor: pointer; }
.esc-flecha { fill: none; stroke: var(--ink-soft); stroke-width: 2.4; stroke-linecap: round; }
.esc-punta { fill: var(--ink-soft); }
.esc-op { font-size: 12px; font-weight: 800; fill: var(--ink-soft); }
.esc-paso { font-size: 9.5px; font-weight: 800; }
.esc-paso.baja { fill: #b45309; }
.esc-paso.sube { fill: #1d4ed8; }
[data-theme="dark"] .esc-unidad { fill: #f87171; }
[data-theme="dark"] .esc-unidad.inicio { fill: #fca5a5; }
[data-theme="dark"] .esc-paso.baja { fill: #fbbf24; }
[data-theme="dark"] .esc-paso.sube { fill: #93c5fd; }

.ejercicio-panel { background: var(--color-surface); border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 18px; }
.control-question { font-weight: 700; margin: 0 0 8px; }
.answer-row { display: flex; gap: 10px; }
.input-numero { flex: 1; padding: 12px 14px; border-radius: var(--radius-sm); border: 2px solid var(--color-border); background: var(--color-bg); color: var(--ink); font-size: 1.1rem; }
.input-numero:focus { outline: none; border-color: var(--color-primary); }

.option-row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.feedback { min-height: 1.4em; margin: 10px 0; font-weight: 700; }
.feedback.ok { color: var(--ok); }
.feedback.error { color: var(--error); }
.explicacion { background: rgba(21,128,61,0.08); border-radius: var(--radius-sm); padding: 12px 14px; margin-bottom: 10px; }

.final-panel { text-align: center; }
.final-panel h2 { font-size: 1.8rem; }

/* ---- estudiantes ------------------------------------------------------- */

.estudiante-card { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.estudiante-card-score { font-weight: 800; color: var(--accent); font-size: 1.1rem; }

/* ---- ranking / progreso ------------------------------------------------ */

.ranking-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.ranking-table th, .ranking-table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--color-border); }
.ranking-table th { color: var(--ink-soft); font-size: 0.85rem; text-transform: uppercase; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin: 16px 0; }
.stat-card { background: var(--color-surface); border-radius: var(--radius-sm); box-shadow: var(--shadow-sm); padding: 14px; text-align: center; }
.stat-icon { font-size: 1.6rem; display: block; }
.stat-value { font-size: 1.3rem; font-weight: 800; color: var(--color-primary); display: block; margin: 4px 0 2px; }
.stat-label { font-size: 0.8rem; color: var(--ink-soft); }

.progress-chart { width: 100%; max-width: 620px; display: block; margin: 10px auto; }
.chart-axis { stroke: var(--color-border); stroke-width: 1.5; }
.chart-label { font-size: 11px; fill: var(--ink-soft); }

/* ---- configuración --------------------------------------------------- */

.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 0; border-bottom: 1px solid var(--color-border);
}
.switch { position: relative; display: inline-block; width: 48px; height: 26px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0; background: var(--color-border); border-radius: 999px; cursor: pointer; transition: background 0.2s;
}
.switch .slider::before {
  content: ''; position: absolute; height: 20px; width: 20px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: transform 0.2s; box-shadow: var(--shadow-sm);
}
.switch input:checked + .slider { background: var(--color-primary); }
.switch input:checked + .slider::before { transform: translateX(22px); }

.confirm-box { background: rgba(220,38,38,0.08); border-radius: var(--radius-sm); padding: 14px; margin-top: 10px; }

/* ---- toasts / celebraciones ------------------------------------------- */

#toast-host { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; width: min(92vw, 420px); }
.toast {
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow); opacity: 0; transform: translateY(-10px); transition: all 0.3s ease; font-weight: 700; text-align: center;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.ok { background: var(--ok); }

.points-popup {
  position: fixed; z-index: 100; color: var(--accent); font-weight: 800; font-size: 1.2rem;
  animation: floatUp 1.1s ease forwards; pointer-events: none;
}
@keyframes floatUp { 0% { opacity: 0; transform: translateY(0); } 20% { opacity: 1; } 100% { opacity: 0; transform: translateY(-40px); } }

.confetti-wrap { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.confetti-piece { position: absolute; top: -10px; width: 8px; height: 14px; opacity: 0.9; animation-name: confettiFall; animation-timing-function: ease-in; }
@keyframes confettiFall { to { transform: translateY(320px) rotate(360deg); opacity: 0; } }

.anim-pulse { animation: pulseKf 0.4s ease; }
@keyframes pulseKf { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.25); } }

.anim-shake { animation: shakeKf 0.4s ease; }
@keyframes shakeKf { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* ---- footer / acerca --------------------------------------------------- */

.about-text { line-height: 1.6; }
.about-text ul { padding-left: 1.2em; }

/* ---- responsive -------------------------------------------------------- */

@media (max-width: 480px) {
  .btn { padding: 12px 14px; font-size: 0.95rem; }
  .ejercicio-header { flex-direction: column; align-items: flex-start; }
  .answer-row { flex-direction: column; }
}

@media (pointer: coarse) {
  .btn, .nivel-card, .chip { min-height: 46px; }
}
