/*
 * table.css — the one table. Below a region of 560 every row becomes a list row.
 *
 * <div class="app-table" aria-busy>
 *   <table class="app-table-grid" role="table">
 *     <caption class="app-table-caption">Aufträge</caption>
 *     <thead role="rowgroup">
 *       <tr role="row">
 *         <th class="app-table-select" role="columnheader" scope="col">[checkbox: select all]</th>
 *         <th class="app-table-header" role="columnheader" scope="col" data-align data-priority data-width aria-sort>
 *           <button class="app-table-sort" type="button">Nummer [icon]</button>   (or plain text)
 *         </th>
 *       </tr>
 *     </thead>
 *     <tbody role="rowgroup">
 *       <tr class="app-table-row" role="row" data-selected data-loading>
 *         <td class="app-table-select" role="cell">[checkbox]</td>
 *         <td class="app-table-cell" role="cell" data-align data-priority data-role="title | trailing">
 *           <a class="app-table-link" href>A-2026-0142</a>   (the title cell of a row that opens)
 *         </td>
 *       </tr>
 *       <tr class="app-table-state" role="row"><td class="app-table-state-cell" role="cell" colspan>[state]</td></tr>
 *     </tbody>
 *     <tfoot role="rowgroup">
 *       <tr class="app-table-total" role="row">
 *         <td class="app-table-select" role="cell"></td>
 *         <th class="app-table-cell" role="rowheader" scope="row" data-role="title">Summe</th>
 *         <td class="app-table-cell" role="cell" data-align="end">1.234,00 €</td>
 *       </tr>
 *     </tfoot>
 *   </table>
 * </div>
 *
 * The roles are written out because a list row is a grid, not a table row, and some browsers
 * drop a table's semantics once its parts are laid out as something else.
 *
 * The grid. A row is exactly one control high (40, or 32 compact), and a cell is one line: it
 * never wraps, it ends in an ellipsis. Row lines are inset shadows, so a row stays 40. Columns
 * take a width from the column scale (s 80, m 120, l 160, xl 240) or share what is left; the
 * table never scrolls sideways. Instead each column has a priority: 1 always shows, 3 goes
 * below a region of 960, 2 goes in a list row.
 *
 * The table hangs its cell padding outside its own box: it is two cell insets wider than the
 * space it is given and starts one inset early, so the text of the first column stands on the
 * same edge as the title above the table. Row hovers and lines reach into the margin.
 *
 * A list row (region below 560): the title cell on the first line, every other cell of
 * priority 1 on a line of its own below it, the trailing cell (a status, an amount) at
 * the end across all lines, the checkbox at the start. Lines are 20, the row has 12 above and
 * below: 2 lines are 64, 3 are 84. The header row is gone; sorting is the screen's menu then.
 * A totals row keeps every total it has, at the end, whatever its column's priority.
 *
 * The row open beside the list (a split view) has aria-current="page" on its link, as the
 * current navigation item has: the same soft accent and accent text, over hover and over a
 * chosen row's tint.
 *
 * A state written as a word (a record lane's trailing cell: In Arbeit, bezahlt, abgelaufen) is
 * grey, in columns and in a list row alike; one that asks for doing (overdue, run out) is in
 * the warning tone. No chip, no colour for a state that is merely where a record stands:
 *   <span class="app-table-word" data-tone="warning">14 Tage überfällig</span>   (data-tone optional)
 *
 * A row whose changes are kept on the device and not yet sent carries a mark after its title,
 * in the tone of the offline status in the shell bar; its words are read out with the title:
 *   <span class="app-table-pending">[icon: sync]<span class="app-table-pending-label">Wartet auf Senden</span></span>
 *
 * The title cell's link covers the whole row, so the row opens on a click anywhere; the
 * checkbox sits above it. The header row sticks to the top of what scrolls: the main area or a
 * pane from a viewport of 960, below that the document, under the shell's bar.
 */
