/**
 * Universal Table System
 * Comprehensive table components with consistent styling and variants
 * Consolidates all table patterns from across the application
 */

/* ===========================================
   TABLE CONTAINER
   =========================================== */

.table-container {
  position: relative;
  overflow-x: auto;
  border-radius: var(--component-radius);
  border: var(--component-border);
  background-color: var(--bg-elevated);
  box-shadow: var(--component-shadow);
}

.table-container::-webkit-scrollbar {
  height: 8px;
}

.table-container::-webkit-scrollbar-track {
  background: var(--bg-tertiary);
  border-radius: var(--component-radius-sm);
}

.table-container::-webkit-scrollbar-thumb {
  background: var(--color-gray-400);
  border-radius: var(--component-radius-sm);
}

.table-container::-webkit-scrollbar-thumb:hover {
  background: var(--color-gray-500);
}

/* ===========================================
   BASE TABLE COMPONENT
   =========================================== */

.table {
  width: 100%;
  margin-bottom: 0;
  color: var(--text-primary);
  background-color: transparent;
  border-collapse: collapse;
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}

.table th,
.table td {
  padding: var(--space-md) var(--space-lg);
  vertical-align: middle;
  border-bottom: 1px solid var(--color-gray-200);
  text-align: left;
}

.table th {
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  background-color: var(--bg-secondary);
  border-bottom: 2px solid var(--color-gray-300);
  position: sticky;
  top: 0;
  z-index: 1;
}

.table td {
  background-color: var(--bg-primary);
}

.table tbody tr {
  transition: var(--transition-colors);
}

.table tbody tr:hover {
  background-color: var(--bg-secondary);
}

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

/* ===========================================
   TABLE VARIANTS
   =========================================== */

/* Striped table */
.table-striped tbody tr:nth-of-type(odd) {
  background-color: var(--bg-secondary);
}

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

/* Bordered table */
.table-bordered {
  border: var(--component-border);
}

.table-bordered th,
.table-bordered td {
  border: var(--component-border);
}

.table-bordered thead th,
.table-bordered thead td {
  border-bottom-width: 2px;
}

/* Borderless table */
.table-borderless th,
.table-borderless td,
.table-borderless thead th,
.table-borderless tbody + tbody {
  border: 0;
}

.table-borderless .table-container {
  border: none;
  box-shadow: none;
}

/* Compact table */
.table-compact th,
.table-compact td {
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-xs);
}

/* Spacious table */
.table-spacious th,
.table-spacious td {
  padding: var(--space-lg) var(--space-xl);
  font-size: var(--text-base);
}

/* Full width table */
.table-full-width {
  width: 100%;
  min-width: 100%;
}

.table-full-width .table-container {
  border-left: none;
  border-right: none;
  border-radius: 0;
}

/* Fixed layout table */
.table-fixed {
  table-layout: fixed;
}

/* ===========================================
   TABLE SIZES
   =========================================== */

.table-sm th,
.table-sm td {
  padding: var(--space-xs) var(--space-sm);
  font-size: var(--text-xs);
}

.table-lg th,
.table-lg td {
  padding: var(--space-lg) var(--space-2xl);
  font-size: var(--text-base);
}

/* ===========================================
   TABLE CELL ALIGNMENT
   =========================================== */

.table th.text-left,
.table td.text-left {
  text-align: left;
}

.table th.text-center,
.table td.text-center {
  text-align: center;
}

.table th.text-right,
.table td.text-right {
  text-align: right;
}

.table th.align-top,
.table td.align-top {
  vertical-align: top;
}

.table th.align-middle,
.table td.align-middle {
  vertical-align: middle;
}

.table th.align-bottom,
.table td.align-bottom {
  vertical-align: bottom;
}

/* ===========================================
   TABLE CELL STYLES
   =========================================== */

/* Active/highlighted row */
.table tbody tr.table-active {
  background-color: var(--color-primary-light);
}

.table tbody tr.table-active:hover {
  background-color: rgba(220, 53, 69, 0.15);
}

