/*
 * SGI Mobile-First Responsive System v1.0
 * Sistema de Gestión Interna - Última Milla
 * Enero 2026
 * 
 * ESTRATEGIA: Mobile-First
 * Los estilos base son para móvil, media queries escalan hacia arriba
 */

/* ============================================
   VARIABLES Y BREAKPOINTS
   ============================================ */
:root {
  /* Breakpoints (referencia) */
  --sgi-bp-xs: 0;
  --sgi-bp-sm: 576px;
  --sgi-bp-md: 768px;
  --sgi-bp-lg: 992px;
  --sgi-bp-xl: 1200px;
  --sgi-bp-xxl: 1400px;
  
  /* Touch target mínimo (WCAG 2.1) */
  --sgi-touch-min: 44px;
  
  /* Espaciado mobile */
  --sgi-spacing-xs: 0.25rem;
  --sgi-spacing-sm: 0.5rem;
  --sgi-spacing-md: 1rem;
  --sgi-spacing-lg: 1.5rem;
}

/* ============================================
   SISTEMA DE HEADER ESTÁNDAR - UNIFORME
   ============================================ */

/* Header de página principal */
.sgi-page-header {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.75rem 0;
  margin-bottom: 1rem;
}

@media (min-width: 576px) {
  .sgi-page-header {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
  }
}

/* Navegación del header (título + volver) */
.sgi-header-nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Botón volver */
.sgi-btn-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background-color: #f1f5f9;
  color: #475569;
  text-decoration: none;
  transition: all 0.15s;
}

.sgi-btn-back:hover {
  background-color: #e2e8f0;
  color: #1e293b;
}

/* Título de página */
.sgi-header-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
  color: #1e293b;
}

@media (min-width: 576px) {
  .sgi-header-title {
    font-size: 1.5rem;
  }
}

/* Acciones del header */
.sgi-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  width: 100%;
}

@media (min-width: 576px) {
  .sgi-header-actions {
    width: auto;
  }
}

/* Botones de acción en header */
.sgi-header-actions .btn {
  flex: 1 1 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  min-height: 40px;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: 8px;
}

@media (min-width: 576px) {
  .sgi-header-actions .btn {
    flex: 0 0 auto;
  }
}

/* Iconos en botones siempre visibles */
.sgi-header-actions .btn .material-icons {
  font-size: 18px;
}

/* Card header (dentro de formularios) */
.card-header.sgi-card-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
}

.card-header.sgi-card-header h4,
.card-header.sgi-card-header h5 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

/* ============================================
   TOOLTIPS Y NOMBRES TRUNCADOS EN TABLAS
   ============================================ */

/* Celda con texto truncado que muestra tooltip */
.sgi-truncate {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: help;
  position: relative;
}

/* Tooltip personalizado para móvil (tap to show) */
.sgi-truncate[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 0;
  top: 100%;
  background-color: #1e293b;
  color: #fff;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12px;
  white-space: normal;
  max-width: 250px;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
  pointer-events: none;
}

.sgi-truncate:hover::after,
.sgi-truncate:focus::after,
.sgi-truncate:active::after {
  opacity: 1;
  visibility: visible;
}

/* Versión mobile: mostrar 2 líneas en vez de truncar agresivamente */
@media (max-width: 575.98px) {
  .table td:first-child {
    max-width: 100px;
    white-space: normal !important;
    line-height: 1.3;
    word-break: break-word;
  }
  
  /* Mostrar máximo 2 líneas con ellipsis */
  .sgi-truncate-2lines {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    white-space: normal !important;
    max-width: none;
  }
}

/* Tooltip para botones de acción */
.table .btn[title] {
  position: relative;
}

/* ============================================
   BOTONES DE HEADER CON TEXTO SIEMPRE VISIBLE
   ============================================ */

/* Botones en listados - mostrar texto corto siempre */
.sgi-btn-labeled {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px !important;
  font-size: 13px !important;
  font-weight: 500;
  border-radius: 8px;
  min-height: 40px;
}

.sgi-btn-labeled .material-icons {
  font-size: 18px;
}

/* En mobile, botones ocupan full width y se apilan */
@media (max-width: 575.98px) {
  .sgi-header-actions .sgi-btn-labeled {
    flex: 1 1 calc(50% - 4px);
    justify-content: center;
  }
}

