/* DIAR-6B.4-R1.2 — passive LTR top-gap geometry audit. */
(function(){
  'use strict';
  var root=document.documentElement, body=document.body;
  if(!body || !String(body.dataset.nativeSidebarPage||'').startsWith('diar-')) return;
  function audit(){
    if(root.dir!=='ltr'){root.dataset.diarLtrTopGap='NOT_LTR';return;}
    if(window.innerWidth<=760){root.dataset.diarLtrTopGap='MOBILE';return;}
    var shell=document.querySelector('.elite-shell[data-native-shell-contract="R4"]');
    var sidebar=shell&&shell.querySelector(':scope > .elite-sidebar[data-native-sidebar-contract="R4"]');
    var main=shell&&shell.querySelector(':scope > .elite-main[data-native-main-contract="R4"]');
    var topbar=main&&main.querySelector(':scope > .elite-topbar--diar-premium');
    if(!shell||!sidebar||!main||!topbar){root.dataset.diarLtrTopGap='MISSING';return;}
    var sr=sidebar.getBoundingClientRect(), mr=main.getBoundingClientRect(), tr=topbar.getBoundingClientRect();
    var style=getComputedStyle(shell), areas=String(style.gridTemplateAreas||'');
    var atDocumentTop=window.scrollY<=2;
    var mainTop=Math.abs(mr.top);
    var topbarTop=atDocumentTop?Math.abs(tr.top):0;
    var sideEdge=Math.abs(sr.left);
    var overlap=Math.max(0,Math.min(sr.right,mr.right)-Math.max(sr.left,mr.left));
    var pass=areas.indexOf('native-sidebar')!==-1 && areas.indexOf('native-main')!==-1 && mainTop<=2 && topbarTop<=2 && sideEdge<=2 && overlap<=1;
    root.dataset.diarLtrTopGap=pass?'PASS':'FAIL';
    root.dataset.diarLtrGridAreas=areas.replace(/\s+/g,' ').trim();
    root.dataset.diarLtrMainTop=mainTop.toFixed(2);
    root.dataset.diarLtrTopbarTop=topbarTop.toFixed(2);
    root.dataset.diarLtrSidebarOverlap=overlap.toFixed(2);
    if(window.console&&console.info){console.info('DIAR_LTR_TOP_GAP|'+(pass?'PASS':'FAIL')+'|AREAS='+root.dataset.diarLtrGridAreas+'|MAIN_TOP='+mainTop.toFixed(2)+'|TOPBAR_TOP='+topbarTop.toFixed(2)+'|OVERLAP='+overlap.toFixed(2));}
  }
  document.addEventListener('DOMContentLoaded',audit,{once:true});
  window.addEventListener('load',audit,{once:true});
  window.addEventListener('resize',audit,{passive:true});
})();
