/*
 * SGI Mobile-First Table System v3.0
 * Professional Slate Design System Compatible
 * December 2025
 *
 * PRINCIPIO FUNDAMENTAL:
 * Las tablas son SIEMPRE tablas <table> - nunca cards ni variantes.
 * Scroll horizontal suave en móvil, columnas inteligentes.
 */

/* ============================================
   DESIGN SYSTEM - SLATE PALETTE
   ============================================ */
:root {
  --sgi-slate-50: #f8fafc;
  --sgi-slate-100: #f1f5f9;
  --sgi-slate-200: #e2e8f0;
  --sgi-slate-300: #cbd5e1;
  --sgi-slate-400: #94a3b8;
  --sgi-slate-500: #64748b;
  --sgi-slate-600: #475569;
  --sgi-slate-700: #334155;
  --sgi-slate-800: #1e293b;
  --sgi-slate-900: #0f172a;
  --sgi-slate-950: #020617;
  
  /* Primary Accent - Brand Blue */
  --sgi-primary: #3b82f6;
  --sgi-primary-dark: #2563eb;
  
  /* Semantic Colors */
  --sgi-success: #10b981;
  --sgi-warning: #f59e0b;
  --sgi-danger: #ef4444;
  --sgi-info: #0ea5e9;

  /* Card Styles */
  --sgi-card-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --sgi-card-radius: 16px;
}

.bg-slate-800 { background-color: var(--sgi-slate-800) !important; }
.bg-slate-900 { background-color: var(--sgi-slate-900) !important; }
.text-slate-300 { color: var(--sgi-slate-300) !important; }
.text-slate-400 { color: var(--sgi-slate-400) !important; }
.rounded-4 { border-radius: 1rem !important; }

/* ============================================
   BASE STYLES (Mobile First - 320px+)
   ============================================ */

/* Table Container */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: var(--sgi-gray-300, #cbd5e1) transparent;
  position: relative;
  border-radius: var(--sgi-radius-lg, 8px);
}

/* Visual scroll indicator - right edge fade for mobile */
.table-responsive::after {
  content: '';
  position: sticky;
  right: 0;
  top: 0;
  bottom: 0;
  width: 24px;
  background: linear-gradient(to right, rgba(0,0,0,0), rgba(0,0,0,0.04));
  pointer-events: none;
  z-index: 5;
}

.table-responsive::-webkit-scrollbar {
  height: 6px;
}

