/*
 * shell.css — the document, the page frame, and the parts of a screen that touch its edges.
 *
 * The page is as tall as the dynamic viewport (dvh, never 100vh, which is wrong on a phone
 * while its toolbars move). Whatever touches an edge of the screen clears the notch, the home
 * indicator and the rounded corners by the device's safe area (--app-space-safe-*, semantic.css).
 * This is the only file that pads one side differently from the other, and only by that.
 *
 * A bar is 12 + 40 + 12 = 64 tall, on the 4px grid. Its dividing line is an inset shadow, not
 * a border, so it takes no room and the bar stays 64.
 *
 * <div class="app-shell" data-nav="rail | wide">   (data-nav only with a navigation)
 *   <nav class="app-shell-nav" id popover aria-label>
 *     <div class="app-shell-nav-head">[icon button: widen the column, aria-pressed]</div>
 *     <div class="app-shell-nav-groups">[navigation groups]</div>
 *   </nav>
 *   <header class="app-shell-header">
 *     <button class="app-icon-button" … popovertarget="[nav id]">[menu]</button>   (not with tabs)
 *     <div class="app-shell-bar">[one layout primitive]</div>
 *   </header>
 *   <main class="app-shell-main">[one page, or one split view]</main>
 *   <footer class="app-shell-footer">[one layout primitive]</footer>
 *   <nav class="app-shell-tabs" aria-label>[tab bar (tab-bar.css)]</nav>   (only with tabs)
 * </div>
 *
 * The frame is a grid of named areas. Below a viewport of 960 it is one column and the document
 * scrolls (so a phone's toolbars can collapse). There the main areas are the tab bar at the
 * bottom, under the thumb, with the page's action bar standing on it; a screen without tabs
 * keeps the navigation as a popover panel from the start edge, opened by the menu button (the
 * browser brings light dismiss, Escape and the return of focus). From 960 the navigation is a
 * column of its own, the tab bar and the menu button go, and the frame is exactly as tall as
 * the viewport: header and footer stay put and the main area (or each pane of a split view)
 * scrolls by itself. The column is a rail of icons, one touch target wide, until the person
 * widens it to its labels; the screen keeps that choice. The rail leaves a tablet held across
 * room for a list and its detail side by side. Navigation and menu button are written only
 * when the screen has a navigation.
 *
 *   < 960          >= 960
 *   header         nav  header
 *   main           nav  main
 *   footer         nav  footer
 *   tabs
 *
 * <div class="app-page">
 *   <div class="app-page-body">[page header, content]</div>
 *   <div class="app-action-bar">[a reason (sync.css), buttons]</div>
 * </div>
 *
 * A page is the padded content of main or of a pane, and a region (layout.css). Its body has
 * the page inset on every side; the inset is the gutter, so the body's edge sits on the grid.
 * The action bar is written only when the page has actions. It stays at the bottom of what
 * scrolls, as wide as the page, and on a narrow region its buttons share the width. A reason
 * why one of them is unavailable takes a line of its own above them.
 *
 * <div class="app-split" data-show="list | detail">
 *   <section class="app-split-pane" data-pane="list" aria-label>[page]</section>
 *   <section class="app-split-pane" data-pane="detail" aria-label>[page]</section>
 * </div>
 *
 * List and detail side by side once the region is 960 wide: the list takes a fixed track,
 * the detail the rest. Narrower, only the pane named by data-show is there; the screen shows
 * the detail when an item is chosen and puts a back link in the detail's page header.
 */
