/*
 * layout.css — placement is a component, not a margin on a child.
 *
 * The parent owns the gap and the flex sizing. A child never separates itself with a margin,
 * and a control never sets flex on its own root: from the components layer it would beat
 * these rules. data-gap takes a step of the space scale (1–6, 8, 12); this is the only file
 * allowed to name those primitives directly, because the step is the contract.
 *
 * The column grid. There is one grid, and everything that sits in columns sits on it:
 *
 *   region width   < 560     560 – 959    >= 960
 *   columns            4          8           12
 *   gutter        narrow       wide        wide
 *
 * A region is a box that lays out its own content: the main area of the shell, a page (also
 * each page in a pane of a split view), a dialog or drawer body. Every region is a container
 * named app-region, and the grid asks the region it sits in, never the viewport, so a form has
 * 12 columns on a wide page and 8 in the detail pane beside a list. The page inset equals the
 * gutter, so the page's edge is one more gutter of the same grid.
 *
 * Few boxes are regions, on purpose. A query container is also a stacking context and the
 * containing block of anything fixed inside it. A page is one, so a select's list and the
 * page's action bar stack in the same context and the list stays on top; were every column a
 * region, a list would open beneath the fields of the next row. Overlays that must cover the
 * whole screen (dialog, drawer, the navigation panel) live in the top layer, which no
 * container holds.
 *
 * <div class="app-columns">
 *   <div class="app-column" data-span="3 | 4 | 6 | 8 | 9 | 12">…</div>
 * </div>
 *
 * A span is written in twelfths, as a fraction of the row: a quarter, a third, a half, two
 * thirds, three quarters or the whole. On fewer columns each fraction becomes the nearest
 * span that keeps rows whole:
 *
 *   span (of 12)       3   4   6   8   9  12
 *   on 8 columns       2   4   4   4   6   8
 *   on 4 columns       2   4   4   4   4   4
 *
 * Every row of the 12 grid stays a whole row on 8 columns (¼+¾, ⅓+⅔, ½+½, ¼×4) except three
 * thirds: 8 does not divide by three, so a third and two thirds both become a half, and the
 * third third wraps. On 4 columns everything is the whole row but a quarter, which is a half. A grid inside a
 * column counts its columns from the region, like every grid; nest a grid only in a full row.
 *
 * <div class="app-grid" data-columns="2 | 3 | 4">[tiles]</div>
 *
 * Equal tiles on the same grid: each tile spans a half, a third or a quarter, and follows the
 * same table. The grid takes the gutter as its gap and has no data-gap of its own; a gap that
 * differs from the gutter would put the tiles off the columns of the page around them.
 */
