/** Shopify CDN: Minification failed

Line 21:0 Unexpected "("
Line 24:2 Comments in CSS use "/* ... */" instead of "//"
Line 25:2 Comments in CSS use "/* ... */" instead of "//"
Line 26:2 Comments in CSS use "/* ... */" instead of "//"
Line 42:2 Comments in CSS use "/* ... */" instead of "//"
Line 43:2 Comments in CSS use "/* ... */" instead of "//"
Line 44:2 Comments in CSS use "/* ... */" instead of "//"
Line 45:2 Comments in CSS use "/* ... */" instead of "//"
Line 48:4 Comments in CSS use "/* ... */" instead of "//"
Line 49:4 Comments in CSS use "/* ... */" instead of "//"
... and 46 more hidden warnings

**/
/* ========================================
   REDDOG CUSTOM NAVIGATION JAVASCRIPT
   Built to spec
   ======================================== */

(function() {
  'use strict';

  // ========================================
  // DOM ELEMENTS
  // ========================================
  
  const header = document.getElementById('reddog-custom-header');
  const hamburger = document.getElementById('reddog-hamburger');
  const mobileOverlay = document.getElementById('reddog-mobile-overlay');
  const mobileClose = document.getElementById('reddog-mobile-close');
  const mobileBackdrop = document.getElementById('reddog-mobile-backdrop');
  const servicesDropdownTrigger = document.getElementById('services-dropdown-trigger');
  const servicesDropdown = document.getElementById('services-dropdown');
  const accordionButtons = document.querySelectorAll('[data-accordion]');

  if (!header) {
    console.error('Reddog custom header not found');
    return;
  }

  // ========================================
  // SCROLL LOGIC
  // Per spec: if (window.scrollY > heroHeight) add class "header--scrolled"
  // ========================================

  function handleScroll() {
    // Adjust heroHeight to match your video/hero section height
    // 0.6 = 60% of viewport height
    const heroHeight = window.innerHeight * 0.6;
    const scrollY = window.scrollY;

    if (scrollY > heroHeight) {
      header.classList.add('header--scrolled');
      document.body.classList.add('reddog-header-active');
    } else {
      header.classList.remove('header--scrolled');
      document.body.classList.remove('reddog-header-active');
    }
  }

  // Throttled scroll for performance
  let scrollTimeout;
  window.addEventListener('scroll', function() {
    if (!scrollTimeout) {
      scrollTimeout = setTimeout(function() {
        handleScroll();
        scrollTimeout = null;
      }, 10);
    }
  });

  // Initial check
  handleScroll();

  // ========================================
  // MOBILE MENU
  // ========================================

  function openMobileMenu() {
    mobileOverlay.classList.add('active');
    mobileBackdrop.classList.add('active');
    hamburger.setAttribute('aria-expanded', 'true');
    document.body.style.overflow = 'hidden'; // Prevent body scroll
    trapFocus(mobileOverlay);
  }

  function closeMobileMenu() {
    mobileOverlay.classList.remove('active');
    mobileBackdrop.classList.remove('active');
    hamburger.setAttribute('aria-expanded', 'false');
    document.body.style.overflow = ''; // Restore body scroll
  }

  // Event listeners
  if (hamburger) {
    hamburger.addEventListener('click', openMobileMenu);
  }

  if (mobileClose) {
    mobileClose.addEventListener('click', closeMobileMenu);
  }

  if (mobileBackdrop) {
    mobileBackdrop.addEventListener('click', closeMobileMenu);
  }

  // ========================================
  // DESKTOP DROPDOWN
  // Opens on hover and click
  // Closes on mouseleave OR click outside
  // ========================================

  let dropdownTimeout;

  function openDropdown() {
    if (!servicesDropdown) return;
    clearTimeout(dropdownTimeout);
    servicesDropdown.classList.add('active');
    if (servicesDropdownTrigger) {
      servicesDropdownTrigger.setAttribute('aria-expanded', 'true');
    }
  }

  function closeDropdown() {
    if (!servicesDropdown) return;
    dropdownTimeout = setTimeout(function() {
      servicesDropdown.classList.remove('active');
      if (servicesDropdownTrigger) {
        servicesDropdownTrigger.setAttribute('aria-expanded', 'false');
      }
    }, 150);
  }

  if (servicesDropdownTrigger && servicesDropdown) {
    // Click to toggle
    servicesDropdownTrigger.addEventListener('click', function(e) {
      e.preventDefault();
      const isOpen = servicesDropdown.classList.contains('active');
      if (isOpen) {
        closeDropdown();
      } else {
        openDropdown();
      }
    });

    // Hover behavior (desktop only)
    if (window.innerWidth >= 1024) {
      const dropdownParent = servicesDropdownTrigger.closest('.reddog-custom-header__nav-item');
      
      if (dropdownParent) {
        dropdownParent.addEventListener('mouseenter', openDropdown);
        dropdownParent.addEventListener('mouseleave', closeDropdown);
      }
    }
  }

  // Close dropdown when clicking outside
  document.addEventListener('click', function(e) {
    if (servicesDropdown && servicesDropdownTrigger) {
      if (!servicesDropdown.contains(e.target) && 
          !servicesDropdownTrigger.contains(e.target)) {
        closeDropdown();
      }
    }
  });

  // ========================================
  // MOBILE ACCORDION
  // Expand/collapse only (no hover behavior)
  // ========================================

  accordionButtons.forEach(function(button) {
    button.addEventListener('click', function() {
      const accordionId = this.getAttribute('data-accordion');
      const submenu = document.getElementById('mobile-submenu-' + accordionId);
      const isExpanded = this.getAttribute('aria-expanded') === 'true';

      if (isExpanded) {
        // Close this accordion
        this.setAttribute('aria-expanded', 'false');
        if (submenu) {
          submenu.classList.remove('active');
        }
      } else {
        // Close all other accordions
        accordionButtons.forEach(function(btn) {
          btn.setAttribute('aria-expanded', 'false');
          const id = btn.getAttribute('data-accordion');
          const sub = document.getElementById('mobile-submenu-' + id);
          if (sub) {
            sub.classList.remove('active');
          }
        });

        // Open this accordion
        this.setAttribute('aria-expanded', 'true');
        if (submenu) {
          submenu.classList.add('active');
        }
      }
    });
  });

  // ========================================
  // KEYBOARD NAVIGATION
  // ESC closes mobile menu
  // ========================================

  document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape' || e.keyCode === 27) {
      closeMobileMenu();
      closeDropdown();
    }
  });

  // ========================================
  // FOCUS TRAP (Mobile menu)
  // ========================================

  function trapFocus(element) {
    const focusableElements = element.querySelectorAll(
      'a[href], button:not([disabled]), textarea, input, select'
    );
    
    if (focusableElements.length === 0) return;
    
    const firstFocusable = focusableElements[0];
    const lastFocusable = focusableElements[focusableElements.length - 1];

    // Focus first element
    setTimeout(function() {
      if (firstFocusable) {
        firstFocusable.focus();
      }
    }, 100);

    // Trap focus within menu
    element.addEventListener('keydown', function(e) {
      if (e.key !== 'Tab' && e.keyCode !== 9) return;

      if (e.shiftKey) {
        // Shift + Tab
        if (document.activeElement === firstFocusable) {
          lastFocusable.focus();
          e.preventDefault();
        }
      } else {
        // Tab
        if (document.activeElement === lastFocusable) {
          firstFocusable.focus();
          e.preventDefault();
        }
      }
    });
  }

  // ========================================
  // WINDOW RESIZE HANDLER
  // ========================================

  let resizeTimeout;
  window.addEventListener('resize', function() {
    clearTimeout(resizeTimeout);
    resizeTimeout = setTimeout(function() {
      // Close mobile menu if resized to desktop
      if (window.innerWidth >= 1024) {
        closeMobileMenu();
      }
      
      // Re-run scroll handler
      handleScroll();
    }, 250);
  });

  // ========================================
  // SMOOTH SCROLL FOR ANCHOR LINKS
  // (Optional - adjust anchor links for fixed header)
  // ========================================

  document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
    anchor.addEventListener('click', function(e) {
      const target = document.querySelector(this.getAttribute('href'));
      if (target) {
        e.preventDefault();
        const headerHeight = header.offsetHeight || 80;
        const targetPosition = target.offsetTop - headerHeight - 20;
        
        window.scrollTo({
          top: targetPosition,
          behavior: 'smooth'
        });
      }
    });
  });

  // ========================================
  // CONSOLE LOG
  // ========================================

  console.log('🐕 Reddog Custom Navigation Loaded');

})();