<!-- RUBY-BACKUP · part 177 -->
z2) || 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="kidDome()" style="width:80px;height:80px;font-size:14px;background:#00ccff;color:#fff;border:3px solid #fff;border-radius:20px;">DOME</button>'+
        '<button onclick="kidPad()" style="width:80px;height:80px;font-size:14px;background:#ff00ff;color:#fff;border:3px solid #fff;border-radius:20px;">PAD</button>'+
        '<button onclick="kidRainbow()" style="width:80px;height:80px;font-size:14px;background:linear-gradient(135deg,#ff0000,#ffff00,#00ff00,#0088ff);color:#fff;border:3px solid #fff;border-radius:20px;">RAINBOW</button>'+
        '<button onclick="kidTreasure()" style="width:80px;height:80px;font-size:14px;background:#ffcc00;color:#000;border:3px solid #fff;border-radius:20px;">TREASURE</button>'+
        '<button onclick="kidPlay()" style="width:80px;height:80px;font-size:14px;background:#00ff88;color:#000;border:3px solid #fff;border-radius:20px;">SURPRISE</button>'+
        '<button onclick="toggleKidmode()" style="width:60px;height:80px;font-size:12px;background:#333;color:#fff;border:2px solid #fff;border-radius:15px;">OFF</button>';
      document.body.appendChild(k);
    }
    k.style.display = 'flex';
    codeStatus('kidmode on — big buttons, tap to play');
  }else{
    if(k) k.style.display = 'none';
    codeStatus('kidmode off');
  }
}
function kidDome(){
  /* they call it the dome */
  if(typeof handleCode==='function') handleCode('soliditronium');
  setTimeout(function(){ if(typeof handleCode==='function') handleCode('enter'); }, 1000);
}
function kidPad(){
  /* they call it the pad */
  showContinuum();
}
function kidRainbow(){ kidRainbowReal(); }
function kidRainbowReal(){
  if(typeof handleCode==='function') handleCode('rainbow');
  /* celebration sound via Web Audio */
  try{
    var ctx = new (window.AudioContext||window.webkitAudioContext)();
    var o = ctx.createOscillator(); o.type='sine'; o.frequency.value=880;
    var g = ctx.createGain(); g.gain.value=0.2;
    o.connect(g); g.connect(ctx.destination); o.start();
    o.frequency.exponentialRampToValueAtTime(1760, ctx.currentTime+0.5);
    o.stop(ctx.currentTime+0.6);
  }catch(e){}
}
function kidTreasure(){
  if(typeof handleCode==='function') handleCode('treasure');
}
function kidPlay(){
  if(typeof handleCode==='function') handleCode('play');
}

/* AGE GATE — adults to adult maps, kids stay safe */
var USER_AGE = null;
function setAge(n){
  USER_AGE = parseInt(n, 10);
  try{ localStorage.setItem('ruby-age', USER_AGE); }catch(e){}
  if(USER_AGE >= 18){
    codeStatus('adult confirmed — full building codes unlocked. Use "buildcodes" for all 