(() => {
  const modal = document.querySelector('[data-dashboard-modal]');
  const openButton = document.querySelector('[data-dashboard-open]');
  const closeButtons = document.querySelectorAll('[data-dashboard-close]');

  if (!modal || !openButton) return;

  const openModal = () => {
    modal.hidden = false;
    document.body.classList.add('ipmc-modal-open');
    const closeButton = modal.querySelector('.ipmc-modal-close');
    if (closeButton) closeButton.focus();
  };

  const closeModal = () => {
    modal.hidden = true;
    document.body.classList.remove('ipmc-modal-open');
    openButton.focus();
  };

  openButton.addEventListener('click', openModal);
  closeButtons.forEach((button) => button.addEventListener('click', closeModal));

  document.addEventListener('keydown', (event) => {
    if (event.key === 'Escape' && !modal.hidden) closeModal();
  });
})();
