/*
 * Desktop Sidebar Navigation (WordPress AdminCP style)
 *
 * Converts the top navbar into a fixed left-hand sidebar for viewports at
 * or above the navbar's lg breakpoint (992px) - the same breakpoint the
 * mobile drawer (mobile-menu.css/js) already uses as its cutoff. Below
 * that breakpoint nothing here applies: the mobile off-canvas menu is
 * completely untouched.
 *
 * Only rendered when the server added body.has-sidebar (a logged-in user
 * with a real nav to show) - see views/layouts/header.php.
 */

:root {
    --sidebar-width: 220px;
}

@media (min-width: 992px) {
    body.has-sidebar {
        margin-left: var(--sidebar-width);
    }

    body.has-sidebar .navbar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: var(--sidebar-width);
        z-index: 1030;
        padding: 0;
        overflow-y: auto;

        /* Thin, translucent macOS-style scrollbar - stays out of the way
           until you actually scroll/hover, like the native overlay
           scrollbar in Safari/macOS. Firefox uses scrollbar-width/-color;
           WebKit/Blink (Chrome, Edge, Safari) use the pseudo-elements
           below, which only Firefox/WebKit engines support, so both are
           declared side by side. */
        scrollbar-width: thin;
        scrollbar-color: rgba(255, 255, 255, 0.25) transparent;

        /* Keeps a focused link clear of the account menu pinned to the
           foot (WCAG 2.4.11; accessibility review A-1). sidebar-menu.js
           sets the real height and nudges what this misses. */
        scroll-padding-bottom: var(--account-menu-height, 5.5rem);
    }

    body.has-sidebar .navbar::-webkit-scrollbar {
        width: 6px;
    }

    body.has-sidebar .navbar::-webkit-scrollbar-track {
        background: transparent;
    }

    body.has-sidebar .navbar::-webkit-scrollbar-thumb {
        background-color: rgba(255, 255, 255, 0.25);
        border-radius: 999px;
        background-clip: padding-box;
    }

    body.has-sidebar .navbar::-webkit-scrollbar-thumb:hover {
        background-color: rgba(255, 255, 255, 0.4);
    }

    body.has-sidebar .navbar .container {
        max-width: none;
        flex-direction: column;
        align-items: stretch;
        justify-content: flex-start;
        height: 100%;
        min-height: 100%;
        padding: 0;
        margin: 0;
    }

    /* Logo, centred at the top of the sidebar. Just the mark - no site-name
       text underneath - with a hairline rule closing off the brand block
       before the nav items start. */
    body.has-sidebar .navbar-brand {
        margin: 0 !important;
        padding: 1.25rem 0.5rem 1rem;
        justify-content: center;
        text-align: center;
        white-space: normal;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }

    body.has-sidebar .navbar-brand .logo-img {
        max-height: 64px;
        margin: 0 auto;
    }

    /* The collapse is always expanded at this breakpoint (Bootstrap's own
       navbar-expand-lg rule); make it fill the rest of the sidebar. */
    body.has-sidebar .navbar-collapse {
        flex: 1 1 auto;
        display: flex;
        align-items: stretch;
        min-height: 0;
    }

    body.has-sidebar .navbar-nav {
        flex-direction: column;
        width: 100%;
        margin: 0 !important;
        /* No bottom padding: the account menu is the last item and sits
           flush with the foot of the sidebar. */
        padding-bottom: 0;
    }

    body.has-sidebar .navbar-nav .nav-item {
        width: 100%;
    }

    body.has-sidebar .navbar .nav-link {
        display: flex;
        align-items: center;
        width: 100%;
        padding: 0.7rem 1rem;
        border-left: 3px solid transparent;
        font-size: 0.92rem;
        white-space: normal;
    }

    body.has-sidebar .navbar .nav-link .sidebar-icon {
        width: 1.25rem;
        margin-right: 0.65rem;
        text-align: center;
        flex: 0 0 auto;
        opacity: 0.85;
    }

    body.has-sidebar .navbar .nav-link:hover,
    body.has-sidebar .navbar .nav-link:focus {
        background: rgba(255, 255, 255, 0.08);
        border-left-color: rgba(255, 255, 255, 0.4);
    }

    /* Active item: white overlay + the brand's red rule device, replacing
       the old teal accent. */
    body.has-sidebar .navbar .nav-link.active {
        background: rgba(255, 255, 255, 0.12);
        border-left-color: var(--theme-nav-accent, var(--rule-accent, #d90429));
        font-weight: 600;
    }

    /* Section headers (Competitions / Club / Club Management / System /
       MyBowlio) - button triggers that expand/collapse the submenu underneath, WordPress-
       AdminCP style groups rather than a floating Bootstrap dropdown. */
    body.has-sidebar .navbar .dropdown-toggle {
        width: 100%;
        background: none;
        border: none;
        cursor: pointer;
        font-family: var(--font-display, inherit);
        font-weight: 700;
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: var(--tracking-eyebrow, 0.14em);
        opacity: 0.75;
        margin-top: 0.75rem;
        padding-bottom: 0.4rem;
        /* A button centres its text by default; a name long enough to wrap
           must still line up with the one-line names above and below it. */
        text-align: left;
    }

    /* Bootstrap's own caret, repurposed as an expand/collapse chevron that
       flips to match the submenu's open state. */
    body.has-sidebar .navbar .dropdown-toggle::after {
        margin-left: auto;
        transition: transform var(--dur-fast, 0.15s) var(--ease-standard, ease);
    }

    body.has-sidebar .navbar .dropdown-toggle[aria-expanded="true"]::after {
        transform: rotate(-180deg);
    }

    body.has-sidebar .navbar .dropdown-toggle:hover,
    body.has-sidebar .navbar .dropdown-toggle:focus-visible {
        opacity: 1;
        background: transparent;
        border-left-color: transparent;
    }

    /* Bootstrap gives a focused .nav-link a faint blue glow, about 1.3:1
       on the sidebar green and clipped at both edges. A white ring drawn
       inside the button is visible (WCAG 1.4.11, 2.4.7). */
    body.has-sidebar .navbar .dropdown-toggle:focus-visible {
        /* --theme-nav-contrast is set only by a saved theme that changes
           the navigation's current text (includes/ThemeSettings.php). */
        outline: 2px solid var(--theme-nav-contrast, #fff);
        outline-offset: -2px;
        box-shadow: none;
    }

    /* Submenu renders as an indented nested list instead of a floating
       Bootstrap dropdown, collapsed by default and expanding smoothly. A
       generously oversized max-height (well beyond any real submenu's
       rendered height) transitions down to 0 rather than animating to/from
       "auto", which CSS can't transition directly. */
    body.has-sidebar .navbar .dropdown-menu {
        position: static;
        display: block;
        float: none;
        width: 100%;
        margin: 0;
        padding: 0;
        background: transparent;
        border: 0;
        border-radius: 0;
        box-shadow: none;
        overflow: hidden;
        max-height: 0;
        /* A collapsed submenu is also visibility:hidden, which takes its
           links out of the tab order and the accessibility tree - otherwise
           keyboard users tab onto links clipped to zero height with no
           visible focus (WCAG 2.4.7; a11y audit HIGH #3). The visibility
           change waits for the collapse animation to finish, and applies
           immediately on expand. */
        visibility: hidden;
        transition: max-height var(--dur-base, 0.25s) var(--ease-standard, ease),
                    visibility 0s linear var(--dur-base, 0.25s);
    }

    body.has-sidebar .navbar .dropdown-toggle[aria-expanded="true"] + .dropdown-menu {
        max-height: 32em;
        visibility: visible;
        transition: max-height var(--dur-base, 0.25s) var(--ease-standard, ease),
                    visibility 0s linear 0s;
    }

    /* Groups collapse only once sidebar-menu.js is running (it sets
       html.sidebar-js before the menu is drawn). Without it the buttons
       cannot open anything, so every group stays open: Logout and the
       rest of the menu must not depend on one script. */
    html:not(.sidebar-js) body.has-sidebar .navbar .dropdown-menu {
        max-height: none;
        visibility: visible;
        transition: none;
    }

    html:not(.sidebar-js) body.has-sidebar .navbar .dropdown-toggle {
        cursor: default;
    }

    html:not(.sidebar-js) body.has-sidebar .navbar .dropdown-toggle::after {
        display: none;
    }

    body.has-sidebar .navbar .dropdown-item {
        /* Bootstrap keeps dropdown items on one line; in a 220px column a
           system notification's detail ran off the edge. */
        white-space: normal;
        display: flex;
        align-items: center;
        width: 100%;
        padding: 0.55rem 1rem 0.55rem 2.35rem;
        font-size: 0.88rem;
        color: rgba(255, 255, 255, 0.85);
        background: transparent;
        border-left: 3px solid transparent;
    }

    body.has-sidebar .navbar .dropdown-item:hover,
    body.has-sidebar .navbar .dropdown-item:focus {
        background: rgba(255, 255, 255, 0.08);
        border-left-color: rgba(255, 255, 255, 0.4);
        color: #fff;
    }

    body.has-sidebar .navbar .dropdown-item.active {
        background: rgba(255, 255, 255, 0.12);
        border-left-color: var(--theme-menu-accent, var(--rule-accent, #d90429));
        color: #fff;
        font-weight: 600;
    }

    body.has-sidebar .navbar .dropdown-divider {
        margin: 0.35rem 1rem;
        border-color: rgba(255, 255, 255, 0.12);
    }

    /* Account menu: picture, name and club pinned to the foot of the
       sidebar, opening upwards over the links above it (like the account
       menu in many web apps). margin-top:auto pushes it down when the menu
       is short; sticky keeps it in view when the sidebar scrolls. */
    body.has-sidebar .navbar .account-menu {
        position: sticky;
        bottom: 0;
        z-index: 2;
        margin-top: auto;
        padding: 0.5rem;
        background: var(--theme-nav-bg, var(--surface-inverse, #003020));
        border-top: 1px solid rgba(255, 255, 255, 0.1);
    }

    body.has-sidebar .navbar .account-menu .account-menu__toggle {
        gap: 0.6rem;
        margin: 0;
        padding: 0.45rem 0.6rem;
        border-left: 0;
        border-radius: var(--radius-md, 10px);
        font-family: inherit;
        font-size: 0.9rem;
        font-weight: 600;
        text-transform: none;
        letter-spacing: normal;
        opacity: 1;
    }

    body.has-sidebar .navbar .account-menu .account-menu__toggle:hover,
    body.has-sidebar .navbar .account-menu .account-menu__toggle[aria-expanded="true"] {
        background: rgba(255, 255, 255, 0.08);
    }

    /* Closed, the chevron points up: the menu opens upwards. */
    body.has-sidebar .navbar .account-menu .account-menu__toggle::after {
        transform: rotate(-180deg);
    }

    body.has-sidebar .navbar .account-menu .account-menu__toggle[aria-expanded="true"]::after {
        transform: none;
    }

    body.has-sidebar .navbar .account-menu .dropdown-menu {
        position: absolute;
        left: 0.5rem;
        right: 0.5rem;
        bottom: calc(100% + 0.25rem);
        width: auto;
        background: var(--theme-menu-bg, var(--green-800, #064027));
        border: 1px solid rgba(255, 255, 255, 0.14);
        border-radius: var(--radius-md, 10px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    }

    body.has-sidebar .navbar .account-menu .dropdown-toggle[aria-expanded="true"] + .dropdown-menu {
        padding: 0.35rem 0;
    }

    body.has-sidebar .navbar .account-menu .dropdown-item {
        padding: 0.55rem 0.9rem;
    }

    /* The theme's red ring is 2.8:1 on this green and was clipped at the
       menu's edges; white, drawn inside (accessibility review A-2). */
    body.has-sidebar .navbar .account-menu .dropdown-item:focus-visible {
        outline: 2px solid #fff;
        outline-offset: -2px;
        box-shadow: none;
    }

    /* No script: every group is open (above), so the account menu sits in
       place under the button instead of covering the links above it. */
    html:not(.sidebar-js) body.has-sidebar .navbar .account-menu .dropdown-menu {
        position: static;
        border: 0;
        box-shadow: none;
        background: transparent;
    }

    html:not(.sidebar-js) body.has-sidebar .navbar .account-menu .account-menu__toggle::after {
        display: none;
    }

    /* ...and, always open, it is too tall to pin over the menu. */
    html:not(.sidebar-js) body.has-sidebar .navbar .account-menu {
        position: static;
    }

    /* The mobile toggle/fallback togglers are already hidden at this
       breakpoint by mobile-menu.css / Bootstrap; nothing else to do here. */
}

/* Avatar for the account menu, at every width (the mobile drawer copies
   the button's content). A photo, or initials on the brand gold. */
.account-avatar {
    flex: 0 0 auto;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    vertical-align: middle;
}

.account-avatar--initials {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--gold-500, #e9c46a);
    color: var(--green-900, #003020);
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1;
    letter-spacing: 0.02em;
}

.account-menu__text {
    min-width: 0;
    text-align: left;
    line-height: 1.25;
    overflow-wrap: anywhere;
}

/* Club name on its own line under the user's name. */
.account-menu__club {
    font-weight: 800;
    opacity: 0.85;
    font-size: 0.6em;
    display: block;
}
