// ============================================================================
// DIY Sequential Sim Shifter — Oil-Embedded Bronze Bushing Pivot, Fixed Top-Drop
// Detent Insert, Slotted Hex Pinch Collar & All-Metal Adjustable Hard Stops
// ============================================================================
// Mechanical Highlights:
//   1. Precision Flanged Bronze Bushing Pivot (`91259A591` + `2x 6338K453`):
//      - `5/16"` (`7.94 mm`) ground alloy-steel shoulder screw (`2"` / `50.80 mm` shoulder) rides in
//        `2x` SAE 841 oil-embedded sintered bronze flanged bushings (`5/16"` ID x `7/16"` OD x `3/8"` L,
//        `9/16"` flange OD x `1/16"` thick) — light bolt-drawn fit (zero mechanical press required).
//   2. Flat-Bed Base Tray + Top-Drop Bolt-In Detent Insert (`detent_insert.stl`):
//      - Eliminates the horizontal slide-out tunnel so `base_tray` prints flat on `Z = 0` with solid
//        compression pads under both `Y = ±29.0 mm` die springs.
//      - Swappable curved `R = 56 mm` 3-washer detent puck bolts straight down with `2x` flush M3 SHCS
//        into captive M3 hex nuts and includes bottom CA-glue relief grooves in each washer seat.
//   3. Press-Free Slotted M3 SHCS Brass-Hex Pinch Collar (`lever_core.stl`):
//      - Accommodates any `5/8"` (`15.88 mm`) to `16.51 mm` AF brass coupling lot by hand and locks solid
//        with `1x` M3 SHCS + captive M3 hex nut.
//   4. 100% Captive M3 Hex Nut Fastening (`12x` M3 SHCS + `12x` M3 Hex Nuts) & Realistic `9.0 mm` NPT Taper Make-Up.
// ============================================================================

/* [Assembly Selection] */
part_select = "all"; // [all: Full Enclosed Assembly, open_skeleton: Unsleeved Internal Skeleton View, exploded: Exploded View, xray_wires: Translucent Case + Wire Cutaway, base_tray: Unified Base Tray + Integrated Pico Cradle, detent_insert: Top-Drop Bolt-In Curved 3-Washer Detent Insert, lever_core: Moving Rocker w/ Bronze Bushings & Hex Pinch Collar, case_sleeve: 1-Piece Motorsport Protective Case & Gate Shroud, knob: Clean Motorsport Barrel Knob w/ Pipe Funnel, hardware: Reference OTS Hardware]

/* [Top Button Customization (Swap Easily for Amazon or Shop Buttons!)] */
btn_mount_d = 19.0; // [12.0, 16.0, 19.0, 22.0]
btn_pitch   = 1.0;

/* [Print Tolerances & Quality] */
$fn = 64;
eps = 0.01;
fit_press = 0.12;
fit_slide = 0.24;

/* [Kinematics Preview] */
preview_angle  = 0; // [-13.4:0.2:13.4]
preview_btn_mm = 0; // [0:0.2:2.0]
show_hardware  = true;
show_case      = true;

/* [Master STEP-Verified Coordinates & Dimensions] */
rail_half_x    = 20.0;
pillar_t       = 6.0;
outer_w        = rail_half_x + pillar_t; // 26.00 mm -> 52.00 mm flush width matching 5537T951 outer faces!
pillar_w       = 36.0;
pillar_h       = 156.0;
pillar_hole_d  = 9.00;          // Verified from 5537T951.step (Ø9.000 mm)
pillar_tab_d   = 2.10;          // Verified from 5537T951.step (7.8 x 5.0 x 2.10 mm backside Slot-8 boss)

pivot_z        = 60.0;          // Main Pivot @ top hole of 5537T951 (Y = 0.0, Z = +60.0 mm)
pivot_sh_d     = 7.9375;        // 91259A591 ground 5/16" shoulder diameter
pivot_sh_l     = 50.80;         // 91259A591 2.000" shoulder length (stops 1.20 mm inside 52.00 mm plate span)
bushing_id     = 7.9375;        // 6338K453 SAE 841 oil-embedded bronze flanged bushing: 5/16" ID
bushing_od     = 11.1125;       // 7/16" OD (lever_core bore sized to Ø11.22 mm light bolt-drawn fit)
bushing_l      = 9.525;         // 3/8" overall length (barrel = 7.9375 mm + flange = 1.5875 mm)
bushing_flg_d  = 14.2875;       // 9/16" flange OD
bushing_flg_t  = 1.5875;        // 1/16" flange thickness
lever_cheek_x  = 16.15;         // Outer cheek of lever_core (+1.5875 mm bronze flange = ±17.74 mm, +0.16 mm clear of ±17.90 mm bosses!)
spine_w        = 22.225;        // Verified from 47065T259.step: 7/8" (22.225 mm) x 4.00" (101.6 mm) x 3/16" (4.775 mm)
spine_l        = 101.60;
spine_t        = 4.775;

washer_top_z   = 4.00;
r_det          = pivot_z - washer_top_z; // 56.00 mm pivot-concentric curved detent radius
washer_id      = 5.556;         // #10 SAE Grade-8 Thru-Hardened Washer ID (7/32")
washer_od      = 12.70;         // #10 SAE Grade-8 Thru-Hardened Washer OD (1/2")
washer_t       = 1.651;         // #10 SAE Grade-8 Thru-Hardened Washer Thickness (0.065")
pocket_deg     = 2 * atan((washer_od / 2) / r_det); // Exact 12.9385° tangent spacing
max_throw_deg  = 13.40;         // Positive all-metal stop (`47065T259` spine -> `1/4"-20` steel stop bolts!)
gate_slot_deg  = 14.15;         // +0.75° non-impact clearance slot in `case_sleeve` roof

ball_d         = 9.525;         // Verified from 3408A87.step: Ø3/8" 440C ball on 5/8"-11 7-50 lb steel plunger
ball_r         = ball_d / 2;

shaft_od       = 13.72;         // Verified from 4830K138.step: 1/4" NPT x 6.00" Seamless 304SS Pipe OD (0.540")
shaft_id       = 9.25;          // Smooth internal wire conduit bore (0.364" Sched 40)
shaft_len      = 152.40;
cpl_af         = 16.51;         // Verified from 50785K92.step: 0.6500" (16.51 mm) across flats -> 19.064 mm corners!
cpl_hex_d      = cpl_af / cos(30);
cpl_len        = 22.35;         // Verified from 50785K92.step: 0.880" (22.35 mm) machined brass hex coupling length
cpl_bore       = 13.02;         // Verified from 50785K92.step: Ø13.02 mm internal smooth clearance bore
cpl_bot_z0     = 69.50;         // Lower 50785K92 spans Z = 69.50 .. 91.85 mm (clamped in slotted M3 pinch collar)
shaft_z0       = 82.85;         // Realistic 9.00 mm tapered 1/4" NPT wrench-tight engagement -> pipe top @ Z = 235.25 mm
cpl_top_z0     = 226.25;        // Realistic 9.00 mm tapered 1/4" NPT engagement -> upper coupling spans Z = 226.25 .. 248.60 mm!

spr_y          = 29.00;         // Solid compression pads over extrusion; opens steel-stop pedestals @ `Y = ±46.20 mm`!
pin_z_bot      = 9.05;          // 91259A617 (3/8" x 3/8" shoulder screw) head bottom on solid 9.05 mm CF-PLA foundation!
spr_seat_z     = 15.40;         // Flush top of 91259A617's Ø14.29 x 6.35 mm hardened alloy-steel head!
spr_top0_z     = 48.40;

stop_y         = 46.20;         // Adjustable `1/4"-20` Steel Hard-Stop Bolts directly under `47065T259` (`±50.8 mm`)!
stop_head_z    = 36.85;         // Exact Z where tilted `47065T259` bottom face lands on the `Ø10.5` rim at `±13.40°`

sw_y           = 58.60;         // Outward of steel stop pedestals; actuated by `R = 61.65 mm` constant-dwell cam!
sw_z_bot       = 12.20;         // Roller top free = 37.17 mm, OP = 35.70 mm, dwell = 35.11 mm (+0.61 mm above 34.50 crush!)
sw_cam_r       = 61.65;
sw_x_off       = -5.00;
sw_len         = 27.8;
sw_h           = 15.9;
sw_w           = 10.3;

wire_corridor_x = 16.00;        // Protected enclosed corridor (+4.89 mm outboard of 22.225 mm 47065T259 spine!)

gate_z0        = 85.00;         // Underside of case_sleeve's solid 8.50 mm gate roof
gate_h         = 8.50;          // Top deck at Z = 93.50 mm
case_wall      = 3.20;          // 3.20 mm solid perimeter shell (inner X = -22.80..+22.80 mm)
case_y_front   = 73.20;         // Inner front wall @ Y = 69.80 mm (+3.40 mm clearance for front M3 SHCS microswitch heads!)
case_y_rear    = -87.80;        // Rear face flush inside `Y = -88.0 mm` Sim-Lab 40x80 arm!
front_bolt_y   = 81.50;         // Front M8 top-slot rig mounting bolt on solid base_tray ear (+0.9 mm clear of case!)

