/* Iuris Tantum · Versión móvil (tipo app) y reglas por materia */

@media (max-width: 768px) {
  header { padding: 0.5rem 0.75rem; }
  .header-inner { flex-wrap: wrap; gap: 0.5rem; }
  .brand-wrap { gap: 0.55rem; flex: 1; }
  .brand-text-block h1.wordmark img { height: 40px; }
  .hero-seal { width: 180px; height: 180px; margin-bottom: 1rem; }
  .hero-seal-stack { margin-bottom: 1rem; }
  .brand-text-block h1 { font-size: 0.85rem; }
  .brand-text-block p { display: none; }
  .menu-toggle { display: inline-flex; }
  .nav-buttons {
    display: none;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.35rem;
    width: 100%;
    padding-bottom: 0.25rem;
  }
  .nav-buttons.open { display: grid; }
  .nav-btn {
    padding: 0.6rem 0.25rem;
    font-size: 0.75rem;
    justify-content: center;
  }
  main { padding: 0.9rem 0.6rem; }
  .main-card { padding: 1.2rem 0.85rem; border-radius: var(--radius-md); }
  .mode-tabs {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.35rem;
  }
  .selection-grid { grid-template-columns: 1fr 1fr; }
  #countContainer { grid-template-columns: repeat(3, 1fr) !important; }
  .quiz-hud { flex-direction: column; align-items: stretch; }
  .hud-actions-group { justify-content: space-between; width: 100%; }
  .stats-matrix { grid-template-columns: 1fr 1fr; }
  .modal-window { max-height: 92vh; width: 95vw; }
  .fc-buttons-eval { grid-template-columns: 1fr; }
}

/* Elementos que sólo existen en la versión móvil */
.header-exit-btn, .quiz-progress, .comp-mobile { display: none; }

/* ============================================================
   VERSIÓN MÓVIL (APP): todo lo de acá abajo sólo aplica en celular;
   en PC la app se ve exactamente igual que antes.
   ============================================================ */
