<!-- RUBY-BACKUP · part 95 -->
rap');
  if(wrap) wrap.style.display = 'flex';
  /* Hide after 5s if full */
  if(window._soliditronium >= 100 && window._obligation >= 100){
    setTimeout(function(){ if(wrap) wrap.style.display = 'none'; }, 3000);
  }
};
/* Fall damage — hitting the ground hard hurts (unless admin) */
window._lastFallY = 0;
/* Global camera nudge for the View joystick — defined at startup */
window.nudgeCamera = function(dx, dy){
  try{
    if(typeof WALK !== 'undefined' && WALK){
      if(typeof camYaw !== 'undefined'){
        camYaw -= dx*0.005;
        camPitch = Math.max(-1.5, Math.min(1.5, camPitch - dy*0.005)); /* look up to the sky — King's order */
        /* Dual-stick flight: View joystick is the flight stick */
        if(typeof FLYM !== 'undefined' && FLYM){
          if(typeof aimYaw !== 'undefined' && typeof camYaw !== 'undefined') aimYaw = camYaw;
          if(typeof aimPitch !== 'undefined' && typeof camPitch !== 'undefined') aimPitch = -camPitch;
          /* Store flight stick input for the physics loop */
          window._flightStickX = dx/40;
          window._flightStickY = -dy/40;
        }
      }
    }else if(typeof camGoal !== 'undefined'){
      camGoal.th -= dx*0.005;
      camGoal.ph = Math.max(0.15, Math.min(1.45, camGoal.ph - dy*0.005));
    }
  }catch(e){}
};
/* Flight stick decay — reset when View joystick released */
if(typeof window._flightStickX === 'undefined'){ window._flightStickX = 0; window._flightStickY = 0; }
/* the camera keeps your eyes where you left them across refreshes */
var CAMKEY = 'ruby-cam-v1';
function saveCam(){
  try{ localStorage.setItem(CAMKEY, JSON.stringify({th:camGoal.th, ph:camGoal.ph, r:camGoal.r})); }catch(e){}
}
try{
  var sc = JSON.parse(localStorage.getItem(CAMKEY)||'null');
  if(sc && isFinite(sc.th) && isFinite(sc.ph) && isFinite(sc.r)){
    camT.th = camGoal.th = sc.th;
    camT.ph = camGoal.ph = Math.max(0.15, Math.min(1.45, sc.ph));
    camT.r  = camGoal.r  = Math.max(6, Math.min(70, sc.r));
  }
}catch(e){}
window.addEventListener('pagehide', saveCam);
function resize(){
  var w = window.innerWidth, h = window.innerHeight;
  renderer.setSize(w, h, false);
  camera.aspect = w/h; camera.updateProjectionMatrix();
}
window.addEventListener('resize', resize); resize();

scene.add(new T.HemisphereLight(0xffe6b0, 0x1a0f08, 0.5));
var sun = new T.DirectionalLight(0xfff2cc, 0.75); sun.position.set(30, 48, 18); scene.add(sun);

/* ---- the flat surface + the gold grid ---- */
var ground = new T.Mesh(
  new T.PlaneGeometry(MAP_UNITS, MAP_UNITS),
  new T.MeshStandardMaterial({color:0x0d0a05, roughness:0.95, metalness:0.05})
);
ground.rotation.x = -Math.PI/2; scene.add(ground);
/* the land's grid — gold lines draped over the hills, fixed for building */
var landGrid = null;
function buildLandGrid(){
  if(landGrid){ scene.remove(landGrid); landGrid = null; }
  var half = 49, step = 4, pts = [], x, z, s;
  for(x=-half; x<=half+0.001; x+=step){
    for(z=-half; z<half; z+=2){ pts.push(x, groundY(x,z)+0.05, z, x, groundY(x,z+2)+0.05, z+2); }
  }
  for(z=-half; z<=half+0.001; z+=step){
    for(x=-half; x<half; x+=2){ pts.push(x, groundY(x,z)+0.05, z, x+2, groundY(x+2,z)+0.05, z); }
  }
  var g = new T.BufferGeometry();
  g.setAttribute('position', new T.Float32BufferAttribute(pts, 3));
  landGrid = new T.LineSegments(g, new T.LineBasicMaterial({color:0xffd700, transparent:true, opacity:0.35, fog:false}));
  scene.add(landGrid);
}

/* ---- the ruby: Minimum-sized, at the center, on a gold ring ---- */
var rubyGrp = new T.Group(); scene.add(rubyGrp);
var ruby = new T.Mesh(
  new T.SphereGeometry(window.MINIMUM/2, 24, 18),
  new T.MeshStandardMaterial({color:0xb3001b, emissive:0xff1a2e, emissiveIntensity:2.2, roughness:0.15, metalness:0.4})
);
ruby.position.y = 1.1; rubyGrp.add(ruby);
var ring = new T.Mesh(
  new T.TorusGeometry(0.9, 0.03, 10, 64),
  new T.MeshStandardMaterial({color:0xd8a920, emissive:0x8a6414, emissiveIntensity:0.7, roughness:0.3, metalness:0.9})
);
ring.rotation.x = Math.PI/2; ring.position.y = 0.29; rubyGrp.add(ring);
var rubyLight = new T.PointLight(0xff2a3a, 2.6, 18); rubyLight.position.y = 1.1; rubyGrp.add(rubyLight);

/* ---- boxes are measured in rubies: 1 ruby = Minimum, the smallest anything can be ---- */
var stoneMat = new T.MeshStandardMaterial({color:0xd8a920, roughness:0.35, metalness:0.85, emissive:0x3a2a08, emissiveIntensity:0.25});
var darkShoeMat = new T.MeshStandardMaterial({color:0x15151c, roughness:0.55, metalness:0.5});
var glowBlueMat = new T.MeshBasicMaterial({color:0x4499ff});
var toeCapMat = new T.MeshStandardMaterial({color:0x2a2a35, roughness:0.3, metalness:0.85});
/* NPC kings — versions of the king standing in the world, that aren't him */
var npcKings = [];
function persistKings(){
  try{ localStorage.setItem('ruby-kings-v1', JSON.stringify(npcKings.map(function(k){ return {x:k.position.x, z:k.position.z}; }))); }catch(e){}
}
function spawnKingNPC(x, z){
  var k = buildKingAvatar();
  k