<!-- SAGA-SOLID · part 92 -->
);
        added++;
      }
    }

    return added;
  };

  WF.ensureDebugger = function(){
    var el = document.getElementById('world-foundation-debug');

    if(el) return el;

    el = document.createElement('div');
    el.id = 'world-foundation-debug';

    el.style.cssText = [
      'position:fixed',
      'top:125px',
      'left:8px',
      'max-width:calc(100vw - 16px)',
      'background:rgba(0,0,0,.88)',
      'color:#7CFF7C',
      'font:11px/1.35 monospace',
      'padding:8px',
      'border:1px solid #7CFF7C',
      'border-radius:4px',
      'z-index:2147483647',
      'white-space:pre',
      'pointer-events:none',
      'display:none'
    ].join(';');

      el.style.display = 'none'; /* gameplay HUD cleanup */
    document.body.appendChild(el);
    return el;
  };

  WF.toggleDebug = function(){
    WF.debug.enabled = !WF.debug.enabled;

    var el = WF.ensureDebugger();
    el.style.display = WF.debug.enabled ? 'block' : 'none';

    if(typeof say === 'function'){
      say(
        'foundation debug ' +
        (WF.debug.enabled ? 'on' : 'off')
      );
    }
  };

  WF.updateDebug = function(){
    if(!WF.debug.enabled) return;
    if(typeof kingPos === 'undefined' || !kingPos) return;

    /*
     * Synchronization is idempotent.
     * No arbitrary 11-second assumption is required.
     */
    WF.syncMountains();

    var support = WF.supportBelow(
      kingPos,
      {
        startOffset:2,
        maxDrop:8,
        maxSlope:60
      }
    );

    WF.debug.support = support;

    var el = WF.ensureDebugger();

    var supportY =
      support.hit ? support.height.toFixed(2) : 'NONE';

    var delta =
      support.hit ? support.playerDelta.toFixed(2) : '—';

    var slope =
      support.hit ? support.slope.toFixed(1) + '°' : '—';

    var kind =
      support.hit ? support.kind : 'NONE';

    el.textContent =
      'FOUNDATION 1  OBSERVE ONLY\n' +
      'MODE ' +
        (window.SAGA_CORE && window.SAGA_CORE.getMoveMode ? window.SAGA_CORE.getMoveMode() : '?') + '\n' +
      'PLAYER ' +
        kingPos.x.toFixed(1) + ', ' +
        kingPos.y.toFixed(1) + ', ' +
        kingPos.z.toFixed(1) + '\n' +
      'SUPPORT Y ' + supportY + '\n' +
      'DELTA ' + delta + '\n' +
      'VALID ' + (!!support.valid) + '\n' +
      'SURFACE ' + kind + '\n' +
      'SLOPE ' + slope + '\n' +
      'SURFACES ' + WF.colliders.length + '\nHYPER ' + (window.HYPER_MOVE ? (window.HYPER_MOVE.grounded ? 'GROUND' : 'AIR') : 'WAIT');
  };

  /*
   * Install debugger command into the existing command system.
   */
  if(typeof CODES !== 'undefined'){
    CODES.shadowdebug = {cat:"Special",desc:"Toggle V206 live shadow debugger",fn:function(){var R=window.ACTOR_RUNTIME;if(!R)return;R.debug.showingTest=false;if(!R.debug.mobile)R.toggleDebug();R.debug.shadowLiveDisplay=!R.debug.shadowLiveDisplay;if(R.debug.shadowLiveDisplay){R.debug.shadowTimer=setInterval(function(){if(!R.debugElement||!R.debug.shadowLiveDisplay)return;var I=R.input,S=R.shadowMotion,L=R.shadowLive;R.debugElement.textContent="V206 LIVE SHADOW\n\nINPUT      "+I.moveX.toFixed(2)+" / "+I.moveZ.toFixed(2)+"\nMAG        "+I.magnitude.toFixed(2)+"\nDESIRED    "+S.desired.length().toFixed(2)+"\nSHADOW V   "+S.velocity.length().toFixed(2)+"\nPRED STEP  "+(L.lastPredictionError===null?"NONE":L.lastPredictionError.toFixed(3))+"\nSAMPLES    "+L.samples+"\nOWNER      "+String(I.owner).toUpperCase()+"\nMODE       "+String(R.locomotion.requestedMode).toUpperCase()+"\n\nREAL PLAYER: LEGACY\nSHADOW: OBSERVE ONLY";},100);}else{clearInterval(R.debug.shadowTimer);}}};
    CODES.aipilot = {cat:"Special",desc:"Start V211 terrain-aware AI shadow pilot",fn:function(){var A=window.AI_TERRAIN_PILOT;if(A)A.start();}};
    CODES.aistop = {cat:"Special",desc:"Stop AI pilot and return control to human",fn:function(){var A=window.AI_TERRAIN_PILOT,W=window.WORLD_INTELLIGENCE;if(A)A.stop();if(W)W.humanTakeover();}};
    CODES.aiforward = {cat:"Special",desc:"V210 AI shadow forward for 2 seconds",fn:function(){var W=window.WORLD_INTELLIGENCE,R=window.ACTOR_RUNTIME;if(!W||!R)return;if(!W.requestAI("world-ai"))return;W.drive("world-ai",{moveX:0,moveZ:1});setTimeout(function(){if(window.WORLD_INTELLIGENCE)window.WORLD_INTELLIGENCE.stop("world-ai");},2000);}};
    CODES.aitest = {cat:"Special",desc:"Run V209 AI control interface test",fn:function(){var W=window.WORLD_INTELLIGENCE,R=window.ACTOR_RUNTIME,start=kingPos.clone(),r=[];function t(n,v){r.push((v?"PASS  ":"FAIL  ")+n);}t("AI API",!!W&&typeof W.snapshot==="function"&&typeof W.drive==="function");var snap=W.snapshot();t("WORLD SNAPSHOT",!!snap&&!!snap.player&&!!snap.perception&&!!snap.terrain);t("REQUEST AI",W.requestAI("test-ai")===true&&R.ai.active===true);t("AI FORWARD",W.drive("test-ai",{moveX:0,moveZ:1})===true&&R.input.owner==="ai"&&R.input.moveZ===1);t("AI RIGHT",W.drive("test-ai",{moveX:1,moveZ:0})===true&&R.input.moveX===1);t("AI DIAGONAL",W.drive("test-ai",{moveX:.5,moveZ:.5})===tr