<!-- SOLID CITY · part 1726 -->
[vi * 3] = nx * il; nor[vi * 3 + 1] = il; nor[vi * 3 + 2] = nz * il;
    colorAt(cx - half + x * step, cz - half + z * step, hh, Math.sqrt(nx * nx + nz * nz), tmpC);
    col[vi * 3] = tmpC.r; col[vi * 3 + 1] = tmpC.g; col[vi * 3 + 2] = tmpC.b; vi++;
  }
  for (z = 0; z < n1; z++) for (x = 0; x < n1; x++) put(x, z, 0);
  var sk = [];  // skirts hide LOD cracks
  var edges = [[0, 0, 1, 0], [0, n, 1, 0], [0, 0, 0, 1], [n, 0, 0, 1]];
  for (var e = 0; e < 4; e++) { sk.push(vi); for (var k = 0; k < n1; k++) put(edges[e][0] + edges[e][2] * k, edges[e][1] + edges[e][3] * k, -skirt); }
  var idx = [];
  for (z = 0; z < n; z++) for (x = 0; x < n; x++) { var a = z * n1 + x, b = a + 1, c = a + n1, d = c + 1; idx.push(a, c, b, b, c, d); }
  function edgeIdx(e, k) { var E = edges[e]; return (E[1] + E[3] * k) * n1 + (E[0] + E[2] * k); }
  for (e = 0; e < 4; e++) for (k = 0; k < n; k++) {
    var t0 = edgeIdx(e, k), t1 = edgeIdx(e, k + 1), s0 = sk[e] + k, s1 = s0 + 1;
    idx.push(t0, t1, s0, t1, s1, s0, t0, s0, t1, t1, s0, s1); // both windings (cheap, avoids cull holes)
  }
  var g = new T.BufferGeometry();
  g.setAttribute('position', new T.BufferAttribute(pos, 3));
  g.setAttribute('normal', new T.BufferAttribute(nor, 3));
  g.setAttribute('color', new T.BufferAttribute(col, 3));
  g.setIndex(idx);
  g.boundingSphere = new T.Sphere(new T.Vector3(0, 0, 0), size * 0.75 + 4500);
  var m = new T.Mesh(g, TERRAIN_MAT);
  m.position.set(cx, 0, cz); m.matrixAutoUpdate = false; m.updateMatrix(); m.visible = false;
  world.add(m);
  return m;
}
function getChunk(cx, cz, size) {
  var k = nodeKey(cx, cz, size), c = chunks.get(k);
  return c;
}
var wanted = new Set(), visibleNow = [];
function lodSelect(cam) {   // cam: absolute {x, y, z}
  var out = [], queue = [];
  function ready(node) { var c = getChunk(node.cx, node.cz, node.size); return c && c.built; }
  function need(node) {
    var k = nodeKey(node.cx, node.cz, node.size);
    if (!chunks.has(k)) { chunks.set(k, { built: false, mesh: null, last: 0 }); buildQueue.push(node); }
    return chunks.get(k);
  }
  function shouldSplit(node) {
    if (node.size <= MIN_SIZE) return false;
    var dx = Math.max(Math.abs(cam.x - node.cx) - node.size / 2, 0), dz = Math.max(Math.abs(cam.z - node.cz) - node.size / 2, 0);
    var dy = Math.max(cam.y - 1200, 0) * 0.6;
    var d = Math.sqrt(dx * dx + dz * dz + dy * dy);
    if (node.size > 40000 && Math.abs(node.cx - ROOT.cx) + Math.abs(node.cz - ROOT.cz) > 1 && d > 60000) return false;
    return d < node.size * SPLIT_K * (S.mode === 'walk' ? 0.36 : 0.5);
  }
  function kids(node) { var q = node.size / 4, s = node.size / 2; return [{ cx: node.cx - q, cz: node.cz - q, size: s }, { cx: node.cx + q, cz: node.cz - q, size: s }, { cx: node.cx - q, cz: node.cz + q, size: s }, { cx: node.cx + q, cz: node.cz + q, size: s }]; }
  // returns true if the subtree can be drawn completely
  function walk(node, depth) {
    if (shouldSplit(node)) {
      var ks = kids(node), sub = [], ok = true;
      for (var i = 0; i < 4; i++) { var r = []; if (!collect(ks[i], r)) ok = false; sub.push(r); }
      if (ok) { for (i = 0; i < 4; i++) for (var j = 0; j < sub[i].length; j++) out.push(sub[i][j]); return true; }
      var self = need(node); if (self.built) { out.push(self); return true; }
      return false;
    }
    var c = need(node); if (c.built) { out.push(c); return true; }
    return false;
  }
  function collect(node, arr) {
    var save = out; out = arr; var ok = walk(node); out = save; return ok;
  }
  walk(ROOT, 0);
  return out;
}
var frameNo = 0;
function updateTerrain(cam, budgetMs) {
  frameNo++;
  var sel = lodSelect(cam);
  for (var i = 0; i < visibleNow.length; i++) if (visibleNow[i].mesh) visibleNow[i].mesh.visible = false;
  for (i = 0; i < sel.length; i++) { if (sel[i].mesh) sel[i].mesh.visible = true; sel[i].last = frameNo; }
  visibleNow = sel;
  // build nearest-first within a time budget
  if (buildQueue.length) {
    buildQueue.sort(function (a, b) { return (a.size - b.size) * 0 + (Math.hypot(a.cx - cam.x, a.cz - cam.z) / a.size) - (Math.hypot(b.cx - cam.x, b.cz - cam.z) / b.size); });
    var t0 = performance.now(), built = 0;
    while (buildQueue.length && (built < 1 || performance.now() - t0 < budgetMs)) {
      var nd = buildQueue.shift(), c = chunks.get(nodeKey(nd.cx, nd.cz, nd.size));
      if (!c || c.built) continue;
      c.mesh = buildChunk(nd.cx, nd.cz, nd.size); c.built = true; built++;
    }
  }
  // evict stale chunks
  if (frameNo % 120 === 0 && chunks.size > 420) {
    chunks.forEach(function (c, k) {
      if (c.built && frameNo - c.last > 600 && chunks.size > 300) { if (c.mesh) { world.remove(c.mesh); c.mesh.geometry.dispose(); } chunks.delete(k); }
    });
    buildQueue = buildQueue.filter(function (n) { return chunks.has(nodeKey(n.cx, n.cz, n.size)); });
  }
}
function terrainStats() { var t = 0, v = 0; for (var i = 0; i < visibleNow.length; i++) if (visibleNow[i].mesh) { v++; 