/* =============================================
   GestionFlota — Mobile overrides (≤ 820px)
   Objetivo: UX nativa-like en móvil.
   - Tablas se transforman en cards apiladas (usa data-label)
   - Modal fullscreen
   - Header compacto con menú plegable
   - Sidebars se vuelven tabs sticky
   - Touch targets ≥ 44px, inputs 16px (iOS no zoom)
   - Safe-area para notch iOS
   ============================================= */

/* iOS: inputs a 16px para evitar auto-zoom */
@media (max-width: 820px) {
  input[type="text"],
  input[type="email"],
  input[type="tel"],
  input[type="number"],
  input[type="password"],
  input[type="date"],
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* Safe-area insets para notch iOS */
@supports (padding: max(0px)) {
  @media (max-width: 820px) {
    .header,
    .footer {
      padding-left: max(var(--space-lg), env(safe-area-inset-left));
      padding-right: max(var(--space-lg), env(safe-area-inset-right));
    }
  }
}

/* ============================================
   HEADER móvil: hamburguesa + nav desplegable
   ============================================ */
.nav-toggle {
  display: none;
  width: 40px;
  height: 40px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}
.nav-toggle:focus { outline: 2px solid var(--primary-ring); }

@media (max-width: 820px) {
  .header { position: sticky; top: 0; z-index: 100; background: var(--surface); border-bottom: 1px solid var(--border); }
  .header__inner {
    padding-top: var(--space-sm);
    padding-bottom: var(--space-sm);
    align-items: center;
  }
  .header__brand { gap: var(--space-xs); }
  .header__logo { width: 32px; height: 32px; font-size: 0.9rem; }
  .header__title { font-size: 0.95rem; }
  .header__subtitle { font-size: 0.68rem; }

  .nav-toggle { display: inline-flex; }

  .header__nav {
    display: none;
    position: absolute;
    top: 100%; left: 0; right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    padding: var(--space-sm);
    z-index: 99;
  }
  .header__nav--open { display: flex; }
  .header__nav .header__link {
    padding: 12px var(--space-sm);
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    border-left: none !important;
    margin: 0 !important;
  }
  .header__nav .header__link:hover,
  .header__nav .header__link--active {
    background: var(--primary-light);
    color: var(--primary);
  }
  .header__nav .btn { width: 100%; margin-top: var(--space-xs); min-height: 44px; }
  .header__operator {
    width: 100%; max-width: none;
    min-height: 44px; padding: 10px 12px;
    margin-top: var(--space-xs);
    font-size: 14px;
  }
}

/* ============================================
   BUTTONS / TOUCH TARGETS
   ============================================ */
@media (max-width: 820px) {
  .btn {
    min-height: 44px;
    padding: 10px 18px;
    font-size: 0.92rem;
  }
  .btn--sm { min-height: 38px; padding: 8px 14px; font-size: 0.85rem; }
  .btn--xs { min-height: 30px; padding: 4px 10px; font-size: 0.78rem; }
  .btn--full { min-height: 48px; }
  .filter-btn {
    min-height: 36px;
    padding: 6px 14px;
    font-size: 0.85rem;
    white-space: nowrap;
  }
  a.header__link { min-height: 44px; display: inline-flex; align-items: center; }
}

/* ============================================
   SIDEBARS: portal y config → tabs sticky top
   ============================================ */
@media (max-width: 820px) {
  .cfg-layout,
  .portal-layout {
    display: block;
    padding: 0;
  }
  .cfg-sidebar,
  .portal-sidebar {
    position: sticky;
    top: 56px; /* debajo del header sticky */
    z-index: 50;
    width: 100%;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 4px;
    padding: var(--space-xs) var(--space-md);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    border-right: none;
    box-shadow: 0 1px 0 rgba(0,0,0,0.02);
    scrollbar-width: none;
  }
  .cfg-sidebar::-webkit-scrollbar,
  .portal-sidebar::-webkit-scrollbar { display: none; }
  .cfg-nav-btn,
  .portal-nav-btn {
    flex-shrink: 0;
    min-height: 40px;
    padding: 8px 14px;
    font-size: 0.85rem;
    border-radius: 999px;
    background: var(--bg);
    border: 1px solid var(--border);
    white-space: nowrap;
  }
  .cfg-nav-btn--active,
  .portal-nav-btn--active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }
  .cfg-content,
  .portal-content { padding: var(--space-md); }
}

