<script>
(function(){
"use strict";
var nav=document.getElementById('nav'),burger=document.getElementById('burger'),drawer=document.getElementById('drawer'),
links=[].slice.call(document.querySelectorAll('.nlinks a'));
document.getElementById('yr').textContent=new Date().getFullYear();
window.addEventListener('scroll',function(){nav.classList.toggle('on',window.pageYOffset>8);},{passive:true});
burger.addEventListener('click',function(){var o=drawer.classList.toggle('open');
burger.setAttribute('aria-expanded',o?'true':'false');document.body.style.overflow=o?'hidden':'';});
drawer.addEventListener('click',function(e){if(e.target.closest('a')){drawer.classList.remove('open');
burger.setAttribute('aria-expanded','false');document.body.style.overflow='';}});
document.querySelectorAll('.faq button').forEach(function(b){
b.addEventListener('click',function(){var f=b.parentElement,a=f.querySelector('.ans'),o=f.classList.toggle('open');
b.setAttribute('aria-expanded',o?'true':'false');a.style.maxHeight=o?(a.scrollHeight+'px'):'0px';});
});
var ids=['problem','warstwy','decyzja','pytania','bezpieczenstwo','pakiety','faq'];
var secs=ids.map(function(id){return document.getElementById(id);}).filter(Boolean);
if('IntersectionObserver' in window&&secs.length){
var so=new IntersectionObserver(function(es){es.forEach(function(e){if(e.isIntersecting){var id='#'+e.target.id;
links.forEach(function(l){l.classList.toggle('act',l.getAttribute('href')===id);});}});},{rootMargin:'-45% 0px -50% 0px'});
secs.forEach(function(s){so.observe(s);});
}
/* ====== VISUAL LAYER: reveals, żywy hero, count-up ====== */
var prm=window.matchMedia&&window.matchMedia('(prefers-reduced-motion: reduce)').matches;
/* scroll reveal z lekkim stagger per kontener */
if(!prm&&'IntersectionObserver' in window){
var rvSel='.sec-head,.hpanel,.flow>*,.pfeature,.pmini,.whatis-pts li,.disarm>div,'+
'.layer .lx,.layer figure,.layer-foot,.dec-grid>div,.dec-foot>div,.qfeat,.qc,.cmp-wrap,.def-close,'+
'.guard li,.steps,.sec-note,.di,.comm-pts li,.comm-chat,.nfc,.nf-yes,.cr-grid>*,.media figure,.plan,.faq';
var els=[].slice.call(document.querySelectorAll(rvSel));
var groups=new Map();
els.forEach(function(el){
var p=el.parentNode,i=groups.get(p)||0;groups.set(p,i+1);
el.classList.add('rv');
el.style.transitionDelay=Math.min(i*70,350)+'ms';
});
var ro=new IntersectionObserver(function(es){es.forEach(function(e){
if(e.isIntersecting){e.target.classList.add('in');ro.unobserve(e.target);}
});},{rootMargin:'0px 0px -8% 0px',threshold:.08});
els.forEach(function(el){ro.observe(el);});
}
/* hero: rotujące pytania (typewriter) + cykl filarów */
var qt=document.querySelector('.hp-qt'),pils=[].slice.call(document.querySelectorAll('.pil'));
if(!prm&&qt){
var qs=['Co zrobić z budżetem, żeby rósł zysk, a nie sam ROAS?',
'Które produkty z PMax naprawdę zarabiają po zwrotach?',
'Płacimy za ruch, który i tak mamy z wyników organicznych?'],
qi=0,ci=qs[0].length,del=0,typing=false;
function tick(){
var t=qs[qi];
if(del>0){del--;setTimeout(tick,del===0?420:34);return;}
if(!typing){ /* kasowanie */
if(ci>0){ci--;qt.textContent='\u201E'+t.slice(0,ci);setTimeout(tick,16);}
else{qi=(qi+1)%qs.length;typing=true;setTimeout(tick,260);}
}else{ /* pisanie */
t=qs[qi];
if(ci<t.length){ci++;qt.textContent='\u201E'+t.slice(0,ci)+(ci===t.length?'\u201D':'');setTimeout(tick,30+Math.random()*40);}
else{typing=false;del=120;setTimeout(tick,34);}
}
}
setTimeout(function(){del=110;tick();},2600);
}
if(!prm&&pils.length===3){
var pi=1; /* startowo aktywny jest 02 */
setInterval(function(){
pi=(pi+1)%3;
pils.forEach(function(p,i){p.classList.toggle('active',i===pi);});
},2800);
}
/* count-up statystyk twórcy */
if(!prm&&'IntersectionObserver' in window){
var vals=[].slice.call(document.querySelectorAll('.stat .v'));
var co=new IntersectionObserver(function(es){es.forEach(function(e){
if(!e.isIntersecting)return;co.unobserve(e.target);
var node=e.target.firstChild;if(!node||node.nodeType!==3)return;
var target=parseInt(node.textContent,10);if(isNaN(target))return;
var t0=null;
function step(ts){if(!t0)t0=ts;var k=Math.min((ts-t0)/900,1);
node.textContent=Math.round(target*(1-Math.pow(1-k,3)));
if(k<1)requestAnimationFrame(step);}
requestAnimationFrame(step);
});},{threshold:.5});
vals.forEach(function(v){co.observe(v);});
}
})();
</script>