(function () {
  const button = document.getElementById('floatingButton');

  window.addEventListener('scroll', () => {
    const full = document.documentElement.scrollHeight;
    const winH = window.innerHeight;
    const denom = Math.max(1, full - winH);
    const pct = (window.scrollY / denom) * 100;
    if (button) button.style.display = pct >= 80 ? 'block' : 'none';
  });

  const getRandomInt = (min, max) =>
    Math.floor(
      Math.random() * (Math.floor(max) - Math.ceil(min) + 1) + Math.ceil(min),
    );

  const updateSlots = () => {
    setTimeout(() => {
      const slot1 = document.querySelector('[data-slot-count]');
      const slot2 = document.querySelector('[data-slot-count2]');
      if (!slot1 || !slot2) return;
      const cur1 = Number(slot1.textContent);
      const cur2 = Number(slot2.textContent);
      if (cur1 <= 2) return;
      slot1.textContent = cur1 - 1;
      slot2.textContent = cur2 + 1;
      updateSlots();
    }, getRandomInt(3, 10) * 1000);
  };
  updateSlots();

  const links = document.querySelectorAll('a');
  const form = document.querySelector('form');
  links.forEach((el) => {
    el.addEventListener('click', (e) => {
      e.preventDefault();
      if (form) form.scrollIntoView({ behavior: 'smooth', block: 'center' });
    });
  });
})();
