@layer components {
    html:has(.panel[open]) {
        overflow: hidden;

        /* only where the content still fits beside the panel; narrower screens get it on top */
        @media (width >= 1400px) {
            --page-push: var(--panel-width);
        }
    }

    /* a sub page: a dialog at /<data-page> that the page's own component fills */
    @scope (.panel) to (.panel-body > *) {
        :scope {
            --panel-pad: 32px;
            /* inside a panel the page is the panel, so the footer copied in lays out for its width */
            container: page / inline-size;
            position: fixed;
            inset: 0 0 0 auto;
            /* without javascript the panel stays out of the top layer, and comes first in the page */
            z-index: 20;
            width: var(--panel-width);
            max-width: none;
            height: 100%;
            max-height: none;
            margin: 0;
            padding: 0;
            border: 0;
            border-inline-start: var(--border);
            background: var(--bg);
            color: var(--fg);
            box-shadow: var(--shadow);
            display: flex;
            flex-direction: column;
            overflow-y: auto;
            overscroll-behavior: contain;
            outline: none;
            translate: 100% 0;
            transition:
                translate var(--t-base) var(--ease),
                display var(--t-base) allow-discrete,
                overlay var(--t-base) allow-discrete;

            &:not([open]) {
                display: none;
            }

            &[open] {
                translate: 0 0;

                @starting-style {
                    translate: 100% 0;
                }
            }

            &::backdrop {
                background: transparent;
                transition:
                    background var(--t-base) ease,
                    display var(--t-base) allow-discrete,
                    overlay var(--t-base) allow-discrete;
            }

            &[open]::backdrop {
                background: var(--scrim);

                @starting-style {
                    background: transparent;
                }
            }

            @media (width < 768px) {
                --panel-pad: var(--edge);
                width: 100%;
                border-inline-start: 0;
            }
        }

        .panel-header {
            position: sticky;
            inset-block-start: 0;
            z-index: 1;
            flex: none;
            display: flex;
            align-items: center;
            height: var(--bar-height);
            padding-inline: var(--panel-pad);
            background: var(--bg);
            border-block-end: var(--border);
        }

        /* phones: the panel covers the site header, so this bar carries the logo instead of the title */
        .panel-logo {
            @media (width >= 768px) {
                display: none;
            }
        }

        .panel-logo-img {
            display: block;
            height: 23.4px;
        }

        /* the title and its byline; from tablet up they sit in the bar, laid over it, and on phones they open the content */
        .panel-heading {
            display: grid;
            align-content: center;
            gap: 8px;
            padding-block-start: 28px;

            @media (width >= 768px) {
                position: sticky;
                inset-block-start: 0;
                z-index: 2;
                width: fit-content;
                height: var(--bar-height);
                margin-block-start: calc(-1 * var(--bar-height));
                padding-block: 0;
            }
        }

        .panel-title {
            @media (width >= 768px) {
                font-size: var(--text-xl);
            }
        }

        .panel-close {
            width: 40px;
            height: 40px;
            margin-inline-start: auto;
            display: grid;
            place-items: center;
            padding: 0;
            border: 1px solid var(--fg);
            border-radius: 50%;
            background: none;
            color: var(--fg);
            cursor: pointer;
            transition:
                background var(--t-fast),
                color var(--t-fast);

            .icon {
                width: 12px;
                height: 12px;
            }

            &:hover {
                background: var(--fg);
                color: var(--bg);
            }

            @media (width < 768px) {
                border-color: transparent;
            }
        }

        .panel-article {
            flex: 1;
            padding-inline: var(--panel-pad);
            font-size: var(--text-sm);
        }

        .panel-intro {
            display: grid;
            gap: 10px;
            padding-block: 28px;
            border-block-end: var(--border);
        }

        /* the date in the byline is the page's permalink */
        .panel-date {
            text-decoration: none;

            &:hover {
                text-decoration: underline;
            }
        }

        .panel-body {
            padding-block-start: 28px;
        }

        /* only phones, where the panel covers the page and its footer */
        :scope > .footer {
            padding-inline: var(--panel-pad);
            border-block-start: var(--border);

            @media (width >= 768px) {
                display: none;
            }
        }
    }
}

@layer components {
    /* a block of a page with a divider under it; its headings put trailing small text at the far end */
    @scope (.section) {
        :scope {
            display: grid;
            gap: 14px;
            padding-block-end: 28px;
            border-block-end: var(--border);

            .section + & {
                padding-block-start: 28px;
            }

            &:last-child {
                border-block-end: 0;
            }

            /* runs straight into the next section: no divider, the next one's padding is the gap */
            &.section--joined {
                padding-block-end: 0;
                border-block-end: 0;
            }
        }

        :is(h3, h4) {
            display: flex;
            align-items: baseline;
            justify-content: space-between;
            gap: 12px;
        }
    }

    /* figures as a dl: a small label over a big number; compact is a ruled row of plain values */
    @scope (.stats) {
        :scope {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px 16px;

            &.stats--compact {
                grid-template-columns: repeat(3, 1fr);
                gap: 16px;
                margin-block-start: 4px;
                padding-block-start: 14px;
                border-block-start: var(--border);
            }
        }

        dt {
            margin-block-end: 6px;
            font-size: var(--text-xs);
            color: var(--muted);
        }

        dd b {
            font-size: var(--text-2xl);
            font-weight: 700;
        }

        dd small {
            display: block;
            margin-block-start: 4px;
            font-size: var(--text-xs);
            color: var(--muted);
        }

        :scope.stats--compact dd {
            font-weight: 500;
        }

        .stats-wide {
            grid-column: 1 / -1;
        }
    }
}
