/* Generated package embeds DESIGN above this runtime. No network, auth, or external writes. */
figma.showUI(__html__, { width: 438, height: 598, themeColors: true });
let importing = false;
const fontStyles={400:'Regular',500:'Medium',600:'Semi Bold',700:'Bold'};
const fontCache=new Map(),styleCache=new Map(),componentCache=new Map(),colorVars=new Map();
const links=[],frameMap=new Map();let componentPage,boardPage,collection,darkModeId,lightModeId;
function rgb(hex){return {r:parseInt(hex.slice(1,3),16)/255,g:parseInt(hex.slice(3,5),16)/255,b:parseInt(hex.slice(5,7),16)/255};}
function escapeXML(s){return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/"/g,'&quot;');}
function tokenKey(hex,type){if(type==='TEXT'&&hex===DESIGN.palette.paper)return 'text/inverse';const e=Object.entries(DESIGN.palette).find(x=>x[1]===hex);return e?'semantic/'+e[0]:null;}
function color(hex,theme,type){if(!hex||hex==='none')return null;if(theme==='dark'&&!(type==='TEXT'&&hex===DESIGN.palette.paper)){const e=Object.entries(DESIGN.palette).find(x=>x[1]===hex);if(e)hex=DESIGN.darkPalette[e[0]];}return rgb(hex);}
function paints(hex,theme,type){if(!hex||hex==='none')return [];const p={type:'SOLID',color:color(hex,theme,type)};const variable=colorVars.get(tokenKey(hex,type));return variable&&(theme!=='dark'||darkModeId)?[figma.variables.setBoundVariableForPaint(p,'color',variable)]:[p];}
async function font(family,weight){const key=family+'/'+weight;if(fontCache.has(key))return fontCache.get(key);let value={family,style:fontStyles[weight]||'Regular'};try{await figma.loadFontAsync(value);}catch{value={family:'Inter',style:fontStyles[weight]||'Regular'};try{await figma.loadFontAsync(value);}catch{value={family:'Inter',style:'Regular'};await figma.loadFontAsync(value);}}fontCache.set(key,value);return value;}
async function typography(n){const key=n.font+'/'+n.size+'/'+n.weight+'/'+n.lineHeight+'/'+(n.tracking||0);if(styleCache.has(key))return styleCache.get(key);const s=figma.createTextStyle();s.name='Ankra / '+(n.font==='Roboto Mono'?'Mono':'Inter')+' / '+n.size+' / '+n.weight+(n.tracking?' / Tracked':'');s.fontName=await font(n.font,n.weight);s.fontSize=n.size;s.lineHeight={value:n.lineHeight,unit:'PIXELS'};s.letterSpacing={value:n.tracking||0,unit:'PIXELS'};styleCache.set(key,s);return s;}
async function createVariables(){collection=figma.variables.createVariableCollection('Ankra / Semantic colors');lightModeId=collection.defaultModeId;collection.renameMode(lightModeId,'Light');try{darkModeId=collection.addMode('Dark');}catch{darkModeId=null;}
for(const [key,hex] of Object.entries(DESIGN.palette)){const v=figma.variables.createVariable('semantic/'+key,collection,'COLOR');v.setValueForMode(lightModeId,rgb(hex));if(darkModeId)v.setValueForMode(darkModeId,rgb(DESIGN.darkPalette[key]));colorVars.set('semantic/'+key,v);const s=figma.createPaintStyle();s.name='Ankra / '+key;s.paints=[{type:'SOLID',color:rgb(hex)}];}
const inverse=figma.variables.createVariable('text/inverse',collection,'COLOR');inverse.setValueForMode(lightModeId,rgb('#FFFFFF'));if(darkModeId)inverse.setValueForMode(darkModeId,rgb('#FFFFFF'));colorVars.set('text/inverse',inverse);
const spacing=figma.variables.createVariableCollection('Ankra / Spacing & radius');for(const x of [4,8,12,16,24,32,48,64]){const v=figma.variables.createVariable('space/'+x,spacing,'FLOAT');v.setValueForMode(spacing.defaultModeId,x);}for(const [k,x] of [['control',7],['card',12]]){const v=figma.variables.createVariable('radius/'+k,spacing,'FLOAT');v.setValueForMode(spacing.defaultModeId,x);}}
async function primitive(n,parent,theme){let out;if(n.type==='TEXT'){out=figma.createText();parent.appendChild(out);out.fontName=await font(n.font,n.weight);out.characters=n.text;out.fontSize=n.size;out.lineHeight={unit:'PIXELS',value:n.lineHeight};out.letterSpacing={unit:'PIXELS',value:n.tracking||0};out.fills=paints(n.color,theme,'TEXT');out.resize(Math.max(1,n.w),Math.max(1,n.h));out.textAutoResize='HEIGHT';await out.setTextStyleIdAsync((await typography(n)).id);}
else if(n.type==='VECTOR'){const toHex=c=>'#'+[c.r,c.g,c.b].map(v=>Math.round(v*255).toString(16).padStart(2,'0')).join('');const stroke=color(n.stroke,theme,'VECTOR'),fill=color(n.fill,theme,'VECTOR');out=figma.createNodeFromSvg(`<svg xmlns="http://www.w3.org/2000/svg" width="${n.w}" height="${n.h}" viewBox="0 0 ${n.viewW||24} ${n.viewH||24}"><path d="${escapeXML(n.d)}" stroke="${stroke?toHex(stroke):'none'}" fill="${fill?toHex(fill):'none'}" stroke-width="${n.sw||1.5}" stroke-linecap="round" stroke-linejoin="round"/></svg>`);parent.appendChild(out);out.resize(Math.max(1,n.w),Math.max(1,n.h));const bindVectors=node=>{if(node.type==='VECTOR'){node.fills=paints(n.fill,theme,'VECTOR');node.strokes=paints(n.stroke,theme,'VECTOR');}for(const child of node.children||[])bindVectors(child);};bindVectors(out);}
else if(n.type==='FRAME'){out=figma.createFrame();parent.appendChild(out);out.resize(Math.max(1,n.w),Math.max(1,n.h));out.fills=[];out.clipsContent=false;for(const c of n.children)await make(c,out,theme);}
else{out=n.type==='ELLIPSE'?figma.createEllipse():figma.createRectangle();parent.appendChild(out);out.resize(Math.max(.01,n.w),Math.max(.01,n.h));out.fills=paints(n.fill,theme,n.type);if(n.stroke&&n.stroke!=='none'){out.strokes=paints(n.stroke,theme,n.type);out.strokeWeight=n.sw||1;out.strokeAlign='CENTER';}if(n.r&&'cornerRadius'in out)out.cornerRadius=n.r;}
out.name=n.name||n.type;out.x=n.x||0;out.y=n.y||0;if(n.target)links.push([out,n.target]);return out;}
function componentIdentity(n,theme){if(n.componentKey.startsWith('sidebar/'))return n.componentKey+(theme==='dark'&&!darkModeId?'/dark':'');const parts=n.componentKey.split('/');const ico=(n.children||[]).find(c=>c.type==='VECTOR');return parts.slice(0,2).join('/')+'/'+n.h+'/'+(ico?ico.name:'none')+(theme==='dark'&&!darkModeId?'/dark':'');}
async function control(n,parent,theme){const key=componentIdentity(n,theme);let entry=componentCache.get(key);if(!entry){const m=figma.createComponent();componentPage.appendChild(m);m.name='Ankra / '+key;m.resize(n.w,n.h);m.fills=[];m.clipsContent=false;const sidebar=n.componentKey.startsWith('sidebar/');let labelKey;
if(sidebar){const start=links.length;for(const child of n.children)await make(child,m,theme);links.length=start;}else{const bg=n.children.find(c=>c.type==='RECTANGLE');if(bg){m.fills=paints(bg.fill,theme,'FRAME');m.cornerRadius=bg.r||0;if(bg.stroke){m.strokes=paints(bg.stroke,theme,'FRAME');m.strokeWeight=1;}}for(const child of n.children.filter(c=>c!==bg)){const element=await primitive({...child,target:undefined},m,theme);if(element.type==='TEXT'){element.name='Label';labelKey=m.addComponentProperty('Label','TEXT',child.text);element.componentPropertyReferences={characters:labelKey};element.textAutoResize='WIDTH_AND_HEIGHT';}}m.layoutMode='HORIZONTAL';m.primaryAxisSizingMode='FIXED';m.counterAxisSizingMode='FIXED';m.primaryAxisAlignItems='MIN';m.counterAxisAlignItems='CENTER';m.itemSpacing=9;m.paddingLeft=n.children.some(c=>c.type==='VECTOR')?13:12;m.paddingRight=12;m.paddingTop=0;m.paddingBottom=0;}
m.x=(componentCache.size%4)*330;m.y=Math.floor(componentCache.size/4)*160;entry={master:m,labelKey};componentCache.set(key,entry);}
const instance=entry.master.createInstance();parent.appendChild(instance);instance.name=n.name;instance.resize(n.w,n.h);instance.x=n.x;instance.y=n.y;if(entry.labelKey){const t=n.children.find(c=>c.type==='TEXT');instance.setProperties({[entry.labelKey]:t.text});}if(n.componentKey.startsWith('sidebar/')){for(const c of n.children.filter(c=>c.target)){const found=instance.findOne(x=>x.name===c.name&&x.type===c.type);if(found)links.push([found,c.target]);}}if(n.target)links.push([instance,n.target]);return instance;}
async function make(n,parent,theme){return n.type==='FRAME'&&n.componentKey?control(n,parent,theme):primitive(n,parent,theme);}
async function build(scope){componentCache.clear();colorVars.clear();links.length=0;frameMap.clear();const core=['cover','brand-colors','components-ai','overview','ai-home','ai-chat','ai-investigation','ai-plan','ai-execution','inbox','applications','app-overview','stack-builder','clusters','cluster-overview','security','cost','mobile-overview','mobile-inbox'];const selected=scope==='core'?DESIGN.boards.filter(b=>core.includes(b.id)):DESIGN.boards;
boardPage=figma.createPage();boardPage.name='Ankra / Complete redesign';componentPage=figma.createPage();componentPage.name='Ankra / Shared components';await figma.setCurrentPageAsync(boardPage);await createVariables();
for(let i=0;i<selected.length;i++){const b=selected[i],f=figma.createFrame();boardPage.appendChild(f);f.name=b.id+' / '+b.title;f.resize(b.w,b.h);f.x=scope==='core'?80+(i%4)*1520:b.x;f.y=scope==='core'?80+Math.floor(i/4)*1100:b.y;f.fills=paints(b.bg,b.theme,'FRAME');f.clipsContent=true;if(darkModeId)f.setExplicitVariableModeForCollection(collection,b.theme==='dark'?darkModeId:lightModeId);f.setPluginData('ankraScreenId',b.id);f.setPluginData('designDescription',b.description||'');f.setPluginData('chapter',b.chapter);frameMap.set(b.id,f);for(const n of b.children)await make(n,f,b.theme||'light');figma.ui.postMessage({type:'progress',value:Math.round((i+1)/selected.length*91),text:`Created ${i+1} of ${selected.length} frames · ${b.title}`});if(i%4===0)await new Promise(r=>setTimeout(r,0));}
if(scope!=='core'){for(const section of DESIGN.layout.sections){const t=figma.createText();boardPage.appendChild(t);t.fontName=await font('Inter',600);t.characters=section.title+'  /  '+section.count+' frames';t.fontSize=34;t.fills=[{type:'SOLID',color:rgb('#152B22')}];t.x=80;t.y=section.y+24;}}
let connected=0;for(const [node,target]of links){const to=frameMap.get(target);if(to){await node.setReactionsAsync([{trigger:{type:'ON_CLICK'},actions:[{type:'NODE',destinationId:to.id,navigation:'NAVIGATE',transition:null,preserveScrollPosition:false}]}]);connected++;}}
let cx=0,cy=0,rowH=0;for(const e of componentCache.values()){const m=e.master;if(cx+m.width>1400){cx=0;cy+=rowH+64;rowH=0;}m.x=cx;m.y=cy;cx+=m.width+48;rowH=Math.max(rowH,m.height);}
boardPage.backgrounds=[{type:'SOLID',color:rgb('#E6EBE3')}];componentPage.backgrounds=[{type:'SOLID',color:rgb('#E6EBE3')}];await figma.setCurrentPageAsync(boardPage);const focus=frameMap.get('overview')||frameMap.get('cover');figma.currentPage.selection=[focus];figma.viewport.scrollAndZoomIntoView([focus]);figma.commitUndo();figma.ui.postMessage({type:'done',text:`Created ${selected.length} editable frames, ${componentCache.size} shared components, ${styleCache.size} text styles, and ${connected} prototype links.${darkModeId?' Light and dark variable modes are available.':' Dark screens use fixed colors because this file does not support a second variable mode.'}`});figma.notify('Ankra redesign created. Select a frame and press Present to explore.');}
figma.ui.onmessage=async message=>{if(message.type!=='build'||importing)return;importing=true;try{await build(message.scope);}catch(error){figma.ui.postMessage({type:'error',text:String(error&&error.message||error)});importing=false;}};
