<!-- SOLID CITY · part 3446 -->
 progress=0;wave=1;activeOrder='FORM UP';selectedId=null;events=[];commandQueue=[];debris=[];particles=[];convoyX=.12;elapsed=0;nextHazardAt=1.4;nextAdvisoryAt=4;paused=false;pauseStarted=0;pauseScreen.hidden=true;pauseBtn.hidden=true;app.inert=false;
    summit=null;document.getElementById('latticeBtn').hidden=true;document.getElementById('summitChoice').hidden=true;document.querySelectorAll('.order-btn').forEach(function(b){b.disabled=false});
    drones=[];
    for(var i=0;i<12;i++)drones.push({id:i+1,name:'D-'+String(i+1).padStart(2,'0'),group:GROUPS[Math.floor(i/4)],x:.20+(i%4)*.035,y:.32+Math.floor(i/4)*.18+(i%2)*.025,vx:0,vy:0,owner:'AI',epoch:1,task:'ESCORT',status:'ACTIVE',hp:100,recover:0,pulse:0,hit:0});
    addEvent('MISSION','CONTROL','Swarm registry initialized: 12 independent tasks');
    addEvent('ORDER','AI COMMAND','FORM UP issued to 12 AI-held drones');
    spawnWave(1);updateAllUI();
  }
  function resize(){
    var r=canvas.getBoundingClientRect();dpr=Math.min(window.devicePixelRatio||1,2);canvas.width=Math.max(1,Math.floor(r.width*dpr));canvas.height=Math.max(1,Math.floor(r.height*dpr));canvasW=r.width;canvasH=r.height;ctx.setTransform(dpr,0,0,dpr,0,0)
  }
  window.addEventListener('resize',resize);

  function pauseMission(){
    if(!running||paused)return;
    paused=true;pauseStarted=performance.now();pressed={up:false,down:false,left:false,right:false};pauseBtn.hidden=true;app.inert=true;pauseScreen.hidden=false;document.getElementById('resumeBtn').focus();
  }
  function resumeMission(){
    if(!running||!paused)return;
    var now=performance.now();missionStart+=now-pauseStarted;last=now;pauseStarted=0;paused=false;pauseScreen.hidden=true;app.inert=false;pauseBtn.hidden=false;pauseBtn.focus();
  }

  function spawnWave(n){
    var amount=5+n*2;
    for(var i=0;i<amount;i++)debris.push({id:++uid,x:.52+Math.random()*.44,y:.16+Math.random()*.69,r:.013+Math.random()*.014,hp:45+n*14,max:45+n*14,vx:-(.003+n*.0008)*(0.6+Math.random()*.8),vy:(Math.random()-.5)*.005,spin:(Math.random()-.5)*2,angle:Math.random()*6.28});
    addEvent('WAVE','FIELD','Wave '+n+' entered with '+amount+' tracked hazards');
  }
  function issueOrder(order){
    if(!running||paused)return;
    if(summit&&(order!=='LATTICE'||['choice','collapse','cage','breach','done'].indexOf(summit.phase)>=0))return;
    activeOrder=order;var ai=drones.filter(function(d){return d.owner==='AI'&&d.status==='ACTIVE'});var held=drones.filter(function(d){return d.owner==='HUMAN'&&d.status==='ACTIVE'});
    ai.forEach(function(d){d.task=order;commandQueue.push({id:++uid,drone:d.id,epoch:d.epoch,order:order,at:elapsed+.08+Math.random()*.25})});
    addEvent('ORDER','HUMAN COMMAND',order+' issued to '+ai.length+' AI-held drones; '+held.length+' human-held excluded');
    document.getElementById('orderNote').textContent=held.length?held.length+' human-held drone'+(held.length===1?' is':'s are')+' visibly excluded from '+order+'.':'Order routed to all active AI-held drones.';
    updateAllUI();
  }
  function claimDrone(id){
    if(!running||paused)return;var d=drones.find(function(x){return x.id===id});if(!d||d.status!=='ACTIVE')return;
    if(summit&&['choice','collapse','breach','done'].indexOf(summit.phase)>=0)return;
    selectedId=id;
    if(d.owner!=='HUMAN'){
      var oldEpoch=d.epoch;d.owner='HUMAN';d.epoch++;d.task='DIRECT';d.handoffAt=0;d.vx=0;d.vy=0;
      addEvent('TAKEOVER',d.name,'Human claimed task; epoch '+oldEpoch+' → '+d.epoch+'; '+d.group+' AI yielded instantly');
    }
    updateAllUI();
  }
  function releaseSelected(){
    if(paused)return;var d=drones.find(function(x){return x.id===selectedId});if(!d||d.owner!=='HUMAN'||d.status!=='ACTIVE')return;
    d.owner='AI';d.epoch++;d.task='HANDOFF';d.handoffAt=elapsed+.45;d.vx=0;d.vy=0;addEvent('HANDOFF',d.name,'Safe release accepted; AI resumes at epoch '+d.epoch);selectedId=null;updateAllUI();
  }
  function processQueue(){
    for(var i=commandQueue.length-1;i>=0;i--){var c=commandQueue[i];if(c.at>elapsed)continue;var d=drones.find(function(x){return x.id===c.drone});commandQueue.splice(i,1);if(!d)continue;
      if(d.epoch!==c.epoch||d.owner!=='AI'){addEvent('REJECTED',d.name,'Stale '+c.order+' command rejected at epoch '+c.epoch+'; current epoch '+d.epoch);continue}
      d.task=c.order;
    }
  }
  function humanControl(d,dt){var ax=0,ay=0;if(pressed.left)ax-=1;if(pressed.right)ax+=1;if(pressed.up)ay-=1;if(pressed.down)ay+=1;var m=Math.hypot(ax,ay)||1;d.vx+=(ax/m)*.24*dt;d.vy+=(ay/m)*.24*dt;var sp=Math.hypot(d.vx,d.vy),max=.15;if(sp>max){d.vx=d.vx/sp*max;d.vy=d.vy/sp*max}}
  function aiControl(d,dt,index){
    var tx=.25,ty=.3+(index%4)*.12;
    if(activeOrder==='FORM UP'){tx=convoyX+.10+((index%4)-1.5)*.032;ty=.32+Math.floor(index/4)*.17}
    else if(activeOrder==='SWEEP'){tx=.42+((index%4)*.09);ty=.22+Math.floor(index/4)*.27}
    else if(activeOrder==='CONVERGE'){var near=nearestDebris(d);if(near){tx=near.x;ty=near.y}}
    else if(