knob_z0        = 173.00;        // Zero-wire-twist axial hex socket slides over upper 50785K92 (`Z = 226.25..248.60 mm`)
knob_h         = 107.00;        // Top deck at Z = 280.00 mm (clears 21 mm button barrel above 45° hourglass stop-cone!)

function nearest_washer_deg(ang) =
    abs(ang) <= (pocket_deg / 2) ? 0.0 : (ang > 0 ? pocket_deg : -pocket_deg);

function ball_local_z(ang) =
    let(tw   = nearest_washer_deg(ang),
        da   = ang - tw,
        ulat = abs(r_det * tan(da)),
        a    = washer_id / 2,
        sec  = r_det * (1.0 / cos(da) - 1.0),
        drop = ulat >= a ? 0.0 : (ball_r - sqrt(ball_r * ball_r - (a - ulat) * (a - ulat))))
    washer_top_z + ball_r - drop - sec;

// ============================================================================
// UTILITY MODULES
// ============================================================================
module rbox(size, r=2, center=false) {
    sx = size[0]; sy = size[1]; sz = size[2];
    tx = center ? -sx/2 : 0;
    ty = center ? -sy/2 : 0;
    tz = center ? -sz/2 : 0;
    translate([tx, ty, tz])
    hull() {
        for (x = [r, sx - r], y = [r, sy - r]) {
            translate([x, y, 0]) cylinder(r=r, h=sz);
        }
    }
}

module teardrop_x(d, h, center=true) {
    r = d / 2;
    rotate([0, 90, 0])
    linear_extrude(height=h, center=center)
    hull() {
        circle(r=r);
        polygon([[0, 0], [-r * 0.707, r * 0.707], [0, r * 1.35], [r * 0.707, r * 0.707]]);
    }
}

module teardrop_y(d, h, center=true) {
    r = d / 2;
    rotate([-90, 0, 0])
    linear_extrude(height=h, center=center)
    hull() {
        circle(r=r);
        polygon([[0, 0], [-r * 0.707, r * 0.707], [0, r * 1.35], [r * 0.707, r * 0.707]]);
    }
}

module metric_thread_bore(d_major=19.25, pitch=1.0, length=7.3) {
    r_mid = (d_major - 0.55) / 2;
    linear_extrude(height=length, twist=-360 * (length / pitch), slices=round(length / pitch * 32), convexity=6)
        translate([0.275, 0, 0])
        circle(r=r_mid, $fn=48);
}

// ============================================================================
// PART 1: Unified Flat-Bed Base Tray + Captive 20AWG Raceways + Rear Pico Cradle
// ============================================================================
module part_base_tray() {
    // Flat-bed (`Z = 0.00 mm`, zero supports) unified chassis:
    //   - Solid `9.05 mm` compression pads under both `Y = ±29.0 mm` die springs directly over extrusion;
    //   - Keyed top-drop center pocket for swappable `detent_insert.stl` with 2x bottom-entry captive M3 hex nuts;
    //   - 2x solid vertical compression pedestals @ `Y = ±46.20 mm` with captive `1/4"-20` nylocs for steel hard stops;
    //   - 100% captive M3 hex nut fastening (4x case sleeve, 4x microswitches, 2x detent insert).
    tray_x = 19.75;
    y_rear = -84.2;
    y_front = 66.4;

    difference() {
        union() {
            // 1. Continuous flat-bed base floor plate from front switch block all the way under rear Pi Pico!
            translate([-tray_x, y_rear, 0.00])
                rbox([tray_x * 2, y_front - y_rear, 11.80], r=1.8);

            // 1b. Solid Front M8 Top-Slot Rig Mounting Ear (`Y = +64.4..+86.5 mm, Z = 0..6.0 mm`, hole @ `Y = +81.5 mm`)
            translate([-13.00, y_front - 2.0, 0.00])
                rbox([26.00, 24.10, 6.00], r=2.0);

            // 2. Front & Rear Spring Cups (`Y = ±29.0 mm`) + Steel-Stop Pedestals (`Y = ±46.2 mm`) + Switch Blocks (`Y = ±58.6 mm`)
            for (sy = [-1, 1]) {
                yb = sy > 0 ? 18.4 : -66.4;
                translate([-tray_x, yb, 11.60])
                    rbox([tray_x * 2, 48.0, 20.20], r=1.8);

                // Side locator & captive M3 case-lock cheeks at `Y = ±29.0 mm` (`±21.00 mm` outer face clears case SHCS bosses!)
                yl = sy > 0 ? 23.5 : -34.5;
                translate([-21.00, yl, 0.00])
                    rbox([42.00, 11.0, 29.0], r=1.4);
            }

            // 3. Integrated Rear Raspberry Pi Pico Vertical Backplane & `+X` Strain-Relief Saddle (`Y = -84.2..-70.0`)
            translate([-16.5, -84.2, 0.00])
                rbox([33.0, 5.2, 57.5], r=1.8);
            hull() {
                translate([12.2, -84.2, 48.0]) rbox([6.8, 5.2, 9.5], r=1.4);
                translate([12.2, -73.0, 49.5]) rbox([6.8, 5.0, 8.0], r=1.4);
            }
            for (sx = [-1, 1], sz = [-1, 1]) {
                translate([sx * 5.7, -79.2, 29.5 + sz * 23.5])
                    rotate([-90, 0, 0])
                    cylinder(d=4.8, h=3.2);
            }
        }

        // Pico standoff pilot holes (#2 / M2 self-tap — low-load PCB mounting only)
        for (sx = [-1, 1], sz = [-1, 1]) {
            translate([sx * 5.7, -79.5, 29.5 + sz * 23.5])
                rotate([-90, 0, 0])
                cylinder(d=1.9, h=4.0);
        }

        // Top `+X` Wire-Saddle Cradle & Zip-Tie Eye on integrated Pico frame
        translate([13.2, -74.0, 52.5])
            cube([5.2, 7.0, 6.0]);
        translate([11.5, -72.0, 50.2])
            cube([8.5, 2.2, 1.7]);

        // Bottom USB-C / Micro-USB Plug Recess under Pico
        translate([-6.5, -84.5, -eps])
            cube([13.0, 14.5, 5.5]);

        // A. Keyed Top-Drop Center Pocket for `detent_insert.stl`:
        //    - Center keel through-window (`X = -7.15..+7.15, Y = -20.40..+20.40, Z = 0..15.0`) seats insert flat on extrusion;
        //    - Upper wing pocket (`X = -13.40..+13.40, Y = -20.40..+20.40, Z = 4.50..15.0`) + diagonal boss clearances
        //      leave two `4.50 mm` thick solid mounting ledges for 2x bottom-entry captive M3 hex nuts @ `(±9.3, ±17.4)`!
        translate([-7.15, -20.40, -eps])
            cube([14.30, 40.80, 15.0]);
        translate([-13.40, -20.40, 4.50])
            cube([26.80, 40.80, 11.0]);
        for (s = [-1, 1]) {
            translate([s * 9.30, s * 17.40, 4.50])
                cylinder(d=8.10, h=11.0, $fn=36);
            translate([s * 9.30, s * 17.40, -eps])
                cylinder(d=3.40, h=5.00);
            translate([s * 9.30, s * 17.40, -eps])
                rotate([0, 0, 30])
                cylinder(d=6.55, h=2.55, $fn=6);
        }

        // C. Side clearance reliefs for 5537T951.step aluminum pillars + `Z = +20.0 mm` backside Slot-8 bosses!
        //    (`Y = ±18.08 mm` shoulders register `base_tray` directly to the machined aluminum side plates!)
        for (sx = [-1, 1]) {
            xc = sx > 0 ? 19.95 : -26.20;
            translate([xc, -18.08, -1.0])
                cube([6.25, 36.16, 40.0]);
            xb = sx > 0 ? 17.60 : -26.20;
            translate([xb, -4.60, 14.80])
                cube([8.60, 9.20, 10.40]);
        }

        // D. Universal Ø20.80 mm Annular Spring Cups @ `Y = ±29.0 mm` + Top-Open Captive `95615A120` Nyloc Stop Pedestals @ `Y = ±46.2 mm`!
        //    Includes full-width `33.0 mm` (`X = -16.5..+16.5, Y = -22.6..+22.6`) swing clearance for `lever_core` plunger housing!
        translate([-16.50, -22.60, 15.00])
            cube([33.00, 45.20, 19.00]);
        for (sy = [-1, 1]) {
            // Ø20.80 mm Annular Die-Spring Cup (`Z = 15.40..33.80 mm`) + Top-Drop Counterbore (`Ø14.85 mm`, `Z = 9.05..15.50 mm`)
            // resting on a solid `9.05 mm` CF-PLA compression foundation directly over the aluminum extrusion!
            translate([0, sy * spr_y, spr_seat_z])
                cylinder(d=20.80, h=19.50);
            translate([0, sy * spr_y, pin_z_bot])
                cylinder(d=14.85, h=spr_seat_z - pin_z_bot + 0.20);

            // Top-open `1/4"-20` Nyloc (`95615A120`) hex well @ `(X = 0, Y = ±46.20 mm, Z = 23.80..32.00 mm)`
            // + `Ø6.80 mm` clearance tail-bore down to `Z = 16.50 mm` for the `1/4"-20 x 1/2"` steel stop bolt!
            translate([0, sy * stop_y, 23.80])
                rotate([0, 0, 30])
                cylinder(d=13.10, h=9.00, $fn=6);
            translate([0, sy * stop_y, 16.50])
                cylinder(d=6.80, h=8.00);
        }

        // E. STEP-Verified 7779K62 (`Omron V15G51C25`) Switch Pockets (`sw_z_bot = 12.20 mm`, `sw_y = ±58.60 mm`)
        //    + 4x Captive M3 Hex Nut Slots (zero M3 threads in plastic!)
        for (sy = [-1, 1]) {
            scale([1, sy, 1]) {
                translate([sw_x_off - 14.15, sw_y - 5.30, sw_z_bot - 0.05])
                    cube([28.30, 10.60, 21.00]);
                translate([8.80, sw_y - 5.80, 2.00])
                    cube([12.80, 11.80, 31.00]);
                for (mh = [[sw_x_off - 11.10, sw_z_bot + 2.80], [sw_x_off + 11.10, sw_z_bot + 13.10]]) {
                    translate([mh[0], sw_y, mh[1]])
                        teardrop_y(d=3.35, h=28.0);
                    translate([mh[0], sw_y - 6.80, mh[1]])
                        rotate([90, 0, 0])
                        cylinder(d=6.55, h=2.7, center=true, $fn=6);
                    translate([mh[0] - 2.90, sw_y - 8.15, mh[1]])
                        cube([5.80, 2.7, 16.0]);
                }
            }
        }

        // F. Full-Length Captive 20-22 AWG Wire Raceways on `+X` (Primary) & `-X` (Expansion)
        for (sx = [-1, 1]) {
            xr = sx > 0 ? 13.90 : -18.30;
            xt = sx > 0 ? 14.15 : -17.35;
            translate([xr, -78.5, 2.00])
                cube([4.40, 142.5, 8.00]);
            translate([xt, -78.5, 9.90])
                cube([3.20, 142.5, 23.0]);
        }

        // G. Rear & Front M8x16 Sim-Lab Top-Slot Rig Mounting Wells (`Y = -69.5 mm` & `Y = +81.5 mm`, 6.0 mm terrace)
        translate([-9.5, -74.2, 6.00])
            cube([19.0, 9.4, 28.0]);
        translate([0, -69.5, -eps])
            cylinder(d=8.5, h=6.2);
        translate([0, front_bolt_y, -eps])
            cylinder(d=8.5, h=6.2);

        // H. 4x Side M3 Case-Sleeve Retaining Screw Bores & Top-Drop Captive M3 Hex Nut Slots @ `Y = ±29.0 mm`
        for (sx = [-1, 1], yc = [-29.0, 29.0]) {
            translate([sx * 19.5, yc, 22.0])
                teardrop_x(d=3.4, h=10.0);
            xn = sx > 0 ? 17.85 : -20.45;
            translate([xn, yc - 2.90, 18.8])
                cube([2.60, 5.80, 11.0]);
        }
    }
}

