/* ==========================================================================
   AGORA — Responsive Mobile First
   Base móvil → tablet (576/768) → laptop (992) → desktop (1200/1400)
   ========================================================================== */

/* ── Tokens fluidos ── */
:root {
  --space-xs: clamp(4px, 1vw, 6px);
  --space-sm: clamp(8px, 1.5vw, 12px);
  --space-md: clamp(12px, 2vw, 16px);
  --space-lg: clamp(16px, 2.5vw, 24px);
  --space-xl: clamp(20px, 3vw, 32px);
  --font-title: clamp(1.35rem, 1.1rem + 1.2vw, 1.75rem);
  --font-subtitle: clamp(0.875rem, 0.82rem + 0.25vw, 1rem);
  --font-body: clamp(0.875rem, 0.84rem + 0.15vw, 0.9375rem);
  --touch-min: 44px;
  --sidebar-width: min(280px, 88vw);
  --header-z: 100;
  --drawer-z: 200;
  --overlay-z: 190;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  overflow-x: clip;
  font-size: var(--font-body);
}

img,
video,
iframe {
  max-width: 100%;
  height: auto;
}

/* ── Tipografía fluida ── */
.page-title {
  font-size: var(--font-title);
  line-height: 1.25;
  word-break: break-word;
}

.page-subtitle,
.card-subtitle {
  font-size: var(--font-subtitle);
}

.kpi-value {
  font-size: clamp(1.5rem, 1.2rem + 1.5vw, 1.75rem);
}

/* ── Formularios táctiles ── */
.form-control,
.search-box input,
.stacked input,
.stacked select,
.stacked textarea,
#formCrearUsuario input {
  width: 100%;
  min-height: var(--touch-min);
  font-size: 16px; /* evita zoom en iOS */
}

.btn {
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px clamp(12px, 3vw, 16px);
}

.btn-sm {
  min-height: 36px;
}

/* ── App shell: mobile first (sin sidebar visible) ── */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

.app-shell .main-content {
  flex: 1;
  min-width: 0;
  width: 100%;
}

/* Sidebar drawer */
.app-shell .sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-width);
  height: 100%;
  height: 100dvh;
  z-index: var(--drawer-z);
  transform: translateX(-105%);
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  border-radius: 0;
  padding: var(--space-lg) var(--space-md);
  box-shadow: 4px 0 32px rgba(0, 0, 0, 0.35);
}

body.sidebar-open .app-shell .sidebar {
  transform: translateX(0);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--overlay-z);
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity 0.25s ease;
}

body.sidebar-open .sidebar-overlay {
  display: block;
  opacity: 1;
}

body.sidebar-open {
  overflow: hidden;
}

/* Botón menú hamburguesa */
.menu-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--touch-min);
  height: var(--touch-min);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.menu-toggle:hover {
  background: var(--surface-muted);
  border-color: var(--border-strong);
}

.menu-toggle svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
}

.menu-toggle[aria-expanded="true"] .icon-menu {
  display: none;
}

.menu-toggle[aria-expanded="true"] .icon-close {
  display: block;
}

.menu-toggle .icon-close {
  display: none;
}

/* Topbar mobile */
.topbar {
  height: auto;
  min-height: 56px;
  padding: var(--space-sm) var(--space-md);
  flex-wrap: wrap;
  gap: var(--space-sm);
  z-index: var(--header-z);
}

.topbar-left {
  flex: 1 1 auto;
  min-width: 0;
  gap: var(--space-sm);
}

.topbar-right {
  flex: 1 1 100%;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-sm);
}

.breadcrumbs {
  font-size: clamp(0.75rem, 0.7rem + 0.2vw, 0.8125rem);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.search-box {
  flex: 1 1 100%;
  min-width: 0;
  order: 10;
}

.search-box input {
  width: 100%;
  min-width: 0;
}

.topbar-right .btn-danger {
  flex: 1 1 auto;
}

/* Auditor: cerrar sesión táctil en móvil */
body.page-auditor .topbar-right .btn-danger {
  position: relative;
  z-index: 2;
  pointer-events: auto;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

body.page-auditor .app-shell .sidebar {
  display: flex;
  flex-direction: column;
}

body.page-auditor .app-shell .sidebar .nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

body.page-auditor .sidebar-footer {
  margin-top: auto;
  padding-top: var(--space-lg);
  border-top: 1px solid rgba(148, 163, 184, 0.22);
  flex-shrink: 0;
}

body.page-auditor .sidebar-footer .sidebar-logout {
  width: 100%;
}

.content {
  padding: var(--space-md);
}

/* KPIs: 1 columna en móvil */
.kpi-grid,
.kpi-grid.kpi-grid-3 {
  grid-template-columns: 1fr;
  gap: var(--space-sm);
}

.grid-2,
.grid-3 {
  grid-template-columns: 1fr;
  gap: var(--space-md);
}

.page-header {
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-md);
}

.actions-row {
  flex-direction: column;
  width: 100%;
}

.actions-row .btn {
  width: 100%;
}

.card {
  padding: var(--space-md);
  border-radius: var(--radius-md);
}

.card-header {
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-sm);
}

