/* css/table.css - Modern Table Styling (Auto-apply, No Classes) */

/* ============================================
   Table Container (auto-applies to any table wrapper)
   ============================================ */
main > div:has(> table),
.content:has(> table),
div:has(> table) {
  background: var(--bg-primary);
  border-radius: var(--border-radius-lg);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  margin-bottom: var(--space-6);
}

/* ============================================
   Table Base
   ============================================ */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  background: var(--bg-primary);
  border-spacing: 0;
}

content:has(table) {
  border-radius: var(--border-radius-lg) var(--border-radius-lg) 0 0;
  box-shadow: var(--shadow-sm);

  header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-primary);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--border-radius-lg) var(--border-radius-lg) 0 0;
    border-bottom: 1px solid var(--border-color);
  }

  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    margin: var(--space-3) var(--space-2);
    font-size: var(--text-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
  }

  > div {
    display: flex;
    justify-content: space-between;
    align-items: center;

    button,
    a .btn-primary,
    a .btn-secondary {
      padding: var(--space-1) var(--space-3) !important;
    }
  }
  > div:last-child {
    border-radius: 0 0 var(--border-radius-lg) var(--border-radius-lg);
  }
}

/* ============================================
   Table Header
   ============================================ */
thead {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}

thead th {
  padding: 0.2rem 0.4rem;
  text-align: left;
  font-weight: var(--font-weight-semibold);
  color: var(--text-secondary);
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
  border-bottom: 1px solid var(--border-color-dark);
  user-select: none;
}

/* First column (checkbox) */
thead th:first-child {
  width: 48px;
  padding: 1rem;
  text-align: center;
}

/* Second column (ID) */
thead th:nth-child(2) {
  width: 100px;
}

/* Last column (Actions) */
thead th:last-child {
  text-align: right;
  width: 180px;
  padding-right: 1rem;
}

/* Sortable header indicator */
thead th:has(svg) {
  cursor: pointer;
  position: relative;
}

thead th:has(svg):hover {
  background: var(--bg-hover);
}

thead th svg {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-left: 0.25rem;
  vertical-align: middle;
  opacity: 0.5;
}

/* ============================================
   Checkboxes
   ============================================ */
thead th input[type="checkbox"],
tbody td input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--primary-color);
  margin: 0;
}

/* Checked row */
tbody tr:has(input[type="checkbox"]:checked) {
  background: var(--bg-selected);
}

/* Checked + hover */
tbody tr:has(input[type="checkbox"]:checked):hover {
  background: var(--bg-selected-hover);
}

/* ============================================
   Table Body
   ============================================ */
tbody tr {
  border-bottom: 1px solid var(--border-color);
  transition: background-color var(--transition-fast);
}

tbody tr:hover {
  background: var(--bg-hover);
}

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

tbody td {
  padding: 0.2rem 0.4rem;
  color: var(--text-primary);
  vertical-align: middle;
}

/* First column (checkbox) */
tbody td:first-child {
  width: 48px;
  text-align: center;
}

/* Second column (ID) */
tbody td:nth-child(2) {
  font-family: var(--font-mono);
  color: var(--text-tertiary);
  /* font-size: var(--text-xs); */
  font-weight: var(--font-weight-semibold);
}

/* Third column (Name/Title) */
tbody td:nth-child(3) {
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
}

/* Last column (Actions) */
tbody td:last-child {
  text-align: right;
  white-space: nowrap;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* ============================================
   Status Badges & Indicators
   ============================================ */
tbody td span[data-badge],
tbody td span[class*="badge"],
tbody td span[class*="status"] {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.25rem 0.75rem;
  border-radius: var(--border-radius-full);
  font-size: var(--text-xs);
  font-weight: var(--font-weight-medium);
  line-height: 1.5;
  white-space: nowrap;
}

/* Auto-detect status from data attributes */
tbody td span[data-badge="success"],
tbody td span:is([class*="verified"], [class*="active"]) {
  background: var(--success-light);
  color: var(--success-color);
}

tbody td span[data-badge="warning"],
tbody td span:is([class*="pending"], [class*="code"]) {
  background: var(--warning-light);
  color: var(--warning-color);
}

tbody td span[data-badge="danger"],
tbody td span:is([class*="error"], [class*="needed"], [class*="duplicate"]) {
  background: var(--danger-light);
  color: var(--danger-color);
}

tbody td span[data-badge="info"] {
  background: var(--info-light);
  color: var(--info-color);
}

/* Status dot indicator */
tbody td span[data-dot="true"]::before,
tbody td span::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

tbody td span:not([data-dot])::before {
  display: none;
}

/* ============================================
   Colored Dots/Indicators
   ============================================ */
tbody td > svg:first-child,
tbody td > span:first-child:has(+ *) {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 0.5rem;
}

/* ============================================
   Progress Bars
   ============================================ */
tbody td:has(progress) {
  min-width: 120px;
}

tbody td progress,
tbody td [role="progressbar"] {
  width: 60px;
  height: 6px;
  border-radius: var(--border-radius-full);
  overflow: hidden;
  appearance: none;
  border: none;
  background: var(--bg-secondary);
  vertical-align: middle;
}

tbody td progress::-webkit-progress-bar {
  background: var(--bg-secondary);
  border-radius: var(--border-radius-full);
}

tbody td progress::-webkit-progress-value {
  background: var(--primary-color);
  border-radius: var(--border-radius-full);
}

tbody td progress::-moz-progress-bar {
  background: var(--primary-color);
  border-radius: var(--border-radius-full);
}

/* Progress with percentage text */
tbody td:has(progress) {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

tbody td progress + span {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
}

/* ============================================
   Rating Stars
   ============================================ */
tbody td:has(svg[fill="currentColor"]) {
  color: var(--warning-color);
}

tbody td svg[fill="currentColor"] {
  width: 16px;
  height: 16px;
  margin-right: 0.25rem;
  vertical-align: middle;
}

/* ============================================
   Action Buttons
   ============================================ */
tbody td:last-child button,
tbody td:last-child a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--border-radius);
  font-size: var(--text-sm);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: all var(--transition-fast);
  text-decoration: none;
  white-space: nowrap;
}

