<!-- SOLID CITY · part 2985 -->
 floorY[j]);
          if (cand < cutY[j]) cand = cutY[j];
          if (cand > hi) cand = hi;
          if (cand === prof[j]) continue;
          var keep = prof[j]; prof[j] = cand;
          var g2 = grades();
          var after = Math.abs(g2[j] - g2[j - 1]) + Math.abs(g2[j + 1] - g2[j]) + Math.abs(g2[j + 2] - g2[j + 1]);
          if (after < before - 1e-9) moved++; else prof[j] = keep;
        }
        if (!moved) break;
      }
      for (i = 0; i < N; i++) if (prof[i] < cutY[i]) prof[i] = cutY[i];
      envelope();
    })();
    for (i = 0; i < N; i++) { st[i].y = prof[i]; st[i].grade = i ? (prof[i] - prof[i - 1]) / (st[i].ds || stepM) : 0; }

    /* SUPERELEVATION — the curriculum's third discipline, and the
     * corridor's quiet triumph: banking is the balance law read
     * straight off the alignment's analytic curvature, bank =
     * atan(e), e ∝ κ. It needs no smoothing passes: on a clothoid
     * the curvature ramps linearly, so the tilt develops along the
     * transition exactly as the runoff law prescribes — the spiral
     * IS the runoff. The constant carries Solid City's own design
     * speed (the principles are the profession's; the dimensions
     * are ours), and the cap remains the spec's law. */
    for (i = 0; i < N; i++) st[i].bank = Math.max(-bankCap, Math.min(bankCap, -Math.atan(st[i].kappa * 1.31)));

    /* THE JUNCTION THROAT: a road that must braid past its
     * neighbour in the first metres from a node may be built
     * narrower there, widening smoothly as the lines part —
     * spec.taperStart / taperEnd {width, over}. Only the SURFACE
     * narrows; the profile above was computed for the full
     * breadth, and the height law does not change. */
    var arcOf = [0];
    for (i = 1; i < N; i++) arcOf.push(arcOf[i - 1] + (st[i].ds || stepM));
    for (i = 0; i < N; i++) {
      var hwI = hw;
      if (spec.taperStart && arcOf[i] < spec.taperStart.over) { var uS = arcOf[i] / spec.taperStart.over, smS = uS * uS * (3 - 2 * uS); hwI = Math.min(hwI, spec.taperStart.width / 2 + (hw - spec.taperStart.width / 2) * smS); }
      if (spec.taperEnd && (arcOf[N - 1] - arcOf[i]) < spec.taperEnd.over) { var uE = (arcOf[N - 1] - arcOf[i]) / spec.taperEnd.over, smE = uE * uE * (3 - 2 * uE); hwI = Math.min(hwI, spec.taperEnd.width / 2 + (hw - spec.taperEnd.width / 2) * smE); }
      st[i].hw = hwI;
    }

    /* corners: the banked right vector carries the height difference */
    for (i = 0; i < N; i++) {
      var s2 = st[i], cb = Math.cos(s2.bank), sb = Math.sin(s2.bank);
      var Rx = s2.rx * cb, Ry = sb, Rz = s2.rz * cb;
      s2.L = [s2.x - Rx * s2.hw, s2.y - Ry * s2.hw, s2.z - Rz * s2.hw];
      s2.R = [s2.x + Rx * s2.hw, s2.y + Ry * s2.hw, s2.z + Rz * s2.hw];
      s2.Rx = Rx; s2.Ry = Ry; s2.Rz = Rz;
    }

    var walkSide = spec.sidewalk === false ? 0 : upSide;   // the uphill side, named for the street kit

    /* surfaces */
    var ribbon = newSurf(), curbL = newSurf(), curbR = newSurf(), goldL = newSurf(), goldR = newSurf();
    var walk = newSurf(), skirtL = newSurf(), skirtR = newSurf(), fans = newSurf(), bankGrass = newSurf();
    var shoL = newSurf(), shoR = newSurf(), rail = newSurf(), posts = newSurf();
    var cliffW = newSurf();   // the cliff walls of the deep cuts — poured stone by the factory
    var stand = [];
    var stats = { length: 0, stations: N, maxGrade: 0, maxKink: 0, maxBank: 0, maxSkirtL: 0, maxSkirtR: 0, cutBank: 0, bank: 0, bankUnmet: 0, worstGradeAt: null, worstKinkAt: null };
    for (i = 0; i < N; i++) {
      var g = Math.abs(st[i].grade);
      if (g > stats.maxGrade) { stats.maxGrade = g; stats.worstGradeAt = [st[i].x, st[i].z]; }
      if (Math.abs(st[i].bank) > stats.maxBank) stats.maxBank = Math.abs(st[i].bank);
      if (i) {
        stats.length += st[i].ds;
        var kink = Math.abs(st[i].grade - st[i - 1].grade);
        if (kink > stats.maxKink) { stats.maxKink = kink; stats.worstKinkAt = [st[i].x, st[i].z]; }
      }
    }
    function edgePt(s, side, out, lift) {
      var d = s.hw + out;
      return [s.x + s.Rx * side * d, s.y + s.Ry * side * d + (lift || 0), s.z + s.Rz * side * d];
    }
    /* THE DRAPE (the finish that answers the fins): the old bank
     * marched outward up to fourteen metres hunting for the land and,
     * not finding it, pinned its foot in the air — 3,152 sail
     * triangles by the census, floating up to thirteen metres above
     * the slope. The drape instead walks a short profile out from
     * the edge and MEETS the land: at each step its height is the
     * land itself, clamped between a falling repose (0.62) and a
     * rising cut (0.95); once it reaches the land it follows it,
     * tucked a quarter-metre under. If the land falls away faster
     * than repose for a full bench depth (3.4 m), the profile
     * becomes a benched shoulder, descending gently — a retaining
     * line at the road's own edge, never a sheet hung in the air.
     * The w