(() => {
  "use strict";
  if(window.parent === window)return;
  try {
    if(window.parent.Workspace){
      Object.defineProperty(window,"pywebview",{configurable:true,get:()=>window.parent.pywebview});
      document.documentElement.classList.add("embedded-workspace");
      document.addEventListener("click",event=>{
        const link=event.target.closest("a[href]");
        if(!link || link.hasAttribute("download"))return;
        const href=link.getAttribute("href");
        const match=href.match(/(?:^|\/)(\d-\d\d)\.html/);
        if(match){event.preventDefault();if(href.includes("cad=reviewed"))sessionStorage.setItem("htzd-workspace-cad-ready","1");window.parent.Workspace.navigate("calc/"+match[1]);}
        else if(/section-cad\.html/.test(href)){event.preventDefault();window.parent.Workspace.navigate("cad");}
        else if(/home-|index\.html|classic_modules/.test(href)){event.preventDefault();window.parent.Workspace.navigate("library");}
      });
    }
  } catch(error){console.warn("Workspace attachment unavailable",error);}
})();
