/* backoffice/_table_fragment.html — one markup, three presentations chosen
   by the width of #backoffice-table (container queries). None of them ever
   scrolls sideways, and no column is ever hidden:

   - < 36rem    cards (phones): first column as the title, "label: value"
                lines, labelled Modificar / Eliminar buttons; the header row
                becomes "Seleccionar todas…" + "Ordenar por: [Título] …" chips.
   - 36–56rem   compact list: the cards' structure (same semantics) inside
                one frame — checkbox | title | icon buttons on the first line,
                the other fields as "label over value" tiles under it.
   - ≥ 56rem    the table: framed, tinted header band (sticky on tall
                screens), 56px rows, text that wraps instead of overflowing
                (only dates, yes/no and choices never wrap), and quiet icon
                buttons that light up on the hovered/focused/checked row.

   data-kind on each cell (date | bool | choice | token | empty | text, set
   in apps/backoffice/views.py) drives the per-type rules. */

.bo-table {
  container: bo-table / inline-size;
}

/* Positioned so the absolutely positioned screen-reader texts inside (the
   row buttons' full names, "select all") can never widen the page. */
.table-wrap {
  position: relative;
}

.bo-table__table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

.bo-table__table th,
.bo-table__table td {
  padding: 0.9rem 0.75rem;
  border-bottom: 1px solid var(--color-border);
  text-align: left;
  vertical-align: middle;
}

.bo-table__table thead th {
  border-bottom: 2px solid var(--color-text);
  font-weight: 700;
  white-space: nowrap;
}

.bo-table__check {
  width: 3rem;
}

/* "Select all" keeps a real <label>: its text is only visible in cards. */
.bo-select-all {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  cursor: pointer;
}

