<!-- SOLID CITY · part 1371 -->
ng origin ---------------- */
var camAbs = new V3(), camLook = new V3(), camUp = new V3(0, 1, 0), shipCam = null, watchFov = 0;
function rebase(ax, az) {
  if (Math.abs(ax - origin.x) > 3000 || Math.abs(az - origin.z) > 3000) {
    origin.x = Math.round(ax / 1000) * 1000; origin.z = Math.round(az / 1000) * 1000;
    world.position.set(-origin.x, 0, -origin.z); world.updateMatrixWorld(true);
  }
}
function placeCamera(dt) {
  if (S.mode === 'walk') {
    var P = S.p, d = S.camDist, cp = Math.cos(S.camPitch);
    camAbs.set(P.x + Math.sin(S.camYaw) * d * cp, P.y + 1.6 + Math.sin(S.camPitch) * d, P.z + Math.cos(S.camYaw) * d * cp);
    var g = groundAt(camAbs.x, camAbs.z) + 0.6; if (camAbs.y < g) camAbs.y = g;
    camLook.set(P.x, P.y + 1.5, P.z); camUp.set(0, 1, 0); shipCam = null; watchFov = 0;
    if (window.CityCitizens) CityCitizens.cam(dt, camAbs, camLook);   // 👥 WATCH-follow a citizen (walk mode only)
  } else if (window.CityWatch && (watchFov = CityWatch.cam(dt, camAbs, camLook, camUp))) {
    var gw = groundAt(camAbs.x, camAbs.z) + 3; if (camAbs.y < gw) camAbs.y = gw; shipCam = null;
  } else {
    watchFov = 0;
    var sh = S.ship, v = shipVectors(), back = S.mode === 'boarded' || S.mode === 'countdown' ? 30 : 24 + Math.min(sh.speed, 3000) * 0.004;
    var want = new V3(sh.x, sh.y, sh.z).addScaledVector(v.f, -back).addScaledVector(S.mode === 'launch' ? UP : v.u, 7);
    if (S.mode === 'boarded' || S.mode === 'countdown') want.set(sh.x + 18, sh.y + 9, sh.z + 26);
    if (S.mode === 'launch' && S.launchT < 5.5) want.set(PAD.x + 60, PAD.y + 12 + Math.max(0, sh.y - PAD.y) * 0.75, PAD.z + 80);
    if (!shipCam) shipCam = want.clone(); else shipCam.lerp(want, Math.min(1, dt * 6));
    // keep camera within a sane tether at high speed
    tv.set(sh.x, sh.y, sh.z); if (shipCam.distanceTo(tv) > back * 2.5) shipCam.copy(want);
    camAbs.copy(shipCam);
    var gy = groundAt(camAbs.x, camAbs.z) + 2; if (camAbs.y < gy) camAbs.y = gy;
    camLook.set(sh.x, sh.y, sh.z).addScaledVector(v.f, S.mode === 'launch' && S.launchT < 5.5 ? 0 : 18);
    camUp.lerp(S.mode === 'fly' || S.mode === 'auto' ? v.u : UP, Math.min(1, dt * 3)).normalize();
  }
  rebase(camAbs.x, camAbs.z);
  camera.position.set(camAbs.x - origin.x, camAbs.y, camAbs.z - origin.z);
  camera.up.copy(camUp);
  camera.lookAt(camLook.x - origin.x, camLook.y, camLook.z - origin.z);
  var alt = Math.max(camAbs.y, 1);
  camera.near = S.mode === 'walk' ? 0.25 : clamp(alt / 2500, 0.5, 40);
  camera.far = clamp(36000 + alt * 9, 36000, 1100000);
  var fov = watchFov > 1 ? watchFov : 62 + (S.mode === 'fly' || S.mode === 'auto' ? clamp(S.ship.speed / 120, 0, 14) : 0);
  camera.fov += (fov - camera.fov) * Math.min(1, dt * 3);
  camera.updateProjectionMatrix();
  // fog + sky by altitude
  var space = smooth((alt - 9000) / 45000);
  scene.fog.near = S.mode === 'walk' ? 900 : 2500 + alt * 2;
  scene.fog.far = (S.mode === 'walk' ? 16000 : 26000 + alt * 6) / Math.max(0.02, 1 - space * 0.98);
  scene.fog.color.setRGB(0.38 * (1 - space) + 0.01, 0.15 * (1 - space) + 0.01, 0.12 * (1 - space) + 0.03);
  skyUni.uAlt.value = alt; skyUni.uDip.value = Math.sqrt(2 * alt / R_PLANET);
  sky.position.copy(camera.position); sky.scale.setScalar(camera.far * 0.6);
  stars.position.copy(camera.position); stars.scale.setScalar(camera.far * 0.55);
  stars.material.opacity = 0.32 + 0.68 * smooth((alt - 2000) / 25000);
  hemi.intensity = 0.55; sun.intensity = 1.25;
  ocean.position.set(camAbs.x - origin.x, 0, camAbs.z - origin.z);
}

/* ---------------- HUD + radar ---------------- */
var radar = $('sc-radar'), rg = radar.getContext('2d'), mapImg = null, MAP = { cx: 8600, cz: 0, span: 76000, n: MOBILE ? 150 : 200 };
function buildMap() {
  var c = document.createElement('canvas'); c.width = c.height = MAP.n; var g = c.getContext('2d'), id = g.createImageData(MAP.n, MAP.n);
  for (var j = 0; j < MAP.n; j++) for (var i = 0; i < MAP.n; i++) {
    var x = MAP.cx - MAP.span / 2 + (i + 0.5) / MAP.n * MAP.span, z = MAP.cz - MAP.span / 2 + (j + 0.5) / MAP.n * MAP.span, h = heightAt(x, z), o = (j * MAP.n + i) * 4;
    if (h < 0) { id.data[o] = 10; id.data[o + 1] = 14 + Math.max(0, 30 + h / 20); id.data[o + 2] = 28 + Math.max(0, 30 + h / 20); }
    else { var t = Math.min(1, h / 3500); id.data[o] = 40 + t * 180; id.data[o + 1] = 26 + t * 130; id.data[o + 2] = 22 + t * 60; if (h > 2900) { id.data[o] = id.data[o + 1] = id.data[o + 2] = 225; } }
    id.data[o + 3] = 255;
  }
  g.putImageData(id, 0, 0); mapImg = c;
}
function drawRadar(ax, az, heading) {
  var W = radar.width, H = radar.height, flying = S.mode !== 'walk';
  var rad = !flying ? 900 : S.ship.y > 20000 ? 40000 : S.ship.y > 4000 ? 18000 : 7000;   // metres from centre to edge
  rg.save(); rg.clearRect(0, 0, W, H); rg.beginPath(); rg.arc(W / 2, H / 2, W / 2 - 2, 0, Math.PI * 2); rg.clip();
  rg.fillStyle = '#04070c'; rg.fillRect(0, 0, W, H);
  if (mapImg) {
    var px = mapImg.width / MAP.span, sxp = (ax - (M