/* V1.00.4 - Seguimiento RQ: modal historial y vistas móviles sin tablas anchas */
@media (max-width: 640px) {
  /* =========================================================
     MODAL HISTORIAL: por encima de cualquier detalle de orden
     ========================================================= */
  app-traking .modal {
    position: fixed !important;
    inset: 0 !important;
    z-index: 9000 !important;
    display: flex !important;
    align-items: flex-end !important;
    justify-content: center !important;
    width: 100vw !important;
    height: 100dvh !important;
    margin: 0 !important;
    padding: 8px 8px max(8px, env(safe-area-inset-bottom)) !important;
    overflow: hidden !important;
    background: rgba(2, 6, 23, 0.72) !important;
    backdrop-filter: blur(3px);
    box-sizing: border-box !important;
  }

  app-traking .modal .modal-content {
    position: relative !important;
    width: 100% !important;
    max-width: 430px !important;
    height: min(94dvh, 860px) !important;
    max-height: 94dvh !important;
    margin: 0 !important;
    padding: 0 10px calc(14px + env(safe-area-inset-bottom)) !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    border: 1px solid rgba(226, 232, 240, 0.9) !important;
    border-radius: 20px !important;
    background: #f8fafc !important;
    box-shadow: 0 24px 70px rgba(2, 6, 23, 0.34) !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
    box-sizing: border-box !important;
  }

  /* Cabecera siempre visible, incluido el botón cerrar */
  app-traking .modal .modal-header {
    position: sticky !important;
    top: 0 !important;
    z-index: 9010 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    min-height: 58px !important;
    margin: 0 -10px 8px !important;
    padding: 9px 10px !important;
    border-bottom: 1px solid #e2e8f0 !important;
    background: rgba(248, 250, 252, 0.98) !important;
    backdrop-filter: blur(10px) !important;
  }

  app-traking .modal .modal-header h3 {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    margin: 0 !important;
    padding: 11px 12px !important;
    border-left: 4px solid #2563eb !important;
    border-radius: 9px !important;
    background: #eef4ff !important;
    color: #0f2b55 !important;
    font-size: 1rem !important;
    line-height: 1.15 !important;
    text-align: left !important;
  }

  app-traking .modal .close-btn {
    position: static !important;
    flex: 0 0 40px !important;
    display: grid !important;
    place-items: center !important;
    width: 40px !important;
    min-width: 40px !important;
    height: 40px !important;
    min-height: 40px !important;
    margin: 0 0 0 8px !important;
    padding: 0 !important;
    border: 1px solid #cbd5e1 !important;
    border-radius: 12px !important;
    background: #ffffff !important;
    color: #334155 !important;
    font-size: 1rem !important;
    line-height: 1 !important;
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08) !important;
  }

  /* =========================================================
     TIMELINE: vertical, compacta y sin espacio muerto
     ========================================================= */
  app-traking .modal .tracking-wrapper {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 0 10px !important;
    padding: 5px 0 2px !important;
    overflow: visible !important;
  }

  app-traking .modal .tracking-container,
  app-traking .modal .tracking-container.unified {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0 !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 2px 0 4px !important;
    overflow: visible !important;
    border: 0 !important;
  }

  app-traking .modal .step-wrapper,
  app-traking .modal .step-wrapper.has-secondary {
    position: relative !important;
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    padding: 0 0 9px 0 !important;
  }

  app-traking .modal .step-wrapper > .tracking-step {
    position: relative !important;
    display: grid !important;
    grid-template-columns: 34px minmax(0, 1fr) auto !important;
    align-items: start !important;
    gap: 8px !important;
    width: 100% !important;
    min-width: 0 !important;
    min-height: 42px !important;
    margin: 0 !important;
    padding: 6px 7px !important;
    border: 1px solid #e2e8f0 !important;
    border-radius: 12px !important;
    background: #ffffff !important;
    text-align: left !important;
    box-sizing: border-box !important;
  }

  app-traking .modal .tracking-step .circle {
    position: relative !important;
    width: 32px !important;
    min-width: 32px !important;
    height: 32px !important;
    min-height: 32px !important;
    margin: 0 !important;
    font-size: 0.85rem !important;
    box-shadow: none !important;
  }

  app-traking .modal .tracking-step .label {
    min-width: 0 !important;
    margin: 1px 0 0 !important;
    color: #0f172a !important;
    font-size: 0.7rem !important;
    font-weight: 800 !important;
    line-height: 1.25 !important;
    text-align: left !important;
    overflow-wrap: anywhere !important;
  }

  app-traking .modal .tracking-step .step-date {
    margin-top: 2px !important;
    color: #64748b !important;
    font-size: 0.58rem !important;
    font-weight: 500 !important;
    line-height: 1.2 !important;
  }

  app-traking .modal .tracking-step .detail {
    align-self: center !important;
    min-width: 44px !important;
    max-width: 74px !important;
    margin: 0 !important;
    padding: 4px 5px !important;
    border-radius: 8px !important;
    font-size: 0.59rem !important;
    line-height: 1.2 !important;
    text-align: center !important;
  }

  app-traking .modal .step-wrapper > .line {
    position: static !important;
    display: block !important;
    width: 2px !important;
    height: 8px !important;
    margin: 0 0 0 22px !important;
    background: #cbd5e1 !important;
  }

  /* Ramas de bodegas: debajo del paso principal, sin posicionamiento absoluto */
  app-traking .modal .secondary-branch {
    position: static !important;
    top: auto !important;
    left: auto !important;
    width: auto !important;
    min-width: 0 !important;
    margin: 7px 0 0 20px !important;
    padding: 0 0 0 10px !important;
    transform: none !important;
    border-left: 2px solid #dbeafe !important;
  }

  app-traking .modal .secondary-branch > .branch-line {
    display: none !important;
  }

  app-traking .modal .secondary-flow {
    display: flex !important;
    flex-direction: column !important;
    gap: 6px !important;
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 0 7px !important;
    padding: 0 !important;
  }

  app-traking .modal .secondary-flow .tracking-step {
    display: grid !important;
    grid-template-columns: 28px minmax(0, 1fr) auto !important;
    align-items: start !important;
    gap: 7px !important;
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    padding: 6px !important;
    border: 1px solid #e2e8f0 !important;
    border-radius: 10px !important;
    background: #f8fafc !important;
  }

  app-traking .modal .secondary-flow .circle {
    width: 27px !important;
    min-width: 27px !important;
    height: 27px !important;
    min-height: 27px !important;
    font-size: 0.72rem !important;
  }

  app-traking .modal .secondary-flow .line {
    display: none !important;
  }

  app-traking .modal .branch-bodega {
    display: block !important;
    margin-top: 2px !important;
    color: #2563eb !important;
    font-size: 0.56rem !important;
    font-weight: 700 !important;
  }

  app-traking .modal .tooltip {
    position: fixed !important;
    top: auto !important;
    left: 12px !important;
    right: 12px !important;
    bottom: calc(14px + env(safe-area-inset-bottom)) !important;
    width: auto !important;
    max-width: none !important;
    transform: none !important;
    white-space: normal !important;
    text-align: center !important;
    z-index: 9050 !important;
  }

  /* El historial comienza inmediatamente después del flujo */
  app-traking .modal .modal-body {
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  app-traking .modal .section {
    width: 100% !important;
    margin: 0 0 10px !important;
    padding: 9px 0 0 !important;
    border-top: 1px solid #e2e8f0 !important;
  }

  app-traking .modal .section h4 {
    margin: 0 0 8px !important;
    padding: 0 0 0 9px !important;
    border-left: 3px solid #3b82f6 !important;
    color: #0f172a !important;
    font-size: 0.78rem !important;
    line-height: 1.2 !important;
  }

  app-traking .modal .grid-cards,
  app-traking .modal .card-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 7px !important;
    width: 100% !important;
    min-width: 0 !important;
  }

  app-traking .modal .grid-cards > .card,
  app-traking .modal .grid-cards > div > .card {
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 0 8px !important;
    padding: 9px !important;
    border: 1px solid #e2e8f0 !important;
    border-radius: 12px !important;
    background: #ffffff !important;
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.05) !important;
    box-sizing: border-box !important;
  }

  app-traking .modal .card-grid label,
  app-traking .modal .card-footer label {
    color: #94a3b8 !important;
    font-size: 0.52rem !important;
    font-weight: 800 !important;
    text-transform: uppercase !important;
  }

  app-traking .modal .card-grid span,
  app-traking .modal .card-footer span {
    color: #0f172a !important;
    font-size: 0.64rem !important;
    line-height: 1.25 !important;
    overflow-wrap: anywhere !important;
  }

  /* =========================================================
     VISTAS PENDIENTES / ENVÍOS: tarjetas, no tablas con scroll
     ========================================================= */
  app-traking .tabla-toggle > .tabla-resultados {
    width: 100% !important;
    max-width: 100% !important;
    overflow: visible !important;
    border: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    border: 0 !important;
    background: transparent !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table > thead {
    display: none !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table > tbody {
    display: block !important;
    width: 100% !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table > tbody > tr {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 6px !important;
    width: 100% !important;
    min-width: 0 !important;
    margin: 0 0 9px !important;
    padding: 9px !important;
    border: 1px solid #e2e8f0 !important;
    border-radius: 13px !important;
    background: #ffffff !important;
    box-shadow: 0 3px 10px rgba(15, 23, 42, 0.05) !important;
    font-size: 0 !important; /* oculta texto suelto fuera de td */
    box-sizing: border-box !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table > tbody > tr > td {
    display: flex !important;
    min-width: 0 !important;
    flex-direction: column !important;
    gap: 2px !important;
    margin: 0 !important;
    padding: 5px 6px !important;
    border: 0 !important;
    border-radius: 8px !important;
    background: #f8fafc !important;
    color: #0f172a !important;
    font-size: 0.65rem !important;
    line-height: 1.25 !important;
    overflow-wrap: anywhere !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table > tbody > tr > td::before {
    display: block !important;
    margin-bottom: 1px !important;
    color: #94a3b8 !important;
    font-size: 0.49rem !important;
    font-weight: 800 !important;
    letter-spacing: 0.035em !important;
    text-transform: uppercase !important;
  }

  /* Pendientes compras: el body tiene 13 celdas actualmente */
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) tbody tr > td:nth-child(1),
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) tbody tr > td:nth-child(5),
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) tbody tr > td:nth-child(8),
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) tbody tr > td:nth-child(10),
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) tbody tr > td:nth-child(13) {
    grid-column: 1 / -1 !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(1)::before { content: "RQ / solicitante / autorizado" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(2)::before { content: "Item" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(3)::before { content: "Orden" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(4)::before { content: "Imperium" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(5)::before { content: "Descripción" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(6)::before { content: "Unidad" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(7)::before { content: "Cantidad" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(8)::before { content: "Observaciones" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(9)::before { content: "Pendiente" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(10)::before { content: "Fechas" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(11)::before { content: "Retraso" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(12)::before { content: "Estado" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(13)::before { content: "Compra / llegada" !important; }

  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(13)):not(:has(tbody tr > td:nth-child(14))) td:nth-child(11):empty {
    display: none !important;
  }

  /* Requerimientos enviados: 15 celdas */
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) tbody tr > td:nth-child(1),
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) tbody tr > td:nth-child(14),
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) tbody tr > td:nth-child(15) {
    grid-column: 1 / -1 !important;
  }

  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(1)::before { content: "Descripción" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(2)::before { content: "Proyecto" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(3)::before { content: "Imperium / orden" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(4)::before { content: "Bodega origen" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(5)::before { content: "Bodega destino" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(6)::before { content: "Unidad" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(7)::before { content: "Cantidad enviada" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(8)::before { content: "Bulto" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(9)::before { content: "Fecha envío" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(10)::before { content: "Nro. orden" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(11)::before { content: "Sector" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(12)::before { content: "Item" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(13)::before { content: "Estado envío" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(14)::before { content: "Novedades" !important; }
  app-traking .tabla-toggle > .tabla-resultados > table:has(tbody tr > td:nth-child(15)) td:nth-child(15)::before { content: "Usuarios" !important; }

  /* =========================================================
     VISTA TODO / FULFILLMENT: tarjetas móviles
     ========================================================= */
  app-traking .nota-movimientos + .tabla-resultados {
    overflow: visible !important;
    border: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    border: 0 !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table > thead {
    display: none !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table > tbody {
    display: block !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table > tbody > tr {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 6px !important;
    width: 100% !important;
    margin: 0 0 9px !important;
    padding: 9px !important;
    border: 1px solid #e2e8f0 !important;
    border-radius: 13px !important;
    background: #ffffff !important;
    box-shadow: 0 3px 10px rgba(15, 23, 42, 0.05) !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table > tbody > tr > td {
    display: flex !important;
    min-width: 0 !important;
    flex-direction: column !important;
    gap: 2px !important;
    padding: 5px !important;
    border: 0 !important;
    border-radius: 8px !important;
    background: #f8fafc !important;
    color: #0f172a !important;
    font-size: 0.62rem !important;
    line-height: 1.22 !important;
    overflow-wrap: anywhere !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table > tbody > tr > td::before {
    color: #94a3b8 !important;
    font-size: 0.47rem !important;
    font-weight: 800 !important;
    text-transform: uppercase !important;
  }

  app-traking .nota-movimientos + .tabla-resultados > table > tbody > tr > td:nth-child(1),
  app-traking .nota-movimientos + .tabla-resultados > table > tbody > tr > td:nth-child(3),
  app-traking .nota-movimientos + .tabla-resultados > table > tbody > tr > td:nth-child(10) {
    grid-column: 1 / -1 !important;
  }

  app-traking .nota-movimientos + .tabla-resultados td:nth-child(1)::before { content: "Requerimiento" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(2)::before { content: "Tipo" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(3)::before { content: "Usuarios" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(4)::before { content: "Solicitado" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(5)::before { content: "A gestionar" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(6)::before { content: "Cancelado" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(7)::before { content: "Enviado" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(8)::before { content: "Pend. compra" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(9)::before { content: "Comprado" !important; }
  app-traking .nota-movimientos + .tabla-resultados td:nth-child(10)::before { content: "Registro / seguimiento" !important; }

  app-traking .nota-movimientos {
    margin: 6px 0 9px !important;
    padding: 8px 9px !important;
    border-radius: 10px !important;
    font-size: 0.61rem !important;
    line-height: 1.3 !important;
  }

  /* Tabs: legibles y estables sin scroll horizontal */
  app-traking > .container > .tabla-toggle {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 5px !important;
  }

  app-traking > .container > .tabla-toggle > button:not(.btn-limpiar) {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 40px !important;
    margin: 0 !important;
    padding: 5px 3px !important;
    font-size: 0.58rem !important;
    line-height: 1.12 !important;
    white-space: normal !important;
  }

  app-traking > .container > .tabla-toggle > .btn-limpiar,
  app-traking > .container > .tabla-toggle > .paginacion,
  app-traking > .container > .tabla-toggle > .tabla-resultados,
  app-traking > .container > .tabla-toggle > table.tabla-ordenes {
    grid-column: 1 / -1 !important;
  }
}
