/*
 * KCM Canada - navigation fix (2026-09-29)
 * Loaded after style.css by kcmcanada_enqueue_menu_fix() in functions.php, with its own
 * filemtime version (see the style_loader_src note there). Max Mega Menu's responsive
 * breakpoint is 767px (Mega Menu > Menu Themes > default), matching the Bootstrap-xs
 * mobile block in style.css, so "mobile" below means <= 767px for both.
 *
 * Why: Max Mega Menu 3.10 renders the toggle as <button class="mega-toggle-standard
 * mega-toggle-label"> and paints the hamburger as that button's ::after. style.css hides
 * ".mega-toggle-label" (meant for the old text-only <span>), which hid the whole button.
 * Rule here: never hide the toggle button - only visually hide its MENU text spans.
 */

@media (max-width: 767px) {

    /* ---------- no sideways page overflow on phones ----------
       Mobile browsers widen the layout viewport to fit over-wide content, which pushes the
       position:fixed toggle off the right edge (home page at 320-359px). overflow-x:clip (not
       hidden) creates no scroll container, so window scrolling/sticky elements are unaffected. */
    html,
    body {
        overflow-x: clip !important;
    }

    /* ---------- fixed mobile header: logo row + (when open) menu + regions, scrolls as one ---------- */
    header.banner,
    html body header.banner {
        display: flex !important;
        flex-direction: column !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    header.banner .header-main { order: 1; flex: 0 0 auto; }
    header.banner .region-selector-bar { order: 2; flex: 0 0 auto; }

    header.banner .header-main > .container {
        width: 100% !important;
        max-width: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    header.banner .header-inner {
        flex-wrap: wrap !important;
        align-items: center !important;
        align-content: flex-start !important;
        min-height: 64px !important;
        padding: 0 !important;
    }

    /* logo row: 64px tall, keeps clear of the 44px toggle on the right */
    header.banner .header-logo {
        height: 64px !important;
        padding: 0 0 0 15px !important;
        max-width: calc(100% - 64px) !important;
        flex: 0 1 auto !important;
    }

    header.banner .header-logo .main-logo {
        max-width: calc(100vw - 84px) !important;
    }

    html body,
    body,
    body.sticky-header-active {
        padding-top: 64px !important;
    }

    /* menu row spans the full width under the logo row (0px tall while closed) */
    header.banner .header-nav {
        flex: 0 0 100% !important;
        width: 100% !important;
        display: block !important;
    }

    header.banner .header-nav #navbar,
    header.banner .header-nav .nav-wrap,
    header.banner #mega-menu-wrap-primary_navigation {
        width: 100% !important;
        float: none !important;
    }

    /* ---------- the toggle: black hamburger, 44x44, top-right, always on top ---------- */
    #mega-menu-wrap-primary_navigation .mega-menu-toggle,
    .mega-menu-wrap .mega-menu-toggle {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        position: fixed !important;
        top: 10px !important;
        right: 8px !important;
        left: auto !important;
        width: 44px !important;
        height: 44px !important;
        min-width: 44px !important;
        padding: 0 !important;
        margin: 0 !important;
        line-height: 44px !important;
        background: transparent !important;
        box-shadow: none !important;
        z-index: 10001 !important;
    }

    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-blocks-left,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-blocks-center {
        display: none !important;
    }

    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-blocks-right,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-block {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        width: 44px !important;
        height: 44px !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* the button itself (new markup) and the label span (old markup) must stay visible */
    #mega-menu-wrap-primary_navigation .mega-menu-toggle button.mega-toggle-standard,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-block .mega-toggle-label,
    .mega-menu-wrap .mega-menu-toggle .mega-toggle-block .mega-toggle-label {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        position: relative !important;
        width: 44px !important;
        height: 44px !important;
        margin: 0 !important;
        padding: 0 !important;
        border: 0 !important;
        background: transparent !important;
        color: #000000 !important;
        cursor: pointer;
        visibility: visible !important;
        opacity: 1 !important;
    }

    #mega-menu-wrap-primary_navigation .mega-menu-toggle button.mega-toggle-standard:focus-visible {
        outline: 2px solid #32779a !important;
        outline-offset: -2px;
    }

    /* hide only the "MENU" text, visually - screen readers still get the button's name */
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-label-closed,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-label-open,
    .mega-menu-wrap .mega-menu-toggle .mega-toggle-label-closed,
    .mega-menu-wrap .mega-menu-toggle .mega-toggle-label-open {
        position: absolute !important;
        width: 1px !important;
        height: 1px !important;
        overflow: hidden !important;
        clip: rect(0 0 0 0) !important;
        clip-path: inset(50%) !important;
        white-space: nowrap !important;
        margin: -1px !important;
        padding: 0 !important;
    }

    /* the hamburger / close glyph (dashicons, drawn by MMM on the button) - black, 28px */
    #mega-menu-wrap-primary_navigation .mega-menu-toggle button.mega-toggle-standard::after,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-block::after {
        display: inline-block !important;
        font-family: "dashicons" !important;
        font-size: 28px !important;
        font-weight: normal !important;
        line-height: 28px !important;
        width: 28px !important;
        height: 28px !important;
        color: #000000 !important;
        speak: never;
    }
    #mega-menu-wrap-primary_navigation .mega-menu-toggle:not(.mega-menu-open) button.mega-toggle-standard::after {
        content: "\f333" !important;
    }
    #mega-menu-wrap-primary_navigation .mega-menu-toggle.mega-menu-open button.mega-toggle-standard::after {
        content: "\f335" !important;
    }
    /* old animated-hamburger markup (pre-3.x) - bars black */
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-animated-inner,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-animated-inner::before,
    #mega-menu-wrap-primary_navigation .mega-menu-toggle .mega-toggle-animated-inner::after {
        background-color: #000000 !important;
    }

    /* ---------- the open panel: in the header's flow (header scrolls), full width ---------- */
    header.banner #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation {
        position: static !important;
        top: auto !important;
        left: auto !important;
        right: auto !important;
        width: 100% !important;
        max-height: none !important;
        overflow: visible !important;
        box-shadow: none !important;
        border-top: 1px solid #eeeeee !important;
        z-index: auto !important;
    }

    /* GIVE: style.css gives it width:100% plus 15px side margins = 30px wider than the screen */
    header.banner #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation > li.mega-menu-item-give > a.mega-menu-link,
    header.banner #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation > li.mega-menu-item > a.mega-menu-link {
        width: auto !important;
        box-sizing: border-box !important;
    }
    /* MMM fixes rows at height:40px; with style.css's 15px padding the label spilled below the row */
    header.banner #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation > li.mega-menu-item > a.mega-menu-link {
        height: auto !important;
        min-height: 44px;
        line-height: 24px !important;
        padding-top: 10px !important;
        padding-bottom: 10px !important;
    }

    /* while the menu is open it must win over page popups: Elementor popups (e.g. #22718 on the
       fullscreen home, triggered by the first tap - often the hamburger) use z-index 9999 = the header's.
       Only while open, so a closed menu never covers a popup's close button. */
    html body header.banner:has(.mega-menu-toggle.mega-menu-open) {
        z-index: 100000 !important;
    }

    /* while the menu is open, park the floating Donate/Give buttons + the Fundraise Up popup
       (z-index ~2^31) so they can't sit on top of the menu; they come back when it closes.
       Fundraise Up forces visibility/pointer-events/display/position inline with !important,
       so it is parked with opacity + clip-path (clipped = not hit-testable) + transform. */
    body:has(#mega-menu-wrap-primary_navigation .mega-menu-toggle.mega-menu-open) iframe[class*="fun-element"],
    body:has(#mega-menu-wrap-primary_navigation .mega-menu-toggle.mega-menu-open) #kcm-give-fab,
    body:has(#mega-menu-wrap-primary_navigation .mega-menu-toggle.mega-menu-open) .to-top {
        visibility: hidden !important;
        pointer-events: none !important;
        opacity: 0 !important;
        clip-path: inset(100%) !important;
        transform: translateY(200%) !important;
    }

    /* sub-menu items: readable, full-width tap rows */
    header.banner #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation ul.mega-sub-menu a.mega-menu-link {
        color: #333333 !important;
        background: #f9f9f9 !important;
        padding: 12px 20px 12px 35px !important;
        min-height: 44px;
        line-height: 20px !important;
        border-bottom: 1px solid #f0f0f0 !important;
    }

    /* ---------- region selector: shown at the bottom of the open menu (it's hidden in the
       closed mobile header to save room). Browsers without :has() keep it hidden, as before. */
    header.banner:has(.mega-menu-toggle.mega-menu-open) .region-selector-bar {
        display: block !important;
        border-top: 1px solid #e0e0e0;
        border-bottom: 0;
        padding: 10px 0 14px;
    }
    header.banner .region-selector-bar .container {
        display: block !important;
        width: 100% !important;
        padding: 0 20px !important;
    }
    header.banner .region-selector-bar .region-selector {
        display: block !important;
    }
    header.banner .region-selector-bar .region-current {
        cursor: default;
        padding: 4px 0 !important;
        font-weight: 600;
        background: transparent !important;
    }
    header.banner .region-selector-bar .region-current .fa-caret-down {
        display: none !important;
    }
    header.banner .region-selector-bar .region-dropdown {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        position: static !important;
        min-width: 0 !important;
        margin: 4px 0 0 !important;
        padding: 0 !important;
        border: 0 !important;
        box-shadow: none !important;
        background: transparent !important;
    }
    header.banner .region-selector-bar .region-dropdown li { margin: 0 !important; }
    header.banner .region-selector-bar .region-dropdown li a {
        display: flex !important;
        align-items: center;
        min-height: 44px;
        padding: 6px 8px !important;
        border-radius: 3px;
    }
}

