/* ===========================================
   Tables (design §D.5)

   .table: a hairline data table with a sticky head that sits just below the
   shell's top bar (--app-shell-offset, from application.css).
   .table-responsive: a keyboard-scrollable wrapper for wide data tables, so
   the document itself never scrolls sideways. Give it role="region",
   tabindex="0" and an accessible name.
   .table--stack: list-like tables become labelled rows below 768px; each
   cell needs data-label. The label sits in a fixed 7rem gutter, so a cell
   may hold any mix of text and elements, which keep their inline flow.
   =========================================== */

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

.table th {
  position: sticky;
  top: var(--app-shell-offset, 0px);
  z-index: calc(var(--z-sticky) - 1);
  padding: var(--spacing-sm) var(--spacing-lg);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-background-subtle);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  text-align: left;
  white-space: nowrap;
}

.table td {
  padding: var(--spacing-md) var(--spacing-lg);
  border-bottom: 1px solid var(--color-border-subtle);
  color: var(--color-text);
  vertical-align: top;
}

.table tr:has(> td a[href]):hover > td {
  background: var(--color-surface-hover);
}

.table__num {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.table-responsive {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

/* The wrapper is the scroll container, so a sticky head would stick to it,
   not the page. */
.table-responsive th {
  position: static;
}

/* Stacked rows below 768px */
.table--stack thead {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.table--stack,
.table--stack tbody,
.table--stack tr {
  display: block;
}

.table--stack tr {
  padding: var(--spacing-sm) 0;
  border-bottom: 1px solid var(--color-border-subtle);
}

.table-responsive .table--stack tr {
  padding-inline: var(--spacing-lg);
}

.table--stack td {
  display: block;
  position: relative;
  min-width: 0;
  padding: var(--spacing-xs) 0 var(--spacing-xs) calc(7rem + var(--spacing-sm));
  border-bottom: 0;
  overflow-wrap: anywhere;
}

.table--stack td:not([data-label]) {
  padding-left: 0;
}

/* A stacked number reads from the start, beside its label. */
.table--stack td.table__num {
  text-align: start;
}

.table--stack td::before {
  content: attr(data-label);
  position: absolute;
  top: var(--spacing-xs);
  left: 0;
  width: 7rem;
  color: var(--color-text-muted);
  text-align: start;
  font-size: var(--text-xs);
  line-height: var(--leading-normal);
}

@media (min-width: 768px) {
  .table--stack {
    display: table;
  }

  .table--stack thead {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: normal;
    display: table-header-group;
  }

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

  .table--stack tr,
  .table-responsive .table--stack tr {
    display: table-row;
    padding: 0;
    border-bottom: 0;
  }

  .table--stack td,
  .table--stack td:not([data-label]) {
    display: table-cell;
    position: static;
    padding: var(--spacing-md) var(--spacing-lg);
    border-bottom: 1px solid var(--color-border-subtle);
  }

  .table--stack td::before {
    content: none;
  }

  .table--stack td.table__num {
    text-align: right;
  }
}
