<!-- SOLID-PLAYTIME · part 187 -->
);
    buildBar=document.createElement('div');
    buildBar.style.cssText='position:fixed;bottom:18px;left:50%;transform:translateX(-50%);z-index:9999;display:flex;gap:8px;';
    buildBar.appendChild(mkBtn('PLACE',doPlace,true));
    buildBar.appendChild(mkBtn('ROTATE',function(){ ghostRot=(ghostRot+Math.PI/2)%(Math.PI*2); if(ghost) ghost.rotation.y=ghostRot; }));
    buildBar.appendChild(mkBtn('SEAL',sealBuild,false));
    buildBar.appendChild(mkBtn('DEMO',demoCastle,false));
    buildBar.appendChild(mkBtn('EXIT',function(){ setBuild(false); },false));
    document.body.appendChild(buildBar);
    gridTimer=setInterval(function(){
      if(!buildOn) return;
      var hx=snap(Kt.hero.position.x), hz=snap(Kt.hero.position.z);
      gridH.position.set(hx,groundY(hx,hz)+0.06,hz);
    },1000);
    var hk0=heldKey();
    toast(hk0?('BUILD MODE — HOLDING '+(hk0.toUpperCase())):'BUILD MODE — PICK A MATERIAL IN ITEMS','#ffd700');
  }else{
    if(gridH){ Kt.scene.remove(gridH); gridH=null; }
    if(ghost){ Kt.scene.remove(ghost); ghost=null; ghostCell=null; }
    try{ if(window.consoleAimHide) window.consoleAimHide(); }catch(e){}
    if(tapLayer&&tapLayer.parentNode) tapLayer.parentNode.removeChild(tapLayer);
    tapLayer=null;
    if(buildBar&&buildBar.parentNode) buildBar.parentNode.removeChild(buildBar);
    buildBar=null;
    if(gridTimer){ clearInterval(gridTimer); gridTimer=null; }
    sessionPlaced=[];
  }
  buildBtnTx.textContent=on?'EXIT':'BUILD';
}
window.setBuildMode=setBuild;
window.isBuildMode=function(){ return buildOn; };

/* BUILD toggle button */
var buildBtn=document.createElement('button');
buildBtn.id='buildBtn';
buildBtn.className='igl-btn';
var buildBtnTx=document.createElement('span'); buildBtnTx.className='igl-text'; buildBtnTx.textContent='BUILD';
buildBtn.appendChild(buildBtnTx);
buildBtn.style.cssText+=';position:fixed;top:132px;left:8px;z-index:9999;min-width:66px;min-height:40px;padding:8px 6px;font-size:11px;';
buildBtn.addEventListener('click',function(e){ e.stopPropagation(); setBuild(!buildOn); });
document.body.appendChild(buildBtn);
setInterval(function(){
  var inWorld=!!document.querySelector('button.attack');
  buildBtn.style.display=inWorld?'inline-flex':'none';
  if(!inWorld&&buildOn) setBuild(false);
},2000);

/* ============ treasure (chest) ============ */
function treasureIcon(){
  if(window._treasureIcon) return window._treasureIcon;
  var c=document.createElement('canvas'); c.width=c.height=56;
  var g=c.getContext('2d');
  g.fillStyle='#c9a227'; g.strokeStyle='#6e5210'; g.lineWidth=2;
  g.fillRect(12,24,32,20); g.strokeRect(12,24,32,20);
  for(var i=0;i<4;i++){ g.fillRect(12+i*8,17,5,9); g.strokeRect(12+i*8,17,5,9); }
  g.fillStyle='#0d0819';
  g.beginPath(); g.arc(28,36,6,Math.PI,0); g.fill(); g.fillRect(22,36,12,8);
  g.fillStyle='rgba(255,215,0,0.28)'; g.fillRect(12,24,32,20);
  window._treasureIcon=c.toDataURL();
  return window._treasureIcon;
}
window.treasureIcon=treasureIcon;
function sealBuild(customName){
  if(sessionPlaced.length===0){ toast('NOTHING TO SEAL — PLACE BLOCKS FIRST','#888'); return; }
  var vc={}, sc={};
  sessionPlaced.forEach(function(p){
    vc[p.variant]=(vc[p.variant]||0)+1;
    sc[p.shape]=(sc[p.shape]||0)+1;
  });
  function top(o){ var bk=null,bn=0; for(var k in o){ if(o[k]>bn){ bn=o[k]; bk=k; } } return bk; }
  var name=customName||((top(vc)+' '+top(sc)).toUpperCase()+' BUILD');
  var item={id:'t-'+Date.now(),kind:'treasure',name:name,css:'#ffd700',n:sessionPlaced.length};
  try{
    if(window.invCatalog) window.invCatalog.push(item);
    else if(window.invState) toast('CHEST NOT READY','#ff5544');
  }catch(e){}
  toast('SEALED TO CHEST: '+name,'#ffd700');
  try{ Kt.burst(Kt.hero.position.x,Kt.hero.position.y+2,Kt.hero.position.z,0xffd700); }catch(e2){}
  sessionPlaced=[];
  markTreasureSlots();
  return item;
}
window.sealBuild=sealBuild;

/* fix treasure slots in the inventory panel (they render via inv-pack) */
function markTreasureSlots(){
  try{
    var panel=document.getElementById('invPanel'); if(!panel) return;
    var gridEl=null, divs=panel.querySelectorAll('div');
    for(var i=0;i<divs.length;i++){ if(divs[i].style.gridTemplateColumns){ gridEl=divs[i]; break; } }
    if(!gridEl) return;
    var page=(window.invState&&window.invState.page)||0;
    var cat=window.invCatalog||[];
    var slots=gridEl.querySelectorAll('button');
    for(var s=0;s<slots.length;s++){
      var item=cat[page*25+s];
      if(item&&item.kind==='treasure'){
        slots[s].dataset.treasure='1';
        slots[s].style.opacity='1';
        slots[s].style.border='2px solid #ffd700';
        var img=slots[s].querySelector('img');
        if(img&&!img.dataset.tfixed){ img.src=treasureIcon(); img.dataset.tfixed='1'; }
        var ds=slots[s].querySelectorAll('div');
        for(var d=0;d<ds.length;d++){
          if(/×|—/.test(ds[d].textContent)){ ds[d].textContent='★ CHEST'; ds[d].style.color='#ffd700'; }
        }
     