(function($) {
  "use strict";

  // ============================================================
  // MOBILE SIDEBAR - FULLY FIXED
  // ============================================================

  function isMobile() {
    return $(window).width() <= 768;
  }

  function openMobileSidebar() {
    $(".sidebar").addClass("mobile-open");
    $("#sidebarOverlay").addClass("active");
    // Prevent body scroll while sidebar is open
    $("body").css("overflow", "hidden");
  }

  function closeMobileSidebar() {
    $(".sidebar").removeClass("mobile-open");
    $("#sidebarOverlay").removeClass("active");
    $("body").css("overflow", "");
    // Also close all open accordion submenus
    $('.sidebar .collapse').collapse('hide');
  }

  // Hamburger button in the topbar (mobile)
  $(document).on('click', '#sidebarToggleTop', function(e) {
    e.preventDefault();
    e.stopPropagation();
    if (isMobile()) {
      if ($(".sidebar").hasClass("mobile-open")) {
        closeMobileSidebar();
      } else {
        openMobileSidebar();
      }
    } else {
      // Desktop: toggle collapsed width
      $("body").toggleClass("sidebar-toggled");
      $(".sidebar").toggleClass("toggled");
      if ($(".sidebar").hasClass("toggled")) {
        $('.sidebar .collapse').collapse('hide');
      }
    }
  });

  // Desktop sidebar collapse button (inside sidebar)
  $(document).on('click', '#sidebarToggle', function(e) {
    e.preventDefault();
    $("body").toggleClass("sidebar-toggled");
    $(".sidebar").toggleClass("toggled");
    if ($(".sidebar").hasClass("toggled")) {
      $('.sidebar .collapse').collapse('hide');
    }
  });

  // Close button inside the sidebar (mobile X button)
  $(document).on('click', '#sidebarCloseMobile', function(e) {
    e.preventDefault();
    e.stopPropagation();
    closeMobileSidebar();
  });

  // Overlay click closes sidebar
  $(document).on('click', '#sidebarOverlay', function() {
    closeMobileSidebar();
  });

  // Escape key closes sidebar
  $(document).on('keydown', function(e) {
    if (e.key === 'Escape' && isMobile()) {
      closeMobileSidebar();
    }
  });

  // On resize: clean up mobile state if switching to desktop
  $(window).on('resize', function() {
    if (!isMobile()) {
      closeMobileSidebar();
      $("body").css("overflow", "");
    } else {
      // On mobile resize, collapse submenus
      $('.sidebar .collapse').collapse('hide');
    }
  });

  // ============================================================
  // DESKTOP: Prevent content wrapper scrolling on sidebar hover
  // ============================================================
  $('body.fixed-nav .sidebar').on('mousewheel DOMMouseScroll wheel', function(e) {
    if ($(window).width() > 768) {
      var e0 = e.originalEvent,
        delta = e0.wheelDelta || -e0.detail;
      this.scrollTop += (delta < 0 ? 1 : -1) * 30;
      e.preventDefault();
    }
  });

  // Scroll to top button appear
  $(document).on('scroll', function() {
    var scrollDistance = $(this).scrollTop();
    if (scrollDistance > 100) {
      $('.scroll-to-top').fadeIn();
    } else {
      $('.scroll-to-top').fadeOut();
    }
  });

  // Smooth scrolling using jQuery easing
  $(document).on('click', 'a.scroll-to-top', function(e) {
    var $anchor = $(this);
    $('html, body').stop().animate({
      scrollTop: ($($anchor.attr('href')).offset().top)
    }, 1000, 'easeInOutExpo');
    e.preventDefault();
  });

})(jQuery);