.bo-select-all__text {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.bo-table__sort {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 2.25rem;
  margin: -0.25rem;
  padding: 0.25rem;
  border: 0;
  border-radius: 0.25rem;
  background: transparent;
  color: var(--color-text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.bo-table__sort:hover {
  color: var(--color-main);
}

.bo-table__sort-icon {
  width: 1rem;
  height: 1rem;
  color: var(--color-muted);
}

th[aria-sort] .bo-table__sort-icon {
  color: var(--color-main);
}

/* Clicking anywhere on a row toggles its checkbox (backoffice.js). */
.bo-table__row {
  cursor: pointer;
}

.bo-check {
  cursor: pointer;
}

/* Per-cell labels and the row buttons' words: cards only. */
.bo-cell__label,
.bo-row-action__text {
  display: none;
}

/* Yes/no and choice values: plain text in cards, badges from 36rem. */
.bo-badge__icon {
  display: none;
}

/* Last column: icon buttons, as narrow as their content. */
.bo-table__table .bo-table__actions {
  width: 1%;
  text-align: right;
  white-space: nowrap;
}

.bo-row-actions {
  display: inline-flex;
  gap: 0.4rem;
}

/* Own white background so they stay legible on hovered/checked rows. */
.bo-row-action {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  width: var(--tap-size);
  height: var(--tap-size);
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-bg);
  color: var(--color-main);
  font: inherit;
  cursor: pointer;
}

.bo-row-action:hover {
  border-color: currentColor;
  background: var(--color-main-soft);
}

.bo-row-action--danger {
  color: var(--color-danger);
}

.bo-row-action--danger:hover {
  background: var(--color-danger-soft);
}

.bo-table__table .bo-table__empty {
  padding: var(--space-3);
  color: var(--color-muted);
  text-align: center;
}

/* --- Cards + compact list: the rows stop being table rows ------------------ */
@container bo-table (max-width: 55.99rem) {
  .bo-table__table,
  .bo-table__table thead,
  .bo-table__table tbody,
  .bo-table__table tr,
  .bo-table__table th,
  .bo-table__table td {
    display: block;
  }

  .bo-table__table th,
  .bo-table__table td,
  .bo-table__table thead th {
    padding: 0;
    border: 0;
  }

  /* Header row: "select all" on its own line, then the sort chips. */
  .bo-table__table thead tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
  }

  .bo-table__table thead .bo-table__check {
    flex-basis: 100%;
    order: -2;
    width: auto;
  }

  .bo-table__table thead tr[data-sort-caption]::before {
    content: attr(data-sort-caption);
    order: -1;
    color: var(--color-muted);
    font-size: 0.875rem;
  }

  .bo-select-all {
    min-height: var(--tap-size);
    font-weight: 400;
  }

  .bo-select-all__text {
    position: static;
    width: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    clip-path: none;
    white-space: normal;
  }

  /* Non-sortable headers and "Acciones": still there for screen readers. */
  .bo-table__table thead .bo-table__th:not(.bo-table__th--sortable),
  .bo-table__table thead .bo-table__actions {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .bo-table__sort {
    min-height: var(--tap-size);
    margin: 0;
    padding: 0 0.85rem;
    border: 1px solid var(--color-border);
    border-radius: 999px;
    background: var(--color-bg);
    font-weight: 600;
  }

  /* The active sort: tint + border + its arrow icon, not colour alone. */
  th[aria-sort] .bo-table__sort {
    border-color: var(--color-main);
    background: var(--color-main-soft);
  }

  .bo-check {
    width: 1.5rem;
    height: 1.5rem;
  }

  .bo-cell--primary .bo-cell__label {
    display: none;
  }

  /* Hover only where there's a real pointer: on touch screens it would
     stick to the last tapped row/card. */
  @media (hover: hover) {
    .bo-table__row:hover {
      background: var(--color-main-soft);
    }
  }

  .bo-table__row:has(.bo-check:checked) {
    background: var(--color-accent-soft);
  }
}

/* --- Cards: the container is narrower than 36rem ----------------------------- */
@container bo-table (max-width: 35.99rem) {
  .bo-table__table thead tr {
    padding-bottom: var(--space-1);
    border-bottom: 2px solid var(--color-text);
  }

  /* Rows: one card each. */
  .bo-table__table tbody {
    display: grid;
    gap: 0.75rem;
    padding-top: 0.75rem;
  }

  .bo-table__table .bo-table__row {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 0.4rem 0.75rem;
    padding: 0.85rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-bg);
  }

  .bo-table__row:has(.bo-check:checked) {
    border-color: var(--color-accent-text);
    box-shadow: inset 4px 0 0 var(--color-accent-text);
  }

  /* Checkbox + title on the first line; the rest use the card's full width. */
  .bo-table__row .bo-table__check {
    grid-row: 1;
    grid-column: 1;
    width: auto;
  }

  .bo-table__row .bo-cell {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0 0.75rem;
  }

  .bo-cell__label {
    display: block;
    flex: 0 0 min(7rem, 35%);
    color: var(--color-muted);
    font-size: 0.875rem;
  }

  .bo-cell__value {
    flex: 1 1 8rem;
    min-width: 0;
    overflow-wrap: anywhere;
  }

  /* First column = the card's title, next to the checkbox. */
  .bo-table__row .bo-cell--primary {
    grid-row: 1;
    grid-column: 2;
    margin-bottom: 0.15rem;
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.35;
  }

  .bo-table__table .bo-table__row .bo-table__actions {
    grid-column: 1 / -1;
    width: auto;
    margin-top: 0.4rem;
    padding-top: 0.6rem;
    border-top: 1px solid var(--color-border);
    white-space: normal;
  }

  .bo-row-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
  }

  .bo-row-action {
    width: auto;
    padding: 0 0.9rem;
  }

  .bo-row-action__text {
    display: inline;
    font-weight: 600;
  }

  .bo-table__table .bo-table__empty {
    padding: var(--space-3) var(--space-1);
  }
}

