/**
 * CardioTrack — Componente: tablas de datos
 * ---------------------------------------------------------------------------
 * Tablas administrativas: densas, legibles y navegables con teclado. Se
 * mantienen como tablas en todos los tamaños (scroll horizontal en el
 * contenedor); solo una vista puede optar por su propio modo tarjeta.
 *
 * Uso:
 *   <div class="ct-table-wrap" role="region" tabindex="0" aria-label="…">
 *     <table class="table ct-table">
 *       <thead><tr><th scope="col">…</th><th scope="col" class="is-num">…</th></tr></thead>
 *       <tbody>…<td class="is-num">…</td>…</tbody>
 *     </table>
 *   </div>
 *
 * Se carga junto a components/forms.css, antes de cardiotrack.css.
 * Especificidad base (0,1,1, como Bootstrap) gracias a :where(): cualquier
 * regla propia de una vista (p. ej. el modo tarjeta móvil de .ia-table) gana
 * sin !important.
 */

.ct-table {
  --bs-table-bg: transparent;
  --bs-table-hover-bg: var(--color-primary-50);
  --bs-table-striped-bg: transparent;
  margin-bottom: 0;
  color: var(--color-text-primary);
  font-size: var(--text-sm);
  line-height: var(--leading-snug);
  font-variant-numeric: tabular-nums;
}

/* --- Encabezado ----------------------------------------------------------
   Sentence case, sin tracking: identifica la columna sin competir con los
   datos. Queda fijo al desplazar dentro de un .ct-table-wrap con altura. */

.ct-table :where(thead) th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-border-strong);
  background: var(--color-surface-soft);
  color: var(--color-text-secondary);
  font-size: var(--text-xs);
  font-weight: var(--font-weight-semibold);
  letter-spacing: normal;
  text-transform: none;
  white-space: nowrap;
  vertical-align: bottom;
}

/* --- Celdas -------------------------------------------------------------- */

.ct-table :where(tbody) :is(td, th) {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

.ct-table :where(tbody) th {
  font-weight: var(--font-weight-semibold);
}

.ct-table :where(tbody) tr:last-child > * {
  border-bottom: 0;
}

/* Hover y foco dentro de la fila: misma señal, sin animar el layout */
.ct-table :where(tbody) tr:is(:hover, :focus-within) > * {
  background-color: var(--color-primary-50);
}

/* --- Alineación de cifras ------------------------------------------------ */

.ct-table .is-num {
  text-align: right;
  white-space: nowrap;
}

/* --- Pie: totales -------------------------------------------------------- */

.ct-table :where(tfoot) :is(td, th) {
  padding: var(--space-2) var(--space-3);
  border-top: 1px solid var(--color-border-strong);
  border-bottom: 0;
  background: var(--color-surface-soft);
  font-weight: var(--font-weight-semibold);
}

/* --- Densidad ------------------------------------------------------------ */

.ct-table--compact :where(thead, tbody, tfoot) :is(td, th) {
  padding: var(--space-1) var(--space-2);
}

/* --- Estado vacío dentro de la tabla ------------------------------------ */

.ct-table .ct-table-empty {
  padding: var(--space-7) var(--space-4);
  color: var(--color-text-secondary);
  text-align: center;
}

/* --- Contenedor con scroll ---------------------------------------------
   role="region" + tabindex="0": permite desplazar con el teclado una tabla
   más ancha que la pantalla (WCAG 2.1.1). */

.ct-table-wrap {
  max-width: 100%;
  overflow-x: auto;
  /* overflow-x: auto fuerza el eje Y a auto y un subpíxel mostraba una barra
     vertical. Para encabezado fijo: max-height + overflow-y: auto. */
  overflow-y: hidden;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

.ct-table-wrap:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Sin borde propio cuando la tabla ya vive dentro de una card */
.ct-table-wrap--flush {
  border: 0;
  border-radius: 0;
}

/* --- Encabezado fijo ---------------------------------------------------------
   Con altura máxima el cuerpo desplaza y el encabezado (ya sticky) se queda. */

.ct-table-wrap--sticky {
  max-height: 480px;
  overflow-y: auto;
}

/* El alto fraccionario de la tabla hacía que scrollHeight superara en 1px a
   clientHeight y apareciera una barra vertical con pocas filas. */
.ct-table-wrap--sticky {
  padding-bottom: 1px;
}

/* --- Orden por columna ---------------------------------------------------------
   <th aria-sort="none|ascending|descending"><button class="ct-sort">…</button></th>
   El estado lo comunica aria-sort; el icono solo lo refuerza. */

.ct-sort {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.ct-sort:hover,
th[aria-sort="ascending"] .ct-sort,
th[aria-sort="descending"] .ct-sort {
  color: var(--color-text-primary);
}

.ct-sort:focus-visible {
  outline: 0;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

.ct-sort .bi {
  font-size: 0.75em;
  opacity: 0.4;
}

th[aria-sort="ascending"] .ct-sort .bi,
th[aria-sort="descending"] .ct-sort .bi {
  opacity: 1;
}

th[aria-sort="ascending"] .ct-sort .bi::before {
  content: "\f148"; /* bi-arrow-up */
}

th[aria-sort="descending"] .ct-sort .bi::before {
  content: "\f128"; /* bi-arrow-down */
}

/* --- Chips de filtros activos -------------------------------------------------- */

.ct-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.ct-chips[hidden] {
  display: none;
}

.ct-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 26px;
  padding: 0 var(--space-1) 0 var(--space-3);
  border-radius: var(--radius-pill);
  background: var(--color-primary-soft);
  color: var(--color-primary);
  font-size: var(--text-xs);
  font-weight: var(--font-weight-semibold);
}

.ct-chip button {
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 50% !important;
  background: transparent;
  color: inherit;
  font-size: var(--text-md);
  line-height: 1;
}

.ct-chip button:hover {
  background: rgba(var(--color-primary-rgb), 0.16);
}

.ct-chip button:focus-visible {
  outline: 0;
  box-shadow: var(--focus-ring);
}

/* --- Filas por página ------------------------------------------------------------ */

.ct-page-size {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
  color: var(--color-text-secondary);
  font-size: var(--text-sm);
  white-space: nowrap;
}

.ct-page-size select {
  width: auto;
  min-width: 64px;
}

/* --- Modo tarjeta en móvil -----------------------------------------------------------
   Cada fila se apila usando data-label como rótulo. Una celda sin data-label
   (la primera, que identifica la fila) ocupa todo el ancho sin rótulo. */

@media (max-width: 767.98px) {
  .ct-table--cards thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  .ct-table--cards,
  .ct-table--cards tbody,
  .ct-table--cards tr,
  .ct-table--cards td {
    display: block;
  }

  .ct-table--cards tbody tr {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border);
  }

  .ct-table--cards tbody td {
    display: flex;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-1) 0;
    border: 0;
    text-align: right;
  }

  .ct-table--cards tbody td[data-label]::before {
    content: attr(data-label);
    flex: none;
    color: var(--color-text-secondary);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-semibold);
    text-align: left;
  }

  .ct-table--cards tbody td:not([data-label]) {
    display: block;
    text-align: left;
  }
}
