/*
 * Correcciones de vista móvil.
 * Se carga DESPUÉS de tailwind.css y de los theme-bundles para ganar la cascada
 * sin depender de recompilar Tailwind (el repo no tiene pipeline de build para eso).
 */

/* ===== Fase 0: layout general (sidebar como drawer/hamburguesa en móvil) ===== */

.sidebar-menu-toggle {
  display: none;
}

@media (max-width: 960px) {
  #layout {
    position: relative;
  }

  .sidebar-menu-toggle {
    display: inline-flex;
  }

  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    width: min(80vw, 300px);
    min-width: 0;
    height: 100vh;
    transform: translateX(0);
    transition: transform 0.25s ease;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.35);
  }

  #layout.sidebar-collapsed #sidebar {
    transform: translateX(-100%);
  }

  #sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(0, 0, 0, 0.45);
  }

  #layout:not(.sidebar-collapsed) #sidebar-backdrop {
    display: block;
  }

  #contenido {
    width: 100%;
    min-width: 0;
  }

  #topbar {
    flex-wrap: wrap;
    row-gap: 0.4rem;
    gap: 0.3rem;
    padding-left: 0.4rem;
    padding-right: 0.4rem;
  }

  #sidebar-menu-toggle {
    order: 0;
    flex-shrink: 0;
  }

  .topbar-context {
    order: 1;
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
  }

  .topbar-context #section-eyebrow,
  .topbar-context #section-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .topbar-actions {
    order: 2;
    flex: 1 1 100%;
    flex-wrap: nowrap;
    justify-content: space-between;
    gap: 0.3rem;
    min-width: 0;
  }

  #user-info {
    flex: 0 1 auto;
    min-width: 0;
    padding: 0.35rem 0.7rem;
    gap: 0.35rem;
  }

  #user-info span,
  #user-info strong {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 7rem;
  }

  #global-search-toggle,
  #help-toggle {
    flex-shrink: 0;
    width: 1.9rem;
    height: 1.9rem;
  }

  .theme-select-wrap {
    flex: 0 1 auto;
    min-width: 0;
    padding: 0.2rem 0.4rem;
    gap: 0.25rem;
  }

  .theme-select-label {
    display: none;
  }

  .theme-select {
    min-width: 0;
    width: auto;
    max-width: 11.5rem;
    font-size: 0.8rem;
    padding: 0.24rem 1.5rem 0.24rem 0.55rem;
  }
}

/* ===== Fase 1: Informes ===== */

@media (max-width: 768px) {
  #inf-advanced-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 600px) {
  .report-activity-head,
  .report-activity-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .report-activity-head .report-activity-col.user,
  .report-activity-row .report-activity-col.user {
    font-weight: 600;
  }
}

/* ===== Fase 2: Mis listas (botones de mover libros dentro del panel) ===== */

@media (max-width: 480px) {
  .lista-book-item {
    grid-template-columns: minmax(0, 1fr);
  }

  .lista-book-actions {
    flex-wrap: wrap;
    justify-content: flex-start;
    margin-top: 0.25rem;
  }
}

/* ===== Fase 3: Mi perfil y Detalle de libro (mismo layout .book-detail-layout) ===== */

@media (max-width: 768px) {
  #sec-perfil .book-top-grid,
  #sec-libro .book-top-grid {
    grid-template-columns: 1fr;
  }

  .book-detail-layout {
    grid-template-columns: 1fr;
  }

  .book-detail-cover-panel {
    justify-self: center;
    width: 100%;
    max-width: 220px;
  }

  .book-detail-cover-frame {
    min-height: 0;
  }

  .book-detail-image {
    max-width: 160px;
    max-height: 220px;
  }
}

@media (max-width: 480px) {
  .book-detail-info-row {
    grid-template-columns: minmax(5.5rem, 7rem) minmax(0, 1fr);
    gap: 0.4rem;
    padding: 0.4rem 0.45rem;
  }

  .book-detail-info-label {
    font-size: 0.76rem;
  }

  .book-detail-info-value {
    font-size: 0.84rem;
  }
}

/* ===== Fase 4: Configuracion ===== */

@media (max-width: 768px) {
  #admin-panel,
  .admin-bulk-import-card {
    padding: 0.6rem;
  }

  #admin-panel * {
    max-width: 100%;
  }

  #sec-configuracion .table-wrap table {
    min-width: 0;
    width: 100%;
    table-layout: fixed;
  }

  #sec-configuracion .table-wrap th,
  #sec-configuracion .table-wrap td {
    white-space: normal;
    word-break: break-word;
    padding: 0.35rem 0.3rem;
    font-size: 0.8rem;
  }

  #admin-users-tbody td:last-child {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
  }

  #admin-users-tbody .btn-accion {
    width: 100%;
    white-space: normal;
    padding: 0.3rem 0.4rem;
    font-size: 0.76rem;
  }
}

/* ===== Fase 5: Mi lectura (resumen y tablas top autores/sagas) ===== */

#sec-mi-lectura .inf-top-compact-table {
  width: 100%;
  min-width: 0;
  table-layout: fixed;
}

#sec-mi-lectura .inf-top-compact-table th,
#sec-mi-lectura .inf-top-compact-table td {
  white-space: normal;
  word-break: break-word;
}

#sec-mi-lectura .inf-top-compact-table th:nth-child(1),
#sec-mi-lectura .inf-top-compact-table td:nth-child(1) {
  width: 46%;
}

#sec-mi-lectura .inf-top-compact-table th:nth-child(n + 2),
#sec-mi-lectura .inf-top-compact-table td:nth-child(n + 2) {
  width: 18%;
}

@media (max-width: 768px) {
  .reading-overview-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .reading-summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