/* home page promo images: style.css caps them at 330px (max-width:768px block), overriding
   img-responsive's 100%, so they spilled out of their column at 320-359px and at 768px */
@media (max-width: 768px) {
    body.home #featured-spots .spot img {
        max-width: min(330px, 100%) !important;
    }
}

/* WordPress admin bar (logged-in editors): fixed 46px bar from 601px up - keep the header under it */
@media (min-width: 601px) and (max-width: 767px) {
    body.admin-bar header.banner,
    html body.admin-bar header.banner { top: 46px !important; max-height: calc(100dvh - 46px) !important; }
    body.admin-bar #mega-menu-wrap-primary_navigation .mega-menu-toggle { top: 56px !important; }
}

/* ---------- tablet / desktop (>= 768px): horizontal bar ---------- */
@media (min-width: 768px) {
    /* flyouts of the right-most items open leftwards, so a hidden sub-menu can't push the page
       wider than the screen (that caused 4-124px of sideways scroll at 768-992px) */
    #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation > li.mega-menu-flyout:nth-last-child(-n+3) > ul.mega-sub-menu {
        left: auto !important;
        right: 0 !important;
    }
    header.banner .header-nav { min-width: 0; }
}

/* 768-991px: the 450px logo left too little room and the bar wrapped to two rows */
@media (min-width: 768px) and (max-width: 991px) {
    header.banner .header-logo .main-logo {
        width: 250px !important;
        height: 60px !important;
        background-size: contain !important;
    }
    header.banner.sticky .header-logo .main-logo {
        background-size: contain !important;
        height: 45px !important;
    }
    #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation > li.mega-menu-item > a.mega-menu-link {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }
    #mega-menu-wrap-primary_navigation #mega-menu-primary_navigation > li.mega-menu-item-give > a.mega-menu-link {
        padding-left: 14px !important;
        padding-right: 14px !important;
    }
}
