<!-- SOLIDABILITY-WORLD · part 40 -->
x + Math.sin(lookYaw) * Math.cos(lookPitch) * dist,
      player.position.y + Math.sin(lookPitch) * dist,
      player.position.z + Math.cos(lookYaw) * Math.cos(lookPitch) * dist
    );
    camera.lookAt(player.position.x, player.position.y + 1, player.position.z);
  }

  // Path trail visualization
  let trailMeshes = [];
  function showPathTrail(path) {
    clearPathTrail();
    const scene = SWEngine.getScene();
    if (!scene) return;
    const mat = new THREE.MeshBasicMaterial({ color: 0x00ffcc, transparent: true, opacity: 0.6 });
    path.forEach((p, i) => {
      if (i === 0) return;
      const prev = path[i - 1];
      const midX = (p.x + prev.x) / 2, midZ = (p.z + prev.z) / 2;
      const len = Math.hypot(p.x - prev.x, p.z - prev.z);
      const geo = new THREE.PlaneGeometry(0.6, len);
      const mesh = new THREE.Mesh(geo, mat);
      mesh.rotation.x = -Math.PI / 2;
      mesh.rotation.z = Math.atan2(p.x - prev.x, p.z - prev.z);
      mesh.position.set(midX, (p.y + prev.y) / 2 + 0.3, midZ);
      scene.add(mesh);
      trailMeshes.push(mesh);
    });
  }

  function clearPathTrail() {
    const scene = SWEngine.getScene();
    if (!scene) return;
    trailMeshes.forEach(m => scene.remove(m));
    trailMeshes = [];
  }

  // Mode management
  function setMode(m) {
    // Leaving AI DRIVE stops the AI
    if ((mode === 'AI_DRIVE' || mode === 'NAVIGATE') && m !== 'AI_DRIVE' && m !== 'NAVIGATE') {
      stopNavigate();
      stopTour();
      aiDriving = false;
    }
    mode = m;
    updateModeUI();
  }

  function getMode() { return mode; }

  function updateModeUI() {
    document.querySelectorAll('.hc-mode-btn').forEach(btn => {
      btn.classList.toggle('active', btn.dataset.mode === mode);
    });
    const indicator = document.getElementById('hc-mode-indicator');
    if (indicator) indicator.textContent = mode;
  }

  // Build the Hyper Controller UI
  function buildUI() {
    const hc = document.createElement('div');
    hc.id = 'hyper-controller';
    hc.innerHTML = `
      <div id="hc-mode-indicator">WALK</div>
      <div id="hc-buttons">
        <button class="hc-mode-btn active" data-mode="WALK" title="Walk (manual)">🚶</button>
        <button class="hc-mode-btn" data-mode="LOOK" title="Look around">👁️</button>
        <button class="hc-btn hc-ai" id="hc-aidrive" title="AI Drive">🤖</button>
        <button class="hc-btn" id="hc-tour" title="Guided tour">🗺️</button>
        <button class="hc-btn" id="hc-find" title="Find a problem">🔍</button>
        <button class="hc-btn" id="hc-focus" title="Focus nearest">🎯</button>
        <button class="hc-btn" id="hc-scan" title="Scan area">📡</button>
        <button class="hc-btn" id="hc-go" title="Navigate to focused">🧭</button>
        <button class="hc-btn" id="hc-return" title="Return">↩️</button>
        <button class="hc-btn hc-stop" id="hc-stop" title="Stop AI">⏹️</button>
        <button class="hc-btn" id="hc-diag" title="Diagnostics">🔍</button>
        <button class="hc-btn" id="hc-heat" title="Heat view">🔥</button>
        <button class="hc-btn" id="hc-investigate" title="AI Investigate">🕵️</button>
        <button class="hc-btn" id="hc-guide" title="Show me my website">👋</button>
        <button class="hc-btn" id="hc-glossary" title="Shared language">📖</button>
      </div>
      <div id="hc-command">
        <input type="text" id="hc-input" placeholder='Try: "drive me to forum"' />
      </div>
    `;
    document.getElementById('app').appendChild(hc);

    // Mode buttons (WALK / LOOK)
    hc.querySelectorAll('.hc-mode-btn').forEach(btn => {
      btn.addEventListener('click', () => {
        stopAI();
        setMode(btn.dataset.mode);
      });
    });

    // AI DRIVE toggle
    document.getElementById('hc-aidrive').addEventListener('click', () => {
      if (aiDriving) {
        stopAI();
      } else {
        aiDriving = true;
        setMode('AI_DRIVE');
        narrate(`AI Drive engaged. Tell me where to go — try "drive me to forum" ` +
          `or tap 🗺️ for a guided tour.`);
      }
    });

    // Guided tour
    document.getElementById('hc-tour').addEventListener('click', startTour);

    // Find a problem
    document.getElementById('hc-find').addEventListener('click', findProblem);

    // Stop
    document.getElementById('hc-stop').addEventListener('click', stopAI);

    // Diagnostics
    document.getElementById('hc-diag').addEventListener('click', () => {
      if (typeof SWDiagnose !== 'undefined') SWDiagnose.showPanel();
    });

    // AI Investigator V2: toggle pause/resume, stop via ⏹️
    document.getElementById('hc-investigate').addEventListener('click', function() {
      if (typeof SWInvestigator === 'undefined') return;
      SWInvestigator.toggle();
    });

    // Human Guide: "Show me my website" — plain-language tour
    document.getElementById('hc-guide').addEventListener('click', function() {
      if (typeof SWGuide === 'undefined') return;
     