@media (prefers-reduced-motion: reduce) {
  .hero-wave,
  .login-wave { animation: none; }
}
@media (min-width: 981px) {
  .grid.cols-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
@media (max-width: 980px) {
  .app-shell,
  .app-shell.student-shell,
  .app-shell.teacher-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: min(84vw, 320px);
    height: 100dvh;
    overflow-y: auto;
    transform: translateX(-105%);
    transition: transform 0.18s ease;
    box-shadow: 18px 0 42px rgba(54, 49, 42, 0.18);
    /* Fondo sólido: la versión de escritorio es semitransparente (0.96) porque
       queda fija sobre un fondo estable; acá el cajón flota encima del panel
       con contenido que se sigue desplazando detrás, y ese contenido se
       transparentaba y ensuciaba la lectura del menú. */
    background: var(--paper);
  }

  .app-shell.nav-open .sidebar {
    transform: translateX(0);
  }

  .mobile-menu-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
  }

  .mobile-menu-backdrop {
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(44, 42, 38, 0.24);
    border: 0;
    padding: 0;
  }

  .app-shell.nav-open .mobile-menu-backdrop {
    display: block;
  }

  .app-shell.auth-locked .mobile-menu-backdrop {
    display: none;
  }

  .profile-mobile-nav {
    position: fixed;
    top: 78px;
    left: 12px;
    right: 12px;
    z-index: 45;
    display: none;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 253, 248, 0.98);
    box-shadow: var(--shadow);
  }

  .app-shell.nav-open .profile-mobile-nav:not([hidden]) {
    display: grid;
  }

  .profile-mobile-nav a {
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px;
    border-radius: 8px;
    background: var(--sage-2);
    color: #3f513b;
    text-decoration: none;
    font-weight: 700;
    font-size: 14px;
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 30;
  }

  .nav {
    grid-template-columns: 1fr;
  }

  .grid.cols-3,
  .portal-hero,
  .student-shell .portal-hero,
  .enrollment-layout,
  .public-hero,
  .login-panel {
    grid-template-columns: 1fr;
  }

  .public-class-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .public-hero {
    min-height: auto;
  }

  .public-hero-visual {
    min-height: 300px;
    order: -1;
  }

  .public-contact {
    align-items: flex-start;
    flex-direction: column;
  }

  .student-shell .portal-hero .surface {
    order: 0;
  }

  /* Student home stats stay 3-up at tablet widths */
  .sh-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .student-shell .content,
  .teacher-shell .content,
  .student-shell .content > .surface,
  .student-shell .content > .grid,
  .teacher-shell .content > .surface,
  .teacher-shell .content > .grid,
  .student-shell .portal-hero,
  .teacher-shell .portal-hero,
  .stack-form,
  .student-shell .profile-details-body {
    max-width: none;
  }

  .enrollment-layout {
    max-width: 760px;
  }

  .enrollment-side,
  .enrollment-layout > .surface {
    max-width: none;
  }

  .agenda-toolbar {
    grid-template-columns: 1fr;
  }

  .agenda-period,
  .agenda-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 680px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    padding: 18px;
    min-height: auto;
  }

  .topbar-title-row {
    width: 100%;
    align-items: flex-start;
  }

  .topbar h1 {
    font-size: 26px;
  }

  .content {
    padding: 16px;
  }

  .grid.cols-2,
  .field-row,
  .checkbox-grid {
    grid-template-columns: 1fr;
  }

  /* Recibo de cobro agrupado: compactar en pantallas angostas */
  #receiptLines .rl-row { gap: 6px; }
  #receiptLines .rl-amount { width: 74px; }

  .portal-summary-grid,
  .grid.cols-2.portal-summary-grid,
  .grid.cols-3:has(> .stat) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .portal-summary-grid {
    max-width: 100%;
    width: 100%;
    gap: 10px;
  }

  .grid:has(> .stat) {
    gap: 10px;
  }

  .grid:has(> .stat) > .stat {
    min-height: 92px;
    padding: 12px;
  }

  .grid:has(> .stat) > .stat strong {
    font-size: 22px;
    line-height: 1.08;
    overflow-wrap: anywhere;
  }

  .grid:has(> .stat) > .stat span,
  .grid:has(> .stat) > .stat small {
    font-size: 12px;
    line-height: 1.25;
  }

  .checkbox-grid.compact-options {
    grid-template-columns: 1fr;
  }

  .price-option-grid {
    grid-template-columns: 1fr;
  }

  .public-home {
    gap: 14px;
  }

  .public-hero-copy {
    padding: 24px;
  }

  .public-hero-copy p:not(.eyebrow) {
    font-size: 16px;
  }

  .public-hero-visual {
    min-height: 300px;
  }

  .public-sello {
    width: min(300px, 78%);
    aspect-ratio: 1 / 1;
    inset: 6% auto auto 11%;
  }

  .public-class-card {
    min-height: 150px;
    padding: 14px;
  }

  .ryo-welcome-mark {
    width: min(290px, 76%);
    inset: 14px 8px auto auto;
  }

  .login-copy {
    min-height: 300px;
  }

  .topbar-actions {
    width: 100%;
    justify-content: space-between;
  }

  .mode-button {
    flex: 0 0 auto;
  }

  .mode-status {
    white-space: normal;
  }

  .agenda-views,
  .agenda-period,
  .agenda-actions {
    flex-wrap: wrap;
  }

  .agenda-date,
  .agenda-teacher-filter {
    width: min(100%, 220px);
  }

  .portal-banner {
    min-height: 220px;
  }

  .portal-banner h2 {
    font-size: 30px;
  }

  .calendar-grid {
    grid-template-columns: repeat(7, minmax(52px, 1fr));
    overflow-x: auto;
    gap: 4px;
  }

  .calendar-day {
    height: 112px;
    padding: 6px;
  }

  .calendar-event {
    font-size: 10px;
    padding: 3px 5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .calendar-more {
    font-size: 10px;
    margin-top: 2px;
  }

  .calendar-header-cell {
    font-size: 10px;
    padding: 2px 2px 6px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ryo-welcome-mark img,
  .ryo-mark-echo {
    animation: none;
    opacity: 0.35;
    transform: none;
  }
}

/* ══════════════════════════════════════════════════════
   MOBILE UX — mejoras targetizadas
   ══════════════════════════════════════════════════════ */

@media (max-width: 680px) {

  /* ── Topbar: barra compacta de fila única ── */
  .topbar {
    flex-direction: row;
    align-items: center;
    padding: 0 14px;
    min-height: 52px;
    gap: 10px;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  .topbar-title-row {
    flex: 1;
    min-width: 0;
    align-items: center;
    width: auto;
  }

  .topbar h1 {
    font-size: 17px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
  }

  #todayLabel {
    display: none;
  }

  .topbar-actions {
    flex: 0 0 auto;
    width: auto;
    justify-content: flex-end;
    padding: 2px 4px;
  }

  #modeStatus {
    display: none !important;
  }

  /* ── Dialog: bottom sheet deslizable ── */
  dialog {
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    max-height: 92vh; /* fallback */
    max-height: 92dvh;
    margin-block-start: auto;
    margin-block-end: 0;
    margin-inline: 0;
    border-radius: 18px 18px 0 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .dialog-head::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--line);
    margin: 0 auto 14px;
  }

  .dialog-content {
    padding: 16px 16px 28px;
  }

  /* ── Content: padding reducido ── */
  .content {
    padding: 14px 14px 40px;
  }

  /* ── Surface: más compacta ── */
  .surface {
    padding: 14px;
  }

  .surface h2,
  .surface h3 {
    font-size: 17px;
    margin-bottom: 12px;
  }

  /* ── Items: menos padding ── */
  .item {
    padding: 12px;
  }

  /* ── Section details ── */
  .section-details summary {
    min-height: 52px;
    padding: 12px 14px;
  }

  .section-details summary strong {
    font-size: 17px;
  }

  .section-details-body {
    padding: 12px 14px 14px;
  }

  /* ── Portal banner: más compacta ── */
  .portal-banner {
    min-height: 190px;
    padding: 20px;
  }

  .portal-banner h2 {
    font-size: 26px;
  }

  /* ── Buttons: grid en .actions para mejor touch ── */
  .dialog-content .actions,
  .section-details-body .actions,
  .item .actions {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 8px;
    margin-top: 12px;
  }

  .dialog-content .actions .primary-button,
  .dialog-content .actions .soft-button,
  .dialog-content .actions .danger-button,
  .section-details-body .actions .primary-button,
  .section-details-body .actions .soft-button,
  .section-details-body .actions .danger-button,
  .item .actions .primary-button,
  .item .actions .soft-button,
  .item .actions .danger-button {
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0 10px;
  }

  /* ── Inputs y selects: touch targets más grandes ── */
  input,
  select,
  textarea {
    min-height: 46px;
  }

  /* ── Compact-row: overflow controlado ── */
  .compact-row {
    align-items: center;
    gap: 8px;
  }

  .compact-row > div:first-child {
    min-width: 0;
    flex: 1;
  }

  .compact-row h4 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ── Item-head: nombre largo no desborda ── */
  .item-head > div:first-child {
    min-width: 0;
    flex: 1;
  }

  .item h3,
  .item h4 {
    overflow-wrap: break-word;
  }

  /* ── Tabla: más legible en scroll horizontal ── */
  .table-wrap {
    -webkit-overflow-scrolling: touch;
    border-radius: 8px;
  }

  /* ── Profile mobile nav: botones más grandes ── */
  .profile-mobile-nav a {
    min-height: 46px;
    font-size: 13px;
  }

  /* ── Stat grid: 2 columnas máximo ── */
  .grid.cols-3:has(> .stat) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  /* ── Receipt en dialog ── */
  .receipt-row {
    flex-wrap: wrap;
    gap: 4px;
  }

  /* ── Teacher portal nav ── */
  .teacher-nav {
    gap: 6px;
    padding: 8px;
  }

  .teacher-nav a {
    min-height: 40px;
    font-size: 13px;
    padding: 0 10px;
  }
}