// ============================================================================
// PART 2: Top-Drop Bolt-In Curved 3-Washer Detent Insert (-12.94°, 0°, +12.94°)
// ============================================================================
module part_detent_insert() {
    // Swappable ~7 g wear puck drops straight down into `base_tray`'s keyed center pocket and bolts down
    // with 2x flush M3x8 SHCS into captive M3 hex nuts at diagonal corners `(+9.3, +16.8)` & `(-9.3, -16.8)`.
    // Each `#10` Grade-8 washer seat includes shallow bottom CA-glue relief grooves so adhesive wicks flat.
    difference() {
        union() {
            // Center keel (`X = -7.00..+7.00, Y = -20.20..+20.20, Z = 0.00..6.90 mm`) sits flat on extrusion!
            translate([-7.00, -20.20, 0.00])
                rbox([14.00, 40.40, 6.90], r=1.4);

            // Side shoulders (`X = -13.15..+13.15, Y = -20.20..+20.20, Z = 4.55..6.90 mm`) key into `base_tray` pocket
            translate([-13.15, -20.20, 4.55])
                rbox([26.30, 40.40, 2.35], r=1.6);

            // 2x Diagonal Flush-Counterbored M3x8 SHCS Mounting Bosses @ `(+9.3, +17.4)` & `(-9.3, -17.4)` (`Z = 4.55..11.20 mm`)
            for (s = [-1, 1]) {
                translate([s * 9.30, s * 17.40, 4.55])
                    cylinder(d=7.60, h=6.65, $fn=36);
            }
        }

        // Precision `R = 56.00 mm` pivot-concentric concave detent track (`16.40 mm` wide clears Ø15.88 mm plunger body!)
        translate([0, 0, pivot_z])
            rotate([0, 90, 0])
            cylinder(r=r_det, h=16.40, center=true, $fn=128);

        // 3x `#10` Grade-8 washer seats + bottom CA-glue relief cross-grooves (`0.50 mm` deep)
        for (wa = [-pocket_deg, 0.0, pocket_deg]) {
            translate([0, 0, pivot_z])
                rotate([-wa, 0, 0])
                translate([0, 0, -r_det - washer_t - 0.03]) {
                    cylinder(d=washer_od + fit_slide, h=washer_t + 3.50, $fn=48);
                    // Shallow bottom CA-glue wicking relief grooves (keeps washers dead-flat with zero hydraulic lift!)
                    translate([-4.8, -0.75, -0.50]) cube([9.6, 1.50, 0.60]);
                    translate([-0.75, -4.8, -0.50]) cube([1.50, 9.6, 0.60]);
                }
        }

        // 2x Diagonal M3x8 SHCS Clearance Bores (`Ø3.4 mm`) + Flush SHCS Counterbores (`Ø6.2 mm x 3.10 mm` deep)
        for (s = [-1, 1]) {
            translate([s * 9.30, s * 17.40, 4.00])
                cylinder(d=3.40, h=8.00, $fn=28);
            translate([s * 9.30, s * 17.40, 8.10])
                cylinder(d=6.20, h=3.30, $fn=32);
        }
    }
}