/* --- Compact list and table (36rem and wider): shared look --------------------- */
@container bo-table (min-width: 36rem) {
  /* One frame around the rows, like the mobile cards and "Mi cuenta". */
  .table-wrap {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-bg);
  }

  .bo-table__table {
    font-size: 0.9375rem;
    line-height: 1.45;
  }

  .bo-table__table .bo-check {
    width: 1.5rem;
    height: 1.5rem;
  }

  /* 44×44 click/tap target around each row's checkbox. */
  .bo-check-target {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--tap-size);
    min-height: var(--tap-size);
    cursor: pointer;
  }

  .bo-select-all {
    justify-content: center;
    min-width: var(--tap-size);
    min-height: var(--tap-size);
  }

  /* The main field (name / title) leads each row. */
  .bo-cell--primary {
    font-weight: 600;
  }

  .bo-cell__value {
    overflow-wrap: anywhere;
  }

  [data-kind="empty"] .bo-cell__value {
    color: var(--color-muted);
  }

  [data-kind="date"] .bo-cell__value {
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }

  /* Yes / No and choices (role): a pill with text — the colour and the
     check mark only reinforce it. */
  .bo-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.05rem 0.6rem;
    border-radius: 999px;
    font-size: 0.8125rem;
    font-weight: 600;
    line-height: 1.5;
    white-space: nowrap;
  }

  .bo-badge__icon {
    display: inline;
    width: 0.95rem;
    height: 0.95rem;
  }

  .bo-badge--yes {
    background: var(--color-success-soft);
    color: var(--color-success);
  }

  .bo-badge--no {
    background: var(--color-bg);
    box-shadow: inset 0 0 0 1px var(--color-border);
    color: var(--color-muted);
  }

  .bo-badge--choice {
    background: var(--color-bg);
    box-shadow: inset 0 0 0 1px var(--color-main-tint);
    color: var(--color-main);
  }

  /* Row buttons: icons only, quiet at rest (the bin in grey, not a column
     of red); they become the card buttons on the hovered, keyboard-focused
     or checked row. Always visible — never revealed on hover. */
  .bo-row-actions {
    gap: 0.25rem;
  }

  .bo-row-action {
    border-color: transparent;
    background: transparent;
  }

  .bo-row-action--danger {
    color: var(--color-muted);
  }

  .bo-table__row:is(:has(:focus-visible), :has(.bo-check:checked)) .bo-row-action {
    border-color: var(--color-border);
    background: var(--color-bg);
  }

  .bo-table__row:is(:has(:focus-visible), :has(.bo-check:checked)) .bo-row-action--danger {
    color: var(--color-danger);
  }

  @media (hover: hover) {
    .bo-table__row:hover .bo-row-action {
      border-color: var(--color-border);
      background: var(--color-bg);
    }

    .bo-table__row:hover .bo-row-action--danger {
      color: var(--color-danger);
    }

    .bo-table__row .bo-row-action:hover {
      border-color: currentColor;
      background: var(--color-main-soft);
    }

    .bo-table__row .bo-row-action--danger:hover {
      background: var(--color-danger-soft);
    }
  }
}

/* --- Compact list: 36rem to 56rem ---------------------------------------------- */
@container bo-table (min-width: 36rem) and (max-width: 55.99rem) {
  /* The header band: "select all" + "Ordenar por" chips, as on phones. */
  .bo-table__table thead tr {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--color-main-tint);
    border-radius: calc(var(--radius) - 1px) calc(var(--radius) - 1px) 0 0;
    background: var(--color-main-wash);
  }

  .bo-table__table thead .bo-table__check {
    margin-left: -0.6rem;
  }

  .bo-select-all {
    justify-content: flex-start;
    padding-left: 0.6rem;
  }

  /* Rows: checkbox at the left and icon buttons at the right of the first
     line (positioned, so the reading/focus order stays checkbox → fields →
     buttons), the title on that line, the other fields as tiles below. */
  .bo-table__table .bo-table__row {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0.4rem 1.25rem;
    padding: 0.6rem 0.75rem 0.75rem 3.6rem;
    border-bottom: 1px solid var(--color-border);
  }

  .bo-table__table tbody .bo-table__row:last-child {
    border-bottom: 0;
    border-radius: 0 0 calc(var(--radius) - 1px) calc(var(--radius) - 1px);
  }

  .bo-table__row:has(.bo-check:checked) {
    box-shadow: inset 4px 0 0 var(--color-accent-text);
  }

  .bo-table__table .bo-table__row > .bo-table__check {
    position: absolute;
    top: 0.6rem;
    left: 0.6rem;
    width: auto;
  }

  .bo-table__table .bo-table__row > .bo-table__actions {
    position: absolute;
    top: 0.6rem;
    right: 0.5rem;
    width: auto;
  }

  .bo-table__row > .bo-cell--primary {
    display: flex;
    flex: 1 0 100%;
    align-items: center;
    min-height: var(--tap-size);
    padding-right: 6.25rem;
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.35;
  }

  .bo-table__row > .bo-cell:not(.bo-cell--primary) {
    display: flex;
    flex: 1 1 9rem;
    flex-direction: column;
    min-width: 0;
  }

  .bo-table__row > .bo-cell[data-kind="token"] {
    flex-basis: 15rem;
  }

  .bo-cell__label {
    display: block;
    color: var(--color-muted);
    font-size: 0.8125rem;
    line-height: 1.35;
  }

  .bo-badge {
    align-self: flex-start;
  }

  .bo-table__table .bo-table__empty {
    padding: var(--space-3) var(--space-2);
  }
}