/* ── Ultra-compact: teléfonos muy pequeños (≤430px) ── */
@media (max-width: 430px) {

  .content {
    padding: 10px 10px 32px;
  }

  .surface {
    padding: 12px;
  }

  .section-details-body {
    padding: 10px 12px 12px;
  }

  .item {
    padding: 10px 12px;
  }

  .section-details summary {
    min-height: 48px;
    padding: 10px 12px;
  }

  .section-details summary strong {
    font-size: 15px;
  }

  .section-details summary small {
    font-size: 12px;
  }

  .portal-banner {
    min-height: 150px;
    padding: 16px;
  }

  .portal-banner h2 {
    font-size: 22px;
  }

  .dialog-content {
    padding: 14px 14px 24px;
  }

  .dialog-head h2 {
    font-size: 17px;
  }

  .dialog-content .actions,
  .section-details-body .actions,
  .item .actions {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 6px;
  }

  .topbar h1 {
    font-size: 15px;
  }

  .topbar-actions {
    padding: 2px;
  }

  /* Stat: fuente más pequeña en pantallas muy chicas */
  .grid:has(> .stat) > .stat strong {
    font-size: 18px;
  }

  .grid:has(> .stat) > .stat {
    min-height: 80px;
    padding: 10px;
  }

  /* Portal summary grid: compensar el espacio reducido */
  .portal-summary-grid .stat {
    min-height: 76px;
    padding: 10px;
  }

  .portal-summary-grid .stat strong {
    font-size: 20px;
  }

  /* Item head: vertical si hay mucho contenido */
  .item-head {
    flex-wrap: wrap;
  }

  /* Pill: más compacta */
  .pill {
    font-size: 11px;
    padding: 0 7px;
    min-height: 22px;
  }

  /* Topbar-actions: solo mostrar "Salir" sin capsule */
  .topbar-actions {
    background: transparent;
    border: 0;
    padding: 0;
  }

  .mode-button {
    padding: 0 10px;
    font-size: 14px;
  }

  /* Tabla: min-width reducido para menos scroll */
  table {
    min-width: 520px;
  }
}

