<!-- SOLID CITY · part 2197 -->
se;
      if (r.status === 200 && r.j && r.j.ok) { hideCard(); applyRoyal(); narrate('👑 <b>The Royal Gate is open.</b> 🪽 FLY has appeared, and 🔨 BUILD answers to your hand alone.', 7); if (window.logBuild) logBuild({ t: Date.now(), site: 'ROYAL GATE', what: 'The King unlocked royal rights (fly + build)' }); }
      else if (r.status === 503) { hideCard(); narrate('The gate is built, but no royal password has been set yet. Set it once on the box and the gate will answer.', 7); }
      else { hideCard(); narrate('The gate stays shut — that was not the royal password. The attempt was written to the royal log.', 6); }
    }).catch(function () { sending = false; hideCard(); narrate('The gate could not be reached. Try again in a moment.', 5); });
  }
  function openGate() {
    showCard('👑 THE ROYAL GATE', 'Your account is recognized, my King. The password is yours alone: checked on the server, never stored, and every attempt is written to the royal log.',
      "<input type='password' id='hc-royal-pw' placeholder='Royal password' autocomplete='off'>",
      [ { label: '🔓 UNLOCK', gold: true, fn: unlock }, { label: 'NOT NOW', fn: hideCard } ]);
    var inp = document.getElementById('hc-royal-pw');
    if (inp) { inp.focus(); inp.addEventListener('keydown', function (e) { if (e.key === 'Enter') unlock(); }); }
  }
  fetch('/api/city/me', { credentials: 'same-origin', cache: 'no-store' }).then(function (r) { return r.json(); }).then(function (j) {
    me = j;
    try { fetch('/api/city/royal/diag', { method: 'POST', credentials: 'same-origin', cache: 'no-store', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': (j && j.csrf) || '' }, body: JSON.stringify({ loggedIn: !!(j && j.loggedIn), king: !!(j && j.king), royal: !!(j && j.royal) }) }).catch(function () {}); } catch (e) {}
    if (!j || !j.loggedIn || !j.king) return; // not the King: no key, no FLY, no BUILD — nothing to see
    if (j.royal) { applyRoyal(); return; }
    var keyBtn = document.createElement('button');
    keyBtn.type = 'button'; keyBtn.id = 'hc-royal-key'; keyBtn.className = 'hc-btn hc-ai';
    keyBtn.title = 'The Royal Gate'; keyBtn.setAttribute('aria-label', 'The Royal Gate'); keyBtn.textContent = '🔑';
    var look2 = hc.querySelector('.hc-mode-btn[data-mode="LOOK"]');
    if (look2 && look2.nextSibling) look2.parentNode.insertBefore(keyBtn, look2.nextSibling); else $('hc-buttons').appendChild(keyBtn);
    keyBtn.addEventListener('click', openGate);
  }).catch(function () {});
})();
$('hc-aidrive').addEventListener('click', function () {
  if (H.nav || H.tour || H.flyDrive || H.armed) { stopAI(); hideCard(); return; }
  stopAI(true); H.armed = true; setMode('AI DRIVE');
  narrate(flying() ? 'AI Drive engaged. Where to? Pick a landmark, or try "fly me to Mauna Kea". Tap 🤖 again to cancel.' : 'AI Drive engaged. Tell me where to go: try "drive me to studio", pick a place, or tap 🗺️ for a guided tour.', 8);
  picker();
});
$('hc-tour').addEventListener('click', function () { if (H.tour) stopAI(); else startTour(); });
$('hc-focus').addEventListener('click', function () { if (!focusNearest()) narrate('Nothing to focus nearby.', 3); });
$('hc-scan').addEventListener('click', scan);
$('hc-go').addEventListener('click', function () {
  if (H.focus) { hideCard(); driveTo(H.focus, 'NAVIGATE'); }
  else { hint(flying() ? 'Focus a landmark first, then tap 🧭' : 'Focus a place first, then tap 🧭'); picker(); }
});
$('hc-return').addEventListener('click', function () { if (!goReturn()) { hint('No previous location'); narrate('No previous location to return to.', 3); } else narrate('Returning to your previous location.', 3); });
$('hc-stop').addEventListener('click', function () { stopAI(); hideCard(); });
$('hc-guide').addEventListener('click', guide);
$('hc-build').addEventListener('click', function () { stopAI(true); hideCard(); clearNarration(); if (window.CityForge) CityForge.toggle(); });
$('hc-watch').addEventListener('click', function () { stopAI(true); hideCard(); if (window.CityWatch) CityWatch.open(); });
$('hc-glossary').addEventListener('click', function () { if (!card.hidden && card.querySelector('.hc-gl')) hideCard(); else glossary(); });
inp.addEventListener('keydown', function (e) {
  if (e.key === 'Escape') { inp.blur(); return; }
  if (e.key !== 'Enter') return;
  var v = inp.value; inp.value = ''; if (A.IS_TOUCH) inp.blur(); handleCommand(v);
});
addEventListener('keydown', function (e) {
  var tg = e.target && e.target.tagName; if (tg === 'INPUT' || tg === 'TEXTAREA') return;
  if (e.key === '/' && !hc.hidden) { e.preventDefault(); inp.focus(); }
  else if (e.key === 'Escape') { if (!card.hidden) hideCard(); else if (H.nav || H.tour || H.flyDrive || H.armed) stopAI(); }
});
var cv = $('sc-gl'); ['pointerdown', 'pointermove'].forEach(function (ev) { cv.addEventListener(ev, function (e) { if (e.type === 'pointerdown' || e.buttons) H.lastDrag = performance.now(); }); });

/* ------------