/* ==========================================================================
   BioTrack Pro — Tables
   DataTable shell, sortable headers, row actions, mobile card transformation.
   ========================================================================== */

.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-sm);
}

.table thead th {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--color-surface-sunk);
  padding: var(--space-3) var(--space-4);
  text-align: left;
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--color-text-muted);
  white-space: nowrap;
  border-bottom: 1px solid var(--color-border);
  user-select: none;
}

.table thead th:first-child { padding-left: var(--space-5); }
.table thead th:last-child { padding-right: var(--space-5); }

.table th.is-sortable {
  cursor: pointer;
  transition: var(--transition-colors);
}

.table th.is-sortable:hover { color: var(--color-text-strong); background: var(--color-surface-hover); }

.table th.is-sorted { color: var(--color-primary); }

.th-inner {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

.th-sort-icon {
  width: 12px;
  height: 12px;
  opacity: 0.4;
  transition: opacity var(--duration-fast), transform var(--duration-fast);
}

.table th.is-sorted .th-sort-icon { opacity: 1; }
.table th[data-dir="desc"] .th-sort-icon { transform: rotate(180deg); }

.table tbody td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border-subtle);
  color: var(--color-text);
  vertical-align: middle;
}

.table tbody td:first-child { padding-left: var(--space-5); }
.table tbody td:last-child { padding-right: var(--space-5); }

.table tbody tr {
  transition: background-color var(--duration-fast) var(--ease-in-out);
}

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

.table tbody tr.is-clickable { cursor: pointer; }
.table tbody tr:hover { background: var(--color-surface-hover); }

.table tbody tr.is-selected { background: var(--color-primary-soft); }

.table tbody tr.is-critical {
  background: var(--color-critical-bg);
}

.table tbody tr.is-critical:hover { filter: brightness(0.98); }

/* Column alignment helpers */
.table .col-num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.table .col-center { text-align: center; }
.table .col-actions { text-align: right; width: 1%; white-space: nowrap; }
.table .col-check { width: 1%; padding-right: 0; }
.table .col-tight { width: 1%; white-space: nowrap; }

/* Cell content patterns */
.cell-primary {
  font-weight: var(--weight-medium);
  color: var(--color-text-strong);
}

.cell-sub {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: 1px;
}

.cell-stack {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.cell-person {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.cell-code {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
}

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

/* Table toolbar */
.table-toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  flex-wrap: wrap;
}

.table-toolbar__search { flex: 1 1 220px; min-width: 180px; max-width: 340px; }

.table-toolbar__filters {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.table-toolbar__count {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
}

/* Selection bar */
.table-selection {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-5);
  background: var(--color-primary-soft);
  border-bottom: 1px solid var(--color-primary-border);
  font-size: var(--text-sm);
  color: var(--color-primary);
  font-weight: var(--weight-medium);
  flex-wrap: wrap;
}

/* Pagination */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--color-border);
  flex-wrap: wrap;
}

.pagination__info {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
}

.pagination__controls {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.pagination__btn {
  min-width: 30px;
  height: 30px;
  padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text-muted);
  display: grid;
  place-items: center;
  transition: var(--transition-colors);
}

.pagination__btn:hover:not(:disabled) {
  background: var(--color-surface-hover);
  color: var(--color-text-strong);
}

.pagination__btn:disabled { opacity: 0.35; cursor: not-allowed; }

.pagination__btn.is-active {
  background: var(--color-primary);
  color: var(--color-on-primary);
  font-weight: var(--weight-semibold);
}

.pagination__ellipsis {
  padding: 0 var(--space-1);
  color: var(--color-text-subtle);
  user-select: none;
}

.pagination__size {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.pagination__size select {
  height: 28px;
  padding: 0 var(--space-5) 0 var(--space-2);
  font-size: var(--text-xs);
  min-height: 0;
}

/* ==========================================================================
   Mobile card transformation
   Applied by DataTable when the viewport is below --bp-md.
   ========================================================================== */

.table-cards {
  display: flex;
  flex-direction: column;
}

.table-card {
  padding: var(--space-4) var(--space-4);
  border-bottom: 1px solid var(--color-border-subtle);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  transition: var(--transition-colors);
  width: 100%;
  text-align: left;
}

.table-card:last-child { border-bottom: none; }
.table-card:active { background: var(--color-surface-hover); }

.table-card__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}

.table-card__title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--color-text-strong);
  line-height: var(--leading-snug);
}

.table-card__sub {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: 2px;
}

.table-card__fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: var(--space-3);
}

.table-card__field-label {
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--color-text-subtle);
  margin-bottom: 2px;
}

.table-card__field-value {
  font-size: var(--text-sm);
  color: var(--color-text);
  word-break: break-word;
}

.table-card__foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  padding-top: var(--space-1);
}

/* ==========================================================================
   Compact / dense table variant (result entry, invoice items)
   ========================================================================== */

.table--dense thead th { padding: var(--space-2) var(--space-3); }
.table--dense tbody td { padding: var(--space-2) var(--space-3); }
.table--dense thead th:first-child,
.table--dense tbody td:first-child { padding-left: var(--space-4); }
.table--dense thead th:last-child,
.table--dense tbody td:last-child { padding-right: var(--space-4); }

.table--bordered tbody td { border-right: 1px solid var(--color-border-subtle); }
.table--bordered tbody td:last-child { border-right: none; }

/* Totals row */
.table tfoot td {
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface-sunk);
  border-top: 1px solid var(--color-border);
  font-weight: var(--weight-semibold);
  color: var(--color-text-strong);
}

.table tfoot td:first-child { padding-left: var(--space-5); }
.table tfoot td:last-child { padding-right: var(--space-5); }

/* Sticky first column, used on wide horizontally-scrolled tables */
.table--sticky-first thead th:first-child,
.table--sticky-first tbody td:first-child {
  position: sticky;
  left: 0;
  background: var(--color-surface);
  z-index: 1;
}

.table--sticky-first thead th:first-child {
  z-index: calc(var(--z-sticky) + 1);
  background: var(--color-surface-sunk);
}

.table--sticky-first tbody tr:hover td:first-child { background: var(--color-surface-hover); }

/* Totals summary block (billing) */
.totals {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.totals__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  font-size: var(--text-sm);
  color: var(--color-text);
}

.totals__row--em {
  padding-top: var(--space-3);
  margin-top: var(--space-1);
  border-top: 1px solid var(--color-border);
  font-size: var(--text-base);
  font-weight: var(--weight-semibold);
  color: var(--color-text-strong);
}

.totals__label { color: var(--color-text-muted); }
.totals__row--em .totals__label { color: var(--color-text-strong); }

.totals__value {
  font-variant-numeric: tabular-nums;
  font-weight: var(--weight-medium);
  white-space: nowrap;
}

.totals__value--danger { color: var(--color-danger); }
.totals__value--success { color: var(--color-success); }
