(function ($) {
	"use strict";

	// Hide Loading Box (Preloader)
	function handlePreloader() {
		if ($('.loader-wrap').length) {
			$('.loader-wrap').delay(1000).fadeOut(500);
		}
	}

	// Run preloader on window load
	$(window).on('load', function () {
		handlePreloader();
	});

	// Close preloader manually
	if ($(".preloader-close").length) {
		$(".preloader-close").on("click", function () {
			$('.loader-wrap').fadeOut(500);
		});
	}
	
	// Sticky Header
    $(window).on('scroll', function() {
      if ($(window).scrollTop() >= 250) {
          $('.is-sticky-on').addClass('is-sticky-menu');
      }
      else {
          $('.is-sticky-on').removeClass('is-sticky-menu');
      }
  });

    if ($('.mobile-menu').length) {

		// Clone menu (IMPORTANT)
      var mobileMenuContent = $('.main-header .menu-area .main-menu').html();

      $('.mobile-menu .menu-box .menu-outer').html(mobileMenuContent);
      $('.sticky-header .main-menu').html(mobileMenuContent);

		//Menu Toggle Btn
      $('.mobile-menu-btn').on('click', function() {
       $('body').addClass('mobile-menu-visible');
   });

		//Menu Toggle Btn
      $('.mobile-menu .menu-backdrop,.mobile-menu .close-btn').on('click', function() {
       $('body').removeClass('mobile-menu-visible');
   });
  }

	//Category Dropdown Toggle
  if($('.category-menu li.category-dropdown ul').length){
      $('.category-menu .category-list li.category-dropdown').append('<div class="dropdown-btn"><span class="icon-1"></span></div>');

  }

	//Category Nav Hide Show
  if($('.category-menu').length){
		//Dropdown Button
      $('.category-menu li.category-dropdown .dropdown-btn').on('click', function() {
       $(this).toggleClass('open');
       $(this).prev('ul').slideToggle(500);
   });
		//Dropdown Button
      $('.category-menu li.category-dropdown .dropdown-btn').on('click', function() {
       $(this).prev('.megamenu').slideToggle(900);
   });
		//Menu Toggle Btn
      $('.category-box').on('click', function() {
       $('body').addClass('category-visible');
   });

		//Menu Close Btn - Use event delegation to ensure it works
      $(document).on('click', '.category-menu .menu-backdrop, .category-menu .close-btn, .category-menu .close-btn i', function(e) {
       e.preventDefault();
       e.stopPropagation();
       $('body').removeClass('category-visible');
   });
  }

  jQuery(function ($) {
      $('.dropdown-btn').on('click', function (e) {
       e.preventDefault();

       var $btn  = $(this);
       var $menu = $btn.next('.dropdown-menu');

       if ($btn.hasClass('open')) {
      // close smoothly
        $btn.removeClass('open');
      $menu.stop(true, true).slideUp(500); // smooth hide
  } else {
      // open smoothly
     $btn.addClass('open');
      $menu.stop(true, true).slideDown(500); // smooth show
  }
});
  });
  $(document).on('keydown', '.dropdown-menu a', function (e) {
    if (window.innerWidth <= 1200) {
 
        if (e.key === "Tab" && !e.shiftKey) {
 
            var $submenu = $(this).closest('.dropdown-menu');
            var $links = $submenu.find('a');
            var $lastLink = $links.last();
 
            if ($(this).is($lastLink)) {
                e.preventDefault();
 
                var $toggleBtn = $submenu.prev('.dropdown-btn');
                $toggleBtn.focus();
            }
        }
    }
});
 document.addEventListener('keydown', function (e) {

    // Check Escape key
    if (e.key === 'Escape') {

        const body = document.body;
        const menuToggle = document.querySelector('.mobile-nav-toggler');

        // Agar mobile menu open hai
        if (body.classList.contains('mobile-menu-visible')) {

            // Remove class from body
            body.classList.remove('mobile-menu-visible');

            // Accessibility: aria-hidden true karo
            const mobileMenu = document.querySelector('.mobile-menu');
            if (mobileMenu) {
                mobileMenu.setAttribute('aria-hidden', 'true');
            }

            // Focus wapas toggle button par bhejo
            if (menuToggle) {
                menuToggle.focus();
            }
      }
    }
});

document.addEventListener('keydown', function (e) {

    const body = document.body;
    const mobileMenu = document.querySelector('.mobile-menu');

    // Sirf tab jab menu open ho
    if (!body.classList.contains('mobile-menu-visible')) return;

    if (e.key === 'Tab') {

        const focusableElements = mobileMenu.querySelectorAll(
            'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'
        );

        const firstElement = focusableElements[0];
        const lastElement  = focusableElements[focusableElements.length - 1];

        // Shift + Tab
        if (e.shiftKey) {
            if (document.activeElement === firstElement) {
                e.preventDefault();
                lastElement.focus();
            }
        } 
        // Normal Tab
        else {
            if (document.activeElement === lastElement) {
                e.preventDefault();
                firstElement.focus();
            }
        }
    }
}); 
	// banner-carousel
  if ($('.banner-carousel').length) {
      $('.banner-carousel').owlCarousel({
       loop:true,
       margin:0,
       nav:true,
       animateOut: 'fadeOut',
       animateIn: 'fadeIn',
       active: true,
       smartSpeed: 1000,
       autoplay: false,
       navText: [ '<i class="fa fa-arrow-left"></i>', '<i class="fa fa-arrow-right"></i>' ],
       responsive:{
        0:{
         items:1
     },
     600:{
         items:1
     },
     800:{
         items:1
     },
     1024:{
         items:1
     }
 }
});
  }

	// category-carousel
  if ($('.category-carousel').length) {
      $('.category-carousel').owlCarousel({
       loop:true,
       margin:50,
       nav:true,
       smartSpeed: 500,
       autoplay: 1000,
       navText: [ '<i class="fa fa-arrow-left"></i>', '<i class="fa fa-arrow-right"></i>' ],
       responsive:{
        0:{
         items:1
     },
     480:{
         items:2
     },
     600:{
         items:4
     },
     800:{
         items:6
     },			
     1200:{
         items:8
     }

 }
});    		
  }
	// shop-carousel
  if ($('.shop-carousel').length) {
      $('.shop-carousel').owlCarousel({
       loop:true,
       margin:0,
       nav:true,
       smartSpeed: 500,
       autoplay: false,
       navText: [ '<i class="fa fa-arrow-left"></i>', '<i class="fa fa-arrow-right"></i>' ],
       responsive:{
        0:{
         items:1
     },
     480:{
         items:2
     },
     600:{
         items:3
     },
     800:{
         items:4
     },			
     1200:{
         items:6
     }
 }
});    		
  }

  $(document).ready(function() {
      $('.header-search-select:not(.ignore)').niceSelect();
  });

  $(document).on("click", ".cart-icon-wrap", function () {
    $(".cart-menu-one").addClass("cart-visible");
});

  $(document).on("click", ".close-icon-one", function () {
    $(".cart-menu-one").removeClass("cart-visible");
});

  $(document).on('keydown', function (e) {
    if (e.key === 'Escape' || e.keyCode === 27) {
        $('body').removeClass('mobile-menu-visible category-visible');
        $(".cart-menu-one").removeClass("cart-visible");
    }
});

})(window.jQuery);