<!-- SAGA-SOLID · part 102 -->
r:1px solid #555;' +
      'background:rgba(255,255,255,0.06);color:#f0e8d8;font-size:15px;cursor:pointer;">Done</button>';
    document.body.appendChild(d);
    document.getElementById('ai-token-copy').onclick = function(){
      try { navigator.clipboard.writeText(token); } catch(e){}
    };
    document.getElementById('ai-token-done').onclick = function(){
      d.remove();
      // Token is NOT stored in browser. Player must have copied it.
    };
  }

  // V219 test → trigger hidden V219 run button, mirror output
  document.getElementById('cockpit-v219run').onclick = function(){
    var btn = document.getElementById('v219-run');
    if(btn) btn.click();
    // Mirror output
    var src = document.getElementById('v219-out');
    var dst = document.getElementById('cockpit-v219out');
    if(src && dst){
      var mirror = setInterval(function(){
        dst.textContent = src.textContent;
        dst.scrollTop = dst.scrollHeight;
        if(sheet.style.display === 'none') clearInterval(mirror);
      }, 500);
    }
  };

  // Takeover → trigger hidden SD takeback
  takeover.onclick = function(){
    var btn = document.getElementById('sd-takeback');
    if(btn) btn.click();
    // Also trigger V219 abort as backup
    var abort = document.getElementById('v219-abort');
    if(abort) abort.click();
  };

  /* ---------- State refresh ---------- */
  function refreshCockpit(){
    try {
      var R = window.ACTOR_RUNTIME;
      var SD = window.SOLID_DRIVE;
      if(!R) return;

      // Pill state
      var aiDriving = R.authority === 'ai_guarded' && R.ai && R.ai.active;
      var aiName = (window.AI_DRIVER && window.AI_DRIVER.activeAgent)
        ? window.AI_DRIVER.activeAgent.name
        : ((window.SOLID_DRIVE && window.SOLID_DRIVE._agentName) || 'SOLID');
      if(aiDriving){
        pill.textContent = '◉ ' + aiName.toUpperCase() + ' DRIVING';
        pill.style.color = CYAN;
        pill.style.borderColor = CYAN;
        takeover.style.display = 'block';
      } else {
        pill.textContent = '◉ YOU DRIVE';
        pill.style.color = GOLD;
        pill.style.borderColor = GOLD;
        takeover.style.display = 'none';
      }

      // Drive status
      var ds = document.getElementById('cockpit-drive-status');
      var authBtn = document.getElementById('cockpit-authorize');
      if(ds){
        var connName = (window.AI_DRIVER && window.AI_DRIVER.activeAgent)
          ? window.AI_DRIVER.activeAgent.name
          : ((SD && SD._agentName) || null);
        if(SD && SD.active){
          ds.innerHTML = '<span style="color:' + CYAN + ';">● Connected</span>' +
            (connName ? '<span style="color:' + DIM + ';font-size:12px;"> — ' + connName + '</span>' : '');
          if(authBtn) authBtn.style.display = 'none';
        } else if(connName){
          ds.innerHTML = '<span style="color:' + CYAN + ';">AI: ' + connName + ' — READY</span>';
          if(authBtn) authBtn.style.display = 'none';
        } else {
          ds.innerHTML = '<span style="color:' + DIM + ';">No AI connected</span>';
          if(authBtn) authBtn.style.display = 'block';
        }
      }

      // Safety
      var sv = R.safetySupervisor;
      var ss = document.getElementById('ck-safety-state');
      if(ss && sv){
        ss.textContent = sv.status + (sv.permit ? ' ✓' : '');
        ss.style.color = (sv.status === 'SAFE' && sv.permit) ? '#7ab87a' : WINE;
      }
      var ca = document.getElementById('ck-authority');
      if(ca) ca.textContent = R.authority;
      var co = document.getElementById('ck-owner');
      if(co){
        co.textContent = R.input.owner === 'human' ? 'YOU' : 'SOLID';
        co.style.color = R.input.owner === 'human' ? GOLD : CYAN;
      }

      // Diagnostics (from hidden SD panel)
      var diagSrc = document.getElementById('sd-diag');
      var diagDst = document.getElementById('cockpit-diag-body');
      if(diagSrc && diagDst) diagDst.textContent = diagSrc.textContent;

    } catch(e){}
  }

  // Refresh pill every 500ms (lightweight)
  setInterval(function(){
    try {
      var R = window.ACTOR_RUNTIME;
      if(!R) return;
      var aiDriving = R.authority === 'ai_guarded' && R.ai && R.ai.active;
      var aiName2 = (window.AI_DRIVER && window.AI_DRIVER.activeAgent)
        ? window.AI_DRIVER.activeAgent.name
        : ((window.SOLID_DRIVE && window.SOLID_DRIVE._agentName) || 'SOLID');
      if(aiDriving){
        pill.textContent = '◉ ' + aiName2.toUpperCase() + ' DRIVING';
        pill.style.color = CYAN;
        pill.style.borderColor = CYAN;
        takeover.style.display = 'block';
      } else {
        pill.textContent = '◉ YOU DRIVE';
        pill.style.color = GOLD;
        pill.style.borderColor = GOLD;
        takeover.style.display = 'none';
      }
    } catch(e){}
  }, 500);

  // Also sync hidden SD status display for its internal logic
  var origSdSetStatus = window._sdSetStatusHook;
  setInterval(refreshCockpit, 2000);

  if(window.consol