const scrollButton = document.querySelector(".scroll-to-top");

window.addEventListener("scroll", function () {
  const scrollTop = window.scrollY;
  const clientHeight = window.innerHeight;

  if (scrollTop > clientHeight) {
    scrollButton.classList.add("visible");
  } else {
    scrollButton.classList.remove("visible");
  }
});

scrollButton.addEventListener("click", function (event) {
  event.preventDefault();

  const duration = 100;
  const startPosition = window.pageYOffset;
  const startTime = performance.now();

  function scrollAnimation(currentTime) {
    const timeElapsed = currentTime - startTime;
    const progress = Math.min(timeElapsed / duration, 1);
    const ease = progress * (2 - progress);
    const scrollTo = startPosition * (1 - ease);

    window.scrollTo(0, scrollTo);

    if (timeElapsed < duration) {
      requestAnimationFrame(scrollAnimation);
    }
  }

  requestAnimationFrame(scrollAnimation);
});
