(function(){
// Toggle aria-expanded for navbar toggles / dropdowns
document.addEventListener('click', function(e){
var btn = e.target.closest('[data-toggle="aria"]');
if(!btn) return;
var targetSelector = btn.getAttribute('data-target');
var el = targetSelector ? document.querySelector(targetSelector) : null;
var expanded = btn.getAttribute('aria-expanded') === 'true';
btn.setAttribute('aria-expanded', (!expanded).toString());
if(el) el.classList.toggle('open');
});
// Keyboard close for dropdowns/overlays on ESC
document.addEventListener('keydown', function(e){
if(e.key === 'Escape'){
document.querySelectorAll('.open').forEach(function(x){ x.classList.remove('open'); });
document.querySelectorAll('[aria-expanded="true"]').forEach(function(b){ b.setAttribute('aria-expanded','false'); });
}
});
// Brief product-card animation on add-to-cart (use data-add-btn on button)
document.addEventListener('click', function(e){
var add = e.target.closest('[data-add-btn]');
if(!add) return;
var card = add.closest('.card');
if(card){
card.classList.remove('fade-added');
void card.offsetWidth;
card.classList.add('fade-added');
setTimeout(function(){ card.classList.remove('fade-added'); }, 600);
}
});
// Lazy image fallback for images with data-src
var imgs = Array.prototype.slice.call(document.querySelectorAll('img[data-src]'));
if('IntersectionObserver' in window && imgs.length){
var io = new IntersectionObserver(function(entries){
entries.forEach(function(e){
if(e.isIntersecting){
var i = e.target;
i.src = i.getAttribute('data-src');
i.onload = function(){ i.classList.add('loaded'); }
io.unobserve(i);
}
});
},{rootMargin:'120px'});
imgs.forEach(function(i){ io.observe(i); });
} else {
imgs.forEach(function(i){ i.src = i.getAttribute('data-src'); i.onload = function(){ i.classList.add('loaded'); }});
}
})();