// ---------- MOBILE MENU TOGGLE ----------
const burger = document.getElementById('burger');
const nav = document.querySelector('.nav');
burger && burger.addEventListener('click', () => {
  const opened = burger.getAttribute('aria-expanded') === 'true';
  burger.setAttribute('aria-expanded', String(!opened));
  nav.classList.toggle('open');
});

// ---------- SMOOTH SCROLL ----------
document.querySelectorAll('a[href^="#"], button[data-scroll]').forEach(el => {
  el.addEventListener('click', function(e){
    const selector = this.getAttribute('href') || this.dataset.scroll;
    if(!selector) return;
    const target = document.querySelector(selector);
    if(target){
      e.preventDefault();
      target.scrollIntoView({behavior:'smooth', block:'start'});
    }
  });
});

// ---------- TO TOP BUTTON ----------
const toTop = document.getElementById('toTop');
window.addEventListener('scroll', () => {
  toTop.style.display = window.scrollY > 400 ? 'block' : 'none';
});
toTop && toTop.addEventListener('click', () => window.scrollTo({top:0, behavior:'smooth'}));

// ---------- CONTACT FORM (SIMULATION) ----------
const contactForm = document.getElementById('contactForm');
contactForm && contactForm.addEventListener('submit', e => {
  e.preventDefault();
  alert('Merci — votre demande a été enregistrée (simulation). Nous vous contacterons sous 48h.');
  contactForm.reset();
});

// ---------- MAILTO QUICK SEND ----------
const mailtoBtn = document.getElementById('mailtoBtn');
mailtoBtn && mailtoBtn.addEventListener('click', () => {
  const name = encodeURIComponent(document.getElementById('name')?.value || '—');
  const email = encodeURIComponent(document.getElementById('email')?.value || '—');
  const phone = encodeURIComponent(document.getElementById('phone')?.value || '—');
  const message = encodeURIComponent(document.getElementById('message')?.value || '—');
  const subject = encodeURIComponent('Demande ImmersiHome — ' + name);
  const body = encodeURIComponent(`Nom: ${name}\nEmail: ${email}\nTél: ${phone}\n\n${message}`);
  window.location.href = `mailto:contact@immersihome.example?subject=${subject}&body=${body}`;
});

// ---------- NEWSLETTER SIMULATION ----------
const nl = document.getElementById('newsletterForm');
nl && nl.addEventListener('submit', (e) => {
  e.preventDefault();
  alert('Merci — vous êtes inscrit(e) à la newsletter (simulation).');
  nl.reset();
});

// ---------- VIDEO MODAL ----------
const openVideoBtn = document.getElementById('openVideoBtn');
const videoModal = document.getElementById('videoModal');
const modalClose = document.getElementById('modalClose');
const modalBackdrop = document.getElementById('modalBackdrop');
const modalVideo = document.getElementById('modalVideo');

let lastFocusedBtn = null; // Pour restaurer le focus après fermeture

function openModal(){
  if(!videoModal) return;
  lastFocusedBtn = document.activeElement;
  videoModal.classList.add('open');
  videoModal.setAttribute('aria-hidden','false');
  modalClose.focus(); // focus sur bouton close
  if(modalVideo){ modalVideo.currentTime = 0; modalVideo.play(); }
}
function closeModal(){
  if(!videoModal) return;
  videoModal.classList.remove('open');
  videoModal.setAttribute('aria-hidden','true');
  if(modalVideo){ modalVideo.pause(); modalVideo.currentTime = 0; }
  lastFocusedBtn && lastFocusedBtn.focus(); // restaurer focus
}

openVideoBtn && openVideoBtn.addEventListener('click', openModal);
modalClose && modalClose.addEventListener('click', closeModal);
modalBackdrop && modalBackdrop.addEventListener('click', closeModal);

// ---------- ACCESSIBILITY: CLOSE ESC ----------
document.addEventListener('keydown', (e) => {
  if(e.key === 'Escape') closeModal();
});

// ---------- OPTIONAL: TRAP FOCUS INSIDE MODAL ----------
document.addEventListener('keydown', (e) => {
  if(!videoModal.classList.contains('open')) return;
  if(e.key === 'Tab'){
    const focusable = videoModal.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
    const first = focusable[0];
    const last = focusable[focusable.length -1];
    if(e.shiftKey && document.activeElement === first){ e.preventDefault(); last.focus(); }
    else if(!e.shiftKey && document.activeElement === last){ e.preventDefault(); first.focus(); }
  }
});
