<script>
  document.addEventListener('click', (e) => {
    const btn = e.target.closest('.help-toggle');
    const panel = document.querySelector((btn && btn.dataset.target) || '#search-help-panel');

    // Toggle when clicking the help button
    if (btn && panel) {
      panel.classList.toggle('open');
      return;
    }
    // Click outside closes it
    if (panel && panel.classList.contains('open') && !panel.contains(e.target)) {
      const clickedHelpButton = e.target.closest('.help-toggle');
      if (!clickedHelpButton) panel.classList.remove('open');
    }
  });
</script>