/* ==========================================================================
   MATH ENGINE - UI RESPONSIVA COMPACTA (todo visible, sin barra de scroll)
   ========================================================================== */

:root {
  --surface-hover: #1c222e;
  --danger-bg: rgba(224, 156, 156, 0.14);
  --success-bg: rgba(156, 184, 116, 0.16);

  --radius-card: 16px;
  --radius-btn: 10px;
  --font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
}

#app {
  color-scheme: dark;
}

[data-theme="light"] {
  --surface-hover: #e5e9de;
  --danger-bg: rgba(225, 170, 170, 0.28);
  --success-bg: rgba(156, 184, 116, 0.28);
}

[data-theme="light"] #app {
  color-scheme: light;
  --danger: #d9534f;
}

:where(#app, #app *) {
  font-family: var(--font-family);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

/* iOS Safari no deja escribir en inputs con user-select: none */
input {
  user-select: text;
  -webkit-user-select: text;
}

#app :focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.hidden {
  display: none !important;
}

.app-layout {
  width: 100%;
  min-height: calc(100dvh - var(--nav-height));
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  height: 60px;
  width: 100%;
  max-width: 850px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--nav-bg);
  backdrop-filter: blur(12px);
  flex-shrink: 0;
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-stat {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.92rem;
  font-weight: 600;
}

.brand-title {
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: 1px;
  color: var(--link);
  text-transform: uppercase;
}

/* Los 4 botones del menú (⚙, sonido, idioma, tema): exactamente el mismo tamaño */
.nav-control-btn {
  width: 40px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition: all 0.2s ease;
}

.nav-control-btn:hover {
  color: var(--primary);
  border-color: var(--primary);
}

/* Banner AdSense móvil: bajo el header, fuera del scroll, siempre visible */
.adsense-mobile-slot {
  width: 100%;
  padding: 4px 0;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

/* Banners propios (en lugar de anuncios) */
.banner-link {
  display: block;
  width: 100%;
}

.banner-img {
  display: block;
  width: 100%;
  max-width: 640px;
  height: auto;
  margin: 0 auto;
  border-radius: 6px;
}

/* App Body */
.app-body {
  width: 100%;
  max-width: 850px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  flex: 1;
  min-height: 0;
}

.main-column {
  flex: none;
  width: 100%;
  min-height: 0;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  overflow: hidden;
  position: relative;
  box-shadow: 0 4px 24px var(--card-hover-shadow);
}

.desktop-sidebar {
  display: flex;
  width: 100%;
  flex-direction: column;
  gap: 14px;
}

.sidebar-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 18px;
  display: flex;
  flex-direction: column;
}

.channel-card h4 {
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--link);
  margin-bottom: 6px;
}

.channel-card p {
  font-size: 0.88rem;
  color: var(--text-muted);
  line-height: 1.4;
  margin-bottom: 14px;
}

.sidebar-link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background-color: #e83535;
  color: #ffffff !important;
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid #e83535;
  transition: all 0.2s ease;
}

.sidebar-link-btn:hover {
  background-color: #e44141;
  transform: translateY(-2px);
}

.yt-icon {
  flex-shrink: 0;
}

.ad-sidebar-card {
  padding: 0;
  display: none;
  overflow: hidden;
}

/* Vistas: si en una pantalla muy pequeña algo no cabe, se puede desplazar sin barra visible */
.view {
  display: none;
  flex-direction: column;
  padding: 16px 18px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: none;
}

.view::-webkit-scrollbar {
  display: none;
}

.view.active {
  display: flex;
}

/* ==========================================================================
   HOME
   ========================================================================== */
.mascot-row-container {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.mascot-img-row {
  width: 64px;
  height: auto;
  flex-shrink: 0;
}

.mascot-bubble-row {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 10px 14px;
  border-radius: 12px;
  flex: 1;
}

.mascot-bubble-row p {
  font-size: 0.86rem;
  line-height: 1.4;
  color: var(--text-muted);
}

.mascot-bubble-row::after {
  content: "";
  position: absolute;
  left: -6px;
  top: 50%;
  transform: translateY(-50%) rotate(45deg);
  width: 10px;
  height: 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  border-left: 1px solid var(--border);
}

/* Desplegables */
.acc {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  margin-bottom: 8px;
  flex-shrink: 0;
}

.acc > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-main);
}

