/* =========================================================================
   Shell: skip link, app header, account menu, App navigation (staff side menu, vendor tab bar)
   and footer. Loaded after main.css by the staff and vendor portals, the sign-in pages and the
   status/changelog pages (they use the logo, header and footer). Values: ux.md section 3.
   Colours come only from tokens (main.css token blocks; white on the navy header is Bootstrap's
   --bs-white / --bs-white-rgb), so both themes work (AC-18).
   ========================================================================= */

/* ---------- Skip link (ux.md 7.2, DR-12) ---------- */
.skip-link {
    position: absolute;
    left: 0.75rem;
    top: -4rem;
    z-index: 1100;
    padding: 0.5rem 0.85rem;
    border-radius: 0.5rem;
    background: var(--app-surface);
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
    box-shadow: var(--app-shadow);
}

.skip-link:focus {
    top: 0.75rem;
    outline: 2px solid var(--app-focus);
    outline-offset: 2px;
}

main:focus {
    outline: none; /* reached through the skip link only; the link itself shows focus */
}

/* ---------- App header ---------- */
/* Navy in both themes (the brand band); white text on it is 11:1 or better. Not sticky. */
.app-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    min-height: 3.75rem;
    padding: var(--space-2) 1.25rem;
    background: linear-gradient(90deg, var(--app-header-from), var(--app-header-to));
    color: var(--bs-white);
}

.app-brand {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
    color: var(--bs-white);
    text-decoration: none;
    border-radius: 0.5rem;
}

.app-brand:hover {
    color: var(--bs-white);
}

.app-brand:focus-visible {
    outline: 2px solid var(--bs-white);
    outline-offset: 2px;
}

.app-brand-text {
    min-width: 0;
}

.app-logo {
    flex: 0 0 auto;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 0.6rem;
    overflow: hidden;
    display: grid;
    place-items: center;
    /* Same navy as the logo artwork (taken from the loyalty card), so the mark sits on its own tile. */
    background-color: var(--brand-logo-tile);
    box-shadow: inset 0 0 0 1px rgba(var(--bs-white-rgb), 0.18);
}

/* People First Healthcare mark, cropped from the loyalty card artwork (uploads/stock_images/card_front.png). */
.app-logo-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.app-title {
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.2;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-subtitle {
    font-size: 0.7rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: rgba(var(--bs-white-rgb), 0.75);
}

.app-user {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex: 0 0 auto;
}

.app-user-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    height: 2.25rem;
    padding: 0 0.65rem 0 0.25rem;
    border: 1px solid rgba(var(--bs-white-rgb), 0.22);
    border-radius: 999px;
    background: rgba(var(--bs-white-rgb), 0.08);
    color: var(--bs-white);
    font-size: var(--fs-sm);
    cursor: pointer;
    transition: background-color 120ms ease;
}

.app-user-chip:hover,
.app-user-chip[aria-expanded="true"] {
    background: rgba(var(--bs-white-rgb), 0.16);
}

.app-user-chip:focus-visible,
.app-logout:focus-visible {
    outline: 2px solid var(--bs-white);
    outline-offset: 2px;
}

.app-user-name {
    max-width: 12rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-user-caret {
    font-size: 0.65rem;
    opacity: 0.85;
}

.app-avatar {
    flex: 0 0 auto;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--bs-white);
    color: var(--brand-navy);
    font-weight: 700;
    font-size: 0.8rem;
    text-transform: uppercase;
}

/* Role badge: the database style (ng-attr-style, A-8) wins over these defaults. */
.app-role {
    font-size: 0.72rem;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    background: var(--bs-white);
    color: var(--brand-navy);
    font-weight: 600;
    white-space: nowrap;
}

.app-logout {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    height: 2.25rem;
    padding: 0 0.85rem;
    border: 1px solid rgba(var(--bs-white-rgb), 0.35);
    border-radius: 999px;
    background: transparent;
    color: var(--bs-white);
    font-size: var(--fs-sm);
    transition: background-color 120ms ease, border-color 120ms ease;
}

