/* ========================================================================
 ! WARRANTEED COMPONENT: UNIFIED NATIVE RESPONSIVE NAVBAR SYSTEM
 ! Developed/Tested: June 2026 // Target Viewport: Unified Cross-Device
 ======================================================================== */

/* ========================================================================
 ! --- 1. GLOBAL CONTAINER BASELINE ---
 ======================================================================== */
.warranteed-navbar {
    width: 100%;
    height: 80px; /* Match body's padding-top */
    position: fixed;
    top: 0;
    left: 0;
    padding: 10px 4%;
    background: #2e0155; /* Your dark purple */
    z-index: 9999; /* Ensure it stays on top of other content */
    display: flex;
    justify-content: space-between; /* Space out brand, links, button */
    align-items: center; /* Vertically center items */
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.navbar-brand-hook {
    display: flex;
    align-items: center;
    height: 100%;
}

.brand-logo {
    height: 40px; /* Adjust logo size */
    width: auto;
    border-radius: 8px;
    margin-right: 20px; /* Space between logo and nav links */
}

/* ========================================================================
 ! --- 2. DESKTOP BASELINE: MAIN STRUCTURAL LAYOUT ENGINE ---
 ! Focus: Outer flexbox grid orchestration, link inline scaling, and brand 
 !        alignments.
 ! Applies to: High-resolution viewports where horizontal layout runways 
 !             are absolute.
 ======================================================================== */
.navbar-menu-drawer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex: 1;
    margin-left: 40px;
}

.nav-links-tree {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 28px;
}

.nav-tree-item {
    position: relative;
}

.nav-tree-toggle,
.nav-tree-link,
.nav-links-tree .nav-tree-item a {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.9rem;
    font-weight: 500;
    color: rgba(248, 250, 252, 0.8);
    text-decoration: none;
    display: flex;
    align-items: center;
    cursor: pointer;
    transition: color 0.2s ease;
}

.nav-tree-toggle:hover,
.nav-tree-link:hover,
.nav-links-tree .nav-tree-item a:hover {
    color: #fff;
}

