 // Regular dropdown toggles
  $('.menu-item-has-children').append(
    '<span class="td_munu_dropdown_toggle"><span></span></span>'
  );

  // Main mobile menu toggle
  $('.td_menu_toggle').on('click', function () {
    $(this)
      .toggleClass('td_toggle_active')
      .siblings('.td_nav_list_wrap')
      .toggleClass('td_active');
  });

  // Universal dropdown handler - works for .menu-item-has-children AND mega menu h4
  $('.td_munu_dropdown_toggle, .td_mega_menu h4').on('click', function (e) {
    e.preventDefault();
    e.stopPropagation();
    
    const $this = $(this);
    let $parent, $subMenu;

    // Detect if this is a regular dropdown or mega menu h4
    if ($this.hasClass('td_munu_dropdown_toggle')) {
      $parent = $this.parent(); // .menu-item-has-children
      $subMenu = $this.siblings('ul, .td_mega_wrapper');
    } else if ($this.is('h4')) {
      $parent = $this.closest('li'); // the li containing h4
      $subMenu = $this.next('ul'); // assume ul comes right after h4
    } else {
      return;
    }

    // If this dropdown is already open, just close it
    if ($parent.hasClass('active')) {
      $subMenu.slideUp(300);
      $parent.removeClass('active');
      $this.removeClass('active');
    } else {
      // Close ALL other open dropdowns on the page, except parents
      $('.menu-item-has-children.active, .td_mega_menu.active, .td_mega_menu li.active')
        .not($parent)
        .not($parent.parents('li')) // Don't close parent mega menu
        .removeClass('active')
        .each(function() {
          $(this).children('ul, .td_mega_wrapper').slideUp(300);
        });

      // Close all h4 sections except current and parent ones
      $('.td_mega_menu h4.active')
        .not($this)
        .not($parent.parents('li').find('h4'))
        .removeClass('active')
        .next('ul').slideUp(300);

      // Also reset all toggle icons
      $('.td_munu_dropdown_toggle.active').not($this).removeClass('active');

      // Open the clicked one
      $subMenu.slideDown(300);
      $parent.addClass('active');
      $this.addClass('active');
    }
  });

  $('.td_header_dropdown_btn').on('click', function () {
    $(this).toggleClass('active');
  });