/* Status row colors */
.table tbody tr.table-success {
  background-color: var(--color-success-light);
}

.table tbody tr.table-warning {
  background-color: var(--color-warning-light);
}

.table tbody tr.table-danger {
  background-color: var(--color-danger-light);
}

.table tbody tr.table-info {
  background-color: var(--color-info-light);
}

/* Cell status colors */
.table th.table-primary,
.table td.table-primary {
  background-color: rgba(220, 53, 69, 0.1);
  color: var(--color-primary-dark);
}

.table th.table-success,
.table td.table-success {
  background-color: var(--color-success-light);
  color: var(--color-success-dark);
}

.table th.table-warning,
.table td.table-warning {
  background-color: var(--color-warning-light);
  color: var(--color-warning-dark);
}

.table th.table-danger,
.table td.table-danger {
  background-color: var(--color-danger-light);
  color: var(--color-danger-dark);
}

.table th.table-info,
.table td.table-info {
  background-color: var(--color-info-light);
  color: var(--color-info-dark);
}

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

/* Sortable columns */
.table th.sortable {
  cursor: pointer;
  user-select: none;
  position: relative;
  padding-right: var(--space-2xl);
}

.table th.sortable::after {
  content: '';
  position: absolute;
  right: var(--space-md);
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 4px solid var(--color-gray-400);
  opacity: 0.5;
  transition: var(--transition-colors);
}

.table th.sortable:hover::after {
  opacity: 0.8;
}

.table th.sortable.sort-asc::after {
  border-bottom: 4px solid var(--color-primary);
  border-top: none;
  opacity: 1;
}

.table th.sortable.sort-desc::after {
  border-top: 4px solid var(--color-primary);
  border-bottom: none;
  opacity: 1;
}

/* Resizable columns */
.table th.resizable {
  position: relative;
}

.table th.resizable::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 2px;
  height: 100%;
  background-color: var(--color-gray-300);
  cursor: col-resize;
  opacity: 0;
  transition: var(--transition-colors);
}

.table th.resizable:hover::before {
  opacity: 1;
}

/* Sticky columns */
.table th.sticky-left,
.table td.sticky-left {
  position: sticky;
  left: 0;
  z-index: 2;
  background-color: inherit;
  border-right: var(--component-border);
}

.table th.sticky-right,
.table td.sticky-right {
  position: sticky;
  right: 0;
  z-index: 2;
  background-color: inherit;
  border-left: var(--component-border);
}

/* ===========================================
   SPECIAL TABLE TYPES
   =========================================== */

/* Stats table for displaying statistics */
.table-stats th {
  background-color: var(--color-primary);
  color: var(--text-inverse);
  font-weight: var(--font-bold);
  text-align: center;
}

.table-stats td {
  text-align: center;
  font-weight: var(--font-medium);
}

.table-stats .stat-value {
  font-size: var(--text-lg);
  font-weight: var(--font-bold);
  color: var(--color-primary);
}

.table-stats .stat-label {
  font-size: var(--text-xs);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Comparison table */
.table-comparison th {
  background-color: var(--bg-tertiary);
  text-align: center;
  font-weight: var(--font-bold);
}

.table-comparison td.feature-name {
  font-weight: var(--font-medium);
  background-color: var(--bg-secondary);
}

.table-comparison td.feature-included {
  text-align: center;
  color: var(--color-success);
  font-weight: var(--font-bold);
}

.table-comparison td.feature-excluded {
  text-align: center;
  color: var(--color-danger);
  font-weight: var(--font-bold);
}

/* Timeline table */
.table-timeline th {
  background-color: var(--color-info);
  color: var(--text-inverse);
}

.table-timeline td.timeline-date {
  font-weight: var(--font-semibold);
  color: var(--color-info-dark);
  white-space: nowrap;
}

.table-timeline td.timeline-event {
  position: relative;
  padding-left: var(--space-2xl);
}

.table-timeline td.timeline-event::before {
  content: '';
  position: absolute;
  left: var(--space-lg);
  top: 50%;
  transform: translateY(-50%);
  width: var(--space-sm);
  height: var(--space-sm);
  background-color: var(--color-info);
  border-radius: 50%;
}

/* ===========================================
   TABLE ACTIONS
   =========================================== */

.table-actions {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  justify-content: flex-end;
}

.table-actions .btn {
  padding: var(--space-xs) var(--space-sm);
  font-size: var(--text-xs);
}

.table-actions .btn-icon {
  padding: var(--space-xs);
}

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

.table-loading {
  position: relative;
}

.table-loading::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.8);
  z-index: 10;
  backdrop-filter: blur(2px);
}