/* Primary action (Edit button) */
tbody td:last-child button,
tbody td:last-child a:first-child {
  background: var(--primary-color);
  color: var(--white);
}

tbody td:last-child button:hover,
tbody td:last-child a:first-child:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

/* Icon-only actions (three dots menu) */
tbody td:last-child button:has(svg:only-child),
tbody td:last-child a:has(svg:only-child) {
  background: transparent;
  color: var(--text-tertiary);
  padding: 0.5rem;
  width: 36px;
  height: 36px;
}

tbody td:last-child button:has(svg:only-child):hover,
tbody td:last-child a:has(svg:only-child):hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  transform: none;
}

tbody td button svg,
tbody td a svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ============================================
   Table Footer / Pagination
   ============================================ */
tfoot,
div:has(> table) + div,
table + div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  background: var(--bg-primary);
  border-top: 1px solid var(--border-color);
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

/* Pagination text */
div:has(> table) + div > div:first-child {
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

/* Pagination buttons container */
div:has(> table) + div > div:last-child {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Pagination buttons */
div:has(> table) + div button,
div:has(> table) + div a {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: all var(--transition-fast);
  text-decoration: none;
  min-width: 36px;
  text-align: center;
}

div:has(> table) + div button:hover,
div:has(> table) + div a:hover {
  background: var(--bg-hover);
  border-color: var(--primary-color);
}

/* Active page */
div:has(> table) + div button[aria-current="page"],
div:has(> table) + div a[aria-current="page"] {
  background: var(--primary-color);
  color: var(--white);
  border-color: var(--primary-color);
}

div:has(> table) + div button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Pagination ellipsis */
div:has(> table) + div span {
  padding: 0 0.5rem;
  color: var(--text-tertiary);
}

/* ============================================
   Table Header Actions (Top Right)
   ============================================ */
div:has(> table)::before,
main > div:has(table) > div:first-child {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.2rem 0.4rem;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-primary);
}

/* ============================================
   Empty State
   ============================================ */
tbody tr:only-child td[colspan] {
  text-align: center;
  padding: var(--space-16);
  color: var(--text-tertiary);
  font-size: var(--text-sm);
}

/* ============================================
   Loading State
   ============================================ */
tbody.loading {
  opacity: 0.5;
  pointer-events: none;
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 1024px) {
  thead th,
  tbody td {
    padding: 0.875rem 1rem;
  }

  /* Hide less important columns */
  thead th:nth-child(n + 7),
  tbody td:nth-child(n + 7) {
    display: none;
  }
}

@media (max-width: 768px) {
  table {
    font-size: var(--text-xs);
  }

  thead th,
  tbody td {
    padding: 0.75rem 0.75rem;
  }

  thead th:first-child,
  tbody td:first-child {
    padding: 0.75rem 0.5rem;
  }

  /* Hide even more columns on mobile */
  thead th:nth-child(n + 5),
  tbody td:nth-child(n + 5) {
    display: none;
  }

  tbody td:last-child {
    flex-direction: column;
  }
}

/* ============================================
   Print Styles
   ============================================ */
@media print {
  div:has(> table) {
    border: none;
    box-shadow: none;
  }

  tbody tr:hover {
    background: transparent;
  }

  tbody td:last-child button,
  tbody td:last-child a {
    display: none;
  }

  div:has(> table) + div {
    display: none;
  }
}