.table-responsive::-webkit-scrollbar-track {
  background: var(--sgi-gray-100, #f1f5f9);
  border-radius: 3px;
}

.table-responsive::-webkit-scrollbar-thumb {
  background: var(--sgi-gray-300, #cbd5e1);
  border-radius: 3px;
}

.table-responsive::-webkit-scrollbar-thumb:hover {
  background: var(--sgi-gray-400, #94a3b8);
}

/* Table Base */
.table {
  width: 100%;
  table-layout: auto;
  border-collapse: collapse;
  background-color: var(--sgi-bg-card, #ffffff);
  font-size: 12px;
}

/* ============================================
   TABLE HEADERS - Slate Gray Professional
   ============================================ */

.table thead {
  display: table-header-group;
  background-color: var(--sgi-gray-800, #1e293b);
}

.table thead th {
  color: var(--sgi-text-inverse, #ffffff);
  font-weight: 600;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 10px 8px;
  text-align: left;
  white-space: nowrap;
  vertical-align: middle;
  border: none;
  position: sticky;
  top: 0;
  z-index: 10;
  background-color: var(--sgi-gray-800, #1e293b);
}

.table thead th:first-child {
  border-radius: var(--sgi-radius-md, 6px) 0 0 0;
}

.table thead th:last-child {
  border-radius: 0 var(--sgi-radius-md, 6px) 0 0;
}

/* Sortable Headers */
.table thead th[onclick],
.table thead th.sortable {
  cursor: pointer;
  user-select: none;
  transition: background-color 0.15s ease;
}

.table thead th[onclick]:hover,
.table thead th.sortable:hover {
  background-color: var(--sgi-gray-700, #334155);
}

/* Sort Indicators */
.table thead th .sort-icon,
.table thead th i.fa-sort,
.table thead th i.fa-sort-up,
.table thead th i.fa-sort-down {
  font-size: 10px;
  margin-left: 4px;
  opacity: 0.7;
}

.table thead th.sorted-asc::after {
  content: ' ↑';
  font-size: 10px;
  opacity: 0.8;
}

.table thead th.sorted-desc::after {
  content: ' ↓';
  font-size: 10px;
  opacity: 0.8;
}

/* ============================================
   TABLE BODY
   ============================================ */

.table tbody {
  display: table-row-group;
}

.table tbody tr {
  border-bottom: 1px solid var(--sgi-border-light, #e2e8f0);
  transition: background-color 0.1s ease;
  min-height: 48px; /* Touch-friendly minimum height */
}

.table tbody tr:last-child {
  border-bottom: none;
}

.table tbody tr:hover {
  background-color: var(--sgi-bg-hover, #f1f5f9);
}

.table tbody td {
  padding: 10px 8px;
  font-size: 12px;
  color: var(--sgi-text-primary, #0f172a);
  vertical-align: middle;
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* ============================================
   MOBILE SPECIFIC (< 576px)
   ============================================ */

@media (max-width: 575.98px) {
  .table {
    font-size: 11px;
    border-collapse: separate; /* Necesario para que el sticky funcione bien con bordes */
    border-spacing: 0;
  }

  /* Sticky First Column Logic */
  .table thead th:first-child,
  .table tbody td:first-child {
    position: sticky;
    left: 0;
    z-index: 20;
    box-shadow: 4px 0 8px -2px rgba(0, 0, 0, 0.1);
    border-right: 1px solid var(--sgi-slate-200);
  }

  .table thead th:first-child {
    background-color: var(--sgi-slate-800) !important;
    z-index: 30; /* Encima de las celdas sticky del body */
  }

  .table tbody td:first-child {
    background-color: var(--sgi-bg-card, #ffffff) !important;
  }

  .table thead th {
    padding: 12px 8px;
    font-size: 9px;
    letter-spacing: 0.05em;
  }

  .table tbody td {
    padding: 12px 8px;
    font-size: 12px;
  }

  /* Ensure Bootstrap d-none classes work properly */
  .table th.d-none,
  .table td.d-none {
    display: none !important;
  }

  /* Row height and touch targets */
  .table tbody tr {
    min-height: 56px;
  }

  /* Action buttons - refined for sticky layout */
  .table .btn-group {
    display: flex;
    gap: 4px;
    flex-wrap: nowrap;
  }

  .table .btn-sm,
  .table .btn-group .btn {
    padding: 8px;
    min-width: 40px !important;
    min-height: 40px !important;
  }

  /* Hide button text on mobile, show only icons */
  .table .btn-sm .btn-text,
  .table .btn-group .btn .btn-text {
    display: none;
  }
}

/* ============================================
   TABLET PORTRAIT (>= 576px)
   ============================================ */

@media (min-width: 576px) {
  .table {
    font-size: 12px;
  }

  .table thead th {
    padding: 10px 8px;
    font-size: 9px;
  }

  .table tbody td {
    padding: 10px 8px;
    font-size: 12px;
  }
}

/* ============================================
   TABLET LANDSCAPE (>= 768px)
   ============================================ */

@media (min-width: 768px) {
  .table thead th {
    font-size: 10px;
    padding: 12px 10px;
  }

  .table tbody td {
    padding: 12px 10px;
    font-size: 13px;
  }

  .table .btn-sm {
    padding: 6px 12px;
    min-height: 36px;
  }

  .table .badge {
    font-size: 10px;
    padding: 4px 8px;
  }
}

/* ============================================
   DESKTOP (>= 992px)
   ============================================ */

@media (min-width: 992px) {
  .table thead th {
    font-size: 10px;
    padding: 12px 12px;
    letter-spacing: 0.05em;
  }

  .table tbody td {
    padding: 12px;
    font-size: 13px;
  }

  .table .truncate {
    max-width: 200px;
  }
}

/* ============================================
   LARGE DESKTOP (>= 1200px)
   ============================================ */

@media (min-width: 1200px) {
  .table {
    font-size: 14px;
  }

  .table thead th {
    font-size: 11px;
    padding: 14px 12px;
  }

  .table tbody td {
    font-size: 14px;
    padding: 14px 12px;
  }
}

/* ============================================
   MODULE-SPECIFIC COLUMN WIDTHS (Desktop)
   ============================================ */

/* Clientes */
@media (min-width: 992px) {
  .clientes-table .clientes-nombre,
  .clientes-table .col-nombre { min-width: 200px; }
  .clientes-table .clientes-tipo,
  .clientes-table .col-tipo { width: 100px; }
  .clientes-table .clientes-acciones,
  .clientes-table .col-acciones { width: 120px; }
}

/* Certificados */
@media (min-width: 992px) {
  .certificados-table .certificados-numero,
  .certificados-table .col-numero { width: 90px; }
  .certificados-table .certificados-cliente,
  .certificados-table .col-cliente { min-width: 180px; }
  .certificados-table .certificados-estado,
  .certificados-table .col-estado { width: 110px; }
  .certificados-table .certificados-acciones,
  .certificados-table .col-acciones { width: 120px; }
}

/* Facturas Emitidas */
@media (min-width: 992px) {
  .facturas-emitidas-table .fe-numero,
  .facturas-emitidas-table .col-numero { width: 100px; }
  .facturas-emitidas-table .fe-cliente,
  .facturas-emitidas-table .col-cliente { min-width: 180px; }
  .facturas-emitidas-table .fe-total,
  .facturas-emitidas-table .col-total { width: 120px; text-align: right; }
  .facturas-emitidas-table .fe-estado,
  .facturas-emitidas-table .col-estado { width: 110px; }
  .facturas-emitidas-table .fe-acciones,
  .facturas-emitidas-table .col-acciones { width: 120px; }
}

/* Facturas Recibidas */
@media (min-width: 992px) {
  .facturas-recibidas-table .fr-numero,
  .facturas-recibidas-table .col-numero { width: 100px; }
  .facturas-recibidas-table .fr-proveedor,
  .facturas-recibidas-table .col-proveedor { min-width: 180px; }
  .facturas-recibidas-table .fr-total,
  .facturas-recibidas-table .col-total { width: 120px; text-align: right; }
  .facturas-recibidas-table .fr-estado,
  .facturas-recibidas-table .col-estado { width: 110px; }
  .facturas-recibidas-table .fr-acciones,
  .facturas-recibidas-table .col-acciones { width: 120px; }
}

/* Presupuestos */
@media (min-width: 992px) {
  .presupuestos-table .presupuestos-numero,
  .presupuestos-table .col-numero { width: 90px; }
  .presupuestos-table .presupuestos-cliente,
  .presupuestos-table .col-cliente { min-width: 180px; }
  .presupuestos-table .presupuestos-estado,
  .presupuestos-table .col-estado { width: 110px; }
  .presupuestos-table .presupuestos-acciones,
  .presupuestos-table .col-acciones { width: 120px; }
}

/* Proyectos */
@media (min-width: 992px) {
  .proyectos-table .proyectos-nombre,
  .proyectos-table .col-nombre { min-width: 220px; }
  .proyectos-table .proyectos-estado,
  .proyectos-table .col-estado { width: 110px; }
  .proyectos-table .proyectos-acciones,
  .proyectos-table .col-acciones { width: 120px; }
}

/* ============================================
   BADGES IN TABLES - Premium Pill Style
   ============================================ */
 
.table .badge {
  font-size: 10px;
  padding: 4px 10px;
  font-weight: 600;
  border-radius: 9999px;
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* SGI Specific Badges */
.badge-success { background-color: #dcfce7 !important; color: #15803d !important; }
.badge-warning { background-color: #fef3c7 !important; color: #92400e !important; }
.badge-danger { background-color: #fee2e2 !important; color: #b91c1c !important; }
.badge-info { background-color: #e0f2fe !important; color: #0369a1 !important; }
.badge-primary { background-color: #dbeafe !important; color: #1d4ed8 !important; }
.badge-secondary { background-color: #f1f5f9 !important; color: #475569 !important; }

@media (min-width: 768px) {
  .table .badge {
    font-size: 10px;
    padding: 4px 10px;
  }
}

/* ============================================
   ACTION BUTTONS IN TABLES
   ============================================ */

.table .btn-group {
  display: inline-flex;
  gap: 2px;
  flex-wrap: nowrap;
}

.table .btn-sm {
  padding: 5px 8px;
  font-size: 11px;
  line-height: 1.4;
  border-radius: var(--sgi-radius-md, 6px);
  min-height: 32px;
}

.table .btn-sm i {
  font-size: 12px;
}

@media (min-width: 768px) {
  .table .btn-group {
    gap: 4px;
  }

  .table .btn-sm {
    padding: 6px 10px;
    font-size: 12px;
  }
}

@media (min-width: 992px) {
  .table .btn-sm {
    padding: 6px 12px;
    font-size: 12px;
  }
}

/* ============================================
   UTILITIES
   ============================================ */

/* Text Alignment */
.table .text-end { text-align: right !important; }
.table .text-center { text-align: center !important; }
.table .text-start { text-align: left !important; }

/* Numeric Columns */
.table .numeric,
.table .col-numeric {
  text-align: right;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
}

/* Currency */
.table .currency,
.table .col-currency {
  text-align: right;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
}

/* Date */
.table .date,
.table .col-date {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  color: var(--sgi-text-secondary, #475569);
  font-size: 0.9em;
}

/* Truncate */
.table .truncate {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 767.98px) {
  .table .truncate {
    max-width: 100px;
  }
}

@media (max-width: 575.98px) {
  .table .truncate {
    max-width: 80px;
  }
}

/* ============================================
   EMPTY STATE
   ============================================ */

.table tbody tr.empty-row td,
.table-empty-message {
  text-align: center;
  padding: 48px 20px;
  color: var(--sgi-text-muted, #94a3b8);
  font-style: normal;
}

.table-empty-message i {
  font-size: 2.5rem;
  display: block;
  margin-bottom: 12px;
  opacity: 0.5;
}

/* ============================================
   LOADING STATE
   ============================================ */

.table-loading {
  position: relative;
  pointer-events: none;
}

.table-loading::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.7);
  z-index: 20;
}

.table-loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 32px;
  height: 32px;
  margin: -16px 0 0 -16px;
  border: 3px solid var(--sgi-gray-200, #e2e8f0);
  border-top-color: var(--sgi-accent, #0ea5e9);
  border-radius: 50%;
  animation: table-spin 0.7s linear infinite;
  z-index: 21;
}

@keyframes table-spin {
  to { transform: rotate(360deg); }
}

/* ============================================
   ACCESSIBILITY
   ============================================ */

/* Focus states */
.table thead th:focus,
.table tbody td:focus,
.table tbody tr:focus-within {
  outline: 2px solid var(--sgi-accent, #0ea5e9);
  outline-offset: -2px;
}

/* Screen reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
  .table {
    border: 1px solid #ccc;
  }

  .table thead {
    background-color: #e5e7eb !important;
    color: #1f2937 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .table thead th {
    background-color: #e5e7eb !important;
    color: #1f2937 !important;
    border-bottom: 2px solid #9ca3af;
  }

  .table tbody tr {
    page-break-inside: avoid;
  }

  /* Show ALL columns when printing */
  .table .d-none,
  .table .d-md-none,
  .table .d-lg-none,
  .table .d-xl-none {
    display: table-cell !important;
  }

  /* Hide action buttons */
  .table .btn,
  .table .btn-group,
  .table .col-acciones,
  .table .actions {
    display: none !important;
  }

  .table .badge {
    border: 1px solid #9ca3af;
    background: transparent !important;
    color: #1f2937 !important;
  }
}

/* ============================================
   EMBEDDED TABLES (Inside cards/details)
   ============================================ */

.card .table {
  margin-bottom: 0;
  border-radius: 0;
}

.card .table thead th {
  font-size: 9px;
  padding: 8px 10px;
}

.card .table tbody td {
  font-size: 12px;
  padding: 8px 10px;
}

@media (min-width: 768px) {
  .card .table thead th {
    font-size: 10px;
    padding: 10px 12px;
  }

  .card .table tbody td {
    font-size: 13px;
    padding: 10px 12px;
  }
}

/* ============================================
   STRIPED VARIANT
   ============================================ */

.table-striped tbody tr:nth-of-type(odd) {
  background-color: var(--sgi-gray-50, #f8fafc);
}

.table-striped tbody tr:nth-of-type(odd):hover {
  background-color: var(--sgi-bg-hover, #f1f5f9);
}

/* ============================================
   ROW STATES
   ============================================ */

.table tbody tr.row-success {
  background-color: var(--sgi-success-bg, #ecfdf5);
}

.table tbody tr.row-warning {
  background-color: var(--sgi-warning-bg, #fffbeb);
}

.table tbody tr.row-danger {
  background-color: var(--sgi-danger-bg, #fef2f2);
}

.table tbody tr.row-info {
  background-color: var(--sgi-info-bg, #ecfeff);
}

/* ============================================
   MATERIAL ICONS SUPPORT
   ============================================ */

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  white-space: nowrap;
  direction: ltr;
  vertical-align: middle;
}

/* Material Icons in table headers */
.table thead th .material-icons {
  font-size: 14px;
  margin-right: 4px;
  vertical-align: middle;
}

/* Material Icons in table cells */
.table tbody td .material-icons {
  font-size: 16px;
  margin-right: 2px;
}

/* Material Icons in buttons */
.table .btn .material-icons {
  margin: 0;
  font-size: 18px;
}

/* Smaller Material Icons for inline usage */
.material-icons.sm {
  font-size: 14px;
}

.material-icons.lg {
  font-size: 24px;
}

.material-icons.xl {
  font-size: 32px;
}
