<!-- RUBY-BACKUP · part 84 -->
y('balance'); }, 'SYSTEM'],
    ['p4-ship','Ship',           function(){ say('ship'); }, 'SYSTEM'],
    ['p4-history','History',     function(){ code('did'); }, 'SYSTEM'],
  ];

  var SOLID_TOOLS = [
    ['p1-cube','Rigging',   function(){ code('codex'); }],
    ['p1-pin','Teleport',   function(){ code('whereami'); }],
  ];

  /* --------------------------------------------------------------------------
     Helpers
     -------------------------------------------------------------------------- */
  function wireOSToggle(){
    var t = document.getElementById('os-toggle');
    if(!t) return;
    function toggle(){
      if(osEl.style.display === 'none'){
        window.pulleyDown();
      } else {
        window.pulleyUp();
      }
    }
    t.onclick = toggle;
    t.addEventListener('touchend', function(e){
      e.preventDefault(); e.stopPropagation(); toggle();
    }, {passive:false});
  }

  function toggleChat(){
    var c = document.getElementById('ruby-chat');
    if(c) c.classList.toggle('open');
  }
  function focusCode(){
    /* Focus the TOP bar (solidinput) and pop the iOS keyboard */
    var top = document.getElementById('solidinput');
    if(top){
      top.focus();
      /* iOS needs a click in the gesture to show keyboard */
      try{ top.click(); }catch(e){}
      /* Scroll it into view */
      try{ top.scrollIntoView({block:'center'}); }catch(e){}
      return;
    }
    /* Fallback to panel input */
    var i = document.getElementById('ruby-os-code');
    if(i) i.focus();
  }

  /* --------------------------------------------------------------------------
     Build the OS
     -------------------------------------------------------------------------- */
  var osEl, pulleyBtn, rope, cycleIdx = -1, allFns = [];

  function build(){
    osEl = document.getElementById('ruby-os');
    if(!osEl){
      osEl = document.createElement('div');
      osEl.id = 'ruby-os';
      document.body.appendChild(osEl);
    }
    osEl.innerHTML = '';
    osEl.style.cssText = 'position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);'
      + 'width:min(88vw,360px);max-height:60vh;overflow-y:auto;overscroll-behavior:contain;'
      + 'background:rgba(10,8,4,0.95);border:2px solid #d8a920;border-radius:18px;'
      + 'padding:16px;z-index:1000;display:flex;flex-wrap:wrap;justify-content:center;gap:8px;'
      + 'touch-action:manipulation;transition:transform 0.6s cubic-bezier(0.4,0,0.2,1),opacity 0.6s;';

    /* CORE header + code box */
    osEl.appendChild(makeHeader('CORE'));
    var row = document.createElement('div');
    row.style.cssText = 'width:100%;display:flex;gap:6px;margin:6px 0;'
      + 'position:sticky;top:0;z-index:10;background:rgba(10,8,4,0.95);padding:6px 0;';
    var input = document.createElement('input');
    input.id = 'ruby-os-code';
    input.placeholder = 'type a code...';
    input.style.cssText = 'flex:1;background:#0a0805;border:1px solid #d8a920;border-radius:8px;'
      + 'color:#ffd700;padding:10px;font-size:16px;font-family:Georgia,serif;';
    var send = document.createElement('button');
    send.textContent = 'send';
    send.style.cssText = 'background:#d8a920;color:#111;border:none;border-radius:8px;'
      + 'padding:10px 18px;font-weight:bold;cursor:pointer;font-family:Georgia,serif;'
      + 'font-size:16px;touch-action:manipulation;';
    send.onclick = function(){
      var c = input.value.trim();
      if(c){ code(c); input.value = ''; }
    };
    input.addEventListener('keydown', function(e){ if(e.key==='Enter') send.onclick(); });
    row.appendChild(input); row.appendChild(send);
    osEl.appendChild(row);

    /* FAVORITES — most-used pinned to top */
    osEl.appendChild(makeHeader('FAVORITES'));
    ['Walk','Fly','Build district','Map','Chat'].forEach(function(name){
      var b = CORE.find(function(x){ return x[1]===name; });
      if(b){ osEl.appendChild(makeButton(b[0], b[1], b[2])); }
    });

    /* CORE buttons grouped */
    var lastGroup = '';
    CORE.forEach(function(b){
      if(b[3] && b[3]!==lastGroup){
        osEl.appendChild(makeHeader(b[3]));
        lastGroup = b[3];
      }
      /* Skip favorites (already shown) */
      if(['Walk','Fly','Build district','Map','Chat'].indexOf(b[1])<0){
        osEl.appendChild(makeButton(b[0], b[1], b[2]));
      }
      allFns.push({label:b[1], fn:b[2]});
    });

    /* BUILD group */
    osEl.appendChild(makeHeader('BUILD'));
    BUILD.forEach(function(b){
      osEl.appendChild(makeButton(b[0], b[1], b[2]));
      allFns.push({label:b[1], fn:b[2]});
    });

    /* WORLD group */
    osEl.appendChild(makeHeader('WORLD'));
    WORLD.forEach(function(b){
      osEl.appendChild(makeButton(b[0], b[1], b[2]));
      allFns.push({label:b[1], fn:b[2]});
    });

    /* SYSTEM group */
    osEl.appendChild(makeHeader('SYSTEM'));
    SYSTEM.forEach(function(b){
      osEl.appendChild(makeButton(b[0], b[1], b[2]));
      allFns.push({label:b[1], fn:b[2]});
    });

    /* 