@layer components {
  /* Base
  /* -------------------------------------------------------------------------- */

  /* Tables sit straight on the page: no box, hairlines between rows, quiet
     headers, compact 32px rows (desktop-design-spec.md: hairlines over
     borders, dense but calm, content not boxed in).

     The wrapper scrolls a table wider than the page, so it never gets clipped
     by the `overflow-x: clip` safety net on the document. A scroll container
     would also trap the sticky header, so the table-scroller controller marks
     a table that fits with data-fits and scrolling is turned off for it.

     It bleeds out by the cell padding, as ghost buttons do, so cell text lines
     up with the text around the table while hover fills and dividers reach
     past it. Never by more than the page's own padding, on narrow screens. */
  .table-scroller {
    --table-cell-padding-inline: 12px;
    --table-bleed: min(var(--table-cell-padding-inline), var(--main-padding));

    inline-size: calc(100% + var(--table-bleed) * 2);
    margin-inline: calc(var(--table-bleed) * -1);
    overflow-x: auto;
    overscroll-behavior-x: contain;

    &[data-fits] {
      overflow-x: visible;
    }
  }

  /* `separate` rather than `collapse`: collapsed borders ignore border-radius,
     which square off the corners of the card the table sits in. */
  .table {
    border-collapse: separate;
    border-spacing: 0;
    inline-size: 100%;
    min-inline-size: var(--table-min-inline-size, 28rem);
  }

  /* Elements
  /* -------------------------------------------------------------------------- */

  .table__row {
    @media (any-hover: hover) {
      .table__body &:hover {
        background-color: var(--color-surface-subtle);
      }
    }
  }

  /* 6px + 20px line + 6px: 32px rows on whole pixels. The divider counts
     toward the height, so it comes out of the bottom padding. */
  .table__header,
  .table__cell {
    border-block-end: var(--border);
    font-size: var(--text-small);
    line-height: 20px;
    padding:
      var(--table-cell-padding,
        6px var(--table-cell-padding-inline, 12px) calc(6px - var(--hairline)));
    text-align: start;
  }

  /* Supporting columns in secondary ink; the row's own column in primary
     (below), and empty values a step quieter still */
  .table__cell {
    color: var(--color-ink-muted);

    .txt-subtle {
      color: var(--color-ink-placeholder);
    }

    /* One text line tall, so a status doesn't stretch its row */
    .badge {
      line-height: 16px;
      padding-block: 2px;
      /* Top, not baseline or middle: either would stretch the line box */
      vertical-align: top;
    }
  }

  /* The column the row is about: one marked primary, or else the first that
     truncates, or else the first */
  .table__cell--primary,
  .table__row:not(:has(> .table__cell--primary)) > :nth-child(1 of .table__cell--truncate),
  .table__row:not(:has(> .table__cell--primary, > .table__cell--truncate)) > .table__cell:first-child {
    color: var(--color-ink);
  }

  /* Column labels as section labels: small, secondary, no fill. They stick to
     the top of the window while a long table scrolls, on the page's colour so
     rows pass under them. Headers never wrap: a label broken mid-phrase is a
     layout failure. The table scrolls inside .table-scroller instead, and the
     column chosen to flex (see .table__cell--truncate) absorbs any squeeze. */
  .table__header {
    background-color: var(--table-header-background, var(--color-canvas));
    color: var(--color-ink-muted);
    font-size: var(--text-x-small);
    font-weight: 500;
    inset-block-start: var(--custom-safe-inset-top);
    position: sticky;
    white-space: nowrap;
    z-index: 1;

    .card & {
      --table-header-background: var(--card-background, var(--color-surface));
    }

    .sort-link:hover,
    .sort-link[data-direction] {
      color: var(--color-ink);
    }
  }

  /* A sortable figure's arrow goes before its label, so the label stays
     over the numbers' right edge */
  .table__header--numeric .sort-link {
    flex-direction: row-reverse;
  }

  /* Transparent rather than removed, so the last row keeps its height */
  .table__body .table__row:last-child .table__cell {
    border-block-end-color: transparent;
  }

  /* Variants
  /* -------------------------------------------------------------------------- */

  .table__cell--label {
    inline-size: var(--table-label-size, 8rem);
    white-space: nowrap;
  }

  /* Figures line up by place value so they compare down the column. Put it
     on the header too so the label sits over the numbers. */
  .table__header--numeric,
  .table__cell--numeric {
    font-variant-numeric: tabular-nums;
    text-align: end;
    white-space: nowrap;
  }

  /* A short mark per row, e.g. a tick, centred under its header */
  .table__header--center,
  .table__cell--center {
    text-align: center;
    white-space: nowrap;
  }

  /* A figure beside a header's label, e.g. how many rows a column covers */
  .table__header-count {
    color: var(--color-ink-muted);
    font-size: var(--text-x-small);
    font-variant-numeric: tabular-nums;
    font-weight: 400;
    margin-inline-start: 0.25em;
  }

  /* The one flexible column: it takes whatever width the others leave and
     cuts its text off with an ellipsis, so every row stays one line tall.
     max-inline-size: 0 stops the text's own width from sizing the column.
     Give the cell a title attribute with the full text. */
  .table__cell--truncate {
    inline-size: 100%;
    max-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* A second long text column beside the one that truncates: it hugs short
     values but stops at a set width and cuts the rest off with an ellipsis,
     so it can't push the table wider or squeeze the flexible column. Give the
     cell a title attribute with the full text. */
  .table__cell--clip {
    max-inline-size: var(--table-clip-size, 16rem);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The column hugs its widest content, leaving free table width to other
     columns. Used so per-row content (e.g. the genre meter) sizes to the
     longest label rather than the stretched column. */
  .table__cell--fit {
    inline-size: 1%;
    white-space: nowrap;
  }

  /* A row that is one link: a click anywhere follows its .table__link (the
     row-link controller on the table body), apart from on the row's own links
     and buttons. Not a CSS stretched link: that needs position: relative on
     the row, which mobile Safari ignores on table rows. */
  .table__row--linked {
    cursor: pointer;
  }

  .table__link {
    color: inherit;

    @media (any-hover: hover) {
      .table__row--linked:hover & {
        color: var(--color-link);
      }
    }
  }

  /* A row whose cells hold form controls. Until the row is in edit mode the
     controls lose their chrome and read as plain cell text; the owning
     controller makes the cells inert so they can't be focused or changed. */
  .table__row--editable {
    .input {
      --input-font-size: var(--text-small);
    }

    &:not(.table__row--editing) {
      .input {
        --input-background: transparent;
        --input-border-color: transparent;
        --input-padding: 0.5em 0;
      }

      .select__chevron,
      ::-webkit-calendar-picker-indicator {
        display: none;
      }
    }
  }

  /* Buttons pinned to the end of the row. The cell takes no more width than
     its contents, and text-align: end works because buttons are inline-flex. */
  .table__cell--actions,
  .table__header--actions {
    inline-size: 1%;
    padding-block: 2px calc(2px - var(--hairline));
    text-align: end;
    white-space: nowrap;

    /* The spec's small icon buttons, 28px, so the row stays 32px */
    .btn--circle {
      --btn-icon-size: 16px;
      --btn-size: 28px;

      vertical-align: top;
    }

    /* Reveal on hover or keyboard focus, pointer devices only. On touch the
       buttons stay visible because there is no hover to reveal them. */
    @media (any-hover: hover) {
      .btn {
        opacity: 0;
        transition: opacity var(--duration-fast) var(--ease-out-expo);
      }

      .table__row:hover & .btn,
      .table__row:focus-within & .btn {
        opacity: 1;
      }
    }
  }
}