/* ============================================
   FILTROS: chips con scroll horizontal suave
   ============================================ */
@media (max-width: 820px) {
  .filters,
  .filters-row,
  #filtros-empresa,
  #filtros-asignado,
  #filtros-gestion {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 6px;
    margin-right: calc(var(--space-md) * -1);
    padding-right: var(--space-md);
  }
  .filters::-webkit-scrollbar,
  .filters-row::-webkit-scrollbar { display: none; }
  .filters-row__label {
    flex-shrink: 0;
    position: sticky; left: 0;
    background: var(--bg);
    padding-right: var(--space-xs);
    z-index: 1;
  }
}

/* ============================================
   TABLAS → CARDS APILADAS (usa data-label en <td>)
   ============================================ */
@media (max-width: 820px) {
  .table-wrapper {
    border: none;
    background: transparent;
    overflow: visible;
  }
  .table-wrapper table {
    display: block;
    background: transparent;
  }
  .table-wrapper thead {
    display: none; /* encabezados ocultos — cada celda se rotula con data-label */
  }
  .table-wrapper tbody { display: block; }
  .table-wrapper tbody tr {
    display: block;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-md);
    margin-bottom: var(--space-sm);
    box-shadow: var(--shadow-sm);
  }
  .table-wrapper tbody tr.tr--selected {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-ring);
  }
  .table-wrapper tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-sm);
    padding: 6px 0;
    border-bottom: 1px dashed var(--border);
    font-size: 0.85rem;
  }
  .table-wrapper tbody td:last-child { border-bottom: none; }
  .table-wrapper tbody td::before {
    content: attr(data-label);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    flex-shrink: 0;
  }
  .table-wrapper tbody td:empty,
  .table-wrapper tbody td[data-label=""] { display: none; }

  /* Patente destacada como header de la card */
  .table-wrapper tbody td.td-patente {
    display: block;
    border-bottom: 1px solid var(--border);
    padding-bottom: var(--space-sm);
    margin-bottom: var(--space-xs);
    font-size: 1rem;
    font-weight: 700;
    color: var(--color-ink);
  }
  .table-wrapper tbody td.td-patente::before {
    display: block;
    margin-bottom: 2px;
  }
  /* Checkbox pegado a la patente */
  .table-wrapper tbody td.td-check {
    position: absolute;
    right: var(--space-md);
    top: var(--space-md);
    padding: 0;
    border: none;
    justify-content: flex-end;
  }
  .table-wrapper tbody td.td-check::before { display: none; }
  .table-wrapper tbody tr { position: relative; padding-right: 42px; }

  /* Badges de estado y gestión alineadas a la derecha */
  .table-wrapper tbody td .badge {
    font-size: 0.78rem;
    padding: 3px 10px;
  }
  .td-asignado {
    align-items: flex-end;
  }
  .td-dias { text-align: right; }
}

/* ============================================
   MODAL FULLSCREEN en móvil
   ============================================ */