// ============================================================================
// PART 3: Moving Rocker w/ Flat 47065T259 Spine Slot & +X Pivot Wire Shoulder
// ============================================================================
module part_lever_core() {
    difference() {
        union() {
            // Main longitudinal upper beam (`tapered to Z = 62.4 mm at Y = ±58 mm` for +9.5 mm clearance under `case_sleeve` roof!)
            hull() {
                translate([-8.00, -34.00, 53.20])
                    rbox([16.00, 68.00, 72.70 - 53.20], r=1.5);
                translate([-8.00, -58.00, 53.20])
                    rbox([16.00, 116.00, 62.40 - 53.20], r=1.5);
            }

            // Central hub around `22.225 mm` (`7/8"`) 47065T259 spine & `Y = ±12.70 mm` bolt bosses
            translate([-12.40, -19.50, 53.20])
                rbox([24.80, 39.00, 19.60], r=1.6);

            // v6.2 Uncrushable `R = 61.65 mm` Pivot-Concentric Constant-Dwell Switch Striker Cams (`X = -16.80..-11.30 mm`):
            //   - Smoothly engages `7779K62`'s `Ø4.80 mm` roller (`centered @ X = -13.51 mm`) at `±10.6°`, clicks past `OP` at `±11.4°`,
            //     and holds a dead-constant `2.06 mm` depression (`+0.61 mm` safe margin before `2.67 mm` bottom-out!) from `±12.0°` to `±20.0°`!
            for (sy = [-1, 1]) {
                intersection() {
                    translate([0, 0, pivot_z])
                        rotate([0, 90, 0])
                        cylinder(r=sw_cam_r, h=40.0, center=true, $fn=128);
                    hull() {
                        translate([-16.80, sy > 0 ? 48.0 : -58.0, 53.50])
                            rbox([12.30, 10.00, 8.00], r=1.0);
                        translate([-16.80, sy > 0 ? 54.0 : -63.0, 46.80])
                            rbox([5.50, 9.00, 7.70], r=1.0);
                    }
                }
            }

            // Flanged Bronze Bushing Boss Cheeks (`6338K453`, outer cheek face `|X| = 16.15 mm` ->
            // with `1.5875 mm` bronze flange outer face lands at `|X| = 17.74 mm`, `+0.16 mm` clear of `±17.90 mm` aluminum bosses!)
            for (sx = [-1, 1]) {
                hull() {
                    translate([sx * (lever_cheek_x - 3.50), 0, pivot_z])
                        rotate([0, 90, 0])
                        cylinder(d=20.4, h=7.00, center=true);
                    translate([sx > 0 ? 11.20 : -15.80, -10.5, 43.00])
                        cube([4.60, 21.0, 11.0]);
                }
            }

            // `+X` Pivot-Height Protected Wire Shoulder (`exits @ X = +16.0, Y = -18.5, Z = 60.0 mm`)
            hull() {
                translate([4.0, -13.5, 62.5]) rbox([11.5, 10.0, 9.5], r=1.4);
                translate([12.2, -19.5, 55.8]) rbox([6.4, 11.0, 9.2], r=1.4);
            }

            // Heavy Slotted M3 SHCS Brass-Hex Pinch Collar (`Z = 69.50..82.60 mm`) for `McMaster 50785K92`!
            //   - Any `5/8"` (`15.88 mm`) to `16.51 mm` AF brass hex lot drops in freely by hand (zero press/hammering!);
            //   - Rear `-Y` pinch ears (`Z = 69.50..79.50 mm`, `+1.86 mm` clear under `gate_z0 = 85.0 mm` at `+13.4°`)
            //     clamp dead-solid with 1x flush M3x12 SHCS + captive M3 hex nut!
            hull() {
                translate([-12.40, -13.40, 69.00])
                    rbox([24.80, 26.80, 9.50], r=2.0);
                translate([-10.80, -11.50, 78.50])
                    rbox([21.60, 23.00, 4.10], r=2.0);
            }
            // Rear `-Y` slotted pinch ears (`X = -7.80..+7.80, Y = -18.00..-10.00, Z = 69.50..79.50 mm`)
            translate([-7.80, -18.00, 69.50])
                rbox([15.60, 8.50, 10.00], r=1.4);

            // Lower `5/8"-11` (`3408A87`) Ball Plunger & Captive Jam-Nut Housing
            hull() {
                translate([-15.8, -11.5, 43.0])
                    rbox([31.6, 23.0, 5.45], r=1.6);
                translate([-15.2, -13.2, 21.0])
                    rbox([30.4, 26.4, 18.0], r=2.2);
                translate([-11.5, -9.8, 16.5])
                    rbox([23.0, 19.6, 4.5], r=2.0);
            }
        }

        // Precision `22.48 mm` (`7/8"` + `0.25 mm`) x `4.95 mm` Full Slide-Through Slot for `McMaster 47065T259.step`!
        translate([-11.24, -70.0, 48.35])
            cube([22.48, 140.0, 4.95]);

        // Inner `Y = ±12.70 mm` Spine Clamp Bores + Bottom Screw-Head Access Wells (`Ø14.40 mm` for `Ø13.50 mm` heads!)
        //   - Nyloc slot floor at `Z = 53.95 mm` gives `7.15 mm` (`90%`) thread engagement through `95615A120`'s nylon collar!
        for (sy = [-1, 1]) {
            yc = sy * 12.70;
            translate([0, yc, 47.0])
                cylinder(d=6.60, h=16.0);
            // Full straight-up insertion & Allen-key access counterbore from below up to `Z = 48.40 mm`
            hull() {
                translate([0, yc, 14.0])
                    cylinder(d=14.40, h=34.42);
                translate([0, yc + sy * 8.0, 14.0])
                    cylinder(d=14.40, h=34.42);
            }
            // Diagonal `-X` side-entry captive `1/4"-20` nyloc slot @ `Z = 53.95..62.35 mm` (`8.40 mm` tall -> fits `95615A120`!)
            hull() {
                translate([0, yc, 53.95])
                    cylinder(d=13.0, h=8.40, $fn=6);
                translate([-14.5, yc + sy * 10.5, 53.95])
                    cylinder(d=13.0, h=8.40, $fn=6);
            }
        }

        // `6338K453` Flanged Bronze Bushing Bolt-Drawn Seats (`Ø11.22 mm` light thumb-start/bolt-drawn fit,
        // `|X| = 6.20..16.50 mm` with `0.45 mm` outer lead-in chamfer & `Ø15.0 mm` outer flange relief) + `Ø8.40 mm` center bore!
        for (sx = [-1, 1]) {
            translate([sx * 11.35, 0, pivot_z])
                rotate([0, 90, 0])
                cylinder(d=bushing_od + 0.11, h=10.50, center=true);
            translate([sx * (lever_cheek_x - 0.10), 0, pivot_z])
                rotate([0, sx > 0 ? 90 : -90, 0])
                cylinder(d1=bushing_od + 0.11, d2=bushing_od + 1.00, h=0.60, center=true);
            translate([sx * (lever_cheek_x + 1.60), 0, pivot_z])
                rotate([0, 90, 0])
                cylinder(d=bushing_flg_d + 0.70, h=3.20, center=true);
        }
        translate([0, 0, pivot_z])
            rotate([0, 90, 0])
            cylinder(d=8.40, h=42.0, center=true);

        // Press-Free Slotted Multi-Lot Hex Socket (`16.85 mm` AF = `d = 19.46 mm, $fn=6`, zero hammering!)
        //   - Rear `1.80 mm` relief slit + flush M3x12 SHCS counterbore (`-X` ear) & captive M3 hex nut pocket (`+X` ear)
        translate([0, 0, cpl_bot_z0])
            cylinder(d=19.46, h=15.00, $fn=6);
        translate([0, 0, 81.45])
            cylinder(d1=19.46, d2=20.46, h=1.30, $fn=6);
        // Rear `-Y` vertical pinch relief slit (`X = -0.90..+0.90, Y = -19.0..-7.0, Z = 69.50..83.50`)
        translate([-0.90, -19.00, cpl_bot_z0 - 0.1])
            cube([1.80, 12.00, 14.50]);
        // Horizontal M3x12 SHCS clamp bore (`Y = -14.80, Z = 74.50`), `-X` flush SHCS counterbore, `+X` captive M3 hex nut pocket
        translate([0, -14.80, 74.50])
            teardrop_x(d=3.40, h=18.00);
        translate([-6.40, -14.80, 74.50])
            teardrop_x(d=6.20, h=3.40);
        translate([6.60, -14.80, 74.50])
            rotate([0, 90, 0])
            cylinder(d=6.55, h=2.80, center=true, $fn=6);
        translate([5.20, -14.80 - 2.90, 74.50])
            cube([2.80, 5.80, 6.00]);

        // Smooth `+X` Internal Wire Conduit (`Ø11.8 -> Ø8.0 mm` funnel right under `50785K92`'s `Ø13.02 mm` bore,
        // routed cleanly around `+X` `Ø11.22 mm` bronze bushing bore with `+1.30 mm` solid wall!)
        translate([0, 0, 68.4])
            cylinder(d1=7.8, d2=11.80, h=1.25);
        hull() {
            translate([ 0.0,   0.0, 68.6]) sphere(d=7.4, $fn=24);
            translate([ 5.5,  -6.5, 68.0]) sphere(d=6.8, $fn=24);
        }
        hull() {
            translate([ 5.5,  -6.5, 68.0]) sphere(d=6.8, $fn=24);
            translate([14.2, -14.5, 65.2]) sphere(d=6.2, $fn=24);
        }
        hull() {
            translate([14.2, -14.5, 65.2]) sphere(d=6.2, $fn=24);
            translate([wire_corridor_x, -17.2, 60.0]) sphere(d=6.4, $fn=24);
        }
        hull() {
            translate([wire_corridor_x, -16.8, 60.0]) sphere(d=6.4, $fn=24);
            translate([wire_corridor_x, -20.0, 60.0]) sphere(d=8.2, $fn=24);
        }
        translate([11.0, -17.5, 56.2])
            cube([8.8, 2.2, 1.7]);

        // `3408A87` (`5/8"-11`) Clearance Bore + Flathead Screwdriver Top Access Bore (`Ø8.5 mm`)
        // + `+X` Side-Entry Captive `94846A213` Jam-Nut Pocket (`flats = 23.81 mm` oriented along `±Y`, `24.40 mm` chute!)
        translate([0, 0, 16.0])
            cylinder(d=16.3, h=22.5);
        translate([0, 0, 30.0])
            cylinder(d=8.5, h=19.0);
        translate([0, 0, 26.5])
            cylinder(d=28.10, h=10.5, center=true, $fn=6);
        translate([0, -12.20, 21.25])
            cube([18.0, 24.40, 10.5]);
    }
}

// ============================================================================
// PART 4: 1-Piece Motorsport Protective Case & Shaft-Stop Gate Sleeve (0 Supports!)
// ============================================================================
module part_case_sleeve() {
    // Print Orientation: Flip 180° onto flat Z = 93.50 mm top deck — ZERO supports anywhere!
    //   - Flush 52.00 mm outer width (`X = -26.00..+26.00 mm`) matches `McMaster 5537T951` anodized
    //     aluminum pillars (`Y = -18.12..+18.12 mm, Z = 0..78.08 mm`) for a billet-cheek motorsport look!
    //   - 8.50 mm thick solid top gate roof (`Z = 85.00..93.50 mm`) transfers ±13.40° shaft stops
    //     directly into the front/rear faces & top edges of the bolted 5537T951 aluminum pillars!
    //   - Fully encloses the front/rear springs, microswitches, X=+16.0 mm flex wire loop, and Pi Pico!
    in_x = outer_w - case_wall; // 22.80 mm -> 45.60 mm clear internal width!
    z_bot = 0.20;
    z_top = gate_z0 + gate_h;   // 93.50 mm