@layer layout {
    /* A row that scrolls sideways (tabs, filter chips) fades out at an edge with more behind
       it, so a cut tab or chip reads as "more this way", not as broken. Driven by the row's
       own scroll position: at the start only the end fades, at the end only the start, between
       both. A row that does not scroll, or a browser without scroll timelines, shows no fade. */
    @keyframes app-scroll-fade {
        0% {
            mask-image: linear-gradient(to right, currentcolor calc(100% - var(--app-scroll-fade)), transparent);
        }

        2%,
        98% {
            mask-image: linear-gradient(to right, transparent, currentcolor var(--app-scroll-fade), currentcolor calc(100% - var(--app-scroll-fade)), transparent);
        }

        100% {
            mask-image: linear-gradient(to right, transparent, currentcolor var(--app-scroll-fade));
        }
    }

    /* Stack: children one under the other. */
    .app-stack {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-stack);
    }

    .app-stack[data-align="start"] { align-items: flex-start; }
    .app-stack[data-align="center"] { align-items: center; }
    .app-stack[data-align="end"] { align-items: flex-end; }

    /* Cluster: a row that wraps. */
    .app-cluster {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: var(--app-space-cluster);
    }

    .app-cluster[data-justify="center"] { justify-content: center; }
    .app-cluster[data-justify="end"] { justify-content: flex-end; }
    .app-cluster[data-justify="between"] { justify-content: space-between; }
    .app-cluster[data-align="start"] { align-items: flex-start; }
    .app-cluster[data-align="end"] { align-items: flex-end; }
    .app-cluster[data-align="baseline"] { align-items: baseline; }

    /* Regions. */
    :where(.app-shell-main, .app-page, .app-dialog-body, .app-drawer-body) {
        container: app-region / inline-size;
    }

    /* The column grid: 4 columns until the region is 560 wide. */
    .app-columns,
    .app-grid {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        column-gap: var(--app-space-gutter-narrow);
        row-gap: var(--app-space-grid-row);
    }

    /* A column is a grid box, so what it holds is a box too: an inline control (a checkbox) gets
       no line box around it, whose descender would put the row off the 4px grid. */
    .app-column {
        display: grid;
        grid-column: span 4;
        align-content: start;
        min-inline-size: 0;
    }

    .app-column[data-span="3"],
    .app-grid[data-columns="4"] > * {
        grid-column: span 2;
    }

    .app-grid > * {
        grid-column: span 4;
        min-inline-size: 0;
    }

    /* 8 columns from a region of 560. */
    @container app-region (width >= 560px) {
        .app-columns,
        .app-grid {
            grid-template-columns: repeat(8, minmax(0, 1fr));
            column-gap: var(--app-space-gutter-wide);
        }

        .app-column[data-span="3"] { grid-column: span 2; }
        .app-column[data-span="4"] { grid-column: span 4; }
        .app-column[data-span="6"] { grid-column: span 4; }
        .app-column[data-span="8"] { grid-column: span 4; }
        .app-column[data-span="9"] { grid-column: span 6; }
        .app-column[data-span="12"] { grid-column: span 8; }

        .app-grid[data-columns="2"] > * { grid-column: span 4; }
        .app-grid[data-columns="3"] > * { grid-column: span 4; }
        .app-grid[data-columns="4"] > * { grid-column: span 2; }
    }

    /* 12 columns from a region of 960. */
    @container app-region (width >= 960px) {
        .app-columns,
        .app-grid {
            grid-template-columns: repeat(12, minmax(0, 1fr));
        }

        .app-column[data-span="3"] { grid-column: span 3; }
        .app-column[data-span="4"] { grid-column: span 4; }
        .app-column[data-span="6"] { grid-column: span 6; }
        .app-column[data-span="8"] { grid-column: span 8; }
        .app-column[data-span="9"] { grid-column: span 9; }
        .app-column[data-span="12"] { grid-column: span 12; }

        .app-grid[data-columns="2"] > * { grid-column: span 6; }
        .app-grid[data-columns="3"] > * { grid-column: span 4; }
        .app-grid[data-columns="4"] > * { grid-column: span 3; }
    }

    /* Sidebar: a pane of fixed width beside a fluid one. When the fluid pane would fall under
       half the width, the two stack. data-side puts the pane at the start or the end. */
    .app-sidebar {
        display: flex;
        flex-wrap: wrap;
        gap: var(--app-space-stack);
    }

    .app-sidebar-pane {
        flex-basis: var(--app-sidebar-width);
        flex-grow: 1;
    }

    .app-sidebar-content {
        flex-basis: 0;
        flex-grow: 999;
        min-inline-size: 50%;
    }

    .app-sidebar[data-side="end"] .app-sidebar-pane { order: 1; }

    /* Switcher: a row until the width runs out, then a stack. */
    .app-switcher {
        display: flex;
        flex-wrap: wrap;
        gap: var(--app-space-stack);
    }

    .app-switcher > * {
        flex-basis: calc((var(--app-switcher-threshold) - 100%) * 999);
        flex-grow: 1;
    }

    /* The gap, from the scale. */
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="1"] { gap: var(--app-space-1); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="2"] { gap: var(--app-space-2); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="3"] { gap: var(--app-space-3); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="4"] { gap: var(--app-space-4); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="5"] { gap: var(--app-space-5); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="6"] { gap: var(--app-space-6); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="8"] { gap: var(--app-space-8); }
    :is(.app-stack, .app-cluster, .app-sidebar, .app-switcher)[data-gap="12"] { gap: var(--app-space-12); }
}