/* ============================================
   FIX: HEADERS RESPONSIVOS
   ============================================ */

/* Todos los headers con d-flex ahora tienen wrap */
.container-fluid > .d-flex,
.card-header .d-flex,
.d-flex.justify-content-between {
  flex-wrap: wrap;
  gap: var(--sgi-spacing-sm);
}

/* Headers de página - stack vertical en mobile */
@media (max-width: 575.98px) {
  .container-fluid > .d-flex:first-child,
  .container-fluid > .row:first-child .d-flex {
    flex-direction: column;
    align-items: stretch !important;
  }
  
  .container-fluid > .d-flex:first-child > div,
  .container-fluid > .d-flex:first-child > h1,
  .container-fluid > .d-flex:first-child > h2 {
    width: 100%;
    text-align: center;
    margin-bottom: var(--sgi-spacing-sm);
  }
  
  /* Títulos más pequeños en mobile */
  h1.h2, h2.h3, .h2, .h3 {
    font-size: 1.25rem;
  }
}

/* ============================================
   FIX: GRUPOS DE BOTONES
   ============================================ */

/* Btn-group se convierte en stack en mobile */
@media (max-width: 575.98px) {
  .btn-group:not(.btn-group-sm):not(.dropdown),
  .btn-group-lg {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    gap: var(--sgi-spacing-xs) !important;
  }
  
  .btn-group:not(.btn-group-sm):not(.dropdown) > .btn,
  .btn-group-lg > .btn {
    width: 100% !important;
    border-radius: var(--sgi-radius-md, 0.375rem) !important;
    margin: 0 !important;
  }
  
  /* Flex container para botones de acción */
  .d-flex.gap-2,
  .d-flex.gap-3 {
    flex-wrap: wrap;
  }
}

/* Botones de acción en tablas - siempre horizontal pero compactos */
@media (max-width: 575.98px) {
  .table .btn-group.btn-group-sm {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    gap: 2px !important;
  }
  
  .table .btn-group-sm .btn {
    min-width: var(--sgi-touch-min) !important;
    min-height: var(--sgi-touch-min) !important;
    padding: 0.375rem !important;
    flex: 0 0 auto !important;
  }
  
  /* Ocultar texto en botones de tabla, solo iconos */
  .table .btn-group-sm .btn span:not(.material-icons),
  .table .btn-group-sm .btn .btn-text {
    display: none !important;
  }
  
  .table .btn-group-sm .btn .material-icons {
    font-size: 18px !important;
    margin: 0 !important;
  }
}

/* ============================================
   FIX: FORMULARIOS DE BÚSQUEDA
   ============================================ */

/* Input groups full-width en mobile */
@media (max-width: 575.98px) {
  .input-group {
    width: 100%;
  }
  
  .input-group-lg {
    flex-wrap: nowrap;
  }
  
  .input-group .form-control {
    min-width: 0; /* Permite shrink */
  }
  
  /* Botón de búsqueda compacto */
  .input-group .btn {
    white-space: nowrap;
    flex-shrink: 0;
  }
  
  /* Ocultar texto en botones de búsqueda, solo icono */
  .input-group .btn span:not(.material-icons) {
    display: none;
  }
}

/* Formularios row con columnas - margin bottom en mobile */
@media (max-width: 767.98px) {
  .row > [class*="col-md-"],
  .row > [class*="col-lg-"],
  .row > [class*="col-xl-"] {
    margin-bottom: var(--sgi-spacing-sm);
  }
  
  /* Último elemento sin margin */
  .row > [class*="col-"]:last-child {
    margin-bottom: 0;
  }
}

/* ============================================
   FIX: PAGINACIÓN RESPONSIVA
   ============================================ */

/* Paginación simplificada en mobile */
@media (max-width: 575.98px) {
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--sgi-spacing-xs);
  }
  
  /* Touch targets adecuados */
  .pagination .page-link {
    min-width: var(--sgi-touch-min);
    min-height: var(--sgi-touch-min);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--sgi-spacing-sm);
  }
  
  /* Ocultar números intermedios, mostrar solo prev/current/next */
  .pagination .page-item:not(.active):not(:first-child):not(:last-child):not(:nth-child(2)):not(:nth-last-child(2)) {
    display: none;
  }
  
  /* Material icons en paginación */
  .pagination .material-icons {
    font-size: 18px;
  }
}

