/* CraftGrid One — refined header and fixed sidebar account dock.
   The dock is a sibling of the scrollable nav, so long/open menus cannot cover it. */
.cg-brand-erp-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    margin-left: 2px;
    padding: 3px 7px;
    border: 1px solid rgba(192, 220, 246, .26);
    border-radius: 999px;
    background: rgba(210, 230, 249, .12);
    color: #e4f1ff;
    font-size: .61rem;
    font-weight: 750;
    letter-spacing: .045em;
    line-height: 1.1;
}

.cg-sidebar-profile-slot { display: none; }

@media (min-width: 992px) {
    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar > .erp-sidebar {
        display: block !important;
        padding-bottom: 88px !important; /* Scroll content can pass fully above the dock. */
    }

    /* The dock is a fixed footer of the sidebar, not part of its scroll content. */
    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar > .cg-sidebar-profile-slot {
        display: block !important;
        position: absolute !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        z-index: 1205 !important;
        box-sizing: border-box !important;
        width: 100% !important;
        margin: 0 !important;
        padding: 8px 9px max(8px, env(safe-area-inset-bottom)) !important;
        border-top: 1px solid rgba(225, 239, 255, .16);
        background: linear-gradient(180deg, rgba(23, 40, 61, .97), #1b3047 30%);
        box-shadow: 0 -5px 12px rgba(6, 18, 32, .12);
    }

    .cg-sidebar-profile-slot .cg-topbar-user-dropdown {
        display: block;
        width: 100%;
    }

    .cg-sidebar-profile-slot .craftgrid-user-button {
        display: flex !important;
        width: 100%;
        min-width: 0;
        height: auto !important;
        min-height: 44px !important;
        max-height: none !important;
        justify-content: flex-start;
        gap: 8px !important;
        padding: 6px 7px !important;
        border: 1px solid rgba(211, 230, 249, .12) !important;
        border-radius: 10px !important;
        background: rgba(255,255,255,.045) !important;
        color: #f4f8ff !important;
        text-align: left;
        box-shadow: none !important;
    }

    .cg-sidebar-profile-slot .craftgrid-user-button:hover,
    .cg-sidebar-profile-slot .craftgrid-user-button:focus-visible,
    .cg-sidebar-profile-slot .craftgrid-user-button[aria-expanded="true"] {
        background: rgba(255,255,255,.09) !important;
        border-color: rgba(160, 205, 245, .28) !important;
    }

    .cg-sidebar-profile-slot .craftgrid-user-button > span.d-none.d-md-inline {
        display: flex !important;
        flex: 1 1 auto;
        flex-direction: column;
        min-width: 0;
        overflow: hidden;
    }

    .cg-sidebar-profile-slot .craftgrid-user-name {
        display: block;
        overflow: hidden;
        color: #f5f9ff !important;
        font-size: .76rem !important;
        font-weight: 650 !important;
        line-height: 1.2 !important;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .cg-sidebar-profile-slot .craftgrid-user-role {
        display: block;
        overflow: hidden;
        margin-top: 2px;
        color: #a9c0d8 !important;
        font-size: .61rem !important;
        line-height: 1.15 !important;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .cg-sidebar-profile-slot .craftgrid-user-button.dropdown-toggle::after {
        margin-left: auto;
        flex: 0 0 auto;
        border-top-color: #a9c7e4;
    }

    /* On the compact rail, show only the avatar while retaining the same account menu. */
    html:not(.craftgrid-sidebar-expanded) .cg-sidebar-profile-slot .craftgrid-user-button {
        justify-content: center;
        padding: 7px 3px !important;
        border-color: transparent !important;
        background: transparent !important;
    }

    html:not(.craftgrid-sidebar-expanded) .cg-sidebar-profile-slot .craftgrid-user-button > span.d-none.d-md-inline,
    html:not(.craftgrid-sidebar-expanded) .cg-sidebar-profile-slot .craftgrid-user-button.dropdown-toggle::after,
    html:not(.craftgrid-sidebar-expanded) .cg-sidebar-profile-slot .cg-user-role-count-mobile {
        display: none !important;
    }

    html:not(.craftgrid-sidebar-expanded) .cg-sidebar-profile-slot .craftgrid-user-button .craftgrid-avatar {
        width: 34px !important;
        height: 34px !important;
        min-width: 34px !important;
        flex-basis: 34px !important;
    }

    .craftgrid-topbar .ms-auto > .vr { display: none !important; }

    /* Position is calculated by craftgrid-sidebar-profile-dock.js.
       Do not use `inset: auto !important`: it overrides the script's left/top values. */
    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu {
        position: fixed !important;
        z-index: 2000 !important;
        display: none;
        width: min(354px, calc(100vw - 24px));
        min-width: min(280px, calc(100vw - 24px));
        max-width: calc(100vw - 24px);
        max-height: calc(100vh - 24px);
        overflow-x: hidden;
        overflow-y: auto;
        margin: 0 !important;
        transform: none !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu.show {
        display: block !important;
        visibility: visible;
        opacity: 1;
        pointer-events: auto;
    }
}

@media (max-width: 991.98px) {
    .cg-brand-erp-badge { font-size: .56rem; padding: 3px 6px; }
    .cg-sidebar-profile-slot { display: none !important; }
}

/* ---------------------------------------------------------
   Final refinement — compact account popover and navigation rhythm.
   Desktop only: mobile account menu and drawer sizing are unchanged.
   --------------------------------------------------------- */
@media (min-width: 992px) {
    /* Tighten the sidebar rhythm in both the icon rail and expanded panel.
       Keep enough height for two-line labels and a comfortable click target. */
    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-link,
    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-group-toggle {
        margin-bottom: 2px !important;
    }

    html:not(.craftgrid-sidebar-expanded) body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-link,
    html:not(.craftgrid-sidebar-expanded) body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-group-toggle {
        min-height: 50px !important;
        gap: 2px !important;
        padding: 4px 2px !important;
    }

    html:not(.craftgrid-sidebar-expanded) body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-link-content {
        gap: 2px !important;
    }

    html:not(.craftgrid-sidebar-expanded) body.craftgrid-authenticated-shell:not(.craftgrid-login-layout):not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-link-content > i {
        height: 20px !important;
        font-size: 1.06rem !important;
    }

    html.craftgrid-sidebar-expanded body.craftgrid-authenticated-shell:not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-link,
    html.craftgrid-sidebar-expanded body.craftgrid-authenticated-shell:not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-group-toggle {
        min-height: 39px !important;
        padding-top: 7px !important;
        padding-bottom: 7px !important;
        margin-bottom: 2px !important;
    }

    html.craftgrid-sidebar-expanded body.craftgrid-authenticated-shell:not(.craftgrid-subscription-gate-layout) .desktop-sidebar .sidebar-group > .sidebar-submenu .sidebar-link {
        min-height: 33px !important;
        padding-top: 5px !important;
        padding-bottom: 5px !important;
        margin-bottom: 1px !important;
    }

    /* A compact, soft account menu. Fixed positioning is set by the docking JS;
       width/height limits here prevent the menu from covering half the dashboard. */
    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu {
        box-sizing: border-box !important;
        width: 320px !important;
        min-width: 0 !important;
        max-width: calc(100vw - 24px) !important;
        max-height: min(430px, calc(100dvh - 100px)) !important;
        padding: 4px 0 !important;
        border: 1px solid #d7e2ec !important;
        border-radius: 12px !important;
        background: #fff !important;
        box-shadow: 0 12px 30px rgba(15, 35, 58, .20), 0 2px 6px rgba(15, 35, 58, .06) !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        scrollbar-width: thin;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu > li.px-3 {
        padding-left: 11px !important;
        padding-right: 11px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu > .cg-user-context {
        padding-top: 5px !important;
        padding-bottom: 6px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-user-context-name {
        font-size: .88rem !important;
        line-height: 1.25 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu > .cg-user-roles {
        padding-top: 6px !important;
        padding-bottom: 7px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-user-roles .small {
        margin-bottom: 3px !important;
        font-size: .75rem !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-assigned-role-chip {
        padding: 3px 7px !important;
        border-radius: 7px !important;
        font-size: .70rem !important;
        line-height: 1.2 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu > li:has(.cg-subscription-menu-card) {
        padding-bottom: 6px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-subscription-menu-card {
        padding: 7px 8px !important;
        border-radius: 9px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-menu-main {
        gap: 8px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-menu-icon {
        width: 32px !important;
        height: 32px !important;
        flex-basis: 32px !important;
        border-radius: 8px !important;
        font-size: .92rem !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-overline {
        margin-bottom: 1px !important;
        font-size: .58rem !important;
        line-height: 1.15 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-menu-title {
        font-size: .84rem !important;
        line-height: 1.2 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-menu-price {
        margin-top: 1px !important;
        font-size: .68rem !important;
        line-height: 1.2 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-status {
        padding: 2px 6px !important;
        font-size: .58rem !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-menu-footer {
        margin-top: 5px !important;
        padding-top: 5px !important;
        gap: 4px 6px !important;
        font-size: .60rem !important;
        line-height: 1.2 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .cg-plan-autorenew {
        padding: 2px 4px !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .dropdown-item {
        padding: 7px 11px !important;
        color: #334b61 !important;
        font-size: .81rem !important;
        line-height: 1.2 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .dropdown-item:hover,
    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .dropdown-item:focus-visible {
        color: #0f5132 !important;
        background: #eff8f4 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu .dropdown-divider {
        margin: 4px 0 !important;
        border-top-color: #e7edf2 !important;
        opacity: 1 !important;
    }

    .cg-sidebar-profile-slot .cg-user-menu.cg-sidebar-profile-menu li > form {
        padding-left: 5px !important;
        padding-right: 5px !important;
    }
}


/* Mobile header correction: keep the CraftGrid One wordmark intact.
   The ERP pill is useful on wider headers, but on phones it competes for
   the same narrow brand width and can overlap the final letters. */
@media (max-width: 575.98px) {
    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand > .cg-brand-erp-badge {
        display: none !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand {
        max-width: 145px !important;
        flex: 0 1 auto !important;
        overflow: hidden !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand .cg-brand-grid-mark {
        flex: 0 0 34px !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand .cg-brand-copy {
        flex: 0 1 auto !important;
        min-width: 0 !important;
        overflow: visible !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand .cg-brand-copy strong {
        white-space: nowrap !important;
        letter-spacing: -.04em;
    }
}

/* Extra-narrow phones: reclaim just enough space for all header controls. */
@media (max-width: 359.98px) {
    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .container-fluid {
        padding-left: 7px !important;
        padding-right: 7px !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-mobile-menu {
        width: 36px !important;
        height: 36px !important;
        margin-right: 5px !important;
        flex: 0 0 36px !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand {
        max-width: 132px !important;
        gap: 6px !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand .cg-brand-grid-mark {
        width: 30px !important;
        height: 30px !important;
        flex-basis: 30px !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-brand .cg-brand-copy strong {
        font-size: .90rem !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .craftgrid-icon-button {
        width: 36px !important;
        height: 36px !important;
        flex: 0 0 36px !important;
    }

    body.craftgrid-authenticated-shell:not(.craftgrid-login-layout) .craftgrid-topbar .container-fluid > .ms-auto {
        gap: 5px !important;
    }
}

/* Mobile account menu: use a body-level portal so Popper and header stacking
   contexts cannot pin the dropdown to the left edge or clip it behind cards. */
@media (max-width: 767.98px) {
    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal {
        position: fixed !important;
        inset: var(--cg-mobile-account-menu-top, 76px) 12px auto 12px !important;
        z-index: 1049 !important;
        display: block !important;
        box-sizing: border-box !important;
        width: auto !important;
        min-width: 0 !important;
        max-width: none !important;
        max-height: var(--cg-mobile-account-menu-max-height, calc(100dvh - 100px)) !important;
        height: auto !important;
        margin: 0 !important;
        padding: 7px 0 !important;
        transform: none !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: contain !important;
        visibility: visible !important;
        opacity: 1 !important;
        background: #fff !important;
        border: 1px solid #d7e2ec !important;
        border-radius: 14px !important;
        box-shadow: 0 14px 34px rgba(15, 35, 58, .22), 0 2px 7px rgba(15, 35, 58, .07) !important;
        color: #243b53 !important;
        scrollbar-width: thin;
        -webkit-overflow-scrolling: touch;
    }

    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal > li,
    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal > li.px-3 {
        min-width: 0;
        padding-left: 14px !important;
        padding-right: 14px !important;
    }

    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal > .cg-user-context {
        padding-top: 5px !important;
        padding-bottom: 8px !important;
        overflow-wrap: anywhere;
    }

    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal .cg-subscription-menu-card {
        width: 100%;
        min-width: 0;
        margin: 4px 0 7px;
        padding: 10px 11px !important;
    }

    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal .dropdown-item {
        min-height: 42px;
        display: flex;
        align-items: center;
        gap: 9px;
        white-space: normal;
    }

    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal .dropdown-divider {
        margin: 6px 0 !important;
    }

    /* Keep the account menu below Bootstrap's modal backdrop/modal stack. */
    body.modal-open > .cg-user-menu.cg-mobile-account-menu-portal {
        z-index: 1049 !important;
    }
}

@media (max-width: 359.98px) {
    body.craftgrid-authenticated-shell > .cg-user-menu.cg-mobile-account-menu-portal {
        inset-inline: 8px !important;
    }
}
