/* DataTable — compact back-office table. Tokens only. Rows and cells show an inset focus ring so
   the card's rounded, scrolling edge never clips it. The wrapper is the card: white surface,
   rounded corners and a soft shadow, with the horizontal scroll inside it. */
.wi-data-table {
  /* The containing block of what cells put absolutely positioned (a .wi-sr-only label in a
     cell), so it stays in the scroll instead of widening the page. Popovers portal out. */
  position: relative;
  max-width: 100%;
  overflow-x: auto;
  background-color: var(--background-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--wi-card-shadow, var(--shadow-sm));
}
.wi-data-table,
.wi-data-table__table,
.wi-data-table__column,
.wi-data-table__row,
.wi-data-table__cell {
  outline: none;
}

.wi-data-table__table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--font-size-sm);
  line-height: var(--font-line-height-sm);
  color: var(--text-primary);
}

.wi-data-table__column {
  padding: var(--space-3) var(--space-4);
  border-bottom: var(--border-1) solid var(--border-default);
  font-size: var(--font-size-xs);
  line-height: var(--font-line-height-xs);
  font-weight: var(--font-weight-semibold);
  letter-spacing: var(--font-letter-spacing-wide);
  text-transform: uppercase;
  text-align: start;
  white-space: nowrap;
  color: var(--text-secondary);
}

.wi-data-table__cell {
  /* Content height = an sm control's (the field density hook), so a Select fits: 32px + the
     padding = 56px rows, plus the 1px divider. content-box so an app-wide border-box reset can't
     shrink them. */
  box-sizing: content-box;
  height: var(--wi-field-height-sm, var(--size-control-sm));
  padding: var(--space-3) var(--space-4);
  border-bottom: var(--border-1) solid var(--border-subtle);
  vertical-align: middle;
  white-space: nowrap;
}
.wi-data-table__cell-content {
  display: contents;
}
/* More room at the table's outer edges than between its columns, so text never sits close to the card's border; the
   footer lines up with them. */
.wi-data-table__column:first-child:not([data-sortable]),
.wi-data-table__cell:first-child,
.wi-data-table__column:first-child > .wi-data-table__sort {
  padding-inline-start: var(--space-6);
}
.wi-data-table__column:last-child:not([data-sortable]),
.wi-data-table__cell:last-child,
.wi-data-table__column:last-child > .wi-data-table__sort {
  padding-inline-end: var(--space-6);
}
.wi-list-footer.wi-data-table__footer {
  padding-inline: var(--space-6);
}
.wi-data-table__column[data-align='end'],
.wi-data-table__cell[data-align='end'] {
  text-align: end;
}
.wi-data-table__row:last-child > .wi-data-table__cell {
  border-bottom: 0;
}
/* A band: its name and what it holds, on the sunken surface, over the rows it names. */
.wi-data-table__band-cell {
  padding: var(--space-2) var(--space-6);
  border-bottom: var(--border-1) solid var(--border-subtle);
  background-color: var(--background-sunken);
  font-size: var(--font-size-xs);
  line-height: var(--font-line-height-xs);
  font-weight: var(--font-weight-semibold);
  text-align: start;
  color: var(--text-secondary);
}

.wi-data-table__table[data-focus-visible],
.wi-data-table__row[data-focus-visible],
.wi-data-table__column[data-focus-visible],
.wi-data-table__cell[data-focus-visible] {
  box-shadow: inset 0 0 0 var(--border-2) var(--border-focus);
}

/* A row whose record link is stretched (`record_link(stretched: true)`) opens the record from anywhere on it: its
   positioned box is what the link covers (utilities.css), and it shows the link's hover, press and focus. */
.wi-data-table__row:has(.wi-record-link--stretched) {
  position: relative;
}
.wi-data-table__row:has(.wi-record-link--stretched:is([data-hovered], [data-pressed])) {
  background-color: var(--background-sunken);
}
.wi-data-table__row:has(.wi-record-link--stretched[data-focus-visible]) {
  box-shadow: inset 0 0 0 var(--border-2) var(--border-focus);
}

/* Centres the slot; the caller's content (an EmptyState, text) controls its own look. */
.wi-data-table__empty {
  display: grid;
  justify-items: center;
  padding: var(--space-6) var(--space-4);
}

