/* WIP CSS Grid tables - replaces jQuery DataTables layout.
   Loaded only on pages with javascript.requireGridTable. */

table.datatable-wipolex.wl-grid {
  display: grid;
  /* Per-table tracks set on --wl-cols by wl-grid-sort.js.
     minmax(0, 1fr) tracks shrink safely on small screens. */
  grid-template-columns: var(--wl-cols, 1fr);
  width: 100%;
  /* Configured tracks can exceed the container (e.g. fixed px widths on a narrow
     window); scroll rather than squeezing columns to nothing. */
  overflow-x: auto;
  /* border-box so percentage tracks adding up to 100% fill the table exactly. */
  box-sizing: border-box;
  border-collapse: collapse;
  /* Cells draw only their bottom/end edge (see below), so the table supplies the
     top/start edge — together that is a single 1px hairline everywhere, like v1. */
  border: 0;
  border-top: 1px solid #ccc;
  border-inline-start: 1px solid #ccc;
}

/* Rows contribute their cells directly to the grid. */
table.datatable-wipolex.wl-grid thead,
table.datatable-wipolex.wl-grid tbody,
table.datatable-wipolex.wl-grid tr {
  display: contents;
}

table.datatable-wipolex.wl-grid th,
table.datatable-wipolex.wl-grid td {
  /* !important: legacy per-column width rules (members-profile.css nth-child 40%/18%/10%)
     must not distort cells inside grid tracks — --wl-cols is the single width source. */
  width: auto !important;
  /* border-box so cell padding/borders stay inside the track width. */
  box-sizing: border-box;
  /* break-word (not anywhere) keeps a cell's min-content at its longest word, so an
     `auto` track can never collapse to one character per line. */
  overflow-wrap: break-word;
  padding: 8px 10px;
  text-align: start;
  border: 0;
  border-bottom: 1px solid #ccc;
  border-inline-end: 1px solid #ccc;
  background: #fff;
}

/* border-collapse does not apply to a grid, so adjacent full borders would double up.
   These selectors out-specify the legacy `border: 1px ... !important` rules in
   wipolex-custom.css and members-profile.css to keep every line a single 1px. */
wu-page-section f-facet table.datatable.datatable-wipolex.wl-grid > thead > tr > th,
wu-page-section f-facet table.datatable.datatable-wipolex.wl-grid > tbody > tr > td,
table.datatable.datatable-wipolex.wl-grid > thead > tr > th,
table.datatable.datatable-wipolex.wl-grid > tbody > tr > td {
  border: 0 !important;
  border-bottom: 1px solid #ccc !important;
  border-inline-end: 1px solid #ccc !important;
}

/* Striping (tr backgrounds do not paint with display: contents).
   Even rows shaded with the legacy .datatable #efefef so v1/v2 match. */
table.datatable-wipolex.wl-grid tbody tr:nth-child(even) td {
  background: #efefef;
}

/* Row hover (must come after striping to win on even rows). */
table.datatable-wipolex.wl-grid tbody tr:hover td {
  background: #e6e6e6;
}

/* Sticky header (replaces DataTables FixedHeader plugin). */
table.datatable-wipolex.wl-grid.wl-grid--sticky thead th {
  position: sticky;
  top: 0;
  z-index: 2;
}

/* Sortable headers (replaces DataTables ordering controls). */
table.datatable-wipolex.wl-grid thead th.wl-sortable {
  cursor: pointer;
  user-select: none;
  padding-inline-end: 22px;
  position: relative;
}

table.datatable-wipolex.wl-grid thead th.wl-sortable::after {
  content: '\2195'; /* up-down arrow */
  position: absolute;
  inset-inline-end: 6px;
  top: 8px; /* pin to the corner: without it the icon follows the wrapped header text */
  opacity: 0.3;
}

table.datatable-wipolex.wl-grid thead th.wl-sortable[aria-sort='ascending']::after {
  content: '\2191';
  opacity: 1;
}

table.datatable-wipolex.wl-grid thead th.wl-sortable[aria-sort='descending']::after {
  content: '\2193';
  opacity: 1;
}

/* Neutralize legacy .datatable rules from wipolex-custom.css that break the
   grid layout (display: table-cell/block hacks, nowrap, mobile card mode). */
table.datatable-wipolex.wl-grid {
  white-space: normal;
}

table.datatable-wipolex.wl-grid > thead > tr > th {
  display: block !important;
  font-weight: normal; /* browser default bold on th is heavier than v1 headers */
  background: #f2f2f2;
}

table.datatable-wipolex.wl-grid tr td {
  display: block !important;
  width: auto !important;
}

table.datatable-wipolex.wl-grid tr td .mobile-only {
  display: none !important; /* header row is always visible in grid mode */
}

/* Small screens: same table (header + lines), readable column minimums,
   horizontal scroll instead of squeezing. 960px matches the legacy
   .datatable breakpoint in wipolex-custom.css. */
@media (max-width: 959px) {
  table.datatable-wipolex.wl-grid {
    grid-template-columns: var(--wl-cols-mobile, var(--wl-cols, 1fr));
  }
}

@media (max-width: 767px) {
  table.datatable-wipolex.wl-grid th,
  table.datatable-wipolex.wl-grid td {
    padding: 6px 6px;
    font-size: 0.9em;
  }
}