    difference() {
        // 1. Sculpted 52.0 mm wide motorsport body with 3.0 mm beveled top roofline (`chamfered hull`)
        hull() {
            translate([-outer_w, case_y_rear, z_bot])
                rbox([outer_w * 2, case_y_front - case_y_rear, z_top - z_bot - 2.8], r=2.8);
            translate([-(outer_w - 2.4), case_y_rear + 2.4, z_bot])
                rbox([(outer_w - 2.4) * 2, (case_y_front - case_y_rear) - 4.8, z_top - z_bot], r=2.4);
        }

        // A. Main Internal Mechanism & Rear Pi Pico Hollow Cavity:
        //    X = -22.80..+22.80 (45.60 mm), Y = -84.40..+69.80 (154.20 mm), Z = 0..85.00 mm!
        //    Leaves 4x internal 45°-tapered SHCS backing pads (`|X| = 21.20..22.80`) at `Y = ±29.0, Z = 22.0`
        //    so flush M3 SHCS heads (`3.0 mm` tall) sit over a solid `1.70 mm` CF-PLA flange!
        difference() {
            translate([-in_x, case_y_rear + 3.40, -eps])
                rbox([in_x * 2, (case_y_front - 3.40) - (case_y_rear + 3.40), gate_z0 + eps], r=1.6);
            for (sx = [-1, 1], yc = [-29.0, 29.0]) {
                xp_in = sx > 0 ? 21.20 : -23.00;
                hull() {
                    translate([xp_in, yc - 5.2, 16.8]) cube([1.80, 10.4, 10.4]);
                    translate([sx > 0 ? 22.75 : -23.00, yc - 6.8, 15.0]) cube([0.25, 13.6, 14.0]);
                }
            }
        }

        // B. Precision Side-Cheek Saddle Cutouts for Exposed Brushed Silver `5537T951` Aluminum Pillars!
        //    - Below Z = 78.10 mm, opens X = ±19.90..±26.50, Y = -18.15..+18.15 mm so the sleeve slides
        //      smoothly down around the 36.0 mm wide aluminum pillars and rests solidly on Z = 78.00 mm!
        for (sx = [-1, 1]) {
            xc = sx > 0 ? 19.90 : -26.50;
            translate([xc, -18.15, -eps])
                cube([6.60, 36.30, 78.15]);
        }

        // C. Non-Impact ±14.15° Clearance Slot in the 8.50 mm Roof (`20.80 mm` wide for exposed `50785K92` brass hex!):
        hull() {
            for (sa = [-gate_slot_deg, gate_slot_deg]) {
                translate([0, 0, pivot_z])
                    rotate([-sa, 0, 0])
                    translate([0, 0, 18.0])
                    cylinder(d=20.80, h=26.0, $fn=48);
            }
        }
        // Subtle 1.0 mm top chamfer ring around the shaft/coupling slot on Z = 93.50 mm deck
        hull() {
            for (sa = [-gate_slot_deg, gate_slot_deg]) {
                translate([0, 0, z_top - 1.0])
                    translate([0, (z_top - pivot_z) * tan(sa), 0])
                    cylinder(d1=20.80, d2=23.00, h=1.1, $fn=36);
            }
        }

        // D. Front M8 Rig-Mounting Ear Relief Arch (`X = -13.25..+13.25, Z = 0..6.30 mm`):
        //    Straddles `base_tray`'s solid front M8 mounting ear cleanly with zero front tunnel!
        translate([-13.25, case_y_front - 5.0, -eps])
            cube([26.50, 6.0, 6.30]);

        // E. Rear Bottom USB-C / Micro-USB Cable Portal (`X = -7.0..+7.0, Y = -88.8..-82.8, Z = 0..14.0 mm`):
        translate([-7.00, case_y_rear - 1.0, -eps])
            cube([14.00, 6.0, 14.00]);

        // F. 4x Flush Side Counterbored M3 SHCS Retaining Screw Holes @ `(Y = ±29.0 mm, Z = 22.0 mm)`:
        //    `Ø6.2 mm x 3.10 mm` deep counterbore sits 100% flush with `M3` SHCS head (`3.0 mm` tall)!
        for (sx = [-1, 1], yc = [-29.0, 29.0]) {
            translate([sx * 23.0, yc, 22.0])
                teardrop_x(d=3.4, h=8.0);
            xc_cb = sx > 0 ? (outer_w - 1.55) : -(outer_w - 1.55);
            translate([xc_cb, yc, 22.0])
                teardrop_x(d=6.2, h=3.20);
        }

        // G. Subtle Motorsport Exterior Accent Grooves & Recessed `-` / `+` Shift Indicators on Top Deck:
        //    `-` Forward (Downshift, Y = +36 mm) and `+` Rearward (Upshift, Y = -36 mm), 0.6 mm deep!
        translate([-4.5, 35.0, z_top - 0.6])
            cube([9.0, 2.2, 0.8]);
        translate([-4.5, -37.2, z_top - 0.6])
            cube([9.0, 2.2, 0.8]);
        translate([-1.1, -40.6, z_top - 0.6])
            cube([2.2, 9.0, 0.8]);

        // Subtle side grip flutes on front/rear flanks (`0.7 mm` shallow vertical accent lines — 0° overhang!)
        for (sx = [-1, 1], yf = [-68.0, -58.0, 34.0, 42.0]) {
            xf = sx > 0 ? (outer_w - 0.7) : -(outer_w + 0.3);
            translate([xf, yf - 1.2, 34.0])
                cube([1.0, 2.4, 44.0]);
        }
    }
}

// ============================================================================
// PART 5: Motorsport Barrel Knob (Zero-Wire-Twist Axial Hex Socket for 50785K92!)
// ============================================================================
module part_knob() {
    bezel_seat_d = btn_mount_d + 3.6;
    dish_top_d   = max(26.8, bezel_seat_d + 4.2);
    z_cpl_top    = cpl_top_z0 + cpl_len + 0.10; // 242.85 mm ceiling above upper 50785K92

    difference() {
        union() {
            translate([0, 0, knob_z0])
                cylinder(d1=25.8, d2=28.4, h=14.0);
            translate([0, 0, knob_z0 + 14.0])
                cylinder(d1=28.4, d2=36.5, h=16.0);
            translate([0, 0, knob_z0 + 30.0])
                cylinder(d=36.5, h=65.0);
            translate([0, 0, knob_z0 + 95.0])
                cylinder(d1=36.5, d2=31.5, h=12.0);
        }

        // Top thumb-button flush dish & thread bore
        translate([0, 0, knob_z0 + knob_h - 2.8])
            cylinder(d1=bezel_seat_d, d2=dish_top_d, h=2.9);

        translate([0, 0, knob_z0 + knob_h - 9.8])
            metric_thread_bore(d_major=btn_mount_d + 0.25, pitch=btn_pitch, length=7.3);

        // Internal button body chamber (`Z = 255.75 .. 270.40 mm`) + 45° Hourglass Axial-Stop & Wire Funnel (`ZERO supports!`)
        //   - Tapers IN at exact 45° (`19.50 -> Ø14.20 mm`) so BOTH `15.88 mm` (5/8") AND `16.51 mm` brass couplings positively bottom out,
        //     then flares OUT at exact 45° (`Ø14.20 -> Ø23.0 mm`) into the button solder cavity below the `Z = 256.20 mm` button barrel!
        translate([0, 0, 255.75])
            cylinder(d=23.0, h=14.65);
        translate([0, 0, 251.40])
            cylinder(d1=14.20, d2=23.0, h=4.40);
        translate([0, 0, z_cpl_top - 0.05])
            cylinder(d1=19.50, d2=14.20, h=2.75);

        // Zero-Wire-Twist Multi-Lot Hex Chimney (`16.85 mm` AF base = `d = 19.46 mm, $fn=6` + 6x `16.02 mm` AF crush ribs @ top `22 mm`!)
        //   - Fits BOTH `5/8"` (`15.88 mm`) catalog lots AND `16.51 mm` STEP lots without binding down the long skirt,
        //     while the rear `M3` SHCS lock screw + captive M3 hex nut locks any lot dead-tight against the opposite hex walls!
        difference() {
            translate([0, 0, knob_z0 - eps])
                cylinder(d=19.46, h=(z_cpl_top - knob_z0) + eps, $fn=6);
            for (ang_r = [30, 90, 150, 210, 270, 330]) {
                rotate([0, 0, ang_r])
                hull() {
                    translate([8.01, -0.70, cpl_top_z0 + 2.50])
                        cube([0.85, 1.40, cpl_len - 3.00]);
                    translate([8.45, -0.70, cpl_top_z0 + 0.20])
                        cube([0.42, 1.40, 0.50]);
                }
            }
        }

        // Flush Rear `M3` SHCS Axial Lock-Screw Bore + Captive M3 Hex Nut Slot @ `Z = 237.40 mm` (bears directly on `-Y` flat of `50785K92`!)
        translate([0, -13.0, 237.40])
            teardrop_y(d=3.4, h=14.0);
        translate([0, -16.8, 237.40])
            teardrop_y(d=6.2, h=4.4);
        translate([-2.85, -13.2, 234.40])
            cube([5.70, 2.60, 9.0]);
    }
}