@media (max-width: 900px) {
  .ledger-head,
  .ledger-row {
    grid-template-columns: 46px 80px minmax(0, 1fr) 64px 52px 84px;
  }
  .ledger-head span:nth-child(4),
  .ledger-row .l-party {
    display: none;
  }
}

/* ── Portal alumna: barra de navegación inferior en móvil ── */
@media (max-width: 680px) {
  .student-shell .sidebar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    height: auto;
    transform: none !important;
    overflow: visible;
    border-radius: 0;
    padding: 0;
    box-shadow: 0 -2px 16px rgba(54, 49, 42, 0.12);
    z-index: 40;
  }

  .student-shell .brand {
    display: none;
  }

  .student-shell .nav {
    display: flex;
    flex-direction: row;
    gap: 0;
    padding: 0;
  }

  .student-shell .nav button {
    flex: 1;
    border-radius: 0;
    padding: 8px 4px 9px;
    font-size: 11.5px;
    font-weight: 600;
    letter-spacing: .2px;
    text-align: center;
    justify-content: center;      /* centra el texto (anula el space-between de base) */
    min-height: 56px;
    width: auto;
    color: var(--muted);
    border-top: 2px solid transparent;
    border-left: none;
    border-right: none;
    border-bottom: none;
  }

  .student-shell .nav button span {
    display: block;
    width: 100%;
    text-align: center;
    margin: 0;
  }

  .student-shell .nav button.active {
    border-top-color: var(--sage);
    background: var(--sage-2);
    color: var(--ink);
    font-weight: 700;
  }

  .student-shell .workspace {
    padding-bottom: 70px;
  }

  .student-shell .mobile-menu-button {
    display: none;
  }
}
@media (prefers-color-scheme: dark) {
  #tourCard { background: #23282a; }
  .tour-title { color: #eee; } .tour-text { color: #cfcfcf; } .tour-text b { color: #fff; }
  .tour-back { background: #3a4042; color: #ddd; }
  /* El módulo de Ayuda va en un diálogo que NO cambia a oscuro (la app es de tema
     claro). Por eso NO se aclara su texto: se dejan los colores base
     (#333/#555/#666), legibles sobre fondo claro en cualquier tema del sistema. */
}

/* ═══════════════════════════════════════════════════════════════════════
   Celdas RÍGIDAS y simétricas — grilla + vista Semana + calendario mensual.
   No se deforman al agregar/quitar clases. Responsive móvil/escritorio. (2026-07-23)
   ═══════════════════════════════════════════════════════════════════════ */
.tpl-grid { table-layout: fixed; }
.tpl-grid td { height: 92px; }
.tpl-cell {
  height: 92px;
  box-sizing: border-box;
  overflow: hidden;
  vertical-align: middle;
}
.tpl-empty {
  height: 92px;
  box-sizing: border-box;
  vertical-align: middle;
}
.tpl-add { height: 100%; min-height: 0; }
/* Columnas del mes perfectamente simétricas en cualquier ancho */
.calendar-grid { grid-template-columns: repeat(7, minmax(0, 1fr)); }

@media (max-width: 640px) {
  .tpl-grid td, .tpl-cell, .tpl-empty { height: 78px; }
  .tpl-type { font-size: 12px; line-height: 1.15; }
  .tpl-level, .tpl-meta { font-size: 10px; line-height: 1.2; }
  .calendar-day { height: 110px; padding: 6px; }
  .calendar-grid { gap: 4px; }
  .calendar-day-num { width: 22px; height: 22px; font-size: 12px; }
}

/* ─── Ajuste fino de tamaños (2026-07-23b): grilla + vista Semana más compactas ───
   Escritorio: −1/3 de alto y columnas ~mitad de ancho (calendario centrado).
   Móvil: celdas más bajas para eliminar el espacio en blanco. */
@media (min-width: 641px) {
  .tpl-grid { max-width: 680px; margin-left: auto; margin-right: auto; }
  .tpl-grid td, .tpl-cell, .tpl-empty { height: 62px; }
  .tpl-cell { padding: 6px 10px; }
}
@media (max-width: 640px) {
  .tpl-grid td, .tpl-cell, .tpl-empty { height: 56px; }
  .tpl-cell { padding: 5px 8px; }
  .tpl-type  { font-size: 11.5px; line-height: 1.12; }
  .tpl-level { font-size: 9.5px;  line-height: 1.15; }
  .tpl-meta  { font-size: 9.5px;  line-height: 1.15; margin-top: 1px; }
  .tpl-grid th.tpl-time { width: 46px; font-size: 11px; }
}

/* ─── Fix simetría (2026-07-23c) ───────────────────────────────────────────
   La grilla/vista Semana deben LLENAR su caja (no quedar flotando con hueco).
   El cuerpo nunca más chico que el contenedor. Se conserva el alto compacto. */
.tpl-grid { max-width: none; margin-left: 0; margin-right: 0; width: 100%; }
/* Calendario mensual: llena la caja, columnas parejas, alto estable */
.calendar-grid { width: 100%; }

/* ─── Tablas ordenables con scroll (Usuarios, Finanzas) 2026-07-23 ───
   overflow-x: en mobile estas tablas (Nombre/Rol/Detalle, Mes/Ingresos/Egresos/
   Comisiones/Saldo...) son más anchas que la pantalla; sin esto el contenido se
   salía de la tarjeta en vez de poder deslizarse para verlo. */
.data-scroll { max-height: 640px; overflow-y: auto; overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: 8px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table thead th {
  position: sticky; top: 0; z-index: 1; background: var(--paper, #fff);
  text-align: left; padding: 9px 10px; font-size: 12px; color: var(--muted);
  font-weight: 700; border-bottom: 2px solid var(--line); white-space: nowrap;
}
.data-table thead th[data-action] { cursor: pointer; user-select: none; }
.data-table thead th[data-action]:hover { color: var(--ink); }
.data-table tbody td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.data-table tbody tr:hover { background: var(--soft); }
.data-table tbody tr[data-action] { cursor: pointer; }
.data-table td.num, .data-table th.num { text-align: right; white-space: nowrap; }
.data-sort-ind { font-size: 9px; opacity: .55; margin-left: 4px; }
@media (max-width: 640px) {
  .data-scroll { max-height: 500px; }
  .data-table { font-size: 12px; }
  .data-table thead th, .data-table tbody td { padding: 6px 7px; }
}
/* Quien pidió no ver movimiento, no lo ve: el freno del doble clic igual actúa. */
@media (prefers-reduced-motion: reduce) {
  [data-action].is-busy::after,
  [data-submit].is-busy::after { animation: none; border-top-color: currentColor; opacity: .5; }
  body.ryo-procesando::before { animation: none; background-position: 0 0; background-size: 100% 100%; opacity: .5; }
}