/* Caret Icon Definitions */
.caret {
    display: inline-block;
    width: 0;
    height: 0;
    margin-left: 6px;
    vertical-align: middle;
    border-top: 4px solid;
    border-right: 4px solid transparent;
    border-left: 4px solid transparent;
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.nav-tree-item.open .caret {
    transform: rotate(180deg);
}

/* ========================================================================
 ! --- 3. DESKTOP INTERACTIVITY: ABSOLUTE MEGAMENU OVERLAYS ---
 ! Focus: Floating hover-driven content panels, inner cell positioning, 
 !        and icon text matrices.
 ! Applies to: The hidden dropdown grids appended to 'Product', 
 !             'Industries', and 'The Registry'.
 ======================================================================== */
.megamenu-panel {
    display: none;
    position: absolute;
    top: calc(100% + 15px);
    left: 50%;
    transform: translateX(-50%);
    background: #ffffff;
    min-width: 460px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
    border-radius: 12px;
    padding: 20px;
    border: 1px solid #e2e8f0;
    z-index: 100000;
}

.nav-tree-item.open .megamenu-panel {
    display: block !important;
}

.megamenu-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.megamenu-cell {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    text-decoration: none;
    padding: 12px;
    border-radius: 8px;
    transition: background 0.2s ease;
}
.megamenu-cell:hover { background: #f8fafc; }

.cell-icon { font-size: 1.1rem; line-height: 1; }
.cell-body strong { display: block; color: #2e0155; font-size: 0.85rem; font-family: 'JetBrains Mono', monospace; }
.cell-body p { margin: 4px 0 0 0; font-size: 0.72rem; color: #64748b; font-family: 'Inter', sans-serif; line-height: 1.3; }

/* Primary Action Link Trigger Button */
.navbar-primary-btn {
    background: #6366f1;
    color: #fff;
    border: none;
    padding: 10px 20px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.85rem;
    font-weight: 700;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s ease;
}
.navbar-primary-btn:hover { background: #4f46e5; }

/* Hamburger Trigger Hidden on Desktop */
.navbar-hamburger-trigger { display: none; }

/* ========================================================================
 ! --- 4. TABLET DESIGN MATRIX (iPad Screens: 769px to 1024px) ---
 ======================================================================== */
@media (min-width: 769px) and (max-width: 1024px) {
    .warranteed-navbar {
        padding: 0 20px !important; /* Tighten the outer container gutters */
    }

    .navbar-menu-drawer {
        margin-left: 20px !important; /* Pull the link group closer to the logo */
    }

    .nav-links {
        font-size: 0.65rem !important;
    }


    .nav-links-tree {
        gap: 16px !important; /* Squeeze horizontal spacing between link items */
    }

    .nav-tree-toggle,
    .nav-tree-link,
    .nav-links-tree .nav-tree-item a {
        font-size: 0.5rem !important; /* Drop font size down from 0.9rem to prevent wrapping */
        letter-spacing: -0.3px !important;
    }

    .caret {
        margin-left: 4px !important; /* Pull caret tighter to the text asset */
    }

    .megamenu-panel {
        min-width: 400px !important; /* Scale down the megamenu width slightly to match layout constraints */
    }

    .navbar-primary-btn {
        padding: 8px 14px !important; /* Slim down action button footprint */
        font-size: 0.8rem !important;
    }
}

/* ========================================================================
! --- 5. MOBILE INTERACTION SYSTEM (Smartphone Screens under 768px) ---
 ======================================================================== */
@media (max-width: 768px) {
    .warranteed-navbar {
        padding: 0 20px !important;
    }

    /* Convert Hamburger Button into an Interactive Vector block */
    .navbar-hamburger-trigger {
        display: flex !important;
        flex-direction: column !important;
        justify-content: space-between !important;
        width: 22px !important;
        height: 16px !important;
        background: none !important;
        border: none !important;
        padding: 0 !important;
        cursor: pointer !important;
        z-index: 100002 !important;
    }

    .hamburger-bar {
        display: block !important;
        width: 100% !important;
        height: 2px !important;
        background-color: #f8fafc !important;
        transition: transform 0.25s ease, opacity 0.25s ease !important;
        transform-origin: left center !important;
    }

    /* Transform Hamburger into an 'X' icon when open */
    .warranteed-navbar.drawer-active .hamburger-bar:nth-child(1) { transform: rotate(45deg) translateY(-1px); }
    .warranteed-navbar.drawer-active .hamburger-bar:nth-child(2) { opacity: 0; }
    .warranteed-navbar.drawer-active .hamburger-bar:nth-child(3) { transform: rotate(-45deg) translateY(1px); }

    /* TRANSLATE MENU DRAWER: Convert into a clean slide-out menu */
    .navbar-menu-drawer {
        position: fixed !important;
        top: 70px !important;
        left: 100vw !important; /* Start completely off-screen to the right */
        width: 100vw !important;
        height: calc(100vh - 70px) !important;
        background: #2e0155 !important;
        margin: 0 !important;
        padding: 30px 24px 40px !important;
        flex-direction: column !important;
        align-items: flex-start !important;
        justify-content: space-between !important;
        box-sizing: border-box !important;
        transition: left 0.28s cubic-bezier(0.16, 1, 0.3, 1) !important;
        z-index: 100001 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }

    nav > .right-container {
        display: none !important;
    }

    /* Slide drawer fully into view bounds */
    .warranteed-navbar.drawer-active .navbar-menu-drawer {
        left: 0 !important;
    }

    .nav-links-tree {
        flex-direction: column !important;
        align-items: flex-start !important;
        width: 100% !important;
        gap: 20px !important;
    }

    .nav-tree-item {
        width: 100% !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.05) !important;
        padding-bottom: 12px !important;
    }

    .nav-tree-toggle, .nav-tree-link {
        font-size: 1.1rem !important;
        width: 100% !important;
        justify-content: space-between !important;
    }

    /* MOBILE ACCORDION: Adapt panel to expand downward smoothly */
    .megamenu-panel {
        position: static !important;
        transform: none !important;
        min-width: 100% !important;
        background: rgba(0, 0, 0, 0.15) !important;
        box-shadow: none !important;
        border: none !important;
        border-radius: 8px !important;
        margin-top: 10px !important;
        padding: 10px !important;
        box-sizing: border-box !important;
    }

    .megamenu-grid {
        grid-template-columns: 1fr !important; /* Stack cells inside dropdown sequentially */
        gap: 4px !important;
    }

    .megamenu-cell {
        padding: 10px !important;
    }
    .megamenu-cell:hover { background: rgba(255, 255, 255, 0.05) !important; }

    .cell-body strong { color: #fff !important; font-size: 0.9rem !important; }
    .cell-body p { color: #94a3b8 !important; font-size: 0.75rem !important; }

    .navbar-action-dock {
        width: 100% !important;
        margin-top: 30px !important;
    }

    .navbar-primary-btn {
        width: 100% !important;
        padding: 14px !important;
        font-size: 1rem !important;
        text-align: center !important;
    }

    /* ========================================================================
    ! MOBILE ACCORDION TRANSFORMATION (Screens 768px and below)
    ======================================================================== */
    /* 1. Transmute panels into static, vertical, nested blocks */
    .mobile-menu .dropdown-menu,
    .mobile-menu .dropdown-menu-vault {
        position: static !important; /* Forces the panel into the normal layout stack flow */
        transform: none !important;
        min-width: 100% !important;
        width: 100% !important;
        box-shadow: none !important; /* Strip desktop depth indicators */
        border: none !important;
        background: rgba(0, 0, 0, 0.15) !important; /* Slightly darker backing tile for contrast */
        padding: 10px !important;
        box-sizing: border-box;
        margin-top: 8px;
    }

    /* 2. Collapse the 2-column grid into a single, sequential vertical column */
    .mobile-menu .dropdown-grid {
        grid-template-columns: 1fr !important; /* Single sequential list structure */
        gap: 8px !important;
    }

    /* 3. Refine link spacing for touch targets */
    .mobile-menu .dropdown-item {
        padding: 12px !important;
        width: 100% !important;
        box-sizing: border-box;
    }

    /* 4. Swatches typography color schema for dark backgrounds */
    .mobile-menu .dropdown-text strong {
        color: #ffffff !important; 
        font-size: 0.9rem !important;
    }

    .mobile-menu .dropdown-text p {
        color: #a5b4fc !important;
        font-size: 0.75rem !important;
    }
}

<script>
    document.addEventListener('DOMContentLoaded', function() {
    const navbar = document.querySelector('.warranteed-navbar');
    const hamburger = document.getElementById('navbarHamburgerTrigger');
    const dropdowns = document.querySelectorAll('.nav-tree-item.has-dropdown, .nav-items.dropdown');
    
    // Core Layout State Token: Matches 'hover:ID' or 'click:ID'
    let activeMenuState = null; 

    function getMenuId(el) {
        const toggle = el.querySelector('.nav-tree-toggle') || el.querySelector('.dropdown-toggle');
        return toggle ? toggle.textContent.trim() : '';
    }

    // Synchronize UI dropdown matrices across viewports
    function syncDropdowns() {
        dropdowns.forEach(dropdown => {
            const menuId = getMenuId(dropdown);
            if (activeMenuState && activeMenuState.split(':')[1] === menuId) {
                dropdown.classList.add('open');
            } else {
                dropdown.classList.remove('open');
            }
        });
    }

    // --- 1. SMART VIEWPORT DROP-DOWN INTERACTION HANDLERS ---
    dropdowns.forEach(dropdown => {
        const toggle = dropdown.querySelector('.nav-tree-toggle');
        const menuId = getMenuId(dropdown);

        // Desktop Pointer Entrance Hook
        dropdown.addEventListener('mouseenter', function() {
            if (window.innerWidth > 768) {
                activeMenuState = `hover:${menuId}`;
                syncDropdowns();
            }
        });

        // Desktop Pointer Exit Hook
        dropdown.addEventListener('mouseleave', function() {
            if (window.innerWidth > 768 && activeMenuState === `hover:${menuId}`) {
                activeMenuState = null;
                syncDropdowns();
            }
        });

        // Unified Tap/Hardware-Click Handler
        if (toggle) {
            toggle.addEventListener('click', function(e) {
                e.preventDefault();
                e.stopPropagation();

                if (activeMenuState === `click:${menuId}` || activeMenuState === `hover:${menuId}`) {
                    activeMenuState = null;
                } else {
                    activeMenuState = `click:${menuId}`;
                }
                syncDropdowns();
            });
        }
    });

    // Tap outside open menus to dismiss them
    window.addEventListener('click', function() {
        if (activeMenuState && !activeMenuState.startsWith('click:')) {
            activeMenuState = null;
            syncDropdowns();
        }
    });

    // --- 2. THE MOBILE OVERLAY HAMBURGER DRAWER ENGINE ---
    if (hamburger && navbar) {
        hamburger.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            
            // Toggle active overlay tracking tokens
            const isActive = navbar.classList.toggle('drawer-active');
            
            if (isActive) {
                document.body.style.overflow = 'hidden'; // Stop page scrolling when drawer is active
            } else {
                document.body.style.overflow = ''; // Restore page scrolling safely
                
                // Close any open accordions inside the drawer on exit
                activeMenuState = null;
                syncDropdowns();
            }
        });
    }

    // Target specifically the dropdown-toggle links sitting within the mobile-menu tree
    const mobileDropdownLinks = document.querySelectorAll('.mobile-menu .dropdown-toggle');

    mobileDropdownLinks.forEach(toggle => {
        toggle.addEventListener('click', function(e) {
            // Stop default navigation and propagation up the DOM tree
            e.preventDefault();
            e.stopPropagation();

            // Target the closest parent <li> element wrapper
            const parentLi = this.closest('.nav-items');
            
            // Toggle the 'open' class token to reveal/hide the accordion stack
            if (parentLi) {
                parentLi.classList.toggle('open');
            }
        });
    });
});
