/* OFFICE — /public/assets/js/app.js */

document.addEventListener('DOMContentLoaded', function () {
  // Menu mobile
  const toggle = document.querySelector('.nav-toggle');
  const nav = document.querySelector('.nav');
  if (toggle && nav) {
    toggle.addEventListener('click', () => nav.classList.toggle('open'));
  }

  // Galeria do produto
  const mainImg = document.querySelector('#gallery-main-img');
  document.querySelectorAll('.gallery-thumbs img').forEach(thumb => {
    thumb.addEventListener('click', () => {
      document.querySelectorAll('.gallery-thumbs img').forEach(t => t.classList.remove('active'));
      thumb.classList.add('active');
      if (mainImg) mainImg.src = thumb.dataset.full || thumb.src;
    });
  });

  // Selecionar variação (cor)
  document.querySelectorAll('.color-swatch').forEach(sw => {
    sw.addEventListener('click', () => {
      document.querySelectorAll('.color-swatch').forEach(s => s.classList.remove('selected'));
      sw.classList.add('selected');
      const inp = document.querySelector('#variacao_id');
      if (inp) inp.value = sw.dataset.id;
    });
  });

  // Filtros auto-submit
  document.querySelectorAll('.auto-submit').forEach(el => {
    el.addEventListener('change', () => el.closest('form').submit());
  });
});
