/* =====================================================================
   Check Up HSJ · Ajustes para celular (se carga al final en todos los portales)
   Objetivo: nada se sale de la pantalla, botones del mismo tamaño y
   formularios cómodos de llenar con el dedo.
   ===================================================================== */

/* Tablets y celulares */
@media (max-width: 900px) {
  html, body { max-width: 100%; overflow-x: hidden; }
  img, video, canvas, svg { max-width: 100%; }
  .table-wrap, .table-scroll { -webkit-overflow-scrolling: touch; }
}

/* Celulares */
@media (max-width: 700px) {
  /* --- Formularios: 16 px evita que iPhone haga zoom al escribir --- */
  input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea { font-size: 16px !important; max-width: 100%; }
  input[type=date], input[type=time], input[type=datetime-local] { min-width: 0; width: 100%; -webkit-appearance: none; appearance: none; min-height: 48px; }
  .form-grid, .doctor-fields-grid, .official-fields { grid-template-columns: minmax(0, 1fr) !important; }
  .form-grid > *, .doctor-fields-grid > * { grid-column: 1 / -1 !important; min-width: 0; }
  label, .form-field, .doctor-field { min-width: 0; }
  input[type=file] { width: 100%; font-size: 14px !important; }

  /* --- Botones: área táctil de 44 px, texto en una línea cuando cabe --- */
  .button, button.button, a.button { min-height: 44px; justify-content: center; text-align: center; }
  .button .icon { flex: none; }

  /* --- Barra superior (admin): una sola fila limpia --- */
  .admin-body .topbar { gap: 8px; height: 62px; padding: 0 12px; }
  .admin-body .topbar > div:not(.topbar-meta) { flex: 1; min-width: 0; }
  .admin-body .topbar h1 { overflow: hidden; margin: 0; font-size: 16px !important; line-height: 1.2; text-overflow: ellipsis; white-space: nowrap; }
  .admin-body .topbar-meta { flex: none; gap: 6px; }
  .admin-body .topbar-session-actions { flex-wrap: nowrap !important; gap: 6px; }
  .admin-body .command-trigger { width: 40px; height: 40px; padding: 0; justify-content: center; }
  .admin-body .command-trigger span, .admin-body .command-trigger kbd { display: none; }
  .admin-body .topbar-user { min-height: 40px; padding: 3px; gap: 0; }
  .admin-body .topbar-user > div, .admin-body .topbar-user > .icon { display: none !important; }
  .admin-body .topbar-user .avatar { width: 34px; height: 34px; font-size: 12px; }
  .admin-body .topbar-logout { width: 40px; min-width: 40px; height: 40px; min-height: 40px !important; padding: 0 !important; }
  .admin-body .topbar-logout span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .admin-body .profile-menu { position: static; }
  .admin-body .profile-switcher { position: fixed !important; top: 66px !important; right: 12px !important; left: 12px !important; width: auto !important; max-width: none !important; z-index: 60; }
  .admin-body .mobile-menu { flex: none; width: 40px; height: 40px; }

  /* --- Contenido --- */
  .admin-body .page-content { padding: 18px 14px 40px; }
  .page-heading { gap: 12px; }
  .page-heading h2 { font-size: 24px; line-height: 1.2; overflow-wrap: anywhere; }
  .page-heading.compact > div, .page-heading > div:first-child { text-align: left; align-items: flex-start; }
  .title-with-badge { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; justify-content: flex-start; }
  .page-heading > .button, .page-heading > a.button, .page-heading > form, .page-heading > form .button { width: 100%; }
  .page-heading { align-items: stretch !important; }
  .page-heading > div:first-child { width: 100%; }
  .heading-actions { display: grid !important; grid-template-columns: minmax(0, 1fr) !important; gap: 10px; width: 100%; }
  .heading-actions > * { width: 100%; min-width: 0; margin: 0; grid-column: auto !important; }
  .heading-actions > form .button, .heading-actions > .button { width: 100%; }
  .heading-actions > .button-primary { order: -1; }
  .admin-body details[style] { margin-left: 0 !important; margin-right: 0 !important; width: 100%; }
  .panel-header { flex-wrap: wrap; gap: 10px; }

  /* --- Filtros: uno debajo de otro, a lo ancho --- */
  .filter-bar, body .panel .filter-bar, body .panel form.filter-bar { display: grid !important; grid-template-columns: minmax(0, 1fr) !important; gap: 10px; }
  .filter-bar > * { grid-column: 1 / -1 !important; }
  .filter-bar > *, .filter-bar .search-field, .filter-bar select, .filter-bar input, .filter-bar .button { width: 100% !important; min-width: 0 !important; }

  /* --- Tablas como tarjetas (las columnas toman su nombre del encabezado) --- */
  table.is-stacked { display: block; width: 100%; border: 0; }
  table.is-stacked thead { display: none; }
  table.is-stacked tbody, table.is-stacked tr, table.is-stacked td, table.is-stacked th[scope=row] { display: block; width: 100%; }
  table.is-stacked tr { margin: 0 0 12px; padding: 12px 14px; border: 1px solid #e3e9f2; border-radius: 14px; background: #fff; box-shadow: 0 2px 8px rgba(28, 45, 80, .04); }
  table.is-stacked td, table.is-stacked th[scope=row] { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 8px 0 !important; border: 0 !important; border-top: 1px solid #f0f3f8 !important; text-align: right; white-space: normal !important; overflow-wrap: anywhere; }
  table.is-stacked tr > :first-child { border-top: 0 !important; }
  table.is-stacked td[data-label]::before { content: attr(data-label); flex: 0 1 auto; max-width: 48%; overflow-wrap: normal; word-break: normal; hyphens: none; color: #7a869a; font-size: 11px; font-weight: 800; letter-spacing: .08em; text-align: left; text-transform: uppercase; }
  table.is-stacked td[colspan] { justify-content: center; text-align: center; }
  table.is-stacked td[colspan]::before { content: none; }
  table.is-stacked td > .button, table.is-stacked td > form, table.is-stacked td > a.button { margin-left: auto; }
  table.is-stacked td:has(.button) { flex-direction: column; align-items: stretch; text-align: left; }
  table.is-stacked td:has(.button) .button, table.is-stacked td:has(.button) form { width: 100%; margin: 0; }
  table.is-stacked td:has(.button) > * + * { margin-top: 6px; }
  .medical-workflow .medical-workflow-step { padding-left: 14px; padding-right: 14px; }
  .medical-workflow { padding-left: 12px; padding-right: 12px; }
  .table-wrap:has(table.is-stacked), .table-scroll:has(table.is-stacked) { overflow: visible; }

  /* --- Expediente: retirar paciente --- */
  .record-remove-panel { display: grid !important; grid-template-columns: 44px minmax(0, 1fr); align-items: start; }
  .record-remove-panel form { grid-column: 1 / -1; width: 100%; margin: 0 !important; }
  .record-remove-panel form .button { width: 100%; }

  /* --- Valoraciones (catálogo) --- */
  .catalog-card { display: grid !important; grid-template-columns: 44px minmax(0, 1fr); gap: 12px; align-items: start; }
  .catalog-card .catalog-main { min-width: 0; }
  .catalog-card .catalog-actions { grid-column: 1 / -1; display: flex; width: 100%; margin: 0; }
  .catalog-card .catalog-actions .button { width: 100%; }

  /* --- QR --- */
  .qr-card-head { align-items: flex-start; gap: 10px; }
  .qr-card-head > div { min-width: 0; flex: 1; }
  .qr-card-head .badge { flex: none; align-self: flex-start; height: auto; }
  .qr-card-actions { display: grid !important; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
  .qr-card-actions > * { width: 100%; min-width: 0; }
  .qr-card-actions .button { width: 100%; padding-left: 10px; padding-right: 10px; white-space: nowrap; }

  /* --- Directorio médico: tarjeta compacta y botón Administrar a lo ancho --- */
  .doctor-directory-card-summary { display: grid !important; grid-template-columns: 44px minmax(0, 1fr); gap: 10px 12px; align-items: start; }
  .doctor-directory-card-summary .doctor-card-identity { min-width: 0; }
  .doctor-directory-card-summary > * { grid-row: auto !important; }
  .doctor-directory-card-summary .doctor-card-account { grid-column: 1 / -1; justify-self: stretch; text-align: center; }
  .doctor-directory-card-summary .doctor-card-open { grid-column: 1 / -1 !important; display: flex !important; flex-direction: row !important; align-items: center; justify-content: center; gap: 8px; width: 100%; min-height: 44px; padding: 10px 14px !important; font-size: 14px !important; }
  .doctor-password-form .button, .doctor-status-form .button, .doctor-delete-form .button, .doctor-form-submit .button, .doctor-management-section > form > .button { width: 100%; }
  .doctor-status-form, .doctor-delete-form { display: grid !important; gap: 10px; }
  .doctor-create-actions { display: grid !important; gap: 12px; }
  .doctor-create-buttons { display: grid !important; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); width: 100%; }
  .doctor-create-buttons .button { width: 100%; }

  /* --- Cuentas administrativas --- */
  .admin-user-item { display: grid !important; grid-template-columns: 44px minmax(0, 1fr); gap: 10px; align-items: center; }
  .admin-user-item > :nth-child(n+3) { grid-column: 2; justify-self: start; }

  /* --- Acceso del paciente --- */
  .access-actions { display: grid !important; grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .access-actions > *, .access-actions .button { width: 100%; }

  /* --- Seguimiento: resumen --- */
  .summary-patient-list { grid-template-columns: minmax(0, 1fr); }
  .summary-patient > .button { justify-self: stretch; }
  .summary-notices time { margin-left: 0; width: 100%; }

  /* --- Desplegables tipo botón y ventanas --- */
  .admin-body details:not([class]) > summary, .admin-body details[style] > summary { width: 100%; justify-content: space-between; }
  .modal-card, .modal-dialog, .command-dialog { max-width: calc(100vw - 24px); }

  /* --- Portal médico --- */
  .doctor-editor-toolbar { flex-wrap: nowrap !important; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; padding-bottom: 4px; }
  .doctor-editor-toolbar { -webkit-mask-image: linear-gradient(90deg, #000 82%, transparent); mask-image: linear-gradient(90deg, #000 82%, transparent); }
  .doctor-editor-toolbar::-webkit-scrollbar { display: none; }
  .doctor-editor-toolbar > * { flex: none !important; }
  .doctor-body .button, .doctor-body button { min-height: 44px; }

  /* --- Mensajes flotantes: a lo ancho y sin tapar la barra --- */
  .toast-region { left: 12px !important; right: 12px !important; width: auto !important; }
  .toast { width: 100%; max-width: none; }
}

/* Celulares pequeños (iPhone SE, Android compactos) */
@media (max-width: 380px) {
  .qr-card-actions, .doctor-create-buttons { grid-template-columns: minmax(0, 1fr) !important; }
  .page-heading h2 { font-size: 22px; }
  .admin-body .topbar h1 { font-size: 15px !important; }
}
