<!-- SOLID CITY · part 2178 -->
() {
        if (!window.CityRoyal) return;
        if (CityRoyal.flying()) { CityRoyal.setFly(false); return; }
        stopAI(true); CityRoyal.setFly(true); setMode('FLY'); showRise(true);
        narrate('🪽 <b>Flight is yours, my King.</b> Steer with the stick, hold ⬆ / ⬇ to climb and sink, tap 🚶 WALK to land.', 6);
      });
      if (window.CityRoyal) CityRoyal.onLand = function () { setMode('WALK'); showRise(false); narrate('Landed. The ground remembers its King.', 3); };
      $('hc-buttons').addEventListener('click', function (e) {
        var t = e.target && e.target.closest ? e.target.closest('.hc-mode-btn') : null;
        if (t && t.getAttribute('data-mode') === 'WALK' && window.CityRoyal && CityRoyal.flying()) { e.stopPropagation(); e.preventDefault(); CityRoyal.setFly(false); }
      }, true);
    }
    flyBtn.style.display = '';
  }
  function unlock() {
    if (sending) return; sending = true;
    var inp = document.getElementById('hc-royal-pw'); var pw = inp ? inp.value : '';
    post('/api/city/royal/unlock', { password: pw }).then(function (r) {
      sending = false;
      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;
    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(); });