.app-logout:hover {
    background: var(--brand-red);
    border-color: var(--brand-red);
    color: var(--bs-white);
}

/* ---------- Account menu (staff, ux.md 3.3) ---------- */
.app-account-menu {
    --bs-dropdown-bg: var(--app-surface);
    --bs-dropdown-color: var(--app-text);
    --bs-dropdown-border-color: var(--app-border);
    --bs-dropdown-border-radius: 0.75rem;
    --bs-dropdown-padding-x: 0.5rem;
    --bs-dropdown-padding-y: 0.5rem;
    --bs-dropdown-divider-bg: var(--app-border);
    --bs-dropdown-divider-margin-y: 0.4rem;
    --bs-dropdown-link-color: var(--app-text);
    --bs-dropdown-link-hover-color: var(--app-text);
    --bs-dropdown-link-hover-bg: var(--app-hover);
    --bs-dropdown-link-active-color: var(--app-text);
    --bs-dropdown-link-active-bg: var(--app-hover);
    width: 18rem;
    max-width: calc(100vw - 1.5rem);
    box-shadow: var(--app-shadow);
    z-index: 1030; /* above the sticky menu bar (1020), below modals */
}

.app-account-head {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 0.35rem 0.5rem;
}

.app-avatar--lg {
    width: 2.5rem;
    height: 2.5rem;
    font-size: 1rem;
    background: var(--btn-primary-bg);
    color: var(--bs-white);
}

.app-account-id {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.2rem;
    min-width: 0;
}

