const D=window.REAL_ESTATE_SYSTEM;const $=s=>document.querySelector(s),$$=s=>[...document.querySelectorAll(s)];const byId=new Map(D.nodes.map(n=>[n.id,n]));let selected=null;$('#stats').innerHTML=Object.entries(D.stats).map(([k,v])=>'<div class="stat"><b>'+v.toLocaleString()+'</b><span>'+k+'</span></div>').join('');[...new Set(D.nodes.map(n=>n.family))].sort().forEach(v=>$('#family').add(new Option(v,v)));[...new Set(D.nodes.map(n=>n.kind))].sort().forEach(v=>$('#kind').add(new Option(v.replaceAll('-',' '),v)));function filtered(){const q=$('#search').value.toLowerCase(),f=$('#family').value,k=$('#kind').value;return D.nodes.filter(n=>(!q||(n.name+' '+n.essence+' '+n.archetype).toLowerCase().includes(q))&&(!f||n.family===f)&&(!k||n.kind===k))}function render(){const rows=filtered();$('#nodeList').innerHTML=rows.map(n=>'<button class="node '+(selected===n.id?'selected':'')+'" data-id="'+n.id+'"><small>'+n.family+'</small><strong>'+n.name+'</strong><div>'+n.archetype+'</div></button>').join('');$$('.node').forEach(b=>b.onclick=()=>show(b.dataset.id))}function show(id){selected=id;const n=byId.get(id);const neighbors=D.edges.filter(e=>e.s===id||e.t===id).slice(0,12).map(e=>{const o=byId.get(e.s===id?e.t:e.s);return '<span class="pill">'+e.p.replaceAll('-',' ')+' · '+(o?.name||'')+'</span>'}).join('');$('#detail').innerHTML='<div class="archetype">'+n.archetype+'</div><h2>'+n.name+'</h2><p>'+n.essence+'</p><blockquote>'+n.divination+'</blockquote><h3>Question</h3><p>'+n.question+'</p><div class="giftwound"><div><h3>Gift</h3><p>'+n.gift+'</p></div><div><h3>Wound / Shadow</h3><p>'+n.wound+'</p></div></div><h3>Graph neighborhood</h3><div>'+neighbors+'</div><p><a href="../nodes/'+n.folder+'/'+n.slug+'.md">Open complete node entry →</a></p>';render()}['search','family','kind'].forEach(id=>$('#'+id).addEventListener('input',render));$$('nav [data-view]').forEach(b=>b.onclick=()=>{$$('nav button,.view').forEach(x=>x.classList.remove('active'));b.classList.add('active');$('#'+b.dataset.view).classList.add('active');if(b.dataset.view==='graph')draw()});const canvas=$('#graphCanvas'),ctx=canvas.getContext('2d');function draw(center=null){const rect=canvas.getBoundingClientRect(),dpr=devicePixelRatio||1;canvas.width=rect.width*dpr;canvas.height=rect.height*dpr;ctx.setTransform(dpr,0,0,dpr,0,0);const W=rect.width,H=rect.height;ctx.clearRect(0,0,W,H);let chosen=center?D.nodes.filter(n=>n.id===center||D.edges.some(e=>(e.s===center&&e.t===n.id)||(e.t===center&&e.s===n.id))):D.nodes.filter((_,i)=>i%7===0).slice(0,95);const ids=new Set(chosen.map(n=>n.id));const pos=new Map(chosen.map((n,i)=>[n.id,{x:50+(n.x/1024)*(W-100),y:50+(n.y/760)*(H-100)}]));ctx.globalAlpha=.35;ctx.strokeStyle='#c79a4a';D.edges.filter(e=>ids.has(e.s)&&ids.has(e.t)).forEach(e=>{const a=pos.get(e.s),b=pos.get(e.t);ctx.beginPath();ctx.moveTo(a.x,a.y);ctx.lineTo(b.x,b.y);ctx.stroke()});ctx.globalAlpha=1;chosen.forEach(n=>{const p=pos.get(n.id);ctx.beginPath();ctx.fillStyle=n.id===center?'#e4c477':n.domain==='Economics'?'#78aeba':n.domain==='Governance'?'#b06b4d':n.domain==='Technology'?'#b8c6c2':'#3c7a66';ctx.arc(p.x,p.y,n.id===center?9:5,0,Math.PI*2);ctx.fill();if(n.id===center){ctx.fillStyle='#e8dfc9';ctx.font='14px system-ui';ctx.fillText(n.name,p.x+14,p.y+5)}})}$('#graphSearch').addEventListener('change',()=>{const q=$('#graphSearch').value.toLowerCase();const n=D.nodes.find(n=>n.name.toLowerCase().includes(q));if(n)draw(n.id)});$('#resetGraph').onclick=()=>draw();const shifts=D.nodes.filter((_,i)=>i%9===0);let si=0;$('#shiftButton').onclick=()=>{const n=shifts[si++%shifts.length];$('#defaultMap').textContent=n.name+' is only a '+n.kind.replaceAll('-',' ')+'.';$('#changedMap').textContent=n.name+' becomes '+n.archetype+': '+n.essence;$('#shiftQuestion').textContent=n.question};render();
