/**
 * App shell -- the chrome shared by every authenticated page: the fixed
 * top bar, the sticky sidebar, and the main column.
 *
 * Geometry from docs/research/05-app-shell.md §5. Two things the prototype
 * got wrong are fixed here rather than reproduced:
 *  - its logo block was 216px against a 256px sidebar, so the wordmark sat
 *    41px short of the sidebar's edge and overhung it when collapsed (D §2.1).
 *    Here the logo block is the sidebar width and tracks it.
 *  - its mobile drawer declared role="dialog" aria-modal="true" but never
 *    moved focus into itself and had no focus trap (D §4). See common.js.
 */
@layer layout {

    /* --- Top bar ------------------------------------------------------- */

    .app-header {
        position: fixed;
        inset-inline: 0;
        top: 0;
        z-index: 50;
        display: flex;
        align-items: center;
        gap: var(--spacing-s3);
        /* Its own height, not the shell's: under the impersonation banner the
           shell token grows by the banner and the header must not. */
        height: var(--shell-header-height-base);
        padding-inline: var(--spacing-s3);
        background-color: var(--surface-colour-brand);
        color: var(--text-colour-white);
    }

    /* --- Impersonation banner -------------------------------------------
       A platform admin signed in as somebody else (App\Admin\Impersonation)
       sees this above the header on every page. It is fixed, like the
       header, and the shell's one height token grows by it, so the header,
       the sidebar and the main column all move down together. Warning
       colours: it is the one state a person should never forget they are
       in. On a phone the explanation gives way to the name and the button. */
    html.is-impersonating {
        --shell-header-height: calc(var(--shell-header-height-base) + var(--impersonation-banner-height));
    }

    .is-impersonating .app-header {
        top: var(--impersonation-banner-height);
    }

    .impersonation-banner {
        position: fixed;
        inset-inline: 0;
        top: 0;
        z-index: 51;
        display: flex;
        align-items: center;
        gap: var(--spacing-s3);
        height: var(--impersonation-banner-height);
        padding-inline: var(--spacing-s3);
        background-color: var(--status-warning-solid);
        color: var(--status-warning-on-solid);
    }

    .impersonation-banner-text {
        flex: 1;
        min-width: 0;
        margin: 0;
        overflow: hidden;
        font-size: var(--font-size-xs);
        line-height: 1.3;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .impersonation-banner-text strong {
        font-weight: var(--font-weight-bold);
    }

    .impersonation-banner-form {
        flex: none;
        margin: 0;
    }

    .impersonation-banner .btn {
        border-color: currentColor;
        background-color: transparent;
        color: inherit;
    }

    .impersonation-banner .btn:hover {
        background-color: var(--surface-colour-on-brand-soft);
    }

    @media (max-width: 640px) {
        .impersonation-banner-explanation {
            display: none;
        }
    }

    .app-header-brand {
        display: flex;
        flex: none;
        align-items: center;
        gap: var(--spacing-s2);
        color: inherit;
        text-decoration: none;
    }

    .app-header-mark {
        display: grid;
        place-items: center;
        width: 32px;
        height: 32px;
        border-radius: var(--radius-m);
        background-color: var(--surface-colour-on-brand);
        font-size: var(--font-size-r);
        font-weight: var(--font-weight-extrabold);
        line-height: 1;
    }

    .app-header-wordmark {
        font-size: var(--font-size-m);
        font-weight: var(--font-weight-extrabold);
        letter-spacing: -0.01em;
        line-height: 1;
    }

    .app-header-plan {
        border-radius: var(--radius-s);
        padding: 2px var(--spacing-s1);
        background-color: var(--surface-colour-accent);
        font-size: var(--font-size-2xs);
        font-weight: var(--font-weight-bold);
        text-transform: uppercase;
        letter-spacing: 0.04em;
        line-height: 1;
    }

    .app-header-actions {
        display: flex;
        align-items: center;
        gap: var(--spacing-s4);
        margin-inline-start: auto;
    }

    /* The prototype rendered this as a dead <span> sitting exactly where
       every SaaS app puts its account menu (D-01); charterhub made it a
       button with no menu behind it, which was no better. It is a link to
       /account here -- the page the sidebar's name opens -- so it needs the
       link underline and colour taken off to look as it did. */
    .app-header-account {
        display: grid;
        place-items: center;
        width: 36px;
        height: 36px;
        border-radius: var(--radius-pill);
        background-color: var(--surface-colour-on-brand);
        color: inherit;
        font-size: var(--font-size-r);
        font-weight: var(--font-weight-bold);
        text-decoration: none;
    }

    .app-header-account:hover {
        background-color: var(--surface-colour-on-brand-strong);
    }

    .app-nav-toggle {
        display: grid;
        place-items: center;
        width: 40px;
        height: 40px;
        flex: none;
        border: 0;
        border-radius: var(--radius-m);
        background: transparent;
        color: inherit;
        cursor: pointer;
    }

    .app-nav-toggle:hover {
        background-color: var(--surface-colour-on-brand-soft);
    }

    /* --- Shell body ---------------------------------------------------- */

    .app-shell {
        display: flex;
        align-items: flex-start;
        padding-top: var(--shell-header-height);
    }

    .app-main {
        flex: 1;
        min-width: 0;
        min-height: calc(100vh - var(--shell-header-height));
        background-color: var(--surface-colour-sunken);
    }

    /* --- Sidebar ------------------------------------------------------- */

    .app-sidebar {
        position: sticky;
        top: var(--shell-header-height);
        z-index: 40;
        display: flex;
        flex: none;
        flex-direction: column;
        width: var(--shell-sidebar-width);
        height: calc(100vh - var(--shell-header-height));
        border-inline-end: 1px solid var(--border-colour);
        background-color: var(--surface-colour);
        transition: width 0.2s var(--transition-timing);
    }

    .app-sidebar-nav {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        padding: var(--spacing-s2);
    }

    .app-sidebar-group-label {
        padding: var(--spacing-s3) var(--spacing-s3) var(--spacing-s1);
        color: var(--text-colour-faint);
        font-size: var(--font-size-2xs);
        font-weight: var(--font-weight-semibold);
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }

    .app-sidebar-item {
        position: relative;
        display: flex;
        align-items: center;
        gap: var(--spacing-s3);
        padding: 10px var(--spacing-s3);
        border-radius: var(--radius-m);
        color: var(--text-colour-light);
        font-size: var(--font-size-r);
        text-decoration: none;
        transition: background-color var(--transition-duration) var(--transition-timing),
                    color var(--transition-duration) var(--transition-timing);
    }

    .app-sidebar-item:hover {
        background-color: var(--surface-colour-sunken);
        color: var(--text-colour-dark);
    }

    .app-sidebar-item.is-active {
        background-color: var(--surface-colour-brand-soft);
        color: var(--text-colour-brand-strong);
        font-weight: var(--font-weight-semibold);
    }

    .app-sidebar-item-icon {
        display: grid;
        place-items: center;
        flex: none;
        color: var(--text-colour-faint);
    }

    .app-sidebar-item.is-active .app-sidebar-item-icon {
        color: var(--text-colour-brand);
    }

    .app-sidebar-item-label {
        flex: 1;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* The bell's unread count, at the item's trailing edge. */
    .app-sidebar-item-count {
        flex: none;
        min-width: 20px;
        padding: 1px 6px;
        border-radius: var(--radius-pill);
        background-color: var(--surface-colour-brand);
        color: var(--text-colour-white);
        font-size: var(--font-size-xs);
        font-weight: var(--font-weight-bold);
        line-height: 18px;
        text-align: center;
    }

    /* --- Sidebar footer ------------------------------------------------ */

    /* The development group: DEV only, rendered by sidebar.twig between the
       navigation and the user. Same items as the nav so every collapsed-state
       rule applies to it unchanged; the rule above it is what separates it
       from the product's own navigation, which `.app-sidebar-nav` scrolls
       independently of. */
    .app-sidebar-dev {
        flex: none;
        padding: 0 var(--spacing-s2) var(--spacing-s2);
        border-top: 1px solid var(--border-colour-soft);
    }

    .app-sidebar-user {
        display: flex;
        align-items: center;
        gap: var(--spacing-s3);
        padding: var(--spacing-s3) var(--spacing-s4);
        border-top: 1px solid var(--border-colour-soft);
    }

    .app-sidebar-user-avatar {
        display: grid;
        place-items: center;
        flex: none;
        width: 36px;
        height: 36px;
        border-radius: var(--radius-pill);
        background-color: var(--surface-colour-brand);
        color: var(--text-colour-white);
        font-size: var(--font-size-xs);
        font-weight: var(--font-weight-bold);
    }

    .app-sidebar-user-detail {
        flex: 1;
        min-width: 0;
    }

    .app-sidebar-user-name {
        overflow: hidden;
        color: var(--text-colour-regular);
        font-size: var(--font-size-r);
        font-weight: var(--font-weight-medium);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* The name links to /account without looking like a nav item: it keeps
       the name's colour and gains an underline only when pointed at. */
    .app-sidebar-user-link {
        color: inherit;
        text-decoration: none;
    }

    .app-sidebar-user-link:hover,
    .app-sidebar-user-link:focus-visible,
    .app-sidebar-user-link[aria-current="page"] {
        text-decoration: underline;
    }

    .app-sidebar-user-meta {
        overflow: hidden;
        color: var(--text-colour-faint);
        font-size: var(--font-size-xs);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-sidebar-signout:hover {
        background-color: var(--status-error-surface);
        color: var(--status-error-text);
    }

    .sidebar-collapsed .app-sidebar-user form {
        display: none;
    }

    .app-sidebar-collapse {
        display: flex;
        align-items: center;
        gap: var(--spacing-s2);
        width: 100%;
        padding: 10px var(--spacing-s5);
        border: 0;
        border-top: 1px solid var(--border-colour-soft);
        background: transparent;
        color: var(--text-colour-faint);
        font-size: var(--font-size-xs);
        font-weight: var(--font-weight-medium);
        cursor: pointer;
    }

    .app-sidebar-collapse:hover {
        background-color: var(--surface-colour-sunken);
        color: var(--text-colour-regular);
    }

    /* --- Collapsed state ------------------------------------------------
       Desktop only. Below 1024px the sidebar is a drawer and the collapse
       control is hidden, but the preference persists in localStorage -- so
       without this guard, collapsing on desktop and then narrowing the window
       opens the drawer as a 68px stub with its labels hidden. These selectors
       carry two classes to the drawer rules' one, so they win on specificity
       regardless of source order. */
    @media (min-width: 1024px) {

    .sidebar-collapsed .app-sidebar {
        width: var(--shell-sidebar-collapsed-width);
    }

    .sidebar-collapsed .app-sidebar-item {
        justify-content: center;
        padding-inline: 0;
    }

    /* Labels are hidden, not removed -- the prototype dropped them from the
       DOM and fell back to a title attribute for the accessible name (D §3.3). */
    .sidebar-collapsed .app-sidebar-item-label,
    .sidebar-collapsed .app-sidebar-group-label-text,
    .sidebar-collapsed .app-sidebar-user-detail,
    .sidebar-collapsed .app-sidebar-collapse-label {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
    }

    /* Collapsed, the count would not fit beside the icon: a dot on the
       bell says there is something, and the label still says how much. */
    .sidebar-collapsed .app-sidebar-item-count {
        position: absolute;
        top: 6px;
        right: 14px;
        min-width: 0;
        width: 8px;
        height: 8px;
        padding: 0;
        overflow: hidden;
        font-size: 0;
    }

    .sidebar-collapsed .app-sidebar-group-label {
        padding-block: var(--spacing-s3) 0;
    }

    .sidebar-collapsed .app-sidebar-user,
    .sidebar-collapsed .app-sidebar-collapse {
        justify-content: center;
        padding-inline: 0;
    }

    .sidebar-collapsed .app-header-brand-text {
        display: none;
    }

    }

    /* --- Below 1024px: the sidebar becomes a drawer --------------------- */

    @media (min-width: 1024px) {
        .app-nav-toggle {
            display: none;
        }

        .app-header-brand {
            width: var(--shell-sidebar-width);
        }

        .sidebar-collapsed .app-header-brand {
            width: var(--shell-sidebar-collapsed-width);
        }
    }

    @media (max-width: 1023px) {
        .app-sidebar {
            position: fixed;
            inset-block: var(--shell-header-height) 0;
            inset-inline-start: 0;
            width: var(--shell-sidebar-width);
            height: auto;
            box-shadow: var(--elevation-4);
            transform: translateX(-100%);
            transition: transform 0.2s var(--transition-timing);
        }

        .app-sidebar.is-open {
            transform: translateX(0);
        }

        .app-sidebar-collapse {
            display: none;
        }

        .app-sidebar-backdrop {
            position: fixed;
            inset-block: var(--shell-header-height) 0;
            inset-inline: 0;
            z-index: 39;
            background-color: var(--overlay-colour);
        }
    }

    /* --- Page header band ---------------------------------------------- */

    .page-band {
        border-bottom: 1px solid var(--border-colour);
        background-color: var(--surface-colour);
        padding: var(--gutter-s7) var(--gutter-page) 0;
    }

    /* Sticky under the fixed header from 641px, so a record's title, its
       facts and its actions stay in view down a long page. Not on a phone: a
       band holding a title and a row of facts, or /deal's chevron strip, would
       permanently take a third to half of a 390px-tall screen.

       z-index 30 puts it above everything in the page body that raises
       itself (row-link overlays and sticky kebab cells, 0-2) and below the
       sidebar (40), the header (50) and every popover (55+). It also makes the
       band a stacking context, which caps the flyouts that hang off it at 30:
       they still paint over the page body, and none of them reaches the
       sidebar. */
    @media (min-width: 641px) {
        .page-band {
            position: sticky;
            top: var(--shell-header-height);
            z-index: 30;
        }
    }

    .page-band-row {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: var(--spacing-s3);
        margin-bottom: var(--spacing-s4);
    }

    /* A page title with the badges that qualify it -- "New enquirer" beside
       a person's name on /enquiry/N. Baseline-aligned so a badge sits on the
       title's line rather than centred against its cap height, and wrapping,
       so on a phone the badges drop under a long name instead of squeezing
       it. */
    .page-title-group {
        display: flex;
        flex-wrap: wrap;
        align-items: baseline;
        gap: var(--spacing-s2) var(--spacing-s3);
        min-width: 0;
    }

    .page-title {
        font-size: var(--font-size-xl);
        font-weight: var(--font-weight-semibold);
        color: var(--text-colour-regular);
    }

    .page-body {
        padding: var(--gutter-s5) var(--gutter-page);
    }

    /* A single-column page that should not stretch to 1600px: the delete
       confirmation, and whatever else asks one question. */

    .page-narrow {
        max-width: 560px;
        margin-inline: auto;
        padding: var(--gutter-s5) var(--gutter-page);
    }
}
