<!-- SAGA-STAGING · part 70 -->
ace;border:2px solid #ffd700;cursor:pointer;display:flex;align-items:center;justify-content:center;";ab.addEventListener("click",function(){var p=document.getElementById("world-intelligence-panel");if(p){p.remove();return;}var x=document.getElementById("m-worldintel");if(x){x.click();if(menu.style.display!=="none")toggleMenu();}});document.body.appendChild(ab);}
  document.body.appendChild(b);
})();


/* IN-GAME CHAT — talk while building */
(function(){
  if(document.getElementById('game-chat')) return;

  /* CLEAR CACHE — on screen next to the diamond. King order 2026-10-05. */
  if(!document.getElementById('clearcache-btn')){
    var ccb = document.createElement('button');
    ccb.id = 'clearcache-btn';
    ccb.innerHTML = '🧹';
    ccb.title = 'Clear cache';
    ccb.style.cssText = 'position:fixed;top:12px;right:64px;z-index:9999;' +
      'width:44px;height:44px;border-radius:50%;' +
      'background:#1a1a2e;color:#ffd700;font-size:20px;' +
      'border:2px solid #ffd700;cursor:pointer;' +
      'display:flex;align-items:center;justify-content:center;';
    ccb.addEventListener('click', function(){ if(confirm('Clear?')) location.reload(); });
    document.body.appendChild(ccb);
  }
  // Chat toggle button (bottom-left)
  var chatBtn = document.createElement('button');
  chatBtn.id = 'game-chat-btn';
  chatBtn.innerHTML = '💬';
  chatBtn.style.cssText = 'position:fixed;top:12px;left:175px;z-index:9999;display:none;' +
    'width:44px;height:44px;border-radius:50%;' +
    'background:#1a1a2e;color:#ffd700;font-size:20px;' +
    'border:2px solid #ffd700;cursor:pointer;';
  document.body.appendChild(chatBtn);

  // Chat panel (hidden by default)
  var panel = document.createElement('div');
  panel.id = 'game-chat';
  panel.style.cssText = 'position:fixed;top:120px;left:12px;z-index:9998;' +
    'width:280px;height:300px;background:rgba(10,10,26,0.95);' +
    'border:2px solid #ffd700;border-radius:12px;display:none;' +
    'flex-direction:column;overflow:hidden;';
  panel.innerHTML =
    '<div id="game-chat-msgs" style="flex:1;overflow-y:auto;padding:8px;color:#ffd700;font-size:13px;"></div>' +
    '<div style="display:flex;border-top:1px solid #ffd700;">' +
    '<input id="game-chat-input" placeholder="Talk..." ' +
    'style="flex:1;background:transparent;border:none;color:#ffd700;padding:10px;font-size:14px;outline:none;">' +
    '<button id="game-chat-send" style="background:#ffd700;color:#1a1a2e;border:none;padding:10px 14px;cursor:pointer;">➤</button>' +
    '</div>';
  document.body.appendChild(panel);

  var open = false;
  function toggleChat(){
    open = !open;
    panel.style.display = open ? 'flex' : 'none';
    if(typeof sfx === 'function') sfx('click');
  }
  chatBtn.addEventListener('click', function(e){ e.stopPropagation(); toggleChat(); });
  chatBtn.addEventListener('touchstart', function(e){ e.stopPropagation(); toggleChat(); }, {passive:true});

  var msgs = panel.querySelector('#game-chat-msgs');
  var input = panel.querySelector('#game-chat-input');

  function addMsg(who, text){
    var d = document.createElement('div');
    d.style.cssText = 'margin:4px 0;padding:4px 8px;border-radius:8px;' +
      (who === 'King' ? 'background:rgba(255,215,0,0.15);text-align:right;' : 'background:rgba(255,255,255,0.05);');
    d.innerHTML = '<b>' + who + ':</b> ' + text.replace(/</g, '&lt;');
    msgs.appendChild(d);
    msgs.scrollTop = msgs.scrollHeight;
  }

  function send(){
    var t = input.value.trim();
    if(!t) return;
    addMsg('King', t);
    input.value = '';
    // Send to server (poll for Solid's replies)
    fetch('/api/game-chat', {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({ msg: t, from: 'King' })
    }).then(function(r){
      if(!r.ok && window.gameChatSay) window.gameChatSay('System', 'Send failed: ' + r.status);
    }).catch(function(e){
      if(window.gameChatSay) window.gameChatSay('System', 'Send error: ' + (e.message || 'network'));
    });
    if(typeof sfx === 'function') sfx('click');
  }

  panel.querySelector('#game-chat-send').addEventListener('click', send);
  input.addEventListener('keydown', function(e){ if(e.key === 'Enter') send(); });

  // Poll for new messages every 3s
  var lastId = 0;
  setInterval(function(){
    fetch('/api/game-chat?since=' + lastId).then(function(r){ return r.json(); }).then(function(data){
      (data.msgs || []).forEach(function(m){
        if(m.id > lastId) lastId = m.id;
        if(m.from !== 'King') addMsg(m.from, m.text);
      });
    }).catch(function(){});
  }, 3000);

  // Expose for Solid to send messages
  window.gameChatSay = function(who, text){ addMsg(who, text); };

  addMsg('Solid', 'I\'m here, my King. Let\'s figure out this toggle.');
})();


/* TRANSCEIVERSHIP — fixed: clean turns, no stick, no delay */
(function(){
  if(window.__tx_init2) return; window.__tx_init2 = true;

  var txBtn = document.getElementById('tra