<!-- SOLID CITY · part 3187 -->
;},850);
  }
  function enemyFire(){
    var alive=state.targets.filter(function(t){return t.alive});if(!alive.length)return;
    var origin=alive[Math.floor(Math.random()*alive.length)];var aim={x:state.ship.x+(Math.random()-.5)*.08,y:state.ship.y+(Math.random()-.5)*.08};
    state.enemyShots.push({x:origin.x,y:origin.y,tx:aim.x,ty:aim.y,t:0,d:.75+Math.random()*.45,damage:state.phase===0?12:state.phase===1?17:23});record('FORTRESS_FIRED',{phase:state.phase+1});
  }
  function enemyHit(s){
    if(state.aegisBurst>0&&Math.random()<.72){record('POINT_DEFENSE_INTERCEPT',{owner:tasks.aegis.owner,epoch:tasks.aegis.epoch});burst(s.x,s.y,'#58dbd0',7);return;}
    var dmg=s.damage;if(state.ship.shield>0){var used=Math.min(state.ship.shield,dmg);state.ship.shield-=used;dmg-=used;record('SHIELD_ABSORBED',{owner:tasks.aegis.owner,damage:used});}
    if(dmg>0){state.ship.hp-=dmg;record('FLEET_DAMAGED',{damage:dmg});}
    if(Math.random()<.13+state.phase*.04&&state.wings>2){state.wings--;record('WING_LOST',{phase:state.phase+1});advise('Interceptor contact — one wing lost. Aegis screen recommended.',true);}
    burst(state.ship.x,state.ship.y,'#ff4b35',10);
    if(state.ship.hp<=0)endGame(false);
  }
  function burst(x,y,c,n){for(var i=0;i<n;i++)state.particles.push({x:x,y:y,vx:(Math.random()-.5)*.16,vy:(Math.random()-.5)*.16,t:0,d:.3+Math.random()*.55,c:c});}
  function claim(task){
    if(!state||!state.running)return;var t=tasks[task],start=performance.now(),old=t.owner;t.epoch++;t.owner=t.owner==='HUMAN'?'AI':'HUMAN';state.lastInput=state.time;
    record('OWNERSHIP_TRANSFER',{task:task,from:old,to:t.owner,epoch:t.epoch,latencyMs:Math.max(0,performance.now()-start)});
    record('STALE_COMMAND_REJECTED',{task:task,rejectedEpoch:t.epoch-1,currentEpoch:t.epoch,source:old});
    updateStations();advise(t.owner==='HUMAN'?'You have '+task.toUpperCase()+'. '+t.admiral+' is advisory only.':t.admiral+' has '+task.toUpperCase()+'. Safe handoff complete.');
  }
  function humanMove(dx,dy){if(tasks.fleet.owner!=='HUMAN')return;state.ship.x=Math.max(.08,Math.min(.48,state.ship.x+dx));state.ship.y=Math.max(.18,Math.min(.82,state.ship.y+dy));state.lastInput=state.time;record('FLEET_MANEUVER',{owner:'HUMAN',x:state.ship.x,y:state.ship.y,epoch:tasks.fleet.epoch});}
  function aegisAction(){if(tasks.aegis.owner!=='HUMAN')return;state.aegisBurst=2.6;state.lastInput=state.time;record('AEGIS_FOCUSED',{owner:'HUMAN',epoch:tasks.aegis.epoch});advise('Point-defense focus active for 2.6 seconds.');}
  function toggleFormation(){state.formation=state.formation==='SCREEN'?'LANCE':'SCREEN';state.lastInput=state.time;record('FORMATION_CHANGED',{owner:'HUMAN',formation:state.formation,epoch:tasks.wings.epoch});updateStations();}
  function ai(dt){
    if(tasks.fleet.owner==='AI'){state.ship.y+=Math.sin(state.time*.42)*dt*.018;state.ship.y=Math.max(.28,Math.min(.72,state.ship.y));}
    if(tasks.bombard.owner==='AI'&&state.bombardCooldown<=0)fireBombard('AI');
    if(tasks.wings.owner==='AI'&&state.wingCooldown<=0)strikeWings('AI');
    if(tasks.aegis.owner==='AI'&&state.aegisBurst<=0&&state.enemyShots.length>1){state.aegisBurst=1.8;record('AEGIS_FOCUSED',{owner:'AI',epoch:tasks.aegis.epoch});}
  }
  function update(dt){
    if(!state.running)return;state.time+=dt;state.bombardCooldown=Math.max(0,state.bombardCooldown-dt);state.wingCooldown=Math.max(0,state.wingCooldown-dt);state.enemyCooldown-=dt;state.aegisBurst=Math.max(0,state.aegisBurst-dt);state.targetPulse+=dt;lastSignal=performance.now();
    if(state.enemyCooldown<=0){enemyFire();state.enemyCooldown=Math.max(.38,1.65-state.phase*.34-Math.min(.35,state.time/240));}
    ai(dt);
    for(var i=state.shots.length-1;i>=0;i--){var s=state.shots[i];s.t+=dt;var q=Math.min(1,s.t/s.d);s.x=s.x+(s.tx-s.x)*Math.min(1,dt*8);s.y=s.y+(s.ty-s.y)*Math.min(1,dt*8);if(q>=1){hitTarget(s);state.shots.splice(i,1);}}
    for(var j=state.enemyShots.length-1;j>=0;j--){var e=state.enemyShots[j];e.t+=dt;e.x=e.x+(e.tx-e.x)*Math.min(1,dt*4.5);e.y=e.y+(e.ty-e.y)*Math.min(1,dt*4.5);if(e.t>=e.d){enemyHit(e);state.enemyShots.splice(j,1);}}
    for(var k=state.particles.length-1;k>=0;k--){var p=state.particles[k];p.t+=dt;p.x+=p.vx*dt;p.y+=p.vy*dt;if(p.t>p.d)state.particles.splice(k,1);}
    if(state.time%10<dt&&state.wings<state.maxWings&&state.phase>0&&Math.random()<.23){state.wings++;record('WING_REINFORCED',{phase:state.phase+1});}
    state.frameSamples.push(1/Math.max(dt,.001));if(state.frameSamples.length>40)state.frameSamples.shift();state.fps=state.frameSamples.reduce(function(a,b){return a+b},0)/state.frameSamples.length;
    updateHud();
  }
  function draw(){
    ctx.clearRect(0,0,W,H);var g=ctx.createRadialGradient(W*.72,H*.5,0,W*.72,H*.5,W*.7);g.addColorStop(0,'#142029');g.addColorStop(.38,'#080d12');g.addColorStop(1,'#020406');ctx.fillStyle=g;ctx.fillRect(0,0,W,H);
    stars.forEach(function(s){ctx.globalAlpha=s.a;ctx.fillStyle='#d9ffff';ctx.fillRect(s.x*W,s.y*H,s.s,s.s)});ctx