// ============================================================================
// OFF-THE-SHELF (OTS) HARDWARE & PROTECTED INTERNAL 20-22 AWG WIRE HARNESSES
// ============================================================================

module wire_polyline(pts, d=2.15) {
    for (i = [0 : len(pts) - 2]) {
        hull() {
            translate(pts[i])   sphere(d=d, $fn=14);
            translate(pts[i+1]) sphere(d=d, $fn=14);
        }
    }
}

module ots_simlab() {
    color([0.24, 0.27, 0.32])
    difference() {
        translate([-20.0, -88.0, -80.0])
            cube([40.0, 176.0, 80.0]);
        translate([-4.0, -89.0, -4.5])
            cube([8.0, 178.0, 4.6]);
        translate([-8.4, -89.0, -12.0])
            cube([16.8, 178.0, 7.55]);
        for (sx = [-1, 1], zz = [-20.0, -60.0]) {
            x_th = sx > 0 ? 15.5 : -20.1;
            x_cv = sx > 0 ? 8.0 : -15.55;
            translate([x_th, -89.0, zz - 4.0]) cube([4.6, 178.0, 8.0]);
            translate([x_cv, -89.0, zz - 8.4]) cube([7.55, 178.0, 16.8]);
        }
    }
}

module ots_tnuts() {
    color([0.82, 0.68, 0.28]) {
        for (ym = [-69.5, front_bolt_y]) {
            difference() {
                translate([-6.8, ym - 10.0, -8.8]) cube([13.6, 20.0, 4.3]);
                translate([0, ym, -10.0]) cylinder(d=6.8, h=6.0);
            }
        }
        for (sx = [-1, 1], zz = [-20.0, -60.0]) {
            xt = sx > 0 ? 11.2 : -15.5;
            difference() {
                translate([xt, -10.0, zz - 6.8]) cube([4.3, 20.0, 13.6]);
                translate([sx * 13.0, 0, zz]) rotate([0, 90, 0]) cylinder(d=6.8, h=8.0, center=true);
            }
        }
    }
}

module ots_pillars() {
    // Exact `5537T951.step` geometry: 156 x 36 x 6.0 mm plate + 4x backside 7.8 x 5.0 x 2.10 mm Slot-8 bosses + Ø9.00 mm holes!
    color([0.82, 0.85, 0.90])
    for (sx = [-1, 1]) {
        xp = sx > 0 ? 20.0 : -26.0;
        xb = sx > 0 ? (20.0 - pillar_tab_d) : -20.0;
        difference() {
            union() {
                translate([xp, -pillar_w/2, -78.0])
                    cube([pillar_t, pillar_w, pillar_h]);
                for (zh = [-60.0, -20.0, 20.0, 60.0]) {
                    translate([xb, -3.90, zh - 2.50])
                        cube([pillar_tab_d, 7.80, 5.00]);
                }
            }
            for (zh = [-60.0, -20.0, 20.0, 60.0]) {
                translate([sx * 21.95, 0, zh])
                    rotate([0, 90, 0])
                    cylinder(d=pillar_hole_d, h=10.5, center=true);
            }
        }
    }
}

module ots_detent_washers() {
    color([0.92, 0.74, 0.24])
    for (wa = [-pocket_deg, 0.0, pocket_deg]) {
        translate([0, 0, pivot_z])
            rotate([-wa, 0, 0])
            translate([0, 0, -r_det - washer_t])
            difference() {
                cylinder(d=washer_od, h=washer_t, $fn=48);
                translate([0, 0, -0.1]) cylinder(d=washer_id, h=washer_t + 0.2, $fn=36);
            }
    }
}

module ots_yoke() {
    // Exact `47065T259.step` geometry: 7/8" (22.225 mm) x 4.00" (101.60 mm) x 3/16" (4.775 mm), 4x Ø6.53 mm holes
    color([0.62, 0.68, 0.76])
    translate([0, 0, spr_top0_z])
    linear_extrude(height=spine_t)
    difference() {
        translate([-spine_w/2, -spine_l/2]) square([spine_w, spine_l]);
        for (yh = [-38.1, -12.7, 12.7, 38.1]) {
            translate([0.0, yh]) circle(d=6.53);
        }
    }
}

module ots_ball_plunger(ang=0) {
    zb = ball_local_z(ang);
    color([0.28, 0.30, 0.35])
        translate([0, 0, 4.85])
        cylinder(d=15.88, h=25.0);
    color([0.85, 0.68, 0.22]) {
        translate([0, 0, 21.5]) cylinder(d=27.2, h=9.53, $fn=6);
        translate([0, 0,  9.5]) cylinder(d=27.2, h=6.8, $fn=6);
    }
    color([0.96, 0.98, 1.00])
        translate([0, 0, zb])
        sphere(d=ball_d);
}

module ots_couplings() {
    // 2x `McMaster 50785K92.step`: Machined Brass 1/4" NPTF Hex Couplings (`16.51 mm` AF / `19.064 mm` corners x `22.35 mm` L, `Ø13.02 mm` bore)
    color([0.85, 0.68, 0.22])
    for (zc = [cpl_bot_z0, cpl_top_z0]) {
        translate([0, 0, zc])
        difference() {
            cylinder(d=cpl_hex_d, h=cpl_len, $fn=6);
            translate([0, 0, -0.2])
                cylinder(d=cpl_bore, h=cpl_len + 0.4);
        }
    }
}

module ots_spring_pins() {
    // 2x `McMaster 91259A617.step`: Black-Oxide Alloy Steel Shoulder Screws (`3/8"` [9.53 mm] Shoulder x `3/8"` [9.53 mm] L, `28.58 mm` OAL)
    color([0.20, 0.22, 0.26])
    for (sy = [-1, 1]) {
        translate([0, sy * spr_y, pin_z_bot]) {
            cylinder(d=14.29, h=6.35);
            translate([0, 0, 6.35])
                cylinder(d=9.53, h=9.525);
            translate([0, 0, 15.875])
                cylinder(d=7.95, h=12.70);
        }
    }
}

module ots_stick() {
    color([0.88, 0.91, 0.95])
    translate([0, 0, shaft_z0])
    difference() {
        cylinder(d=shaft_od, h=shaft_len);
        translate([0, 0, -0.2]) cylinder(d=shaft_id, h=shaft_len + 0.4);
    }
}

module ots_top_button(btn_mm=0) {
    bz = knob_z0 + knob_h - 2.8;
    color([0.76, 0.80, 0.85]) {
        translate([0, 0, bz - 21.0])
            cylinder(d=btn_mount_d - 0.2, h=21.0);
        translate([0, 0, bz])
            cylinder(d1=btn_mount_d + 3.0, d2=btn_mount_d + 2.4, h=1.8);
    }
    color([0.14, 0.65, 0.95])
        translate([0, 0, bz + 1.82 - btn_mm * 0.3])
        difference() {
            cylinder(d=btn_mount_d - 2.2, h=0.4);
            translate([0, 0, -0.1]) cylinder(d=btn_mount_d - 4.0, h=0.6);
        }
    color([0.90, 0.93, 0.97])
        translate([0, 0, bz + 0.2 - btn_mm])
        cylinder(d=btn_mount_d - 4.2, h=2.0);
}

module ots_wire_harness() {
    color([0.95, 0.22, 0.22])
    wire_polyline([
        [-2.8,   0.0, 254.0],
        [-1.1,   0.0, 248.0],
        [-1.1,   0.0,  69.2],
        [ 5.5,  -6.5,  68.8],
        [14.2, -14.5,  66.0],
        [wire_corridor_x, -18.5, 61.1]
    ], d=2.10);

    color([0.20, 0.22, 0.26])
    wire_polyline([
        [ 2.8,   0.0, 254.0],
        [ 1.1,   0.0, 248.0],
        [ 1.1,   0.0,  69.2],
        [ 5.5,  -6.5,  67.0],
        [14.2, -14.5,  64.2],
        [wire_corridor_x, -18.5, 58.9]
    ], d=2.10);
}

module ots_sloop_wires(ang=0) {
    // Fully enclosed inside `part_case_sleeve` (`X = +16.00 mm` inside `±22.80 mm` shell — +5.75 mm wall margin!)
    y_exR = -18.5 * cos(ang) + 1.1 * sin(ang);
    z_exR = pivot_z + 18.5 * sin(ang) + 1.1 * cos(ang);
    y_exB = -18.5 * cos(ang) - 1.1 * sin(ang);
    z_exB = pivot_z + 18.5 * sin(ang) - 1.1 * cos(ang);

