/* DIAR-6B.4-R1.3 — initial-render and BFCache geometry gate. */
(function(){
  'use strict';
  var root=document.documentElement, body=document.body;
  if(!body || String(body.getAttribute('data-native-sidebar-page')||'').indexOf('diar-')!==0) return;
  var samples=[], healed=false, resizeTimer=0;

  function nodes(){
    var shell=document.querySelector('.elite-shell[data-native-shell-contract="R4"]');
    return {
      shell:shell,
      sidebar:shell&&shell.querySelector(':scope > .elite-sidebar[data-native-sidebar-contract="R4"]'),
      main:shell&&shell.querySelector(':scope > .elite-main[data-native-main-contract="R4"]'),
      topbar:shell&&shell.querySelector(':scope > .elite-main[data-native-main-contract="R4"] > .elite-topbar--diar-premium:first-child')
    };
  }
  function clearStaleInlineGeometry(n){
    [n.shell,n.main,n.topbar].forEach(function(el){
      if(!el||!el.style)return;
      ['top','bottom','margin-top','margin-block-start','padding-top','padding-block-start','transform','translate','inset-block-start'].forEach(function(name){el.style.removeProperty(name);});
    });
  }
  function measure(phase,allowHeal){
    if(window.innerWidth<=760){root.dataset.diarInitialRenderGeometry='MOBILE';return true;}
    var n=nodes();
    if(!n.shell||!n.sidebar||!n.main||!n.topbar){root.dataset.diarInitialRenderGeometry='MISSING';return false;}
    var sr=n.sidebar.getBoundingClientRect(), mr=n.main.getBoundingClientRect(), tr=n.topbar.getBoundingClientRect();
    var atTop=Math.abs(window.scrollY||0)<=2;
    var rtl=root.dir==='rtl';
    var mainTop=atTop?Math.abs(mr.top):0;
    var topbarTop=atTop?Math.abs(tr.top):0;
    var sideOk=rtl?Math.abs(sr.right-window.innerWidth)<=3:Math.abs(sr.left)<=3;
    var overlap=Math.max(0,Math.min(sr.right,mr.right)-Math.max(sr.left,mr.left));
    var pass=mainTop<=3&&topbarTop<=3&&sideOk&&overlap<=1;
    samples.push({phase:phase,pass:pass,mainTop:+mainTop.toFixed(2),topbarTop:+topbarTop.toFixed(2),overlap:+overlap.toFixed(2),scrollY:+(window.scrollY||0).toFixed(2)});
    root.dataset.diarInitialRenderGeometry=pass?'PASS':'FAIL';
    root.dataset.diarInitialRenderPhase=phase;
    root.dataset.diarInitialRenderMainTop=mainTop.toFixed(2);
    root.dataset.diarInitialRenderTopbarTop=topbarTop.toFixed(2);
    root.dataset.diarInitialRenderSamples=JSON.stringify(samples.slice(-8));
    if(!pass&&allowHeal&&atTop&&!healed){
      healed=true;
      clearStaleInlineGeometry(n);
      root.classList.add('diar-geometry-self-heal');
      void n.shell.offsetHeight;
      requestAnimationFrame(function(){measure(phase+'_HEALED',false);});
    }
    if(window.console&&console.info){console.info('DIAR_INITIAL_RENDER_GEOMETRY|'+(pass?'PASS':'FAIL')+'|PHASE='+phase+'|MAIN_TOP='+mainTop.toFixed(2)+'|TOPBAR_TOP='+topbarTop.toFixed(2)+'|OVERLAP='+overlap.toFixed(2));}
    return pass;
  }
  function milestones(prefix){
    measure(prefix+'_DOM',true);
    requestAnimationFrame(function(){
      measure(prefix+'_RAF1',true);
      requestAnimationFrame(function(){measure(prefix+'_RAF2',true);});
    });
    window.setTimeout(function(){measure(prefix+'_100MS',true);},100);
    window.setTimeout(function(){measure(prefix+'_500MS',true);},500);
  }
  function boot(){milestones('INITIAL');}
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',boot,{once:true});else boot();
  window.addEventListener('load',function(){measure('LOAD',true);},{once:true});
  window.addEventListener('pageshow',function(event){measure(event.persisted?'PAGESHOW_BFCACHE':'PAGESHOW',true);},{passive:true});
  window.addEventListener('resize',function(){window.clearTimeout(resizeTimer);resizeTimer=window.setTimeout(function(){measure('RESIZE',true);},120);},{passive:true});
})();
