/* DIAR-6B.3-R3.1 — passive geometry proof; never mutates layout classes. */
(function(){
  'use strict';
  var root=document.documentElement, body=document.body;
  if(!body || !String(body.dataset.nativeSidebarPage||'').startsWith('diar-')) return;

  function audit(){
    var sidebar=document.querySelector('.elite-shell[data-native-shell-contract="R4"] > .elite-sidebar[data-native-sidebar-contract="R4"]');
    var main=document.querySelector('.elite-shell[data-native-shell-contract="R4"] > .elite-main[data-native-main-contract="R4"]');
    if(!sidebar || !main){ root.dataset.diarRtlPhysicalPlacement='MISSING'; return; }
    if(window.innerWidth<=760){ root.dataset.diarRtlPhysicalPlacement='MOBILE'; return; }
    var s=sidebar.getBoundingClientRect(), m=main.getBoundingClientRect();
    var rtl=root.dir==='rtl';
    var edgeDelta=rtl?Math.abs(s.right-window.innerWidth):Math.abs(s.left);
    var overlap=Math.max(0,Math.min(s.right,m.right)-Math.max(s.left,m.left));
    var reserved=rtl?Math.abs(m.right-s.left):Math.abs(m.left-s.right);
    var pass=edgeDelta<=2 && overlap<=1 && reserved<=2;
    root.dataset.diarRtlPhysicalPlacement=pass?'PASS':'FAIL';
    root.dataset.diarSidebarEdgeDelta=edgeDelta.toFixed(2);
    root.dataset.diarSidebarMainOverlap=overlap.toFixed(2);
    root.dataset.diarSidebarReservedGap=reserved.toFixed(2);
    if(window.console && console.info){
      console.info('DIAR_RTL_PHYSICAL_PLACEMENT|'+(pass?'PASS':'FAIL')+
        '|DIR='+(rtl?'RTL':'LTR')+'|EDGE_DELTA='+edgeDelta.toFixed(2)+
        '|OVERLAP='+overlap.toFixed(2)+'|RESERVED_GAP='+reserved.toFixed(2));
    }
  }
  document.addEventListener('DOMContentLoaded',audit,{once:true});
  window.addEventListener('load',audit,{once:true});
  window.addEventListener('resize',audit,{passive:true});
})();