    color([0.95, 0.22, 0.22])
    wire_polyline([
        [wire_corridor_x, y_exR, z_exR],
        [wire_corridor_x, -36.0, z_exR * 0.65 + 56.0 * 0.35],
        [wire_corridor_x, -56.0, 56.4],
        [wire_corridor_x, -72.0, 56.0],
        [ 7.5,            -76.5, 44.0]
    ], d=2.10);

    color([0.20, 0.22, 0.26])
    wire_polyline([
        [wire_corridor_x, y_exB, z_exB],
        [wire_corridor_x, -36.0, (z_exB) * 0.65 + 53.8 * 0.35],
        [wire_corridor_x, -56.0, 54.2],
        [wire_corridor_x, -72.0, 53.8],
        [ 9.0,            -76.5, 41.0]
    ], d=2.10);
}

module ots_base_wires() {
    color([0.22, 0.62, 0.98])
    wire_polyline([
        [10.2,  58.6, 18.8],
        [15.0,  57.5, 13.5],
        [15.0,  53.0,  4.2],
        [15.0, -74.0,  4.2],
        [ 9.5, -76.5, 18.0]
    ], d=2.15);

    color([0.20, 0.22, 0.26])
    wire_polyline([
        [10.2,  56.2, 14.0],
        [17.3,  55.5, 10.5],
        [17.3,  51.0,  4.2],
        [17.3, -74.0,  4.2],
        [ 9.5, -77.0, 14.0]
    ], d=2.15);

    color([0.18, 0.84, 0.45])
    wire_polyline([
        [10.2, -58.6, 18.8],
        [15.0, -59.5, 13.5],
        [15.0, -63.0,  7.2],
        [15.0, -74.0,  7.2],
        [ 9.5, -76.5, 26.0]
    ], d=2.15);

    color([0.25, 0.27, 0.32])
    wire_polyline([
        [10.2, -56.2, 14.0],
        [17.3, -57.5, 10.5],
        [17.3, -62.0,  7.2],
        [17.3, -74.0,  7.2],
        [ 9.5, -77.0, 22.0]
    ], d=2.15);
}

module ots_bearings() {
    // 2x `McMaster 6338K453`: SAE 841 Oil-Embedded Sintered Bronze Flanged Sleeve Bearings
    //   - 5/16" (7.94 mm) ID x 7/16" (11.11 mm) OD x 3/8" (9.53 mm) L, 9/16" (14.29 mm) Flange OD x 1/16" (1.59 mm) thick
    //   - Flange seats flush against `lever_core` cheek (`|X| = 16.15 mm`), outer bronze face @ `|X| = 17.74 mm`!
    color([0.78, 0.56, 0.24])
    for (sx = [-1, 1]) {
        barrel_l = bushing_l - bushing_flg_t;
        difference() {
            union() {
                // Bronze barrel inside lever_core (`|X| = 8.21..16.15 mm`)
                translate([sx * (lever_cheek_x - barrel_l / 2), 0, pivot_z])
                    rotate([0, 90, 0])
                    cylinder(d=bushing_od, h=barrel_l, center=true);
                // Bronze thrust flange outside lever_core (`|X| = 16.17..17.76 mm`, +0.02 mm clear of cheek face)
                translate([sx * (lever_cheek_x + 0.02 + bushing_flg_t / 2), 0, pivot_z])
                    rotate([0, 90, 0])
                    cylinder(d=bushing_flg_d, h=bushing_flg_t, center=true);
            }
            translate([sx * (lever_cheek_x - barrel_l / 2 + bushing_flg_t / 2), 0, pivot_z])
                rotate([0, 90, 0])
                cylinder(d=bushing_id, h=bushing_l + 1.0, center=true);
        }
    }
}

module ots_return_springs(ang=0) {
    color([0.16, 0.48, 0.92])
    for (sy = [-1, 1]) {
        dz = sy * spr_y * sin(-ang);
        h_sp = max(24.0, (spr_top0_z + dz) - spr_seat_z);
        translate([0, sy * spr_y, spr_seat_z])
        difference() {
            cylinder(d=18.8, h=h_sp);
            translate([0, 0, -0.5]) cylinder(d=10.7, h=h_sp + 1.0);
        }
    }
}

module ots_yoke_bolts() {
    // Uses 2x of the FREE `1/4"-20 x 1/2"` (12.7 mm) flanged button-head screws included with `47065T259` @ `Y = ±12.70 mm`!
    color([0.22, 0.25, 0.30])
    for (yc = [-12.70, 12.70]) {
        translate([0, yc, 48.4]) cylinder(d=6.35, h=12.7);
        translate([0, yc, 45.2]) cylinder(d=13.5, h=3.2);
        translate([0, yc, 54.05]) cylinder(d=12.7, h=7.95, $fn=6);
    }
}

module ots_stop_bolts() {
    // v6.2 All-Metal Adjustable Positive Hard-Stop Pedestal Hardware (`Y = ±46.20 mm`):
    //   - Uses 2x SPARE `1/4"-20 x 1/2"` steel flanged button-head screws (free with `47065T259`)
    //     + 2x `95615A120` `1/4"-20` nylon-insert locknuts!
    //   - Domed `Ø13.5 mm` steel head sits at `Z = 37.07 mm` so `47065T259` lands steel-on-aluminum at `±13.40°`!
    for (sy = [-1, 1]) {
        color([0.92, 0.68, 0.16])
            translate([0, sy * stop_y, 23.85])
            rotate([0, 0, 30])
            cylinder(d=12.7, h=7.95, $fn=6);
        color([0.78, 0.82, 0.88]) {
            translate([0, sy * stop_y, stop_head_z - 3.20 - 12.70])
                cylinder(d=6.35, h=12.70);
            translate([0, sy * stop_y, stop_head_z - 3.20])
                cylinder(d1=13.50, d2=10.50, h=3.20);
        }
    }
}

module ots_bolts() {
    color([0.22, 0.25, 0.30]) {
        // 4x Free M8x16 side rig bolts (5537T951 -> Sim-Lab 40x80)
        for (sx = [-1, 1], zz = [-20.0, -60.0]) {
            translate([sx * 11.6, 0, zz])
                rotate([0, sx > 0 ? 90 : -90, 0]) cylinder(d=8.0, h=16.0);
            translate([sx * 26.8, 0, zz])
                rotate([0, 90, 0]) cylinder(d=14.8, h=1.56, center=true);
            translate([sx * 29.2, 0, zz])
                rotate([0, 90, 0]) cylinder(d=13.8, h=3.3, center=true);
        }
        // 2x Free M8x16 top-slot rig bolts (rear & front base_tray ears)
        for (ym = [-69.5, front_bolt_y]) {
            translate([0, ym, -8.40]) cylinder(d=8.0, h=16.0);
            translate([0, ym,  6.00]) cylinder(d=14.8, h=1.56);
            translate([0, ym,  7.56]) cylinder(d=13.8, h=3.30);
        }
        // Precision Ground Alloy-Steel Socket Head Shoulder Screw (`McMaster 91259A591`, 5/16" x 2" shoulder, 1/4"-20 thread)
        //   - Head (`Ø11.11 x 5.56 mm`) flush against left plate (`X = -26.00 mm`);
        //   - Ground 5/16" (`7.94 mm`) shoulder spans `X = -26.00 .. +24.80 mm` (stops 1.20 mm inside right plate);
        //   - 1/4"-20 thread + 1/4" flat washer + `95615A120` locknut clamp solidly on right plate (`X = +26.00 mm`)!
        translate([-26.00, 0, pivot_z])
            rotate([0, 90, 0]) cylinder(d=pivot_sh_d, h=pivot_sh_l);
        translate([-26.00 - 5.56, 0, pivot_z])
            rotate([0, 90, 0]) cylinder(d=11.11, h=5.56);
        translate([-26.00 + pivot_sh_l, 0, pivot_z])
            rotate([0, 90, 0]) cylinder(d=6.35, h=11.11);
        translate([26.00, 0, pivot_z])
            rotate([0, 90, 0]) cylinder(d=15.88, h=1.60);
        translate([27.60, 0, pivot_z])
            rotate([0, 90, 0]) cylinder(d=12.7, h=7.95, $fn=6);

        // 4x Side Flush M3x10 SHCS Case-Sleeve Retaining Screws @ Y = ±29.0 mm
        for (sx = [-1, 1], yc = [-29.0, 29.0]) {
            translate([sx * 21.0, yc, 22.0])
                rotate([0, 90, 0]) cylinder(d=3.0, h=8.0, center=true);
            translate([sx * 24.45, yc, 22.0])
                rotate([0, 90, 0]) cylinder(d=5.5, h=3.0, center=true);
        }
        // 2x Diagonal Flush M3x8 SHCS Detent-Insert Retaining Screws @ `(+9.3, +17.4)` & `(-9.3, -17.4)`
        for (s = [-1, 1]) {
            translate([s * 9.30, s * 17.40, 0.10])
                cylinder(d=3.0, h=8.00);
            translate([s * 9.30, s * 17.40, 8.10])
                cylinder(d=5.5, h=3.00);
        }
    }
}

