<!-- RUBY-BACKUP · part 421 -->
uildCodes(){
  var b = document.getElementById('buildcodes');
  if(!b){
    b = document.createElement('div');
    b.id = 'buildcodes';
    b.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);z-index:10000;background:rgba(20,10,0,0.95);border:2px solid #ffcc00;border-radius:12px;padding:15px;color:#fff;font:12px monospace;max-width:500px;max-height:85vh;overflow:auto;';
    document.body.appendChild(b);
  }
  b.style.display = 'block';
  renderBuildCodes();
}
function renderBuildCodes(){
  var b = document.getElementById('buildcodes');
  if(!b) return;
  /* filter by category */
  var filtered = BC_CAT==='All' ? BUILD_CODES : BUILD_CODES.filter(function(x){ return x.cat===BC_CAT; });
  var perPage = 25;
  var pages = Math.ceil(filtered.length/perPage);
  if(BC_PAGE >= pages) BC_PAGE = 0;
  var start = BC_PAGE*perPage;
  var page = filtered.slice(start, start+perPage);
  var html = '<b style="color:#fc0">BUILDING CODES — '+filtered.length+'</b> ';
  html += '<button onclick="hideBuildCodes()">CLOSE</button><br><br>';
  /* category filter */
  var cats = ['All','Foundations','Walls','Towers','Bridges','Domes & Arches','Materials','Colors & Finishes','Scale & Size','Soliditronium','Special'];
  html += 'SIFT: ';
  cats.forEach(function(cat){
    html += '<button onclick="bcCat(\''+cat+'\')"'+(cat===BC_CAT?' style="background:#fc0;color:#000"':'')+'>'+cat+'</button> ';
  });
  html += '<br><br>';
  /* codes */
  page.forEach(function(item){
    html += '<div style="padding:5px;border-bottom:1px solid #430;margin-bottom:3px">';
    html += '<b style="color:#fc0">'+item.title+'</b> <small>('+item.code+')</small><br>';
    html += '<i style="color:#aaa">'+item.desc+'</i><br>';
    html += '<button onclick="runCodeByName(\''+item.code+'\')">USE</button>';
    html += '</div>';
  });
  /* pagination */
  html += '<br><div style="text-align:center">';
  if(BC_PAGE>0) html += '<button onclick="bcPage(-1)">PREV</button> ';
  html += 'Page '+(BC_PAGE+1)+'/'+pages+' ';
  if(BC_PAGE<pages-1) html += '<button onclick="bcPage(1)">NEXT</button>';
  html += '</div>';
  b.innerHTML = html;
}
function bcCat(cat){ BC_CAT=cat; BC_PAGE=0; renderBuildCodes(); }
function bcPage(d){ BC_PAGE+=d; renderBuildCodes(); }
function hideBuildCodes(){
  var b = document.getElementById('buildcodes');
  if(b) b.style.display='none';
}

/* PLACEMENT TARGET — select where items go */
var PLACE_MODE = false;
var PLACE_TARGET = null; /* {x, z} */
var placeMarker = null;
function togglePlaceMode(){
  PLACE_MODE = !PLACE_MODE;
  if(!PLACE_MODE){
    PLACE_TARGET = null;
    if(placeMarker && placeMarker.parent) placeMarker.parent.remove(placeMarker);
    placeMarker = null;
  }
  codeStatus(PLACE_MODE ? 'PLACEMENT — tap the map to choose where items go' : 'placement off — items go to spawn point');
  updatePlaceToggle();
}
function setPlaceTarget(x, z){
  PLACE_TARGET = {x: Math.round(x), z: Math.round(z)};
  showPlaceMarker();
  codeStatus('items will place at '+PLACE_TARGET.x+', '+PLACE_TARGET.z);
}
function showPlaceMarker(){
  if(!PLACE_TARGET || typeof THREE==='undefined') return;
  if(placeMarker && placeMarker.parent) placeMarker.parent.remove(placeMarker);
  var geo = new THREE.RingGeometry(0.5, 0.7, 32);
  var mat = new THREE.MeshBasicMaterial({color: 0xffcc00, transparent: true, opacity: 0.9, side: THREE.DoubleSide});
  placeMarker = new THREE.Mesh(geo, mat);
  placeMarker.rotation.x = -Math.PI/2;
  var gy = 0;
  if(typeof fortifiedGroundY==='function') gy = fortifiedGroundY(PLACE_TARGET.x, PLACE_TARGET.z);
  placeMarker.position.set(PLACE_TARGET.x*0.13, gy+0.05, PLACE_TARGET.z*0.13);
  if(typeof scene!=='undefined') scene.add(placeMarker);
}

/* REMOVE TO PLACE — clear existing, make room for new */
var REPLACE_MODE = false;
function toggleReplaceMode(){
  REPLACE_MODE = !REPLACE_MODE;
  codeStatus(REPLACE_MODE ? 'REPLACE — new items clear the spot first' : 'replace off — items stack on occupied spots');
}

/* CAPSULE STORAGE — removed items go to the Soliditronium */
var CAPSULE_ITEMS = [];
function loadCapsuleItems(){
  try{ CAPSULE_ITEMS = JSON.parse(localStorage.getItem('ruby-capsule-items')||'[]'); }catch(e){ CAPSULE_ITEMS=[]; }
}
function saveCapsuleItems(){
  try{ localStorage.setItem('ruby-capsule-items', JSON.stringify(CAPSULE_ITEMS)); }catch(e){}
}
function sendToCapsule(stoneData){
  /* stoneData: {x,z,y,s,c,shape} — archived, not lost */
  CAPSULE_ITEMS.push({
    x: stoneData.x, z: stoneData.z, y: stoneData.y||0,
    s: stoneData.s||2, c: stoneData.c||'#888888',
    shape: stoneData.shape||'b',
    removedAt: Date.now()
  });
  saveCapsuleItems();
}
function capsuleBeam(){
  /* visual: items fly to the capsule */
  if(typeof THREE==='undefined' || typeof scene==='undefined') return;
  if(typeof mePos==='undefined') return;
  var start = new THREE.Vector3(mePos.x*0.13, 2, mePos.z*0.13);
  var end = new THREE.Vector3(28*0.13, 2, 28*0.13); /* capsule at 28,28 */
  va