.pill-tabs {
  display: flex;
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  flex-wrap: nowrap;
  gap: 4px;
  padding: 4px;
}

.pill-tabs::-webkit-scrollbar {
  display: none;
}

.pill {
  flex-shrink: 0;
  white-space: nowrap;
  min-height: 40px;
  padding: 8px 12px;
  font-size: 0.8125rem;
}

.auth-card--solo {
  width: 100%;
  max-width: min(520px, 100%);
  margin-inline: auto;
}

/* Auth pages: estilos detallados en css/auth.css */

/* ── Modales mobile first ── */
.modal {
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  background: rgba(15, 23, 42, 0.65);
  backdrop-filter: blur(4px);
}

.modal[style*="display: block"],
.modal[style*="display:block"] {
  display: flex !important;
  align-items: flex-end;
}

.modal[style*="display: flex"],
.modal[style*="display:flex"] {
  align-items: flex-end;
}

.modal-content {
  width: 100% !important;
  max-width: 100% !important;
  margin: 0 !important;
  max-height: 92dvh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0 !important;
  padding: var(--space-lg) var(--space-md) calc(var(--space-lg) + env(safe-area-inset-bottom, 0px));
  -webkit-overflow-scrolling: touch;
}

.modal-content.wide-modal {
  width: 100% !important;
  max-width: 100% !important;
}

.modal-docs-header {
  flex-direction: column;
  gap: var(--space-sm);
}

.modal-docs-close {
  align-self: flex-end;
}

#cerrarModal,
.modal-content > span[style*="float"] {
  position: absolute !important;
  top: 14px !important;
  right: 14px !important;
  float: none !important;
  width: var(--touch-min);
  height: var(--touch-min);
  display: grid !important;
  place-items: center;
  z-index: 2;
}

/* ── Tablas → tarjetas en móvil ── */
.table-container.table-cards-mobile {
  overflow-x: visible;
  border: none;
  background: transparent;
}

.table-container.table-cards-mobile table {
  border-collapse: separate;
  border-spacing: 0;
}

.table-container.table-cards-mobile thead {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.table-container.table-cards-mobile tbody {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.table-container.table-cards-mobile tbody tr {
  display: flex;
  flex-direction: column;
  gap: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
  box-shadow: 0 4px 16px -8px rgba(15, 23, 42, 0.12);
}

.table-container.table-cards-mobile tbody tr:hover {
  background: var(--surface);
}

.table-container.table-cards-mobile tbody tr.table-empty-row {
  border-style: dashed;
  text-align: center;
  padding: var(--space-lg);
}

.table-container.table-cards-mobile tbody tr.table-empty-row td {
  display: block;
  border: none;
  padding: 0;
}

.table-container.table-cards-mobile tbody tr.table-empty-row td::before {
  display: none;
}

.table-container.table-cards-mobile td {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-md);
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.875rem;
  text-align: right;
  min-width: 0;
  width: 100% !important;
  white-space: normal !important;
  word-break: break-word;
}

.table-container.table-cards-mobile td:last-child {
  border-bottom: none;
}

.table-container.table-cards-mobile td::before {
  content: attr(data-label);
  font-weight: 700;
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  flex: 0 0 38%;
  max-width: 42%;
  text-align: left;
  padding-right: 8px;
}

.table-container.table-cards-mobile td.table-actions-cell,
.table-container.table-cards-mobile td:last-child:has(.table-actions),
.table-container.table-cards-mobile td:has(.btn) {
  flex-direction: column;
  align-items: stretch;
  text-align: left;
}

.table-container.table-cards-mobile td.table-actions-cell::before,
.table-container.table-cards-mobile td:has(.table-actions)::before {
  margin-bottom: 4px;
}

.table-container.table-cards-mobile .table-historial td.table-actions-cell,
.table-container.table-cards-mobile .table-pendientes td.table-actions-cell {
  min-width: 0 !important;
}

.table-container.table-cards-mobile .table-actions {
  width: 100%;
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-left: 0;
  gap: var(--space-sm);
}

.table-container.table-cards-mobile .table-actions .btn {
  flex: 1 1 calc(50% - 4px);
  min-width: 120px;
}

/* Tablas sin data-label: scroll horizontal como fallback */
.table-container:not(.table-cards-mobile) {
  -webkit-overflow-scrolling: touch;
}

/* ── Paginación ── */
.paginacion {
  gap: var(--space-sm);
  padding: var(--space-sm) 0;
}

.paginacion .btn-sm {
  min-width: 80px;
  flex: 1 1 auto;
  max-width: 140px;
}

/* ── Toasts ── */
.toast-stack,
.toast-stack-user,
#customAlert.alert {
  top: max(12px, env(safe-area-inset-top, 12px));
  right: var(--space-sm);
  left: var(--space-sm);
  width: auto;
}

/* ── Listas y timeline ── */
.list-item {
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-sm);
}

.timeline-entry {
  gap: var(--space-sm);
}

.cola-item {
  grid-template-columns: minmax(0, 1fr) !important;
}

