<!-- SOLIDABILITY-WORLD · part 42 -->
  'authentication': 'auth', 'auth': 'auth',
        'ai systems': 'ai', 'legal center': 'legal',
      };
      const mappedQuery = prototypeMap[query] || query;

      const room = layout.rooms.find(r =>
        r.label.toLowerCase().includes(mappedQuery) ||
        (r.filePath && r.filePath.toLowerCase().includes(mappedQuery)) ||
        r.routes.some(rt => rt.name.toLowerCase().includes(mappedQuery)) ||
        (r.functions && r.functions.some(fn => fn.name.toLowerCase().includes(mappedQuery)))
      );
      if (room) {
        focusedRoom = room;
        const detail = room.filePath
          ? `${room.filePath} (complexity ${room.complexity})`
          : `${room.routes.length} routes`;
        aiDriveTo(room, `You asked for "${query}" — matched ${room.label} (${detail}).`);
      } else {
        narrate(`I couldn't find anything matching "${query}". ` +
          `Try a filename like "server.js", "deck", or "views".`);
      }
      return;
    }

    // "show me what this room controls" — inspect current room
    if (/show me what this (room|area) (controls|does)/.test(c)) {
      const player = SWEngine.getPlayer();
      const room = layout.rooms.find(r =>
        Math.hypot(r.position.x - player.position.x, r.position.z - player.position.z) < r.size.width &&
        Math.abs(r.position.y - player.position.y) < 5
      );
      if (room) {
        SWGlass.show(room);
        narrate(`You're in <b>${room.label}</b>. I've opened its details in the glass panel.`);
      } else {
        narrate(`You're not inside a room right now. Walk through a teal doorway to enter one.`);
      }
      return;
    }

    // "return" / "go back" / "take me back"
    if (/^(return|go back|back|take me back)$/.test(c)) {
      if (!goReturn()) narrate(`No previous location to return to.`);
      else narrate(`Returning to your previous location.`);
      return;
    }

    // "scan"
    if (/^scan$/.test(c)) {
      scan();
      return;
    }

    narrate(`I understand: "drive me to X", "tour", "find problem", "take me upstairs", "stop".`);
  }

  function showCommandFeedback(html) {
    const input = document.getElementById('hc-input');
    if (!input) return;
    // Show as placeholder temporarily
    const orig = input.placeholder;
    input.placeholder = html.replace(/<[^>]*>/g, '');
    setTimeout(() => { input.placeholder = orig; }, 3000);
  }

  // ============ AI DRIVE ============

  // Narration: AI explains what it's doing
  function narrate(text) {
    let bar = document.getElementById('ai-narration');
    if (!bar) {
      bar = document.createElement('div');
      bar.id = 'ai-narration';
      document.getElementById('app').appendChild(bar);
    }
    bar.innerHTML = `<span class="ai-badge">🤖 AI DRIVE</span> ${text}`;
    bar.classList.add('visible');
    clearTimeout(bar._hide);
    bar._hide = setTimeout(() => bar.classList.remove('visible'), 8000);
  }

  function clearNarration() {
    const bar = document.getElementById('ai-narration');
    if (bar) bar.classList.remove('visible');
  }

  // Start AI DRIVE to a destination room
  function aiDriveTo(room, reason) {
    if (!room) return false;
    aiDriving = true;
    setMode('AI_DRIVE');
    pushHistory();

    const destName = room.label || 'destination';
    narrate(`Driving to <b>${destName}</b>. ${reason || ''}`);

    const player = SWEngine.getPlayer();
    const path = findPath(
      { x: player.position.x, y: player.position.y, z: player.position.z },
      { roomKey: room.key }
    );

    if (!path || !path.length) {
      narrate(`Cannot reach <b>${destName}</b> — no valid path through the building. ` +
        `The destination may be behind a wall or on an inaccessible floor.`);
      aiDriving = false;
      setMode('WALK');
      return false;
    }

    // Describe the route
    const floors = [...new Set(path.map(p => Math.round(p.y / 8)))];
    const viaStairs = path.some((p, i) => i > 0 && Math.abs(p.y - path[i-1].y) > 1);
    let route = `Taking ${path.length} waypoints`;
    if (viaStairs) route += ', using the stairs';
    if (floors.length > 1) route += ` (floors ${floors.map(f => f + 1).join(' → ')})`;
    narrate(`Driving to <b>${destName}</b>. ${route}. ${reason || ''}`);

    navPath = path;
    navIndex = 0;
    navigating = true;
    focusedRoom = room;
    showPathTrail(path);
    return true;
  }

  // Guided Tour: walk through all rooms explaining each
  function startTour() {
    const layout = SWWorld.getLayout();
    if (!layout || !layout.rooms.length) return;
    aiDriving = true;
    touring = true;
    // Full map: tour top 10 buildings by complexity. Prototype: all rooms by floor.
    if (layout.fullMap || layout.public || !layout.building) {
      tourQueue = [...layout.rooms]
        .sort((a, b) => b.complexity - a.complexity)
        .slice(0, 10);
    } else {
      tourQueue = [...layout.rooms].sort((a, b) => a.floor - b.floor);
    }
    tourIndex = 0;
    