<!-- SOLIDIVISM-STAGING · part 1 -->
===== FILE: public/solidivism-staging/foundation.js =====
/* ============================================================
   SOLIDIVISM FOUNDATION v1.0
   The most impressive kind — clean, solid, unbreakable.
   Claimed by The King, October 8, 2026.
   
   Principles:
   1. Everything visible IS solid. Everything solid IS visible.
   2. No separation between render and collision — one system.
   3. Built to be seen. Built to hold.
   ============================================================ */

const Solidivism = (function() {
  'use strict';

  let scene, camera, renderer, player;
  let colliders = [];
  let buildings = [];
  let labelMeshes = [];

  // ── INIT: The foundation ──

  // ── EPISTEMOLOGY OBSERVATION (fire-and-forget, never breaks 3D) ──
  function observe(eventType, data) {
    try {
      if (typeof Epistemology !== 'undefined' && Epistemology.handleObservation) {
        Epistemology.handleObservation(eventType, data);
      }
    } catch (e) {
      // Silent: observation failure must NEVER break 3D
      // Diagnosable via Epistemology.Health.report()
    }
  }

  function init(containerId) {
    const container = document.getElementById(containerId);
    
    // Scene: dark tactical void
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0x0a0a0e);
    
    // Camera: cinematic third-person
    camera = new THREE.PerspectiveCamera(
      60, window.innerWidth / window.innerHeight, 0.1, 500
    );
    camera.position.set(0, 12, 83); // Behind avatar at new spawn
    
    // Renderer: maximum quality (with error capture for diagnostics)
    try {
      renderer = new THREE.WebGLRenderer({ antialias: true });
    } catch (rendererErr) {
      var errDiv = document.createElement('div');
      errDiv.id = 'webgl-error';
      errDiv.style.cssText = 'position:fixed;top:10px;left:10px;background:#ff0000;color:#fff;padding:20px;z-index:99999;font-family:monospace;max-width:80%;';
      errDiv.textContent = 'RENDERER_FAIL: ' + rendererErr.message + ' | STACK: ' + (rendererErr.stack || 'none').substring(0, 800);
      document.body.appendChild(errDiv);
      throw rendererErr;
    }
    renderer.setSize(container.clientWidth, container.clientHeight);
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
    container.appendChild(renderer.domElement);
    observe('session.start', { timestamp: new Date().toISOString(), userAgent: navigator.userAgent });
    
    // Lights: dramatic tactical lighting
    const ambient = new THREE.AmbientLight(0xffffff, 0.4);
    scene.add(ambient);
    
    const keyLight = new THREE.DirectionalLight(0xff3333, 1.2);
    keyLight.position.set(30, 50, 30);
    scene.add(keyLight);
    
    const fillLight = new THREE.DirectionalLight(0x3388ff, 0.5);
    fillLight.position.set(-30, 20, -30);
    scene.add(fillLight);
    
    const rimLight = new THREE.PointLight(0xff0000, 1.2, 150);
    rimLight.position.set(0, 10, 0);
    scene.add(rimLight);
    
    // Ground: tactical grid
    createGround();
    
    // Player: the avatar
    createPlayer();
    
    // Start the loop
    animate();
    
    // Handle resize
    window.addEventListener('resize', onResize);
  }

  // ── GROUND: Futuristic motherboard floor (seamless 4-side loop) ──
  function createMotherboardTexture() {
    const S = 512;
    const c = document.createElement('canvas');
    c.width = S; c.height = S;
    const x = c.getContext('2d');
    
    // PCB base: deep black-green
    x.fillStyle = '#0a0f0a';
    x.fillRect(0, 0, S, S);
    
    // Subtle PCB texture noise
    x.fillStyle = '#0d130d';
    for (let i = 0; i < 200; i++) {
      const px = Math.floor(Math.random() * S);
      const py = Math.floor(Math.random() * S);
      x.fillRect(px, py, 2, 2);
    }
    
    // Circuit traces: red copper lines (keep away from edges for seamless loop)
    const M = 24; // margin — nothing crosses the edge
    x.strokeStyle = '#CC0000';
    x.lineWidth = 3;
    
    // Horizontal traces
    const hLines = [80, 150, 230, 320, 410, 470];
    hLines.forEach(y => {
      x.beginPath();
      x.moveTo(M, y);
      // Zigzag like real motherboard traces
      let cx = M;
      while (cx < S - M - 40) {
        const seg = 40 + Math.random() * 60;
        x.lineTo(cx + seg, y);
        if (Math.random() > 0.5 && cx + seg < S - M - 40) {
          const dy = (Math.random() > 0.5 ? 1 : -1) * 30;
          x.lineTo(cx + seg, y + dy);
          x.lineTo(cx + seg + 30, y + dy);
          x.lineTo(cx + seg + 30, y);
          cx += seg + 30;
        } else {
          cx += seg;
        }
      }
      x.lineTo(S - M, y);
      x.stroke();
    });
    
    // Vertical traces
    const vLines = [70, 140, 250, 340, 430];
    vLines.forEach(px => {
      x.beginPath();
      x.moveTo(px, M);
      let cy = M;
      while (cy < S - M - 40) {
        const seg = 40 + Math.random() * 60;
        x.lineTo(px, cy + seg);
        cy += seg;
      }
      