(function(){
'use strict';
function compact(v,locale){return new Intl.NumberFormat(locale,{notation:'compact',maximumFractionDigits:2}).format(Number(v||0));}
function full(v,locale){return new Intl.NumberFormat(locale,{maximumFractionDigits:0}).format(Number(v||0));}
function rr(ctx,x,y,w,h,r){r=Math.min(r,Math.abs(w)/2,Math.abs(h)/2);ctx.beginPath();ctx.moveTo(x+r,y);ctx.arcTo(x+w,y,x+w,y+h,r);ctx.arcTo(x+w,y+h,x,y+h,r);ctx.arcTo(x,y+h,x,y,r);ctx.arcTo(x,y,x+w,y,r);ctx.closePath();}
var palette={moh:['#0e9f9a','#42c6bd'],drug:['#7c3aed','#a878ef'],net:['#2f6bd8','#75a4f5'],neutral:['#486581','#8aa4bd']};
function renderHorizontalBar(canvasId,config){
 var canvas=document.getElementById(canvasId); if(!canvas||!config)return {rendered:false,reason:'invalid'};
 var rows=(config.rows||[]).slice().filter(function(r){return Number(r.value||0)!==0;}).sort(function(a,b){return Number(b.value)-Number(a.value);}).slice(0,Number(config.limit||10));
 if(!rows.length)return {rendered:false,empty:true};
 var locale=config.locale||(document.documentElement.lang==='ar'?'ar-SA':'en-US'),rtl=document.documentElement.dir==='rtl',dpr=window.devicePixelRatio||1;
 var rect=canvas.getBoundingClientRect(),W=Math.max(420,rect.width),H=Math.max(360,Number(config.height||420));canvas.width=W*dpr;canvas.height=H*dpr;
 var ctx=canvas.getContext('2d');ctx.setTransform(dpr,0,0,dpr,0,0);ctx.clearRect(0,0,W,H);
 var left=rtl?72:112,right=rtl?112:72,top=18,bottom=26,plotW=W-left-right,rowH=(H-top-bottom)/rows.length,max=Math.max.apply(null,rows.map(function(r){return Number(r.value||0);}).concat([1]));
 var colors=palette[config.tone]||palette.neutral;
 ctx.font='600 12px Segoe UI,Tahoma,Arial';ctx.textBaseline='middle';
 rows.forEach(function(row,i){
   var y=top+i*rowH+rowH*.5,barH=Math.min(18,rowH*.52),barW=Math.max(2,(Number(row.value||0)/max)*plotW),x=rtl?W-right-barW:left;
   ctx.fillStyle='#eef1f7';rr(ctx,rtl?W-right-plotW:left,y-barH/2,plotW,barH,8);ctx.fill();
   var g=ctx.createLinearGradient(x,0,x+barW,0);g.addColorStop(0,colors[0]);g.addColorStop(1,colors[1]);ctx.fillStyle=g;rr(ctx,x,y-barH/2,barW,barH,8);ctx.fill();
   ctx.fillStyle='#23304d';ctx.textAlign=rtl?'right':'left';ctx.fillText(String(row.label),rtl?W-8:8,y);
   ctx.font='800 12px Segoe UI,Tahoma,Arial';ctx.textAlign=rtl?'left':'right';ctx.fillText(compact(row.value,locale),rtl?8:W-8,y);ctx.font='600 12px Segoe UI,Tahoma,Arial';
 });
 canvas.dataset.chartRendered='true';canvas.dataset.chartRows=String(rows.length);
 return {rendered:true,rows:rows.length};
}
window.ExecutiveUI=window.ExecutiveUI||{};
window.ExecutiveUI.Charts={renderHorizontalBar:renderHorizontalBar,formatCompact:compact,formatFull:full};
window.ExecutiveUI.initTable=function(root){(root||document).querySelectorAll('[data-exec-table]').forEach(function(table){table.dataset.execReady='true';});};
document.addEventListener('DOMContentLoaded',function(){window.ExecutiveUI.initTable(document);});
})();