.app-account-name {
    max-width: 100%;
    font-size: var(--fs-md);
    font-weight: 600;
    color: var(--app-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-account-item {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    min-height: 2.5rem;
    padding: 0 0.75rem;
    border-radius: 0.5rem;
    font-size: var(--fs-md);
    font-weight: 500;
}

.app-account-item > i {
    flex: 0 0 1.25rem;
    text-align: center;
    color: var(--accent);
}

.app-account-item:focus-visible {
    outline: 2px solid var(--app-focus);
    outline-offset: -2px;
}

/* Dark mode switch: state is also the knob position and the filled moon, not colour only. */
.theme-switch-track {
    position: relative;
    flex: 0 0 auto;
    margin-left: auto;
    width: 2.25rem;
    height: 1.25rem;
    border: 1px solid var(--app-border-strong);
    border-radius: 999px;
    background: var(--app-surface-2);
    transition: background-color 120ms ease, border-color 120ms ease;
}

.theme-switch-thumb {
    position: absolute;
    top: 50%;
    left: 0.15rem;
    width: 0.8rem;
    height: 0.8rem;
    border-radius: 50%;
    background: var(--app-muted);
    transform: translateY(-50%);
    transition: transform 120ms ease, background-color 120ms ease;
}

.theme-switch[aria-checked="true"] .theme-switch-track {
    background: var(--btn-primary-bg);
    border-color: var(--btn-primary-bg);
}

.theme-switch[aria-checked="true"] .theme-switch-thumb {
    background: var(--bs-white);
    transform: translate(1rem, -50%);
}

/* ---------- App navigation ---------- */
/* Base: a horizontal tab bar under the header (the vendor portal's nav, ux.md 3.4). */
.app-nav {
    padding: 0;
    background-color: var(--app-surface);
    border-bottom: 1px solid var(--app-border);
    box-shadow: var(--app-shadow-sm);
    position: sticky;
    top: 0;
    z-index: 1020;
}

.app-nav .navbar-nav .nav-link {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.85rem;
    color: var(--app-text);
    font-weight: 500;
    font-size: var(--fs-md);
    border-bottom: 3px solid transparent;
    white-space: nowrap;
    transition: background-color 120ms ease, color 120ms ease;
}

.app-nav .navbar-nav .nav-link:hover {
    color: var(--accent);
    background-color: var(--app-hover);
}

.app-nav .navbar-nav .nav-link.active {
    color: var(--accent);
    border-bottom-color: var(--accent-red);
    font-weight: 600;
}

.app-nav .nav-link:focus-visible {
    outline: 2px solid var(--app-focus);
    outline-offset: -2px;
    box-shadow: none;
}

/* ---------- Staff side menu (Contract §9, ux.md 3.1 and 3.2) ---------- */
/* Layout wrapper (portal.ejs): header, then .app-shell = nav + main, then footer. */
.app-shell {
    flex: 1 0 auto;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.app-shell > main {
    min-width: 0;
}

/* Full-window tabs (FILL_TABS in public/js/staff/portal.js): main is inside .app-shell, so the
   fill chain runs body -> .app-shell -> main, and main stays the only scrolling area. */
body:has(main.main-fill) {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

body:has(main.main-fill) > .app-shell {
    flex: 1 1 0;
    min-height: 0;
}

.app-shell > .app-nav {
    flex-direction: column;
    align-items: stretch;
    flex-wrap: nowrap;
}

.app-nav-inner {
    width: 100%;
}

.app-shell .app-nav .navbar-nav {
    flex-direction: column;
    flex-wrap: nowrap;
}

.app-shell .app-nav .navbar-nav .nav-link {
    position: relative;
    gap: 0.65rem;
    height: 2.5rem;
    margin: 1px 0;
    padding: 0 0.75rem;
    border: 0;
    border-radius: 0.5rem;
    color: var(--app-text);
}

.app-shell .app-nav .nav-link > i,
.app-nav-current > i {
    flex: 0 0 1.25rem;
    text-align: center;
}

.app-nav-label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.app-shell .app-nav .navbar-nav .nav-link:hover {
    color: var(--app-text);
    background-color: var(--app-hover);
}

/* Active: fill, accent text, bold and a red bar (not colour only). */
.app-shell .app-nav .navbar-nav .nav-link.active {
    color: var(--accent);
    background-color: var(--nav-active-bg);
    font-weight: 600;
}

.app-shell .app-nav .navbar-nav .nav-link.active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 50%;
    width: 3px;
    height: 1rem;
    border-radius: 3px;
    background: var(--accent-red);
    transform: translateY(-50%);
}

/* Group headings: not focusable, hairline above every heading but the first. */
.app-nav-group {
    list-style: none;
}

.app-nav-group-label {
    display: block;
    padding: 1rem 0.75rem 0.35rem;
    font-size: var(--fs-xs);
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--app-muted);
}

.app-nav-group ~ .app-nav-group {
    margin-top: 0.5rem;
    border-top: 1px solid var(--app-border);
}

/* Below 992 px: sticky top bar; the toggle shows the current tab; the menu opens as an overlay panel. */
@media (max-width: 991.98px) {
    .app-nav-inner {
        position: relative;
    }

    .app-nav-toggle {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        width: 100%;
        min-height: 3rem;
        padding: 0 1rem;
        border: 0;
        border-radius: 0;
        color: var(--app-text);
        font-size: var(--fs-md);
        font-weight: 600;
        text-align: left;
    }

    .app-nav-toggle:focus {
        box-shadow: none;
    }

    .app-nav-toggle:focus-visible {
        outline: 2px solid var(--app-focus);
        outline-offset: -2px;
    }

    .app-nav-toggle-icon {
        flex: 0 0 1.25rem;
        font-size: 1.1rem;
        color: var(--app-muted);
    }

    .app-nav-current {
        flex: 1 1 auto;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        min-width: 0;
        color: var(--accent);
    }

    .app-nav-collapse {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        max-height: calc(100dvh - 6.75rem); /* window minus header and bar */
        overflow-y: auto;
        padding: 0.5rem;
        background: var(--app-surface);
        border-bottom: 1px solid var(--app-border);
        box-shadow: var(--app-shadow);
    }

    .app-shell .app-nav .navbar-nav .nav-link {
        height: 2.75rem;
    }

    .app-subtitle {
        display: none;
    }
}

/* 992 px and wider: fixed-width left column next to main; the list sticks under the window top
   and scrolls inside itself only if it is taller than the window (a sidebar, not page content). */
@media (min-width: 992px) {
    .app-shell {
        flex-direction: row;
        align-items: stretch;
    }

    .app-shell > .app-nav {
        flex: 0 0 15.5rem;
        width: 15.5rem;
        position: relative;
        top: auto;
        z-index: auto;
        border-bottom: 0;
        border-right: 1px solid var(--app-border);
        box-shadow: none;
        min-height: 0;
    }

    .app-shell > .app-nav > .app-nav-inner {
        padding: 0.75rem 0.5rem;
    }

    /* Full-window tabs: the page cannot scroll, so a menu taller than the space left scrolls inside
       itself (the sidebar exception in the spec's "Side menu" section). */
    body:has(main.main-fill) .app-shell > .app-nav {
        overflow-y: auto;
        scrollbar-width: thin;
    }

    .app-shell > .app-nav .navbar-collapse {
        flex-direction: column;
        align-items: stretch;
    }

    .app-shell > main {
        flex: 1 1 0;
    }
}

/* Tall enough windows (the full menu is about 47rem): the menu sticks while the page scrolls.
   On shorter windows it scrolls with the page instead, so a scrolling page never shows a second
   scrollbar in the menu. */
@media (min-width: 992px) and (min-height: 800px) {
    .app-shell > .app-nav > .app-nav-inner {
        position: sticky;
        top: 0;
        max-height: 100vh;
        max-height: 100dvh;
        overflow-y: auto;
        scrollbar-width: thin;
    }

    body:has(main.main-fill) .app-shell > .app-nav > .app-nav-inner {
        position: static;
        max-height: none;
        overflow: visible;
    }
}

@media (min-width: 992px) and (max-width: 1199.98px) {
    .app-shell > .app-nav {
        flex-basis: 14rem;
        width: 14rem;
    }
}

/* ---------- Footer ---------- */
.app-footer {
    flex-shrink: 0;
    text-align: center;
    padding: 0.6rem 1rem;
    background-color: transparent;
    border-top: 1px solid var(--app-border);
    color: var(--app-muted);
    font-size: var(--fs-sm);
}

.app-footer p,
.app-footer-text {
    margin: 0;
}

.app-footer a,
.app-footer .changelog-link {
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
}

.app-footer a:hover {
    text-decoration: underline;
}

.app-footer a:focus-visible {
    outline: 2px solid var(--app-focus);
    outline-offset: 2px;
    border-radius: 0.25rem;
}

/* ---------- Phones (< 576 px) ---------- */
@media (max-width: 575.98px) {
    .app-header {
        min-height: 3.5rem;
        padding: var(--space-2) 0.85rem;
    }

    /* Chip: avatar and caret only; Logout: icon only, 44 px square. */
    .app-user-chip .app-user-name,
    .app-user-chip .app-role,
    .app-logout-label {
        display: none;
    }

    .app-user-chip {
        height: 2.75rem;
        padding: 0 0.6rem 0 0.45rem;
    }

    .app-logout {
        width: 2.75rem;
        height: 2.75rem;
        padding: 0;
    }

    /* Footer: two short lines instead of a separator stranded at a line end. */
    .app-footer-text > span:not(.app-footer-sep) {
        display: block;
    }

    .app-footer-sep {
        display: none;
    }
}

/* ---------- Reduced motion (AC-22) ---------- */
@media (prefers-reduced-motion: reduce) {
    .app-user-chip,
    .app-logout,
    .app-nav .navbar-nav .nav-link,
    .theme-switch-track,
    .theme-switch-thumb {
        transition: none;
    }

    .app-nav .collapsing {
        transition: none;
    }
}
