(function(){
function getOffset(){
var candidates=['#wpadminbar','.site-header','.header','.l-header','.xnhs-header','header[role="banner"]'];
var offset=12;
candidates.forEach(function(sel){
var el=document.querySelector(sel);
if(!el) return;
var st=window.getComputedStyle(el);
if(st.position==='fixed'||st.position==='sticky') offset+=el.getBoundingClientRect().height||0;
});
return Math.min(Math.max(offset,12),180);
}
function findTarget(a){
var href=a.getAttribute('href')||'';
var key=(a.getAttribute('data-xnbsp-target')||'').replace(/^#/,'');
var id=href.charAt(0)==='#' ? href.slice(1):'';
var target=null;
if(id){
try{ target=document.getElementById(id); }catch(e){}}
if(target) return target;
if(key){
target=document.querySelector('[data-xnbsp-anchor="'+key+'"]')||document.getElementById('xnbsp-'+key+'-entry');
}
return target;
}
document.addEventListener('click', function(e){
var a=e.target.closest&&e.target.closest('.xnbsp a[href^="#"], a[data-xnbsp-target]');
if(!a) return;
var target=findTarget(a);
if(!target) return;
e.preventDefault();
var y=target.getBoundingClientRect().top + window.pageYOffset - getOffset();
window.scrollTo({top:Math.max(0,y),behavior:'smooth'});
try{ history.replaceState(null,'','#'+(target.id||a.getAttribute('data-xnbsp-target'))); }catch(err){}}, false);
})();