/* TechnoPulse.Ui.Data: the DataGrid (Grid/DataGrid.razor, DS-K02), a wrapper over QuickGrid's table, and the column
   picker. A host links this file after suite.css, on the pages that show a grid:
       <link rel="stylesheet" href="_content/TechnoPulse.Ui.Data/css/data-grid.css" />
   The rules sit in the suite's components layer, so suite.css's layer order holds; tokens only
   (src/ui/.stylelintrc.json). Layout never depends on QuickGrid's own optional stylesheet: a host may or may not load
   it, and its few rules (th position, text alignment) agree with these.

   The scroller is the grid's frame: it scrolls both ways inside the page, the header sticks to its top, and it is a
   stacking context, so the header's z-index never reaches the page. */

@layer components {
    .tp-grid {
        --grid-max-height: min(70vh, 48rem);

        display: flex;
        flex-direction: column;
        gap: var(--space-2);
        min-inline-size: 0;
    }

    .tp-grid__scroller {
        position: relative;
        isolation: isolate;
        max-block-size: var(--grid-max-height);
        overflow: auto;
        border: var(--border-width) solid var(--color-border);
        border-radius: var(--radius-md);
        background: var(--color-surface);
    }

    .tp-grid__table {
        inline-size: 100%;
        border-collapse: separate;
        border-spacing: 0;
        color: var(--color-text);
        font-size: var(--density-font-size);
        line-height: var(--line-height-snug);
    }

    /* ---- Header ------------------------------------------------------------------------------------- */

    .tp-grid__table thead {
        position: sticky;
        inset-block-start: 0;
        z-index: var(--z-raised);
    }

    .tp-grid__table th {
        padding: 0;
        border-block-end: var(--border-width) solid var(--color-border-strong);
        background: var(--color-surface-2);
        font-weight: var(--font-weight-semibold);
        text-align: start;
        vertical-align: bottom;
        white-space: nowrap;
    }

    .tp-grid__table .col-header-content { display: flex; align-items: stretch; }

    .tp-grid__head {
        display: flex;
        flex: 1 1 auto;
        align-items: center;
        min-block-size: var(--control-height);
    }

    .tp-grid__head > .tp-grid__title { padding: var(--space-2) var(--space-3); }

    .tp-grid__sort {
        display: flex;
        flex: 1 1 auto;
        align-items: center;
        gap: var(--space-1);
        min-block-size: var(--control-height);
        padding: var(--space-2) var(--space-3);
        border: 0;
        background: transparent;
        color: inherit;
        font: inherit;
        text-align: inherit;
        cursor: pointer;
    }

    .tp-grid__sort:hover { background: var(--color-surface); }
    .tp-grid__sort-icon { flex: none; color: var(--color-text-muted); }
    .tp-grid__table th:is([aria-sort="ascending"], [aria-sort="descending"]) .tp-grid__sort-icon { color: var(--color-accent-text); }

    /* Numbers line up at the end; a sortable heading puts its arrow before the title, so the title ends with them. */
    .tp-grid__cell--end { text-align: end; font-variant-numeric: var(--font-numeric-data); }
    .tp-grid__cell--center { text-align: center; }
    .tp-grid__table th.tp-grid__cell--end .tp-grid__head { justify-content: flex-end; }
    .tp-grid__table th.tp-grid__cell--end .tp-grid__sort { flex-direction: row-reverse; justify-content: flex-start; }
    .tp-grid__table th.tp-grid__cell--center :is(.tp-grid__head, .tp-grid__sort) { justify-content: center; }

    /* ---- Rows --------------------------------------------------------------------------------------- */

    .tp-grid__table td {
        block-size: var(--row-height);
        padding: var(--space-2) var(--space-3);
        border-block-end: var(--border-width) solid var(--color-border);
        vertical-align: middle;
    }

    .tp-grid__table tbody tr:last-child td { border-block-end: 0; }
    .tp-grid__table tbody tr:hover td { background: var(--color-surface-2); }

    /* The row whose record is open (CurrentKey): the selected colour and a bar at its start. */
    .tp-grid__table tr.tp-grid__row--current td { background: var(--color-surface-2); }
    .tp-grid__table tr.tp-grid__row--current td:first-child { box-shadow: inset var(--space-1) 0 0 var(--color-accent); }

    /* QuickGrid pads the last page with empty rows; they hold nothing, so they take no space and are never read out. */
    .tp-grid__table tbody > tr:not([aria-rowindex]):has(> td) { display: none; }

    /* A virtualised grid needs rows of one height: one line each, cut with an ellipsis. */
    .tp-grid[data-virtual] .tp-grid__table td {
        max-inline-size: 24rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Rows a virtualised grid has not loaded yet. */
    .tp-grid__table td.grid-cell-placeholder::after {
        content: "";
        display: block;
        block-size: 0.875em;
        border-radius: var(--radius-xs);
        background: var(--color-surface-2);
    }

    /* While a new sort or page loads, the old rows stay, dimmed. */
    .tp-grid[aria-busy="true"] .tp-grid__table tbody { opacity: 0.6; }

    .tp-grid__link {
        border-radius: var(--radius-xs);
        color: var(--color-accent-text);
        font-weight: var(--font-weight-semibold);
        text-decoration: underline;
        text-decoration-thickness: 1px;
        text-underline-offset: 0.18em;
    }

    .tp-grid__link:hover { text-decoration-thickness: 2px; }

    /* On touch the row's link is a full 44px target. */
    @media (pointer: coarse) {
        .tp-grid__link {
            display: inline-flex;
            align-items: center;
            min-block-size: var(--target-size);
        }
    }

    /* ---- Loading, empty and pages ------------------------------------------------------------------- */

    .tp-grid__skeleton { padding: var(--space-4) var(--space-3); }
    .tp-grid__skeleton .tp-skeleton__shapes { gap: var(--space-5); }

    .tp-grid__empty { padding: var(--space-6) var(--space-4); }
    .tp-grid__empty-text { margin: 0; color: var(--color-text-muted); text-align: center; }

    .tp-grid__pager {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: var(--space-2) var(--space-4);
    }

    .tp-grid__summary {
        margin: 0;
        color: var(--color-text-muted);
        font-variant-numeric: var(--font-numeric-data);
    }

    .tp-grid__pages { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
    .tp-grid__page-of { font-variant-numeric: var(--font-numeric-data); }

    /* At the first or last page the button stays focusable (a disabled one would drop focus) but does nothing. */
    .tp-grid__pages .tp-button[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }

    /* ---- Focus -------------------------------------------------------------------------------------- */

    :is(.tp-grid__scroller, .tp-grid__sort, .tp-grid__link):focus-visible {
        outline: var(--focus-width) solid var(--color-focus);
        outline-offset: var(--focus-offset);
        box-shadow: 0 0 0 calc(var(--focus-width) + var(--focus-offset)) var(--color-focus-halo);
    }

    .tp-grid__scroller:focus-visible,
    .tp-grid__sort:focus-visible { outline-offset: calc(var(--focus-width) * -1); }

    @media (forced-colors: active) {
        .tp-grid__scroller, .tp-grid__table th { border-color: CanvasText; }
        .tp-grid__table tr.tp-grid__row--current td:first-child { outline: 2px solid Highlight; outline-offset: -2px; }
    }
}