/* Tablet: mostrar más páginas */
@media (min-width: 576px) and (max-width: 767.98px) {
  .pagination .page-item {
    display: flex;
  }
}

/* Info de paginación en stack en mobile */
@media (max-width: 575.98px) {
  .card-footer .d-flex.justify-content-between {
    flex-direction: column;
    gap: var(--sgi-spacing-md);
    text-align: center;
  }
}

/* ============================================
   FIX: CARDS Y CONTENEDORES
   ============================================ */

@media (max-width: 575.98px) {
  .card {
    border-radius: 0.5rem;
    margin-left: -0.5rem;
    margin-right: -0.5rem;
    border-left: none;
    border-right: none;
  }
  
  .card-body {
    padding: var(--sgi-spacing-md);
  }
  
  .card-header,
  .card-footer {
    padding: var(--sgi-spacing-sm) var(--sgi-spacing-md);
  }
}

/* ============================================
   FIX: MODALES RESPONSIVOS
   ============================================ */

@media (max-width: 575.98px) {
  .modal-dialog {
    margin: var(--sgi-spacing-sm);
    max-width: calc(100% - 1rem);
  }
  
  .modal-content {
    border-radius: 0.75rem;
  }
  
  .modal-header,
  .modal-body,
  .modal-footer {
    padding: var(--sgi-spacing-md);
  }
  
  .modal-footer {
    flex-wrap: wrap;
    gap: var(--sgi-spacing-sm);
  }
  
  .modal-footer .btn {
    flex: 1 1 auto;
  }
}

/* ============================================
   FIX: FILTROS COLAPSABLES
   ============================================ */

/* En mobile, filtros colapsados por defecto */
@media (max-width: 767.98px) {
  .card-header[data-bs-toggle="collapse"] {
    cursor: pointer;
  }
  
  .card-header[data-bs-toggle="collapse"]::after {
    content: '';
    display: inline-block;
    width: 0.5rem;
    height: 0.5rem;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg);
    margin-left: auto;
    transition: transform 0.2s;
  }
  
  .card-header[data-bs-toggle="collapse"][aria-expanded="true"]::after {
    transform: rotate(-135deg);
  }
}

/* ============================================
   FIX: BADGES EN TABLAS
   ============================================ */