.table-loading::after {
  content: 'Loading...';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: var(--text-lg);
  font-weight: var(--font-medium);
  color: var(--text-secondary);
  z-index: 11;
}

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

.table-empty {
  text-align: center;
  padding: var(--space-3xl) var(--space-xl);
  color: var(--text-muted);
}

.table-empty-icon {
  font-size: var(--text-4xl);
  color: var(--color-gray-300);
  margin-bottom: var(--space-lg);
}

.table-empty-title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--text-secondary);
  margin-bottom: var(--space-sm);
}

.table-empty-description {
  font-size: var(--text-sm);
  color: var(--text-muted);
  margin-bottom: var(--space-lg);
}

/* ===========================================
   TABLE PAGINATION
   =========================================== */

.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-lg) var(--space-xl);
  background-color: var(--bg-secondary);
  border-top: var(--component-border);
  border-radius: 0 0 var(--component-radius) var(--component-radius);
}

.table-pagination-info {
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

.table-pagination-controls {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

/* ===========================================
   RESPONSIVE TABLE BEHAVIORS
   =========================================== */

@media (max-width: 768px) {
  /* Stack table for mobile */
  .table-responsive-stack {
    border: none;
    border-radius: 0;
  }
  
  .table-responsive-stack .table,
  .table-responsive-stack .table thead,
  .table-responsive-stack .table tbody,
  .table-responsive-stack .table th,
  .table-responsive-stack .table td,
  .table-responsive-stack .table tr {
    display: block;
  }
  
  .table-responsive-stack .table thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }
  
  .table-responsive-stack .table tr {
    background-color: var(--bg-elevated);
    border: var(--component-border);
    border-radius: var(--component-radius);
    margin-bottom: var(--space-lg);
    padding: var(--space-lg);
  }
  
  .table-responsive-stack .table td {
    border: none;
    position: relative;
    padding-left: 50%;
    padding-bottom: var(--space-sm);
    white-space: normal;
    text-align: right;
  }
  
  .table-responsive-stack .table td::before {
    content: attr(data-label);
    position: absolute;
    left: 0;
    width: 45%;
    text-align: left;
    font-weight: var(--font-semibold);
    color: var(--text-primary);
  }
  
  /* Reduce padding on mobile */
  .table th,
  .table td {
    padding: var(--space-sm) var(--space-md);
  }
  
  .table-actions {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-xs);
  }
  
  .table-actions .btn {
    width: 100%;
    justify-content: center;
  }
  
  .table-pagination {
    flex-direction: column;
    gap: var(--space-md);
    text-align: center;
  }
}

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

/* Focus styles for interactive elements */
.table th.sortable:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
}

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

/* High contrast mode support */
@media (prefers-contrast: high) {
  .table {
    border: 2px solid;
  }
  
  .table th,
  .table td {
    border: 1px solid;
  }
  
  .table th.sortable:focus {
    outline: 3px solid;
    outline-offset: 2px;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  .table tbody tr,
  .table th.sortable::after {
    transition: none;
  }
}

/* Print styles */
@media print {
  .table-container {
    border: 1px solid #000;
    box-shadow: none;
    overflow: visible;
  }
  
  .table {
    border-collapse: collapse;
  }
  
  .table th,
  .table td {
    border: 1px solid #000;
    background-color: transparent !important;
    color: #000 !important;
  }
  
  .table-actions {
    display: none;
  }
  
  .table-pagination {
    display: none;
  }
}