@media (max-width: 820px) {
  .modal-overlay {
    align-items: flex-start;
    padding: 0;
    backdrop-filter: none;
    background: rgba(17, 24, 39, 0.6);
  }
  .modal,
  .modal--gestion,
  .modal--tramite,
  .modal--masivo {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    padding: var(--space-lg) var(--space-md);
    padding-top: calc(var(--space-lg) + env(safe-area-inset-top, 0));
    padding-bottom: calc(var(--space-lg) + env(safe-area-inset-bottom, 0));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .modal h3 {
    font-size: 1.05rem;
    padding-right: 32px;
    position: relative;
  }
  .modal__actions {
    position: sticky;
    bottom: 0;
    background: var(--surface);
    padding: var(--space-sm) 0;
    margin-top: var(--space-md);
    border-top: 1px solid var(--border);
    flex-wrap: wrap;
    gap: var(--space-xs);
  }
  .modal__actions .btn { flex: 1; min-width: 120px; }
  .modal__record-info {
    grid-template-columns: auto 1fr;
    font-size: 0.85rem;
    gap: 4px var(--space-sm);
  }

  /* Pipeline de estados: más compacto en móvil */
  .pipeline {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .pipeline::-webkit-scrollbar { display: none; }
  .pipeline__step-label { font-size: 0.7rem; }
}

/* ============================================
   KPI cards: más compactas pero legibles
   ============================================ */
@media (max-width: 820px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: var(--space-xs); }
  .kpi-card { padding: var(--space-sm) var(--space-md); }
  .kpi-card__label { font-size: 0.68rem; }
  .kpi-card__value { font-size: 1.45rem; margin: 2px 0; }
  .kpi-card__detail { font-size: 0.68rem; line-height: 1.3; }
}

@media (max-width: 380px) {
  .kpi-grid { grid-template-columns: 1fr; }
}

/* ============================================
   BENTO LANDING: más compacto
   ============================================ */
@media (max-width: 820px) {
  .landing-hero { padding: var(--space-2xl) 0 var(--space-xl); }
  .landing-hero__title { font-size: clamp(1.5rem, 7vw, 2rem); }
  .landing-hero__desc { font-size: 0.95rem; line-height: 1.55; }
  .landing-hero__cta {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-sm);
  }
  .landing-hero__cta .btn { width: 100%; min-height: 48px; }
  .landing-hero__stats {
    justify-content: space-around;
    flex-wrap: wrap;
    gap: var(--space-md);
  }

  .service-group { margin-top: var(--space-xl); }
  .service-group__title { font-size: 1.15rem; }
  .bento-grid--permanente,
  .bento-grid--eventual {
    grid-template-columns: 1fr;
    gap: var(--space-sm);
  }
  .bento-card,
  .bento-card--lg { padding: var(--space-lg); }
  .bento-card__body h4 { font-size: 1rem; }
  .bento-card__body p { font-size: 0.88rem; }
  .bento-card__features li { font-size: 0.82rem; }
  .bento-card__icon { width: 40px; height: 40px; }
  .bento-card__icon i[data-lucide] { width: 20px; height: 20px; }

  /* Steps: flecha oculta, cards en columna */
  .steps-grid {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
  }
  .step-card__arrow { display: none; }
  .step-card {
    display: flex;
    align-items: flex-start;
    gap: var(--space-md);
    text-align: left;
  }
  .step-card__number { flex-shrink: 0; margin-bottom: 0; }

  /* Sección contacto — más compacta */
  .landing-section { padding: var(--space-xl) 0; }
}

/* ============================================
   PANEL / PORTAL: barras de acciones y buscadores
   ============================================ */
@media (max-width: 820px) {
  /* Barra de acciones masivas: sticky abajo como bottom sheet */
  .bulk-bar {
    position: sticky;
    bottom: 0;
    z-index: 40;
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-xs);
    padding: var(--space-sm) var(--space-md);
    margin: var(--space-md) calc(var(--space-md) * -1) 0;
    border-radius: 0;
    border-top: 1px solid var(--border);
    background: var(--surface);
    box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.08);
  }
  .bulk-bar__info { font-size: 0.85rem; }
  .bulk-bar__actions { display: flex; gap: var(--space-xs); flex-wrap: wrap; }
  .bulk-bar__actions .btn { flex: 1; min-width: 120px; }

  /* Search box */
  .search-box input {
    font-size: 16px !important;
    min-height: 44px;
  }
  .search-box__icon { top: 12px; }

  /* Banner demo: apilar en móvil */
  #banner-demo {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: var(--space-sm);
  }
  #banner-demo > span { font-size: 0.82rem; }
  #banner-demo button { width: 100%; margin-left: 0 !important; }

  /* Sección header (h2 + botón) */
  .section__header,
  .portal-section__header,
  .cfg-section__header {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-xs);
  }
  .section__header h2,
  .portal-section__header h2,
  .cfg-section__header h2 { font-size: 1.1rem; }

  /* Lotes de trabajo: 1 col */
  .lotes__grid { grid-template-columns: 1fr !important; }
  .lote-card { padding: var(--space-md); }

  /* Panel de gestión por lote: cerrado por defecto y más apretado */
  .lote-panel__toggle { padding: 10px var(--space-md); font-size: 0.9rem; }
  .lote-section { flex-direction: column; align-items: stretch; gap: var(--space-xs); }
  .lote-section__btns { gap: 4px; }
}

