/* nav.js — navigation, mobile menu, theme toggle, BTT, FAQ, scroll reveal */
(function(){
'use strict';
var pRM = window.matchMedia('(prefers-reduced-motion:reduce)');
/* ── nav scroll ── */
var nav = document.querySelector('nav'),
btt = document.querySelector('.btt'),
ticking = false;
window.addEventListener('scroll', function(){
if (!ticking) {
requestAnimationFrame(function(){
var y = window.scrollY;
if (nav) nav.classList.toggle('scrolled', y > 20);
if (btt) btt.classList.toggle('show', y > 400);
ticking = false;
});
ticking = true;
}
}, {passive: true});
/* ── mobile menu ── */
var burger = document.querySelector('.burger'),
mm = document.getElementById('mm'),
mclose = document.querySelector('.mobile-close');
function openMM(){ mm.classList.add('open'); burger.setAttribute('aria-expanded','true'); }
function closeMM(){ mm.classList.remove('open'); burger.setAttribute('aria-expanded','false'); }
function toggleMM(){ if (mm.classList.contains('open')) closeMM(); else openMM(); }
if (burger) burger.addEventListener('click', toggleMM);
if (mclose) mclose.addEventListener('click', closeMM);
if (mm) mm.querySelectorAll('a').forEach(function(a){ a.addEventListener('click', closeMM); });
if (mm) mm.addEventListener('click', function(e){ if (e.target === mm) closeMM(); });
/* ── mobile sub-menu (Ressources) ── */
document.querySelectorAll('.mm-sub-trigger').forEach(function(btn){
btn.addEventListener('click', function(){
var sub = btn.nextElementSibling;
if (sub && sub.classList.contains('mm-sub')){
btn.classList.toggle('open');
sub.classList.toggle('open');
}
});
});
/* ── theme toggle ── */
document.querySelectorAll('.theme-toggle').forEach(function(btn){
btn.addEventListener('click', function(){
var h = document.documentElement;
var isDark = h.getAttribute('data-theme') !== 'light';
h.setAttribute('data-theme', isDark ? 'light' : 'dark');
localStorage.setItem('theme', isDark ? 'light' : 'dark');
var tc = document.querySelector('meta[name="theme-color"]');
if (tc) tc.content = isDark ? '#ffffff' : '#030712';
});
});
/* ── BTT ── */
if (btt) btt.addEventListener('click', function(){
window.scrollTo({top: 0, behavior: pRM.matches ? 'auto' : 'smooth'});
});
/* ── FAQ ── */
document.querySelectorAll('.faq-q').forEach(function(q){
function toggle(){ q.parentElement.classList.toggle('open'); }
q.addEventListener('click', toggle);
q.addEventListener('keydown', function(e){
if (e.key === 'Enter' || e.key === ' '){ e.preventDefault(); toggle(); }
});
});
/* ── smooth scroll ── */
document.querySelectorAll('a[href^="#"]').forEach(function(a){
a.addEventListener('click', function(e){
var t = document.querySelector(this.getAttribute('href'));
if (t){ e.preventDefault(); t.scrollIntoView({behavior: pRM.matches ? 'auto' : 'smooth'}); }
});
});
/* ── scroll reveal ── */
if (!pRM.matches && 'IntersectionObserver' in window){
var obs = new IntersectionObserver(function(entries){
entries.forEach(function(e){
if (e.isIntersecting){
e.target.style.opacity = '1';
e.target.style.transform = 'translateY(0)';
obs.unobserve(e.target);
}
});
}, {threshold: .06, rootMargin: '0px 0px -40px 0px'});
document.querySelectorAll('.bento-card,.prod,.c-card,.pcard-sm,.stp,.chip,.faq-item,.timeline-step,.reveal').forEach(function(el){
el.style.opacity = '0';
el.style.transform = 'translateY(18px)';
el.style.transition = 'opacity .5s ease,transform .5s ease';
obs.observe(el);
});
}
})();