/* Loading keeps full text contrast: a sunken body and a bar along the top edge, drawn as an inset
   shadow on the card so the table doesn't move. Until the rows arrive, the empty state is hidden
   (also from assistive tech): no "no results" before any data. */
.wi-data-table[aria-busy='true'] {
  box-shadow: inset 0 var(--border-2) 0 var(--border-focus), var(--shadow-sm);
}
.wi-data-table[aria-busy='true'] .wi-data-table__empty {
  visibility: hidden;
}
.wi-data-table[aria-busy='true'] .wi-data-table__row {
  background-color: var(--background-sunken);
}

/* Sorting through the URL: the label is a link; the arrow shows the direction (decorative). */
.wi-data-table__column[data-sortable] {
  padding: 0;
}
/* The link fills the header cell (the cell's padding moves onto it), so a press anywhere in the
   header follows it. */
.wi-data-table__sort {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-3) var(--space-4);
  color: inherit;
  text-decoration: none;
  outline: none;
}
.wi-data-table__sort:hover,
.wi-data-table__column[aria-sort='ascending'] .wi-data-table__sort,
.wi-data-table__column[aria-sort='descending'] .wi-data-table__sort {
  color: var(--text-primary);
}
.wi-data-table__sort-icon,
.wi-data-table__error-icon {
  flex: none;
  width: var(--size-icon-xs);
  height: var(--size-icon-xs);
}
.wi-data-table__column[aria-sort='none'] .wi-data-table__sort-icon {
  color: var(--text-tertiary);
}
.wi-data-table__sort:hover .wi-data-table__sort-icon {
  color: inherit;
}
.wi-data-table__column[data-align='end'] .wi-data-table__sort {
  justify-content: flex-end;
}
/* The link fills the cell, so its ring is inset like the cell's own. */
.wi-data-table__sort:focus-visible {
  box-shadow: inset 0 0 0 var(--border-2) var(--border-focus);
}

/* Error rows: tinted cells (there is no row hover, so no hover tint), and they keep this tint
   while loading (the sunken loading colour is on the row, under the cells). The cell with the
   problem carries the text. The cell backgrounds are opaque and paint over the row's own inset
   focus ring, so on an error row the ring is drawn on the cells: top and bottom on each, plus the
   left edge of the first and the right edge of the last. */
.wi-data-table__row[data-status='error'] > .wi-data-table__cell {
  background-color: var(--feedback-error-bg);
}
.wi-data-table__row[data-status='error'][data-focus-visible] {
  box-shadow: none;
}
.wi-data-table__row[data-status='error'][data-focus-visible] > .wi-data-table__cell {
  box-shadow:
    inset 0 var(--border-2) 0 var(--border-focus),
    inset 0 calc(-1 * var(--border-2)) 0 var(--border-focus);
}
.wi-data-table__row[data-status='error'][data-focus-visible] > .wi-data-table__cell:first-child {
  box-shadow:
    inset 0 var(--border-2) 0 var(--border-focus),
    inset 0 calc(-1 * var(--border-2)) 0 var(--border-focus),
    inset var(--border-2) 0 0 var(--border-focus);
}
.wi-data-table__row[data-status='error'][data-focus-visible] > .wi-data-table__cell:last-child {
  box-shadow:
    inset 0 var(--border-2) 0 var(--border-focus),
    inset 0 calc(-1 * var(--border-2)) 0 var(--border-focus),
    inset calc(-1 * var(--border-2)) 0 0 var(--border-focus);
}
.wi-data-table__error-text {
  display: inline-flex;
  align-items: flex-start;
  gap: var(--space-1);
  color: var(--feedback-error-fg);
  font-weight: var(--font-weight-medium);
}
.wi-data-table__error-icon {
  /* Centre the icon on the first line of text (the line box is shorter than the icon); the text
     may wrap. */
  margin-block: calc((var(--font-line-height-sm) - var(--size-icon-xs)) / 2);
}

/* Selection: a narrow checkbox column; the checkbox is the DS Checkbox box (src/internal/
   CheckboxBox.css), driven by React Aria's data-* states. A selected row gets a subtle tint. The
   error tint is on the cells, so it stays on top, and while loading the sunken colour wins: in
   both the checkbox carries the selection. */
