<!-- RUBY-BACKUP · part 422 -->
r 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*dz2) || 1;
        dx2 /= len2; dz2 /= len2;
        var fx = ox2 + dx2 * 6;
        var fz = oz2 + dz2 * 6;
        return {x: Math.round(fx/0.13), z: Math.round(fz/0.13)};
      }
    }catch(e){}
  }
  if(typeof getSpawnPoint==='function') return getSpawnPoint();
  return {x: 8, z: 8};
}
function updatePlaceToggle(){
  var btn = document.getElementById('placetogglebtn');
  if(btn){
    btn.textContent = '\u25C9';
    btn.style.background = PLACE_MODE ? '#fff2aa' : '#d8a920';
    btn.style.color = '#111';
    btn.style.boxShadow = PLACE_MODE ? '0 0 12px #d8a920' : 'none';
  }
}
/* place toggle button */
setTimeout(function(){
  if(!document.getElementById('placetogglebtn')){
    var b = document.createElement('button');
    b.id = 'placetogglebtn';
    b.textContent = '\u25C9';
    b.title = 'PLACE';
    b.style.cssText = 'position:fixed;bottom:478px;right:14px;z-index:9999;width:46px;height:46px;background:#d8a920;color:#111;border:1px solid #d8a920;border-radius:50%;font-size:24px;line-height:1;padding:0;cursor:pointer;-webkit-tap-highlight-color:transparent;';
    b.onclick = togglePlaceMode;
    document.body.appendChild(b);
    updatePlaceToggle();
  }
}, 4000);
/* hook map taps when PLACE_MODE is on */
document.addEventListener('click', function(e){
  if(!PLACE_MODE) return;
  /* raycast to find ground position */
  if(typeof raycaster!=='undefined' && typeof camera!=='undefined'){
    var mouse = new THREE.Vector2();
    mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
    raycaster.setFromCamera(mouse, camera);
    /* intersect with ground plane */
    var plane = new THREE.Plane(new THREE.Vector3(0,1,0), 0);
    var pt = new THREE.Vector3();
    if(raycaster.ray.intersectPlane(plane, pt)){
      var gx = Math.round(pt.x / 0.13);
      var gz = Math.round(pt.z / 0.13);
      setPlaceTarget(gx, gz);
    }
  }
}, true);

/* KIDMODE — big buttons, no typing, they call it the dome and the pad */
var KIDMODE_ON = false;
function toggleKidmode(){
  KIDMODE_ON = !KIDMODE_ON;
  var k = document.getElementById('kidmode');
  if(KIDMODE_ON){
    if(!k){
      k = document.createElement('div');
      k.id = 'kidmode';
      k.style.cssText = 'position:fixed;bottom:10px;left:50%;transform:translateX(-50%);z-index:10001;display:flex;gap:10px;padding:10px;';
      k.innerHTML =
        '<button onclick="kidDo