<!-- SOLID CITY · part 1123 -->
 ---------------- */
(function () {
  var st = $('sc-stick'), knob = st.querySelector('i'), id = null, cx = 0, cy = 0, R = 50;
  function set(x, y) { var d = Math.hypot(x, y); if (d > R) { x *= R / d; y *= R / d; } knob.style.transform = 'translate(' + x + 'px,' + y + 'px)'; input.sx = x / R; input.sy = y / R; }
  st.addEventListener('pointerdown', function (e) { id = e.pointerId; st.setPointerCapture(id); var r = st.getBoundingClientRect(); cx = r.left + r.width / 2; cy = r.top + r.height / 2; set(e.clientX - cx, e.clientY - cy); e.preventDefault(); });
  st.addEventListener('pointermove', function (e) { if (e.pointerId === id) set(e.clientX - cx, e.clientY - cy); });
  function end(e) { if (e.pointerId === id) { id = null; set(0, 0); } }
  st.addEventListener('pointerup', end); st.addEventListener('pointercancel', end);
})();
/* look / mouse-fly drag on the canvas, tap to select */
(function () {
  var id = null, lx = 0, ly = 0, sx = 0, sy = 0, moved = 0;
  canvas.addEventListener('pointerdown', function (e) { if (id !== null) return; id = e.pointerId; canvas.setPointerCapture(id); lx = sx = e.clientX; ly = sy = e.clientY; moved = 0; });
  canvas.addEventListener('pointermove', function (e) {
    if (e.pointerId !== id) return; var dx = e.clientX - lx, dy = e.clientY - ly; lx = e.clientX; ly = e.clientY; moved += Math.abs(dx) + Math.abs(dy);
    if (S.mode === 'fly') { input.dragX = clamp((e.clientX - sx) / 120, -1, 1); input.dragY = clamp((e.clientY - sy) / 120, -1, 1); }
    else { S.camYaw -= dx * 0.006; S.camPitch = clamp(S.camPitch + dy * 0.004, -0.35, 1.2); }
  });
  function end(e) { if (e.pointerId !== id) return; id = null; input.dragX = input.dragY = 0; if (moved < 8) onTap(e.clientX, e.clientY); }
  canvas.addEventListener('pointerup', end); canvas.addEventListener('pointercancel', end);
  canvas.addEventListener('wheel', function (e) { e.preventDefault(); if (S.mode === 'fly') S.ship.throttle = clamp(S.ship.throttle - e.deltaY * 0.0008, 0, 1); else S.camDist = clamp(S.camDist + e.deltaY * 0.01, 3, 20); }, { passive: false });
})();
addEventListener('keydown', function (e) { if (e.target && e.target.tagName === 'INPUT') return; input.keys[e.code] = true; if (e.code === 'KeyL') toggleLevel(); if (e.code === 'KeyH') returnToCity(); if (e.code === 'KeyE') actPress(); if (window.CityPlayer && e.code === 'KeyM') CityPlayer.toggle(); if (window.CityPlayer && e.code === 'KeyN') CityPlayer.next(); });
addEventListener('keyup', function (e) { input.keys[e.code] = false; });
addEventListener('blur', function () { input.keys = {}; });
// Locked / switched app: the 3D loop already stops (requestAnimationFrame), the music keeps playing
// (CityPlayer, plain <audio>). Stop speech/narration so the music is not left ducked under silence.
document.addEventListener('visibilitychange', function () { if (!document.hidden) return; input.keys = {}; try { if (window.speechSynthesis) speechSynthesis.cancel(); } catch (e) {} if (window.CityPlayer) CityPlayer.duck(false); });
/* throttle slider */
(function () {
  var el = $('sc-thr'), id = null;
  function set(e) { var r = el.getBoundingClientRect(); S.ship.throttle = clamp(1 - (e.clientY - r.top) / r.height, 0, 1); }
  el.addEventListener('pointerdown', function (e) { id = e.pointerId; el.setPointerCapture(id); set(e); e.preventDefault(); });
  el.addEventListener('pointermove', function (e) { if (e.pointerId === id) set(e); });
  el.addEventListener('pointerup', function () { id = null; });
})();
function holdBtn(el, on, off) { el.addEventListener('pointerdown', function (e) { e.preventDefault(); on(); }); ['pointerup', 'pointercancel', 'pointerleave'].forEach(function (ev) { el.addEventListener(ev, off); }); }
holdBtn($('b-boost'), function () { S.ship.boost = true; $('b-boost').classList.add('is-on'); }, function () { S.ship.boost = false; $('b-boost').classList.remove('is-on'); });
function toggleLevel() { S.autoLevel = !S.autoLevel; $('b-level').classList.toggle('is-on', S.autoLevel); }
$('b-level').addEventListener('click', toggleLevel);
$('b-rtc').addEventListener('click', returnToCity);
$('b-land').addEventListener('click', function () { if (S.mode === 'fly' || S.mode === 'auto') startAuto(true); });
$('sc-launch').addEventListener('click', startCountdown);
$('sc-act').addEventListener('click', actPress);
$('p-close').addEventListener('click', function () { $('sc-portal').hidden = true; });
function fitView() { var v = vpSize(); renderer.setSize(v[0], v[1], false); camera.aspect = v[0] / v[1]; camera.updateProjectionMatrix(); }
addEventListener('resize', fitView); addEventListener('orientationchange', function () { setTimeout(fitView, 250); setTimeout(fitView, 800); resyncBoot(); });
function resyncBoot() { try { var b = document.getElementById('sc-boot'); if (!b || b.hidden) return; b.style.display = 'none'; void b.offsetHeight; b.style.display = ''; } catch (e) {} }
document.addEventListener('visibilitychange', function () { 