<!-- RUBY-BACKUP · part 176 -->
!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 */
  var geo = new THREE.BufferGeometry().setFromPoints([start, end]);
  var mat = new THREE.LineBasicMaterial({color: 0xffcc00, transparent: true, opacity: 0.9});
  var beam = new THREE.Line(geo, mat);
  scene.add(beam);
  var op = 0.9;
  var iv = setInterval(function(){
    op -= 0.15;
    mat.opacity = op;
    if(op<=0){ scene.remove(beam); clearInterval(iv); }
  }, 80);
}
setTimeout(loadCapsuleItems, 2000);
function clearAreaAt(x, z, radius){
  if(typeof stones==='undefined') return 0;
  var removed = 0;
  /* find stones in radius — send to capsule, not deleted */
  for(var i=stones.length-1; i>=0; i--){
    var s = stones[i];
    var dx = s.x - x, dz = s.z - z;
    if(Math.sqrt(dx*dx+dz*dz) <= radius){
      /* archive to the capsule at the Soliditronium */
      sendToCapsule({x: s.x, z: s.z, y: s.y||0, s: s.s||2, c: s.c||'#888888', shape: s.shape||'b'});
      /* remove from map */
      if(s.mesh && s.mesh.parent) s.mesh.parent.remove(s.mesh);
      stones.splice(i, 1);
      removed++;
    }
  }
  if(removed>0) capsuleBeam();
  return removed;
}
function clearPlaceTarget(){
  var pos = getPlacePos();
  var n = clearAreaAt(pos.x, pos.z, 3);
  codeStatus(n ? 'cleared '+n+' stones — ready for new' : 'spot already clear');
}
/* hook into placeStone: if REPLACE_MODE and target occupied, clear first */
var origPlaceStone = placeStone;
placeStone = function(cx, cz, rubies, shapeKey, instant, quiet, yRubies, onLand){
  if(REPLACE_MODE && typeof stoneAt!=='undefined'){
    var yy = (typeof yRubies!=='undefined') ? yRubies : 0;
    if(stoneAt(cx, cz, yy)){
      clearAreaAt(cx, cz, 1);
    }
  }
  return origPlaceStone(cx, cz, rubies, shapeKey, instant, quiet, yRubies, onLand);
};
function getPlacePos(){
  /* returns placement target, or IN FRONT of camera as default */
  if(PLACE_MODE && PLACE_TARGET) return PLACE_TARGET;
  if(PLACE_MODE){
    /* no target set — place in front of you */
    try{
      if(typeof camera!=='undefined' && camera){
        var dx2, dz2, ox2, oz2;
        if(typeof CROWN!=='undefined' && CROWN){
          var dir2 = new THREE.Vector3();
          camera.getWorldDirection(dir2);
          dx2 = dir2.x; dz2 = dir2.z;
          ox2 = camera.position.x; oz2 = camera.position.z;
        } else {
          var px2 = 0, pz2 = 0;
          if(typeof myAvatar!=='undefined' && myAvatar){ px2 = myAvatar.position.x; pz2 = myAvatar.position.z; }
          dx2 = px2 - camera.position.x;
          dz2 = pz2 - camera.position.z;
          ox2 = px2; oz2 = pz2;
        }
        var len2 = Math.sqrt(dx2*dx2 + dz2*d