(function () {
  // Menu mobile
  var btn = document.getElementById('menuBtn');
  var nav = document.getElementById('mainNav');
  if (btn && nav) {
    btn.addEventListener('click', function () {
      var open = nav.classList.toggle('open');
      btn.setAttribute('aria-expanded', open ? 'true' : 'false');
      btn.textContent = open ? '✕' : '☰';
    });
    nav.querySelectorAll('a').forEach(function (a) {
      a.addEventListener('click', function () {
        nav.classList.remove('open');
        btn.textContent = '☰';
        btn.setAttribute('aria-expanded', 'false');
      });
    });
  }

  // Carrousel des bannières (seulement s'il est présent sur la page)
  var track = document.getElementById('adtrack');
  if (!track) return;
  var slides = Array.prototype.slice.call(track.children);
  var dotsBox = document.getElementById('addots');
  var dots = dotsBox ? Array.prototype.slice.call(dotsBox.children) : [];
  if (slides.length === 0) return;
  var i = 0, timer = null;
  var reduce = window.matchMedia('(prefers-reduced-motion:reduce)').matches;
  function setDots(n){ dots.forEach(function(d,k){ d.classList.toggle('active', k===n); }); }
  function go(n){ i=(n+slides.length)%slides.length; track.scrollTo({left:slides[i].offsetLeft,behavior:'smooth'}); setDots(i); }
  dots.forEach(function(d,k){ d.addEventListener('click', function(){ go(k); }); });
  function start(){ if(reduce || slides.length<2) return; timer=setInterval(function(){ go(i+1); },5000); }
  function stop(){ if(timer){ clearInterval(timer); timer=null; } }
  var car = document.querySelector('.carousel');
  if (car){ car.addEventListener('mouseenter', stop); car.addEventListener('mouseleave', start); }
  var st;
  track.addEventListener('scroll', function(){ clearTimeout(st); st=setTimeout(function(){ var n=Math.round(track.scrollLeft/track.clientWidth); if(n!==i){ i=n; setDots(i); } },80); });
  start();
})();

// Galerie de la page détail : cliquer une miniature change l'image principale
(function () {
  var main = document.getElementById('mainImg');
  if (!main) return;
  var thumbs = document.querySelectorAll('.thumbs .thumb');
  thumbs.forEach(function (t) {
    t.addEventListener('click', function () {
      main.src = t.getAttribute('data-full');
      thumbs.forEach(function (x) { x.classList.remove('active'); });
      t.classList.add('active');
    });
  });
})();