.acc > summary::-webkit-details-marker {
  display: none;
}

.acc > summary::after {
  content: "▾";
  color: var(--primary);
  transition: transform 0.15s ease;
}

.acc[open] > summary::after {
  transform: rotate(180deg);
}

.acc-body {
  padding: 2px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.acc-note {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.35;
}

.mode-label {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

/* 16px: evita el zoom automático de iOS al enfocar un select */
.select-field {
  width: 100%;
  min-width: 0;
  background: var(--surface);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 8px;
  font-size: 16px;
  cursor: pointer;
}

.chips-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.table-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 7px 0;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.table-chip:hover {
  border-color: var(--primary);
}

.table-chip.active {
  background: var(--btn-bg);
  color: var(--btn-text);
  border-color: var(--primary);
}

.btn-wide {
  width: 100%;
  padding: 10px 18px;
  font-size: 0.92rem;
}

/* Progreso por tabla */
.progress-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.progress-tile {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  background: var(--surface);
}

.progress-tile strong {
  font-size: 0.9rem;
}

.progress-tile span {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.progress-tile.good {
  border-color: var(--primary);
  background: var(--success-bg);
}

.progress-tile.low {
  border-color: var(--danger);
  background: var(--danger-bg);
}

/* ==========================================================================
   PRÁCTICA
   ========================================================================== */
.practice-view-fixed {
  justify-content: space-between;
  padding: 16px 20px;
}

.practice-view-fixed.active {
  display: flex;
  flex-direction: column;
}

.practice-top-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin-bottom: auto;
  flex-shrink: 0;
}

.exit-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0 4px;
}

.exit-btn:hover {
  color: var(--danger);
}

.lesson-progress-bar {
  flex: 1;
  height: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--primary);
  transition: width 0.3s ease;
}

.progress-count {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.round-label {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--btn-text);
  background: var(--btn-bg);
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}

.practice-operation-card {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 0 20px;
  flex-shrink: 0;
}

.operation-display {
  font-size: clamp(1.7rem, 8vw, 2.7rem);
  font-weight: 700;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px 14px;
}

.answer-box {
  display: inline-block;
  min-width: 50px;
  padding: 0 8px;
  border-bottom: 3px solid var(--primary);
  text-align: center;
  color: var(--primary);
}

.practice-interaction-area {
  width: 100%;
  min-height: 175px;
  max-height: 175px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 0 18px;
}

/* MODO ESCRIBIR: operación, campo y feedback juntos y centrados (sin huecos) */
.practice-view-fixed.practice-write .practice-top-bar {
  margin-bottom: auto;
}

.practice-view-fixed.practice-write .practice-operation-card {
  margin: 0 0 20px;
}

.practice-view-fixed.practice-write .practice-interaction-area {
  min-height: 64px;
  max-height: 64px;
  margin: 0 0 18px;
}

.practice-view-fixed.practice-write .feedback-tray {
  margin-bottom: auto;
}

.options-container-6 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(3, auto);
  gap: 8px;
  width: 100%;
  height: 100%;
}

.option-btn {
  position: relative;
  background: var(--bg);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: clamp(1.05rem, 4.6vw, 1.35rem);
  font-weight: 600;
  padding: 9px 0;
  cursor: pointer;
  transition: all 0.12s ease;
}

.option-btn:focus:not(:focus-visible) {
  outline: none;
}

.option-btn:not(:disabled):hover {
  border-color: var(--primary);
  background: var(--surface-hover);
  color: var(--primary);
}

.option-btn:disabled {
  cursor: default;
}

.option-btn.correct,
#write-input.correct {
  border-color: var(--primary);
  background: var(--success-bg);
  color: var(--primary);
}

.option-btn.wrong,
#write-input.wrong {
  border-color: var(--danger);
  background: var(--danger-bg);
  color: var(--danger);
}

.key-badge {
  position: absolute;
  top: 3px;
  left: 8px;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-muted);
  opacity: 0.5;
}

.write-input-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}

.write-form-box {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 340px;
}

#write-input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1.8rem;
  font-weight: 700;
  text-align: center;
  padding: 8px;
  outline: none;
}

#write-input:focus {
  border-color: var(--primary);
}