/* --- Table: 56rem and wider ------------------------------------------------------ */
@container bo-table (min-width: 56rem) {
  .bo-table__table th,
  .bo-table__table td {
    padding: 0.375rem 0.75rem;
  }

  .bo-table__row {
    height: 3.5rem;
  }

  .bo-table__table tbody .bo-table__row:last-child > td {
    border-bottom: 0;
  }

  /* Header band in a pale brand blue (no black rule); labels wrap between
     words instead of pushing the table wider. */
  .bo-table__table thead th {
    padding-block: 0.25rem;
    border-bottom: 1px solid var(--color-main-tint);
    background: var(--color-main-wash);
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.3;
    white-space: normal;
  }

  .bo-table__table thead th:first-child {
    border-top-left-radius: calc(var(--radius) - 1px);
  }

  .bo-table__table thead th:last-child {
    border-top-right-radius: calc(var(--radius) - 1px);
  }

  .bo-table__table .bo-table__check {
    width: var(--tap-size);
    padding-inline: 0.4rem 0;
  }

  /* Sort buttons fill their header cell (44px tall). */
  .bo-table__sort {
    justify-content: flex-start;
    width: calc(100% + 1rem);
    min-height: var(--tap-size);
    margin: 0 -0.5rem;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius);
    font-weight: 600;
    text-align: left;
  }

  .bo-table__sort:hover {
    background: var(--color-bg);
    color: var(--color-main);
  }

  /* The sorted column: blue label, solid arrow and a bar — not colour alone. */
  .bo-table__table thead th[aria-sort] {
    box-shadow: inset 0 -3px 0 var(--color-main);
  }

  th[aria-sort] .bo-table__sort {
    color: var(--color-main);
  }

  /* Short values never wrap and take only the room they need; the rest
     (titles, names, e-mails, DOIs) share what's left and wrap. */
  td:is([data-kind="date"], [data-kind="bool"], [data-kind="choice"]) {
    width: 1%;
    white-space: nowrap;
  }

  .bo-table__table .bo-table__actions {
    padding-inline: 0.25rem 0.5rem;
  }

  /* Row states, painted on the cells so the frame's rounded corners hold. */
  .bo-table__row:has(:focus-visible) > td {
    background: var(--color-main-soft);
  }

  @media (hover: hover) {
    .bo-table__row:hover > td {
      background: var(--color-main-soft);
    }
  }

  .bo-table__row:has(.bo-check:checked) > td {
    background: var(--color-accent-soft);
  }

  .bo-table__row:has(.bo-check:checked) > .bo-table__check {
    box-shadow: inset 4px 0 0 var(--color-accent-text);
  }

  .bo-table__table tbody .bo-table__row:last-child > td:first-child {
    border-bottom-left-radius: calc(var(--radius) - 1px);
  }

  .bo-table__table tbody .bo-table__row:last-child > td:last-child {
    border-bottom-right-radius: calc(var(--radius) - 1px);
  }

  /* Tall enough screens: the header band stays in view while scrolling
     (25/50 rows), right under the fixed navbar; scroll-padding keeps
     focused rows from hiding under either. */
  @media (min-height: 40rem) {
    .bo-table__table thead th {
      position: sticky;
      top: var(--navbar-offset, 0px);
      z-index: 1;
    }
  }
}

@media (min-height: 40rem) {
  html:has(#backoffice-table) {
    scroll-padding-top: calc(var(--navbar-offset, 0px) + 3.5rem);
  }
}

/* --- Pagination ---------------------------------------------------------------- */
.pagination {
  display: flex;
  justify-content: center;
  margin-top: var(--space-2);
}

.pagination__list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.pagination__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--tap-size);
  min-height: var(--tap-size);
  padding: 0 0.75rem;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--color-main);
  font: inherit;
  cursor: pointer;
}

.pagination__btn:hover {
  background: var(--color-main-soft);
}

.pagination__btn--current {
  border-color: var(--color-main);
  background: var(--color-main-soft);
  color: var(--color-text);
  font-weight: 700;
  cursor: default;
}

.pagination__btn--disabled,
.pagination__btn--disabled:hover {
  background: transparent;
  color: var(--color-muted);
  cursor: default;
}

.pagination__ellipsis {
  padding: 0 0.5rem;
  color: var(--color-muted);
}