/* ── Tablet: 576px+ ── */
@media (min-width: 576px) {
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .topbar-right {
    flex: 0 1 auto;
    flex-wrap: nowrap;
  }

  .search-box {
    flex: 0 1 auto;
    order: 0;
    min-width: 160px;
  }

  .search-box input {
    width: 140px;
  }

  .actions-row {
    flex-direction: row;
    width: auto;
  }

  .actions-row .btn {
    width: auto;
  }

  .modal {
    align-items: center;
    padding: var(--space-md);
  }

  .modal[style*="display: block"],
  .modal[style*="display:block"],
  .modal[style*="display: flex"],
  .modal[style*="display:flex"] {
    align-items: center !important;
  }

  .modal-content {
    border-radius: var(--radius-lg) !important;
    max-width: min(520px, 100%) !important;
    max-height: 88dvh;
  }

  .modal-content.wide-modal {
    max-width: min(960px, 100%) !important;
  }
}

/* ── Tablet: 768px+ ── */
@media (min-width: 768px) {
  .content {
    padding: clamp(18px, 2.5vw, 28px);
  }

  .kpi-grid.kpi-grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .kpi-grid:not(.kpi-grid-3) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card-header {
    flex-direction: row;
    align-items: center;
  }

  .page-header {
    flex-direction: row;
    align-items: flex-start;
  }

  .topbar {
    flex-wrap: nowrap;
    padding: 0 clamp(18px, 2.5vw, 28px);
    min-height: 64px;
  }

  .topbar-right {
    flex: 0 0 auto;
  }

  .search-box input {
    width: 180px;
  }

  /* Tablas normales desde tablet */
  .table-container.table-cards-mobile thead {
    position: static;
    width: auto;
    height: auto;
    margin: 0;
    clip: auto;
    overflow: visible;
  }

  .table-container.table-cards-mobile tbody {
    display: table-row-group;
  }

  .table-container.table-cards-mobile tbody tr {
    display: table-row;
    background: transparent;
    border: none;
    border-radius: 0;
    padding: 0;
    box-shadow: none;
  }

  .table-container.table-cards-mobile td {
    display: table-cell;
    text-align: left;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    width: auto !important;
  }

  .table-container.table-cards-mobile td::before {
    display: none;
  }

  .table-container.table-cards-mobile .table-actions {
    justify-content: flex-end;
    flex-wrap: nowrap;
  }

  .table-container.table-cards-mobile .table-actions .btn {
    flex: 0 0 auto;
    min-width: 0;
  }
}

/* ── Laptop: 992px+ — sidebar fijo, ocultar drawer ── */
@media (min-width: 992px) {
  .menu-toggle {
    display: none;
  }

  .sidebar-overlay {
    display: none !important;
  }

  body.sidebar-open {
    overflow: auto;
  }

  .app-shell {
    display: grid;
    grid-template-columns: 260px 1fr;
  }

  .app-shell .sidebar {
    position: sticky;
    top: 0;
    transform: none;
    width: auto;
    height: 100vh;
    height: 100dvh;
    box-shadow: var(--shadow-card);
    padding: 28px 22px;
  }

  body.page-auditor .sidebar-footer {
    display: none;
  }

  .topbar {
    min-height: 76px;
    height: 76px;
    padding: 0 28px;
  }

  .topbar-right {
    flex: 0 0 auto;
  }

  .kpi-grid:not(.kpi-grid-3) {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* ── Desktop: 1200px+ ── */
@media (min-width: 1200px) {
  .content {
    padding: clamp(20px, 2vw, 28px) clamp(20px, 2vw, 28px) clamp(28px, 3vw, 40px);
  }

  .search-box input {
    width: 200px;
  }
}

/* ── Wide: 1400px+ ── */
@media (min-width: 1400px) {
  .content {
    padding-inline: clamp(24px, 3vw, 40px);
  }
}

/* Super admin: cola + salud en 2 columnas desde laptop */
@media (min-width: 992px) {
  .grid-salud-row:not(.is-cola-vacia) {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}

/* Asignados: ver css/empresa-detalle.css */

/* Document preview modal (creado dinámicamente) */
.doc-preview-overlay {
  padding: var(--space-md) !important;
  align-items: flex-end !important;
}

.doc-preview-content {
  width: 100% !important;
  max-width: 100% !important;
  max-height: 92dvh !important;
  border-radius: 20px 20px 0 0 !important;
  overflow-y: auto !important;
}

@media (min-width: 576px) {
  .doc-preview-overlay {
    align-items: center !important;
  }

  .doc-preview-content {
    max-width: min(900px, 100%) !important;
    border-radius: 16px !important;
  }
}

/* Utilidad: texto que no debe desbordar */
.text-truncate-mobile {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* Safe areas iOS */
@supports (padding: max(0px)) {
  .topbar {
    padding-left: max(var(--space-md), env(safe-area-inset-left));
    padding-right: max(var(--space-md), env(safe-area-inset-right));
  }

  .auth-page {
    padding-bottom: max(var(--space-md), env(safe-area-inset-bottom));
  }
}