module ots_sw_base() {
    // Exact `7779K62.step` (`Omron V15G51C25KBYOMI` 0.5" Short Roller Lever):
    //   - Body: 27.8 x 10.3 x 15.9 mm @ `Z = 15.00..30.90 mm, Y = ±48.50 mm`
    //   - Roller (`Ø4.80 mm`): centered at `X = sw_x_off - 8.51 = -13.51 mm, Z = 37.53 mm` (top @ `Z = 39.97 mm`!)
    //   - `.187"` QC Tabs: extend `+11.79 mm` out `+X` face (`X = +8.90..+20.69 mm`, +2.11 mm clear inside ±22.80 case!)
    for (sy = [-1, 1]) {
        color([0.12, 0.12, 0.14])
            translate([sw_x_off - sw_len/2, sy * sw_y - sw_w/2, sw_z_bot])
            cube([sw_len, sw_w, sw_h]);
        // Stainless 0.5" hinged roller lever + Ø4.80 mm roller (`exact 7779K62.step` coordinates!)
        color([0.75, 0.78, 0.82]) {
            hull() {
                translate([sw_x_off - 12.0, sy * sw_y - 2.0, sw_z_bot + sw_h + 0.5]) cube([1.5, 4.0, 1.0]);
                translate([sw_x_off - 8.51, sy * sw_y - 2.0, sw_z_bot + 21.8]) cube([1.5, 4.0, 1.2]);
            }
            translate([sw_x_off - 8.51, sy * sw_y, sw_z_bot + 22.57])
                rotate([90, 0, 0])
                cylinder(d=4.80, h=4.4, center=true);
        }
        color([0.85, 0.72, 0.30]) {
            translate([sw_x_off + sw_len/2, sy * sw_y - 0.4, sw_z_bot + 6.8]) cube([11.79, 0.8, 4.75]);
            translate([sw_x_off + sw_len/2, sy * sw_y - 2.5, sw_z_bot + 1.3]) cube([11.79, 0.8, 4.75]);
        }
    }
}

module ots_switches() {
    ots_sw_base();
}

module ots_pico() {
    color([0.08, 0.50, 0.24])
        translate([-10.5, -76.0, 4.0])
        cube([21.0, 1.6, 51.0]);
}

// ============================================================================
// ARTICULATED SUB-ASSEMBLIES, CUTAWAY WIRE VIEW & FULL RENDER DISPATCH
// ============================================================================

module moving_lever_group(ang=0, btn_mm=0, inc_ots=true) {
    translate([0, 0, pivot_z])
    rotate([-ang, 0, 0])
    translate([0, 0, -pivot_z]) {
        color([0.02, 0.71, 0.83]) part_lever_core();
        color([0.90, 0.22, 0.24]) part_knob();
        if (inc_ots) {
            ots_yoke();
            ots_ball_plunger(ang);
            ots_couplings();
            ots_stick();
            ots_top_button(btn_mm);
            ots_wire_harness();
            ots_bearings();
            ots_yoke_bolts();
        }
    }
}

module full_assembly(ang=preview_angle, btn_mm=preview_btn_mm, inc_case=show_case) {
    color([0.15, 0.39, 0.92]) part_base_tray();
    color([0.98, 0.45, 0.09]) part_detent_insert();

    moving_lever_group(ang, btn_mm, show_hardware);

    if (show_hardware) {
        ots_simlab();
        ots_tnuts();
        ots_pillars();
        ots_detent_washers();
        ots_spring_pins();
        ots_return_springs(ang);
        ots_stop_bolts();
        ots_bolts();
        ots_switches();
        ots_sloop_wires(ang);
        ots_base_wires();
        ots_pico();
    }

    if (inc_case) {
        // Matte Charcoal-Slate Motorsport Protective Case Sleeve framing the silver 5537T951 cheeks!
        color([0.16, 0.20, 0.26]) part_case_sleeve();
    }
}

module xray_wires_assembly(ang=preview_angle) {
    ots_top_button(0);
    ots_spring_pins();
    ots_stop_bolts();
    ots_sw_base();
    ots_pico();
    translate([0, 0, pivot_z]) rotate([-ang, 0, 0]) translate([0, 0, -pivot_z]) {
        ots_couplings();
        ots_wire_harness();
    }
    ots_sloop_wires(ang);
    ots_base_wires();
    color([0.24, 0.27, 0.32, 0.22]) ots_simlab();
    color([0.82, 0.85, 0.90, 0.28]) ots_pillars();
    color([0.98, 0.45, 0.09, 0.70]) part_detent_insert();
    translate([0, 0, pivot_z]) rotate([-ang, 0, 0]) translate([0, 0, -pivot_z]) {
        color([0.62, 0.68, 0.76, 0.34]) ots_yoke();
        color([0.88, 0.91, 0.95, 0.24]) ots_stick();
        color([0.02, 0.71, 0.83, 0.30]) part_lever_core();
        color([0.94, 0.27, 0.27, 0.24]) part_knob();
    }
    color([0.15, 0.39, 0.92, 0.26]) part_base_tray();
    color([0.16, 0.22, 0.30, 0.22]) part_case_sleeve();
}

module exploded_assembly() {
    color([0.15, 0.39, 0.92]) part_base_tray();
    translate([0, 0, 18]) {
        color([0.98, 0.45, 0.09]) part_detent_insert();
        if (show_hardware) translate([0, 0, 10]) ots_detent_washers();
    }
    translate([0, 0, 96]) color([0.16, 0.20, 0.26]) part_case_sleeve();

    translate([0, 0, 32]) {
        color([0.02, 0.71, 0.83]) part_lever_core();
        translate([0, 0, 128]) color([0.90, 0.22, 0.24]) part_knob();
        if (show_hardware) {
            ots_yoke();
            ots_ball_plunger(0);
            translate([0, 0, 12]) ots_couplings();
            translate([0, 0, 26]) ots_stick();
            translate([0, 0, 160]) ots_top_button(0);
            translate([0, 0, 26]) ots_wire_harness();
            ots_bearings();
            ots_yoke_bolts();
        }
    }

    if (show_hardware) {
        translate([0, 0, -28]) ots_simlab();
        translate([0, 0, -18]) ots_tnuts();
        for (sx = [-1, 1]) translate([sx * 24, 0, 0])
            intersection() {
                ots_pillars();
                translate([sx > 0 ? 0 : -40, -30, -90]) cube([40, 60, 180]);
            }
        ots_spring_pins();
        ots_return_springs(0);
        ots_stop_bolts();
        ots_bolts();
        ots_sw_base();
        ots_sloop_wires(0);
        ots_base_wires();
        ots_pico();
    }
}

// Dispatch based on part_select
if (part_select == "all")                        full_assembly();
else if (part_select == "open_skeleton")         full_assembly(inc_case=false);
else if (part_select == "exploded")              exploded_assembly();
else if (part_select == "xray_wires")            xray_wires_assembly();
else if (part_select == "base_tray")             part_base_tray();
else if (part_select == "detent_insert")         part_detent_insert();
else if (part_select == "lever_core")            part_lever_core();
else if (part_select == "case_sleeve")           part_case_sleeve();
else if (part_select == "knob")                  part_knob();
else if (part_select == "ots_simlab")            ots_simlab();
else if (part_select == "ots_tnuts")             ots_tnuts();
else if (part_select == "ots_pillars")           ots_pillars();
else if (part_select == "ots_detent_washers")    ots_detent_washers();
else if (part_select == "ots_yoke")              ots_yoke();
else if (part_select == "ots_ball_plunger")      ots_ball_plunger(0);
else if (part_select == "ots_couplings")         ots_couplings();
else if (part_select == "ots_spring_pins")       ots_spring_pins();
else if (part_select == "ots_stick")             ots_stick();
else if (part_select == "ots_top_button")        ots_top_button(0);
else if (part_select == "ots_wire_harness")      ots_wire_harness();
else if (part_select == "ots_sloop_wires")       ots_sloop_wires(0);
else if (part_select == "ots_base_wires")        ots_base_wires();
else if (part_select == "ots_bearings")          ots_bearings();
else if (part_select == "ots_return_springs")    ots_return_springs(0);
else if (part_select == "ots_yoke_bolts")        ots_yoke_bolts();
else if (part_select == "ots_stop_bolts")        ots_stop_bolts();
else if (part_select == "ots_bolts")             ots_bolts();
else if (part_select == "ots_switches")          ots_switches();
else if (part_select == "ots_pico")              ots_pico();
else if (part_select == "hardware") {
    ots_simlab(); ots_tnuts(); ots_pillars(); ots_detent_washers();
    ots_yoke(); ots_ball_plunger(0); ots_couplings(); ots_spring_pins(); ots_stick(); ots_top_button(0);
    ots_wire_harness(); ots_sloop_wires(0); ots_base_wires(); ots_bearings();
    ots_return_springs(0); ots_yoke_bolts(); ots_stop_bolts(); ots_bolts(); ots_switches(); ots_pico();
}
