/* =========================================================================
   LONGIMATH — styles.css
   Paleta: azul / celeste / blanco / gris claro. CSS Grid + Flexbox +
   Media Queries. Sin medidas fijas de layout (todo relativo / clamp).
   ========================================================================= */

:root {
  --color-primary: #2563eb;      /* azul */
  --color-primary-dark: #1d4ed8;
  --color-primary-light: #38bdf8; /* celeste */
  --color-bg: #f3f6fb;            /* gris claro */
  --color-surface: #ffffff;       /* blanco */
  --color-border: #e2e8f0;
  --ink: #1e293b;
  --ink-soft: #64748b;
  --ok: #16a34a;
  --error: #dc2626;
  --accent: #ffb703;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 8px 24px rgba(37, 99, 235, 0.10);
  --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: #3b82f6;
  --color-primary-dark: #2563eb;
  --color-primary-light: #38bdf8;
  --color-bg: #0f172a;
  --color-surface: #1e293b;
  --color-border: #334155;
  --ink: #f1f5f9;
  --ink-soft: #94a3b8;
  --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 { 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; }
.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.4rem, 8vw, 4rem);
  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: #08324a; }
.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(37,99,235,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; }

/* Listados de estudiantes: se ven 5 y el resto se alcanza con el scroll,
   así la lista no crece sin fin cuando hay un curso entero registrado. */
.lista-scroll {
  flex-direction: column; flex-wrap: nowrap;
  max-height: calc(5 * 47px + 4 * 8px);
  overflow-y: auto; padding-right: 4px;
}
.lista-scroll .chip { flex: 0 0 auto; text-align: left; }

.tabla-scroll { max-height: calc(40px + 5 * 42px); overflow-y: auto; margin-top: 12px; }
.tabla-scroll .ranking-table { margin-top: 0; }
.tabla-scroll thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--color-surface); box-shadow: inset 0 -1px 0 var(--color-border);
}
.chip { border: 2px solid var(--color-border); background: var(--color-surface); border-radius: 999px; padding: 8px 14px; cursor: pointer; font-weight: 600; }
.chip-grado {
  display: inline-block; margin-left: 4px; padding: 1px 7px;
  border-radius: 999px; background: rgba(37,99,235,0.12); color: var(--color-primary);
  font-size: 0.78rem; font-weight: 800;
}
.form-card select {
  width: 100%; padding: 12px; font-size: 1rem; margin-bottom: 12px;
  border: 2px solid var(--color-border); border-radius: var(--radius-sm);
  background: var(--color-surface); color: var(--ink); font-family: inherit;
}
.form-card select:focus { outline: none; border-color: var(--color-primary); }
.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 ------------------------------------------------------- */

.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; }
/* ejercicio ya calificado: se sigue viendo, pero no se puede volver a tocar */
.ejercicio-area.bloqueada, #ejercicio-controles.bloqueado { pointer-events: none; }
#ejercicio-controles.bloqueado .input-numero { opacity: 0.7; }
/* max-height: si el ejercicio dibuja algo muy alto (un cuaderno, una hoja
   carta), el SVG se encoge en vez de crecer hacia abajo. Sin esto la regla
   del Nivel 4 quedaba fuera de la pantalla y parecía que había desaparecido. */
.exercise-svg { width: 100%; max-width: 560px; height: auto; max-height: 58vh; touch-action: none; user-select: none; }
.exercise-svg.small { max-width: 440px; }

/* Nivel 1: la unidad patrón es un segmento que se arrastra encima del
   segmento a medir (no una ficha rectangular sobre casillas). */
.unit-base-line { stroke: var(--ink); stroke-width: 6; stroke-linecap: butt; }
.unit-base-cap { stroke: var(--ink); stroke-width: 3; }

.unit-seg-line { stroke-width: 8; stroke-linecap: butt; }
.unit-seg-cap { stroke-width: 3; }

.unit-token-seg { cursor: grab; touch-action: none; }
.unit-token-seg .unit-seg-line { stroke: var(--color-primary); }
.unit-token-seg .unit-seg-cap { stroke: var(--color-primary-dark); }
.unit-token-seg.dragging { cursor: grabbing; opacity: 0.9; }