@layer layout {
    *,
    *::before,
    *::after {
        box-sizing: border-box;
    }

    html {
        color-scheme: var(--app-color-scheme);
        background: var(--app-color-page);
        color: var(--app-color-text);
        font-family: var(--app-font-text);
        font-size: var(--app-text-body-size);
        font-weight: var(--app-text-body-weight);
        line-height: var(--app-text-body-leading);
        text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
    }

    body {
        min-block-size: var(--app-page-height);
        margin: 0;
    }

    /* Text elements carry no spacing of their own; the parent's gap places them. */
    :where(h1, h2, h3, h4, h5, h6, p, ul, ol, dl, dd, figure, blockquote) {
        margin: 0;
    }

    :where(ul, ol)[role] {
        padding: 0;
        list-style: none;
    }

    /* Bold is the strong role, one of the three weights, not the browser's 700. */
    :where(b, strong) {
        font-weight: var(--app-text-strong-weight);
    }

    /* Headings and short prose avoid a lone last word. Not balance for headings: it evens the
       lines by breaking at any chance, and a title's order number (A-2026-0142) has a chance at
       every hyphen. */
    :where(h1, h2, h3, h4, h5, h6, p, dd, li) {
        text-wrap: pretty;
    }

    :where(button, input, select, textarea) {
        font: inherit;
        color: inherit;
    }

    .app-shell {
        display: grid;
        grid-template-areas:
            "header"
            "main"
            "footer"
            "tabs";
        grid-template-rows: auto minmax(0, 1fr) auto auto;
        grid-template-columns: minmax(0, 1fr);
        min-block-size: var(--app-page-height);
    }

    .app-shell-header {
        position: sticky;
        inset-block-start: 0;
        z-index: var(--app-z-header);
        display: grid;
        grid-area: header;
        grid-template-columns: minmax(0, 1fr);
        align-items: center;
        gap: var(--app-space-inline);
        padding-block-start: calc(var(--app-space-bar-y) + var(--app-space-safe-top));
        padding-block-end: var(--app-space-bar-y);
        padding-inline-start: calc(var(--app-space-page) + var(--app-space-safe-left));
        padding-inline-end: calc(var(--app-space-page) + var(--app-space-safe-right));
        background: var(--app-color-surface);
        box-shadow: inset 0 calc(-1 * var(--app-border-width)) 0 var(--app-color-line);
    }

    .app-shell-header:has(> [popovertarget]) {
        grid-template-columns: auto minmax(0, 1fr);
    }

    /* The bar is a container, so what sits in it can tell a phone's header from a wide one
       (sync.css shortens the sync status). */
    .app-shell-bar {
        container: app-bar / inline-size;
        display: grid;
        min-inline-size: 0;
    }

    /* A header with nothing to show takes no space. */
    .app-shell-header:not(:has(> [popovertarget])):has(> .app-shell-bar:empty) {
        display: none;
    }

    .app-shell-main {
        display: flex;
        grid-area: main;
        flex-direction: column;
        min-inline-size: 0;
    }

    .app-shell-main > *,
    .app-split-pane > * {
        flex: 1 0 auto;
    }

    .app-shell-footer {
        position: sticky;
        inset-block-end: 0;
        z-index: var(--app-z-header);
        display: grid;
        grid-area: footer;
        align-items: center;
        gap: var(--app-space-inline);
        padding-block-start: var(--app-space-bar-y);
        padding-block-end: calc(var(--app-space-bar-y) + var(--app-space-safe-bottom));
        padding-inline-start: calc(var(--app-space-page) + var(--app-space-safe-left));
        padding-inline-end: calc(var(--app-space-page) + var(--app-space-safe-right));
        background: var(--app-color-surface);
        box-shadow: inset 0 var(--app-border-width) 0 var(--app-color-line);
    }

    .app-shell-footer:empty {
        display: none;
    }

    /* The tab bar: on the bottom edge, so it clears the home indicator and the rounded corners. */
    .app-shell-tabs {
        anchor-name: --app-tab-bar;
        position: sticky;
        inset-block-end: 0;
        z-index: var(--app-z-header);
        grid-area: tabs;
        padding-block-start: var(--app-space-tab-bar);
        padding-block-end: calc(var(--app-space-tab-bar) + var(--app-space-safe-bottom));
        padding-inline-start: calc(var(--app-space-tab-bar) + var(--app-space-safe-left));
        padding-inline-end: calc(var(--app-space-tab-bar) + var(--app-space-safe-right));
        background: var(--app-color-surface);
        box-shadow: inset 0 var(--app-border-width) 0 var(--app-color-line);
    }

    /* The page's action bar and the footer stand on the tab bar, which clears the bottom edge
       for them. */
    @media (width < 960px) {
        .app-shell:has(> .app-shell-tabs) .app-action-bar,
        .app-shell:has(> .app-shell-tabs) > .app-shell-footer {
            inset-block-end: calc(var(--app-tab-bar-height) + var(--app-space-safe-bottom));
            padding-block-end: var(--app-space-bar-y);
        }

        .app-shell:has(> .app-shell-tabs):has(> .app-shell-footer:empty) .app-page:not(:has(> .app-action-bar)) > .app-page-body {
            padding-block-end: var(--app-space-page);
        }
    }

    /* While the keyboard is up (fsm-ui.js sets data-keyboard and the inset): the tab bar steps
       aside, the action bar and the footer stand on the keyboard, which covers the home
       indicator, and the page keeps room to scroll its last field above them. */
    :root[data-keyboard] .app-shell-tabs {
        display: none;
    }

    :root[data-keyboard] .app-shell .app-action-bar,
    :root[data-keyboard] .app-shell > .app-shell-footer {
        inset-block-end: var(--app-space-keyboard);
        padding-block-end: var(--app-space-bar-y);
    }

    :root[data-keyboard] .app-shell .app-page > .app-page-body {
        padding-block-end: calc(var(--app-space-page) + var(--app-space-keyboard));
    }

    :root[data-keyboard] {
        scroll-padding-block: var(--app-bar-height) calc(var(--app-space-keyboard) + var(--app-bar-height));
    }

    /* The head of the column: the button that widens or narrows it. A panel has no use for it. */
    .app-shell-nav-head {
        display: none;
    }

    .app-shell-nav-groups {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-nav-groups);
    }

    /* The navigation, as a panel from the start edge while it is open on a narrow screen. */
    .app-shell-nav {
        grid-area: nav;
        min-block-size: 0;
        margin: 0;
        border: 0;
        background: var(--app-color-nav);
        color: var(--app-color-text);
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    .app-shell-nav:popover-open {
        position: fixed;
        inset-block: 0;
        inset-inline: 0 auto;
        z-index: var(--app-z-side-panel);
        display: flex;
        flex-direction: column;
        gap: var(--app-space-nav-groups);
        inline-size: min(calc(var(--app-nav-width) + var(--app-space-safe-left)), 100%);
        block-size: var(--app-page-height);
        max-block-size: var(--app-page-height);
        padding-block-start: calc(var(--app-space-nav-inset) + var(--app-space-safe-top));
        padding-block-end: calc(var(--app-space-nav-inset) + var(--app-space-safe-bottom));
        padding-inline-start: calc(var(--app-space-nav-inset) + var(--app-space-safe-left));
        padding-inline-end: var(--app-space-nav-inset);
        box-shadow: var(--app-shadow-drawer);
    }

    .app-shell-nav::backdrop {
        background: var(--app-color-scrim);
    }

    /* A page that touches the top or the bottom of the screen clears it itself. */
    .app-shell:not(:has(> .app-shell-nav)):has(> .app-shell-header > .app-shell-bar:empty) .app-page-body {
        padding-block-start: calc(var(--app-space-page) + var(--app-space-safe-top));
    }

    .app-shell:has(> .app-shell-footer:empty) .app-page:not(:has(> .app-action-bar)) > .app-page-body {
        padding-block-end: calc(var(--app-space-page) + var(--app-space-safe-bottom));
    }

    .app-shell:has(> .app-shell-footer:not(:empty)) .app-action-bar {
        padding-block-end: var(--app-space-bar-y);
    }

    /* Page. */
    .app-page {
        display: flex;
        flex-direction: column;
        min-inline-size: 0;
    }

    .app-page-body {
        display: flex;
        flex: 1 0 auto;
        flex-direction: column;
        gap: var(--app-space-page-gap);
        min-inline-size: 0;
        padding-block: var(--app-space-page);
        padding-inline-start: calc(var(--app-space-page) + var(--app-space-safe-left));
        padding-inline-end: calc(var(--app-space-page) + var(--app-space-safe-right));
    }

    /* Rows that run to the screen's edges: a row of chips (or a board's lanes) that scrolls sideways is cut by the
       screen, not in mid-air at the gutter, which says "more behind" (as on iOS and Android) even
       where there is no scroll fade (Safari before 26). The page body takes back its own inset
       and gives it back as padding, so at rest the first chip sits on the gutter, and a chip
       scrolled to with the keyboard stops there too. */
    @container app-region (width < 560px) {
        .app-page-body > .app-filter-bar > .app-filter-bar-filters,
        .app-page-body > .app-board {
            margin-inline-start: calc(-1 * (var(--app-space-page) + var(--app-space-safe-left)));
            margin-inline-end: calc(-1 * (var(--app-space-page) + var(--app-space-safe-right)));
            padding-inline-start: calc(var(--app-space-page) + var(--app-space-safe-left));
            padding-inline-end: calc(var(--app-space-page) + var(--app-space-safe-right));
            scroll-padding-inline-start: calc(var(--app-space-page) + var(--app-space-safe-left));
            scroll-padding-inline-end: calc(var(--app-space-page) + var(--app-space-safe-right));
        }
    }

    .app-action-bar {
        anchor-name: --app-action-bar;
        position: sticky;
        inset-block-end: 0;
        z-index: var(--app-z-header);
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: var(--app-space-actions);
        padding-block-start: var(--app-space-bar-y);
        padding-block-end: calc(var(--app-space-bar-y) + var(--app-space-safe-bottom));
        padding-inline-start: calc(var(--app-space-page) + var(--app-space-safe-left));
        padding-inline-end: calc(var(--app-space-page) + var(--app-space-safe-right));
        background: var(--app-color-surface);
        box-shadow: inset 0 var(--app-border-width) 0 var(--app-color-line);
    }

    .app-action-bar > .app-reason {
        flex: 1 0 100%;
        justify-content: flex-end;
    }

    @container app-region (width < 560px) {
        .app-action-bar > * {
            flex: 1 1 0;
        }

        .app-action-bar > .app-reason {
            flex: 1 0 100%;
            justify-content: flex-start;
        }
    }

    /* Split view. */
    /* The split is a container of its own, so what sits in a pane can tell whether the other
       pane is beside it (page-header.css drops the back link then). It holds both panes with
       everything in them, so it adds no stacking context that splits a page from its lists. */
    .app-split {
        container: app-split / inline-size;
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        min-inline-size: 0;
    }

    .app-split-pane {
        display: flex;
        flex-direction: column;
        min-inline-size: 0;
    }

    .app-split[data-show="list"] > [data-pane="detail"],
    .app-split[data-show="detail"] > [data-pane="list"] {
        display: none;
    }

    @container app-region (width >= 960px) {
        .app-split {
            grid-template-columns: var(--app-split-list-width) minmax(0, 1fr);
        }

        .app-split[data-show] > .app-split-pane {
            display: flex;
        }

        .app-split > [data-pane="list"] {
            border-inline-end: var(--app-border-width) solid var(--app-color-line);
            background: var(--app-color-surface);
        }
    }

    /* From a viewport of 960: the navigation is a column and the frame stops scrolling. */
    @media (width >= 960px) {
        /* The status bar's band runs across both columns in the bars' surface, and the column
           and the header start under it, so the column's line ends where the header's does
           instead of running up through the clock. */
        .app-shell {
            block-size: var(--app-page-height);
            padding-block-start: var(--app-space-safe-top);
            background: linear-gradient(var(--app-color-surface) 0 0) top / 100% var(--app-space-safe-top) no-repeat;
        }

        .app-shell-header {
            padding-block-start: var(--app-space-bar-y);
        }

        .app-shell:not(:has(> .app-shell-nav)):has(> .app-shell-header > .app-shell-bar:empty) .app-page-body {
            padding-block-start: var(--app-space-page);
        }

        .app-shell:has(> .app-shell-nav) {
            grid-template-areas:
                "nav header"
                "nav main"
                "nav footer";
            grid-template-columns: calc(var(--app-nav-rail-width) + var(--app-space-safe-left)) minmax(0, 1fr);
        }

        .app-shell[data-nav="wide"]:has(> .app-shell-nav) {
            grid-template-columns: calc(var(--app-nav-width) + var(--app-space-safe-left)) minmax(0, 1fr);
        }

        .app-shell-tabs {
            display: none;
        }

        .app-shell-nav-head {
            display: flex;
        }

        /* The button stands over the icons, in the middle of the rail. */
        .app-shell:not([data-nav="wide"]) .app-shell-nav-head {
            justify-content: center;
        }

        /* The column reaches the bottom and the start edge of the screen: on a tablet it clears
           the gesture bar and the rounded corners, as the panel does (the band above clears the
           status bar). */
        .app-shell-nav {
            position: static;
            display: flex;
            flex-direction: column;
            gap: var(--app-space-nav-groups);
            inline-size: auto;
            block-size: auto;
            padding-block-start: var(--app-space-nav-inset);
            padding-block-end: calc(var(--app-space-nav-inset) + var(--app-space-safe-bottom));
            padding-inline-start: calc(var(--app-space-nav-inset) + var(--app-space-safe-left));
            padding-inline-end: var(--app-space-nav-inset);
            border-inline-end: var(--app-border-width) solid var(--app-color-line);
        }

        .app-shell-header:has(> [popovertarget]) {
            grid-template-columns: minmax(0, 1fr);
        }

        .app-shell-header:has(> .app-shell-bar:empty) {
            display: none;
        }

        .app-shell-main {
            overflow-y: auto;
            overscroll-behavior: contain;
        }

        .app-shell-main > .app-split {
            flex: 1 1 0;
            grid-template-rows: minmax(0, 1fr);
            min-block-size: 0;
        }

        .app-split-pane {
            overflow-y: auto;
            overscroll-behavior: contain;
        }
    }

    /* 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) {
        .app-shell-header {
            border-block-end: var(--app-border-width) solid var(--app-color-forced-line);
        }
        .app-action-bar {
            border-block-start: var(--app-border-width) solid var(--app-color-forced-line);
        }
        .app-shell-nav:popover-open {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
    }
}