@layer components {
    .app-table {
        position: relative;
        inset-inline-start: calc(-1 * var(--app-space-cell-x));
        inline-size: calc(100% + 2 * var(--app-space-cell-x));
        min-inline-size: 0;
    }

    .app-table-grid {
        inline-size: 100%;
        table-layout: fixed;
        border-collapse: separate;
        border-spacing: 0;
        color: var(--app-color-text);
        font-size: var(--app-text-body-size);
        line-height: var(--app-text-body-leading);
    }

    .app-table-caption {
        position: absolute;
        inline-size: var(--app-border-width);
        block-size: var(--app-border-width);
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    :is(.app-table-header, .app-table-cell, .app-table-select) {
        block-size: var(--app-control-height);
        padding: 0 var(--app-space-cell-x);
        overflow: hidden;
        text-align: start;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: middle;
        box-shadow: inset 0 calc(-1 * var(--app-border-width)) 0 var(--app-color-line);
    }

    .app-table-select {
        inline-size: calc(var(--app-choice-size) + 2 * var(--app-space-cell-x));
    }

    /* The checkbox stands above the row's link. */
    .app-table-select .app-choice {
        z-index: 1;
        vertical-align: middle;
    }

    /* The header row is opaque, to cover the rows that scroll under it, in the colour of what
       the table stands on: the page, or a card, dialog or drawer. */
    thead :is(.app-table-header, .app-table-select) {
        position: sticky;
        inset-block-start: 0;
        z-index: var(--app-z-header);
        background: var(--app-color-page);
        color: var(--app-color-text-muted);
        font-size: var(--app-text-table-head-size);
        font-weight: var(--app-text-table-head-weight);
        line-height: var(--app-text-label-leading);
        box-shadow: inset 0 calc(-1 * var(--app-border-width)) 0 var(--app-color-line-strong);
    }

    :is(.app-card, .app-dialog, .app-drawer) thead :is(.app-table-header, .app-table-select) {
        background: var(--app-color-surface);
    }

    :is(.app-table-header, .app-table-cell)[data-align="end"] {
        font-variant-numeric: tabular-nums;
        text-align: end;
    }

    .app-table-header[data-width="s"] { inline-size: var(--app-column-width-s); }
    .app-table-header[data-width="m"] { inline-size: var(--app-column-width-m); }
    .app-table-header[data-width="l"] { inline-size: var(--app-column-width-l); }
    .app-table-header[data-width="xl"] { inline-size: var(--app-column-width-xl); }

    /* Sorting. */
    .app-table-sort {
        display: inline-flex;
        align-items: center;
        gap: var(--app-space-header-row);
        max-inline-size: 100%;
        padding: 0;
        border: 0;
        border-radius: var(--app-radius-option);
        background: none;
        color: inherit;
        font: inherit;
        cursor: pointer;
    }

    .app-table-sort .app-icon {
        color: var(--app-color-text-subtle);
    }

    .app-table-header[aria-sort] {
        color: var(--app-color-text);
    }

    .app-table-header[aria-sort] .app-table-sort .app-icon {
        color: var(--app-color-accent-text);
    }

    @media (hover: hover) {
        .app-table-sort:hover {
            color: var(--app-color-text);
        }
    }

    .app-table-sort:focus-visible {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: var(--app-focus-ring-offset);
    }

    /* Rows. */
    .app-table-row {
        position: relative;
    }

    .app-table-row[data-selected="true"] > * {
        background: var(--app-color-row-selected);
    }

    @media (hover: hover) {
        .app-table-row:not([data-loading]):hover > * {
            background: var(--app-color-row-hover);
        }

        .app-table-row[data-selected="true"]:hover > * {
            background: var(--app-color-row-selected);
        }
    }

    /* The open row. A step more specific than hover, so it holds under the pointer. */
    .app-table-row:not([data-loading]):has(.app-table-link[aria-current="page"]) > * {
        background: var(--app-color-row-current);
    }

    .app-table-row:has(.app-table-link[aria-current="page"]) > [data-role="title"] {
        color: var(--app-color-row-current-text);
    }

    .app-table-cell[data-role="title"] {
        font-weight: var(--app-text-tab-weight);
    }

    .app-table-word {
        color: var(--app-color-text-muted);
    }

    .app-table-word[data-tone="warning"] {
        color: var(--app-color-warning-text);
        font-weight: var(--app-text-label-weight);
    }

    .app-table-link {
        color: inherit;
        text-decoration: none;
    }

    /* The mark takes its icon and the gap before it as its own width, so it needs no margin. */
    .app-table-pending {
        display: inline-flex;
        justify-content: flex-end;
        inline-size: calc(var(--app-icon-size) + var(--app-space-inline));
        color: var(--app-color-row-pending);
        vertical-align: top;
    }

    .app-table-pending .app-icon {
        block-size: var(--app-text-body-leading);
    }

    .app-table-pending-label {
        position: absolute;
        inline-size: var(--app-border-width);
        block-size: var(--app-border-width);
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    .app-table-link::after {
        content: "";
        position: absolute;
        inset: 0;
    }

    .app-table-link:focus-visible {
        outline: none;
    }

    .app-table-link:focus-visible::after {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: calc(-1 * var(--app-focus-ring-width));
    }

    /* Totals. */
    .app-table-total > * {
        font-weight: var(--app-text-strong-weight);
        box-shadow: inset 0 var(--app-border-width-strong) 0 var(--app-color-line-strong);
    }

    /* A state (empty, error) in place of the rows. */
    .app-table-state-cell {
        padding: 0;
        white-space: normal;
    }

    /* Priorities. */
    @container app-region (width < 960px) {
        :is(.app-table-header, .app-table-cell)[data-priority="3"] {
            display: none;
        }
    }

    /* List rows. */
    @container app-region (width < 560px) {
        .app-table-grid,
        .app-table-grid > :is(tbody, tfoot) {
            display: block;
        }

        .app-table-grid > thead {
            display: none;
        }

        .app-table-grid > :is(tbody, tfoot) > tr {
            display: grid;
            grid-template-columns: [select] auto [main] minmax(0, 1fr) [trailing] auto;
            column-gap: var(--app-space-cell-x);
            padding: var(--app-space-row-y) var(--app-space-cell-x);
            box-shadow: inset 0 calc(-1 * var(--app-border-width)) 0 var(--app-color-line);
        }

        .app-table-grid > :is(tbody, tfoot) > tr > * {
            display: block;
            grid-column: main;
            block-size: auto;
            padding: 0;
            background: none;
            box-shadow: none;
            text-align: start;
        }

        .app-table-grid > :is(tbody, tfoot) > tr > [data-align] {
            text-align: start;
        }

        .app-table-grid > :is(tbody, tfoot) > tr > :is([data-priority="2"], [data-priority="3"]) {
            display: none;
        }

        .app-table-grid > :is(tbody, tfoot) > tr > :not([data-role]) {
            color: var(--app-color-text-muted);
        }

        .app-table-grid > :is(tbody, tfoot) > tr > [data-role="title"] {
            grid-row: 1;
        }

        .app-table-grid > :is(tbody, tfoot) > tr > [data-role="trailing"] {
            grid-row: 1 / span 4;
            grid-column: trailing;
            align-self: center;
            color: var(--app-color-text);
            text-align: end;
        }

        .app-table-grid > :is(tbody, tfoot) > tr > .app-table-select {
            display: flex;
            grid-row: 1 / span 4;
            grid-column: select;
            inline-size: auto;
        }

        .app-table-grid > :is(tbody, tfoot) > tr > .app-table-select:empty {
            display: none;
        }

        /* A row's background is the row's, now that the cells are lines in it. */
        .app-table-row[data-selected="true"] {
            background: var(--app-color-row-selected);
        }

        .app-table-row:not([data-loading]):has(.app-table-link[aria-current="page"]) {
            background: var(--app-color-row-current);
        }

        /* A total stays, whatever its column's priority: at the end, one per line beside the
           label. A column without a total has no cell in the row. */
        .app-table-grid > tfoot > tr > td:empty {
            display: none;
        }

        .app-table-grid > tfoot > tr > td.app-table-cell:not(:empty) {
            display: block;
            grid-column: trailing;
            color: var(--app-color-text);
            text-align: end;
        }

        .app-table-grid > tfoot > .app-table-total {
            box-shadow: inset 0 var(--app-border-width-strong) 0 var(--app-color-line-strong);
        }

        .app-table-grid > tbody > .app-table-state {
            display: block;
            padding: 0;
        }

        .app-table-row[data-loading] > * {
            display: flex;
            align-items: center;
            min-block-size: var(--app-text-body-leading);
        }
    }

    @media (hover: hover) {
        @container app-region (width < 560px) {
            .app-table-row:not([data-loading]):hover {
                background: var(--app-color-row-hover);
            }
        }
    }

    /* Below a 960 viewport the document scrolls, and the shell's bar is over it. */
    @media (width < 960px) {
        .app-shell-main thead :is(.app-table-header, .app-table-select) {
            inset-block-start: calc(var(--app-bar-height) + var(--app-space-safe-top));
        }
    }

    /* Forced colours (a Windows contrast theme): shadows and backgrounds are gone, so frames
       are outlines and a chosen state is the system highlight. */
    @media (forced-colors: active) {
        :is(.app-table-header, .app-table-cell, .app-table-select) {
            border-block-end: var(--app-border-width) solid var(--app-color-forced-line);
        }

        .app-table-row:has(.app-table-link[aria-current="page"]) > * {
            forced-color-adjust: none;
            background: var(--app-color-forced-current);
            color: var(--app-color-forced-current-text);
        }
    }
}