/* Bandeja de feedback: espacio siempre reservado (sin saltos) */
.feedback-tray {
  width: 100%;
  min-height: 130px;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: stretch;
  gap: 10px;
  background: transparent;
  box-sizing: border-box;
  margin-bottom: auto;
  flex-shrink: 0;
  visibility: hidden;
}

.feedback-content {
  flex: 1 1 auto;
  min-width: 0;
  scrollbar-width: none;
}

.feedback-content::-webkit-scrollbar {
  display: none;
}

.feedback-tray.correct {
  visibility: visible;
  background: var(--success-bg);
  border-color: var(--primary);
}

.feedback-tray.incorrect {
  visibility: visible;
  background: var(--danger-bg);
  border-color: var(--danger);
}

.feedback-tray.correct h3 {
  color: var(--primary);
  font-size: 1rem;
}

.feedback-tray.incorrect h3 {
  color: var(--danger);
  font-size: 1rem;
}

.feedback-tray p {
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.3;
}

.feedback-tray .btn-primary {
  align-self: flex-end;
}

.btn-primary {
  background: var(--btn-bg);
  color: var(--btn-text);
  border: 1px solid var(--border);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Resumen Final */
.summary-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  gap: 14px;
}

/* Gato del resumen final: ajusta aquí su tamaño (mín, ideal según alto de pantalla, máx) */
.mascot-img {
  width: clamp(90px, 22vh, 140px);
  height: auto;
}

.summary-title {
  font-size: 1.6rem;
  font-weight: 800;
}

.summary-subtitle {
  color: var(--text-muted);
  font-size: 0.88rem;
}

.summary-stats {
  display: flex;
  gap: 16px;
}

.stat-box {
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 12px 20px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
}

.stat-number {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--primary);
}

.stat-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

/* Puerta de acceso (adblock) */
.gate-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
}

.gate-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 24px;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  text-align: center;
}

.gate-card h2 {
  font-size: 1.2rem;
}

.gate-card p {
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

/* Ventana de confirmación */
.confirm-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  width: 100%;
  margin-top: 4px;
}

.btn-secondary {
  background: transparent;
  color: var(--text-main);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-hover);
}

/* Texto informativo bajo la app */
.app-article {
  max-width: 850px;
  margin: 30px auto 0;
  padding: 0 20px;
  line-height: 1.8;
}

html[lang="es"] .text-en,
html[lang="en"] .text-es {
  display: none;
}

/* Escritorio: el anuncio va en la barra lateral */
@media (min-width: 900px) {
  .app-body {
    flex-direction: row;
  }
  .main-column {
    flex: 1;
    width: auto;
    max-width: 580px;
    margin: 0;
  }
  .desktop-sidebar {
    width: 300px;
  }
  .ad-sidebar-card {
    display: flex;
  }
  .adsense-mobile-slot {
    display: none;
  }
}

/* Pantallas bajas: compactar para que todo quepa sin scroll */
@media (max-height: 760px) {
  .mascot-img-row {
    width: 48px;
  }
  .mascot-bubble-row {
    padding: 8px 12px;
  }
  .mascot-bubble-row p {
    font-size: 0.8rem;
  }
  .practice-interaction-area {
    min-height: 150px;
    max-height: 150px;
  }
  .feedback-tray {
    min-height: 110px;
  }
}

/* Pantallas muy bajas: el gato sigue visible, en una sola fila compacta */
@media (max-height: 640px) {
  .mascot-row-container {
    margin-bottom: 8px;
  }
  .mascot-img-row {
    width: 40px;
  }
  .mascot-bubble-row p {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

@media (max-width: 600px) {
  .app-body {
    padding: 8px;
  }
  .main-column {
    border-radius: 12px;
  }
  .view {
    padding: 12px;
  }
}

/* 4 botones + estadísticas + marca en pantallas estrechas */
@media (max-width: 480px) {
  .app-header {
    padding: 0 8px;
  }
  .header-left,
  .header-right {
    gap: 6px;
  }
  .header-stat {
    font-size: 0.82rem;
  }
  .brand-title {
    font-size: 0.74rem;
    letter-spacing: 0;
  }
  .nav-control-btn {
    width: 34px;
    height: 30px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #app,
  #app * {
    transition: none !important;
    animation: none !important;
  }
}