.wi-data-table__select {
  width: var(--size-control-sm);
  padding-inline-end: 0;
}
/* A click on a selectable row selects it, so it shows a pointer, not a text cursor (its text can't
   be selected). Controls in a cell keep their own cursor (text in a field, pointer on a button). */
.wi-data-table--selectable .wi-data-table__row {
  cursor: pointer;
}
.wi-data-table--selectable .wi-data-table__cell :is(input, textarea, [contenteditable]) {
  cursor: text;
}

.wi-data-table__row[data-selected] {
  background-color: var(--background-accent-subtle);
}

/* Cell errors: error text and a line along the bottom edge, which shows on a tinted row whatever the
   cell holds (an underline skips a badge or a flex wrapper). The focus ring replaces the line. */
.wi-data-table__cell[data-status='error'] {
  background-color: var(--feedback-error-bg);
  color: var(--feedback-error-fg);
  font-weight: var(--font-weight-medium);
}
.wi-data-table__cell[data-status='error']:not([data-focus-visible]) {
  box-shadow: inset 0 calc(-1 * var(--border-2)) 0 var(--feedback-error-fg);
}

/* Row details: a row of their own under the open row, one cell across every column. The open row
   drops its divider so the two read as one; the details take the row's error tint (the rule on
   error rows above). */
.wi-data-table__row[data-expanded] > .wi-data-table__cell {
  border-bottom-color: transparent;
}
.wi-data-table__cell.wi-data-table__details-cell {
  height: auto;
  padding-block: 0 var(--space-4);
  white-space: normal;
  vertical-align: top;
}
/* The details are read, not selected: no pointer on a selectable table. */
.wi-data-table--selectable .wi-data-table__details-row {
  cursor: default;
}
/* The details' one cell is both the first and the last: the ring goes all round. */
.wi-data-table__details-row[data-status='error'][data-focus-visible] > .wi-data-table__details-cell {
  box-shadow: inset 0 0 0 var(--border-2) var(--border-focus);
}
/* An error cell in a row without the error status paints over that row's ring: draw its part. */
.wi-data-table__row[data-focus-visible]:not([data-status='error']) > .wi-data-table__cell[data-status='error'] {
  box-shadow:
    inset 0 var(--border-2) 0 var(--border-focus),
    inset 0 calc(-1 * var(--border-2)) 0 var(--border-focus);
}

/* The trigger: a Badge (sm) that is a button, with a chevron that turns when open. A border keeps
   the danger badge visible on the tinted error row it sits in. */
.wi-data-table__details-trigger {
  margin: 0;
  border: var(--border-1) solid var(--border-default);
  cursor: pointer;
  outline: none;
}
.wi-data-table__details-trigger.wi-badge--danger {
  border-color: var(--feedback-error-border);
}
.wi-data-table__details-trigger[data-focus-visible] {
  outline: var(--border-2) solid var(--border-focus);
  outline-offset: var(--border-2);
}
.wi-data-table__details-icon {
  flex: none;
  width: var(--size-icon-xs);
  height: var(--size-icon-xs);
}
.wi-data-table__details-chevron {
  transition: transform 200ms ease;
}
.wi-data-table__details-trigger[aria-expanded='true'] .wi-data-table__details-chevron {
  transform: rotate(90deg);
}
@media (prefers-reduced-motion: reduce) {
  .wi-data-table__details-chevron {
    transition: none;
  }
}

/* A line's problems: the field in a column of its own, the message beside it, one per line. */
.wi-data-table__error-list {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: var(--space-2) var(--space-4);
  margin: 0;
}
.wi-data-table__error-item {
  display: contents;
}
.wi-data-table__error-field {
  font-weight: var(--font-weight-semibold);
  color: var(--feedback-error-fg);
}
.wi-data-table__error-message {
  margin: 0;
  color: var(--text-primary);
}

/* Footer: under the rows, joined to the table (its background, corners and shadow), laid out as any list's footer
 * (ListFooter.css: the range on the left, the pages on the right). Sticky at the left edge: a block as wide as the
 * visible table, it stays in view while a wide table scrolls sideways. */
.wi-data-table__footer {
  position: sticky;
  left: 0;
  border-top: var(--border-1) solid var(--border-default);
}

/* The app's own variant: a table inside a card is part of that card (prototype's card tables), not a card in a card:
   no surface, corners or shadow of its own, its first and last columns on the card's edges. */
