// Scroll to Top
window.onscroll = function() {
  const style_accessories_button = document.querySelector('.scroll-top-box');
  if (style_accessories_button) {
    if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
      style_accessories_button.style.display = "block";
    } else {
      style_accessories_button.style.display = "none";
    }
  }
};

const scrollTopLink = document.querySelector('.scroll-top-box a');
if (scrollTopLink) {
  scrollTopLink.onclick = function(event) {
    event.preventDefault();
    window.scrollTo({ top: 0, behavior: 'smooth' });
  };
}

// products tabs
document.addEventListener("DOMContentLoaded", () => {
  const style_accessories_tabTitles = [...document.querySelectorAll(".tab-title")];
  const style_accessories_tabContents = [...document.querySelectorAll(".tab-content")];

  if (!style_accessories_tabTitles.length) return;

  // Default: activate first tab
  style_accessories_tabTitles.forEach((tab, index) => {
    tab.classList.toggle("active", index === 0);
    tab.setAttribute("tabindex", "0"); // make focusable by keyboard
  });
  style_accessories_tabContents.forEach((content, index) => {
    content.classList.toggle("active", index === 0);
  });

  // Mouse click handling (FIXED)
  document.addEventListener("click", (e) => {
    const style_accessories_clickedTab = e.target.closest(".tab-title");
    if (style_accessories_clickedTab) {
      const style_accessories_clickedIndex = style_accessories_tabTitles.indexOf(style_accessories_clickedTab);
      if (style_accessories_clickedIndex !== -1) {
        style_accessories_setActive(style_accessories_clickedIndex);
      }
    }
  });

  // Keyboard navigation
  document.addEventListener("keydown", (e) => {
    const style_accessories_activeIndex = style_accessories_tabTitles.findIndex(tab => tab.classList.contains("active"));
    let style_accessories_newIndex = style_accessories_activeIndex;

    if (e.key === "ArrowRight") {
      style_accessories_newIndex = (style_accessories_activeIndex + 1) % style_accessories_tabTitles.length;
      style_accessories_tabTitles[style_accessories_newIndex].focus();
      style_accessories_setActive(style_accessories_newIndex);
    } else if (e.key === "ArrowLeft") {
      style_accessories_newIndex = (style_accessories_activeIndex - 1 + style_accessories_tabTitles.length) % style_accessories_tabTitles.length;
      style_accessories_tabTitles[style_accessories_newIndex].focus();
      style_accessories_setActive(style_accessories_newIndex);
    } else if (e.key === "Enter" || e.key === " ") {
      const focusedIndex = style_accessories_tabTitles.indexOf(document.activeElement);
      if (focusedIndex !== -1) style_accessories_setActive(focusedIndex);
    }
  });

  function style_accessories_setActive(index) {
    style_accessories_tabTitles.forEach((tab, i) => tab.classList.toggle("active", i === index));
    style_accessories_tabContents.forEach((content, i) => content.classList.toggle("active", i === index));
  }
});