@media (max-width: 575.98px) {
  .table .badge {
    font-size: 9px;
    padding: 3px 6px;
    max-width: 70px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

/* ============================================
   FIX: TEXTO TRUNCADO
   ============================================ */

@media (max-width: 575.98px) {
  .table td {
    max-width: 120px;
  }
  
  .table .truncate,
  .table td.col-nombre,
  .table td.col-cliente,
  .table td.clientes-nombre {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

@media (min-width: 576px) and (max-width: 991.98px) {
  .table .truncate,
  .table td.col-nombre,
  .table td.col-cliente,
  .table td.clientes-nombre {
    max-width: 150px;
  }
}

/* ============================================
   FIX: ALERTAS Y MENSAJES
   ============================================ */

@media (max-width: 575.98px) {
  .alert {
    padding: var(--sgi-spacing-sm) var(--sgi-spacing-md);
    font-size: 0.875rem;
  }
  
  .alert .d-flex {
    flex-wrap: wrap;
    gap: var(--sgi-spacing-sm);
  }
  
  .alert .btn-group {
    width: 100%;
    margin-top: var(--sgi-spacing-sm);
  }
}

/* ============================================
   FIX: DROPDOWNS
   ============================================ */

@media (max-width: 575.98px) {
  .dropdown-menu {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    transform: none !important;
    border-radius: 1rem 1rem 0 0;
    max-height: 60vh;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom);
  }
  
  .dropdown-item {
    padding: var(--sgi-spacing-md);
    min-height: var(--sgi-touch-min);
  }
}

/* Prevenir que dropdowns normales usen el estilo de bottom-sheet */
@media (max-width: 575.98px) {
  .navbar .dropdown-menu {
    position: absolute !important;
    bottom: auto !important;
    border-radius: var(--sgi-radius-md, 0.375rem);
  }
}

/* ============================================
   FIX: OFFCANVAS MOBILE MENU
   ============================================ */

.offcanvas {
  max-width: 85vw;
}

.offcanvas-body .nav-link {
  padding: var(--sgi-spacing-md);
  min-height: var(--sgi-touch-min);
  display: flex;
  align-items: center;
  gap: var(--sgi-spacing-sm);
}

/* ============================================
   FIX: TABLAS MÓVILES - ESTRATEGIA SIMPLIFICADA
   ============================================ */

/* Evitar overflow del contenedor */
.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

/* MÓVIL: Ocultar columnas secundarias - SOLO las que tienen d-none explícito */
@media (max-width: 575.98px) {
  /* Solo ocultar columnas que Bootstrap ya marca como ocultas en ese breakpoint */
  /* NO forzar display:none en columnas con clases d-md, d-lg, d-xl */
  
  /* Tamaño de fuente reducido */
  .table {
    font-size: 12px !important;
  }
  
  .table th,
  .table td {
    padding: 8px 6px !important;
    vertical-align: middle !important;
  }
  
  /* Primera columna (nombre/número) con ancho limitado */
  .table th:first-child,
  .table td:first-child {
    max-width: 90px !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  /* Columna de acciones: MÁXIMO 3 BOTONES VISIBLES */
  .table .btn-group {
    display: inline-flex !important;
    flex-wrap: nowrap !important;
    gap: 2px !important;
  }
  
  /* Cada botón pequeño y cuadrado */
  .table .btn-group .btn,
  .table .btn-group-sm .btn {
    flex: 0 0 auto !important;
    min-width: 32px !important;
    width: 32px !important;
    height: 32px !important;
    padding: 4px !important;
    border-radius: 4px !important;
    font-size: 0 !important; /* Oculta texto */
  }
  
  /* Solo mostrar primeros 3 botones */
  .table .btn-group .btn:nth-child(n+4) {
    display: none !important;
  }
  
  /* Iconos pequeños */
  .table .btn .material-icons {
    font-size: 16px !important;
    margin: 0 !important;
  }
  
  /* Badges compactos */
  .table .badge {
    font-size: 9px !important;
    padding: 2px 4px !important;
    max-width: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

/* TABLET: Mostrar algunas columnas más */
@media (min-width: 576px) and (max-width: 767.98px) {
  .table {
    font-size: 13px !important;
  }
  
  .table th,
  .table td {
    padding: 10px 8px !important;
  }
  
  /* Botones más grandes en tablet */
  .table .btn-group .btn {
    min-width: 36px !important;
    width: 36px !important;
    height: 36px !important;
  }
  
  /* Mostrar 4 botones en tablet */
  .table .btn-group .btn:nth-child(n+5) {
    display: none !important;
  }
}

/* DESKTOP: Mostrar todo */
@media (min-width: 768px) {
  .table .btn-group .btn {
    display: inline-flex !important;
  }
}

/* ============================================
   UTILIDADES RESPONSIVE
   ============================================ */

/* Ocultar completamente en mobile */
.hide-mobile {
  display: none !important;
}

@media (min-width: 576px) {
  .hide-mobile {
    display: initial !important;
  }
}

/* Mostrar solo en mobile */
.show-mobile {
  display: initial !important;
}

@media (min-width: 576px) {
  .show-mobile {
    display: none !important;
  }
}

/* Stack en mobile */
.stack-mobile {
  display: flex;
  flex-direction: column;
  gap: var(--sgi-spacing-sm);
}

@media (min-width: 576px) {
  .stack-mobile {
    flex-direction: row;
  }
}

/* Full width en mobile */
.full-mobile {
  width: 100%;
}

@media (min-width: 576px) {
  .full-mobile {
    width: auto;
  }
}

/* ============================================
   ANIMACIONES SUAVES
   ============================================ */

/* Transiciones suaves para todos los cambios responsive */
@media (prefers-reduced-motion: no-preference) {
  .btn,
  .form-control,
  .card,
  .table,
  .badge,
  .alert {
    transition: all 0.15s ease-in-out;
  }
}

/* ============================================
   SAFE AREA INSETS (iPhone X+)
   ============================================ */

@supports (padding: env(safe-area-inset-bottom)) {
  .sgi-bottom-nav {
    padding-bottom: env(safe-area-inset-bottom);
  }
  
  .modal-dialog {
    padding-bottom: env(safe-area-inset-bottom);
  }
}