/* ============================================
   CFG pages: cards de formulario más aireadas
   ============================================ */
@media (max-width: 820px) {
  .cfg-card { padding: var(--space-md); }
  .cfg-card__title { font-size: 0.95rem; margin-bottom: var(--space-sm); }
  .form-group label { font-size: 0.82rem; margin-bottom: 4px; }
  .form-group input,
  .form-group textarea,
  .form-group select {
    min-height: 44px;
    padding: 10px 12px;
  }
  textarea { min-height: 80px; }
}

/* ============================================
   Ocultar scrollbars horizontales en chips/filters
   manteniendo scroll funcional
   ============================================ */
.filters,
.filters-row,
.pipeline {
  -webkit-overflow-scrolling: touch;
}

/* ============================================
   Reveal animation: más rápida en móvil
   ============================================ */
@media (max-width: 820px) {
  .reveal { transition-duration: 280ms; }
}

/* ============================================
   Pin dialog / login dialog full-width en móvil
   ============================================ */
@media (max-width: 820px) {
  .pin-dialog,
  .login-dialog {
    width: calc(100% - var(--space-lg) * 2);
    max-width: 400px;
    padding: var(--space-xl);
  }
  .pin-dialog input,
  .login-dialog input {
    font-size: 16px;
    min-height: 44px;
  }
}

/* ============================================
   Utilidades móviles genéricas
   ============================================ */
.mobile-only { display: none; }
.desktop-only { display: block; }

@media (max-width: 820px) {
  .mobile-only { display: block; }
  .desktop-only { display: none; }
}

/* ============================================
   Portal cliente: Mi Flota en movil
   ============================================ */