.unit-seg-placed { cursor: pointer; touch-action: none; }
.unit-seg-placed .unit-seg-line { stroke: var(--color-primary); }
.unit-seg-placed .unit-seg-cap { stroke: var(--color-primary-dark); }
.unit-seg-placed.alt .unit-seg-line { stroke: var(--color-primary-light); }
.unit-seg-placed.alt .unit-seg-cap { stroke: var(--color-primary); }
.unit-seg-placed:hover .unit-seg-line { stroke: var(--accent); }

.ruler-body { fill: #fef9ec; stroke: #d8c48a; stroke-width: 1.5; }
[data-theme="dark"] .ruler-body { fill: #3a3220; stroke: #6b5c33; }
.ruler-tick { stroke: var(--ink); stroke-width: 1.4; }
.ruler-num { font-size: 10px; fill: var(--ink-soft); }
.ruler-group { cursor: grab; touch-action: none; }
.ruler-group.arrastrando { cursor: grabbing; }
.ruler-group.aligned .ruler-body { fill: #e6f9ee; stroke: var(--ok); stroke-width: 2.5; }

/* Nivel 3: el objeto es una ilustración SVG a escala real sobre la regla. */
.objeto-nombre { font-size: 15px; font-weight: 700; fill: var(--ink); }
.objeto-img { pointer-events: none; }
.svg-caption { font-size: 12px; fill: var(--ink-soft); }
.svg-question { font-size: 20px; fill: var(--ink); font-weight: 700; }
/* El "vs." del Nivel 6, en azul oscuro para separar bien las dos medidas.
   En tema oscuro se aclara: un azul oscuro sobre fondo oscuro no se vería. */
.svg-vs { fill: #1e3a8a; font-weight: 800; }
[data-theme="dark"] .svg-vs { fill: #93c5fd; }

/* Modo práctica: repasar un nivel ya completado sin tocar el progreso. */
.banner-practica {
  background: #fef3c7; border: 2px solid #f59e0b; color: #92400e;
  border-radius: var(--radius-sm); padding: 8px 12px; margin: 10px 0 0;
  font-weight: 800; text-align: center; font-size: 0.92rem;
}
.aviso-practica {
  background: #fef3c7; border-left: 5px solid #f59e0b; color: #92400e;
  border-radius: var(--radius-sm); padding: 8px 12px; margin: 0 0 10px;
  font-weight: 700; font-size: 0.9rem;
}
[data-theme="dark"] .banner-practica,
[data-theme="dark"] .aviso-practica { background: #3b2f0b; border-color: #b45309; color: #fde68a; }

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

/* Nivel 8: el ejercicio a un lado y la escalera de conversiones al otro.
   En pantallas angostas se apilan. */
.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: 330px;
  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: 13px; 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: 10px; 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; }
/* Nivel 4: el texto cambia al alinear la regla ("alinea..." / "¿cuánto
   mide...?"). Reservar dos líneas evita que el panel cambie de alto y que la
   página pegue un salto justo mientras se está arrastrando. */
.ctrl-alinear .control-question { min-height: 2.8em; }
.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(37,99,235,0.08); border-radius: var(--radius-sm); padding: 12px 14px; margin-bottom: 10px; }

.order-list { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 360px; margin: 0 auto; }
.order-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--color-bg); border: 2px solid var(--color-border); border-radius: var(--radius-sm);
  padding: 12px 14px; font-weight: 700;
  position: relative; touch-action: none;
}
.order-item.dragging { box-shadow: var(--shadow); border-color: var(--color-primary); z-index: 5; }
.order-handle { cursor: grab; color: var(--ink-soft); font-size: 1.2rem; touch-action: none; padding: 4px 8px; }

/* ---- 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; }
.confirm-box label { display: block; font-weight: 700; margin-bottom: 4px; }
.confirm-box input[type="password"] {
  width: 100%; max-width: 180px; padding: 10px 12px; font-size: 1rem;
  border: 2px solid var(--color-border); border-radius: var(--radius-sm);
  background: var(--color-surface); color: var(--ink); margin-bottom: 8px;
  letter-spacing: 0.3em;
}
.confirm-box input[type="password"]:focus { outline: none; border-color: var(--color-primary); }

/* ---- 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; }
}