.wi-card .wi-data-table {
  background-color: transparent;
  border-radius: 0;
  box-shadow: none;
}

.wi-card .wi-data-table__column:first-child,
.wi-card .wi-data-table__cell:first-child {
  padding-inline-start: 0;
}

.wi-card .wi-data-table__column:last-child,
.wi-card .wi-data-table__cell:last-child {
  padding-inline-end: 0;
}

/* On a phone, every list behaves the same: views.md, "A list on a phone". The chevron shows there only, on a row whose
   record link is stretched over it. */
.wi-data-table__chevron {
  display: none;
}
@media (max-width: 767.98px) {
  /* A band is the table's: the phone's list has its own. */
  .wi-data-table__band {
    display: none;
  }
  .wi-data-table {
    border-block: var(--border-1) solid var(--border-subtle);
    border-radius: 0;
    box-shadow: none;
  }
  /* The head is out of sight, still read: its parts' ARIA roles keep the table a table for a screen reader. */
  .wi-data-table__head {
    position: absolute;
    width: var(--border-1);
    height: var(--border-1);
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
  .wi-data-table__table,
  .wi-data-table__body,
  .wi-data-table__row,
  .wi-data-table__cell {
    display: block;
  }
  .wi-data-table__row[hidden] {
    display: none;
  }
  .wi-data-table__body:not([data-empty]) .wi-data-table__row:not([hidden]) {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
  }
  .wi-data-table__row {
    position: relative;
    padding: var(--space-3) var(--space-4);
    border-bottom: var(--border-1) solid var(--border-subtle);
  }
  /* No line under the last row a phone shows, a closed row's details after it or not. */
  .wi-data-table__row:not([hidden]):not(:has(~ .wi-data-table__row:not([hidden]))) {
    border-bottom: 0;
  }
  .wi-data-table__body:not([data-empty]) .wi-data-table__cell,
  .wi-data-table__cell:first-child,
  .wi-data-table__cell:last-child {
    height: auto;
    padding: 0;
    border-bottom: 0;
    white-space: normal;
  }
  .wi-data-table__cell[role='rowheader'] {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
  }
  .wi-data-table__row-header {
    flex: 1;
    min-width: 0;
  }
  .wi-data-table__row:has(.wi-record-link--stretched) .wi-data-table__chevron {
    display: block;
    flex: none;
    width: var(--size-icon-sm);
    height: var(--size-icon-sm);
    color: var(--text-tertiary);
  }
  .wi-data-table__cell:not([role='rowheader']) {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    text-align: end;
  }
  .wi-data-table__cell:not([role='rowheader']):empty {
    display: none;
  }
  .wi-data-table__cell:not([role='rowheader'])[data-label]::before {
    content: attr(data-label);
    flex: none;
    color: var(--text-tertiary);
    font-size: var(--font-size-xs);
    line-height: var(--font-line-height-xs);
    text-align: start;
  }
  /* A cell whose column shows no label (a row's action) starts on the left. */
  .wi-data-table__cell:not([role='rowheader']):not([data-label]) {
    justify-content: flex-start;
  }
  /* The row header is the card's name; a `list_on_phone` one lays out its own lines. */
  .wi-data-table:not(.wi-data-table--list-on-phone) .wi-data-table__cell[role='rowheader'] {
    font-weight: var(--font-weight-semibold);
  }
  /* An open row and its details read as one card. */
  .wi-data-table__row[data-expanded] {
    border-bottom-color: transparent;
  }
  .wi-data-table__details-row {
    padding-top: 0;
  }
  .wi-data-table__row[data-status='error'] {
    background-color: var(--feedback-error-bg);
  }

  .wi-data-table--list-on-phone .wi-data-table__body:not([data-empty]) .wi-data-table__cell:not([role='rowheader']) {
    display: none;
  }
  .wi-data-table--list-on-phone.wi-data-table--edge-to-edge {
    max-width: none;
    margin-inline: calc(-1 * var(--wi-app-shell-gutter, var(--space-4)));
  }
  /* In a card, the card is the list's edge. */
  .wi-card .wi-data-table {
    border-block: 0;
  }
  .wi-card .wi-data-table__row {
    padding-inline: 0;
  }
}