@media (max-width: 820px) {
  .flota-kpis { grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
  .flota-kpi { padding: var(--space-sm); }
  .flota-kpi__value { font-size: 1.15rem; }
  .flota-kpi__label { font-size: 0.62rem; }

  .vehiculo-card { margin-bottom: var(--space-sm); }
  .vehiculo-card__head {
    padding: var(--space-md);
    gap: var(--space-sm);
  }
  .vehiculo-card__icon { width: 38px; height: 38px; }
  .vehiculo-card__icon svg,
  .vehiculo-card__icon i[data-lucide] { width: 20px; height: 20px; }
  .vehiculo-card__patente { font-size: 0.95rem; }
  .vehiculo-card__tipo { font-size: 0.72rem; }
  .vehiculo-card__estado { font-size: 0.7rem; padding: 3px 10px; }

  .vehiculo-tramites__item {
    padding: 10px var(--space-md);
    grid-template-columns: 18px 1fr auto;
    grid-template-rows: auto auto;
    gap: 4px var(--space-sm);
  }
  .vehiculo-tramites__icon { grid-row: 1 / 3; }
  .vehiculo-tramites__label { grid-column: 2; grid-row: 1; font-size: 0.88rem; }
  .vehiculo-tramites__fecha { grid-column: 2; grid-row: 2; font-size: 0.76rem; }
  .vehiculo-tramites__chip { grid-column: 3; grid-row: 1 / 3; align-self: center; }

  .vehiculo-docs {
    flex-direction: column;
    align-items: stretch;
    padding: var(--space-sm) var(--space-md);
    gap: var(--space-xs);
  }
  .vehiculo-docs__list { width: 100%; }
  .vehiculo-docs__zip { width: 100%; justify-content: center; }

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

/* ============================================
   Vista Prospectos en mobile
   ============================================ */
@media (max-width: 820px) {
  .vista-prospectos__grid { grid-template-columns: 1fr; }
  .vista-prospectos__header { flex-direction: column; align-items: stretch; }
  .prospecto-card__acciones .btn { flex: 1 1 100%; }
}

/* ============================================
   Vista "Por operador" en mobile
   ============================================ */
@media (max-width: 820px) {
  .vista-por-operador__grid {
    grid-template-columns: 1fr;
  }
  .vista-por-operador__header {
    flex-direction: column;
    align-items: stretch;
  }
  .operador-card__header {
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  .tramite-mini-row {
    flex-wrap: wrap;
    padding: 0.6rem 0.7rem;
  }
  .tramite-mini-row__main { width: 100%; }
  .tramite-mini-row__meta { width: 100%; text-align: right; }
}

/* ====================================================
   D1 · Mobile-first improvements (Polish 2)
   ==================================================== */

@media (max-width: 768px) {
  /* Touch targets ≥44px (WCAG · accesibilidad) */
  .btn,
  .filter-btn,
  .portal-nav-btn,
  .clientes-tab,
  .modal__close,
  .login-dialog__close,
  .toggle-trigger {
    min-height: 44px;
  }

  /* Inputs: mejor tap target + sin zoom en iOS */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  input[type="tel"],
  select,
  textarea {
    min-height: 44px;
    font-size: 16px;
  }

  /* Sidebar del portal pasa a bottom nav fijo (mejor patrón mobile) */
  .portal-layout__sidebar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    height: auto;
    flex-direction: row;
    justify-content: space-around;
    padding: var(--space-xs) 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    border-right: none;
    box-shadow: 0 -2px 12px rgba(17, 24, 39, 0.06);
    z-index: 100;
  }
  .portal-nav-btn {
    flex: 1;
    text-align: center;
    border-radius: 0;
    padding: 0.5rem 0.4rem !important;
    font-size: 0.72rem;
    min-width: 0;
  }
  .portal-nav-btn--active {
    background: transparent;
    color: var(--primary);
    border-bottom: 2px solid var(--primary);
  }
  .portal-layout__main {
    padding-bottom: 90px; /* espacio para el bottom nav */
  }

  /* Banner sticky en mobile */
  .alertas-home-banner {
    position: sticky;
    top: 0;
    z-index: 50;
    margin-left: calc(-1 * var(--space-md));
    margin-right: calc(-1 * var(--space-md));
    border-radius: 0;
  }

  /* Cards con más aire en mobile */
  .vehiculo-card { padding: var(--space-md); }

  /* Header simplificado en mobile */
  .header__subtitle { display: none; }
  .header__search-label { display: none; }

  /* Achievement grid más compacto */
  .achievements-grid {
    grid-template-columns: 1fr;
  }

  /* Demo data banner full-width en mobile */
  .demo-data-banner {
    margin-left: calc(-1 * var(--space-md));
    margin-right: calc(-1 * var(--space-md));
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }
}

/* Pull-to-refresh indicator (D1) */
.pull-to-refresh-indicator {
  position: fixed;
  top: -50px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary);
  color: #fff;
  padding: 8px 18px;
  border-radius: 0 0 14px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  transition: top 0.25s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 1000;
  pointer-events: none;
}
.pull-to-refresh-indicator--visible { top: 0; }

/* Solicitudes · paso ④Aceptar tiene 3 botones · en mobile apilar y ancho completo */
@media (max-width: 640px) {
  .sol-card__action-row {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }
  .sol-card__action-row .btn {
    width: 100%;
  }
}