@media (max-width: 768px) {
  :root { --tabbar-h: 64px; --safe-b: env(safe-area-inset-bottom, 0px); }

  html { -webkit-text-size-adjust: 100%; }
  body { min-height: 100dvh; overscroll-behavior-y: none; }

  /* Barra superior compacta */
  header {
    padding: calc(0.45rem + env(safe-area-inset-top, 0px)) 0.9rem 0.45rem;
    background: rgba(10, 12, 17, 0.97);
    /* backdrop-filter/transform en un ancestro rompen el position: fixed de los hijos */
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .main-card { animation-name: fadeIn; }
  .rationale-drawer, .hint-toast-card { animation-name: fadeIn; }
  .header-inner { flex-wrap: nowrap; min-height: 40px; }
  .brand-text-block h1.wordmark img { height: 40px; }
  .menu-toggle { display: none; }

  .header-exit-btn {
    align-items: center;
    gap: 0.35rem;
    background: var(--bg-card);
    border: 1px solid var(--border-bright);
    color: var(--text-high);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 800;
    min-height: 40px;
    padding: 0 0.85rem;
    border-radius: 999px;
    cursor: pointer;
  }
  body[data-screen="quiz"] .header-exit-btn,
  body[data-screen="flashcards"] .header-exit-btn { display: inline-flex; }

  /* Menú inferior tipo app (reemplaza al menú hamburguesa) */
  .nav-buttons,
  .nav-buttons.open {
    display: grid;
    /* Una columna por botón visible (en el inicio "Cuadro" está oculto: 4, en Reales: 5) */
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 0;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 60;
    width: 100%;
    height: calc(var(--tabbar-h) + var(--safe-b));
    padding: 0 0.25rem var(--safe-b);
    background: rgba(10, 12, 17, 0.96);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border-subtle);
  }
  .nav-buttons .nav-btn,
  .nav-buttons .nav-btn.btn-nav-highlight {
    flex-direction: column;
    justify-content: center;
    gap: 0.25rem;
    height: var(--tabbar-h);
    padding: 0;
    background: none;
    border: none;
    border-radius: 0;
    color: var(--text-mid);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.01em;
  }
  .nav-buttons .nav-btn svg { width: 22px; height: 22px; stroke-width: 2; }
  .nav-buttons .nav-btn:hover { background: none; color: var(--text-mid); }
  .nav-buttons .nav-btn:active { color: var(--gold-primary); background: none; }
  body[data-screen="subject"] #btnGoHome,
  body[data-screen="config"] #btnGoHome,
  body[data-screen="results"] #btnGoHome { color: var(--gold-primary); }
  body[data-screen="quiz"] .nav-buttons,
  body[data-screen="flashcards"] .nav-buttons { display: none; }

  /* Contenido a pantalla completa, sin "tarjeta dentro de la página" */
  main {
    justify-content: flex-start;
    padding: 1rem 1rem calc(var(--tabbar-h) + var(--safe-b) + 1.5rem);
    --bg-inset: #11151d;
  }
  .main-card,
  .main-card.main-card {
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
    overflow: visible;
  }
  .main-card::before { display: none; }

  footer { padding: 1rem 1rem calc(var(--tabbar-h) + var(--safe-b) + 1rem); }
  .footer-inner { justify-content: center; text-align: center; font-size: 0.72rem; gap: 0.35rem; }
  .footer-inner > div:first-child { display: none; }
  body[data-screen="quiz"] footer,
  body[data-screen="flashcards"] footer { display: none; }

  .kbd-hint { display: none; }

  /* Pantalla de inicio */
  .hero-seal { width: 150px; height: 150px; margin: 0.5rem auto 1rem; }
  .hero-seal-stack { margin: 0.5rem auto 1rem; }
  .subject-intro { margin-bottom: 1.25rem; }
  .subject-intro h2 { font-size: 1.3rem; }
  .subject-grid { grid-template-columns: 1fr; gap: 0.75rem; }
  .subject-card {
    display: grid;
    grid-template-columns: 44px 1fr;
    column-gap: 0.85rem;
    row-gap: 0.2rem;
    padding: 1rem;
    border-radius: 18px;
    background: var(--bg-card);
  }
  .subject-card > :not(.subject-card-icon) { grid-column: 2; }
  .subject-card-icon {
    grid-row: 1 / span 3;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    background: rgba(251, 191, 36, 0.1);
    border-radius: 12px;
  }
  .subject-card-title { font-size: 1.02rem; }
  .subject-card-desc { font-size: 0.8rem; }
  .subject-card-meta { font-size: 0.66rem; }

  /* Configuración */
  .subject-crumb { margin-bottom: 1rem; flex-wrap: nowrap; }
  .subject-crumb-title { font-size: 1.25rem; }
  .link-btn { min-height: 34px; padding: 0 0.85rem; font-size: 0.74rem; }

  .mode-tabs {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.3rem;
    padding: 0.3rem;
    margin-bottom: 1rem;
    border-radius: 16px;
    background: var(--bg-card);
  }
  .tab-btn {
    flex-direction: row;
    justify-content: flex-start;
    gap: 0.5rem;
    min-height: 46px;
    padding: 0 0.75rem;
    font-size: 0.8rem;
    text-align: left;
    border-radius: 12px;
  }
  .tab-btn svg { flex-shrink: 0; width: 17px; height: 17px; }
  .tab-btn .badge-exam-pill { display: none; }
  .tab-btn.active { background: rgba(251, 191, 36, 0.12); box-shadow: inset 0 0 0 1.5px var(--gold-primary); }

  .exam-banner { padding: 0.75rem 0.85rem; gap: 0.65rem; margin-bottom: 1rem; border-radius: 14px; }
  .exam-banner-icon { font-size: 1.2rem; }
  .exam-banner-text h4 { font-size: 0.78rem; }
  .exam-banner-text p { font-size: 0.76rem; }

  .config-section-title { font-size: 0.74rem; margin-bottom: 0.6rem; }
  .topics-header { margin-bottom: 0.4rem; }
  .topics-hint { font-size: 0.72rem; margin: 0 0 0.7rem; }
  .selection-grid { gap: 0.5rem; margin-bottom: 1.25rem; }
  .card-label { padding: 0.75rem; border-radius: 14px; }
  .card-title { font-size: 0.86rem; }
  .card-subtitle { font-size: 0.68rem; }
  .topic-stat { margin-top: 0.3rem; font-size: 0.64rem; }
  #difficultyContainer .card-label { min-height: 0; }
  #countContainer { gap: 0.5rem; }
  #countContainer .card-label { padding: 0.65rem 0.75rem; }

  /* Botón Comenzar fijo arriba del menú inferior */
  #btnLaunchQuiz {
    position: fixed;
    left: 1rem;
    right: 1rem;
    bottom: calc(var(--tabbar-h) + var(--safe-b) + 0.6rem);
    width: auto;
    margin: 0;
    z-index: 55;
    min-height: 54px;
    padding: 0 1rem;
    font-size: 0.9rem;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6), 0 4px 18px rgba(251, 191, 36, 0.25);
  }
  body[data-screen="config"] main { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 6rem); }
  details.diagnostic-panel { margin-top: 0.5rem; padding: 0.9rem; border-radius: 16px; }
  .semaphore-grid { grid-template-columns: 1fr 1fr; gap: 0.5rem; }

  /* Pregunta en curso */
  .quiz-progress {
    display: block;
    height: 4px;
    border-radius: 999px;
    background: var(--bg-card);
    overflow: hidden;
    margin-bottom: 0.9rem;
  }
  .quiz-progress > span {
    display: block;
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, var(--gold-dark), var(--gold-primary));
    transition: width 0.3s ease;
  }
  .quiz-hud {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    padding-bottom: 0.85rem;
    margin-bottom: 1rem;
  }
  .hud-tags { flex: 1 1 100%; gap: 0.35rem; }
  .hud-tag { font-size: 0.64rem; padding: 0.2rem 0.5rem; }
  .hud-actions-group { flex: 1 1 100%; gap: 0.45rem; justify-content: flex-start; }
  .hud-actions-group > div:last-child { margin-left: auto; font-size: 0.9rem !important; }
  .score-badge-live { font-size: 0.75rem; min-height: 36px; padding: 0 0.6rem; }
  .btn-hint { min-height: 36px; font-size: 0.75rem; }
  .timer-container { min-height: 36px; }
  .timer-linear-track { margin-bottom: 1rem; }

  .case-narrative { padding: 0.85rem 0.95rem; font-size: 0.88rem; margin-bottom: 1rem; }
  .question-headline { font-size: 1.12rem; line-height: 1.38; margin-bottom: 1rem; }
  .options-grid { gap: 0.6rem; margin-bottom: 1rem; }
  .opt-choice-btn {
    min-height: 58px;
    align-items: center;
    padding: 0.8rem 0.9rem;
    font-size: 0.93rem;
    border-radius: 16px;
    background: var(--bg-card);
    transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
  }
  .opt-choice-btn:active:not(:disabled) { transform: scale(0.985); }
  .opt-prefix { width: 30px; height: 30px; border-radius: 9px; }
  .opt-choice-btn:disabled:not(.correct-choice):not(.wrong-choice) { opacity: 0.55; }

  .hint-toast-card { padding: 0.85rem; }
  .hint-toast-title { flex-direction: column; align-items: flex-start; gap: 0.2rem; }
  .hint-toast-text { font-size: 0.82rem; }

  .rationale-drawer { padding: 1rem; border-radius: 16px; scroll-margin-top: 70px; }
  .rationale-status { flex-wrap: wrap; font-size: 1rem; }
  .points-earned-pill { margin-left: 0; font-size: 0.7rem; }
  .article-pill { font-size: 0.72rem; }
  .rationale-text { font-size: 0.88rem; }
  .traps-section-title { font-size: 0.7rem; }
  .trap-item { font-size: 0.8rem; }

  /* "Siguiente" como barra fija al pie */
  .rationale-actions,
  .rationale-actions.rationale-actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    margin: 0;
    padding: 0.7rem 1rem calc(0.7rem + var(--safe-b));
    background: linear-gradient(to top, rgba(7, 8, 10, 0.98) 65%, rgba(7, 8, 10, 0));
  }
  .btn-advance {
    width: 100%;
    min-height: 54px;
    justify-content: center;
    font-size: 1rem;
    border-radius: 16px;
  }
  body[data-screen="quiz"] main,
  body[data-screen="flashcards"] main { padding-bottom: calc(var(--safe-b) + 6.5rem); }

  /* Flashcards */
  .flashcard-stage { gap: 0.9rem; }
  .flashcard-header { padding-bottom: 0.75rem; }
  .flashcard-header .hud-tag-diff { display: none; }
  .flashcard-header > div:last-child { font-size: 0.8rem !important; white-space: nowrap; }
  #fcAnswerHeading { display: block; margin: 0.35rem 0 0 !important; }
  .flashcard-box { padding: 1.25rem 1rem; min-height: 0; border-radius: 20px; border-width: 1.5px; }
  .fc-prompt-lead { font-size: 0.66rem; margin-bottom: 0.75rem; }
  .fc-question-text { font-size: 1.08rem; margin-bottom: 0.25rem; }
  .fc-answer-container { padding-top: 1rem; margin-top: 1rem; }
  #btnFlipFlashcard,
  #btnFlipFlashcard.btn-master-action {
    position: fixed;
    left: 1rem;
    right: 1rem;
    bottom: calc(0.8rem + var(--safe-b));
    width: auto !important;
    margin: 0 !important;
    min-height: 54px;
    padding: 0 1rem !important;
    font-size: 0.82rem;
    letter-spacing: 0.02em;
    border-radius: 16px;
    z-index: 40;
  }
  .fc-buttons-eval {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.45rem;
    margin: 0;
    padding: 0.7rem 0.75rem calc(0.7rem + var(--safe-b));
    background: linear-gradient(to top, rgba(7, 8, 10, 0.98) 70%, rgba(7, 8, 10, 0));
  }
  .fc-btn { min-height: 56px; justify-content: center; font-size: 0.76rem; border-radius: 14px; text-align: center; }
  .fc-btn span + span { display: none; }

  /* Resultados */
  .badge-icon-result { width: 56px; height: 56px; font-size: 1.6rem; margin-bottom: 0.75rem; }
  .result-score-callout { font-size: 2.6rem; }
  .result-verdict { font-size: 1.15rem; }
  .result-summary-msg { font-size: 0.82rem; margin-bottom: 1.1rem; }
  .stats-matrix { gap: 0.5rem; margin-bottom: 1.1rem; }
  .stat-cell { background: var(--bg-card); padding: 0.65rem 0.4rem; }
  .stat-cell-lbl { font-size: 0.6rem; }
  #btnRestartQuiz { min-height: 54px; padding: 0 1rem; font-size: 0.88rem; margin-top: 0; }
  #btnRetryWrong { min-height: 50px; padding: 0 1rem; font-size: 0.84rem; }
  .breakdown-wrapper { margin-top: 1rem; padding-top: 1.1rem; }
  .breakdown-header { font-size: 0.92rem; }
  .breakdown-header > span:last-child { display: none; }
  .review-card { padding: 0.9rem; background: var(--bg-card); }
  .review-prompt { font-size: 0.88rem; }
  .review-ans-line { flex-direction: column; gap: 0.1rem; }
  .review-ans-label { min-width: 0; }

  /* Ventanas como hojas que suben desde abajo */
  .modal-veil { align-items: flex-end; padding: 0; }
  .modal-window,
  .modal-window.modal-window {
    width: 100%;
    max-width: none !important;
    max-height: 92dvh;
    border-radius: 22px 22px 0 0;
    border-bottom: none;
    padding-bottom: var(--safe-b);
    animation: sheetUp 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  }
  .modal-top { padding: 0.9rem 1rem; position: relative; }
  .modal-top::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    width: 38px;
    height: 4px;
    margin-left: -19px;
    border-radius: 999px;
    background: var(--border-bright);
  }
  .modal-top > div { font-size: 0.95rem !important; }
  .modal-top > button {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    border-radius: 50% !important;
    background: var(--bg-inset) !important;
    font-size: 1.3rem !important;
    line-height: 1;
  }
  .modal-search { padding: 0.65rem 1rem; }
  .search-input { font-size: 16px; min-height: 44px; } /* 16px: evita el zoom automático de iOS */
  .modal-scroll { padding: 1rem; overscroll-behavior: contain; }
  .tutorial-step-card { padding: 0.85rem; gap: 0.75rem; }

  #confirmHomeModal .modal-window > div:last-child > div { flex-direction: column-reverse; }
  #confirmHomeModal .modal-window > div:last-child button { width: 100% !important; min-height: 50px; justify-content: center; }
  #btnStayInQuiz { color: var(--text-high); }

  /* La tarjeta de autor sigue centrada */
  #authorModal { align-items: center; padding: 1rem; }
  #authorModal .modal-window { border-radius: var(--radius-lg); padding-bottom: 0; }

  /* Cuadro comparativo: un instituto por vez en lugar de la tabla ancha */
  .comparative-table-wrap { display: none; }
  .comp-mobile { display: block; }
  .comp-chips {
    display: flex;
    gap: 0.4rem;
    overflow-x: auto;
    padding-bottom: 0.6rem;
    margin: 0 -1rem 0.4rem;
    padding-left: 1rem;
    padding-right: 1rem;
    scrollbar-width: none;
  }
  .comp-chips::-webkit-scrollbar { display: none; }
  .comp-chip {
    flex-shrink: 0;
    min-height: 38px;
    padding: 0 0.9rem;
    border-radius: 999px;
    border: 1px solid var(--border-bright);
    background: var(--bg-inset);
    color: var(--text-mid);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 800;
    cursor: pointer;
  }
  .comp-chip[aria-pressed="true"] { border-color: var(--gold-primary); color: #07080a; background: var(--gold-primary); }
  .comp-row { padding: 0.75rem 0; border-bottom: 1px solid var(--border-subtle); }
  .comp-row:last-child { border-bottom: none; }
  .comp-row-label { font-size: 0.68rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em; color: var(--gold-primary); margin-bottom: 0.25rem; }
  .comp-row-value { font-size: 0.88rem; color: #e5e7eb; line-height: 1.45; }

  .app-toast { top: calc(60px + env(safe-area-inset-top, 0px)) !important; }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

@media (max-width: 768px) and (prefers-reduced-motion: reduce) {
  .modal-window { animation: none; }
}

/* El cuadro comparativo es de Derechos Reales: no se ofrece fuera de esa materia */
body:not([data-subject]) #btnOpenComparative,
body:not([data-subject]) #btnCrumbComparative { display: none !important; }
/* En celular el cuadro ya está en la barra inferior */
@media (max-width: 768px) { #btnCrumbComparative { display: none; } }
