const menuBtn=document.querySelector('.menu-toggle');
const nav=document.querySelector('.main-nav');
menuBtn?.addEventListener('click',()=>{const open=menuBtn.getAttribute('aria-expanded')==='true';menuBtn.setAttribute('aria-expanded',String(!open));nav.classList.toggle('open')});
nav?.querySelectorAll('a').forEach(a=>a.addEventListener('click',()=>{nav.classList.remove('open');menuBtn?.setAttribute('aria-expanded','false')}));

const observer=new IntersectionObserver(entries=>{entries.forEach(entry=>{if(entry.isIntersecting){entry.target.classList.add('visible');observer.unobserve(entry.target)}})},{threshold:.12});
document.querySelectorAll('.reveal').forEach(el=>observer.observe(el));


document.getElementById('year').textContent=new Date().getFullYear();

const carousel=document.querySelector('[data-carousel]');if(carousel){const track=carousel.querySelector('.carousel-track'),cards=[...track.children],prev=carousel.querySelector('.carousel-prev'),next=carousel.querySelector('.carousel-next'),dots=carousel.querySelector('.carousel-dots');let index=0;const per=()=>innerWidth<=650?1:innerWidth<=1180?2:3,max=()=>Math.max(0,cards.length-per()),gap=()=>parseFloat(getComputedStyle(cards[0]).marginRight)||0,render=()=>{index=Math.min(index,max());track.style.transform=`translateX(-${index*(cards[0].getBoundingClientRect().width+gap())}px)`;[...dots.children].forEach((d,i)=>d.classList.toggle('active',i===index));prev.disabled=index===0;next.disabled=index===max()},build=()=>{dots.innerHTML='';for(let i=0;i<=max();i++){const b=document.createElement('button');b.type='button';b.setAttribute('aria-label',`Ir al grupo ${i+1}`);b.onclick=()=>{index=i;render()};dots.appendChild(b)}};prev.onclick=()=>{index=Math.max(0,index-1);render()};next.onclick=()=>{index=Math.min(max(),index+1);render()};addEventListener('resize',()=>{build();render()});build();render()}
const seoVideo=document.querySelector('.seo-video[data-src]');if(seoVideo){new IntersectionObserver(entries=>entries.forEach(e=>{if(e.isIntersecting){if(!seoVideo.src)seoVideo.src=seoVideo.dataset.src;seoVideo.play().catch(()=>{})}else seoVideo.pause()}),{rootMargin:'250px 0px',threshold:.1}).observe(seoVideo)}
