<!-- SOLID CITY · part 97 -->
 '🗣 Voice my words: off'; });
  panelEl.querySelector('#cz-gesture').addEventListener('click', function () { var c = panelCitizen(); if (c && c.motion) c.motion.play(c.rec.signature); });
  panelEl.querySelector('#cz-stop').addEventListener('click', function (e) {
    var c = panelCitizen(); if (!c) return;
    if (c.stopped) { api('/api/citizens/' + c.id + '/resume', { method: 'POST', body: '{}' }).then(function (r) { if (r.ok) { c.stopped = false; refreshOps(); addLine('system', c.name + ' resumes.', 'sys'); } else addLine('system', r.error || 'The King or an admin resumes.', 'sys'); }); }
    else {
      if (c.motion) c.motion.stopAll ? c.motion.stopAll() : c.motion.cancel();
      c.session = null; c.moveTarget = null;
      api('/api/citizens/' + c.id + '/stop', { method: 'POST', body: '{}' }).then(function (r) { if (r.ok) { c.stopped = true; refreshOps(); addLine('system', c.name + ' is stopped.', 'sys'); } else addLine('system', r.error || 'Not yours to stop.', 'sys'); });
    }
  });
  panelEl.querySelector('#cz-possess').addEventListener('click', function () {
    var c = panelCitizen(); if (!c) return;
    if (M.possess && M.possess.c === c) { releasePossess(true); addLine('system', 'You step back into yourself. ' + c.name + ' walks home.', 'sys'); return; }
    if (c.op && c.op.type === 'human' && M.me && c.op.name === M.me) possess(c);
    else addLine('system', 'Inhabit first — the body follows the operator.', 'sys');
  });
  panelEl.querySelector('#cz-inhabit').addEventListener('click', function () {
    var c = panelCitizen(); if (!c) return;
    var mine = c.op && c.op.type === 'human' && M.me && c.op.name === M.me;
    var to = mine ? { type: 'ai' } : (c.op && c.op.type === 'human' ? { type: 'ai' } : { type: 'human' });
    api('/api/citizens/' + c.id + '/handoff', { method: 'POST', body: JSON.stringify({ to: to }) }).then(function (r) {
      if (r.ok) { c.op = r.operator; c.epoch = r.epoch; drawPlate(c); refreshOps(); addLine('system', 'Operator now: ' + (r.operator.type === 'human' ? 'you, inhabiting ' + c.name : r.operator.type) + ' (epoch ' + r.epoch + ').', 'sys'); }
      else addLine('system', r.error || 'Handoff refused.', 'sys');
    });
  });
}
function addLine(who, text, kind) {
  if (!logEl) return;
  var d = document.createElement('div');
  d.style.cssText = kind === 'me' ? 'align-self:flex-end;background:#23314a;border-radius:12px;padding:7px 11px;max-width:85%' : kind === 'sys' ? 'color:#9aa4b5;font-size:12px;align-self:center' : 'align-self:flex-start;background:#1a2436;border-radius:12px;padding:7px 11px;max-width:85%';
  d.textContent = (kind === 'sys' ? '' : who + ': ') + text;
  logEl.appendChild(d); while (logEl.children.length > 30) logEl.removeChild(logEl.firstChild);
  logEl.scrollTop = logEl.scrollHeight;
}
function refreshOps() {
  var c = panelCitizen(); if (!c) return;
  titleEl.textContent = c.name + ' — ' + c.rec.craft;
  var opLine = !c.op ? '' : c.op.type === 'human' ? 'Operated by ' + (c.op.name || 'a human') + ' · epoch ' + c.op.epoch : c.op.type === 'unoccupied' ? 'Unoccupied · resting' : 'Operated by AI · epoch ' + (c.op.epoch || 1);
  opsEl.textContent = opLine + (c.stopped ? ' · ⛔ STOPPED' : '');
  panelEl.querySelector('#cz-inhabit').textContent = (c.op && c.op.type === 'human' && M.me && c.op.name === M.me) ? 'Release' : 'Inhabit';
  panelEl.querySelector('#cz-possess').textContent = (M.possess && M.possess.c === c) ? '🚶 Release body' : '👁 Possess';
  panelEl.querySelector('#cz-voicemine').style.display = (c.op && c.op.type === 'human' && M.me && c.op.name === M.me) ? '' : 'none';
  panelEl.querySelector('#cz-stop').textContent = c.stopped ? '▶ Resume' : '⛔ Stop';
  var inboxEl = panelEl.querySelector('#cz-inbox');
  if (c.op && c.op.type === 'human' && M.me && c.op.name === M.me) {
    api('/api/citizens/' + c.id + '/inbox').then(function (r) {
      if (r.ok && r.inbox.length) { inboxEl.style.display = ''; inboxEl.textContent = '📥 Waiting for your answer: ' + r.inbox.map(function (m) { return m.speaker + ' — “' + m.text + '”'; }).join(' · '); }
      else inboxEl.style.display = 'none';
    });
  } else inboxEl.style.display = 'none';
}
function openPanel(c) {
  M.panelFor = c.id; panelEl.style.display = '';
  logEl.innerHTML = '';
  addLine(c.name, c.rec.greeting || ('I am ' + c.name + '.'), 'them');
  refreshOps();
}
function closePanel() { M.panelFor = null; panelEl.style.display = 'none'; }
function sendPanel() {
  var c = panelCitizen(); if (!c) return;
  var text = inputEl.value.trim(); if (!text) return;
  inputEl.value = '';
  var mine = c.op && c.op.type === 'human' && M.me && c.op.name === M.me;
  addLine(mine ? c.name + ' (you)' : 'You', text, 'me');
  if (c.stopped) { addLine('system', c.name + ' is stopped and cannot answer.', 'sys'); return; }
  if (mine) {
    api('/api/citizens/' + c.id + '/reply', { method: 'POST', body: JSON.stringify({ text: text, epoch: c.op.epoch }) }).then(function (r) {
 