/**
 * KeyPilot Chrome Extension - Bundled Version
 * Generated on 2026-08-05T13:13:46.464Z
 */

(() => {
  // Global scope for bundled modules


  // Module: src/config/keyboard-layouts.js
/**
 * Keyboard layout architecture for KeyPilot.
 *
 * Goals:
 * - Separate "what an action does" from "which physical keys trigger it".
 * - Allow multiple built-in layouts today (right/left-handed browsing).
 * - Provide a single source of truth consumed by:
 *   - runtime keydown mapping (KeyPilot)
 *   - keyboard visualization (popup + floating keyboard reference)
 * - Future-proof for user-defined layouts (store user layouts separately; keep IDs stable).
 */

/**
 * @typedef {'browsing-right'|'browsing-left'} BuiltinKeyboardLayoutId
 * @typedef {BuiltinKeyboardLayoutId|string} KeyboardLayoutId
 */

/**
 * @typedef {{
 *   keys: string[],
 *   matchOn?: Array<'key'|'code'>,
 *   displayKey?: string,
 *   keyLabel?: string
 * }} KeyAssignment
 */

/**
 * @typedef {{
 *   handler: string,
 *   label: string,
 *   description: string,
 *   keyboardClass?: string|null,
 *   row?: number|null
 * }} ActionDef
 */

/**
 * @typedef {{
 *   id: BuiltinKeyboardLayoutId,
 *   label: string,
 *   description?: string,
 *   assignments: Record<string, KeyAssignment>,
 *   keyboardLayout: any[]
 * }} BuiltinKeyboardLayout
 */

const DEFAULT_KEYBOARD_LAYOUT_ID = /** @type {const} */ ('browsing-right');

const BUILTIN_KEYBOARD_LAYOUT_META = Object.freeze([
  Object.freeze({
    id: /** @type {const} */ ('browsing-right'),
    label: 'Browsing: right-handed',
    description: 'Mouse: right hand. Keyboard shortcuts primarily on the left side.'
  }),
  Object.freeze({
    id: /** @type {const} */ ('browsing-left'),
    label: 'Browsing: left-handed',
    description: 'Mouse: left hand. Keyboard shortcuts primarily on the right side.'
  })
]);

/**
 * @param {any} raw
 * @returns {BuiltinKeyboardLayoutId}
 */
function normalizeKeyboardLayoutId(raw) {
  const v = String(raw || '').trim();
  if (v === 'browsing-right' || v === 'browsing-left') return /** @type {BuiltinKeyboardLayoutId} */ (v);
  return DEFAULT_KEYBOARD_LAYOUT_ID;
}

/**
 * Canonical action definitions (no key assignments).
 * Keep this stable; it’s the contract between key mappings + handlers + UI.
 *
 * NOTE: This intentionally mirrors the old `KEYBINDINGS` metadata fields so we can
 * generate the legacy object shape used by existing code + build tooling.
 *
 * @type {Record<string, ActionDef>}
 */
const KEYBINDING_ACTION_DEFS = Object.freeze({
  ACTIVATE: Object.freeze({
    handler: 'handleActivateKey',
    label: 'Click Element',
    description: 'Click Element',
    keyboardClass: 'key-activate',
    row: 2
  }),
  // Foreground new tab (switch to the new tab).
  ACTIVATE_NEW_TAB: Object.freeze({
    handler: 'handleActivateNewTabKey',
    label: 'Click New Tab',
    description: 'Open Link in New Tab (Foreground)',
    keyboardClass: 'key-activate-new',
    row: 2
  }),
  // Background new tab (middle-click style; do not switch focus).
  ACTIVATE_NEW_TAB_BACKGROUND: Object.freeze({
    handler: 'handleActivateNewTabBackgroundKey',
    label: 'Click New Tab Background',
    description: 'Open Link in New Tab (Background, like middle click)',
    keyboardClass: 'key-activate-new-over',
    row: 2
  }),
  BACK: Object.freeze({
    handler: 'handleBackKey',
    label: 'Go Back',
    description: 'Go Back (History)',
    keyboardClass: 'key-back',
    row: 2
  }),
  BACK2: Object.freeze({
    handler: 'handleBackKey',
    label: 'Go Back',
    description: 'Go Back (History)',
    keyboardClass: 'key-back',
    row: 2
  }),
  FORWARD: Object.freeze({
    handler: 'handleForwardKey',
    label: 'Go Forward',
    description: 'Go Forward (History)',
    keyboardClass: 'key-forward',
    row: 1
  }),
  DELETE: Object.freeze({
    handler: 'handleDeleteKey',
    label: 'Delete Mode',
    description: 'Delete Mode',
    keyboardClass: 'key-delete',
    row: 2
  }),
  COLS_TOGGLE: Object.freeze({
    handler: 'handleColsToggleKey',
    label: 'Cols Toggle',
    description: 'Columnize element under cursor (multi-column layout)',
    keyboardClass: 'key-cols',
    row: 3
  }),
  TAB_LEFT: Object.freeze({
    handler: 'handleTabLeftKey',
    label: 'Tab Left',
    description: 'Move To Previous Tab',
    keyboardClass: 'key-gray',
    row: 1
  }),
  TAB_RIGHT: Object.freeze({
    handler: 'handleTabRightKey',
    label: 'Tab Right',
    description: 'Move To Next Tab',
    keyboardClass: 'key-gray',
    row: 1
  }),
  ROOT: Object.freeze({
    handler: 'handleRootKey',
    label: 'Go to Site Root',
    description: 'Go to Site Root',
    keyboardClass: null,
    row: null
  }),
  LAUNCHER: Object.freeze({
    handler: 'handleLauncherKey',
    label: 'Launcher',
    description: 'Open Launcher (Quick Access to Sites)',
    keyboardClass: 'key-launcher-orange',
    row: 2
  }),
  CLOSE_TAB: Object.freeze({
    handler: 'handleCloseTabKey',
    label: 'Close Tab',
    description: 'Close Tab',
    keyboardClass: 'key-close-tab',
    row: 3
  }),
  CANCEL: Object.freeze({
    handler: 'cancelModes',
    label: 'Exit Focus',
    description: 'Exit Focus',
    keyboardClass: null,
    row: null
  }),
  PAGE_UP_INSTANT: Object.freeze({
    handler: 'handleInstantPageUp',
    label: 'Page Up Fast',
    description: 'Page Up (Instant)',
    keyboardClass: 'key-scroll',
    row: 3
  }),
  PAGE_DOWN_INSTANT: Object.freeze({
    handler: 'handleInstantPageDown',
    label: 'Page Down Fast',
    description: 'Page Down (Instant)',
    keyboardClass: 'key-scroll',
    row: 3
  }),
  PAGE_TOP: Object.freeze({
    handler: 'handlePageTop',
    label: 'Scroll To Top',
    description: 'Scroll to Top',
    keyboardClass: 'key-scroll',
    row: 3
  }),
  PAGE_BOTTOM: Object.freeze({
    handler: 'handlePageBottom',
    label: 'Scroll To Bottom',
    description: 'Scroll to Bottom',
    keyboardClass: 'key-scroll',
    row: 3
  }),
  NEW_TAB: Object.freeze({
    handler: 'handleNewTabKey',
    label: 'New Tab',
    description: 'Open New Tab',
    keyboardClass: 'key-gray',
    row: 1
  }),
  OPEN_POPOVER: Object.freeze({
    handler: 'handleOpenPopover',
    label: 'Open Popover',
    description: 'Open Link in Popover',
    keyboardClass: 'key-open-popover',
    row: 2
  }),
  PREVIEW_LINK_POPOVER: Object.freeze({
    handler: 'handlePreviewLinkPopover',
    label: 'Preview Link',
    description: 'Open Link Preview in Popover',
    keyboardClass: 'key-preview-popover',
    row: 2
  }),
  OPEN_SETTINGS_POPOVER: Object.freeze({
    handler: 'handleToggleSettingsPopover',
    label: 'Settings',
    description: 'Open KeyPilot Settings',
    keyboardClass: 'key-settings-dark',
    row: null
  }),
  OMNIBOX: Object.freeze({
    handler: 'handleOpenOmnibox',
    label: 'Omnibox',
    description: 'Open Omnibox (Address Bar Overlay)',
    keyboardClass: 'key-orange',
    row: 2
  }),
  TAB_HISTORY: Object.freeze({
    handler: 'handleToggleTabHistoryPopover',
    label: 'Tab History',
    description: 'Open Tab History (Branch-Retaining)',
    keyboardClass: 'key-gray',
    row: 2
  }),
  TOGGLE_KEYBOARD_HELP: Object.freeze({
    handler: 'handleToggleKeyboardHelp',
    label: 'KB Reference',
    description: 'Show/Hide the floating KeyPilot keyboard reference',
    keyboardClass: 'key-purple',
    row: 2
  }),
  // Text select: default character-level (H on right-handed layout).
  HIGHLIGHT: Object.freeze({
    handler: 'handleHighlightKey',
    label: 'Text Select',
    description: 'Select text (character level)',
    keyboardClass: 'key-highlight',
    row: 2
  }),
  // Rectangle region select (Y on right-handed; R free on left-handed).
  RECTANGLE_HIGHLIGHT: Object.freeze({
    handler: 'handleRectangleHighlightKey',
    label: 'Rectangle Select',
    description: 'Select text in a rectangle',
    keyboardClass: 'key-rect-highlight',
    row: 1
  }),
  // Copy image under cursor (I on right-handed; E on left-handed — I is OPEN_POPOVER there).
  COPY_HOVERED_IMAGE: Object.freeze({
    handler: 'handleCopyHoveredImageKey',
    label: 'Copy Image',
    description: 'Copy image under cursor to clipboard',
    // Default key face (no tinted key-gray / family fill).
    keyboardClass: null,
    row: 1
  })
});

function upperLetter(s) {
  const ch = String(s || '');
  if (!ch) return '';
  return ch.length === 1 ? ch.toUpperCase() : ch;
}

/**
 * @param {KeyAssignment} a
 * @returns {{ keyLabel: string, displayKey: string }}
 */
function normalizeAssignmentLabels(a) {
  const keys = Array.isArray(a?.keys) ? a.keys : [];
  const first = keys[0] || '';

  // If explicit labels were provided, trust them.
  const explicitDisplay = typeof a?.displayKey === 'string' ? a.displayKey : '';
  const explicitKeyLabel = typeof a?.keyLabel === 'string' ? a.keyLabel : '';
  if (explicitDisplay || explicitKeyLabel) {
    const dk = explicitDisplay || explicitKeyLabel;
    const kl = explicitKeyLabel || explicitDisplay;
    return { keyLabel: kl || dk || '', displayKey: dk || kl || '' };
  }

  // Default: single letter keys show as uppercase.
  if (typeof first === 'string' && first.length === 1 && /[a-zA-Z]/.test(first)) {
    const up = upperLetter(first);
    return { keyLabel: up, displayKey: up };
  }

  // Default fallback: use the first key token.
  return { keyLabel: String(first || ''), displayKey: String(first || '') };
}

/**
 * Build the legacy `KEYBINDINGS` object shape used throughout the codebase.
 *
 * @param {BuiltinKeyboardLayoutId} layoutId
 * @returns {Record<string, any>}
 */
function buildKeybindingsForLayout(layoutId) {
  const id = normalizeKeyboardLayoutId(layoutId);
  const layout = BUILTIN_KEYBOARD_LAYOUTS[id];
  const out = {};

  for (const [actionId, def] of Object.entries(KEYBINDING_ACTION_DEFS)) {
    const assign = layout?.assignments?.[actionId];
    if (!assign || !Array.isArray(assign.keys)) continue;
    const labels = normalizeAssignmentLabels(assign);

    out[actionId] = {
      keys: assign.keys.slice(),
      ...(Array.isArray(assign.matchOn) ? { matchOn: assign.matchOn.slice() } : {}),
      handler: def.handler,
      label: def.label,
      description: def.description,
      keyLabel: labels.keyLabel,
      keyboardClass: def.keyboardClass ?? null,
      row: def.row ?? null,
      displayKey: labels.displayKey
    };
  }

  return out;
}

/**
 * @param {Record<string, KeyAssignment>} base
 * @returns {Record<string, KeyAssignment>}
 */
function cloneAssignments(base) {
  const out = {};
  for (const [k, v] of Object.entries(base || {})) {
    out[k] = {
      keys: Array.isArray(v?.keys) ? v.keys.slice() : [],
      ...(Array.isArray(v?.matchOn) ? { matchOn: v.matchOn.slice() } : {}),
      ...(typeof v?.displayKey === 'string' ? { displayKey: v.displayKey } : {}),
      ...(typeof v?.keyLabel === 'string' ? { keyLabel: v.keyLabel } : {})
    };
  }
  return out;
}

/**
 * Right-handed browsing (existing behavior).
 * @type {Record<string, KeyAssignment>}
 */
const ASSIGNMENTS_BROWSING_RIGHT = Object.freeze({
  TAB_LEFT: Object.freeze({ keys: ['q', 'Q'] }),
  TAB_RIGHT: Object.freeze({ keys: ['w', 'W'] }),
  OPEN_POPOVER: Object.freeze({ keys: ['p', 'P'] }),
  PREVIEW_LINK_POPOVER: Object.freeze({ keys: ['e', 'E'] }),
  FORWARD: Object.freeze({ keys: ['r', 'R'] }),
  NEW_TAB: Object.freeze({ keys: ['t', 'T'] }),

  CLOSE_TAB: Object.freeze({ keys: ['a', 'A'] }),
  BACK2: Object.freeze({ keys: ['s', 'S'] }),
  BACK: Object.freeze({ keys: ['d', 'D'] }),
  ACTIVATE: Object.freeze({ keys: ['f', 'F'] }),
  ACTIVATE_NEW_TAB_BACKGROUND: Object.freeze({ keys: ['g', 'G'] }),
  HIGHLIGHT: Object.freeze({ keys: ['h', 'H'] }),

  TAB_HISTORY: Object.freeze({ keys: ['j', 'J'] }),
  TOGGLE_KEYBOARD_HELP: Object.freeze({ keys: ['k', 'K'] }),
  OMNIBOX: Object.freeze({ keys: ['l', 'L'] }),
  LAUNCHER: Object.freeze({ keys: [';', ':', 'Semicolon', '`', '~', 'Backquote'], matchOn: ['key', 'code'], displayKey: ';', keyLabel: ';' }),

  OPEN_SETTINGS_POPOVER: Object.freeze({ keys: ["'", 'Quote'], matchOn: ['key', 'code'], displayKey: "'" }),

  PAGE_TOP: Object.freeze({ keys: ['z', 'Z'] }),
  PAGE_BOTTOM: Object.freeze({ keys: ['x', 'X'] }),
  PAGE_UP_INSTANT: Object.freeze({ keys: ['c', 'C'] }),
  PAGE_DOWN_INSTANT: Object.freeze({ keys: ['v', 'V'] }),
  ACTIVATE_NEW_TAB: Object.freeze({ keys: ['b', 'B'] }),
  RECTANGLE_HIGHLIGHT: Object.freeze({ keys: ['y', 'Y'] }),
  COPY_HOVERED_IMAGE: Object.freeze({ keys: ['i', 'I'] }),

  ROOT: Object.freeze({ keys: ['1', '!'], displayKey: '1', keyLabel: '1' }),
  DELETE: Object.freeze({ keys: ['Backspace'], displayKey: 'Backspace', keyLabel: 'Backspace' }),
  COLS_TOGGLE: Object.freeze({ keys: ['.', '>'], displayKey: '.', keyLabel: '.' }),
  CANCEL: Object.freeze({ keys: ['Escape'], displayKey: 'Esc', keyLabel: 'Esc' })
});

/**
 * Left-handed browsing:
 * Move the main left-cluster to the right side to be comfortable for the right hand.
 *
 * Note: A few "UI utility" actions remain on the left side to avoid collisions with
 * primary actions on the right home cluster.
 *
 * @type {Record<string, KeyAssignment>}
 */
const ASSIGNMENTS_BROWSING_LEFT = Object.freeze({
  // Top row cluster: Q W E R T  ->  P O I U Y (mirrored)
  TAB_LEFT: Object.freeze({ keys: ['p', 'P'] }),
  TAB_RIGHT: Object.freeze({ keys: ['o', 'O'] }),
  OPEN_POPOVER: Object.freeze({ keys: ['i', 'I'] }),
  PREVIEW_LINK_POPOVER: Object.freeze({ keys: ['w', 'W'] }),
  FORWARD: Object.freeze({ keys: ['u', 'U'] }),
  NEW_TAB: Object.freeze({ keys: ['y', 'Y'] }),

  // Home row cluster: A S D F G  ->  ; L K J H (mirrored-ish around center)
  CLOSE_TAB: Object.freeze({ keys: [';', ':'], displayKey: ';', keyLabel: ';' }),
  BACK2: Object.freeze({ keys: ['l', 'L'] }),
  BACK: Object.freeze({ keys: ['k', 'K'] }),
  ACTIVATE: Object.freeze({ keys: ['j', 'J'] }),
  ACTIVATE_NEW_TAB_BACKGROUND: Object.freeze({ keys: ['h', 'H'] }),
  // H is background-tab open on left; G/R free for selection.
  HIGHLIGHT: Object.freeze({ keys: ['g', 'G'] }),
  RECTANGLE_HIGHLIGHT: Object.freeze({ keys: ['r', 'R'] }),

  // Utility actions: keep on the left to avoid colliding with J/K/L cluster.
  TAB_HISTORY: Object.freeze({ keys: ['f', 'F'] }),
  TOGGLE_KEYBOARD_HELP: Object.freeze({ keys: ['d', 'D'] }),
  OMNIBOX: Object.freeze({ keys: ['s', 'S'] }),
  LAUNCHER: Object.freeze({ keys: ['a', 'A', '`', '~', 'Backquote'], matchOn: ['key', 'code'], displayKey: 'a/`', keyLabel: 'a/`' }),

  OPEN_SETTINGS_POPOVER: Object.freeze({ keys: ["'", 'Quote'], matchOn: ['key', 'code'], displayKey: "'" }),

  // Bottom row cluster: Z X C V B  ->  / . , M N (mirrored)
  // Period reserved for COLS_TOGGLE (same muscle memory as right-handed).
  PAGE_TOP: Object.freeze({ keys: ['/', '?'], displayKey: '/', keyLabel: '/' }),
  PAGE_BOTTOM: Object.freeze({ keys: ['b', 'B'] }),
  PAGE_UP_INSTANT: Object.freeze({ keys: [',', '<'], displayKey: ',', keyLabel: ',' }),
  PAGE_DOWN_INSTANT: Object.freeze({ keys: ['m', 'M'] }),
  ACTIVATE_NEW_TAB: Object.freeze({ keys: ['n', 'N'] }),
  COLS_TOGGLE: Object.freeze({ keys: ['.', '>'], displayKey: '.', keyLabel: '.' }),
  // I is OPEN_POPOVER on left-handed; E is free.
  COPY_HOVERED_IMAGE: Object.freeze({ keys: ['e', 'E'] }),

  ROOT: Object.freeze({ keys: ['1', '!'], displayKey: '1', keyLabel: '1' }),
  DELETE: Object.freeze({ keys: ['Backspace'], displayKey: 'Backspace', keyLabel: 'Backspace' }),
  CANCEL: Object.freeze({ keys: ['Escape'], displayKey: 'Esc', keyLabel: 'Esc' })
});

/**
 * Keyboard visualization layouts for the keybindings UI.
 * This is the same schema used by `src/ui/keybindings-ui-shared.js`.
 */
const KEYBOARD_UI_LAYOUT_RIGHT = Object.freeze([
  [
    { type: 'special', text: 'Tab', className: 'key key-tab' },
    { type: 'action', id: 'TAB_LEFT', fallbackText: 'Tab Left' },
    { type: 'action', id: 'TAB_RIGHT', fallbackText: 'Tab Right' },
    { type: 'action', id: 'PREVIEW_LINK_POPOVER', fallbackText: 'Preview Link' },
    { type: 'action', id: 'FORWARD', fallbackText: 'Go Forward' },
    { type: 'action', id: 'NEW_TAB', fallbackText: 'New Tab' },
    { type: 'action', id: 'RECTANGLE_HIGHLIGHT', fallbackText: 'Rectangle Select' },
    { type: 'key', text: 'U' },
    { type: 'action', id: 'COPY_HOVERED_IMAGE', fallbackText: 'Copy Image' },
    { type: 'key', text: 'O' },
    { type: 'action', id: 'OPEN_POPOVER', fallbackText: 'Open Popover' },
    { type: 'key', text: '[' },
    { type: 'key', text: ']' },
    { type: 'action', id: 'DELETE', fallbackText: 'Delete Mode', className: 'key key-backspace' }
  ],
  [
    { type: 'special', text: 'Caps', className: 'key key-caps' },
    { type: 'action', id: 'CLOSE_TAB', fallbackText: 'Close Tab' },
    { type: 'action', id: 'BACK2', fallbackText: 'Go Back' },
    { type: 'action', id: 'BACK', fallbackText: 'Go Back' },
    { type: 'action', id: 'ACTIVATE', fallbackText: 'Click Element' },
    { type: 'action', id: 'ACTIVATE_NEW_TAB_BACKGROUND', fallbackText: 'Click New Tab Background' },
    { type: 'action', id: 'HIGHLIGHT', fallbackText: 'Text Select' },
    { type: 'action', id: 'TAB_HISTORY', fallbackText: 'History' },
    { type: 'action', id: 'TOGGLE_KEYBOARD_HELP', fallbackText: 'KB Reference' },
    { type: 'action', id: 'OMNIBOX', fallbackText: 'Omnibox' },
    { type: 'action', id: 'LAUNCHER', fallbackText: 'Launcher' },
    { type: 'action', id: 'OPEN_SETTINGS_POPOVER', fallbackText: 'Settings' },
    { type: 'special', text: 'Enter', className: 'key key-enter' }
  ],
  [
    { type: 'special', text: 'Shift', className: 'key key-shift' },
    { type: 'action', id: 'PAGE_TOP', fallbackText: 'Scroll To Top' },
    { type: 'action', id: 'PAGE_BOTTOM', fallbackText: 'Scroll To Bottom' },
    { type: 'action', id: 'PAGE_UP_INSTANT', fallbackText: 'Page Up Fast' },
    { type: 'action', id: 'PAGE_DOWN_INSTANT', fallbackText: 'Page Down Fast' },
    { type: 'action', id: 'ACTIVATE_NEW_TAB', fallbackText: 'Click New Tab' },
    { type: 'key', text: 'N' },
    { type: 'key', text: 'M' },
    { type: 'key', text: ',' },
    { type: 'action', id: 'COLS_TOGGLE', fallbackText: 'Cols Toggle' },
    { type: 'key', text: '/' },
    { type: 'special', text: 'Shift', className: 'key key-shift' }
  ]
]);

// Left-handed UI layout: move the action clusters to the right-hand physical keys.
const KEYBOARD_UI_LAYOUT_LEFT = Object.freeze([
  [
    { type: 'special', text: 'Tab', className: 'key key-tab' },
    { type: 'key', text: 'Q' },
    { type: 'action', id: 'PREVIEW_LINK_POPOVER', fallbackText: 'Preview Link' }, // W
    { type: 'action', id: 'COPY_HOVERED_IMAGE', fallbackText: 'Copy Image' }, // E
    { type: 'action', id: 'RECTANGLE_HIGHLIGHT', fallbackText: 'Rectangle Select' }, // R
    { type: 'key', text: 'T' },
    { type: 'action', id: 'NEW_TAB', fallbackText: 'New Tab' }, // Y
    { type: 'action', id: 'FORWARD', fallbackText: 'Go Forward' }, // U
    { type: 'action', id: 'OPEN_POPOVER', fallbackText: 'Open Popover' }, // I
    { type: 'action', id: 'TAB_RIGHT', fallbackText: 'Tab Right' }, // O
    { type: 'action', id: 'TAB_LEFT', fallbackText: 'Tab Left' }, // P
    { type: 'key', text: '[' },
    { type: 'key', text: ']' },
    { type: 'action', id: 'DELETE', fallbackText: 'Delete Mode', className: 'key key-backspace' }
  ],
  [
    { type: 'special', text: 'Caps', className: 'key key-caps' },
    { type: 'action', id: 'LAUNCHER', fallbackText: 'Launcher' },
    // Utility keys on the left (to avoid colliding with right-hand cluster)
    { type: 'action', id: 'OMNIBOX', fallbackText: 'Omnibox' }, // S
    { type: 'action', id: 'TOGGLE_KEYBOARD_HELP', fallbackText: 'KB Reference' }, // D
    { type: 'action', id: 'TAB_HISTORY', fallbackText: 'History' }, // F
    { type: 'action', id: 'HIGHLIGHT', fallbackText: 'Text Select' }, // G
    { type: 'action', id: 'ACTIVATE_NEW_TAB_BACKGROUND', fallbackText: 'Click New Tab Background' }, // H
    { type: 'action', id: 'ACTIVATE', fallbackText: 'Click Element' }, // J
    { type: 'action', id: 'BACK', fallbackText: 'Go Back' }, // K
    { type: 'action', id: 'BACK2', fallbackText: 'Go Back' }, // L
    { type: 'action', id: 'CLOSE_TAB', fallbackText: 'Close Tab' }, // ;
    { type: 'action', id: 'OPEN_SETTINGS_POPOVER', fallbackText: 'Settings' }, // '
    { type: 'special', text: 'Enter', className: 'key key-enter' }
  ],
  [
    { type: 'special', text: 'Shift', className: 'key key-shift' },
    { type: 'key', text: 'Z' },
    { type: 'key', text: 'X' },
    { type: 'key', text: 'C' },
    { type: 'key', text: 'V' },
    { type: 'action', id: 'PAGE_BOTTOM', fallbackText: 'Scroll To Bottom' }, // B
    { type: 'action', id: 'ACTIVATE_NEW_TAB', fallbackText: 'Click New Tab' }, // N
    { type: 'action', id: 'PAGE_DOWN_INSTANT', fallbackText: 'Page Down Fast' }, // M
    { type: 'action', id: 'PAGE_UP_INSTANT', fallbackText: 'Page Up Fast' }, // ,
    { type: 'action', id: 'COLS_TOGGLE', fallbackText: 'Cols Toggle' }, // .
    { type: 'action', id: 'PAGE_TOP', fallbackText: 'Scroll To Top' }, // /
    { type: 'special', text: 'Shift', className: 'key key-shift' }
  ]
]);

/** @type {Record<BuiltinKeyboardLayoutId, BuiltinKeyboardLayout>} */
const BUILTIN_KEYBOARD_LAYOUTS = Object.freeze({
  'browsing-right': Object.freeze({
    id: 'browsing-right',
    label: 'Browsing: right-handed',
    description: 'Mouse: right hand. Keyboard shortcuts primarily on the left side.',
    assignments: ASSIGNMENTS_BROWSING_RIGHT,
    keyboardLayout: KEYBOARD_UI_LAYOUT_RIGHT
  }),
  'browsing-left': Object.freeze({
    id: 'browsing-left',
    label: 'Browsing: left-handed',
    description: 'Mouse: left hand. Keyboard shortcuts primarily on the right side.',
    assignments: ASSIGNMENTS_BROWSING_LEFT,
    keyboardLayout: KEYBOARD_UI_LAYOUT_LEFT
  })
});

/**
 * @param {BuiltinKeyboardLayoutId} layoutId
 * @returns {any[]}
 */
function getKeyboardUiLayoutForLayout(layoutId) {
  const id = normalizeKeyboardLayoutId(layoutId);
  return BUILTIN_KEYBOARD_LAYOUTS[id]?.keyboardLayout || BUILTIN_KEYBOARD_LAYOUTS[DEFAULT_KEYBOARD_LAYOUT_ID].keyboardLayout;
}
















  // Module: src/config/constants.js
/**
 * Application constants and configuration
 */

// Legacy export used across the codebase and by `extension/build.js`.
// This represents the *default* layout. Runtime code should prefer computing
// keybindings from the active settings/layout when available.
const KEYBINDINGS = buildKeybindingsForLayout(DEFAULT_KEYBOARD_LAYOUT_ID);

const SELECTORS = {
  CLICKABLE: 'a[href], button, input, select, textarea',
  // Prefer IDL-backed checks via isTypingContext() when possible. These selectors
  // are best-effort for matches()/querySelector (note: bare <input> has no type attr).
  TEXT_INPUTS: 'input:not([type]), input[type="text"], input[type="search"], input[type="url"], input[type="email"], input[type="tel"], input[type="password"], input[type="number"], input[type="date"], input[type="datetime-local"], input[type="month"], input[type="week"], input[type="time"], textarea',
  FOCUSABLE_TEXT: 'input:not([type]), input[type="text"], input[type="search"], input[type="url"], input[type="email"], input[type="tel"], input[type="password"], input[type="number"], input[type="date"], input[type="datetime-local"], input[type="month"], input[type="week"], input[type="time"], textarea, [contenteditable="true"], [contenteditable=""], [contenteditable="plaintext-only"]'
};

const ARIA_ROLES = {
  CLICKABLE: ['link', 'button']
};

/**
 * Semantic categories for interactive hover/activation targets.
 * Hover UI, F-key feedback, and activation should branch on category — not treat
 * every clickable the same as a hyperlink.
 *
 * Priority when classifying (most specific first):
 *   text > slider > button > link > media > control > generic
 */
const CLICKABLE_CATEGORY = {
  /** Nothing interactive under the pointer */
  NONE: 'none',
  /** Navigation: <a href>, role=link, data-kp-url rows */
  LINK: 'link',
  /** Discrete actions: <button>, role=button */
  BUTTON: 'button',
  /** Typing surfaces: text inputs, textarea, contenteditable */
  TEXT: 'text',
  /** Video/audio surface (thumbnail or player body) */
  MEDIA: 'media',
  /** Continuous value: range, role=slider, media scrub tracks */
  SLIDER: 'slider',
  /** Other form/ARIA controls: checkbox, radio, select, tab, switch, … */
  CONTROL: 'control',
  /** Non-semantic interactive (cursor:pointer, onclick, tracked click listener) */
  GENERIC: 'generic'
};

const CSS_CLASSES = {
  CURSOR_HIDDEN: 'kpv2-cursor-hidden',
  FOCUS: 'kpv2-focus',
  DELETE: 'kpv2-delete',
  HIGHLIGHT: 'kpv2-highlight',
  HIDDEN: 'kpv2-hidden',
  RIPPLE: 'kpv2-ripple',
  FOCUS_OVERLAY: 'kpv2-focus-overlay',
  /** Temporary outline that scales up on F-click activation */
  FOCUS_PULSE: 'kpv2-focus-pulse',
  /** Temporary outline with a marquee/chaser light traveling the perimeter on F-click */
  FOCUS_MARQUEE: 'kpv2-focus-marquee',
  /** Temporary hard flash (strobe) on F-click activation */
  FOCUS_FLASH: 'kpv2-focus-flash',
  /** Temporary dashed border whose dashes chase around the perimeter on F-click */
  FOCUS_DASH: 'kpv2-focus-dash',
  /** Temporary frame that scales (pop then shrink) when copying an image under cursor */
  IMAGE_COPY_PULSE: 'kpv2-image-copy-pulse',
  DELETE_OVERLAY: 'kpv2-delete-overlay',
  /**
   * Shared inspector-mode hover chrome (Delete, Cols, future pick tools).
   * Kind-specific colors applied via CSS vars / inline styles.
   */
  INSPECTOR: 'kpv2-inspector',
  INSPECTOR_OVERLAY: 'kpv2-inspector-overlay',
  /** Top-right companion instruction while inspector pick is active (like highlight mode) */
  INSPECTOR_MODE_INDICATOR: 'kpv2-inspector-mode-indicator',
  /** @deprecated prefer INSPECTOR + kind; kept for style/compat during transition */
  COLS: 'kpv2-cols',
  COLS_OVERLAY: 'kpv2-cols-overlay',
  /** Applied multicol layout on the chosen target */
  COLS_ACTIVE: 'kpv2-cols-active',
  /** Page-mode markers on html/body while whole-page columns are active */
  COLS_PAGE: 'kpv2-cols-page',
  /** Widget shell wrapping a columnized target (outline + slip chrome) */
  COLS_SHELL: 'kpv2-cols-shell',
  /** Content region inside the shell that holds the target */
  COLS_BODY: 'kpv2-cols-body',
  /** Placeholder left in flow when shell is promoted to a popover */
  COLS_PLACEHOLDER: 'kpv2-cols-placeholder',
  /** Slip-edit chrome (NLE-style content window scrubber) */
  COLS_SLIP_BAR: 'kpv2-cols-slip-bar',
  COLS_SLIP_TRACK: 'kpv2-cols-slip-track',
  COLS_SLIP_KNOB: 'kpv2-cols-slip-knob',
  COLS_SLIP_LABEL: 'kpv2-cols-slip-label',
  /** Slip-bar action: promote columns widget to floating popover */
  COLS_EXPAND_BTN: 'kpv2-cols-expand-btn',
  /** Slip-bar action: clear columns / restore element */
  COLS_CLOSE_BTN: 'kpv2-cols-close-btn',
  HIGHLIGHT_OVERLAY: 'kpv2-highlight-overlay',
  HIGHLIGHT_SELECTION: 'kpv2-highlight-selection',
  TEXT_FIELD_GLOW: 'kpv2-text-field-glow',
  VIEWPORT_MODAL_FRAME: 'kpv2-viewport-modal-frame',
  ACTIVE_TEXT_INPUT_FRAME: 'kpv2-active-text-input-frame',
  ESC_EXIT_LABEL: 'kpv2-esc-exit-label',
  TEXT_FOCUS_INPUT: 'kpv2-text-focus-input',
  TEXT_FOCUS_INPUT_PARENT: 'kpv2-text-focus-input-parent',
  /** Modifier: focused text field uses left-edge 10px pulsating bar (default style). */
  TEXT_FOCUS_LEFT_EDGE: 'kpv2-text-focus-left-edge',
  TEXT_HOVER_INPUT: 'kpv2-text-hover-input',
  TEXT_HOVER_INPUT_PARENT: 'kpv2-text-hover-input-parent',

  /** Canvas-based focus/delete overlay host (OverlayManager) */
  CANVAS_OVERLAY: 'kpv2-canvas-overlay',
  /** CSS custom-properties focus/delete overlay host (OverlayManager) */
  CSS_PROPS_OVERLAY: 'kpv2-css-props-overlay',
  /** Focused text-field overlay (OverlayManager) */
  FOCUSED_TEXT_OVERLAY: 'kpv2-focused-text-overlay',

  // Omnibox overlay UI
  OMNIBOX_BACKDROP: 'kpv2-omnibox-backdrop',
  OMNIBOX_PANEL: 'kpv2-omnibox-panel',
  OMNIBOX_INPUT: 'kpv2-omnibox-input',
  OMNIBOX_SUGGESTIONS: 'kpv2-omnibox-suggestions',
  OMNIBOX_SUGGESTION: 'kpv2-omnibox-suggestion',
  OMNIBOX_EMPTY: 'kpv2-omnibox-empty',

  // PopupManager (shared backdrop for modals/popups that should blur the page)
  POPUP_BACKDROP: 'kpv2-popup-backdrop'
};

const ELEMENT_IDS = {
  CURSOR: 'kpv2-cursor',
  STYLE: 'kpv2-style'
};

const Z_INDEX = {
  // Utility layers (occasionally used for measurement elements)
  PAGE_BEHIND: -1,
  DEFAULT: 1,

  // Keep all KeyPilot UI comfortably above typical site z-index values.
  // Note: Many browsers effectively clamp very large z-index values; using a
  // high-but-safe base avoids accidental collisions and keeps ordering clear.
  _BASE: 2147483000,

  // Low-ish KeyPilot overlays
  VIEWPORT_MODAL_FRAME: 2147483010,
  HIGHLIGHT_SELECTION: 2147483015,

  // PopupManager layers (kept BELOW click overlays so the green click rectangle can sit above popups)
  POPUP_BACKDROP: 2147483009,
  POPUP_PANEL_BASE: 2147483012,
  POPUP_PANEL_MAX: 2147483017,

  // Focus/delete/highlight overlays
  OVERLAYS_BELOW_2: 2147483018,
  OVERLAYS_BELOW: 2147483019,
  OVERLAYS: 2147483020,
  OVERLAYS_ABOVE: 2147483021,

  // macOS-style control strip (upper-left; stays at top; below walkthrough in z-order)
  CONTROL_STRIP: 2147483025,

  // Onboarding walkthrough (top-left, stacked below the control strip on screen).
  // z-index above the strip so if they ever overlap the panel wins; still below
  // green hover/click overlays and floating keyboard help.
  ONBOARDING_PANEL: 2147483026,

  // Cols Toggle slip-edit bar (bottom of viewport; below keyboard help / cursor)
  COLS_SLIP_BAR: 2147483030,

  // Iframe-based popover modal (Open Popover)
  POPOVER_IFRAME_MODAL: 2147483035,

  // Notifications / message overlays
  MESSAGE_BOX: 2147483040,
  DEBUG_HUD: 2147483041,
  NOTIFICATION: 2147483040,

  // Omnibox overlay (should sit above most UI, but below keyboard help + cursor)
  OMNIBOX: 2147483042,

  // Floating keyboard reference + key-click tooltip (above page UI, below cursor)
  FLOATING_KEYBOARD_HELP: 2147483045,
  KEYBINDINGS_POPOVER: 2147483046,

  // Cursor should remain above everything else.
  CURSOR: 2147483050
};

/**
 * Scroll distances / behavior for page / popover keyboard scrolling (defaults).
 * Runtime values can be overridden via Settings (`kp_settings_v1.scroll`).
 * Used by key handlers and popover iframe bridges.
 *
 * C / V (half-page) use cursor-aware scrolling (`scroll-at-point.js`):
 * nested overflow under the pointer first (vertical, or horizontal when that
 * container scrolls on X), then the document. Iframes are forwarded via the
 * light frame-click-agent (KP_FRAME_SCROLL).
 */
const SCROLL = Object.freeze({
  /** Z / X (and popover equivalents): large page step */
  PAGE_PX: 800,
  /** C / V: smaller step (default = prior 400px × 1.25) */
  HALF_PAGE_PX: 500,
  /** Default CSS scroll-behavior for keyboard scrolling */
  BEHAVIOR: 'smooth'
});

const MODES = {
  NONE: 'none',
  /**
   * Shared element-pick inspector (DOM inspector style).
   * Concrete tool is state.inspectorKind (see INSPECTOR_KIND).
   * Used by Delete Mode, Cols Toggle, and future pick tools.
   */
  INSPECTOR: 'inspector',
  /**
   * @deprecated Use MODES.INSPECTOR + INSPECTOR_KIND.DELETE.
   * Kept so older status strings / comparisons still resolve if needed.
   */
  DELETE: 'delete',
  /**
   * @deprecated Use MODES.INSPECTOR + INSPECTOR_KIND.COLS.
   */
  COLS: 'cols',
  TEXT_FOCUS: 'text_focus',
  HIGHLIGHT: 'highlight',
  POPOVER: 'popover',
  OMNIBOX: 'omnibox'
};

/**
 * Inspector tool kinds while mode === MODES.INSPECTOR.
 * Register visuals/behavior in modules/inspector-mode.js.
 */
const INSPECTOR_KIND = Object.freeze({
  DELETE: 'delete',
  COLS: 'cols'
});

// Cursor behavior mode:
// - NO_CUSTOM_CURSORS: KeyPilot does not override the page cursor at all.
// - CUSTOM_CURSORS: KeyPilot applies its cursor styling/overrides (current legacy behavior).
const CURSOR_MODE = Object.freeze({
  NO_CUSTOM_CURSORS: 'NO-CUSTOM-CURSORS',
  CUSTOM_CURSORS: 'CUSTOM-CURSORS'
});

/**
 * System UI font for KeyPilot chrome injected into host pages.
 * Pin this on popovers/titlebars so site body fonts (e.g. freight-text-pro) cannot leak in.
 * Single declaration — the content bundle is one IIFE scope (no per-module consts of the same name).
 */
const KP_UI_FONT =
  "system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif";

const COLORS = {
  // Primary cursor colors
  FOCUS_GREEN: 'rgba(0,180,0,0.95)',
  FOCUS_GREEN_BRIGHT: 'rgba(0,128,0,0.95)',
  DELETE_RED: 'rgba(220,0,0,0.95)',
  /** Cols Toggle accent (purple, distinct from delete red / highlight blue) */
  COLS_PURPLE: 'rgba(156,39,176,0.95)',
  COLS_PURPLE_BRIGHT: 'rgba(186,104,200,0.95)',
  HIGHLIGHT_BLUE: 'rgba(0,120,255,0.95)',
  ORANGE: '#ff8c00',
  // Focus overlay (alternate) colors (used to visually distinguish DOM-hover targeting mode)
  FOCUS_BLUE: 'rgba(33,150,243,0.95)',

  // Text and background colors
  TEXT_WHITE_PRIMARY: 'rgba(255,255,255,0.95)',
  TEXT_WHITE_SECONDARY: 'rgba(255,255,255,0.8)',
  TEXT_GREEN_BRIGHT: '#6ced2b',

  // Background colors
  MESSAGE_BG_BROWN: '#ad6007',
  MESSAGE_BG_GREEN: '#10911b',

  // Border and shadow colors
  ORANGE_BORDER: 'rgba(255,140,0,0.4)',
  ORANGE_SHADOW: 'rgba(255,140,0,0.45)',
  ORANGE_SHADOW_DARK: 'rgba(255,140,0,0.8)',
  ORANGE_SHADOW_LIGHT: 'rgba(255,140,0,0.3)',
  GREEN_SHADOW: 'rgba(0,180,0,0.45)',
  GREEN_SHADOW_BRIGHT: 'rgba(0,180,0,0.5)',
  BLUE_SHADOW: 'rgba(33,150,243,0.35)',
  BLUE_SHADOW_BRIGHT: 'rgba(33,150,243,0.45)',
  DELETE_SHADOW: 'rgba(220,0,0,0.35)',
  DELETE_SHADOW_BRIGHT: 'rgba(220,0,0,0.45)',
  COLS_SHADOW: 'rgba(156,39,176,0.35)',
  COLS_SHADOW_BRIGHT: 'rgba(156,39,176,0.5)',
  HIGHLIGHT_SHADOW: 'rgba(0,120,255,0.35)',
  HIGHLIGHT_SHADOW_BRIGHT: 'rgba(0,120,255,0.45)',
  BLACK_SHADOW: 'rgba(40, 40, 40, 0.7)',

  // Ripple effect colors
  RIPPLE_GREEN: 'rgba(0,200,0,0.35)',
  RIPPLE_GREEN_MID: 'rgba(0,200,0,0.22)',
  RIPPLE_GREEN_TRANSPARENT: 'rgba(0,200,0,0)',

  // Flash animation colors
  FLASH_GREEN: 'rgba(0,255,0,1)',
  FLASH_GREEN_SHADOW: 'rgba(0,255,0,0.8)',
  FLASH_GREEN_GLOW: 'rgba(0,255,0,0.9)',

  // Image-copy pulse (distinct from green F-click pulse)
  IMAGE_COPY_FRAME: 'rgba(33,150,243,0.95)',
  IMAGE_COPY_FRAME_SHADOW: 'rgba(33,150,243,0.55)',
  IMAGE_COPY_FRAME_GLOW: 'rgba(100,180,255,0.75)',
  IMAGE_COPY_FILL: 'rgba(33,150,243,0.14)',
  IMAGE_COPY_FLASH: 'rgba(255,255,255,0.45)',

  // Notification colors
  NOTIFICATION_SUCCESS: '#4CAF50',
  NOTIFICATION_ERROR: '#f44336',
  NOTIFICATION_WARNING: '#ff9800',
  NOTIFICATION_INFO: '#2196F3',
  NOTIFICATION_SHADOW: 'rgba(0, 0, 0, 0.15)',

  // Text field glow
  TEXT_FIELD_GLOW: 'rgba(255,165,0,0.8)',

  // Highlight selection colors
  HIGHLIGHT_SELECTION_BG: 'rgba(0,120,255,0.3)',
  HIGHLIGHT_SELECTION_BORDER: 'rgba(0,120,255,0.6)',

  // New colors for ESC exit labels
  ORANGE_BG: 'rgba(255, 165, 0, 0.9)',
  ORANGE_TEXT: '#fff',
  ORANGE_BORDER: '#d35400',
  FOCUS_GREEN_BG: 'rgba(46, 204, 113, 0.9)',
  FOCUS_GREEN_BG_T2: 'rgba(46, 204, 113, 0.4)',
  FOCUS_GREEN_TEXT: '#fff',
  FOCUS_GREEN: '#27ae60',
  FOCUS_BLUE_BG_T2: 'rgba(33,150,243,0.25)'
};

// Legacy scale-based cursor storage (keypilot_cursor_size / keypilot_cursor_visible)
// was removed. Cursor appearance lives in kp_settings_v1 via settings-manager
// (clickMode.cursor + cursorMode).

const RECTANGLE_SELECTION = {
  // Visual rectangle settings
  MIN_WIDTH: 3,           // Minimum rectangle width to show (pixels)
  MIN_HEIGHT: 3,          // Minimum rectangle height to show (pixels)
  MIN_DRAG_DISTANCE: 5,   // Minimum drag distance to start selection (pixels)

  // Visual feedback settings
  SHOW_IMMEDIATE_FEEDBACK: true,        // Show rectangle for any movement
  HIDE_ZERO_SIZE: false,                // Don't hide zero-size rectangles

  // Performance limits (should match browser capabilities)
  MAX_AREA_PIXELS: 50000000,           // 50M pixels (e.g., 10000x5000) - very generous limit
  MAX_TEXT_NODES: 10000,               // Maximum text nodes to process - matches browser selection limits
  ENABLE_AREA_LIMIT: false,            // Disable area limiting by default - browsers handle large selections fine
  ENABLE_NODE_LIMIT: true,             // Keep node limit as safety measure for DOM traversal performance

  // Performance notes:
  // - Area limits are disabled by default because browsers can handle enormous text selections
  // - Node limits remain enabled to prevent DOM traversal performance issues on complex pages
  // - These limits only apply to rectangle selection, not manual browser selection
  // - The clipboard is typically the real limiting factor, not the selection itself
};

const EDGE_ONLY_SELECTION = {
  // Smart Targeting Options
  SMART_TARGETING: {
    ENABLED: true,                     // Enable smart element targeting
    TEXT_ELEMENT_TAGS: [               // HTML tags that commonly contain text
      'p', 'span', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
      'li', 'td', 'th', 'a', 'strong', 'em', 'b', 'i', 'u',
      'blockquote', 'pre', 'code', 'label', 'legend', 'article',
      'section', 'header', 'footer', 'main', 'aside', 'nav'
    ],
    SKIP_ELEMENT_TAGS: [               // HTML tags to skip (non-text elements)
      'img', 'video', 'audio', 'canvas', 'svg', 'iframe',
      'script', 'style', 'noscript', 'object', 'embed'
    ],
    MIN_TEXT_LENGTH: 1,                // Minimum text content length to consider
    CHECK_COMPUTED_STYLE: true,        // Check if element is visible via computed style
    INCLUDE_ARIA_LABELS: true,         // Include elements with aria-label/aria-labelledby
    MAX_ELEMENTS_TO_OBSERVE: 5000,     // Maximum elements to observe simultaneously
  },

  // Character Detection Settings
  CHARACTER_DETECTION: {
    ENABLED: true,                     // Enable edge-level character detection
    USE_RANGE_API: true,               // Use Range API for precise character positioning
    CACHE_CHARACTER_POSITIONS: true,   // Cache character positions using WeakMap
    CHARACTER_CACHE_SIZE: 1000,        // Maximum characters to cache per element
    BOUNDARY_DETECTION_PRECISION: 1,   // Pixel precision for boundary detection
    BATCH_CHARACTER_PROCESSING: true,  // Process characters in batches
    CHARACTER_BATCH_SIZE: 50,          // Number of characters to process per batch
    MAX_CHARACTERS_PER_ELEMENT: 10000, // Maximum characters to process per element
  },

  // Cache Configuration
  CACHE_CONFIGURATION: {
    ELEMENT_CACHE_SIZE: 1000,          // Maximum number of elements to cache
    CHARACTER_CACHE_SIZE: 5000,        // Maximum number of character positions to cache
    CACHE_CLEANUP_THRESHOLD: 800,      // Start cleanup when cache reaches this size
    CACHE_CLEANUP_BATCH_SIZE: 200,     // Number of entries to remove during cleanup
    ENABLE_PREDICTIVE_CACHING: true,   // Pre-cache elements likely to intersect
    PREDICTIVE_CACHE_DISTANCE: 100,    // Distance in pixels to pre-cache elements
    CACHE_TTL_MS: 30000,               // Time-to-live for cached entries (30 seconds)
    ENABLE_CACHE_COMPRESSION: false,   // Enable cache compression (experimental)
  },

  // Performance Monitoring Configuration
  PERFORMANCE_MONITORING: {
    ENABLED: false,                     // Enable performance monitoring
    MONITORING_INTERVAL: 1000,         // How often to check performance (ms)
    COLLECT_DETAILED_METRICS: true,    // Collect detailed performance metrics
    TRACK_CACHE_EFFICIENCY: true,      // Track cache hit/miss ratios
    TRACK_PROCESSING_TIME: true,       // Track processing time per operation
    TRACK_MEMORY_USAGE: true,          // Track memory usage
    PERFORMANCE_LOG_INTERVAL: 5000,    // How often to log performance stats (ms)
    ENABLE_PERFORMANCE_ALERTS: true,   // Enable performance degradation alerts
  },

  // Fallback Configuration
  FALLBACK_CONFIGURATION: {
    ENABLED: true,                     // Enable automatic fallback
    FALLBACK_THRESHOLD_MS: 15,         // Fall back to spatial if processing exceeds this
    MAX_CONSECUTIVE_FAILURES: 3,       // Max failures before fallback
    FALLBACK_RECOVERY_ATTEMPTS: 5,     // Attempts to recover from fallback
    FALLBACK_RECOVERY_DELAY: 2000,     // Delay between recovery attempts (ms)
    ENABLE_GRACEFUL_DEGRADATION: true, // Enable graceful performance degradation
    FALLBACK_TO_SPATIAL_METHOD: true,  // Fallback to spatial intersection method
  },

  // Performance Thresholds
  MAX_PROCESSING_TIME_MS: 10,          // Maximum time for edge processing (ms)
  MAX_ELEMENTS_PER_UPDATE: 50,         // Maximum elements to process per update
  FALLBACK_THRESHOLD_MS: 15,           // Fall back to spatial if processing exceeds this
  CACHE_HIT_RATIO_THRESHOLD: 0.7,     // Minimum acceptable cache hit ratio

  // Memory Management
  MAX_MEMORY_USAGE_MB: 50,             // Maximum memory usage for edge-only processing
  MEMORY_CHECK_INTERVAL: 5000,        // How often to check memory usage (ms)
  ENABLE_MEMORY_MONITORING: true,     // Monitor memory usage and cleanup
  GARBAGE_COLLECTION_THRESHOLD: 0.8,  // Trigger cleanup at 80% of memory limit

  // Processing Options
  INTERSECTION_OBSERVER_THRESHOLDS: [0, 0.1, 0.5, 1.0], // Multiple thresholds for granular updates
  BATCH_PROCESSING_SIZE: 10,           // Process elements in batches of this size
  ENABLE_ADAPTIVE_PROCESSING: true,    // Adjust processing based on page complexity
  FRAME_RATE_TARGET: 60,               // Target frame rate during drag operations

  // Adaptive Processing Settings (Task 2.1)
  PAGE_COMPLEXITY_ANALYSIS: {
    ENABLE_COMPLEXITY_ANALYSIS: false,   // Enable page complexity analysis
    ELEMENT_COUNT_THRESHOLD_LOW: 500,   // Low complexity threshold
    ELEMENT_COUNT_THRESHOLD_HIGH: 2000, // High complexity threshold
    DOM_DEPTH_THRESHOLD_LOW: 10,        // Low DOM depth threshold
    DOM_DEPTH_THRESHOLD_HIGH: 20,       // High DOM depth threshold
    TEXT_NODE_DENSITY_THRESHOLD: 0.3,   // Text node density threshold
    COMPLEXITY_CHECK_INTERVAL: 10000,   // How often to analyze page complexity (ms)
  },

  FRAME_RATE_PROCESSING: {
    TARGET_FPS: 60,                     // Target frame rate during drag operations
    FRAME_TIME_BUDGET_MS: 16.67,        // Time budget per frame (1000ms / 60fps)
    PROCESSING_TIME_BUDGET_MS: 8,       // Max processing time per frame
    FRAME_RATE_MONITORING_WINDOW: 10,   // Number of frames to monitor for rate calculation
    MIN_ACCEPTABLE_FPS: 30,             // Minimum acceptable frame rate
    FRAME_RATE_ADJUSTMENT_FACTOR: 0.8,  // Reduce processing when frame rate drops
  },

  BATCH_PROCESSING: {
    ENABLE_BATCH_PROCESSING: true,      // Enable batch processing optimization
    DEFAULT_BATCH_SIZE: 5,              // Default batch size for processing
    MAX_BATCH_SIZE: 20,                 // Maximum batch size
    MIN_BATCH_SIZE: 1,                  // Minimum batch size
    BATCH_TIMEOUT_MS: 4,                // Maximum time to wait for batch completion
    ADAPTIVE_BATCH_SIZING: true,        // Adjust batch size based on performance
  },

  QUALITY_ADJUSTMENTS: {
    ENABLE_QUALITY_ADJUSTMENTS: true,   // Enable quality adjustments based on available time
    HIGH_QUALITY_TIME_THRESHOLD: 5,     // Time threshold for high quality processing (ms)
    MEDIUM_QUALITY_TIME_THRESHOLD: 10,  // Time threshold for medium quality processing (ms)
    LOW_QUALITY_PROCESSING_LIMIT: 20,   // Maximum elements to process in low quality mode
    QUALITY_ADJUSTMENT_HYSTERESIS: 2,   // Frames to wait before quality adjustment
  },

  // Predictive Caching Settings (Task 2.2)
  PREDICTIVE_CACHING: {
    ENABLE_PREDICTIVE_CACHING: true,    // Enable predictive caching strategies
    ENABLE_USER_BEHAVIOR_ANALYSIS: true, // Analyze user behavior patterns
    ENABLE_VIEWPORT_BASED_CACHING: true, // Cache based on viewport position
    ENABLE_SCROLL_PREDICTION: true,     // Predict scroll direction and cache ahead

    // User behavior analysis
    BEHAVIOR_PATTERN_WINDOW: 20,        // Number of recent interactions to analyze
    INTERACTION_TIMEOUT_MS: 2000,       // Time between interactions to consider separate
    MIN_PATTERN_CONFIDENCE: 0.6,        // Minimum confidence to act on patterns
    PATTERN_ANALYSIS_INTERVAL: 5000,    // How often to analyze patterns (ms)

    // Viewport-based caching
    VIEWPORT_CACHE_MARGIN: 200,         // Pixels beyond viewport to cache
    VIEWPORT_CACHE_SECTORS: 9,          // Divide viewport into sectors for caching
    CACHE_WARMING_DISTANCE: 300,        // Distance ahead to warm cache (pixels)
    VIEWPORT_UPDATE_THROTTLE: 100,      // Throttle viewport updates (ms)

    // Scroll prediction
    SCROLL_VELOCITY_SAMPLES: 5,         // Number of scroll samples for velocity calculation
    SCROLL_PREDICTION_DISTANCE: 500,    // Distance to predict ahead (pixels)
    MIN_SCROLL_VELOCITY: 50,            // Minimum velocity to trigger prediction (px/s)
    SCROLL_DIRECTION_THRESHOLD: 10,     // Pixels to determine scroll direction

    // Cache preloading
    PRELOAD_BATCH_SIZE: 10,             // Elements to preload per batch
    PRELOAD_THROTTLE_MS: 50,            // Throttle between preload batches
    MAX_PRELOAD_ELEMENTS: 100,          // Maximum elements to preload
    PRELOAD_PRIORITY_THRESHOLD: 0.7,    // Confidence threshold for high priority preload
  },

  // Debug and Monitoring
  ENABLE_PERFORMANCE_LOGGING: false,    // Log detailed performance metrics
  ENABLE_CACHE_METRICS: false,          // Track cache hit/miss ratios
  ENABLE_MEMORY_LOGGING: false,        // Log memory usage (can be verbose)
  PERFORMANCE_LOG_INTERVAL: 5000,     // How often to log performance stats (ms)
};

// Performance monitoring removed

const FEATURE_FLAGS = {
  // Rectangle Selection Method
  // Prefer caretRangeFromPoint (browser-native drag semantics). Edge-only IntersectionObserver
  // is off by default: a non-ancestor fixed root never reports intersections, so selection
  // stayed empty and completeSelection would not exit highlight mode.
  USE_INTELLIGENT_RECTANGLE_SELECTION: true, // Use browser-native caret selection instead of spatial intersection
  USE_NATIVE_SELECTION_API: true, // Use document.caretRangeFromPoint for efficient selection

  // Edge-Only Processing Control (experimental / heavy; off by default — see USE_EDGE_ONLY_SELECTION)
  ENABLE_EDGE_ONLY_PROCESSING: false,  // Use edge-only intersection processing
  EDGE_ONLY_FALLBACK_ENABLED: true,    // Allow fallback to spatial method if edge-only fails
  FORCE_EDGE_ONLY_MODE: false,         // Force edge-only processing even if performance degrades
  ENABLE_EDGE_ONLY_CACHE: true,        // Enable text node caching for edge-only processing

  // Enhanced RectangleIntersectionObserver Integration (Task 2)
  ENABLE_ENHANCED_RECTANGLE_OBSERVER: false, // Master flag for enhanced integration (Task 2.1, 2.2, 2.3)

  // Edge-Only Processing Feature Flags (Task 1.1)
  USE_EDGE_ONLY_SELECTION: false,        // Off: broken root/target relationship; use caret/spatial instead
  ENABLE_SMART_TARGETING: true,          // Enable smart element targeting
  ENABLE_CHARACTER_DETECTION: true,      // Enable edge-level character detection
  ENABLE_SELECTION_CACHING: true,        // Enable text node caching
  ENABLE_AUTOMATIC_FALLBACK: true,       // Auto-fallback on performance issues
  ENABLE_EDGE_BATCH_PROCESSING: true,    // Batch intersection updates
  ENABLE_PREDICTIVE_CACHING: false,      // Predictive caching off with edge-only stack
  DETAILED_EDGE_LOGGING: false,          // Detailed debug logging for edge processing (off for ship)
  EDGE_CACHE_SIZE_MANAGEMENT: true,      // Enable cache size management
  EDGE_ADAPTIVE_PROCESSING: true,        // Enable adaptive processing
  ENABLE_TEXT_ELEMENT_FILTER: true,      // Enable TextElementFilter class
  ENABLE_EDGE_CHARACTER_DETECTOR: true,  // Enable EdgeCharacterDetector class

  // Selection behavior options
  RECTANGLE_SELECTION_FALLBACK_TO_SPATIAL: true, // Fall back to spatial method if intelligent method fails
  RECTANGLE_SELECTION_SCAN_STEP: 8, // Pixel step size for boundary scanning (performance vs accuracy)
  RECTANGLE_SELECTION_MAX_SCAN_TIME: 50, // Maximum time in ms to spend scanning for boundaries

  // Clipboard options
  ENABLE_RICH_TEXT_CLIPBOARD: true, // Copy both plain text and HTML formatting to clipboard
  RICH_TEXT_FALLBACK_TO_PLAIN: true, // Fall back to plain text if rich text copying fails

  // UI feature flags
  SHOW_WINDOW_OUTLINE: false, // Show window outline during text mode

  // Hover/click targeting strategy (product decision: DOM-hover only)
  // Permanent primary path: attach DOM hover listeners and drive `state.focusEl` from
  // browser-native hover targeting. RBush spatial indexing is retired (vendor removed;
  // residual index code is no-op / isolated). Activation (F) still falls back to
  // elementFromPoint if nothing is hovered.
  ENABLE_DOM_HOVER_LISTENERS: true,

  // ---- Focus-ring paint experiments (DOM-hover element styling) ----
  //
  // Tentative purpose of ENABLE_FOCUS_CLIP_INSET:
  //   When an ancestor overflow/content-visibility/contain box is tight enough to
  //   clip a positive outline-offset ring, switch to inset outline (negative
  //   offset) so the ring paints inside the target. Does NOT mutate page overflow
  //   (that broke IMDb carousels). Keep as a flag so we can A/B or disable if
  //   inset misbehaves on some skins.
  //
  // Tentative purpose of ENABLE_FOCUS_TIGHT_WRAPPER_PROMOTION:
  //   When a clip ancestor is nearly the same size as the hover target, paint the
  //   ring on that ancestor instead of the target (e.g. some content-visibility
  //   row wrappers). Default OFF: on IMDb, this promotes off <a.ipc-lockup-overlay>
  //   onto parent .ipc-poster, so the real clickable never shows data-kp-focus and
  //   the ring can look "missing" on the overlay link the user is inspecting.
  ENABLE_FOCUS_CLIP_INSET: true,
  ENABLE_FOCUS_TIGHT_WRAPPER_PROMOTION: false,

  // Debug and development flags
  DEBUG_RECTANGLE_SELECTION: false, // Enable detailed logging for rectangle selection
  DEBUG_EDGE_ONLY_PROCESSING: false, // Enable detailed logging for edge-only processing
  SHOW_SELECTION_METHOD_IN_UI: false, // Show which selection method was used in notifications
  DEBUG_RECTANGLE_HUD: false, // Show live rectangle debugging HUD with coordinates and calls
  ENABLE_DEBUG_PANEL: false // Enable upper-right debug panel showing performance metrics
};


  // Module: ../node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js
var querySelectorShadowDom=function(e){"use strict";function t(e,t,u,s){void 0===s&&(s=null),e=function(e){function t(){r&&(u.length>0&&/^[~+>]$/.test(u[u.length-1])&&u.push(" "),u.push(r))}var n,r,l,o,u=[],s=[0],a=0,h=/(?:[^\\]|(?:^|[^\\])(?:\\\\)+)$/,i=/^\s+$/,c=[/\s+|\/\*|["'>~+[(]/g,/\s+|\/\*|["'[\]()]/g,/\s+|\/\*|["'[\]()]/g,null,/\*\//g];for(e=e.trim();;){if(r="",(l=c[s[s.length-1]]).lastIndex=a,!(n=l.exec(e))){r=e.substr(a),t();break}if((o=a)<(a=l.lastIndex)-n[0].length&&(r=e.substring(o,a-n[0].length)),s[s.length-1]<3){if(t(),"["===n[0])s.push(1);else if("("===n[0])s.push(2);else if(/^["']$/.test(n[0]))s.push(3),c[3]=new RegExp(n[0],"g");else if("/*"===n[0])s.push(4);else if(/^[\])]$/.test(n[0])&&s.length>0)s.pop();else if(/^(?:\s+|[~+>])$/.test(n[0])&&(u.length>0&&!i.test(u[u.length-1])&&0===s[s.length-1]&&u.push(" "),1===s[s.length-1]&&5===u.length&&"="===u[2].charAt(u[2].length-1)&&(u[4]=" "+u[4]),i.test(n[0])))continue;u.push(n[0])}else u[u.length-1]+=r,h.test(u[u.length-1])&&(4===s[s.length-1]&&(u.length<2||i.test(u[u.length-2])?u.pop():u[u.length-1]=" ",n[0]=""),s.pop()),u[u.length-1]+=n[0]}return u.join("").trim()}(e);var a=u.querySelector(e);return document.head.createShadowRoot||document.head.attachShadow?!t&&a?a:n(e,",").reduce((function(e,a){if(!t&&e)return e;var h=n(a.replace(/^\s+/g,"").replace(/\s*([>+~]+)\s*/g,"$1")," ").filter((function(e){return!!e})).map((function(e){return n(e,">")})),i=h.length-1,c=o(h[i][h[i].length-1],u,s),f=function(e,t,n){return function(o){for(var u=t,s=o,a=!1;s&&!r(s);){var h=!0;if(1===e[u].length)h=s.matches(e[u]);else{var i=s,c=[].concat(e[u]).reverse(),f=Array.isArray(c),g=0;for(c=f?c:c[Symbol.iterator]();;){var d;if(f){if(g>=c.length)break;d=c[g++]}else{if((g=c.next()).done)break;d=g.value}var p=d;if(!i||!i.matches(p)){h=!1;break}i=l(i,n)}}if(h&&0===u){a=!0;break}h&&u--,s=l(s,n)}return a}}(h,i,u);return t?e=e.concat(c.filter(f)):(e=c.find(f))||null}),t?[]:null):t?u.querySelectorAll(e):a}function n(e,t){return e.match(/\\?.|^$/g).reduce((function(e,n){return'"'!==n||e.sQuote?"'"!==n||e.quote?e.quote||e.sQuote||n!==t?e.a[e.a.length-1]+=n:e.a.push(""):(e.sQuote^=1,e.a[e.a.length-1]+=n):(e.quote^=1,e.a[e.a.length-1]+=n),e}),{a:[""]}).a}function r(e){return e.nodeType===Node.DOCUMENT_FRAGMENT_NODE||e.nodeType===Node.DOCUMENT_NODE}function l(e,t){var n=e.parentNode;return n&&n.host&&11===n.nodeType?n.host:n===t?null:n}function o(e,t,n){void 0===e&&(e=null),void 0===n&&(n=null);var r=[];if(n)r=n;else{var l=function e(t){for(var n=0;n<t.length;n++){var l=t[n];r.push(l),l.shadowRoot&&e(l.shadowRoot.querySelectorAll("*"))}};t.shadowRoot&&l(t.shadowRoot.querySelectorAll("*")),l(t.querySelectorAll("*"))}return e?r.filter((function(t){return t.matches(e)})):r}return e.collectAllElementsDeep=o,e.querySelectorAllDeep=function(e,n,r){return void 0===n&&(n=document),void 0===r&&(r=null),t(e,!0,n,r)},e.querySelectorDeep=function(e,n,r){return void 0===n&&(n=document),void 0===r&&(r=null),t(e,!1,n,r)},Object.defineProperty(e,"__esModule",{value:!0}),e}({});



  // Module: src/config/search-engines.js
/**
 * Search engine catalog (single source of truth).
 * - SEARCH_ENGINE_META: engines selectable as KeyPilot default (settings / omnibox / newtab)
 * - LAUNCHER_SEARCH_SITES: full list shown in Launcher → Searches favorites
 */

/** @typedef {'brave'|'google'|'duckduckgo'} SearchEngineId */

/**
 * @typedef {{
 *   id: SearchEngineId,
 *   label: string,
 *   homeUrl: string,
 *   searchUrlPrefix: string
 * }} SearchEngineMeta
 */

/** @type {Readonly<Record<SearchEngineId, SearchEngineMeta>>} */
const SEARCH_ENGINE_META = Object.freeze({
  brave: Object.freeze({
    id: 'brave',
    label: 'Brave',
    homeUrl: 'https://search.brave.com/',
    searchUrlPrefix: 'https://search.brave.com/search?q='
  }),
  google: Object.freeze({
    id: 'google',
    label: 'Google',
    homeUrl: 'https://www.google.com/',
    searchUrlPrefix: 'https://www.google.com/search?q='
  }),
  duckduckgo: Object.freeze({
    id: 'duckduckgo',
    label: 'DuckDuckGo',
    homeUrl: 'https://duckduckgo.com/',
    searchUrlPrefix: 'https://duckduckgo.com/?q='
  })
});

const DEFAULT_SEARCH_ENGINE_ID = /** @type {SearchEngineId} */ ('brave');

/**
 * Launcher favorites for the Searches category.
 * Includes settings engines plus additional common search homes.
 * @type {ReadonlyArray<{ title: string, url: string, isDefault: true }>}
 */
const LAUNCHER_SEARCH_SITES = Object.freeze([
  Object.freeze({ title: 'Google', url: 'https://google.com', isDefault: true }),
  Object.freeze({ title: 'Bing', url: 'https://bing.com', isDefault: true }),
  Object.freeze({ title: 'DuckDuckGo', url: 'https://duckduckgo.com', isDefault: true }),
  Object.freeze({ title: 'Yahoo', url: 'https://yahoo.com', isDefault: true }),
  Object.freeze({ title: 'Brave Search', url: 'https://search.brave.com', isDefault: true }),
  Object.freeze({ title: 'Ecosia', url: 'https://ecosia.org', isDefault: true }),
  Object.freeze({ title: 'Startpage', url: 'https://startpage.com', isDefault: true }),
  Object.freeze({ title: 'Yandex', url: 'https://yandex.com', isDefault: true })
]);

/**
 * @param {any} raw
 * @returns {SearchEngineId}
 */
function normalizeSearchEngineId(raw) {
  if (raw === 'google' || raw === 'duckduckgo' || raw === 'brave') return raw;
  return DEFAULT_SEARCH_ENGINE_ID;
}

/**
 * @param {any} engine
 * @returns {SearchEngineMeta}
 */
function getSearchEngineMeta(engine) {
  const id = normalizeSearchEngineId(engine);
  return SEARCH_ENGINE_META[id] || SEARCH_ENGINE_META[DEFAULT_SEARCH_ENGINE_ID];
}



  // Module: src/config/url-policy.js
/**
 * URL / tab skip policy (single source of truth).
 * Used by the service worker for tab switching and navigation graph recording.
 */

/** Schemes that should not be recorded in the per-tab navigation graph. */
const SKIP_URL_PATTERNS = Object.freeze([
  /^chrome:\/\//i,
  /^chrome-extension:\/\//i,
  /^edge:\/\//i,
  /^about:/i,
  /^data:/i,
  /^chrome-native:/i,
  /^view-source:/i
]);

/**
 * Patterns for Q/W tab cycling (after KeyPilot New Tab is allowlisted).
 * Note: chrome-extension:// is intentionally not in this list so other extension
 * pages can participate if needed; New Tab is handled explicitly below.
 */
const SKIP_TAB_URL_PATTERNS = Object.freeze([
  /^chrome:\/\//i,
  /^edge:\/\//i,
  /^about:/i,
  /^data:/i,
  /^chrome-native:/i,
  /^view-source:/i
]);

/**
 * @param {string|null|undefined} url
 * @returns {boolean}
 */
function isSkippableUrl(url) {
  if (!url || typeof url !== 'string') return true;
  const u = url.trim();
  if (!u) return true;
  return SKIP_URL_PATTERNS.some((pattern) => pattern.test(u));
}

/**
 * True when this URL is KeyPilot's custom New Tab (or Chromium's chrome://newtab
 * surface while that override is active).
 * @param {string|null|undefined} u
 * @returns {boolean}
 */
function isKeyPilotNewTabUrl(u) {
  if (!u || typeof u !== 'string') return false;
  const s = u.trim();
  if (!s) return false;

  // Common Chromium/Chrome variants when New Tab is overridden.
  if (/^chrome:\/\/newtab\/?/i.test(s) || /^chrome:\/\/new-tab-page\/?/i.test(s)) {
    return true;
  }

  try {
    if (typeof chrome === 'undefined' || !chrome.runtime?.getURL) return false;
    const kpNewTabUrl = chrome.runtime.getURL('pages/newtab.html');
    if (s === kpNewTabUrl || s.startsWith(`${kpNewTabUrl}#`) || s.startsWith(`${kpNewTabUrl}?`)) {
      return true;
    }
    const kp = new URL(kpNewTabUrl);
    const parsed = new URL(s);
    if (parsed.origin === kp.origin && parsed.pathname.endsWith('/pages/newtab.html')) {
      return true;
    }
  } catch {
    // ignore
  }

  return false;
}

/**
 * Whether a browser tab should be skipped for Q/W tab cycling.
 * @param {{ url?: string, pendingUrl?: string }|null|undefined} tab
 * @returns {boolean}
 */
function isSkippableTab(tab) {
  const url = typeof tab?.url === 'string' ? tab.url : '';
  const pendingUrl = typeof tab?.pendingUrl === 'string' ? tab.pendingUrl : '';
  if (!url && !pendingUrl) return true;

  // Always allow the KeyPilot custom New Tab page in left/right tab cycling.
  if (isKeyPilotNewTabUrl(url) || isKeyPilotNewTabUrl(pendingUrl)) return false;

  return SKIP_TAB_URL_PATTERNS.some((pattern) => pattern.test(url || pendingUrl));
}



  // Module: src/messaging/types.js
/**
 * KeyPilot runtime message types (single source of truth).
 *
 * Routing notes:
 * - Content script ↔ service worker: chrome.runtime.sendMessage / onMessage
 * - Extension page → tab UI: prefer SW forward, or chrome.tabs.sendMessage
 * - Parent ↔ popover iframe: window.postMessage (KP_POPOVER_* family)
 */

/** @typedef {typeof MSG[keyof typeof MSG]} MessageType */

const MSG = Object.freeze({
  // --- Extension enable / status ---
  GET_STATE: 'KP_GET_STATE',
  SET_STATE: 'KP_SET_STATE',
  TOGGLE_STATE: 'KP_TOGGLE_STATE',
  STATE_RESPONSE: 'KP_STATE_RESPONSE',
  STATE_CHANGED: 'KP_STATE_CHANGED',
  UPDATE_STATE: 'KP_UPDATE_STATE',
  GET_STATUS: 'KP_GET_STATUS',
  STATUS: 'KP_STATUS',

  // --- Transient onboarding actions ---
  TRANSIENT_ACTION: 'KP_TRANSIENT_ACTION',

  // --- Tab / history navigation ---
  TAB_LEFT: 'KP_TAB_LEFT',
  TAB_RIGHT: 'KP_TAB_RIGHT',
  NEW_TAB: 'KP_NEW_TAB',
  CLOSE_TAB: 'KP_CLOSE_TAB',
  GO_BACK: 'KP_GO_BACK',
  GO_FORWARD: 'KP_GO_FORWARD',
  OPEN_URL_BACKGROUND: 'KP_OPEN_URL_BACKGROUND',
  OPEN_URL_FOREGROUND: 'KP_OPEN_URL_FOREGROUND',

  // --- UI open (content-script handlers; SW may forward) ---
  OPEN_SETTINGS_POPOVER: 'KP_OPEN_SETTINGS_POPOVER',
  OPEN_GUIDE_POPOVER: 'KP_OPEN_GUIDE_POPOVER',
  OPEN_ONBOARDING: 'KP_OPEN_ONBOARDING',
  /** Reset walkthrough progress and open it (e.g. Guide "Launch Walkthrough"). */
  LAUNCH_WALKTHROUGH: 'KP_LAUNCH_WALKTHROUGH',

  // --- History / bookmarks / favicon (SW APIs for content scripts) ---
  OMNIBOX_SUGGEST: 'KP_OMNIBOX_SUGGEST',
  GET_BOOKMARKS: 'KP_GET_BOOKMARKS',
  BROWSER_HISTORY_GET: 'KP_BROWSER_HISTORY_GET',
  GET_TOP_SITES: 'KP_GET_TOP_SITES',
  GET_HISTORY_FOR_DOMAINS: 'KP_GET_HISTORY_FOR_DOMAINS',
  GET_FAVICON: 'KP_GET_FAVICON',

  // --- Page preview screenshots for card backgrounds ---
  GET_PAGE_THUMB: 'KP_GET_PAGE_THUMB',
  PAGE_THUMB_RESPONSE: 'KP_PAGE_THUMB_RESPONSE',
  PAGE_THUMB_UPDATED: 'KP_PAGE_THUMB_UPDATED',

  // --- Per-tab navigation graph ---
  NAVGRAPH_GET: 'KP_NAVGRAPH_GET',
  NAVGRAPH_JUMP: 'KP_NAVGRAPH_JUMP',
  NAVGRAPH_CLEAR: 'KP_NAVGRAPH_CLEAR',

  // --- Generic ---
  SUCCESS: 'KP_SUCCESS',
  ERROR: 'KP_ERROR',

  // --- Link Preview mobile User-Agent (SW declarativeNetRequest session rules) ---
  SET_PREVIEW_MOBILE_UA: 'KP_SET_PREVIEW_MOBILE_UA',

  // --- Parent ↔ popover iframe (window.postMessage) ---
  POPOVER_BRIDGE_INIT: 'KP_POPOVER_BRIDGE_INIT',
  POPOVER_BRIDGE_READY: 'KP_POPOVER_BRIDGE_READY',
  POPOVER_REQUEST_CLOSE: 'KP_POPOVER_REQUEST_CLOSE',
  POPOVER_BRIDGE_KEYDOWN: 'KP_POPOVER_BRIDGE_KEYDOWN',
  POPOVER_SCROLL: 'KP_POPOVER_SCROLL',
  /** Guide iframe → parent: close guide and open walkthrough from a reset state. */
  POPOVER_LAUNCH_WALKTHROUGH: 'KP_POPOVER_LAUNCH_WALKTHROUGH',

  // --- Parent → child frame activate (window.postMessage; third-party iframes) ---
  // Top-frame KeyPilot posts this when F/B/G lands on a cross-origin <iframe>.
  // Child frame-click-agent performs elementFromPoint + click in its own document.
  FRAME_ACTIVATE: 'KP_FRAME_ACTIVATE',

  // --- Parent → child frame scroll (window.postMessage; C/V under an iframe) ---
  // Top-frame KeyPilot posts this when C/V lands on an <iframe> shell. Child
  // frame-click-agent runs scroll-at-point at local coordinates (nested overflow
  // first, then the frame document).
  FRAME_SCROLL: 'KP_FRAME_SCROLL'
});

/**
 * Message types that open tab-local UI and must be forwarded by the SW
 * when sent via chrome.runtime.sendMessage from extension pages.
 */
const TAB_UI_FORWARD_TYPES = Object.freeze([
  MSG.OPEN_SETTINGS_POPOVER,
  MSG.OPEN_GUIDE_POPOVER,
  MSG.OPEN_ONBOARDING,
  MSG.LAUNCH_WALKTHROUGH
]);



  // Module: src/utils/dom-context.js
/**
 * Shared DOM/keyboard context helpers (single source of truth).
 * Used by EventManager and the shared popover iframe bridge.
 */

/** Input types where letter keys should type, not run KeyPilot shortcuts. */
const TEXT_ENTRY_INPUT_TYPES = Object.freeze([
  'text',
  'search',
  'url',
  'email',
  'tel',
  'password',
  'number',
  'date',
  'datetime-local',
  'month',
  'week',
  'time'
]);

const TEXT_ENTRY_TYPE_SET = new Set(TEXT_ENTRY_INPUT_TYPES);

/**
 * Walk open shadow roots to find the true focused element.
 * document.activeElement stops at shadow hosts.
 *
 * @param {Document|ShadowRoot|null|undefined} [root]
 * @returns {Element|null}
 */
function getDeepActiveElement(root = document) {
  let active = null;
  try {
    active = root?.activeElement || null;
  } catch {
    active = null;
  }

  while (active) {
    let next = null;
    try {
      next = active.shadowRoot?.activeElement || null;
    } catch {
      next = null;
    }
    if (!next) break;
    active = next;
  }

  return active;
}

/**
 * Best-effort real event target across open shadow boundaries.
 * Prefer composedPath()[0]; fall back to event.target.
 *
 * @param {Event|null|undefined} e
 * @returns {EventTarget|null}
 */
function getComposedEventTarget(e) {
  if (!e) return null;
  try {
    const path = typeof e.composedPath === 'function' ? e.composedPath() : null;
    if (path && path.length) {
      for (const node of path) {
        if (node && /** @type {any} */ (node).nodeType === 1) return node;
      }
    }
  } catch { /* ignore */ }
  return e.target || null;
}

/**
 * True only when the user is in a real text-entry field where letter keys should type.
 * Non-text inputs (checkbox, radio, button, range, etc.) must NOT block KeyPilot shortcuts.
 *
 * @param {EventTarget|null|undefined} target
 * @param {{ treatSelectAsTyping?: boolean }} [opts]
 * @returns {boolean}
 */
function isTypingContext(target, opts = {}) {
  if (!target) return false;

  // Text nodes can be the original event target inside contenteditable.
  let el = /** @type {any} */ (target);
  try {
    if (el.nodeType === 3) el = el.parentElement;
  } catch { /* ignore */ }
  if (!el || el.nodeType !== 1) return false;

  const node = /** @type {HTMLElement} */ (el);

  try {
    if (node.isContentEditable) return true;
  } catch { /* ignore */ }

  // Walk up a few ancestors for nested contenteditable hosts / custom wrappers.
  try {
    let p = node.parentElement;
    let depth = 0;
    while (p && depth++ < 4) {
      if (p.isContentEditable) return true;
      p = p.parentElement;
    }
  } catch { /* ignore */ }

  const tag = node.tagName?.toLowerCase?.() || '';
  if (tag === 'textarea') {
    return !(/** @type {HTMLTextAreaElement} */ (node)).disabled;
  }
  if (opts.treatSelectAsTyping && tag === 'select') {
    return !(/** @type {HTMLSelectElement} */ (node)).disabled;
  }
  if (tag !== 'input') return false;

  const input = /** @type {HTMLInputElement} */ (node);
  if (input.disabled || input.readOnly) return false;

  const type = String(input.type || 'text').toLowerCase();
  return TEXT_ENTRY_TYPE_SET.has(type);
}

/**
 * Resolve the element that should suppress KeyPilot letter shortcuts right now.
 * Checks composed event target first, then deep activeElement (shadow-aware).
 *
 * @param {Event|null|undefined} e
 * @param {{ treatSelectAsTyping?: boolean }} [opts]
 * @returns {Element|null}
 */
function resolveTypingTarget(e, opts = {}) {
  const composed = getComposedEventTarget(e);
  if (isTypingContext(composed, opts)) return /** @type {Element} */ (composed);

  const deep = getDeepActiveElement();
  if (deep && isTypingContext(deep, opts)) return deep;

  // Last resort: light-DOM activeElement (may be a shadow host — still try).
  try {
    if (typeof document !== 'undefined' && isTypingContext(document.activeElement, opts)) {
      return /** @type {Element} */ (document.activeElement);
    }
  } catch { /* ignore */ }

  return null;
}

/**
 * @param {KeyboardEvent|null|undefined} e
 * @returns {boolean}
 */
function hasModifierKeys(e) {
  if (!e) return false;
  return !!(e.ctrlKey || e.metaKey || e.altKey || e.shiftKey);
}

/**
 * Bundle-safe aliases for class methods.
 * After import-stripping, methods like `isTypingContext(){ return isTypingContext() }`
 * can be ambiguous; always call through these distinct names from EventManager.
 */
const kpIsTypingContext = isTypingContext;
const kpResolveTypingTarget = resolveTypingTarget;
const kpGetDeepActiveElement = getDeepActiveElement;
const kpGetComposedEventTarget = getComposedEventTarget;
const kpHasModifierKeys = hasModifierKeys;



  // Module: src/utils/extension-context.js
/**
 * Helpers for Chrome extension context validity.
 *
 * After an extension reload/update, content scripts on already-open pages keep
 * running but chrome.* APIs throw "Extension context invalidated". Detect that
 * early, fail closed, and avoid spamming console/errors.
 */

let _contextInvalidated = false;

/**
 * @returns {boolean} True when chrome.runtime is usable from this realm.
 */
function isExtensionContextValid() {
  if (_contextInvalidated) return false;
  try {
    // chrome.runtime.id is undefined after the extension context is invalidated.
    if (typeof chrome === 'undefined' || !chrome.runtime || !chrome.runtime.id) {
      _contextInvalidated = true;
      return false;
    }
    return true;
  } catch {
    _contextInvalidated = true;
    return false;
  }
}

/**
 * @param {unknown} error
 * @returns {boolean}
 */
function isExtensionContextInvalidatedError(error) {
  if (!error) return false;
  const msg = typeof error === 'string'
    ? error
    : (error && typeof error === 'object' && 'message' in error
      ? String(/** @type {{ message?: unknown }} */ (error).message || '')
      : String(error));
  return /Extension context invalidated/i.test(msg);
}

/**
 * Mark the context as dead for this content-script lifetime.
 */
function markExtensionContextInvalidated() {
  _contextInvalidated = true;
}

/**
 * @param {unknown} error
 * @returns {boolean} True if the error was an invalidated-context failure.
 */
function noteExtensionContextError(error) {
  if (isExtensionContextInvalidatedError(error)) {
    markExtensionContextInvalidated();
    return true;
  }
  // Some paths only surface a missing runtime id without that exact message.
  if (!isExtensionContextValid()) {
    return true;
  }
  return false;
}

/**
 * Fire-and-forget runtime message with invalidated-context handling.
 * @param {object} message
 * @param {{
 *   onInvalidated?: () => void,
 *   onError?: (error: unknown) => void,
 *   onResponse?: (response: unknown) => void
 * }} [opts]
 * @returns {boolean} False if the message could not be sent (invalid context or sync throw).
 */
function safeRuntimeSendMessage(message, opts = {}) {
  if (!isExtensionContextValid()) {
    try { opts.onInvalidated?.(); } catch { /* ignore */ }
    return false;
  }

  try {
    const result = chrome.runtime.sendMessage(message);
    // MV3 returns a Promise; swallow invalidated rejections so they don't surface as unhandled.
    if (result && typeof result.then === 'function') {
      result.then((response) => {
        try { opts.onResponse?.(response); } catch { /* ignore */ }
      }).catch((error) => {
        if (noteExtensionContextError(error)) {
          try { opts.onInvalidated?.(); } catch { /* ignore */ }
          return;
        }
        try { opts.onError?.(error); } catch { /* ignore */ }
      });
    }
    return true;
  } catch (error) {
    if (noteExtensionContextError(error)) {
      try { opts.onInvalidated?.(); } catch { /* ignore */ }
      return false;
    }
    try { opts.onError?.(error); } catch { /* ignore */ }
    return false;
  }
}



  // Module: src/utils/storage.js
/**
 * Shared chrome.storage helpers.
 *
 * Policy used across KeyPilot:
 * - Prefer `chrome.storage.sync` (profile-wide)
 * - Fall back to `chrome.storage.local` when sync fails or is empty
 * - Return caller default when neither has a value
 */

/**
 * Read a single key: sync → local → defaultValue.
 * @template T
 * @param {string} key
 * @param {T} [defaultValue]
 * @returns {Promise<T>}
 */
async function storageGetValue(key, defaultValue = undefined) {
  if (!key || typeof key !== 'string') return defaultValue;

  try {
    if (chrome?.storage?.sync?.get) {
      const syncResult = await chrome.storage.sync.get([key]);
      if (syncResult && Object.prototype.hasOwnProperty.call(syncResult, key) &&
          syncResult[key] !== undefined) {
        return /** @type {T} */ (syncResult[key]);
      }
    }
  } catch {
    // ignore, fall back to local
  }

  try {
    if (chrome?.storage?.local?.get) {
      const localResult = await chrome.storage.local.get([key]);
      if (localResult && Object.prototype.hasOwnProperty.call(localResult, key) &&
          localResult[key] !== undefined) {
        return /** @type {T} */ (localResult[key]);
      }
    }
  } catch {
    // ignore
  }

  return defaultValue;
}

/**
 * Read multiple keys. For each key, prefer sync value when present, else local.
 * @param {string[]} keys
 * @returns {Promise<Record<string, any>>}
 */
async function storageGetKeys(keys) {
  const list = Array.isArray(keys) ? keys.filter((k) => typeof k === 'string' && k) : [];
  if (!list.length) return {};

  /** @type {Record<string, any>} */
  let sync = {};
  /** @type {Record<string, any>} */
  let local = {};

  try {
    if (chrome?.storage?.sync?.get) {
      sync = (await chrome.storage.sync.get(list)) || {};
    }
  } catch {
    sync = {};
  }

  try {
    if (chrome?.storage?.local?.get) {
      local = (await chrome.storage.local.get(list)) || {};
    }
  } catch {
    local = {};
  }

  /** @type {Record<string, any>} */
  const out = {};
  for (const key of list) {
    if (Object.prototype.hasOwnProperty.call(sync, key) && sync[key] !== undefined) {
      out[key] = sync[key];
    } else if (Object.prototype.hasOwnProperty.call(local, key) && local[key] !== undefined) {
      out[key] = local[key];
    }
  }
  return out;
}

/**
 * Write a single key: try sync, then local.
 * @param {string} key
 * @param {any} value
 * @param {{ includeTimestamp?: boolean }} [opts]
 * @returns {Promise<boolean>} true if either area accepted the write
 */
async function storageSetValue(key, value, opts = {}) {
  if (!key || typeof key !== 'string') return false;

  /** @type {Record<string, any>} */
  const payload = { [key]: value };
  if (opts.includeTimestamp) {
    payload.timestamp = Date.now();
  }

  try {
    if (chrome?.storage?.sync?.set) {
      await chrome.storage.sync.set(payload);
      return true;
    }
  } catch {
    // fall back to local
  }

  try {
    if (chrome?.storage?.local?.set) {
      await chrome.storage.local.set(payload);
      return true;
    }
  } catch {
    // ignore
  }

  return false;
}

/**
 * Write an object of keys: try sync, then local.
 * @param {Record<string, any>} obj
 * @returns {Promise<boolean>}
 */
async function storageSetObject(obj) {
  if (!obj || typeof obj !== 'object') return false;

  try {
    if (chrome?.storage?.sync?.set) {
      await chrome.storage.sync.set(obj);
      return true;
    }
  } catch {
    // fall back
  }

  try {
    if (chrome?.storage?.local?.set) {
      await chrome.storage.local.set(obj);
      return true;
    }
  } catch {
    // ignore
  }

  return false;
}



  // Module: src/utils/image-utils.js
/**
 * Image / media utilities for KeyPilot.
 *
 * Designed as reusable helpers (not clipboard-tied). First consumer is the
 * I-key (COPY_HOVERED_IMAGE) which copies the result to the clipboard.
 *
 * Sources supported:
 * - <img> (including src pointing at .svg)
 * - CSS background-image url(...)
 * - Inline <svg> (hit on svg or any descendant path/g/etc.)
 */

/** Max ancestor walk when searching for img / svg / background-image. */
const MAX_ANCESTOR_DEPTH = 12;

/** Fetch / decode timeout for materializing remote images. */
const MATERIALIZE_TIMEOUT_MS = 8000;

/** Max canvas edge when rasterizing SVG (avoids huge allocations). */
const MAX_SVG_RASTER_EDGE = 4096;

/**
 * @typedef {'img'|'background'|'svg'} HoveredImageKind
 *
 * @typedef {{
 *   kind: HoveredImageKind,
 *   element: Element,
 *   url: string|null
 * }} HoveredImageSource
 *
 * @typedef {{
 *   kind: HoveredImageKind,
 *   element: Element,
 *   url: string|null,
 *   blob: Blob,
 *   mimeType: string
 * }} HoveredImageResult
 */

/**
 * Shadow-DOM–aware elementFromPoint (mirrors ElementDetector.deepElementFromPoint).
 * @param {number} x
 * @param {number} y
 * @returns {Element|null}
 */
function deepElementFromPoint(x, y) {
  let el = null;
  try {
    el = document.elementFromPoint(x, y);
  } catch {
    return null;
  }
  let guard = 0;
  while (el && el.shadowRoot && guard++ < 10) {
    let nested = null;
    try {
      nested = el.shadowRoot.elementFromPoint(x, y);
    } catch {
      break;
    }
    if (!nested || nested === el) break;
    el = nested;
  }
  return el;
}

/**
 * True when a node looks like KeyPilot-injected UI (should not count as page media).
 * @param {Element|null|undefined} el
 * @returns {boolean}
 */
function isKeyPilotChrome(el) {
  if (!el || el.nodeType !== 1) return false;
  try {
    const id = typeof el.id === 'string' ? el.id : '';
    if (id && (id === 'kpv2-cursor' || id.startsWith('kpv2-'))) return true;
  } catch { /* ignore */ }
  try {
    const cls = typeof el.className === 'string'
      ? el.className
      : (el.className && typeof el.className.baseVal === 'string' ? el.className.baseVal : '');
    if (cls && (/\bkpv2-/.test(cls) || /\bkp-/.test(cls))) return true;
  } catch { /* ignore */ }
  return false;
}

/**
 * @param {Element|null|undefined} el
 * @returns {el is HTMLImageElement}
 */
function isHtmlImage(el) {
  return !!(el && el.nodeType === 1 && el.tagName === 'IMG');
}

/**
 * True when el is an <svg> root (SVGSVGElement).
 * @param {Element|null|undefined} el
 * @returns {boolean}
 */
function isSvgRoot(el) {
  if (!el || el.nodeType !== 1) return false;
  try {
    if (typeof SVGSVGElement !== 'undefined' && el instanceof SVGSVGElement) return true;
  } catch { /* ignore */ }
  return String(el.tagName || '').toLowerCase() === 'svg';
}

/**
 * Resolve the outermost <svg> for a hit target (path, g, use, etc.).
 * @param {Element|null|undefined} el
 * @returns {SVGSVGElement|Element|null}
 */
function getSvgRoot(el) {
  if (!el || el.nodeType !== 1) return null;
  if (isSvgRoot(el)) return el;

  // SVG geometry nodes expose ownerSVGElement → root <svg>.
  try {
    const owner = /** @type {any} */ (el).ownerSVGElement;
    if (owner && isSvgRoot(owner)) return owner;
  } catch { /* ignore */ }

  try {
    if (typeof el.closest === 'function') {
      const svg = el.closest('svg');
      if (svg) return svg;
    }
  } catch { /* ignore */ }

  return null;
}

/**
 * Best URL for an <img>. Prefer currentSrc (srcset-aware).
 * @param {HTMLImageElement} img
 * @returns {string}
 */
function getImgUrl(img) {
  try {
    const current = (typeof img.currentSrc === 'string' && img.currentSrc) ? img.currentSrc.trim() : '';
    if (current) return current;
  } catch { /* ignore */ }
  try {
    const src = (typeof img.src === 'string' && img.src) ? img.src.trim() : '';
    if (src) return src;
  } catch { /* ignore */ }
  try {
    const attr = (img.getAttribute && img.getAttribute('src')) || '';
    return String(attr || '').trim();
  } catch {
    return '';
  }
}

/**
 * Reject empty / trivial image URLs.
 * @param {string|null|undefined} url
 * @returns {boolean}
 */
function isUsableImageUrl(url) {
  if (!url || typeof url !== 'string') return false;
  const u = url.trim();
  if (!u || u === 'about:blank') return false;
  return true;
}

/**
 * Extract the first url(...) from a CSS background-image value.
 * Skips gradients and other non-url layers.
 * @param {string} cssValue
 * @returns {string|null}
 */
function extractFirstBackgroundImageUrl(cssValue) {
  if (!cssValue || typeof cssValue !== 'string') return null;
  const value = cssValue.trim();
  if (!value || value === 'none') return null;

  const urlRe = /url\s*\(\s*(['"]?)([^'")]+)\1\s*\)/gi;
  let match;
  while ((match = urlRe.exec(value)) !== null) {
    const raw = (match[2] || '').trim();
    if (!raw) continue;
    if (/^(none|inherit|initial|unset|revert)$/i.test(raw)) continue;
    return raw;
  }
  return null;
}

/**
 * Resolve a possibly-relative CSS/image URL against the owner document.
 * @param {string} url
 * @param {Element} element
 * @returns {string}
 */
function resolveUrl(url, element) {
  if (!url) return '';
  if (/^(data:|blob:|https?:|chrome-extension:)/i.test(url)) return url;
  try {
    const base =
      (element && element.ownerDocument && element.ownerDocument.baseURI) ||
      (typeof document !== 'undefined' ? document.baseURI : '') ||
      '';
    return new URL(url, base).href;
  } catch {
    return url;
  }
}

/**
 * Next ancestor for walk (parentElement, or shadow host).
 * @param {Element} el
 * @returns {Element|null}
 */
function getParentOrHost(el) {
  let next = null;
  try {
    next = el.parentElement;
  } catch {
    next = null;
  }
  if (!next) {
    try {
      const root = el.getRootNode && el.getRootNode();
      if (root && /** @type {any} */ (root).host) next = /** @type {any} */ (root).host;
    } catch { /* ignore */ }
  }
  return next;
}

/**
 * @param {number} x
 * @param {number} y
 * @param {DOMRect|ClientRect|{left:number,top:number,right:number,bottom:number,width:number,height:number}} rect
 * @returns {boolean}
 */
function pointInRect(x, y, rect) {
  if (!rect) return false;
  const w = Number(rect.width) || 0;
  const h = Number(rect.height) || 0;
  if (w < 1 || h < 1) return false;
  return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
}

/**
 * @param {Element} el
 * @param {number} x
 * @param {number} y
 * @returns {boolean}
 */
function elementContainsPoint(el, x, y) {
  try {
    return pointInRect(x, y, el.getBoundingClientRect());
  } catch {
    return false;
  }
}

/**
 * Source from a single element if it itself is an image/svg/background.
 * Does not search descendants.
 * @param {Element|null|undefined} el
 * @returns {HoveredImageSource|null}
 */
function sourceFromSelf(el) {
  if (!el || el.nodeType !== 1 || isKeyPilotChrome(el)) return null;

  if (isHtmlImage(el)) {
    const url = getImgUrl(/** @type {HTMLImageElement} */ (el));
    if (isUsableImageUrl(url)) {
      return { kind: 'img', element: el, url: resolveUrl(url, el) };
    }
  }

  if (isSvgRoot(el)) {
    return { kind: 'svg', element: el, url: null };
  }

  // Hit on SVG child geometry → root <svg>
  try {
    const svgRoot = getSvgRoot(el);
    if (svgRoot && svgRoot !== el && !isKeyPilotChrome(svgRoot)) {
      return { kind: 'svg', element: svgRoot, url: null };
    }
  } catch { /* ignore */ }

  try {
    const style = window.getComputedStyle && window.getComputedStyle(el);
    const bg = style ? String(style.backgroundImage || '') : '';
    if (bg && bg !== 'none' && /url\s*\(/i.test(bg)) {
      const extracted = extractFirstBackgroundImageUrl(bg);
      if (extracted && isUsableImageUrl(extracted)) {
        return {
          kind: 'background',
          element: el,
          url: resolveUrl(extracted, el)
        };
      }
    }
  } catch { /* ignore */ }

  return null;
}

/**
 * Tree search under `root` for an image that still covers (x, y).
 * Handles common card markup where hit is the wrapping <a>/<picture>/<div>
 * and the real <img> is a descendant (sometimes with pointer-events: none).
 *
 * @param {Element} root
 * @param {number} x
 * @param {number} y
 * @returns {HoveredImageSource|null}
 */
function findImageInSubtree(root, x, y) {
  if (!root || root.nodeType !== 1) return null;

  // <picture> → prefer its selected <img>
  try {
    if (String(root.tagName || '').toLowerCase() === 'picture') {
      const img = root.querySelector('img');
      if (img) {
        const fromImg = sourceFromSelf(img);
        if (fromImg) return fromImg;
      }
    }
  } catch { /* ignore */ }

  // Prefer <img> descendants whose box still contains the cursor.
  // pointer-events:none imgs never win elementFromPoint but still "paint" under the cursor.
  try {
    const imgs = root.querySelectorAll('img');
    /** @type {HoveredImageSource|null} */
    let fallback = null;
    for (let i = 0; i < imgs.length; i++) {
      const img = imgs[i];
      if (isKeyPilotChrome(img)) continue;
      const url = getImgUrl(/** @type {HTMLImageElement} */ (img));
      if (!isUsableImageUrl(url)) continue;
      const src = {
        kind: /** @type {const} */ ('img'),
        element: img,
        url: resolveUrl(url, img)
      };
      if (elementContainsPoint(img, x, y)) return src;
      // Fallback: single usable img inside a small wrapper (e.g. image-holder / link)
      if (!fallback) fallback = src;
    }
    // Only use non-point fallback for tight wrappers (link/picture/figure/holder),
    // not for huge containers like <article> or <body> (would steal unrelated images).
    if (fallback && imgs.length === 1) {
      const tag = String(root.tagName || '').toLowerCase();
      const cls = typeof root.className === 'string' ? root.className : '';
      const tight =
        tag === 'a' || tag === 'picture' || tag === 'figure' || tag === 'button' ||
        /image|thumb|media|photo|cover|figure|poster/i.test(cls);
      if (tight) return fallback;
    }
  } catch { /* ignore */ }

  // Inline SVG descendants under the point
  try {
    const svgs = root.querySelectorAll('svg');
    for (let i = 0; i < svgs.length; i++) {
      const svg = svgs[i];
      if (isKeyPilotChrome(svg)) continue;
      if (!isSvgRoot(svg)) continue;
      if (elementContainsPoint(svg, x, y)) {
        return { kind: 'svg', element: svg, url: null };
      }
    }
  } catch { /* ignore */ }

  return null;
}

/**
 * Walk hit node + ancestors: self, then tree under each node for nested media.
 * @param {Element|null} start
 * @param {number} x
 * @param {number} y
 * @returns {HoveredImageSource|null}
 */
function findImageSourceFromElement(start, x, y) {
  if (!start) return null;

  // Fast path: SVG geometry under cursor
  try {
    const svgHit = getSvgRoot(start);
    if (svgHit && !isKeyPilotChrome(svgHit)) {
      return { kind: 'svg', element: svgHit, url: null };
    }
  } catch { /* ignore */ }

  let el = start;
  let depth = 0;
  while (el && el.nodeType === 1 && depth < MAX_ANCESTOR_DEPTH) {
    if (isKeyPilotChrome(el)) {
      el = getParentOrHost(el);
      depth++;
      continue;
    }

    const selfHit = sourceFromSelf(el);
    if (selfHit) return selfHit;

    // Nested media (e.g. <a><picture><img></picture></a> when hit is <a> or <picture>)
    const nested = findImageInSubtree(el, x, y);
    if (nested) return nested;

    el = getParentOrHost(el);
    depth++;
  }

  return null;
}

/**
 * Scan the full element stack under the cursor (top → bottom).
 * Catches cases where the painted <img> is below a transparent overlay.
 * @param {number} x
 * @param {number} y
 * @returns {HoveredImageSource|null}
 */
function findImageFromPointStack(x, y) {
  let stack = null;
  try {
    if (typeof document.elementsFromPoint === 'function') {
      stack = document.elementsFromPoint(x, y);
    }
  } catch {
    stack = null;
  }
  if (!stack || !stack.length) return null;

  const limit = Math.min(stack.length, 24);
  for (let i = 0; i < limit; i++) {
    const el = stack[i];
    if (!el || el.nodeType !== 1) continue;
    if (isKeyPilotChrome(el)) continue;
    const selfHit = sourceFromSelf(el);
    if (selfHit) return selfHit;
  }
  return null;
}

/**
 * Sync discovery: find the image source under the cursor.
 *
 * Strategy (Engadget-style cards and similar):
 * 1. elementsFromPoint stack — painted <img> may sit under a transparent overlay
 * 2. hit node + ancestor walk with **subtree** search — hit is often <a>/<picture>
 *    while the real <img> is nested (or pointer-events: none)
 *
 * @param {number} clientX
 * @param {number} clientY
 * @param {{ elementFromPoint?: (x: number, y: number) => Element|null }} [options]
 * @returns {HoveredImageSource|null}
 */
function findHoveredImageSource(clientX, clientY, options = {}) {
  if (typeof clientX !== 'number' || typeof clientY !== 'number' ||
      !Number.isFinite(clientX) || !Number.isFinite(clientY)) {
    return null;
  }

  // 1) Full stack under the cursor
  const fromStack = findImageFromPointStack(clientX, clientY);
  if (fromStack) return fromStack;

  // 2) Primary hit + ancestor/tree walk
  const hitFn = typeof options.elementFromPoint === 'function'
    ? options.elementFromPoint
    : deepElementFromPoint;

  let hit = null;
  try {
    hit = hitFn(clientX, clientY);
  } catch {
    hit = null;
  }
  if (!hit || hit.nodeType !== 1) return null;

  return findImageSourceFromElement(hit, clientX, clientY);
}

/**
 * @param {HTMLCanvasElement} canvas
 * @returns {Promise<Blob|null>}
 */
function canvasToPngBlob(canvas) {
  return new Promise((resolve) => {
    try {
      canvas.toBlob((b) => resolve(b || null), 'image/png');
    } catch {
      resolve(null);
    }
  });
}

/**
 * Convert an arbitrary image Blob to PNG via bitmap + canvas.
 * @param {Blob} blob
 * @returns {Promise<Blob|null>}
 */
async function blobToPng(blob) {
  if (!blob) return null;

  if (blob.type === 'image/png') return blob;

  // SVG blobs often fail createImageBitmap — go straight to <img> path for those.
  const isSvg = /svg/i.test(blob.type || '') ||
    (typeof blob.type === 'string' && blob.type.includes('svg'));

  if (!isSvg) {
    try {
      if (typeof createImageBitmap === 'function') {
        const bitmap = await createImageBitmap(blob);
        try {
          const canvas = document.createElement('canvas');
          canvas.width = bitmap.width || 1;
          canvas.height = bitmap.height || 1;
          const ctx = canvas.getContext('2d');
          if (!ctx) return null;
          ctx.drawImage(bitmap, 0, 0);
          return await canvasToPngBlob(canvas);
        } finally {
          try { bitmap.close(); } catch { /* ignore */ }
        }
      }
    } catch (err) {
      console.warn('[KeyPilot] createImageBitmap PNG convert failed:', err);
    }
  }

  // Fallback: HTMLImageElement + object URL (works for SVG + raster).
  try {
    const objectUrl = URL.createObjectURL(blob);
    try {
      const img = await loadHtmlImage(objectUrl);
      const canvas = document.createElement('canvas');
      canvas.width = Math.max(1, img.naturalWidth || img.width || 1);
      canvas.height = Math.max(1, img.naturalHeight || img.height || 1);
      const ctx = canvas.getContext('2d');
      if (!ctx) return null;
      ctx.drawImage(img, 0, 0);
      return await canvasToPngBlob(canvas);
    } finally {
      try { URL.revokeObjectURL(objectUrl); } catch { /* ignore */ }
    }
  } catch (err) {
    console.warn('[KeyPilot] Image element PNG convert failed:', err);
    return null;
  }
}

/**
 * @param {string} src
 * @returns {Promise<HTMLImageElement>}
 */
function loadHtmlImage(src) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    // Helps some cross-origin SVG/raster loads when CORS headers allow it.
    try { img.crossOrigin = 'anonymous'; } catch { /* ignore */ }
    const timer = setTimeout(() => {
      cleanup();
      reject(new Error('Image load timed out'));
    }, MATERIALIZE_TIMEOUT_MS);
    const cleanup = () => {
      clearTimeout(timer);
      img.onload = null;
      img.onerror = null;
    };
    img.onload = () => {
      cleanup();
      resolve(img);
    };
    img.onerror = () => {
      cleanup();
      reject(new Error('Image failed to load'));
    };
    img.src = src;
  });
}

/**
 * Fetch a URL (or use data/blob directly) and return a Blob.
 * @param {string} url
 * @returns {Promise<Blob|null>}
 */
async function fetchImageBlob(url) {
  if (!url) return null;

  const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
  const timer = setTimeout(() => {
    try { controller?.abort(); } catch { /* ignore */ }
  }, MATERIALIZE_TIMEOUT_MS);

  try {
    const res = await fetch(url, {
      signal: controller?.signal,
      credentials: 'include',
      mode: 'cors',
      cache: 'force-cache'
    });
    if (!res.ok) {
      console.warn('[KeyPilot] Image fetch failed:', res.status, url.slice(0, 120));
      return null;
    }
    const blob = await res.blob();
    return blob || null;
  } catch (err) {
    try {
      const res = await fetch(url, {
        credentials: 'omit',
        mode: 'cors',
        cache: 'force-cache'
      });
      if (!res.ok) return null;
      return await res.blob();
    } catch (err2) {
      console.warn('[KeyPilot] Image fetch error:', err2?.message || err2);
      return null;
    }
  } finally {
    clearTimeout(timer);
  }
}

/**
 * Pixel size to rasterize an SVG (CSS pixels × devicePixelRatio, capped).
 * @param {Element} svg
 * @returns {{ width: number, height: number }}
 */
function getSvgRasterSize(svg) {
  let cssW = 0;
  let cssH = 0;
  try {
    const rect = svg.getBoundingClientRect();
    cssW = rect.width || 0;
    cssH = rect.height || 0;
  } catch { /* ignore */ }

  if (cssW < 1 || cssH < 1) {
    try {
      const vb = /** @type {any} */ (svg).viewBox?.baseVal;
      if (vb && vb.width > 0 && vb.height > 0) {
        cssW = vb.width;
        cssH = vb.height;
      }
    } catch { /* ignore */ }
  }

  if (cssW < 1) cssW = 64;
  if (cssH < 1) cssH = 64;

  const dpr = Math.min(
    (typeof window !== 'undefined' && window.devicePixelRatio) ? window.devicePixelRatio : 1,
    2
  );
  let width = Math.round(cssW * dpr);
  let height = Math.round(cssH * dpr);

  const maxEdge = Math.max(width, height);
  if (maxEdge > MAX_SVG_RASTER_EDGE) {
    const scale = MAX_SVG_RASTER_EDGE / maxEdge;
    width = Math.max(1, Math.round(width * scale));
    height = Math.max(1, Math.round(height * scale));
  }

  return { width: Math.max(1, width), height: Math.max(1, height) };
}

/**
 * Serialize an inline <svg> into a standalone SVG document string suitable for
 * loading into an Image / blob URL. Relative resource URLs are absolutized when possible.
 *
 * @param {Element} svg
 * @returns {string|null}
 */
function serializeInlineSvg(svg) {
  if (!svg || !isSvgRoot(svg)) return null;

  let clone;
  try {
    clone = /** @type {Element} */ (svg.cloneNode(true));
  } catch {
    return null;
  }

  try {
    if (!clone.getAttribute('xmlns')) {
      clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
    }
    // Many icons use xlink:href on <use>/<image>.
    if (!clone.getAttribute('xmlns:xlink')) {
      clone.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
    }
  } catch { /* ignore */ }

  // Ensure explicit dimensions so browsers rasterize consistently.
  try {
    const rect = svg.getBoundingClientRect();
    if (!clone.getAttribute('width') && rect.width > 0) {
      clone.setAttribute('width', String(rect.width));
    }
    if (!clone.getAttribute('height') && rect.height > 0) {
      clone.setAttribute('height', String(rect.height));
    }
  } catch { /* ignore */ }

  // Preserve or synthesize viewBox so scaling stays correct.
  try {
    if (!clone.getAttribute('viewBox')) {
      const vb = /** @type {any} */ (svg).viewBox?.baseVal;
      if (vb && vb.width > 0 && vb.height > 0) {
        clone.setAttribute('viewBox', `${vb.x} ${vb.y} ${vb.width} ${vb.height}`);
      } else {
        const w = parseFloat(clone.getAttribute('width') || '') || 0;
        const h = parseFloat(clone.getAttribute('height') || '') || 0;
        if (w > 0 && h > 0) {
          clone.setAttribute('viewBox', `0 0 ${w} ${h}`);
        }
      }
    }
  } catch { /* ignore */ }

  // Absolutize href / xlink:href on nested images and uses (best-effort).
  try {
    const refs = clone.querySelectorAll('image, use');
    refs.forEach((node) => {
      for (const attr of ['href', 'xlink:href']) {
        try {
          const val = node.getAttribute(attr);
          if (!val || val.startsWith('#') || /^(data:|blob:)/i.test(val)) continue;
          node.setAttribute(attr, resolveUrl(val, svg));
        } catch { /* ignore */ }
      }
    });
  } catch { /* ignore */ }

  try {
    const serializer = new XMLSerializer();
    let xml = serializer.serializeToString(clone);
    // Ensure XML declaration-free but with SVG namespace (clone attrs should cover this).
    if (!/xmlns=/.test(xml)) {
      xml = xml.replace(
        /<svg\b/,
        '<svg xmlns="http://www.w3.org/2000/svg"'
      );
    }
    return xml;
  } catch (err) {
    console.warn('[KeyPilot] SVG serialize failed:', err);
    return null;
  }
}

/**
 * Rasterize an inline <svg> element to a PNG Blob.
 * @param {Element} svg
 * @returns {Promise<{ blob: Blob, mimeType: string }|null>}
 */
async function materializeInlineSvg(svg) {
  if (!svg || !isSvgRoot(svg)) return null;

  const xml = serializeInlineSvg(svg);
  if (!xml) return null;

  const { width, height } = getSvgRasterSize(svg);
  const svgBlob = new Blob([xml], { type: 'image/svg+xml;charset=utf-8' });
  const objectUrl = URL.createObjectURL(svgBlob);

  try {
    const img = await loadHtmlImage(objectUrl);
    const canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    const ctx = canvas.getContext('2d');
    if (!ctx) return null;

    // Transparent background — matches typical icon SVGs.
    ctx.clearRect(0, 0, width, height);
    ctx.drawImage(img, 0, 0, width, height);

    const png = await canvasToPngBlob(canvas);
    if (!png || png.size === 0) return null;
    return { blob: png, mimeType: 'image/png' };
  } catch (err) {
    console.warn('[KeyPilot] Inline SVG rasterize failed:', err);
    return null;
  } finally {
    try { URL.revokeObjectURL(objectUrl); } catch { /* ignore */ }
  }
}

/**
 * Materialize a source URL into a clipboard-friendly PNG Blob when possible.
 * Falls back to the original image/* blob type if PNG conversion fails.
 *
 * @param {string} url
 * @param {{ element?: Element|null }} [options]
 * @returns {Promise<{ blob: Blob, mimeType: string }|null>}
 */
async function materializeImageBlob(url, options = {}) {
  if (!isUsableImageUrl(url)) return null;

  // Fast path: already-loaded <img> can be drawn directly (helps same-origin SVG imgs).
  const el = options.element;
  if (isHtmlImage(el)) {
    try {
      const img = /** @type {HTMLImageElement} */ (el);
      if (img.complete && (img.naturalWidth > 0 || img.naturalHeight > 0)) {
        const canvas = document.createElement('canvas');
        canvas.width = Math.max(1, img.naturalWidth || img.width || 1);
        canvas.height = Math.max(1, img.naturalHeight || img.height || 1);
        const ctx = canvas.getContext('2d');
        if (ctx) {
          ctx.drawImage(img, 0, 0);
          const png = await canvasToPngBlob(canvas);
          // Tainted canvas yields null toBlob in some browsers — fall through to fetch.
          if (png && png.size > 0) {
            return { blob: png, mimeType: 'image/png' };
          }
        }
      }
    } catch {
      // Tainted or security error — fetch path below.
    }
  }

  const raw = await fetchImageBlob(url);
  if (!raw || raw.size === 0) return null;

  // Prefer PNG for ClipboardItem compatibility in Chromium.
  const png = await blobToPng(raw);
  if (png && png.size > 0) {
    return { blob: png, mimeType: 'image/png' };
  }

  const type = (raw.type && raw.type.startsWith('image/')) ? raw.type : 'image/png';
  return { blob: raw, mimeType: type };
}

/**
 * Find the image rendered under the cursor and return a Blob ready for use
 * (clipboard, media library, etc.). Not tied to the clipboard.
 *
 * @param {number} clientX - Viewport X (e.g. state.lastMouse.x)
 * @param {number} clientY - Viewport Y (e.g. state.lastMouse.y)
 * @param {{ elementFromPoint?: (x: number, y: number) => Element|null }} [options]
 * @returns {Promise<HoveredImageResult|null>}
 */
async function getHoveredImage(clientX, clientY, options = {}) {
  const source = findHoveredImageSource(clientX, clientY, options);
  if (!source) return null;

  let materialized = null;
  if (source.kind === 'svg') {
    materialized = await materializeInlineSvg(source.element);
  } else {
    materialized = await materializeImageBlob(source.url || '', { element: source.element });
  }
  if (!materialized) return null;

  return {
    kind: source.kind,
    element: source.element,
    url: source.url,
    blob: materialized.blob,
    mimeType: materialized.mimeType
  };
}



  // Module: src/utils/youtube-thumb.js
/**
 * YouTube video ID + official thumbnail URL helpers.
 * Shared by Launcher, New Tab, and page-thumb UI (prefer API thumbs over captures).
 */

/**
 * Extract YouTube video ID from a URL.
 * Supports:
 * - https://www.youtube.com/watch?v=VIDEO_ID
 * - https://youtu.be/VIDEO_ID
 * - https://www.youtube.com/embed/VIDEO_ID
 * - https://www.youtube.com/v/VIDEO_ID
 * - https://www.youtube.com/shorts/VIDEO_ID
 *
 * @param {string|null|undefined} url
 * @returns {string|null}
 */
function extractYouTubeVideoId(url) {
  if (!url || typeof url !== 'string') return null;

  try {
    const urlObj = new URL(url);
    const hostname = urlObj.hostname.replace(/^www\./, '').toLowerCase();

    if (!hostname.includes('youtube.com') && !hostname.includes('youtu.be')) {
      return null;
    }

    // youtu.be/VIDEO_ID
    if (hostname === 'youtu.be' || hostname.endsWith('.youtu.be')) {
      const videoId = urlObj.pathname.slice(1).split('/')[0].split('?')[0].split('&')[0];
      if (videoId && videoId.length === 11) return videoId;
      return null;
    }

    if (hostname.includes('youtube.com')) {
      if (urlObj.pathname === '/watch' && urlObj.searchParams.has('v')) {
        const videoId = urlObj.searchParams.get('v');
        if (videoId && videoId.length === 11) return videoId;
      }

      const embedMatch = urlObj.pathname.match(/^\/embed\/([a-zA-Z0-9_-]{11})/);
      if (embedMatch) return embedMatch[1];

      const vMatch = urlObj.pathname.match(/^\/v\/([a-zA-Z0-9_-]{11})/);
      if (vMatch) return vMatch[1];

      const shortsMatch = urlObj.pathname.match(/^\/shorts\/([a-zA-Z0-9_-]{11})/);
      if (shortsMatch) return shortsMatch[1];
    }

    return null;
  } catch {
    return null;
  }
}

/**
 * Official YouTube thumbnail URL.
 * @param {string} videoId
 * @param {'default'|'mqdefault'|'hqdefault'|'sddefault'|'maxresdefault'} [quality]
 * @returns {string|null}
 */
function getYouTubeThumbnailUrl(videoId, quality = 'hqdefault') {
  if (!videoId || typeof videoId !== 'string') return null;
  const valid = ['default', 'mqdefault', 'hqdefault', 'sddefault', 'maxresdefault'];
  const q = valid.includes(quality) ? quality : 'hqdefault';
  return `https://img.youtube.com/vi/${videoId}/${q}.jpg`;
}

/**
 * Prefer official YouTube thumb when the page URL is a video watch/embed link.
 * @param {string|null|undefined} pageUrl
 * @param {'default'|'mqdefault'|'hqdefault'|'sddefault'|'maxresdefault'} [quality]
 * @returns {string|null}
 */
function getYouTubeThumbnailUrlForPage(pageUrl, quality = 'hqdefault') {
  const id = extractYouTubeVideoId(pageUrl);
  if (!id) return null;
  return getYouTubeThumbnailUrl(id, quality);
}



  // Module: src/utils/popover-resize.js
/**
 * Generic popover / modal panel resize handles.
 *
 * Attaches edge + corner hit targets (with resize cursors) and a visible
 * bottom-right grip icon. Works with any fixed-position panel, including those
 * initially centered via margin:auto or transform: translate(-50%, -50%).
 *
 * @param {HTMLElement} panel
 * @param {{
 *   minWidth?: number,
 *   minHeight?: number,
 *   margin?: number,
 *   zIndex?: number|string,
 *   aspectRatio?: number|true,
 *     // number = width/height; true = lock to size at resize-start
 *   onResizeStart?: () => void,
 *   onResizeEnd?: () => void,
 *   onResize?: (rect: { left: number, top: number, width: number, height: number }) => void
 * }} [options]
 * @returns {{ dispose: () => void }|null}
 */
function makePopoverResizable(panel, options = {}) {
  if (!panel || !(panel instanceof Element)) return null;

  // Avoid double-attaching.
  try {
    if (panel.dataset && panel.dataset.kpResizable === '1') {
      return panel.__kpResizeApi || null;
    }
  } catch { /* ignore */ }

  const doc = panel.ownerDocument || document;
  const minWidth = Math.max(160, Number(options.minWidth) || 280);
  const minHeight = Math.max(120, Number(options.minHeight) || 180);
  const margin = Math.max(0, Number(options.margin) || 8);
  const handleZ = options.zIndex != null ? String(options.zIndex) : '20';
  // Fixed ratio (width/height), or true = capture at each resize start.
  const aspectRatioOpt = options.aspectRatio;
  const lockAspect = aspectRatioOpt === true || (typeof aspectRatioOpt === 'number' && aspectRatioOpt > 0);
  const fixedAspect = (typeof aspectRatioOpt === 'number' && aspectRatioOpt > 0)
    ? aspectRatioOpt
    : null;

  const EDGE = 6;
  const CORNER = 12;

  /** @type {Array<{ el: HTMLElement, dir: string }>} */
  const handles = [];
  /** @type {{ dir: string, startX: number, startY: number, origin: { left: number, top: number, width: number, height: number }, pointerId: number, aspect?: number }|null} */
  let drag = null;
  /** @type {HTMLElement[]} */
  let frozenIframes = [];
  let prevBodyCursor = '';
  let prevBodyUserSelect = '';

  const dirs = [
    { dir: 'n', cursor: 'n-resize' },
    { dir: 's', cursor: 's-resize' },
    { dir: 'e', cursor: 'e-resize' },
    { dir: 'w', cursor: 'w-resize' },
    { dir: 'ne', cursor: 'ne-resize' },
    { dir: 'nw', cursor: 'nw-resize' },
    { dir: 'se', cursor: 'se-resize' },
    { dir: 'sw', cursor: 'sw-resize' }
  ];

  // Ensure a positioning context for absolute handles.
  try {
    const cs = doc.defaultView?.getComputedStyle?.(panel);
    if (cs && (cs.position === 'static' || !cs.position)) {
      panel.style.position = 'fixed';
    }
  } catch {
    panel.style.position = panel.style.position || 'fixed';
  }

  const handleStyleBase = `
    position: absolute;
    z-index: ${handleZ};
    background: transparent;
    touch-action: none;
    pointer-events: auto;
  `;

  const placeHandle = (el, dir) => {
    // Reset
    el.style.top = el.style.right = el.style.bottom = el.style.left = '';
    el.style.width = el.style.height = '';

    if (dir === 'n' || dir === 's') {
      el.style.left = `${CORNER}px`;
      el.style.right = `${CORNER}px`;
      el.style.height = `${EDGE}px`;
      el.style.width = 'auto';
      if (dir === 'n') el.style.top = '0';
      else el.style.bottom = '0';
    } else if (dir === 'e' || dir === 'w') {
      el.style.top = `${CORNER}px`;
      el.style.bottom = `${CORNER}px`;
      el.style.width = `${EDGE}px`;
      el.style.height = 'auto';
      if (dir === 'e') el.style.right = '0';
      else el.style.left = '0';
    } else {
      // Corners
      el.style.width = `${CORNER}px`;
      el.style.height = `${CORNER}px`;
      if (dir.includes('n')) el.style.top = '0';
      if (dir.includes('s')) el.style.bottom = '0';
      if (dir.includes('e')) el.style.right = '0';
      if (dir.includes('w')) el.style.left = '0';
    }
  };

  for (const { dir, cursor } of dirs) {
    const el = doc.createElement('div');
    el.className = `kpv2-popover-resize-handle kpv2-popover-resize-${dir}`;
    el.dataset.kpResizeDir = dir;
    el.setAttribute('aria-hidden', 'true');
    el.style.cssText = handleStyleBase + `cursor: ${cursor};`;
    placeHandle(el, dir);

    // Visible grip icon only on SE corner
    if (dir === 'se') {
      el.style.width = '16px';
      el.style.height = '16px';
      el.style.display = 'flex';
      el.style.alignItems = 'flex-end';
      el.style.justifyContent = 'flex-end';
      el.style.padding = '0 2px 2px 0';
      el.style.color = 'rgba(255,255,255,0.45)';
      el.title = 'Resize';

      const svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
      svg.setAttribute('viewBox', '0 0 12 12');
      svg.setAttribute('width', '12');
      svg.setAttribute('height', '12');
      svg.setAttribute('aria-hidden', 'true');
      svg.style.cssText = 'display:block; pointer-events:none;';
      // Three diagonal grip lines (outline style)
      const lines = [
        'M4 11 L11 4',
        'M7 11 L11 7',
        'M10 11 L11 10'
      ];
      for (const d of lines) {
        const path = doc.createElementNS('http://www.w3.org/2000/svg', 'path');
        path.setAttribute('d', d);
        path.setAttribute('fill', 'none');
        path.setAttribute('stroke', 'currentColor');
        path.setAttribute('stroke-width', '1.5');
        path.setAttribute('stroke-linecap', 'round');
        svg.appendChild(path);
      }
      el.appendChild(svg);

      el.addEventListener('mouseenter', () => {
        el.style.color = 'rgba(255,255,255,0.75)';
      });
      el.addEventListener('mouseleave', () => {
        if (!drag) el.style.color = 'rgba(255,255,255,0.45)';
      });
    }

    handles.push({ el, dir });
    panel.appendChild(el);
  }

  /**
   * Convert centered / inset / transform-based layout into explicit fixed box.
   * Uses visual rect so transforms (e.g. translate(-50%,-50%)) are accounted for.
   */
  const pinGeometry = () => {
    const rect = panel.getBoundingClientRect();
    const s = panel.style;
    s.position = 'fixed';
    s.transform = 'none';
    try { s.webkitTransform = 'none'; } catch { /* ignore */ }
    // Do not set the `inset` shorthand after left/top — it resets all four sides.
    s.right = 'auto';
    s.bottom = 'auto';
    s.left = `${rect.left}px`;
    s.top = `${rect.top}px`;
    s.width = `${rect.width}px`;
    s.height = `${rect.height}px`;
    s.margin = '0';
    s.maxWidth = 'none';
    s.maxHeight = 'none';
    // Rect sizes include borders; border-box keeps the visual size stable.
    s.boxSizing = 'border-box';
    return {
      left: rect.left,
      top: rect.top,
      width: rect.width,
      height: rect.height
    };
  };

  const freezeIframes = () => {
    frozenIframes = [];
    try {
      const list = panel.querySelectorAll('iframe');
      list.forEach((iframe) => {
        frozenIframes.push(iframe);
        try {
          iframe.dataset.kpPrevPe = iframe.style.pointerEvents || '';
          iframe.style.pointerEvents = 'none';
        } catch { /* ignore */ }
      });
    } catch { /* ignore */ }
  };

  const unfreezeIframes = () => {
    for (const iframe of frozenIframes) {
      try {
        iframe.style.pointerEvents = iframe.dataset.kpPrevPe || '';
        delete iframe.dataset.kpPrevPe;
      } catch { /* ignore */ }
    }
    frozenIframes = [];
  };

  /**
   * @param {number} left
   * @param {number} top
   * @param {number} width
   * @param {number} height
   * @param {{ aspect?: number, anchorRight?: boolean, anchorBottom?: boolean }} [opts]
   */
  const clampRect = (left, top, width, height, opts = {}) => {
    const vw = doc.defaultView?.innerWidth ?? window.innerWidth;
    const vh = doc.defaultView?.innerHeight ?? window.innerHeight;
    const maxW = Math.max(minWidth, vw - margin * 2);
    const maxH = Math.max(minHeight, vh - margin * 2);
    const aspect = (typeof opts.aspect === 'number' && opts.aspect > 0) ? opts.aspect : null;

    let w = width;
    let h = height;
    let l = left;
    let t = top;

    if (aspect) {
      // Min box that satisfies both minWidth and minHeight at this ratio.
      const minWAspect = Math.max(minWidth, minHeight * aspect);
      const minHAspect = minWAspect / aspect;

      // Fit inside viewport while keeping ratio.
      let fitW = Math.min(Math.max(w, minWAspect), maxW);
      let fitH = fitW / aspect;
      if (fitH > maxH) {
        fitH = maxH;
        fitW = fitH * aspect;
      }
      if (fitW < minWAspect) {
        fitW = minWAspect;
        fitH = minHAspect;
      }
      // If still too tall after min width, re-fit to height.
      if (fitH > maxH) {
        fitH = Math.min(maxH, Math.max(minHAspect, maxH));
        fitW = fitH * aspect;
      }

      w = fitW;
      h = fitH;

      // Preserve the opposite corner when anchoring from W/N edges.
      if (opts.anchorRight) l = left + (width - w);
      if (opts.anchorBottom) t = top + (height - h);
    } else {
      w = Math.min(Math.max(width, minWidth), maxW);
      h = Math.min(Math.max(height, minHeight), maxH);
    }

    // Keep fully on-screen with margin
    if (l < margin) l = margin;
    if (t < margin) t = margin;
    if (l + w > vw - margin) l = Math.max(margin, vw - margin - w);
    if (t + h > vh - margin) t = Math.max(margin, vh - margin - h);

    // Re-clamp size if viewport is tiny (may break aspect slightly on tiny screens)
    if (!aspect) {
      w = Math.min(w, vw - margin - l);
      h = Math.min(h, vh - margin - t);
      w = Math.max(minWidth, w);
      h = Math.max(minHeight, h);
    } else {
      const availW = vw - margin - l;
      const availH = vh - margin - t;
      if (w > availW || h > availH) {
        const scale = Math.min(availW / w, availH / h, 1);
        w = Math.max(minWidth, w * scale);
        h = w / aspect;
      }
    }

    return { left: l, top: t, width: w, height: h };
  };

  const applyRect = (r) => {
    panel.style.left = `${r.left}px`;
    panel.style.top = `${r.top}px`;
    panel.style.width = `${r.width}px`;
    panel.style.height = `${r.height}px`;
    try {
      options.onResize?.(r);
    } catch { /* ignore */ }
  };

  const onPointerMove = (e) => {
    if (!drag) return;
    const dx = e.clientX - drag.startX;
    const dy = e.clientY - drag.startY;
    const { left: ol, top: ot, width: ow, height: oh } = drag.origin;
    const dir = drag.dir;
    const aspect = drag.aspect || null;

    let left = ol;
    let top = ot;
    let width = ow;
    let height = oh;

    if (aspect) {
      // Maintain width/height = aspect. Drive from the primary moved edge(s).
      const moveE = dir.includes('e');
      const moveW = dir.includes('w');
      const moveS = dir.includes('s');
      const moveN = dir.includes('n');
      const horizontal = moveE || moveW;
      const vertical = moveS || moveN;

      if (horizontal && vertical) {
        // Corner: use the axis with the larger relative change.
        const candidateW = moveE ? (ow + dx) : (ow - dx);
        const candidateH = moveS ? (oh + dy) : (oh - dy);
        const fromW = Math.abs(candidateW / ow);
        const fromH = Math.abs(candidateH / oh);
        if (fromW >= fromH) {
          width = candidateW;
          height = width / aspect;
        } else {
          height = candidateH;
          width = height * aspect;
        }
      } else if (horizontal) {
        width = moveE ? (ow + dx) : (ow - dx);
        height = width / aspect;
      } else {
        height = moveS ? (oh + dy) : (oh - dy);
        width = height * aspect;
      }

      // Anchor opposite edges so the non-dragged corner stays put.
      if (moveW) left = ol + (ow - width);
      if (moveN) top = ot + (oh - height);

      applyRect(clampRect(left, top, width, height, {
        aspect,
        anchorRight: moveW,
        anchorBottom: moveN
      }));
      return;
    }

    if (dir.includes('e')) width = ow + dx;
    if (dir.includes('s')) height = oh + dy;
    if (dir.includes('w')) {
      width = ow - dx;
      left = ol + dx;
      // If width hits min, freeze left edge
      if (width < minWidth) {
        width = minWidth;
        left = ol + ow - minWidth;
      }
    }
    if (dir.includes('n')) {
      height = oh - dy;
      top = ot + dy;
      if (height < minHeight) {
        height = minHeight;
        top = ot + oh - minHeight;
      }
    }

    applyRect(clampRect(left, top, width, height));
  };

  const endDrag = (e) => {
    if (!drag) return;
    const pointerId = drag.pointerId;
    const dir = drag.dir;
    drag = null;

    try {
      if (e && typeof e.pointerId === 'number') {
        // release on the handle if possible
      }
    } catch { /* ignore */ }

    unfreezeIframes();

    try {
      doc.body.style.cursor = prevBodyCursor;
      doc.body.style.userSelect = prevBodyUserSelect;
    } catch { /* ignore */ }

    doc.removeEventListener('pointermove', onPointerMove, true);
    doc.removeEventListener('pointerup', endDrag, true);
    doc.removeEventListener('pointercancel', endDrag, true);

    // Restore SE grip idle color
    const se = handles.find((h) => h.dir === 'se');
    if (se) se.el.style.color = 'rgba(255,255,255,0.45)';

    try {
      options.onResizeEnd?.();
    } catch { /* ignore */ }

    void pointerId;
    void dir;
  };

  const onPointerDown = (e, dir) => {
    if (e.pointerType === 'mouse' && e.button !== 0) return;
    e.preventDefault();
    e.stopPropagation();

    // Pin after caller onResizeStart may adjust geometry (e.g. bottom-dock → top/left).
    try {
      options.onResizeStart?.();
    } catch { /* ignore */ }

    const origin = pinGeometry();
    let aspect = fixedAspect;
    if (lockAspect && !aspect && origin.height > 0) {
      aspect = origin.width / origin.height;
    }
    drag = {
      dir,
      startX: e.clientX,
      startY: e.clientY,
      origin,
      pointerId: e.pointerId,
      aspect: aspect || undefined
    };

    freezeIframes();

    try {
      prevBodyCursor = doc.body.style.cursor || '';
      prevBodyUserSelect = doc.body.style.userSelect || '';
      const cursorMap = {
        n: 'n-resize', s: 's-resize', e: 'e-resize', w: 'w-resize',
        ne: 'ne-resize', nw: 'nw-resize', se: 'se-resize', sw: 'sw-resize'
      };
      doc.body.style.cursor = cursorMap[dir] || 'se-resize';
      doc.body.style.userSelect = 'none';
    } catch { /* ignore */ }

    doc.addEventListener('pointermove', onPointerMove, true);
    doc.addEventListener('pointerup', endDrag, true);
    doc.addEventListener('pointercancel', endDrag, true);

    try {
      e.currentTarget?.setPointerCapture?.(e.pointerId);
    } catch { /* ignore */ }
  };

  for (const { el, dir } of handles) {
    el.addEventListener('pointerdown', (e) => onPointerDown(e, dir));
  }

  const dispose = () => {
    endDrag();
    for (const { el } of handles) {
      try { el.remove(); } catch { /* ignore */ }
    }
    handles.length = 0;
    try {
      if (panel.dataset) delete panel.dataset.kpResizable;
    } catch { /* ignore */ }
    try {
      delete panel.__kpResizeApi;
    } catch { /* ignore */ }
  };

  try {
    panel.dataset.kpResizable = '1';
  } catch { /* ignore */ }

  const api = { dispose };
  try {
    panel.__kpResizeApi = api;
  } catch { /* ignore */ }

  return api;
}



  // Module: src/utils/panel-position.js
/**
 * Generalized fixed-panel positioning for KeyPilot UI chrome.
 *
 * Shared by the floating keyboard reference, control strip, and future panels:
 * - Viewport clamping with a consistent margin (panel never flush to the edge)
 * - Snap targets: 4 corners + center of each edge
 * - Anchor-based positions that re-resolve on resize / different viewports
 * - Free (left/top) positions that reclamp on apply
 * - Pointer drag with magnetic snap on release
 *
 * Position state is a plain object suitable for chrome.storage:
 *   { left?: number, top?: number, anchor?: PanelAnchor|null }
 */

/** Default inset from the viewport edge (matches existing keyboard drag clamp). */
const PANEL_POSITION_MARGIN_PX = 8;

/** Distance (px) within which release snaps to a corner / edge center. */
const PANEL_SNAP_THRESHOLD_PX = 56;

/** Ignore tiny pointer jitter before treating a gesture as a drag. */
const PANEL_DRAG_MOVE_THRESHOLD_PX = 3;

/**
 * Named snap / dock anchors.
 * @typedef {'top-left'|'top-center'|'top-right'|'middle-left'|'middle-right'|'bottom-left'|'bottom-center'|'bottom-right'} PanelAnchor
 */

/** @type {readonly PanelAnchor[]} */
const PANEL_ANCHORS = Object.freeze([
  'top-left',
  'top-center',
  'top-right',
  'middle-left',
  'middle-right',
  'bottom-left',
  'bottom-center',
  'bottom-right'
]);

/**
 * @typedef {{
 *   left?: number,
 *   top?: number,
 *   anchor?: PanelAnchor|string|null
 * }} PanelPositionState
 */

/**
 * @typedef {{
 *   left: number,
 *   top: number,
 *   width: number,
 *   height: number,
 *   anchor?: PanelAnchor|null
 * }} PanelBox
 */

/**
 * @param {any} raw
 * @returns {PanelAnchor|null}
 */
function normalizePanelAnchor(raw) {
  if (typeof raw !== 'string') return null;
  const a = raw.trim();
  return PANEL_ANCHORS.includes(/** @type {PanelAnchor} */ (a))
    ? /** @type {PanelAnchor} */ (a)
    : null;
}

/**
 * @param {any} raw
 * @param {PanelPositionState|null} [fallback]
 * @returns {PanelPositionState|null}
 */
function normalizePanelPositionState(raw, fallback = null) {
  const fb = fallback && typeof fallback === 'object' ? fallback : null;
  if (!raw || typeof raw !== 'object') {
    return fb
      ? {
          left: Number.isFinite(fb.left) ? fb.left : undefined,
          top: Number.isFinite(fb.top) ? fb.top : undefined,
          anchor: normalizePanelAnchor(fb.anchor)
        }
      : null;
  }
  const left = Number(raw.left);
  const top = Number(raw.top);
  const anchor = normalizePanelAnchor(raw.anchor);
  const hasLeft = Number.isFinite(left);
  const hasTop = Number.isFinite(top);
  if (!anchor && !hasLeft && !hasTop) {
    return fb
      ? {
          left: Number.isFinite(fb.left) ? fb.left : undefined,
          top: Number.isFinite(fb.top) ? fb.top : undefined,
          anchor: normalizePanelAnchor(fb.anchor)
        }
      : null;
  }
  /** @type {PanelPositionState} */
  const out = {};
  if (hasLeft) out.left = left;
  if (hasTop) out.top = top;
  if (anchor) out.anchor = anchor;
  else if (raw.anchor === null) out.anchor = null;
  return out;
}

/**
 * @param {{ width?: number, height?: number }|null} [opts]
 * @returns {{ width: number, height: number }}
 */
function getViewportSize(opts = null) {
  // Prefer clientWidth/Height (layout viewport, excludes classic scrollbars) so
  // right/bottom clamps match the visible page edge — not the scrollbar gutter.
  let fallbackW = 0;
  let fallbackH = 0;
  if (typeof window !== 'undefined') {
    try {
      const de = document.documentElement;
      // Prefer the larger of client/inner — client can be 0 at document_start.
      fallbackW = Math.max(de?.clientWidth || 0, window.innerWidth || 0);
      fallbackH = Math.max(de?.clientHeight || 0, window.innerHeight || 0);
    } catch {
      fallbackW = window.innerWidth || 0;
      fallbackH = window.innerHeight || 0;
    }
  }
  const w = opts && Number.isFinite(opts.width) ? opts.width : fallbackW;
  const h = opts && Number.isFinite(opts.height) ? opts.height : fallbackH;
  return {
    width: Math.max(0, Math.round(w) || 0),
    height: Math.max(0, Math.round(h) || 0)
  };
}

/**
 * Clamp a top-left position so the panel stays fully inside the viewport
 * with `margin` padding on every side (never flush to the edge / off-screen).
 *
 * @param {{
 *   left: number,
 *   top: number,
 *   width: number,
 *   height: number,
 *   margin?: number,
 *   viewportWidth?: number,
 *   viewportHeight?: number
 * }} args
 * @returns {{ left: number, top: number }}
 */
function clampPanelPosition(args) {
  const margin = Math.max(0, Number(args.margin));
  const m = Number.isFinite(margin) ? margin : PANEL_POSITION_MARGIN_PX;
  const vp = getViewportSize({
    width: args.viewportWidth,
    height: args.viewportHeight
  });
  const w = Math.max(0, Number(args.width) || 0);
  const h = Math.max(0, Number(args.height) || 0);
  const maxLeft = Math.max(m, vp.width - w - m);
  const maxTop = Math.max(m, vp.height - h - m);
  const left = Number(args.left);
  const top = Number(args.top);
  return {
    left: Math.max(m, Math.min(Number.isFinite(left) ? left : m, maxLeft)),
    top: Math.max(m, Math.min(Number.isFinite(top) ? top : m, maxTop))
  };
}

/**
 * Pixel position for a named anchor given panel size + viewport.
 *
 * @param {PanelAnchor|string|null|undefined} anchor
 * @param {{
 *   width: number,
 *   height: number,
 *   margin?: number,
 *   viewportWidth?: number,
 *   viewportHeight?: number
 * }} size
 * @returns {{ left: number, top: number, anchor: PanelAnchor|null }}
 */
function positionForAnchor(anchor, size) {
  const a = normalizePanelAnchor(anchor);
  const margin = Math.max(0, Number.isFinite(Number(size.margin)) ? Number(size.margin) : PANEL_POSITION_MARGIN_PX);
  const vp = getViewportSize({
    width: size.viewportWidth,
    height: size.viewportHeight
  });
  const w = Math.max(0, Number(size.width) || 0);
  const h = Math.max(0, Number(size.height) || 0);

  const leftMin = margin;
  const topMin = margin;
  const leftMax = Math.max(margin, vp.width - w - margin);
  const topMax = Math.max(margin, vp.height - h - margin);
  const leftCenter = Math.round((vp.width - w) / 2);
  const topCenter = Math.round((vp.height - h) / 2);

  let left = leftMin;
  let top = topMin;

  switch (a) {
    case 'top-left':
      left = leftMin;
      top = topMin;
      break;
    case 'top-center':
      left = leftCenter;
      top = topMin;
      break;
    case 'top-right':
      left = leftMax;
      top = topMin;
      break;
    case 'middle-left':
      left = leftMin;
      top = topCenter;
      break;
    case 'middle-right':
      left = leftMax;
      top = topCenter;
      break;
    case 'bottom-left':
      left = leftMin;
      top = topMax;
      break;
    case 'bottom-center':
      left = leftCenter;
      top = topMax;
      break;
    case 'bottom-right':
      left = leftMax;
      top = topMax;
      break;
    default:
      left = leftMin;
      top = topMin;
      return { left, top, anchor: null };
  }

  const clamped = clampPanelPosition({
    left,
    top,
    width: w,
    height: h,
    margin,
    viewportWidth: vp.width,
    viewportHeight: vp.height
  });
  return { left: clamped.left, top: clamped.top, anchor: a };
}

/**
 * All snap targets (corners + edge centers) for the current viewport/size.
 *
 * @param {{
 *   width: number,
 *   height: number,
 *   margin?: number,
 *   viewportWidth?: number,
 *   viewportHeight?: number
 * }} size
 * @returns {Array<{ anchor: PanelAnchor, left: number, top: number }>}
 */
function getPanelSnapTargets(size) {
  /** @type {Array<{ anchor: PanelAnchor, left: number, top: number }>} */
  const out = [];
  for (const anchor of PANEL_ANCHORS) {
    const p = positionForAnchor(anchor, size);
    if (p.anchor) {
      out.push({ anchor: p.anchor, left: p.left, top: p.top });
    }
  }
  return out;
}

/**
 * Nearest snap target within threshold, or null if none close enough.
 *
 * @param {{
 *   left: number,
 *   top: number,
 *   width: number,
 *   height: number,
 *   margin?: number,
 *   threshold?: number,
 *   viewportWidth?: number,
 *   viewportHeight?: number
 * }} args
 * @returns {{ anchor: PanelAnchor, left: number, top: number, distance: number }|null}
 */
function findNearestPanelSnap(args) {
  const threshold = Math.max(
    0,
    Number.isFinite(Number(args.threshold)) ? Number(args.threshold) : PANEL_SNAP_THRESHOLD_PX
  );
  const targets = getPanelSnapTargets(args);
  let best = null;
  for (const t of targets) {
    const dx = t.left - args.left;
    const dy = t.top - args.top;
    const d = Math.hypot(dx, dy);
    if (d <= threshold && (!best || d < best.distance)) {
      best = { ...t, distance: d };
    }
  }
  return best;
}

/**
 * Resolve a stored position into concrete left/top (and optional anchor).
 * Prefers anchor when present so layout adapts across viewport sizes.
 *
 * @param {PanelPositionState|null|undefined} stored
 * @param {{
 *   width: number,
 *   height: number,
 *   margin?: number,
 *   defaultAnchor?: PanelAnchor|string|null,
 *   viewportWidth?: number,
 *   viewportHeight?: number
 * }} size
 * @returns {{ left: number, top: number, anchor: PanelAnchor|null }}
 */
function resolvePanelPosition(stored, size) {
  const margin = Math.max(0, Number.isFinite(Number(size.margin)) ? Number(size.margin) : PANEL_POSITION_MARGIN_PX);
  const w = Math.max(0, Number(size.width) || 0);
  const h = Math.max(0, Number(size.height) || 0);
  const state = normalizePanelPositionState(stored);
  const defaultAnchor = normalizePanelAnchor(size.defaultAnchor);

  if (state?.anchor) {
    return positionForAnchor(state.anchor, {
      width: w,
      height: h,
      margin,
      viewportWidth: size.viewportWidth,
      viewportHeight: size.viewportHeight
    });
  }

  if (state && Number.isFinite(state.left) && Number.isFinite(state.top)) {
    const clamped = clampPanelPosition({
      left: /** @type {number} */ (state.left),
      top: /** @type {number} */ (state.top),
      width: w,
      height: h,
      margin,
      viewportWidth: size.viewportWidth,
      viewportHeight: size.viewportHeight
    });
    return { left: clamped.left, top: clamped.top, anchor: null };
  }

  if (defaultAnchor) {
    return positionForAnchor(defaultAnchor, {
      width: w,
      height: h,
      margin,
      viewportWidth: size.viewportWidth,
      viewportHeight: size.viewportHeight
    });
  }

  const clamped = clampPanelPosition({
    left: margin,
    top: margin,
    width: w,
    height: h,
    margin,
    viewportWidth: size.viewportWidth,
    viewportHeight: size.viewportHeight
  });
  return { left: clamped.left, top: clamped.top, anchor: null };
}

/**
 * Convert bottom/right/centered layout into an explicit fixed top/left box
 * so free drag can pin the panel anywhere.
 *
 * @param {HTMLElement} panel
 * @param {{ pinWidth?: boolean, pinHeight?: boolean }} [opts]
 * @returns {PanelBox}
 */
function pinPanelGeometry(panel, opts = {}) {
  const pinWidth = opts.pinWidth !== false;
  const pinHeight = opts.pinHeight === true;
  const rect = panel.getBoundingClientRect();
  const s = panel.style;
  s.position = 'fixed';
  s.transform = 'none';
  try { s.webkitTransform = 'none'; } catch { /* ignore */ }
  // Never set the `inset` shorthand after left/top — it resets all four sides to auto.
  s.right = 'auto';
  s.bottom = 'auto';
  s.left = `${rect.left}px`;
  s.top = `${rect.top}px`;
  if (pinWidth) {
    s.width = `${rect.width}px`;
  }
  // Only force height when requested (most strips/panels size height via CSS).
  if (pinHeight) {
    s.height = `${rect.height}px`;
  }
  s.margin = '0';
  s.boxSizing = 'border-box';
  return {
    left: rect.left,
    top: rect.top,
    width: rect.width,
    height: rect.height
  };
}

/**
 * Measure a panel's layout box. Prefer offset* (layout) over getBoundingClientRect
 * so a display:none / not-yet-laid-out pass can still fall back to scroll size.
 * @param {HTMLElement} panel
 * @returns {{ width: number, height: number }}
 */
function measurePanelSize(panel) {
  let width = 0;
  let height = 0;
  try {
    width = panel.offsetWidth || 0;
    height = panel.offsetHeight || 0;
  } catch { /* ignore */ }
  if (width <= 0 || height <= 0) {
    try {
      const rect = panel.getBoundingClientRect();
      if (width <= 0) width = rect.width || 0;
      if (height <= 0) height = rect.height || 0;
    } catch { /* ignore */ }
  }
  // Content-sized fallback when the box is still collapsing (common before keyboard paint).
  if (width <= 0) {
    try { width = panel.scrollWidth || 0; } catch { /* ignore */ }
  }
  if (height <= 0) {
    try { height = panel.scrollHeight || 0; } catch { /* ignore */ }
  }
  return { width: Math.max(0, width), height: Math.max(0, height) };
}

/**
 * Write fixed left/top onto the panel (never use the `inset` shorthand afterward).
 * @param {HTMLElement} panel
 * @param {{ left: number, top: number, anchor?: PanelAnchor|null }} resolved
 * @param {{ pinSize?: boolean, width?: number, height?: number }} [opts]
 */
function writePanelPosition(panel, resolved, opts = {}) {
  const s = panel.style;
  s.position = 'fixed';
  s.transform = 'none';
  try { s.webkitTransform = 'none'; } catch { /* ignore */ }
  s.right = 'auto';
  s.bottom = 'auto';
  s.left = `${Math.round(resolved.left)}px`;
  s.top = `${Math.round(resolved.top)}px`;
  s.margin = '0';
  if (opts.pinSize) {
    if (opts.width > 0) s.width = `${Math.round(opts.width)}px`;
    if (opts.height > 0) s.height = `${Math.round(opts.height)}px`;
    s.maxWidth = 'none';
    s.maxHeight = 'none';
  }
  try {
    if (resolved.anchor) panel.setAttribute('data-kp-panel-anchor', resolved.anchor);
    else panel.removeAttribute('data-kp-panel-anchor');
  } catch { /* ignore */ }
}

/**
 * Apply a resolved (or stored) position to a fixed panel element.
 *
 * Always remeasures after writing and reclamps so a first pass with height=0
 * (pre-layout) cannot leave the panel hanging below the viewport once content paints.
 *
 * @param {HTMLElement} panel
 * @param {PanelPositionState|{ left: number, top: number, anchor?: PanelAnchor|null }|null|undefined} position
 * @param {{
 *   margin?: number,
 *   defaultAnchor?: PanelAnchor|string|null,
 *   pinSize?: boolean,
 *   width?: number,
 *   height?: number,
 *   fallbackWidth?: number,
 *   fallbackHeight?: number
 * }} [options]
 * @returns {{ left: number, top: number, anchor: PanelAnchor|null }}
 */
function applyPanelPosition(panel, position, options = {}) {
  if (!panel || !panel.style) {
    return { left: 0, top: 0, anchor: null };
  }

  const measured = measurePanelSize(panel);
  let width = Number.isFinite(options.width) ? /** @type {number} */ (options.width) : measured.width;
  let height = Number.isFinite(options.height) ? /** @type {number} */ (options.height) : measured.height;
  const margin = Math.max(0, Number.isFinite(Number(options.margin)) ? Number(options.margin) : PANEL_POSITION_MARGIN_PX);

  // Unlaid-out panels report 0×0. Using height 0 makes maxTop ≈ vh − margin, so a free
  // top (or bottom-* anchor) lands at the bottom edge and then grows off-screen.
  // Prefer explicit fallbacks / conservative estimates for the first clamp pass.
  const fallbackW = Number.isFinite(options.fallbackWidth) ? options.fallbackWidth : 0;
  const fallbackH = Number.isFinite(options.fallbackHeight) ? options.fallbackHeight : 0;
  if (width < 32) width = fallbackW > 0 ? fallbackW : 320;
  if (height < 32) height = fallbackH > 0 ? fallbackH : 160;

  let resolved = resolvePanelPosition(position, {
    width,
    height,
    margin,
    defaultAnchor: options.defaultAnchor
  });

  writePanelPosition(panel, resolved, {
    pinSize: options.pinSize,
    width,
    height
  });

  // Second pass: live geometry after styles apply. Fixes free positions that were
  // saved/applied while the panel still had height 0 (top ≈ vh − margin → offscreen).
  try {
    const live = measurePanelSize(panel);
    const liveW = live.width || width;
    const liveH = live.height || height;
    if (liveW > 0 && liveH > 0) {
      const state = normalizePanelPositionState(position);
      const anchor = normalizePanelAnchor(state?.anchor) || normalizePanelAnchor(resolved.anchor);
      if (anchor) {
        resolved = positionForAnchor(anchor, {
          width: liveW,
          height: liveH,
          margin
        });
      } else {
        const left = Number.isFinite(resolved.left) ? resolved.left : margin;
        const top = Number.isFinite(resolved.top) ? resolved.top : margin;
        const clamped = clampPanelPosition({
          left,
          top,
          width: liveW,
          height: liveH,
          margin
        });
        resolved = { left: clamped.left, top: clamped.top, anchor: null };
      }
      writePanelPosition(panel, resolved, {
        pinSize: options.pinSize,
        width: liveW,
        height: liveH
      });

      // Final safety: if the painted rect still spills past the viewport, pin it.
      const painted = panel.getBoundingClientRect();
      const vp = getViewportSize();
      const spills =
        painted.bottom > vp.height - margin + 1 ||
        painted.right > vp.width - margin + 1 ||
        painted.top < margin - 1 ||
        painted.left < margin - 1;
      if (spills && painted.width > 0 && painted.height > 0) {
        const fixed = clampPanelPosition({
          left: painted.left,
          top: painted.top,
          width: painted.width,
          height: painted.height,
          margin
        });
        resolved = {
          left: fixed.left,
          top: fixed.top,
          anchor: anchor || null
        };
        // If we had to shove a free position, drop the stale anchor so next apply uses left/top.
        if (!anchor) resolved.anchor = null;
        writePanelPosition(panel, resolved);
      }
    }
  } catch { /* ignore */ }

  return resolved;
}

/**
 * Build a storable position after a drag ends (optional magnetic snap).
 *
 * @param {{
 *   left: number,
 *   top: number,
 *   width: number,
 *   height: number,
 *   margin?: number,
 *   snapThreshold?: number,
 *   snap?: boolean
 * }} args
 * @returns {PanelPositionState & { left: number, top: number }}
 */
function finalizePanelDragPosition(args) {
  const margin = Math.max(0, Number.isFinite(Number(args.margin)) ? Number(args.margin) : PANEL_POSITION_MARGIN_PX);
  const snapEnabled = args.snap !== false;
  const clamped = clampPanelPosition({
    left: args.left,
    top: args.top,
    width: args.width,
    height: args.height,
    margin
  });

  if (snapEnabled) {
    const snap = findNearestPanelSnap({
      left: clamped.left,
      top: clamped.top,
      width: args.width,
      height: args.height,
      margin,
      threshold: args.snapThreshold
    });
    if (snap) {
      return {
        left: Math.round(snap.left),
        top: Math.round(snap.top),
        anchor: snap.anchor
      };
    }
  }

  return {
    left: Math.round(clamped.left),
    top: Math.round(clamped.top),
    anchor: null
  };
}

/**
 * Make `handle` drag `panel` with viewport margin clamp + optional snap on release.
 *
 * @param {HTMLElement} panel
 * @param {HTMLElement} handle
 * @param {{
 *   margin?: number,
 *   snapThreshold?: number,
 *   snap?: boolean,
 *   moveThresholdPx?: number,
 *   excludeSelector?: string,
 *   pinWidth?: boolean,
 *   pinHeight?: boolean,
 *   cursorGrab?: string,
 *   cursorGrabbing?: string,
 *   onMoveStart?: () => void,
 *   onMove?: (box: { left: number, top: number }) => void,
 *   onMoveEnd?: (state: PanelPositionState & { left: number, top: number, moved: boolean }) => void
 * }} [options]
 * @returns {{ dispose: () => void }|null}
 */
function makePanelDraggable(panel, handle, options = {}) {
  if (!panel || !(panel instanceof Element) || !handle || !(handle instanceof Element)) {
    return null;
  }

  // Avoid double-attaching on the same handle.
  try {
    if (handle.dataset && handle.dataset.kpPanelDraggable === '1') {
      return handle.__kpPanelDragApi || null;
    }
  } catch { /* ignore */ }

  const margin = Math.max(0, Number.isFinite(Number(options.margin)) ? Number(options.margin) : PANEL_POSITION_MARGIN_PX);
  const snapThreshold = Math.max(
    0,
    Number.isFinite(Number(options.snapThreshold)) ? Number(options.snapThreshold) : PANEL_SNAP_THRESHOLD_PX
  );
  const moveThreshold = Math.max(
    0,
    Number.isFinite(Number(options.moveThresholdPx)) ? Number(options.moveThresholdPx) : PANEL_DRAG_MOVE_THRESHOLD_PX
  );
  const snapEnabled = options.snap !== false;
  const pinWidth = options.pinWidth !== false;
  const pinHeight = options.pinHeight === true;
  const excludeSelector = typeof options.excludeSelector === 'string' && options.excludeSelector
    ? options.excludeSelector
    : '';
  const cursorGrab = options.cursorGrab || 'grab';
  const cursorGrabbing = options.cursorGrabbing || 'grabbing';

  /** @type {{ startX: number, startY: number, originLeft: number, originTop: number, pointerId: number, moved: boolean }|null} */
  let dragState = null;

  const measure = () => {
    const rect = panel.getBoundingClientRect();
    return {
      width: panel.offsetWidth || rect.width || 0,
      height: panel.offsetHeight || rect.height || 0
    };
  };

  const onPointerMove = (e) => {
    if (!dragState || !panel.isConnected) return;
    const dx = e.clientX - dragState.startX;
    const dy = e.clientY - dragState.startY;
    if (!dragState.moved) {
      if (Math.abs(dx) < moveThreshold && Math.abs(dy) < moveThreshold) return;
      dragState.moved = true;
      try { handle.style.cursor = cursorGrabbing; } catch { /* ignore */ }
      try { options.onMoveStart?.(); } catch { /* ignore */ }
    }
    const size = measure();
    const next = clampPanelPosition({
      left: dragState.originLeft + dx,
      top: dragState.originTop + dy,
      width: size.width,
      height: size.height,
      margin
    });
    panel.style.left = `${next.left}px`;
    panel.style.top = `${next.top}px`;
    panel.style.right = 'auto';
    panel.style.bottom = 'auto';
    try { panel.removeAttribute('data-kp-panel-anchor'); } catch { /* ignore */ }
    try { options.onMove?.(next); } catch { /* ignore */ }
  };

  const endDrag = (e) => {
    if (!dragState) return;
    const state = dragState;
    const pointerId = state.pointerId;
    dragState = null;

    try { handle.style.cursor = cursorGrab; } catch { /* ignore */ }
    try {
      if (e && typeof e.pointerId === 'number') handle.releasePointerCapture(e.pointerId);
      else if (typeof pointerId === 'number') handle.releasePointerCapture(pointerId);
    } catch { /* ignore */ }

    document.removeEventListener('pointermove', onPointerMove, true);
    document.removeEventListener('pointerup', endDrag, true);
    document.removeEventListener('pointercancel', endDrag, true);

    if (!panel.isConnected) {
      try {
        options.onMoveEnd?.({ left: state.originLeft, top: state.originTop, anchor: null, moved: false });
      } catch { /* ignore */ }
      return;
    }

    const size = measure();
    const rect = panel.getBoundingClientRect();
    const finalPos = state.moved
      ? finalizePanelDragPosition({
          left: rect.left,
          top: rect.top,
          width: size.width,
          height: size.height,
          margin,
          snapThreshold,
          snap: snapEnabled
        })
      : {
          left: Math.round(rect.left),
          top: Math.round(rect.top),
          anchor: normalizePanelAnchor(panel.getAttribute?.('data-kp-panel-anchor'))
        };

    if (state.moved) {
      applyPanelPosition(panel, finalPos, { margin, pinSize: false });
    }

    try {
      options.onMoveEnd?.({
        left: finalPos.left,
        top: finalPos.top,
        anchor: finalPos.anchor ?? null,
        moved: !!state.moved
      });
    } catch { /* ignore */ }
  };

  const onPointerDown = (e) => {
    if (!panel.isConnected) return;
    if (e.pointerType === 'mouse' && e.button !== 0) return;

    if (excludeSelector) {
      try {
        const t = /** @type {Element|null} */ (e.target instanceof Element ? e.target : e.target?.parentElement);
        if (t?.closest?.(excludeSelector)) return;
      } catch { /* ignore */ }
    }

    e.preventDefault();
    e.stopPropagation();

    const origin = pinPanelGeometry(panel, { pinWidth, pinHeight });
    dragState = {
      startX: e.clientX,
      startY: e.clientY,
      originLeft: origin.left,
      originTop: origin.top,
      pointerId: e.pointerId,
      moved: false
    };

    try { handle.setPointerCapture(e.pointerId); } catch { /* ignore */ }
    document.addEventListener('pointermove', onPointerMove, true);
    document.addEventListener('pointerup', endDrag, true);
    document.addEventListener('pointercancel', endDrag, true);
  };

  try {
    handle.style.cursor = handle.style.cursor || cursorGrab;
    handle.style.touchAction = handle.style.touchAction || 'none';
    handle.style.userSelect = handle.style.userSelect || 'none';
  } catch { /* ignore */ }

  handle.addEventListener('pointerdown', onPointerDown);

  const api = {
    dispose: () => {
      endDrag();
      try { handle.removeEventListener('pointerdown', onPointerDown); } catch { /* ignore */ }
      try {
        if (handle.dataset) delete handle.dataset.kpPanelDraggable;
      } catch { /* ignore */ }
      try { delete handle.__kpPanelDragApi; } catch { /* ignore */ }
    }
  };

  try {
    handle.dataset.kpPanelDraggable = '1';
    handle.__kpPanelDragApi = api;
  } catch { /* ignore */ }

  return api;
}



  // Module: src/utils/scroll-at-point.js
/**
 * Cursor-aware keyboard scrolling.
 *
 * C / V (and callers that reuse this helper) should scroll the nearest overflow
 * container under the pointer first; only if nothing nested can scroll do we
 * fall back to the document / window. Horizontal-only overflow maps C→left and
 * V→right; mixed or vertical-only overflow uses up/down.
 */

/** Pixels of slack when testing whether an edge still has room to scroll. */
const EDGE_EPS = 1;

/**
 * Composed parent: light DOM parent, or open shadow host when crossing a root.
 * @param {Node|null|undefined} node
 * @returns {Element|null}
 */
function composedParent(node) {
  if (!node || node.nodeType !== 1) return null;
  const el = /** @type {Element} */ (node);
  if (el.parentElement) return el.parentElement;
  try {
    const root = typeof el.getRootNode === 'function' ? el.getRootNode() : null;
    if (root && typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot) {
      return root.host || null;
    }
  } catch { /* ignore */ }
  return null;
}

/**
 * Shadow-piercing elementFromPoint (does not enter iframes).
 * @param {number} x
 * @param {number} y
 * @param {Document} [doc]
 * @returns {Element|null}
 */
function elementFromPointDeep(x, y, doc = document) {
  try {
    let el = doc.elementFromPoint(x, y);
    let guard = 0;
    while (el && el.shadowRoot && guard++ < 10) {
      const nested = el.shadowRoot.elementFromPoint(x, y);
      if (!nested || nested === el) break;
      el = nested;
    }
    return el || null;
  } catch {
    return null;
  }
}

/**
 * @param {string|null|undefined} overflow
 * @returns {boolean}
 */
function overflowAllowsScroll(overflow) {
  const o = String(overflow || '').toLowerCase();
  return o === 'auto' || o === 'scroll' || o === 'overlay';
}

/**
 * True when `el` is the document scrolling root (html / body / scrollingElement).
 * These often report overflow:visible yet still scroll the viewport.
 * @param {Element} el
 * @param {Document} doc
 * @returns {boolean}
 */
function isDocumentScrollRoot(el, doc) {
  try {
    const se = doc.scrollingElement;
    if (se && el === se) return true;
    if (el === doc.documentElement || el === doc.body) return true;
  } catch { /* ignore */ }
  return false;
}

/**
 * @typedef {{ canY: boolean, canX: boolean, maxTop: number, maxLeft: number }} ScrollCapacity
 */

/**
 * @param {Element} el
 * @param {Document} doc
 * @returns {ScrollCapacity}
 */
function getScrollCapacity(el, doc = document) {
  if (!el || el.nodeType !== 1) {
    return { canY: false, canX: false, maxTop: 0, maxLeft: 0 };
  }

  let maxTop = 0;
  let maxLeft = 0;
  try {
    maxTop = Math.max(0, (el.scrollHeight || 0) - (el.clientHeight || 0));
    maxLeft = Math.max(0, (el.scrollWidth || 0) - (el.clientWidth || 0));
  } catch {
    return { canY: false, canX: false, maxTop: 0, maxLeft: 0 };
  }

  // Document roots: treat as scrollable when content overflows even if CSS
  // overflow is visible (browser default viewport scrolling).
  if (isDocumentScrollRoot(el, doc)) {
    return {
      canY: maxTop > EDGE_EPS,
      canX: maxLeft > EDGE_EPS,
      maxTop,
      maxLeft
    };
  }

  let oy = '';
  let ox = '';
  try {
    const cs = (el.ownerDocument?.defaultView || window).getComputedStyle(el);
    oy = cs?.overflowY || '';
    ox = cs?.overflowX || '';
  } catch {
    return { canY: false, canX: false, maxTop: 0, maxLeft: 0 };
  }

  return {
    canY: overflowAllowsScroll(oy) && maxTop > EDGE_EPS,
    canX: overflowAllowsScroll(ox) && maxLeft > EDGE_EPS,
    maxTop,
    maxLeft
  };
}

/**
 * @param {Element} el
 * @param {'y'|'x'} axis
 * @param {number} sign  -1 = up/left, +1 = down/right
 * @returns {boolean}
 */
function canScrollInDirection(el, axis, sign) {
  if (!el) return false;
  try {
    if (axis === 'y') {
      const top = el.scrollTop || 0;
      if (sign < 0) return top > EDGE_EPS;
      const max = Math.max(0, (el.scrollHeight || 0) - (el.clientHeight || 0));
      return top < max - EDGE_EPS;
    }
    const left = el.scrollLeft || 0;
    if (sign < 0) return left > EDGE_EPS;
    const max = Math.max(0, (el.scrollWidth || 0) - (el.clientWidth || 0));
    return left < max - EDGE_EPS;
  } catch {
    return false;
  }
}

/**
 * Apply a delta to an element (or window for document roots).
 * @param {Element} el
 * @param {number} deltaX
 * @param {number} deltaY
 * @param {ScrollBehavior} [behavior]
 * @param {Document} [doc]
 * @param {Window} [win]
 * @returns {boolean}
 */
function scrollElementBy(el, deltaX, deltaY, behavior = 'smooth', doc = document, win = window) {
  if (!el) return false;
  const dx = Number(deltaX) || 0;
  const dy = Number(deltaY) || 0;
  if (!dx && !dy) return false;

  const opts = { left: dx, top: dy, behavior };

  // Prefer element.scrollBy; fall back to mutating scrollTop/scrollLeft.
  try {
    if (typeof el.scrollBy === 'function') {
      el.scrollBy(opts);
      return true;
    }
  } catch { /* fall through */ }

  try {
    if (behavior === 'smooth' && typeof el.scrollTo === 'function') {
      el.scrollTo({
        left: (el.scrollLeft || 0) + dx,
        top: (el.scrollTop || 0) + dy,
        behavior
      });
      return true;
    }
  } catch { /* fall through */ }

  try {
    if (dx) el.scrollLeft = (el.scrollLeft || 0) + dx;
    if (dy) el.scrollTop = (el.scrollTop || 0) + dy;
    return true;
  } catch { /* ignore */ }

  // Last resort for document roots.
  if (isDocumentScrollRoot(el, doc) && win && typeof win.scrollBy === 'function') {
    try {
      win.scrollBy(opts);
      return true;
    } catch {
      try {
        win.scrollBy(dx, dy);
        return true;
      } catch { /* ignore */ }
    }
  }

  return false;
}

/**
 * Resolve which axis to use for a capacity snapshot.
 * Prefer vertical when it can move in `sign`; else horizontal when it can.
 * @param {ScrollCapacity} cap
 * @param {Element} el
 * @param {number} sign
 * @returns {'y'|'x'|null}
 */
function pickAxis(cap, el, sign) {
  if (cap.canY && canScrollInDirection(el, 'y', sign)) return 'y';
  if (cap.canX && canScrollInDirection(el, 'x', sign)) return 'x';
  return null;
}

/**
 * Find the best scroll target under a viewport point.
 *
 * @param {number} clientX
 * @param {number} clientY
 * @param {number} sign  -1 = up/left (C), +1 = down/right (V)
 * @param {{ doc?: Document, win?: Window }} [ctx]
 * @returns {{ el: Element, axis: 'y'|'x' }|null}
 */
function findScrollTargetAtPoint(clientX, clientY, sign, ctx = {}) {
  const doc = ctx.doc || document;
  const x = Number(clientX);
  const y = Number(clientY);
  if (!Number.isFinite(x) || !Number.isFinite(y)) return null;

  let start = elementFromPointDeep(x, y, doc);
  // If hit-testing failed (e.g. over nothing), still allow document scroll.
  if (!start) {
    const se = doc.scrollingElement || doc.documentElement || doc.body;
    if (se) {
      const cap = getScrollCapacity(se, doc);
      const axis = pickAxis(cap, se, sign);
      if (axis) return { el: se, axis };
    }
    return null;
  }

  // Skip non-element / text nodes.
  if (start.nodeType !== 1) {
    start = start.parentElement || /** @type {Element|null} */ (composedParent(start));
  }

  /** @type {Element|null} */
  let n = /** @type {Element|null} */ (start);
  let depth = 0;
  /** @type {Element|null} */
  let seenDocRoot = null;

  while (n && n.nodeType === 1 && depth++ < 64) {
    if (n.tagName === 'IFRAME' || n.tagName === 'FRAME') {
      // Caller handles iframe forwarding; do not treat the shell as a scroller.
      return null;
    }

    // Skip KeyPilot chrome (ids/classes) so we don't scroll our own overlays.
    try {
      const id = n.id || '';
      if (id === 'kpv2-cursor' || id === 'kpv2-frame-hover' || (typeof id === 'string' && id.startsWith('kpv2-'))) {
        n = composedParent(n);
        continue;
      }
      if (n.classList) {
        let skip = false;
        n.classList.forEach((c) => {
          if (typeof c === 'string' && c.startsWith('kpv2-')) skip = true;
        });
        if (skip) {
          n = composedParent(n);
          continue;
        }
      }
    } catch { /* ignore */ }

    const cap = getScrollCapacity(n, doc);
    if (cap.canY || cap.canX) {
      if (isDocumentScrollRoot(n, doc)) {
        seenDocRoot = n;
        // Keep walking? Document roots are usually outermost — try them last
        // only after nested candidates fail. Continue so nested is preferred
        // when we somehow start above them; normally we hit nested first.
        n = composedParent(n);
        continue;
      }
      const axis = pickAxis(cap, n, sign);
      if (axis) return { el: n, axis };
    }

    n = composedParent(n);
  }

  // Fallback: document scrolling element / html / body / window.
  const candidates = [];
  try {
    if (doc.scrollingElement) candidates.push(doc.scrollingElement);
  } catch { /* ignore */ }
  try {
    if (doc.documentElement) candidates.push(doc.documentElement);
  } catch { /* ignore */ }
  try {
    if (doc.body) candidates.push(doc.body);
  } catch { /* ignore */ }
  if (seenDocRoot) candidates.push(seenDocRoot);

  const tried = new Set();
  for (const el of candidates) {
    if (!el || tried.has(el)) continue;
    tried.add(el);
    const cap = getScrollCapacity(el, doc);
    const axis = pickAxis(cap, el, sign);
    if (axis) return { el, axis };
  }

  return null;
}

/**
 * Scroll under the cursor: nested overflow first, then the page.
 *
 * @param {number} clientX
 * @param {number} clientY
 * @param {number} sign  -1 = C (up/left), +1 = V (down/right)
 * @param {number} deltaPx  positive distance in the chosen direction
 * @param {ScrollBehavior} [behavior]
 * @param {{ doc?: Document, win?: Window }} [ctx]
 * @returns {{ scrolled: boolean, axis: 'y'|'x'|null, el: Element|null }}
 */
function scrollAtPoint(clientX, clientY, sign, deltaPx, behavior = 'smooth', ctx = {}) {
  const doc = ctx.doc || document;
  const win = ctx.win || (doc.defaultView || window);
  const amount = Math.abs(Number(deltaPx)) || 0;
  const s = sign < 0 ? -1 : 1;

  if (!amount) {
    return { scrolled: false, axis: null, el: null };
  }

  const target = findScrollTargetAtPoint(clientX, clientY, s, { doc, win });
  if (!target) {
    // Absolute last resort: window scroll on Y (preserves old C/V behavior).
    try {
      if (win && typeof win.scrollBy === 'function') {
        win.scrollBy({ top: s * amount, left: 0, behavior });
        return { scrolled: true, axis: 'y', el: doc.scrollingElement || doc.documentElement || null };
      }
    } catch { /* ignore */ }
    return { scrolled: false, axis: null, el: null };
  }

  const { el, axis } = target;
  const dx = axis === 'x' ? s * amount : 0;
  const dy = axis === 'y' ? s * amount : 0;
  const ok = scrollElementBy(el, dx, dy, behavior, doc, win);
  return { scrolled: ok, axis, el };
}



  // Module: src/ui/url-listing.js
/**
 * Shared URL listing helpers.
 *
 * Goal: centralize how we render "lists of URLs" across the extension:
 * - Omnibox suggestions
 * - History popovers
 * - Top sites / bookmarks surfaces
 *
 * Supports:
 * - list + grid views
 * - container creation (scroll wrappers)
 * - per-usage customization via callbacks and class/style overrides
 */

/**
 * @typedef {'list'|'grid'} UrlListingView
 */

/**
 * @typedef {object} UrlListClassNames
 * @property {string} [container]
 * @property {string} [row]
 * @property {string} [rowSelected]
 * @property {string} [content]
 * @property {string} [text]
 * @property {string} [title]
 * @property {string} [meta]
 * @property {string} [url]
 * @property {string} [favicon]
 */

/**
 * @typedef {object} UrlListingItemParts
 * @property {HTMLElement} row
 * @property {HTMLElement} content
 * @property {HTMLElement} text
 * @property {HTMLElement} titleEl
 * @property {HTMLElement} metaEl
 * @property {HTMLElement} urlEl
 * @property {HTMLImageElement|null} faviconEl
 */

const GENERIC_FAVICON_SVG = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
  <path fill="#ffffff" fill-opacity="0.78" d="M12 2c5.52 0 10 4.48 10 10s-4.48 10-10 10S2 17.52 2 12 6.48 2 12 2Zm0 2c-1.5 0-2.93.35-4.2.98.58.5 1.27 1.33 1.9 2.6.82-.28 1.74-.44 2.3-.48V4Zm2 0v3.1c.56.04 1.48.2 2.3.48.63-1.27 1.32-2.1 1.9-2.6A7.95 7.95 0 0 0 14 4Zm-6.72 2.2A7.99 7.99 0 0 0 4.07 11h3.21c.06-1.39.29-2.65.63-3.74-.24-.5-.45-.84-.63-1.06ZM19.93 11A7.99 7.99 0 0 0 16.72 6.2c-.18.22-.39.56-.63 1.06.34 1.09.57 2.35.63 3.74h3.21ZM9.3 8.73c-.24.8-.41 1.74-.46 2.77h3.16V9.15c-.9.05-1.85.22-2.7.58Zm5.4 0c-.85-.36-1.8-.53-2.7-.58v2.35h3.16c-.05-1.03-.22-1.97-.46-2.77ZM4.07 13a7.99 7.99 0 0 0 3.21 4.8c.18-.22.39-.56.63-1.06-.34-1.09-.57-2.35-.63-3.74H4.07Zm4.77 0c.05 1.03.22 1.97.46 2.77.85.36 1.8.53 2.7.58V13H8.84Zm3.16 3.85c-.56-.04-1.48-.2-2.3-.48-.63 1.27-1.32 2.1-1.9 2.6A7.95 7.95 0 0 0 12 20v-3.15Zm2 0V20c1.5 0 2.93-.35 4.2-.98-.58-.5-1.27-1.33-1.9-2.6-.82.28-1.74.44-2.3.48Zm1.16-3.85H12v3.35c.9-.05 1.85-.22 2.7-.58.24-.8.41-1.74.46-2.77Zm1.56 3.74c.24.5.45.84.63 1.06a7.99 7.99 0 0 0 3.21-4.8h-3.21c-.06 1.39-.29 2.65-.63 3.74Z"/>
</svg>
`.trim();

/** @type {string} */
const GENERIC_FAVICON_DATA_URL = `data:image/svg+xml;utf8,${encodeURIComponent(GENERIC_FAVICON_SVG)}`;

/**
 * @param {string} url
 * @param {number} [size]
 */
function getChromeFavicon2Url(url, size = 32) {
  const u = String(url || '').trim();
  const s = Number(size) || 32;
  return `chrome://favicon2/?size=${encodeURIComponent(String(s))}&pageUrl=${encodeURIComponent(u)}`;
}

/**
 * Strip a leading "www." from a host (or host+port / host+path fragment) for display.
 * @param {string} hostOrUrlPart
 * @returns {string}
 */
function stripWwwPrefix(hostOrUrlPart) {
  return String(hostOrUrlPart || '').replace(/^www\./i, '');
}

/**
 * Extracts and formats a URL for display: removes protocol and leading www.
 * @param {string} url
 * @returns {string} cleaned URL string (no protocol / no leading www.)
 */
function formatUrlForDisplay(url) {
  const u = String(url || '').trim();
  if (!u) return '';

  try {
    // Remove protocol (http://, https://, etc.), then leading www.
    let cleanUrl = u.replace(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//, '');
    cleanUrl = stripWwwPrefix(cleanUrl);
    return cleanUrl;
  } catch {
    return stripWwwPrefix(u.replace(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//, ''));
  }
}

/**
 * Extract hostname without leading www. for display/grouping.
 * @param {string} url
 * @returns {string}
 */
function extractDomain(url) {
  try {
    const hostname = new URL(String(url || '').trim()).hostname || '';
    return stripWwwPrefix(hostname);
  } catch {
    return '';
  }
}

/**
 * Parse a URL into domain + path lines for three-line listings.
 * Domain never includes a leading "www.".
 *
 * @param {string} rawUrl
 * @returns {{ domain: string, path: string }}
 */
function parseUrlForThreeLineDisplay(rawUrl) {
  const input = String(rawUrl || '').trim();
  if (!input) return { domain: '', path: '' };

  try {
    const u = new URL(input, 'https://example.invalid');
    const scheme = (u.protocol || '').replace(/:$/, '');
    const hostOnly = stripWwwPrefix(u.hostname || '');
    const host = hostOnly + (u.port ? `:${u.port}` : '');

    // Domain line: prefer host if present; otherwise fall back to scheme.
    const domain = host || scheme || stripWwwPrefix(input);

    // Path line: everything after the domain: pathname + search + hash.
    // Keep '/' for empty paths so the third line isn't blank for homepages.
    const pathname = u.pathname || '';
    const rest = `${pathname || ''}${u.search || ''}${u.hash || ''}`;
    const path = rest || (host ? '/' : '');

    return { domain, path };
  } catch {
    // Very defensive fallback: attempt split on first slash after scheme.
    const m = input.match(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/([^\/?#]+)([^\s]*)?$/);
    if (m) {
      const domain = stripWwwPrefix(m[1] || input);
      const path = m[2] || '/';
      return { domain, path };
    }
    return { domain: stripWwwPrefix(input), path: '' };
  }
}

/**
 * Extract path + search + hash for display (empty string for bare `/`).
 * @param {string} url
 * @returns {string}
 */
function extractPath(url) {
  try {
    const urlObj = new URL(String(url || '').trim());
    const path = `${urlObj.pathname || ''}${urlObj.search || ''}${urlObj.hash || ''}`;
    return path === '/' ? '' : path;
  } catch {
    return '';
  }
}

/**
 * Preferred MV3 approach:
 * - Requires "favicon" permission in manifest.
 * - For content scripts injecting DOM into pages, the internal path must be declared
 *   in web_accessible_resources (e.g. "_favicon/*").
 *
 * @param {string} pageUrl
 * @param {number} [size]
 */
function getExtensionFaviconUrl(pageUrl, size = 32) {
  const u = String(pageUrl || '').trim();
  const s = Number(size) || 32;

  try {
    if (typeof chrome !== 'undefined' && chrome?.runtime?.getURL) {
      const url = new URL(chrome.runtime.getURL('/_favicon/'));
      url.searchParams.set('pageUrl', u);
      url.searchParams.set('size', String(s));
      return url.toString();
    }
  } catch {
    // ignore
  }

  // Fallback (older behavior / environments without runtime.getURL)
  return getChromeFavicon2Url(u, s);
}

/**
 * Google's public favicon service — loadable as <img src> (img-src https:) without fetch/connect-src.
 * Often higher-res than Chrome's cached tab favicon when sz is large.
 *
 * @param {string} pageUrl
 * @param {number} [size]
 * @returns {string}
 */
function getGoogleS2FaviconUrl(pageUrl, size = 128) {
  const s = Math.max(16, Math.min(256, Number(size) || 128));
  try {
    const u = new URL(String(pageUrl || '').trim());
    const domain = (u.hostname || '').replace(/^www\./i, '');
    if (!domain) return '';
    return `https://www.google.com/s2/favicons?domain=${encodeURIComponent(domain)}&sz=${s}`;
  } catch {
    return '';
  }
}

/**
 * Request favicon from service worker (multi-source high-res probe + cache).
 * Requires extension_pages CSP connect-src to allow https: fetches in the SW.
 *
 * @param {string} pageUrl
 * @param {number} [size]
 * @returns {Promise<string|null>} Data URL or null if failed
 */
async function requestFaviconFromServiceWorker(pageUrl, size = 32) {
  try {
    if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
      return null;
    }

    const response = await chrome.runtime.sendMessage({
      type: 'KP_GET_FAVICON',
      pageUrl: pageUrl,
      size: size
    });

    if (response && response.type === 'KP_FAVICON_RESPONSE' && response.success && response.dataUrl) {
      return response.dataUrl;
    }
  } catch (e) {
    // Message passing failed or service worker unavailable
  }
  return null;
}

/**
 * Paint a quick Chrome favicon, then upgrade to a higher-res asset when available.
 *
 * Upgrade order (CSP-safe):
 * 1. Chrome `/_favicon/` (instant, often low-res)
 * 2. Google s2 as direct <img src> (no fetch; uses img-src)
 * 3. Service-worker multi-source probe → data: URL (needs connect-src https:)
 *
 * @param {HTMLImageElement} img
 * @param {string} pageUrl
 * @param {object} [opts]
 * @param {number} [opts.displaySize] CSS/display box size
 * @param {number} [opts.requestSize] preferred source size (default max(display, 128))
 * @param {string} [opts.fallbackUrl]
 * @param {boolean} [opts.highRes=true]
 */
function attachFaviconWithUpgrade(img, pageUrl, opts = {}) {
  if (!img) return;
  const url = String(pageUrl || '').trim();
  const displaySize = Math.max(12, Number(opts.displaySize) || 32);
  const requestSize = Math.max(
    displaySize,
    Number(opts.requestSize) || Math.max(128, displaySize * 2)
  );
  const fallback =
    typeof opts.fallbackUrl === 'string' && opts.fallbackUrl
      ? opts.fallbackUrl
      : GENERIC_FAVICON_DATA_URL;
  const highRes = opts.highRes !== false;

  try {
    img.alt = img.alt || '';
    img.referrerPolicy = 'no-referrer';
    img.decoding = 'async';
    img.loading = img.loading || 'lazy';
  } catch {
    // ignore
  }

  /** @type {'chrome' | 'google' | 'sw' | 'fallback' | ''} */
  let stage = '';

  const setSrc = (src, nextStage) => {
    try {
      stage = nextStage;
      img.src = src;
    } catch {
      // ignore
    }
  };

  const chromeUrl = getExtensionFaviconUrl(url, Math.min(128, Math.max(32, displaySize * 2)));
  const googleUrl = getGoogleS2FaviconUrl(url, requestSize);

  // 1) Instant paint from Chrome's favicon cache.
  setSrc(chromeUrl, 'chrome');

  img.addEventListener('error', () => {
    // Do not downgrade a successful SW data: URL.
    if (stage === 'sw' || stage === 'fallback') return;
    if (stage === 'chrome' && googleUrl) {
      setSrc(googleUrl, 'google');
      try {
        img.dataset.kpFaviconHires = 'google';
      } catch {
        // ignore
      }
      return;
    }
    if (stage === 'google' || stage === 'chrome') {
      setSrc(fallback, 'fallback');
      try {
        img.dataset.kpFaviconFallback = 'true';
      } catch {
        // ignore
      }
    }
  });

  if (!highRes || !url) return;

  // 2) Google s2 upgrade via <img src> (uses img-src, not connect-src / fetch).
  const tryGoogleUpgrade = () => {
    if (!googleUrl || !img.isConnected) return;
    if (stage === 'sw' || stage === 'fallback') return;
    if (stage !== 'chrome') return;
    setSrc(googleUrl, 'google');
    try {
      img.dataset.kpFaviconHires = 'google';
    } catch {
      // ignore
    }
  };

  try {
    requestAnimationFrame(() => {
      setTimeout(tryGoogleUpgrade, 0);
    });
  } catch {
    tryGoogleUpgrade();
  }

  // 3) SW multi-source probe → cached data: URL (needs CSP connect-src https: + host_permissions).
  requestFaviconFromServiceWorker(url, requestSize)
    .then((dataUrl) => {
      if (!dataUrl || !img.isConnected) return;
      try {
        img.src = dataUrl;
        img.dataset.kpFaviconHires = 'sw';
        stage = 'sw';
      } catch {
        // ignore
      }
    })
    .catch(() => {
      // Chrome / Google path remains.
    });
}

/**
 * @param {Document} doc
 * @param {string} url
 * @param {object} [opts]
 * @param {number} [opts.size]
 * @param {string} [opts.faviconUrl]
 * @param {string} [opts.fallbackUrl]
 * @param {boolean} [opts.highRes]
 */
function createFaviconImg(doc, url, { size = 18, faviconUrl, fallbackUrl, highRes = true } = {}) {
  const d = doc || document;
  const img = /** @type {HTMLImageElement} */ (d.createElement('img'));
  img.alt = '';
  img.referrerPolicy = 'no-referrer';
  img.decoding = 'async';
  img.loading = 'lazy';
  img.width = size;
  img.height = size;
  const fallback = typeof fallbackUrl === 'string' && fallbackUrl ? fallbackUrl : GENERIC_FAVICON_DATA_URL;

  if (typeof faviconUrl === 'string' && faviconUrl) {
    img.src = faviconUrl;
    img.addEventListener(
      'error',
      () => {
        attachFaviconWithUpgrade(img, url, {
          displaySize: size,
          fallbackUrl: fallback,
          highRes
        });
      },
      { once: true }
    );
    if (highRes) {
      // Still try to upgrade custom URL if SW finds something sharper.
      requestFaviconFromServiceWorker(url, Math.max(size, 128)).then((dataUrl) => {
        if (dataUrl && img.isConnected) {
          img.src = dataUrl;
          img.dataset.kpFaviconHires = 'true';
        }
      }).catch(() => {});
    }
    return img;
  }

  attachFaviconWithUpgrade(img, url, {
    displaySize: size,
    fallbackUrl: fallback,
    highRes
  });

  return img;
}

/**
 * Creates a container appropriate for URL listings (optionally scrollable).
 *
 * @param {object} params
 * @param {Document} [params.doc]
 * @param {UrlListingView} [params.view]
 * @param {string} [params.className]
 * @param {Partial<CSSStyleDeclaration>} [params.style]
 * @param {boolean} [params.useInlineStyles]
 * @param {string} [params.maxHeight] e.g. '40vh'
 * @param {boolean} [params.scrollY]
 * @returns {HTMLElement}
 */
function createUrlListingContainer({
  doc,
  view = 'list',
  className = '',
  style = {},
  useInlineStyles = true,
  maxHeight,
  scrollY = true
} = {}) {
  const d = doc || document;
  const el = d.createElement('div');
  if (className) el.className = className;
  el.dataset.kpUrlListingView = view;

  if (useInlineStyles) {
    Object.assign(el.style, {
      display: view === 'grid' ? 'grid' : 'block',
      ...(scrollY ? { overflowY: 'auto' } : {}),
      ...(maxHeight ? { maxHeight } : {})
    });
  }

  if (style && typeof style === 'object') {
    Object.assign(el.style, style);
  }

  return el;
}

/**
 * Render a URL listing into a container.
 *
 * Callers can customize:
 * - how to read title/url/meta/favicon
 * - selection
 * - per-row DOM additions via `decorateRow`
 *
 * @template T
 * @param {object} params
 * @param {HTMLElement} params.container
 * @param {T[]} params.items
 * @param {UrlListingView} [params.view]
 * @param {UrlListClassNames} [params.classNames]
 * @param {boolean} [params.useInlineStyles]
 * @param {string} [params.emptyText]
 * @param {(item: T, idx: number) => string} [params.getTitle]
 * @param {(item: T, idx: number) => string} [params.getUrl]
 * @param {(item: T, idx: number) => string} [params.getMeta]
 * @param {(item: T, idx: number) => string} [params.getFaviconUrl]
 * @param {boolean} [params.showFavicon]
 * @param {boolean} [params.showUrlLine]
 * @param {boolean} [params.showMetaLine]
 * @param {number} [params.selectedIndex]
 * @param {(item: T, idx: number) => boolean} [params.isSelected]
 * @param {(item: T, idx: number) => Record<string,string>} [params.getRowDataset]
 * @param {string} [params.rowTag] Defaults to 'div'
 * @param {(args: {row: HTMLElement, item: T, idx: number, parts: UrlListingItemParts}) => void} [params.decorateRow]
 * @param {(args: {row: HTMLElement, item: T, idx: number}) => void} [params.onRowMouseEnter]
 * @param {(args: {row: HTMLElement, item: T, idx: number, event: MouseEvent}) => void} [params.onRowMouseDown]
 * @param {(args: {row: HTMLElement, item: T, idx: number, event: MouseEvent}) => void} [params.onRowClick]
 */
function renderUrlListing({
  container,
  items,
  view = 'list',
  classNames = {},
  useInlineStyles = true,
  emptyText,
  getTitle,
  getUrl,
  getMeta,
  getFaviconUrl,
  showFavicon = false,
  showUrlLine = true,
  showMetaLine = false,
  selectedIndex = -1,
  isSelected,
  getRowDataset,
  rowTag = 'div',
  decorateRow,
  onRowMouseEnter,
  onRowMouseDown,
  onRowClick
}) {
  if (!container) return;
  const doc = container.ownerDocument || document;

  container.dataset.kpUrlListingView = view;
  if (classNames.container) container.classList.add(classNames.container);
  container.textContent = '';

  if (!Array.isArray(items) || !items.length) {
    if (typeof emptyText === 'string' && emptyText) {
      const empty = doc.createElement('div');
      empty.textContent = emptyText;
      if (useInlineStyles) {
        Object.assign(empty.style, {
          padding: '10px 16px',
          fontSize: '13px',
          color: 'rgba(255,255,255,0.55)',
          fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif'
        });
      }
      container.appendChild(empty);
    }
    return;
  }

  // Layout hints for grid/list.
  if (useInlineStyles) {
    if (view === 'grid') {
      Object.assign(container.style, {
        display: 'grid',
        gridTemplateColumns: 'repeat(auto-fill, minmax(180px, 1fr))',
        gap: '10px'
      });
    } else {
      // let caller control container scroll/padding; we only ensure block.
      if (container.style.display === '') container.style.display = 'block';
    }
  }

  for (let idx = 0; idx < items.length; idx += 1) {
    const item = items[idx];
    const row = /** @type {HTMLElement} */ (doc.createElement(rowTag));

    const selected = typeof isSelected === 'function' ? Boolean(isSelected(item, idx)) : idx === selectedIndex;
    if (classNames.row) row.className = classNames.row;
    if (selected && classNames.rowSelected) row.classList.add(classNames.rowSelected);
    row.dataset.kpUrlListingIndex = String(idx);

    if (typeof getRowDataset === 'function') {
      const data = getRowDataset(item, idx) || {};
      for (const [k, v] of Object.entries(data)) {
        if (!k) continue;
        row.dataset[k] = String(v);
      }
    }

    // If using <a> tag, set href attribute for native link behavior
    const rawUrlText = typeof getUrl === 'function' ? String(getUrl(item, idx) || '') : '';
    if (rowTag === 'a' && rawUrlText && row instanceof HTMLAnchorElement) {
      row.href = rawUrlText;
    }

    // Structure:
    // row
    //  - content (flex)
    //    - [favicon?]
    //    - text (title + meta/url)
    const content = doc.createElement('div');
    const text = doc.createElement('div');
    const titleEl = doc.createElement('div');
    const metaEl = doc.createElement('div');
    const urlEl = doc.createElement('div');

    let faviconEl = null;
    const titleText = typeof getTitle === 'function' ? String(getTitle(item, idx) || '') : (rawUrlText || '');
    const metaText = typeof getMeta === 'function' ? String(getMeta(item, idx) || '') : '';

    titleEl.textContent = titleText;
    metaEl.textContent = metaText;

    // Format URL for display: remove protocol and make domain bold
    if (rawUrlText) {
      const cleanUrl = formatUrlForDisplay(rawUrlText);
      // Extract domain (everything before the first /)
      const domainMatch = cleanUrl.match(/^([^\/]+)/);
      if (domainMatch) {
        const domain = domainMatch[1];
        const rest = cleanUrl.slice(domain.length);
        urlEl.innerHTML = `<strong>${domain}</strong>${rest}`;
      } else {
        urlEl.textContent = cleanUrl;
      }
    } else {
      urlEl.textContent = '';
    }

    if (classNames.content) content.className = classNames.content;
    if (classNames.text) text.className = classNames.text;
    if (classNames.title) titleEl.className = classNames.title;
    if (classNames.meta) metaEl.className = classNames.meta;
    if (classNames.url) urlEl.className = classNames.url;

    if (useInlineStyles) {
      Object.assign(row.style, {
        userSelect: 'none',
        textDecoration: 'none',
        color: 'inherit'
      });
      Object.assign(content.style, {
        display: 'flex',
        alignItems: view === 'grid' ? 'flex-start' : 'center',
        gap: '10px'
      });
      Object.assign(text.style, {
        minWidth: '0',
        flex: '1 1 auto'
      });
      Object.assign(titleEl.style, {
        fontSize: '13px',
        fontWeight: view === 'grid' ? '700' : '700',
        color: 'rgba(255,255,255,0.92)',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
        fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif'
      });
      Object.assign(urlEl.style, {
        marginTop: '2px',
        fontSize: '11px',
        color: 'rgba(255,255,255,0.62)',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
        fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif'
      });
      Object.assign(metaEl.style, {
        marginTop: '2px',
        fontSize: '11px',
        color: 'rgba(255,255,255,0.62)',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
        fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif'
      });

      if (view === 'grid') {
        Object.assign(row.style, {
          padding: '10px 10px',
          borderRadius: '12px',
          cursor: 'pointer',
          background: selected ? 'rgba(255,140,0,0.16)' : 'rgba(0,0,0,0.10)',
          border: selected ? '1px solid rgba(255,140,0,0.35)' : '1px solid rgba(255,255,255,0.10)'
        });
      } else {
        Object.assign(row.style, {
          display: 'block',
          padding: '8px 10px',
          borderRadius: '10px',
          cursor: 'pointer',
          background: selected ? 'rgba(255,140,0,0.18)' : 'rgba(0,0,0,0)',
          border: selected ? '1px solid rgba(255,140,0,0.35)' : '1px solid rgba(255,255,255,0.06)',
          margin: '6px 6px 6px 6px'
        });
      }
    }

    if (showFavicon) {
      const faviconUrl = typeof getFaviconUrl === 'function' ? String(getFaviconUrl(item, idx) || '') : '';
      faviconEl = createFaviconImg(doc, rawUrlText, { size: 18, faviconUrl });
      if (classNames.favicon) faviconEl.className = classNames.favicon;
      if (useInlineStyles) {
        Object.assign(faviconEl.style, {
          width: '18px',
          height: '18px',
          borderRadius: '4px',
          background: 'rgba(255,255,255,0.08)',
          flex: '0 0 auto'
        });
      }
      content.appendChild(faviconEl);
    }

    text.appendChild(titleEl);
    if (showMetaLine) text.appendChild(metaEl);
    if (showUrlLine) text.appendChild(urlEl);

    content.appendChild(text);
    row.appendChild(content);

    // If the caller provided a row click handler, make the row explicitly interactive.
    // This helps KeyPilot's IntersectionObserverManager discover/index these rows (via `[role="link"]`)
    // and improves accessibility (keyboard focus + Enter/Space activation).
    if (typeof onRowClick === 'function') {
      try {
        // Only set these when the row itself is the click target.
        row.setAttribute('role', 'link');
        // Make keyboard focusable unless it's already focusable (e.g. <a>, <button>).
        if (typeof row.tabIndex !== 'number' || row.tabIndex < 0) row.tabIndex = 0;
        if (rawUrlText) row.dataset.kpUrl = rawUrlText;
        if (titleText) row.setAttribute('aria-label', titleText);
      } catch {
        // ignore
      }
    }

    if (typeof onRowMouseEnter === 'function') {
      row.addEventListener('mouseenter', () => onRowMouseEnter({ row, item, idx }), { passive: true });
    }
    if (typeof onRowMouseDown === 'function') {
      row.addEventListener('mousedown', (event) => onRowMouseDown({ row, item, idx, event }), true);
    }
    if (typeof onRowClick === 'function') {
      row.addEventListener('click', (event) => onRowClick({ row, item, idx, event }), true);
      // Keyboard activation for accessibility + consistency with "clickable row" semantics.
      row.addEventListener('keydown', (event) => {
        try {
          const k = event && typeof event.key === 'string' ? event.key : '';
          if (k !== 'Enter' && k !== ' ') return;
          event.preventDefault();
          onRowClick({ row, item, idx, event: /** @type {any} */ (event) });
        } catch {
          // ignore
        }
      }, true);
    }

    const parts = {
      row,
      content,
      text,
      titleEl,
      metaEl,
      urlEl,
      faviconEl
    };

    if (typeof decorateRow === 'function') {
      decorateRow({ row, item, idx, parts });
    }

    container.appendChild(row);
  }
}





  // Module: src/ui/page-thumb-ui.js
/**
 * Client helpers for page-preview card backgrounds.
 *
 * Prefer official YouTube thumbnails for video URLs; otherwise request a
 * stored capture from the service worker (IndexedDB).
 */

/**
 * Build the darkened cover background used by Launcher / New Tab cards.
 * @param {string} imageUrl
 * @param {number} [topAlpha=0.7]
 * @param {number} [bottomAlpha=0.85]
 * @returns {string}
 */
function buildDarkenedThumbBackground(imageUrl, topAlpha = 0.55, bottomAlpha = 0.78) {
  const url = String(imageUrl || '').trim();
  if (!url) return '';
  const t = Math.max(0, Math.min(1, Number(topAlpha) || 0.7));
  const b = Math.max(0, Math.min(1, Number(bottomAlpha) || 0.85));
  // Quote data: / http(s) URLs for CSS url().
  const safe = url.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
  return (
    `linear-gradient(to bottom, rgba(0,0,0,${t}) 0%, rgba(0,0,0,${b}) 100%), ` +
    `url("${safe}") center / cover no-repeat`
  );
}

/**
 * Request a stored page screenshot from the service worker.
 * @param {string} pageUrl
 * @returns {Promise<string|null>} data URL or null
 */
async function requestPageThumb(pageUrl) {
  const url = String(pageUrl || '').trim();
  if (!url) return null;

  try {
    if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
      return null;
    }
    const response = await chrome.runtime.sendMessage({
      type: 'KP_GET_PAGE_THUMB',
      pageUrl: url
    });
    if (
      response &&
      response.type === 'KP_PAGE_THUMB_RESPONSE' &&
      response.success &&
      typeof response.dataUrl === 'string' &&
      response.dataUrl
    ) {
      return response.dataUrl;
    }
  } catch {
    // SW unavailable or no thumb.
  }
  return null;
}

/**
 * Resolve the best available card background image URL for a page.
 * YouTube video → official thumb; else stored capture.
 *
 * @param {string} pageUrl
 * @param {{ youtubePrefer?: boolean, youtubeQuality?: string }} [opts]
 * @returns {Promise<{ url: string, source: 'youtube'|'capture' }|null>}
 */
async function resolveCardBackgroundImage(pageUrl, opts = {}) {
  const youtubePrefer = opts.youtubePrefer !== false;
  if (youtubePrefer) {
    const yt = getYouTubeThumbnailUrlForPage(
      pageUrl,
      /** @type {any} */ (opts.youtubeQuality || 'hqdefault')
    );
    if (yt) return { url: yt, source: 'youtube' };
  }

  const dataUrl = await requestPageThumb(pageUrl);
  if (dataUrl) return { url: dataUrl, source: 'capture' };
  return null;
}

/**
 * Apply a darkened page-preview (or YouTube) background to a card element.
 * Falls back to solid colors until a thumb is available.
 *
 * @param {HTMLElement} el
 * @param {string} pageUrl
 * @param {object} [opts]
 * @param {string} [opts.fallbackSolid='#2a2a2a']
 * @param {string} [opts.hoverSolid='#333']
 * @param {boolean} [opts.manageHover=false] wire mouseenter/leave for darken lift
 * @param {boolean} [opts.youtubePrefer=true]
 * @param {number} [opts.idleTop=0.7]
 * @param {number} [opts.idleBottom=0.85]
 * @param {number} [opts.hoverTop=0.5]
 * @param {number} [opts.hoverBottom=0.75]
 * @param {boolean} [opts.useCssVar=false] set --kp-page-thumb + class instead of inline bg
 * @param {string} [opts.cssVarName='--kp-page-thumb']
 * @param {string} [opts.readyClass='kp-has-page-thumb']
 * @returns {{ refresh: () => Promise<void>, dispose: () => void }}
 */
function applyCardBackground(el, pageUrl, opts = {}) {
  if (!el) {
    return { refresh: async () => {}, dispose: () => {} };
  }

  const fallbackSolid = opts.fallbackSolid || '#2a2a2a';
  const hoverSolid = opts.hoverSolid || '#333';
  const manageHover = opts.manageHover === true;
  const youtubePrefer = opts.youtubePrefer !== false;
  // Idle uses the previous hover darkness; hover lifts further for more photo.
  const idleTop = opts.idleTop != null ? opts.idleTop : 0.55;
  const idleBottom = opts.idleBottom != null ? opts.idleBottom : 0.78;
  const hoverTop = opts.hoverTop != null ? opts.hoverTop : 0.32;
  const hoverBottom = opts.hoverBottom != null ? opts.hoverBottom : 0.52;
  const useCssVar = opts.useCssVar === true;
  const cssVarName = opts.cssVarName || '--kp-page-thumb';
  const readyClass = opts.readyClass || 'kp-has-page-thumb';

  let disposed = false;
  let hovering = false;
  /** @type {string|null} */
  let thumbUrl = null;

  const paintSolid = (hover) => {
    if (useCssVar) {
      try {
        el.classList.remove(readyClass);
        el.style.removeProperty(cssVarName);
      } catch {
        // ignore
      }
      // Leave ambient CSS background when no thumb.
      return;
    }
    el.style.background = hover ? hoverSolid : fallbackSolid;
  };

  const paintThumb = (url, hover) => {
    if (useCssVar) {
      try {
        const safe = String(url).replace(/\\/g, '\\\\').replace(/"/g, '\\"');
        el.style.setProperty(cssVarName, `url("${safe}")`);
        el.classList.add(readyClass);
        if (hover) el.classList.add('kp-page-thumb-hover');
        else el.classList.remove('kp-page-thumb-hover');
      } catch {
        // ignore
      }
      return;
    }
    const top = hover ? hoverTop : idleTop;
    const bottom = hover ? hoverBottom : idleBottom;
    el.style.background = buildDarkenedThumbBackground(url, top, bottom);
  };

  const paint = () => {
    if (disposed || !el.isConnected) return;
    if (thumbUrl) paintThumb(thumbUrl, hovering);
    else paintSolid(hovering);
  };

  const onEnter = () => {
    hovering = true;
    paint();
  };
  const onLeave = () => {
    hovering = false;
    paint();
  };

  if (manageHover) {
    el.addEventListener('mouseenter', onEnter);
    el.addEventListener('mouseleave', onLeave);
  }

  // Immediate solid / optional sync YouTube paint.
  paintSolid(false);
  if (youtubePrefer) {
    const yt = getYouTubeThumbnailUrlForPage(pageUrl, 'hqdefault');
    if (yt) {
      thumbUrl = yt;
      try {
        el.dataset.kpThumbSource = 'youtube';
        el.dataset.kpThumbUrl = yt;
        el.dataset.kpThumbReady = '1';
      } catch {
        // ignore
      }
      paint();
    }
  }

  const refresh = async () => {
    if (disposed) return;
    // Already have YouTube official — do not replace with capture.
    if (thumbUrl && el.dataset?.kpThumbSource === 'youtube') return;

    const resolved = await resolveCardBackgroundImage(pageUrl, { youtubePrefer });
    if (disposed || !el.isConnected) return;
    if (!resolved?.url) return;

    // Prefer keeping youtube if resolve returned youtube.
    thumbUrl = resolved.url;
    try {
      el.dataset.kpThumbSource = resolved.source;
      el.dataset.kpThumbUrl = resolved.url;
      el.dataset.kpThumbReady = '1';
    } catch {
      // ignore
    }
    paint();
  };

  // Async capture lookup (no-op if YouTube already painted and we skip).
  void refresh();

  const dispose = () => {
    disposed = true;
    if (manageHover) {
      el.removeEventListener('mouseenter', onEnter);
      el.removeEventListener('mouseleave', onLeave);
    }
  };

  return { refresh, dispose };
}



  // Module: src/modules/state-manager.js
/**
 * Application state management
 */

class StateManager {
  constructor() {
    this.state = {
      mode: MODES.NONE,
      lastMouse: { x: 0, y: 0 },
      focusEl: null,
      /**
       * Shared inspector hover target (Delete, Cols, future pick tools).
       * Only meaningful while mode === MODES.INSPECTOR.
       */
      inspectorEl: null,
      /** @type {string|null} INSPECTOR_KIND value while in inspector mode */
      inspectorKind: null,
      highlightEl: null,
      highlightStartPosition: null,
      currentSelection: null,
      isInitialized: false,
      popoverOpen: false,
      popoverUrl: null
    };
    
    this.subscribers = new Set();
  }

  getState() {
    return { ...this.state };
  }

  setState(updates) {
    const prevState = { ...this.state };
    this.state = { ...this.state, ...updates };
    
    // Notify subscribers of state changes
    this.notifySubscribers(prevState, this.state);
  }

  subscribe(callback) {
    this.subscribers.add(callback);
    
    // Return unsubscribe function
    return () => {
      this.subscribers.delete(callback);
    };
  }

  notifySubscribers(prevState, newState) {
    for (const callback of this.subscribers) {
      try {
        callback(newState, prevState);
      } catch (error) {
        console.error('State subscriber error:', error);
      }
    }
  }

  // Convenience methods
  setMode(mode) {
    this.setState({ mode });
  }

  setMousePosition(x, y) {
    this.setState({ lastMouse: { x, y } });
  }

  setFocusElement(element) {
    this.setState({ focusEl: element });
  }

  setInspectorElement(element) {
    this.setState({ inspectorEl: element });
  }

  /**
   * Enter shared inspector pick mode for a tool kind.
   * @param {string} kind INSPECTOR_KIND value
   */
  enterInspector(kind) {
    this.setState({
      mode: MODES.INSPECTOR,
      inspectorKind: kind || null,
      inspectorEl: null,
      // Clear normal focus hover so green click chrome doesn't fight inspector outline
      focusEl: null
    });
  }

  /**
   * Exit inspector pick mode without clearing sticky page effects.
   */
  exitInspector() {
    this.setState({
      mode: MODES.NONE,
      inspectorKind: null,
      inspectorEl: null
    });
  }

  setHighlightElement(element) {
    this.setState({ highlightEl: element });
  }

  setHighlightStartPosition(position) {
    this.setState({ highlightStartPosition: position });
  }

  setCurrentSelection(selection) {
    this.setState({ currentSelection: selection });
  }

  setPopoverOpen(isOpen, url = null) {
    if (isOpen) {
      this.setState({ 
        mode: MODES.POPOVER,  // Set mode when opening
        // Popovers are modal: stop tracking/clicking background page elements.
        // Clearing focus/inspector immediately also hides hover chrome until the
        // user moves the mouse over popover UI.
        focusEl: null,
        inspectorEl: null,
        inspectorKind: null,
        popoverOpen: isOpen, 
        popoverUrl: url 
      });
    } else {
      this.setState({ 
        mode: MODES.NONE,  // Reset mode when closing
        popoverOpen: false, 
        popoverUrl: null 
      });
    }
  }

  clearElements() {
    this.setState({ 
      focusEl: null, 
      inspectorEl: null,
      highlightEl: null,
      highlightStartPosition: null,
      currentSelection: null
    });
  }

  isInspectorMode() {
    return this.state.mode === MODES.INSPECTOR;
  }

  /**
   * @param {string} kind
   * @returns {boolean}
   */
  isInspectorKind(kind) {
    return this.state.mode === MODES.INSPECTOR && this.state.inspectorKind === kind;
  }

  /** @deprecated prefer isInspectorKind(INSPECTOR_KIND.DELETE) */
  isDeleteMode() {
    return this.isInspectorKind('delete') || this.state.mode === MODES.DELETE;
  }

  /** @deprecated prefer isInspectorKind(INSPECTOR_KIND.COLS) */
  isColsMode() {
    return this.isInspectorKind('cols') || this.state.mode === MODES.COLS;
  }

  isHighlightMode() {
    return this.state.mode === MODES.HIGHLIGHT;
  }

  isTextFocusMode() {
    return this.state.mode === MODES.TEXT_FOCUS;
  }

  isPopoverMode() {
    return this.state.mode === MODES.POPOVER;
  }

  reset() {
    this.setState({
      mode: MODES.NONE,
      focusEl: null,
      inspectorEl: null,
      inspectorKind: null,
      highlightEl: null,
      highlightStartPosition: null,
      currentSelection: null,
      popoverOpen: false,
      popoverUrl: null
    });
  }
}



  // Module: src/modules/event-manager.js
/**
 * Centralized event management
 *
 * Build note: content-bundled.js concatenates modules and strips ESM imports.
 * Free functions from earlier modules (e.g. `dom-context.js`) remain in scope.
 * Never use `import { x as y }` aliases — the alias binding is deleted with the import.
 * Call free functions via distinct names (kpIsTypingContext, …) from methods so the
 * method name cannot shadow the free function after bundling.
 */

class EventManager {
  constructor() {
    this.listeners = new Map();
    this.isActive = false;
  }

  start() {
    if (this.isActive) return;
    
    this.addListener(document, 'keydown', this.handleKeyDown.bind(this), { capture: true });
    
    // Multiple mouse move listeners to catch events that might be captured
    this.addListener(document, 'mousemove', this.handleMouseMove.bind(this));
    this.addListener(document, 'mousemove', this.handleMouseMove.bind(this), { capture: true });
    this.addListener(window, 'mousemove', this.handleMouseMove.bind(this));
    
    // Additional mouse events for better tracking
    this.addListener(document, 'mouseenter', this.handleMouseMove.bind(this));
    this.addListener(document, 'mouseover', this.handleMouseMove.bind(this));
    
    this.addListener(document, 'scroll', this.handleScroll.bind(this), { passive: true });
    
    this.isActive = true;
  }

  stop() {
    if (!this.isActive) return;
    
    this.removeAllListeners();
    this.isActive = false;
  }

  cleanup() {
    this.stop();
    if (this.focusDetector) {
      this.focusDetector.stop();
    }
  }

  addListener(element, event, handler, options = {}) {
    // Include capture flag so capture + bubble listeners don't clobber each other.
    const capture = !!(options && (options.capture || options === true));
    const key = `${element.constructor.name}-${event}-${capture ? 'c' : 'b'}`;
    
    if (this.listeners.has(key)) {
      this.removeListenerByKey(key);
    }
    
    element.addEventListener(event, handler, options);
    this.listeners.set(key, { element, event, handler, options, capture });
  }

  removeListenerByKey(key) {
    const listener = this.listeners.get(key);
    if (listener) {
      listener.element.removeEventListener(listener.event, listener.handler, listener.options);
      this.listeners.delete(key);
    }
  }

  removeListener(element, event) {
    // Remove both capture and bubble variants for this element+event.
    for (const capture of [true, false]) {
      const key = `${element.constructor.name}-${event}-${capture ? 'c' : 'b'}`;
      this.removeListenerByKey(key);
    }
  }

  removeAllListeners() {
    for (const [_key, listener] of this.listeners) {
      listener.element.removeEventListener(listener.event, listener.handler, listener.options);
    }
    this.listeners.clear();
  }

  handleKeyDown(_e) {
    // Override in implementation
  }

  handleMouseMove(_e) {
    // Override in implementation  
  }

  handleScroll(_e) {
    // Override in implementation
  }

  /**
   * @param {EventTarget|null|undefined} target
   * @returns {boolean}
   */
  isTypingContext(target) {
    return kpIsTypingContext(target);
  }

  /**
   * @returns {Element|null}
   */
  getDeepActiveElement() {
    return kpGetDeepActiveElement();
  }

  /**
   * @param {Event|null|undefined} e
   * @returns {EventTarget|null}
   */
  getComposedEventTarget(e) {
    return kpGetComposedEventTarget(e);
  }

  /**
   * @param {Event|null|undefined} e
   * @returns {Element|null}
   */
  resolveTypingTarget(e) {
    return kpResolveTypingTarget(e);
  }

  /**
   * @param {KeyboardEvent|null|undefined} e
   * @returns {boolean}
   */
  hasModifierKeys(e) {
    return kpHasModifierKeys(e);
  }
}



  // Module: src/modules/cursor.js
/**
 * Cursor overlay management
 */

class CursorManager {
  constructor() {
    this.cursorEl = null;
    this.lastPosition = { x: 0, y: 0 };
    this.isStuck = false;
    this.stuckCheckInterval = null;
    this.forceUpdateCount = 0;
    this.currentMode = null; // Cache current mode to avoid unnecessary updates
    this.currentModeKey = null; // Cache mode+options to allow updates within same mode
    this.uriCache = new Map(); // Cache generated URIs
  }

  ensure() {
    if (this.cursorEl) return;

    // Handoff from early-inject:
    // early-inject no longer creates a DOM cursor element, so we can't rely on
    // `#kpv2-cursor` existing to know early-inject is active. Instead, if the
    // early API exists, signal handoff once so early-inject stops managing UI.
    try {
      const earlyApi = window.KEYPILOT_EARLY;
      if (earlyApi && !window.__KP_EARLY_HANDOFF_DONE) {
        window.__KP_EARLY_HANDOFF_DONE = true;
        try {
          const earlyPosition = typeof earlyApi.getPosition === 'function' ? earlyApi.getPosition() : null;
          if (earlyPosition && typeof earlyPosition.x === 'number' && typeof earlyPosition.y === 'number') {
            this.lastPosition = earlyPosition;
          }
        } catch {
          // ignore
        }
        try {
          window.dispatchEvent(new CustomEvent('keypilot-main-loaded'));
        } catch {
          // ignore
        }
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot] Took over from early injection, using CSS cursor');
        }
      }
    } catch {
      // ignore
    }

    // Mark cursor as initialized (using CSS cursor, no DOM element needed)
    this.cursorEl = { style: {} }; // Dummy object for compatibility

    // IMPORTANT:
    // Do NOT clobber an existing cursor that may already be applied by `early-inject.js`.
    // The main KeyPilot instance will immediately call `setMode()` with the correct
    // settings-derived options once settings + initial mode are known.
    //
    // Previously, calling `setMode('none', {})` here overwrote the cursor CSS variable
    // with hardcoded defaults (gap=0, size=15) until the first hover/mousemove updated state.
  }

  setMode(mode, options = {}) {
    if (!this.cursorEl) return;

    // Skip if mode+options haven't changed to avoid unnecessary CSS updates.
    // NOTE: Text focus mode needs to update cursor color based on hasClickableElement.
    const {
      cursorType = null,
      crossHairQuadrantWidth = 15,
      gap = 0,
      strokeLineCap = 'round',
      strokeWidth = 4,
      crossHairScalingFactor = 1,
      hasClickableElement = false
    } = options || {};
    const nextModeKey = `${mode}|${cursorType || ''}|${crossHairQuadrantWidth}|${gap}|${strokeLineCap}|${strokeWidth}|${crossHairScalingFactor}|${hasClickableElement ? 1 : 0}`;

    if (this.currentModeKey === nextModeKey) {
      return;
    }

    this.currentMode = mode;
    this.currentModeKey = nextModeKey;

    // Native cursor options bypass SVG entirely.
    if (cursorType === 'native_arrow') {
      document.documentElement.style.setProperty('--kpv2-cursor', 'default');
      if (document.documentElement.style.cursor) document.documentElement.style.cursor = '';
      if (document.body.style.cursor) document.body.style.cursor = '';
      return;
    }
    if (cursorType === 'native_pointer') {
      document.documentElement.style.setProperty('--kpv2-cursor', 'pointer');
      if (document.documentElement.style.cursor) document.documentElement.style.cursor = '';
      if (document.body.style.cursor) document.body.style.cursor = '';
      return;
    }

    // Always set cursor to ensure it works over links and other elements
    const cursorUri = this.getCursorDataUri(mode, options);
    const cursorValue = `url("${cursorUri}") 30 30, auto`;

    console.log('setMode: cursorValue', cursorValue);
    // Set cursor using CSS variable on document element
    // The StyleManager has a global rule to apply this to all elements
    document.documentElement.style.setProperty('--kpv2-cursor', cursorValue);

    // Clean up direct styles if they exist - they can interfere with the variable
    if (document.documentElement.style.cursor) document.documentElement.style.cursor = '';
    if (document.body.style.cursor) document.body.style.cursor = '';
  }


  updatePosition(x, y) {
    if (!this.cursorEl) return;
    // CSS cursor doesn't need position updates - browser handles it automatically
    // This method is kept for API compatibility but does nothing
    this.lastPosition = { x, y };
  }

  getCurrentMode() {
    return this.currentMode || 'none';
  }

  /**
   * Generate SVG data URI for cursor mode
   */
  getCursorDataUri(mode, options = {}) {
    // Extract parameters with defaults
    const {
      cursorType = null,
      crossHairQuadrantWidth = 15,
      gap = 0,
      strokeLineCap = 'round',
      strokeWidth = 4,
      crossHairScalingFactor = 1,
      hasClickableElement = false
    } = options;

    // Apply scaling factor
    const scaledGap = gap * crossHairScalingFactor;
    const scaledWidth = crossHairQuadrantWidth * crossHairScalingFactor;

    // Calculate segment positions (center is now at 30,30 for 60x60 canvas)
    const centerX = 30;
    const centerY = 30;
    
    let segmentStart, segmentEnd, segmentStart2, segmentEnd2;
    
    if (scaledGap === 0) {
      // Intersecting lines: bars extend from center outward
      segmentStart = centerY - scaledWidth;
      segmentEnd = centerY;
      segmentStart2 = centerY;
      segmentEnd2 = centerY + scaledWidth;
    } else {
      // Four separate bars: each bar is `gap` pixels away from center
      segmentStart = centerY - scaledGap - scaledWidth;
      segmentEnd = centerY - scaledGap;
      segmentStart2 = centerY + scaledGap;
      segmentEnd2 = centerY + scaledGap + scaledWidth;
    }

    const cacheKey = `${mode}-${cursorType || ''}-${crossHairQuadrantWidth}-${gap}-${strokeLineCap}-${strokeWidth}-${crossHairScalingFactor}-${hasClickableElement ? 1 : 0}`;
    if (this.uriCache.has(cacheKey)) {
      return this.uriCache.get(cacheKey);
    }

    let svgContent = '';

    if (mode === 'text_focus' && cursorType === 't_square') {
      // Orange by default; green when a clickable element is armed/hovered in text focus.
      const color = hasClickableElement ? COLORS.FOCUS_GREEN_BRIGHT : COLORS.ORANGE;
      const scale = crossHairScalingFactor || 1;
      const half = 14 * scale;
      const x = 30 - half;
      const y = 30 - half;
      const w = 2 * half;
      const h = 2 * half;
      const tHalf = half * 0.6;
      const tTopY = 30 - half * 0.45;
      const tBottomY = 30 + half * 0.7;
      svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
        <rect x="${x}" y="${y}" width="${w}" height="${h}" fill="none" stroke="${color}" stroke-width="${strokeWidth}" />
        <line x1="${30 - tHalf}" y1="${tTopY}" x2="${30 + tHalf}" y2="${tTopY}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="30" y1="${tTopY}" x2="30" y2="${tBottomY}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
      </svg>`;
    } else if (mode === 'text_focus') {
      // Orange crosshair by default; green when a clickable element is armed/hovered in text focus.
      const color = hasClickableElement ? COLORS.FOCUS_GREEN_BRIGHT : COLORS.ORANGE;
      svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
        <line x1="30" y1="${segmentStart}" x2="30" y2="${segmentEnd}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="30" y1="${segmentStart2}" x2="30" y2="${segmentEnd2}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="${segmentStart}" y1="30" x2="${segmentEnd}" y2="30" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="${segmentStart2}" y1="30" x2="${segmentEnd2}" y2="30" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
      </svg>`;
    } else if (mode === 'delete') {
      // Red X - scaled for 60x60 canvas
      svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
        <line x1="12" y1="12" x2="48" y2="48" stroke="${COLORS.DELETE_RED}" stroke-width="5" stroke-linecap="round"/>
        <line x1="48" y1="12" x2="12" y2="48" stroke="${COLORS.DELETE_RED}" stroke-width="5" stroke-linecap="round"/>
      </svg>`;
    } else if (mode === 'cols') {
      // Purple column-grid cursor (two vertical columns)
      const color = COLORS.COLS_PURPLE || 'rgba(156,39,176,0.95)';
      svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
        <rect x="10" y="12" width="16" height="36" rx="1" fill="none" stroke="${color}" stroke-width="3"/>
        <rect x="34" y="12" width="16" height="36" rx="1" fill="none" stroke="${color}" stroke-width="3"/>
        <line x1="13" y1="20" x2="23" y2="20" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <line x1="13" y1="28" x2="23" y2="28" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <line x1="13" y1="36" x2="23" y2="36" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <line x1="37" y1="20" x2="47" y2="20" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <line x1="37" y1="28" x2="47" y2="28" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <line x1="37" y1="36" x2="47" y2="36" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
      </svg>`;
    } else if (mode === 'highlight') {
      // Blue crosshair
      const color = COLORS.HIGHLIGHT_BLUE;
      svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
        <line x1="30" y1="${segmentStart}" x2="30" y2="${segmentEnd}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="30" y1="${segmentStart2}" x2="30" y2="${segmentEnd2}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="${segmentStart}" y1="30" x2="${segmentEnd}" y2="30" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="${segmentStart2}" y1="30" x2="${segmentEnd2}" y2="30" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
      </svg>`;
    } else {
      // Default green crosshair
      const color = COLORS.FOCUS_GREEN_BRIGHT;
      svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
        <line x1="30" y1="${segmentStart}" x2="30" y2="${segmentEnd}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="30" y1="${segmentStart2}" x2="30" y2="${segmentEnd2}" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="${segmentStart}" y1="30" x2="${segmentEnd}" y2="30" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
        <line x1="${segmentStart2}" y1="30" x2="${segmentEnd2}" y2="30" stroke="${color}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLineCap}"/>
      </svg>`;
    }

    // Convert to data URI
    const encoded = encodeURIComponent(svgContent);
    const uri = `data:image/svg+xml,${encoded}`;
    this.uriCache.set(cacheKey, uri);
    return uri;
  }

  hide() {
    if (this.cursorEl) {
      // Hide cursor by setting the CSS variable to default
      document.documentElement.style.setProperty('--kpv2-cursor', 'default');
    }
  }

  show() {
    if (this.cursorEl) {
      // Show cursor by restoring current mode
      const currentMode = this.getCurrentMode();
      // Reset currentMode to force update in setMode
      const oldMode = this.currentMode;
      this.currentMode = null; 
      this.currentModeKey = null;
      this.setMode(currentMode, {});
    }
  }





  cleanup() {
    if (this.stuckCheckInterval) {
      clearInterval(this.stuckCheckInterval);
      this.stuckCheckInterval = null;
    }

    // Remove CSS cursor
    document.documentElement.style.cursor = '';
    document.body.style.cursor = '';
    // Also remove the CSS variable
    document.documentElement.style.removeProperty('--kpv2-cursor');
    
    this.cursorEl = null;
    this.currentMode = null; // Reset mode so setMode will update when re-enabled
    this.currentModeKey = null;
  }

  createElement(tag, props = {}, ...children) {
    const node = document.createElement(tag);
    for (const [k, v] of Object.entries(props)) {
      if (v == null) continue;
      if (k === 'className') node.className = v;
      else if (k === 'text') node.textContent = v;
      else node.setAttribute(k, v);
    }
    for (const c of children) {
      if (c == null) continue;
      node.appendChild(typeof c === 'string' ? document.createTextNode(c) : c);
    }
    return node;
  }
}


  // Module: src/modules/element-detector.js
/**
 * Element detection and interaction utilities
 */

class ElementDetector {
  constructor() {
    this.CLICKABLE_ROLES = ['link', 'button', 'slider', 'checkbox', 'radio', 'tab', 'menuitem', 'option', 'switch', 'treeitem', 'combobox', 'spinbutton'];

    // Composite widgets / structural containers that often host delegated click
    // listeners. A tracked click handler alone must not make the entire region
    // a KeyPilot hover target — the leaf items (tabs, options, etc.) are.
    this.COMPOSITE_CONTAINER_ROLES = [
      'tablist', 'menu', 'menubar', 'listbox', 'tree', 'grid', 'radiogroup',
      'toolbar', 'group', 'navigation', 'list', 'directory', 'rowgroup', 'table'
    ];

    this.CLICKABLE_SEL = 'a[href], button, input, select, textarea, video, audio, summary';
    // Include <summary> so details/summary groups (e.g. New Tab recent-history outlines)
    // are semantic hover/F targets for the full header, not only cursor:pointer leaves.
    this.FOCUSABLE_SEL = 'a[href], button, input, select, textarea, video, audio, summary, [contenteditable="true"], [role="button"], [role="link"], [role="checkbox"], [role="radio"], [role="tab"], [data-action], [data-toggle], [data-click], [data-href], [data-link], [vue-click], [ng-click]';

    // Track elements with addEventListener click handlers
    this.clickHandlerElements = new WeakSet();

    // Depth for nested _withNativePageCursors calls (one suspend/restore pair).
    this._nativeCursorSuspendDepth = 0;
    this._nativeCursorWasHidden = false;

    // Wrap addEventListener to track click handlers
    this.setupEventListenerTracking();
  }

  /**
   * Run `fn` with KeyPilot's custom-cursor override temporarily disabled so
   * getComputedStyle(...).cursor reflects the page's real cursor (e.g. pointer).
   *
   * When CUSTOM_CURSORS is on, style-manager forces:
   *   html.kpv2-cursor-hidden * { cursor: var(--kpv2-cursor) !important; }
   * which makes every element report the crosshair (or other custom cursor) and
   * hides CSS cursor:pointer signals used to find non-semantic clickables.
   *
   * Suspend/restore is synchronous and re-entrant — no paint should occur mid-task.
   * @template T
   * @param {() => T} fn
   * @returns {T}
   */
  _withNativePageCursors(fn) {
    let html = null;
    try { html = document.documentElement; } catch { /* ignore */ }

    if (!html || !html.classList) {
      return fn();
    }

    if (this._nativeCursorSuspendDepth > 0) {
      this._nativeCursorSuspendDepth++;
      try {
        return fn();
      } finally {
        this._nativeCursorSuspendDepth--;
      }
    }

    const hadHidden = html.classList.contains(CSS_CLASSES.CURSOR_HIDDEN);
    this._nativeCursorWasHidden = hadHidden;
    if (hadHidden) {
      try { html.classList.remove(CSS_CLASSES.CURSOR_HIDDEN); } catch { /* ignore */ }
    }
    this._nativeCursorSuspendDepth = 1;
    try {
      return fn();
    } finally {
      this._nativeCursorSuspendDepth = 0;
      if (hadHidden) {
        try { html.classList.add(CSS_CLASSES.CURSOR_HIDDEN); } catch { /* ignore */ }
      }
      this._nativeCursorWasHidden = false;
    }
  }

  /**
   * Computed cursor with custom-cursor override suspended.
   * @param {Element} el
   * @returns {string}
   */
  _getPageComputedCursor(el) {
    if (!el || !window.getComputedStyle) return '';
    return this._withNativePageCursors(() => {
      try {
        return window.getComputedStyle(el).cursor || '';
      } catch {
        return '';
      }
    });
  }

  /**
   * True when el is a layout/composite container that should not become a
   * clickable hover target solely because of a delegated click listener.
   * @param {Element} el
   * @param {string} [role]
   * @returns {boolean}
   */
  isCompositeClickContainer(el, role = '') {
    if (!el || el.nodeType !== 1) return false;
    const r = (role || (el.getAttribute && (el.getAttribute('role') || '').trim().toLowerCase()) || '');
    if (r && this.COMPOSITE_CONTAINER_ROLES.includes(r)) return true;
    const tag = el.tagName;
    return tag === 'NAV' || tag === 'UL' || tag === 'OL' || tag === 'MENU' ||
      tag === 'TABLE' || tag === 'TBODY' || tag === 'THEAD' || tag === 'TFOOT';
  }

  setupEventListenerTracking() {
    // Store the original addEventListener
    const originalAddEventListener = EventTarget.prototype.addEventListener;

    // Wrap addEventListener to track click handlers
    EventTarget.prototype.addEventListener = function(type, listener, options) {
      // Call the original method
      originalAddEventListener.call(this, type, listener, options);

      // Track click handlers
      if (type === 'click' && this instanceof Element) {
        try {
          // Use a WeakSet to avoid memory leaks
          elementDetectorInstance.clickHandlerElements.add(this);
        } catch {
          // Ignore errors in tracking
        }
      }
    };

    // Also track the element detector instance for the wrapped function
    window.elementDetectorInstance = this;
  }

  hasTrackedClickHandler(el) {
    return this.clickHandlerElements.has(el);
  }

  /**
   * Like Node.contains, but crosses open shadow boundaries via host hops.
   * archive.org tiles: clickable <a> in tile-dispatcher wraps <collection-tile>
   * whose internals live in a nested shadow — plain contains() is always false
   * for those descendants, which breaks sticky hover and nested-chrome checks.
   *
   * @param {Node|null|undefined} host
   * @param {Node|null|undefined} node
   * @returns {boolean}
   */
  composedContains(host, node) {
    if (!host || !node || host.nodeType !== 1) return false;
    if (host === node) return true;
    try {
      if (host.contains(node)) return true;
    } catch { /* ignore */ }

    let n = node;
    let depth = 0;
    while (n && depth++ < 32) {
      if (n === host) return true;
      try {
        if (n.parentElement) {
          n = n.parentElement;
          continue;
        }
        const root = typeof n.getRootNode === 'function' ? n.getRootNode() : null;
        if (root && typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot) {
          n = root.host || null;
          continue;
        }
      } catch {
        break;
      }
      break;
    }
    return false;
  }

  deepElementFromPoint(x, y) {
    let el = document.elementFromPoint(x, y);
    let guard = 0;
    while (el && el.shadowRoot && guard++ < 10) {
      const nested = el.shadowRoot.elementFromPoint(x, y);
      if (!nested || nested === el) break;
      el = nested;
    }
    return el;
  }

  /**
   * True when this element itself sets cursor:pointer (not merely inheriting it).
   * Sites like suno.com put `cursor-pointer` on <body>, which would otherwise make
   * every node look interactive via getComputedStyle().cursor.
   *
   * When KeyPilot custom cursors are enabled, page cursor styles are overridden
   * with !important — this method suspends that override before reading computed
   * styles so cursor:pointer-only clickables still get hover outlines.
   */
  hasExplicitCursorPointer(el) {
    if (!el || el.nodeType !== 1) return false;
    try {
      if (el === document.body || el === document.documentElement) return false;
    } catch { /* ignore */ }

    try {
      if (el.style && String(el.style.cursor || '').toLowerCase() === 'pointer') {
        return true;
      }
    } catch { /* ignore */ }

    try {
      const cls = typeof el.className === 'string'
        ? el.className
        : (el.className && typeof el.className.baseVal === 'string' ? el.className.baseVal : '');
      if (cls && (/\bcursor-pointer\b/i.test(cls) || /\bcursorPointer\b/.test(cls))) {
        return true;
      }
    } catch { /* ignore */ }

    // Local CSS rule: computed pointer while parent is not pointer.
    // Must read with custom-cursor override suspended (see _withNativePageCursors).
    try {
      if (!window.getComputedStyle) return false;
      return this._withNativePageCursors(() => {
        const own = window.getComputedStyle(el).cursor;
        if (own !== 'pointer') return false;
        const parent = el.parentElement;
        if (!parent) return false;
        return window.getComputedStyle(parent).cursor !== 'pointer';
      });
    } catch {
      return false;
    }
  }

  isLikelyInteractive(el, opts = {}) {
    if (!el || el.nodeType !== 1) return false;

    // Never treat the page root as a clickable hover target. Sites often put
    // delegated click listeners on body/html; if we accept those, hover can
    // "stick" on the previous real target when the pointer moves to empty chrome.
    try {
      if (el === document.body || el === document.documentElement) return false;
      if (el.tagName === 'BODY' || el.tagName === 'HTML') return false;
    } catch { /* ignore */ }
    
    const allowCursor = (opts && Object.prototype.hasOwnProperty.call(opts, 'allowCursor'))
      ? !!opts.allowCursor
      : true;

    const matchesSelector = el.matches(this.FOCUSABLE_SEL);
    const role = (el.getAttribute && (el.getAttribute('role') || '').trim().toLowerCase()) || '';
    const hasRole = role && this.CLICKABLE_ROLES.includes(role);

    // Explicit onclick attribute still counts as intentional interactivity.
    // Tracked addEventListener('click') alone is weaker — often event delegation
    // on a parent list/nav/tablist — and must not light up the whole container.
    const hasInlineClick = !!(el.onclick || (el.getAttribute && el.getAttribute('onclick')));
    const hasTrackedClick = this.hasTrackedClickHandler(el);
    let hasClickHandler = hasInlineClick || hasTrackedClick;
    if (hasClickHandler && !matchesSelector && !hasRole && !hasInlineClick &&
        this.isCompositeClickContainer(el, role)) {
      hasClickHandler = false;
    }

    // getComputedStyle() is relatively expensive; only use it as a last resort.
    // Ignore inherited cursor:pointer from body/html-wide styles.
    let hasCursor = false;
    if (allowCursor && !matchesSelector && !hasRole && !hasClickHandler) {
      hasCursor = this.hasExplicitCursorPointer(el);
    }

    // Debug logging
    if (window.KEYPILOT_DEBUG && (matchesSelector || hasRole || hasClickHandler || hasCursor)) {
      console.log('[KeyPilot Debug] isLikelyInteractive:', {
        tagName: el.tagName,
        href: el.href,
        matchesSelector: matchesSelector,
        role: role,
        hasRole: hasRole,
        hasClickHandler: !!hasClickHandler,
        hasTrackedClickHandler: this.hasTrackedClickHandler(el),
        hasCursor: hasCursor,
        allowCursor: allowCursor,
        selector: this.FOCUSABLE_SEL
      });
    }

    return matchesSelector || hasRole || hasClickHandler || hasCursor;
  }

  /**
   * Whether an element is (or belongs to) a media/progress scrubber UI.
   * Used for focus styling (no link-style fill) and activation routing.
   */
  isScrubberLike(el) {
    if (!el || el.nodeType !== 1) return false;
    try {
      if (this.isNativeType(el, 'range')) return true;
      const role = (el.getAttribute('role') || '').trim().toLowerCase();
      if (role === 'slider') return true;
      if (this.looksLikeScrubTrack(el)) return true;
      if (typeof el.closest === 'function') {
        if (el.closest('[role="slider"], input[type="range"]')) return true;
      }
      return false;
    } catch {
      return false;
    }
  }

  /**
   * Thin, wide interactive bar — typical custom progress/seek track (Rumble, Suno presentation).
   * Strict geometry only; no "nearby control" search.
   */
  looksLikeScrubTrack(el) {
    if (!el || el.nodeType !== 1) return false;
    try {
      if (el.tagName === 'A' || el.tagName === 'BUTTON' || el.tagName === 'VIDEO' || el.tagName === 'AUDIO') {
        return false;
      }
      // Never treat real controls as tracks.
      if (typeof el.closest === 'function') {
        if (el.closest('button, [role="button"], a[href], select, textarea')) return false;
      }

      const role = (el.getAttribute('role') || '').trim().toLowerCase();
      if (role && role !== 'presentation' && role !== 'none' && role !== 'slider' && role !== 'progressbar') {
        if (this.CLICKABLE_ROLES.includes(role) && role !== 'slider') return false;
      }

      const rect = el.getBoundingClientRect();
      if (!rect || rect.width < 64 || rect.height <= 0 || rect.height > 28) return false;
      if (rect.width / Math.max(rect.height, 1) < 4) return false;

      let pe = 'auto';
      try { pe = window.getComputedStyle(el).pointerEvents; } catch { /* ignore */ }
      if (pe === 'none') return false;

      // Pointer / presentation / progress semantics on this node (not inherited body cursor alone).
      if (
        this.hasExplicitCursorPointer(el) ||
        role === 'presentation' ||
        role === 'slider' ||
        role === 'progressbar'
      ) {
        return true;
      }

      // Rumble-style: cursor may be set on a parent chrome node via inline style.
      // Read with custom-cursor override suspended so CUSTOM_CURSORS mode still works.
      const cursor = this._getPageComputedCursor(el);
      if (cursor !== 'pointer') return false;
      let p = el.parentElement;
      let d = 0;
      while (p && d < 3) {
        if (this.hasExplicitCursorPointer(p)) return true;
        // Parent is also a thin wide bar (track stack).
        try {
          const pr = p.getBoundingClientRect();
          if (pr.width >= rect.width * 0.9 && pr.height > 0 && pr.height <= 28) return true;
        } catch { /* ignore */ }
        p = p.parentElement;
        d++;
      }
      return false;
    } catch {
      return false;
    }
  }

  /**
   * Structural range/slider for the hit element only — no proximity / playbar scanning.
   * Thumb-style players: range is a sibling of the visual track under the same trackHost.
   * @param {Element} el
   * @returns {HTMLElement|null}
   */
  findRelatedRangeOrSlider(el) {
    if (!el || el.nodeType !== 1) return null;

    try {
      if (this.isNativeType(el, 'range')) return /** @type {HTMLElement} */ (el);
      const role = (el.getAttribute('role') || '').trim().toLowerCase();
      if (role === 'slider') return /** @type {HTMLElement} */ (el);
    } catch { /* ignore */ }

    // Direct ancestor only (not "find any range in the playbar").
    try {
      if (typeof el.closest === 'function') {
        const viaClosest = el.closest('input[type="range"], [role="slider"]');
        if (viaClosest) return /** @type {HTMLElement} */ (viaClosest);
      }
    } catch { /* ignore */ }

    // Structural sibling thumb: only when this node (or a short ancestor) is the scrub track.
    // trackHost > [presentation track | wrapper > input[type=range]]
    let n = el;
    let depth = 0;
    while (n && n.nodeType === 1 && depth < 5) {
      const onTrack =
        this.looksLikeScrubTrack(n) ||
        ((n.getAttribute?.('role') || '').trim().toLowerCase() === 'presentation' &&
          (() => {
            try {
              const r = n.getBoundingClientRect();
              return r.width >= 64 && r.height > 0 && r.height <= 28;
            } catch {
              return false;
            }
          })());

      if (onTrack && n.parentElement) {
        for (const sib of n.parentElement.children) {
          if (!sib || sib === n || sib.nodeType !== 1) continue;
          if (this.isNativeType(sib, 'range')) return /** @type {HTMLElement} */ (sib);
          const r = (sib.getAttribute?.('role') || '').trim().toLowerCase();
          if (r === 'slider') return /** @type {HTMLElement} */ (sib);
          // One level only: wrapper that holds the thumb-sized range input.
          try {
            for (const child of sib.children || []) {
              if (this.isNativeType(child, 'range')) return /** @type {HTMLElement} */ (child);
              const cr = (child.getAttribute?.('role') || '').trim().toLowerCase();
              if (cr === 'slider') return /** @type {HTMLElement} */ (child);
            }
          } catch { /* ignore */ }
        }
      }

      n = n.parentElement;
      depth++;
    }

    return null;
  }

  /**
   * Resolve a scrubber for the hit element only (track / range / role=slider).
   * Does not pull in nearby play/volume buttons via playbar-wide searches.
   * @param {Element|null} el
   * @param {number} [clientX]
   * @param {number} [clientY]
   * @returns {{ kind: 'range'|'role-slider'|'track', control: HTMLElement, track: HTMLElement }|null}
   */
  resolveScrubber(el, clientX, clientY) {
    if (!el || el.nodeType !== 1) return null;

    // Play/volume/nav controls must never resolve as the progress scrubber.
    try {
      if (el.tagName === 'A' || el.tagName === 'TEXTAREA' || el.tagName === 'BUTTON') return null;
      if (el.tagName === 'INPUT') {
        const t = (el.getAttribute('type') || 'text').toLowerCase();
        if (t !== 'range') return null;
      }
      if (typeof el.closest === 'function') {
        if (el.closest('button, [role="button"], a[href], select, textarea')) return null;
      }
    } catch { /* ignore */ }

    const control = this.findRelatedRangeOrSlider(el);
    if (control) {
      const kind = this.isNativeType(control, 'range') ? 'range' : 'role-slider';
      const track = this.getScrubTrackElement(control, el) || control;
      return { kind, control, track: /** @type {HTMLElement} */ (track) };
    }

    // Custom track with no ARIA/range (Rumble-style pure div scrubber).
    let n = el;
    let depth = 0;
    while (n && n.nodeType === 1 && depth < 5) {
      if (this.looksLikeScrubTrack(n)) {
        try {
          const r = n.getBoundingClientRect();
          if (r.width > window.innerWidth * 0.98 && r.height > 40) {
            n = n.parentElement;
            depth++;
            continue;
          }
        } catch { /* ignore */ }

        let track = /** @type {HTMLElement} */ (n);
        let p = n.parentElement;
        let up = 0;
        while (p && up < 3) {
          if (this.looksLikeScrubTrack(p)) {
            try {
              const pr = p.getBoundingClientRect();
              const tr = track.getBoundingClientRect();
              if (pr.width >= tr.width * 0.9 && pr.height <= 28) track = /** @type {HTMLElement} */ (p);
            } catch { /* ignore */ }
          }
          p = p.parentElement;
          up++;
        }

        return { kind: 'track', control: track, track };
      }
      n = n.parentElement;
      depth++;
    }

    return null;
  }

  /**
   * For thumb-sized range inputs, return the full-width track element used for geometry/seek.
   * @param {Element} control - range or role=slider
   * @param {Element} [hintEl] - original hit element
   * @returns {HTMLElement|null}
   */
  getScrubTrackElement(control, hintEl) {
    if (!control || control.nodeType !== 1) return null;

    try {
      if ((control.getAttribute('role') || '').trim().toLowerCase() === 'slider') {
        return /** @type {HTMLElement} */ (control);
      }
    } catch { /* ignore */ }

    let controlRect = null;
    try { controlRect = control.getBoundingClientRect(); } catch { controlRect = null; }

    // Prefer an explicit presentation sibling/host around a tiny thumb input.
    const roots = [];
    try {
      if (control.parentElement) roots.push(control.parentElement);
      if (control.parentElement?.parentElement) roots.push(control.parentElement.parentElement);
      if (control.parentElement?.parentElement?.parentElement) {
        roots.push(control.parentElement.parentElement.parentElement);
      }
      if (hintEl && hintEl !== control) roots.push(hintEl, hintEl.parentElement);
    } catch { /* ignore */ }

    for (const root of roots) {
      if (!root || !root.querySelector) continue;
      try {
        const presentation = root.querySelector?.(':scope > [role="presentation"], [role="presentation"]');
        if (presentation) {
          const pr = presentation.getBoundingClientRect();
          if (pr.width > 40 && (!controlRect || pr.width > controlRect.width * 2)) {
            return /** @type {HTMLElement} */ (presentation);
          }
        }
      } catch { /* ignore */ }

      // Host that is substantially wider than the thumb is the track.
      try {
        const rr = root.getBoundingClientRect();
        if (controlRect && rr.width > controlRect.width * 3 && rr.height <= 48 && rr.height > 0) {
          return /** @type {HTMLElement} */ (root);
        }
      } catch { /* ignore */ }
    }

    // Walk up for a significantly wider horizontal host.
    let n = control.parentElement;
    let depth = 0;
    while (n && depth < 6) {
      try {
        const rr = n.getBoundingClientRect();
        if (controlRect && rr.width > Math.max(controlRect.width * 3, 80) && rr.height <= 40) {
          // Prefer a presentation child if present.
          const pres = n.querySelector?.('[role="presentation"]');
          if (pres) {
            const pr = pres.getBoundingClientRect();
            if (pr.width > 40) return /** @type {HTMLElement} */ (pres);
          }
          return /** @type {HTMLElement} */ (n);
        }
      } catch { /* ignore */ }
      n = n.parentElement;
      depth++;
    }

    // Hint element itself may be the track fill.
    if (hintEl && this.looksLikeScrubTrack(hintEl)) {
      return /** @type {HTMLElement} */ (hintEl);
    }

    return /** @type {HTMLElement} */ (control);
  }

  findClickable(el) {
    // Batch cursor-override suspension for the whole ancestor walk so we don't
    // toggle html.kpv2-cursor-hidden once per node when probing cursor:pointer.
    return this._withNativePageCursors(() => this._findClickableUnsuspended(el));
  }

  _findClickableUnsuspended(el) {
    // Standard interactive walk only — no scrubber "proximity" remapping of hover focus.
    // F-activation still resolves scrubbers from the hit target in ActivationHandler.
    let n = el;
    let depth = 0;
    let cursorOnlyCandidate = null;
    // Depth 20: archive.org-style nested open shadows (tile-dispatcher → collection-tile → …)
    // can sit well below 10 steps from the deepest hit target.
    while (n && n !== document.body && n.nodeType === 1 && depth < 20) {
      // Prefer semantic clickables (anchors/buttons/roles/handlers/etc.) over cursor:pointer-only
      // descendants. This avoids returning child <img>/<div> nodes inside <a href> that inherit
      // cursor:pointer from the anchor.
      if (this.isLikelyInteractive(n, { allowCursor: false })) {
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot Debug] findClickable found:', {
            tagName: n.tagName,
            href: n.href,
            className: n.className,
            depth: depth
          });
        }
        return n;
      }

      // Cursor-pointer-only fallback: store the first cursor candidate but keep walking up.
      // If we later find a semantic interactive ancestor, we'll return that instead.
      if (!cursorOnlyCandidate && this.isLikelyInteractive(n, { allowCursor: true })) {
        cursorOnlyCandidate = n;
      }

      n = n.parentElement || (n.getRootNode() instanceof ShadowRoot ? n.getRootNode().host : null);
      depth++;
    }

    const finalResult = cursorOnlyCandidate || (el && this.isLikelyInteractive(el) ? el : null);
    if (window.KEYPILOT_DEBUG && !finalResult && el) {
      console.log('[KeyPilot Debug] findClickable found nothing for:', {
        tagName: el.tagName,
        href: el.href,
        className: el.className
      });
    }

    return finalResult;
  }

  /**
   * True when `el` is nested chrome inside `host` (More menu, small sub-links,
   * icon buttons) rather than the primary hover target for the row/card/tab.
   * @param {Element} el
   * @param {Element} host
   * @returns {boolean}
   */
  _isNestedHoverChrome(el, host) {
    if (!el || !host || el === host || el.nodeType !== 1 || host.nodeType !== 1) return false;
    try {
      if (!this.composedContains(host, el)) return false;
    } catch {
      return false;
    }

    const tag = el.tagName;
    const role = ((el.getAttribute && el.getAttribute('role')) || '').trim().toLowerCase();
    if (tag === 'BUTTON' || role === 'button') return true;
    if (el.getAttribute && el.getAttribute('aria-haspopup')) return true;

    // Small nested links inside a larger row/card (trend "with X", avatars, etc.).
    if (tag === 'A' || role === 'link') {
      try {
        const er = el.getBoundingClientRect();
        const hr = host.getBoundingClientRect();
        const eArea = Math.max(1, er.width * er.height);
        const hArea = Math.max(1, hr.width * hr.height);
        return eArea < hArea * 0.35;
      } catch {
        return true;
      }
    }
    return false;
  }

  /**
   * Prefer a stable "host" interactive for hover chrome: row-sized role=link
   * cards and role=tab strips, instead of nested More/buttons/sub-links.
   * @param {Element} leaf
   * @returns {Element|null}
   */
  _findPreferableHoverHost(leaf) {
    if (!leaf || leaf.nodeType !== 1) return null;

    /** @type {Element|null} */
    let hostLink = null;
    /** @type {Element|null} */
    let hostTab = null;
    let n = leaf;
    let depth = 0;
    while (n && n !== document.body && n.nodeType === 1 && depth++ < 14) {
      const role = ((n.getAttribute && n.getAttribute('role')) || '').trim().toLowerCase();
      if (role === 'tab') hostTab = n;
      if (role === 'link') {
        try {
          const r = n.getBoundingClientRect();
          // Row/card-ish: wide enough and tall enough to be the primary target.
          if (r.width >= 100 && r.height >= 32) hostLink = n;
        } catch { /* ignore */ }
      }
      n = n.parentElement || (n.getRootNode instanceof Function && n.getRootNode() instanceof ShadowRoot
        ? n.getRootNode().host
        : null);
    }

    // Tabs: always prefer the tab host over an inner label/button.
    if (hostTab && (leaf === hostTab || this.composedContains(hostTab, leaf))) return hostTab;

    // Link rows/cards: promote nested chrome up to the host row.
    if (hostLink && this.composedContains(hostLink, leaf) && leaf !== hostLink) {
      if (this._isNestedHoverChrome(leaf, hostLink)) return hostLink;
    }
    return null;
  }

  /**
   * Resolve the element that should receive hover focus chrome / F-activate.
   * Stabilizes against nested controls and brief nulls while still inside the
   * previous host (generic fix for list rows, tabs, cards — not site-specific).
   *
   * @param {Element|null|undefined} underEl - Deepest node under the pointer
   * @param {Element|null|undefined} prevFocus - Previous hover focus element
   * @returns {Element|null}
   */
  resolveHoverFocusTarget(underEl, prevFocus = null) {
    const under = underEl && underEl.nodeType === 1 ? underEl : null;
    const prev = prevFocus && prevFocus.nodeType === 1 && prevFocus.isConnected
      ? prevFocus
      : null;

    // Sticky: pointer still inside previous host → keep it when leaf is missing
    // or is nested chrome (avoids thrash / clear between children).
    // Use composedContains so open-shadow tile content (archive.org collection-tile
    // inside tile-dispatcher <a>) still counts as "inside" the focused link.
    if (prev && under) {
      try {
        if (this.composedContains(prev, under)) {
          const leafInside = this.findClickable(under);
          if (!leafInside || leafInside === prev || this._isNestedHoverChrome(leafInside, prev)) {
            return prev;
          }
          // Leaf is a distinct primary target inside prev (e.g. large nested link):
          // still prefer host for row-sized role=link / tab.
          const host = this._findPreferableHoverHost(leafInside);
          if (host === prev) return prev;
          if (host) return host;
          return leafInside;
        }
      } catch { /* ignore */ }
    }

    if (!under) return null;

    const leaf = this.findClickable(under);
    if (!leaf) return null;

    const host = this._findPreferableHoverHost(leaf);
    return host || leaf;
  }

  isTextLike(el) {
    if (!el || el.nodeType !== 1) return false;
    if (el.tagName === 'TEXTAREA') return true;
    if (el.tagName === 'INPUT') {
      const t = (el.getAttribute('type') || 'text').toLowerCase();
      return ['text', 'search', 'url', 'email', 'tel', 'password', 'number'].includes(t);
    }
    return false;
  }

  isNativeType(el, type) {
    return el && el.tagName === 'INPUT' && (el.getAttribute('type') || '').toLowerCase() === type;
  }

  isContentEditable(el) {
    if (!el || el.nodeType !== 1) return false;
    return el.isContentEditable || el.getAttribute('contenteditable') === 'true';
  }

  /**
   * Classify an interactive element into a clickable category.
   * Categories drive hover chrome, F-key feedback, and activation — sliders are
   * not treated as links even when they share a player bar with other controls.
   *
   * Priority: text > slider > button > link > media > control > generic
   *
   * @param {Element|null} el
   * @returns {string} One of CLICKABLE_CATEGORY values
   */
  getClickableCategory(el) {
    if (!el || el.nodeType !== 1) return CLICKABLE_CATEGORY.NONE;

    try {
      // 1) Text entry
      if (this.isTextLike(el) || this.isContentEditable(el)) {
        return CLICKABLE_CATEGORY.TEXT;
      }

      // 2) Slider / scrubber (before link — track fills must not inherit "link" UI)
      if (this.isNativeType(el, 'range')) return CLICKABLE_CATEGORY.SLIDER;
      const role = (el.getAttribute?.('role') || '').trim().toLowerCase();
      if (role === 'slider' || role === 'progressbar') return CLICKABLE_CATEGORY.SLIDER;
      if (this.looksLikeScrubTrack(el)) return CLICKABLE_CATEGORY.SLIDER;
      try {
        if (typeof el.closest === 'function' &&
            el.closest('input[type="range"], [role="slider"], [role="progressbar"]')) {
          return CLICKABLE_CATEGORY.SLIDER;
        }
      } catch { /* ignore */ }

      // 3) Button / disclosure summary — findClickable returns the host when hovering children
      if (el.tagName === 'BUTTON' || role === 'button' || el.tagName === 'SUMMARY') {
        return CLICKABLE_CATEGORY.BUTTON;
      }
      try {
        if (typeof el.matches === 'function' && el.matches('[role="button"]')) {
          return CLICKABLE_CATEGORY.BUTTON;
        }
      } catch { /* ignore */ }

      // 4) Link (navigation) — only when the resolved target is the link itself
      if (el.tagName === 'A' && /** @type {any} */ (el).href) return CLICKABLE_CATEGORY.LINK;
      if (role === 'link') return CLICKABLE_CATEGORY.LINK;
      try {
        if (typeof el.matches === 'function' && el.matches('a[href], [role="link"]')) {
          return CLICKABLE_CATEGORY.LINK;
        }
      } catch { /* ignore */ }

      // 5) Media surface (video/audio element or host that is the media control itself)
      if (el.tagName === 'VIDEO' || el.tagName === 'AUDIO') return CLICKABLE_CATEGORY.MEDIA;

      // 6) Other form / ARIA controls
      if (el.tagName === 'SELECT') return CLICKABLE_CATEGORY.CONTROL;
      if (this.isNativeType(el, 'checkbox') || this.isNativeType(el, 'radio') ||
          this.isNativeType(el, 'file') || this.isNativeType(el, 'color') ||
          this.isNativeType(el, 'date') || this.isNativeType(el, 'time')) {
        return CLICKABLE_CATEGORY.CONTROL;
      }
      if (['checkbox', 'radio', 'tab', 'menuitem', 'option', 'switch', 'treeitem',
           'combobox', 'spinbutton'].includes(role)) {
        return CLICKABLE_CATEGORY.CONTROL;
      }

      // 7) Catch remaining interactive nodes (cursor / handlers / inputs)
      if (el.tagName === 'INPUT') {
        // Non-text, non-range inputs already handled; leftover types → control
        return CLICKABLE_CATEGORY.CONTROL;
      }
      if (this.isLikelyInteractive(el)) return CLICKABLE_CATEGORY.GENERIC;

      return CLICKABLE_CATEGORY.NONE;
    } catch {
      return CLICKABLE_CATEGORY.NONE;
    }
  }

  /**
   * Whether this category should get "link-style" hover/activation chrome:
   * scale-up pulse, link key hints, navigational affordances.
   * Sliders and media surfaces are excluded.
   * @param {string} category
   * @returns {boolean}
   */
  isLinkStyleCategory(category) {
    return category === CLICKABLE_CATEGORY.LINK || category === CLICKABLE_CATEGORY.GENERIC;
  }

  /**
   * Whether focus fill (blue wash) should be suppressed for this category/element.
   * @param {Element|null} el
   * @returns {boolean}
   */
  shouldSuppressFocusFillForElement(el) {
    const cat = this.getClickableCategory(el);
    if (cat === CLICKABLE_CATEGORY.SLIDER) return true;
    if (cat === CLICKABLE_CATEGORY.TEXT) return true;
    // Media: suppress only when the player shell has semantic seek chrome
    // (thumbnail videos without a range/slider keep the blue wash).
    if (cat === CLICKABLE_CATEGORY.MEDIA) {
      try {
        let root = el;
        if (el?.tagName === 'VIDEO' && el.parentElement) root = el.parentElement;
        let n = root;
        let depth = 0;
        while (n && n.nodeType === 1 && depth < 5) {
          if (n === document.body || n === document.documentElement) break;
          if (n.querySelector?.('input[type="range"], [role="slider"], [role="progressbar"]')) {
            return true;
          }
          try {
            const r = n.getBoundingClientRect();
            if (r.height > window.innerHeight * 0.85 && r.width > window.innerWidth * 0.85) break;
          } catch { /* ignore */ }
          n = n.parentElement;
          depth++;
        }
      } catch { /* ignore */ }
      return false;
    }
    return false;
  }
}



  // Module: src/modules/activation-handler.js
/**
 * Smart element activation with semantic handling
 */
class ActivationHandler {
  constructor(elementDetector) {
    this.detector = elementDetector;
  }

  /**
   * If the given element is inside a <summary>, return that <summary>.
   * Clicking a child inside <summary> does NOT trigger <details> toggling because
   * the activation behavior is on the <summary> element itself (not ancestors).
   * @param {any} el
   * @returns {HTMLElement|null}
   */
  resolveSummaryActivator(el) {
    try {
      if (!el || el.nodeType !== 1) return null;
      const element = /** @type {HTMLElement} */ (el);
      if (element.tagName === 'SUMMARY') return element;
      if (typeof element.closest === 'function') {
        const summary = element.closest('summary');
        if (summary && summary.tagName === 'SUMMARY') return /** @type {HTMLElement} */ (summary);
      }
      if (element.tagName === 'DETAILS') {
        const summary = element.querySelector(':scope > summary');
        if (summary && summary.tagName === 'SUMMARY') return /** @type {HTMLElement} */ (summary);
      }
    } catch { /* ignore */ }
    return null;
  }

  /**
   * Dispatch a "realistic" click sequence (pointer + mouse) with coordinates.
   * Many webapps (e.g. Internet Archive BookReader) rely on clientX/clientY to
   * decide behavior (page-turn zones), and `HTMLElement.click()` does not carry
   * meaningful coordinates.
   */
  dispatchClickSequence(target, clientX, clientY) {
    if (!target) return;

    const common = {
      bubbles: true,
      cancelable: true,
      composed: true,
      view: window,
      clientX,
      clientY,
      button: 0,
      buttons: 1
    };

    // Pointer events (preferred when supported)
    const hasPointer = typeof window.PointerEvent === 'function';
    if (hasPointer) {
      const pCommon = {
        ...common,
        pointerId: 1,
        pointerType: 'mouse',
        isPrimary: true
      };
      try { target.dispatchEvent(new PointerEvent('pointerover', pCommon)); } catch { }
      try { target.dispatchEvent(new PointerEvent('pointerenter', pCommon)); } catch { }
      try { target.dispatchEvent(new PointerEvent('pointerdown', pCommon)); } catch { }
    } else {
      // Some sites attach handlers to pointer* names; approximate with MouseEvent if needed.
      try { target.dispatchEvent(new MouseEvent('pointerover', common)); } catch { }
      try { target.dispatchEvent(new MouseEvent('pointerenter', common)); } catch { }
      try { target.dispatchEvent(new MouseEvent('pointerdown', common)); } catch { }
    }

    // Mouse events
    try { target.dispatchEvent(new MouseEvent('mouseover', common)); } catch { }
    try { target.dispatchEvent(new MouseEvent('mouseenter', common)); } catch { }
    try { target.dispatchEvent(new MouseEvent('mousemove', common)); } catch { }
    try { target.dispatchEvent(new MouseEvent('mousedown', common)); } catch { }

    // Release (buttons=0)
    const commonUp = { ...common, buttons: 0 };
    if (hasPointer) {
      const pUp = { ...commonUp, pointerId: 1, pointerType: 'mouse', isPrimary: true };
      try { target.dispatchEvent(new PointerEvent('pointerup', pUp)); } catch { }
    } else {
      try { target.dispatchEvent(new MouseEvent('pointerup', commonUp)); } catch { }
    }
    try { target.dispatchEvent(new MouseEvent('mouseup', commonUp)); } catch { }
    try { target.dispatchEvent(new MouseEvent('click', commonUp)); } catch { }
  }

  smartClick(el, clientX, clientY, openInNewTab = false) {
    if (!el) return false;

    // Scrubbers must not be promoted to a surrounding card/link activator.
    // (Suno/Rumble progress bars sit inside large pointer/link-ish chrome.)
    try {
      const scrub = this.detector.resolveScrubber?.(el, clientX, clientY);
      if (scrub?.control) {
        if (scrub.kind === 'range' || this.detector.isNativeType(scrub.control, 'range')) {
          return this.handleRange(scrub.control, clientX, clientY, scrub.track);
        }
        if (scrub.kind === 'role-slider') {
          return this.handleRoleSlider(scrub.control, clientX, clientY, scrub.track);
        }
        return this.handleScrubberTrack(scrub.track || scrub.control, clientX, clientY);
      }
    } catch { /* ignore */ }

    // First, try to find a more specific clickable parent (links, buttons)
    // Prioritize links for video/audio elements (common on video websites)
    let activator = el;

    // Special case: <details>/<summary> accordions.
    // Ensure our fallback .click() targets the <summary>, otherwise the accordion won't toggle.
    const summaryActivator = this.resolveSummaryActivator(el);
    if (summaryActivator) {
      activator = summaryActivator;
    }

    if (el.closest) {
      let specificClickable;

      // For video/audio elements, prioritize finding parent links
      if (el.tagName === 'VIDEO' || el.tagName === 'AUDIO') {
        specificClickable = el.closest('a[href]');
        if (specificClickable) {
          console.log('[KeyPilot] Found parent link for video/audio element:', specificClickable.href);
        }
      }

      // If no specific handling above, look for any clickable parent
      if (!specificClickable) {
        // Keep <summary> as activator if we already resolved it above.
        if (!summaryActivator) {
          specificClickable = el.closest('a[href], button, [role="button"], [onclick], [tabindex]');
        }
      }

      if (specificClickable) {
        activator = specificClickable;
      }
    }

    // Special handling for links
    if (activator.tagName === 'A' && activator.href) {
      if (openInNewTab) {
        console.log('[KeyPilot] Activating link in new tab:', activator.href);
        // Store original target and temporarily change it to open in new tab
        const originalTarget = activator.target;
        activator.target = '_blank';

        try {
          // Try programmatic click first
          activator.click();
          return true;
        } catch (error) {
          console.log('[KeyPilot] Programmatic click failed, using window.open:', error);
          // Fallback to direct window.open
          window.open(activator.href, '_blank');
          return true;
        } finally {
          // Restore original target
          if (originalTarget !== undefined) {
            activator.target = originalTarget;
          } else {
            activator.removeAttribute('target');
          }
        }
      } else {
        console.log('[KeyPilot] Activating link in same window:', activator.href);

        // Store original target and temporarily change it
        const originalTarget = activator.target;
        activator.target = '_self';

        try {
          // Try programmatic click first
          activator.click();
          return true;
        } catch (error) {
          console.log('[KeyPilot] Programmatic click failed, using navigation:', error);
          // Fallback to direct navigation
          window.location.href = activator.href;
          return true;
        } finally {
          // Restore original target
          if (originalTarget !== undefined) {
            activator.target = originalTarget;
          } else {
            activator.removeAttribute('target');
          }
        }
      }
    }

    // For non-links, prefer a coordinate-carrying click sequence at the actual point under cursor.
    // (IA BookReader uses click zones; a coordinate-less `.click()` won't page-turn.)
    let pointTarget = null;
    try {
      if (Number.isFinite(clientX) && Number.isFinite(clientY) && this.detector?.deepElementFromPoint) {
        pointTarget = this.detector.deepElementFromPoint(clientX, clientY);
      }
    } catch { /* ignore */ }

    // If the actual point is inside a <summary>, ensure the activator is the <summary>.
    const pointSummary = this.resolveSummaryActivator(pointTarget);
    if (pointSummary) activator = pointSummary;

    // <details>/<summary>: the browser's open/close toggle is summary *activation behavior*.
    // Synthetic MouseEvent/PointerEvent clicks (isTrusted=false) do NOT run that behavior —
    // only a real user click or HTMLElement.click() does. Hitting a child (label, count,
    // padding) used to dispatch only synthetic events, so F appeared to work only on a few
    // hit targets depending on what was under the pointer.
    if (activator && activator.tagName === 'SUMMARY') {
      try {
        activator.click();
      } catch { /* ignore */ }
      return true;
    }

    const primary = pointTarget || el || activator;
    this.dispatchClickSequence(primary, clientX, clientY);
    // If `primary` is already inside the clickable activator (e.g. a <span> inside a <button>),
    // the event will bubble to the activator. Dispatching again on the activator would double-click.
    try {
      const primaryNode = /** @type {any} */ (primary);
      const activatorEl = /** @type {any} */ (activator);
      const activatorContainsPrimary =
        !!(activatorEl &&
          primaryNode &&
          typeof activatorEl.contains === 'function' &&
          activatorEl.contains(primaryNode));
      if (activator && activator !== primary && !activatorContainsPrimary) {
        this.dispatchClickSequence(activator, clientX, clientY);
      }
      // No extra `activator.click()` fallback here; it causes double activation for normal controls
      // because `dispatchClickSequence()` already emits a click. (Summary handled above.)
    } catch { /* ignore */ }

    return true;
  }



  handleSmartActivate(target, x, y, openInNewTab = false) {
    if (!target) return false;

    // Handle label elements
    target = this.resolveLabel(target);

    // IMPORTANT: Check if video/audio is wrapped in a link first
    // This handles video preview thumbnails on video websites where clicking should navigate
    if ((target.tagName === 'VIDEO' || target.tagName === 'AUDIO') && target.closest) {
      const parentLink = target.closest('a[href]');
      if (parentLink) {
        // Let the link be handled by smartClick instead of controlling media playback
        console.log('[KeyPilot] Video/audio wrapped in link, deferring to link activation');
        return false;
      }
    }

    // Media scrubbers (thumb-style ranges, custom div tracks, role=slider).
    // Resolve before generic radio/checkbox/text so track fills map to the real control.
    try {
      const scrub = this.detector.resolveScrubber?.(target, x, y);
      if (scrub?.control) {
        if (scrub.kind === 'range' || this.detector.isNativeType(scrub.control, 'range')) {
          return this.handleRange(scrub.control, x, y, scrub.track);
        }
        if (scrub.kind === 'role-slider') {
          return this.handleRoleSlider(scrub.control, x, y, scrub.track);
        }
        if (scrub.kind === 'track') {
          return this.handleScrubberTrack(scrub.track || scrub.control, x, y);
        }
      }
    } catch { /* ignore */ }

    // Handle different input types semantically
    if (this.detector.isNativeType(target, 'radio')) {
      return this.handleRadio(target);
    }

    if (this.detector.isNativeType(target, 'checkbox')) {
      return this.handleCheckbox(target);
    }

    if (this.detector.isNativeType(target, 'range')) {
      return this.handleRange(target, x, y);
    }

    // Handle role="slider" elements
    const role = (target.getAttribute && (target.getAttribute('role') || '').trim().toLowerCase()) || '';
    if (role === 'slider') {
      return this.handleRoleSlider(target, x, y);
    }

    if (this.detector.isTextLike(target)) {
      return this.handleTextField(target);
    }

    if (this.detector.isContentEditable(target)) {
      return this.handleContentEditable(target);
    }

    // Handle video and audio elements (only if not wrapped in a link)
    if (target.tagName === 'VIDEO' || target.tagName === 'AUDIO') {
      return this.handleMediaElement(target);
    }

    return false;
  }

  resolveLabel(target) {
    // If the target IS a label or is nested INSIDE a label, resolve to the label's control.
    // This is critical for settings UIs where the visible click target is a <div> inside <label>.
    let label = null;
    try {
      if (target && target.tagName === 'LABEL') label = target;
      else if (target && typeof target.closest === 'function') label = target.closest('label');
    } catch { /* ignore */ }

    if (label && label.tagName === 'LABEL') {
      const forId = label.getAttribute('for');
      if (forId) {
        const labelCtl = (label.getRootNode() || document).getElementById(forId);
        if (labelCtl) return labelCtl;
      } else {
        const ctl = label.querySelector('input, textarea, select');
        if (ctl) return ctl;
      }
    }
    return target;
  }

  handleRadio(target) {
    if (!target.checked) {
      target.checked = true;
      this.dispatchInputChange(target);
    }
    return true;
  }

  handleCheckbox(target) {
    target.checked = !target.checked;
    this.dispatchInputChange(target);
    return true;
  }

  /**
   * Geometry used to map clientX → slider value.
   * Thumb-sized range inputs are only ~12px wide and positioned with left:%;
   * value math must use the full track host rect instead of the thumb box.
   */
  getSliderMetricsRect(control, trackHint = null) {
    let track = trackHint;
    try {
      if (!track && this.detector.getScrubTrackElement) {
        track = this.detector.getScrubTrackElement(control);
      }
    } catch { /* ignore */ }

    let controlRect = null;
    let trackRect = null;
    try { controlRect = control.getBoundingClientRect(); } catch { controlRect = null; }
    try { trackRect = track && track !== control ? track.getBoundingClientRect() : null; } catch { trackRect = null; }

    // Prefer a substantially wider track when the control looks like a thumb-only range.
    if (trackRect && trackRect.width > 0 && controlRect && controlRect.width > 0) {
      if (trackRect.width >= controlRect.width * 2.5) return trackRect;
    }
    if (trackRect && trackRect.width > 40 && (!controlRect || controlRect.width < 24)) {
      return trackRect;
    }
    if (controlRect && controlRect.width > 0) return controlRect;
    return trackRect;
  }

  /**
   * Closest slider / range host for a hit target (time scrubber, volume, etc.).
   * Used to scope synthetic drag-end events to one control.
   * @param {EventTarget|null|undefined} originTarget
   * @returns {Element|null}
   */
  resolveSliderRoot(originTarget) {
    try {
      const el = /** @type {Element|null} */ (
        originTarget && /** @type {any} */ (originTarget).nodeType === 1
          ? originTarget
          : null
      );
      if (!el) return null;
      if (typeof el.closest === 'function') {
        const root = el.closest(
          '[role="slider"], input[type="range"], [data-media-time-slider], .vds-slider, .vds-time-slider'
        );
        if (root) return root;
      }
      return el;
    } catch {
      return null;
    }
  }

  /**
   * True when this control is a volume / non-timeline slider.
   * Volume must not trigger media.currentTime seeks or full-page drag teardown.
   * @param {Element|null|undefined} el
   * @returns {boolean}
   */
  isVolumeOrNonSeekSlider(el) {
    if (!el || el.nodeType !== 1) return false;
    try {
      const label = `${el.getAttribute('aria-label') || ''} ${el.getAttribute('aria-valuetext') || ''}`.toLowerCase();
      if (/\b(volume|mute|sound|loudness|gain)\b/.test(label)) return true;
      // data-media-volume-slider / class tokens used by Vidstack and common players
      try {
        if (el.hasAttribute('data-media-volume-slider')) return true;
      } catch { /* ignore */ }
      const cls = String(/** @type {any} */ (el).className || '').toLowerCase();
      if (/\b(volume|mute)[-_]?slider\b|\bvolume-?control\b|\bvds-volume\b/.test(cls)) return true;
      // Nested under a mute/volume control chrome
      if (typeof el.closest === 'function') {
        const host = el.closest(
          '[aria-label*="volume" i], [aria-label*="mute" i], [data-media-volume-slider], .vds-volume-slider'
        );
        if (host) return true;
      }
    } catch { /* ignore */ }
    return false;
  }

  /**
   * Force-end a synthetic pointer drag on ONE slider tree only.
   *
   * Media scrubbers (Vidstack/vds-slider, custom React players like Suno) call
   * `setPointerCapture` on pointerdown and keep seeking until the captured
   * pointerup. Synthetic events cannot capture a real pointer id
   * (`NotFoundError: No active pointer…`), so the initial element-level up in
   * `dispatchClickSequence` often leaves `data-dragging` / isSeeking true and
   * the knob sticks to the real mouse.
   *
   * We re-fire pointerup/mouseup/pointercancel on the activated slider subtree
   * after React commits drag-start. We intentionally do NOT dispatch on
   * document/window — that can end (or drive) other sliders in the same player
   * (e.g. volume drag affecting the time scrubber).
   *
   * @param {number} clientX
   * @param {number} clientY
   * @param {number} [pointerId=1] Must match the id used in dispatchClickSequence.
   * @param {EventTarget|null} [originTarget] Element that received the down.
   */
  endSyntheticPointerDrag(clientX, clientY, pointerId = 1, originTarget = null) {
    const x = Number.isFinite(clientX) ? clientX : 0;
    const y = Number.isFinite(clientY) ? clientY : 0;
    const id = Number.isFinite(pointerId) ? pointerId : 1;
    const sliderRoot = this.resolveSliderRoot(originTarget);

    const upCommon = {
      bubbles: true,
      cancelable: true,
      composed: true,
      view: window,
      clientX: x,
      clientY: y,
      button: 0,
      buttons: 0
    };
    const pUp = {
      ...upCommon,
      pointerId: id,
      pointerType: 'mouse',
      isPrimary: true
    };

    const hasPointer = typeof window.PointerEvent === 'function';
    /** @type {Set<EventTarget>} */
    const targets = new Set();

    // Hit target + ancestors up through the slider root (only this control).
    try {
      let n = originTarget && /** @type {any} */ (originTarget).nodeType === 1
        ? /** @type {Element} */ (originTarget)
        : null;
      let depth = 0;
      while (n && n.nodeType === 1 && depth < 12) {
        targets.add(n);
        if (sliderRoot && n === sliderRoot) break;
        // Stop before leaving the slider into shared player chrome.
        if (!sliderRoot && (n.parentElement === document.body || n === document.documentElement)) break;
        n = n.parentElement;
        depth++;
      }
    } catch { /* ignore */ }

    if (sliderRoot) targets.add(sliderRoot);
    if (originTarget) targets.add(originTarget);

    for (const t of targets) {
      if (!t || typeof t.dispatchEvent !== 'function') continue;
      if (hasPointer) {
        try { t.dispatchEvent(new PointerEvent('pointerup', pUp)); } catch { /* ignore */ }
        try { t.dispatchEvent(new PointerEvent('pointercancel', pUp)); } catch { /* ignore */ }
      } else {
        try { t.dispatchEvent(new MouseEvent('pointerup', upCommon)); } catch { /* ignore */ }
        try { t.dispatchEvent(new MouseEvent('pointercancel', upCommon)); } catch { /* ignore */ }
      }
      try { t.dispatchEvent(new MouseEvent('mouseup', upCommon)); } catch { /* ignore */ }
    }

    // Best-effort release only inside this slider tree.
    try {
      for (const t of targets) {
        if (!t || typeof /** @type {any} */ (t).hasPointerCapture !== 'function') continue;
        try {
          if (/** @type {any} */ (t).hasPointerCapture(id)) {
            /** @type {any} */ (t).releasePointerCapture(id);
          }
        } catch { /* ignore */ }
      }
    } catch { /* ignore */ }
  }

  /**
   * Pointer/mouse down+up at coordinates.
   * Custom media scrubbers listen for this (not for programmatic range.value).
   * mouseup is required on some players to commit the seek.
   */
  dispatchPointerSeek(target, clientX, clientY) {
    if (!target) return;
    const clientYSafe = Number.isFinite(clientY) ? clientY : (() => {
      try {
        const r = target.getBoundingClientRect();
        return r.top + r.height / 2;
      } catch {
        return 0;
      }
    })();
    // Full pointer+mouse sequence including mouseup (commit) at the seek point.
    this.dispatchClickSequence(target, clientX, clientYSafe);

    // Force drag-end on THIS slider AFTER the player's drag-start state commits.
    //
    // Why deferred:
    // - Synthetic pointerdown cannot setPointerCapture (no real OS pointer id).
    // - Players often arm `data-dragging` via React state that flushes after the
    //   event handler returns; a synchronous up races that commit and loses.
    // Microtask + macrotask cover React batching and setTimeout-armed handlers.
    // Events stay scoped to the slider tree (not document) so volume/time stay independent.
    const endDrag = () => {
      try {
        this.endSyntheticPointerDrag(clientX, clientYSafe, 1, target);
      } catch { /* ignore */ }
    };
    try {
      queueMicrotask(endDrag);
    } catch {
      endDrag();
    }
    try {
      setTimeout(endDrag, 0);
    } catch { /* ignore */ }
  }

  /**
   * True when the range input is a positioned thumb and a separate wider track owns seek geometry
   * (common custom players: small input with left:%, full-width visual track sibling).
   */
  isThumbStyleRange(control, track) {
    if (!control || !track || track === control) return false;
    try {
      const cr = control.getBoundingClientRect();
      const tr = track.getBoundingClientRect();
      if (!cr || !tr || cr.width <= 0 || tr.width <= 0) return false;
      return tr.width >= cr.width * 2.5;
    } catch {
      return false;
    }
  }

  /**
   * Containment root for "is under-point inside this scrubber?" checks.
   * Expands one level to a short track host (presentation + thumb wrapper) without
   * expanding into a full playbar that would include play/volume buttons.
   */
  getSeekContainmentRoot(track) {
    if (!track || track.nodeType !== 1) return track;
    try {
      const parent = track.parentElement;
      if (!parent || parent === document.body || parent === document.documentElement) {
        return track;
      }
      const pr = parent.getBoundingClientRect();
      // Track hosts are short horizontal bars, not whole player chrome.
      if (pr.height > 0 && pr.height <= 48 && pr.width >= 64) {
        return parent;
      }
    } catch { /* ignore */ }
    return track;
  }

  /**
   * Prefer the real hit-target under the cursor (progress fill / remaining bar).
   * Synthetic events on the outer presentation wrapper alone often move the knob
   * UI without committing media seek; events on the under-point node do both.
   */
  resolveSeekPointTarget(track, clientX, clientY) {
    if (!track) return track;
    try {
      if (Number.isFinite(clientX) && Number.isFinite(clientY) && this.detector?.deepElementFromPoint) {
        const under = this.detector.deepElementFromPoint(clientX, clientY);
        if (!under) return track;
        const root = this.getSeekContainmentRoot(track) || track;
        if (under === track || under === root ||
            (typeof root.contains === 'function' && root.contains(under)) ||
            (typeof track.contains === 'function' && track.contains(under))) {
          return under;
        }
      }
    } catch { /* ignore */ }
    return track;
  }

  /**
   * Find <audio>/<video> associated with a scrubber (same player shell, or duration ≈ range max).
   * @param {Element} fromEl
   * @param {HTMLInputElement|null} [rangeEl]
   * @returns {HTMLMediaElement|null}
   */
  findAssociatedMedia(fromEl, rangeEl = null) {
    try {
      let n = fromEl;
      let depth = 0;
      while (n && n.nodeType === 1 && depth < 8) {
        if (n === document.body || n === document.documentElement) break;
        if (n.tagName === 'VIDEO' || n.tagName === 'AUDIO') {
          return /** @type {HTMLMediaElement} */ (n);
        }
        try {
          const list = n.querySelectorAll?.('audio, video');
          if (list && list.length) {
            let best = null;
            for (const m of list) {
              if (Number.isFinite(m.duration) && m.duration > 1) {
                best = m;
                break;
              }
              if (!best) best = m;
            }
            if (best) return /** @type {HTMLMediaElement} */ (best);
          }
        } catch { /* ignore */ }
        try {
          const r = n.getBoundingClientRect();
          if (r.height > window.innerHeight * 0.85 && r.width > window.innerWidth * 0.85) break;
        } catch { /* ignore */ }
        n = n.parentElement;
        depth++;
      }
    } catch { /* ignore */ }

    // Match range max ≈ media duration (playback progress controls).
    try {
      const max = rangeEl ? this.asNum(rangeEl.max, NaN) : NaN;
      if (Number.isFinite(max) && max > 1) {
        const all = document.querySelectorAll('audio, video');
        for (const m of all) {
          if (Number.isFinite(m.duration) && Math.abs(m.duration - max) < 1.5) {
            return /** @type {HTMLMediaElement} */ (m);
          }
        }
      }
    } catch { /* ignore */ }

    return null;
  }

  /**
   * Soft fallback: set media.currentTime from cursor position along the track.
   * Used after pointer seek for custom players that update the knob but not media.
   * Never runs for volume / non-timeline sliders (would scrub the video when adjusting volume).
   */
  maybeSeekAssociatedMedia(trackEl, clientX, clientY, rangeEl = null) {
    try {
      if (!trackEl || !Number.isFinite(clientX)) return;
      if (this.isVolumeOrNonSeekSlider(trackEl) || this.isVolumeOrNonSeekSlider(rangeEl)) return;
      // Also ignore when the resolved slider root is clearly volume chrome.
      try {
        const root = this.resolveSliderRoot(trackEl);
        if (root && this.isVolumeOrNonSeekSlider(root)) return;
      } catch { /* ignore */ }

      const media = this.findAssociatedMedia(trackEl, rangeEl);
      if (!media || !Number.isFinite(media.duration) || media.duration <= 0) return;

      const metricsEl = this.getSeekContainmentRoot(trackEl) || trackEl;
      const rect = metricsEl.getBoundingClientRect();
      if (!rect || rect.width <= 0) return;

      // Volume/popover sliders are short; timeline scrubbers span most of the player width.
      // Guard against mapping a narrow control's X onto media duration.
      if (rect.width < 120) return;

      const pct = this.clamp((clientX - rect.left) / rect.width, 0, 1);
      const targetTime = pct * media.duration;
      // Only nudge when clearly out of sync (avoid fighting smooth seeking UIs).
      if (Math.abs(media.currentTime - targetTime) < 0.35) return;
      media.currentTime = targetTime;
    } catch { /* ignore */ }
  }

  /**
   * Generic custom-scrubber seek: one pointer sequence on the element under the cursor.
   * Do not set range.value from our geometry, do not fire on a tiny thumb input,
   * do not double-dispatch on track + fill.
   */
  seekCustomScrubber(track, clientX, clientY, rangeEl = null) {
    if (!track) return false;
    const pointTarget = this.resolveSeekPointTarget(track, clientX, clientY);
    this.dispatchPointerSeek(pointTarget, clientX, clientY);
    // Soft media sync after pointer commit (timeline scrubbers only).
    this.maybeSeekAssociatedMedia(track, clientX, clientY, rangeEl);
    return true;
  }

  handleRange(target, clientX, clientY = null, trackHint = null) {
    let track = trackHint;
    try {
      if (!track && this.detector.getScrubTrackElement) {
        track = this.detector.getScrubTrackElement(target);
      }
    } catch { /* ignore */ }
    track = track || target;

    // Thumb-style custom scrubbers: site owns seek math via pointer at clientX/Y on the
    // visual hit target under the cursor (often a fill child of the track).
    // Do NOT:
    //  - set .value from our rect math (misses thumb-width compensation → offset knob)
    //  - fire pointer events on the tiny range input (native range misreads clientX)
    //  - double-dispatch (track wrapper + fill) — causes offset / UI-only seeks
    if (this.isThumbStyleRange(target, track)) {
      return this.seekCustomScrubber(track, clientX, clientY, target);
    }

    // Native full-width <input type="range">: map clientX onto the control and set value.
    const rect = this.getSliderMetricsRect(target, track);
    const min = this.asNum(target.min, 0);
    const max = this.asNum(target.max, 100);
    const stepAttr = target.step && target.step !== 'any' ? this.asNum(target.step, 1) : 'any';

    if (rect && rect.width > 0) {
      const pct = this.clamp((clientX - rect.left) / rect.width, 0, 1);
      let val = min + pct * (max - min);

      if (stepAttr !== 'any' && Number.isFinite(stepAttr) && stepAttr > 0) {
        const steps = Math.round((val - min) / stepAttr);
        val = min + steps * stepAttr;
      }
      val = this.clamp(val, min, max);
      const before = target.value;
      try {
        target.value = String(val);
        if (target.value !== before) this.dispatchInputChange(target);
      } catch { /* ignore */ }
    }

    // Coordinate sequence on the range itself for listeners that use clientX.
    this.dispatchPointerSeek(target, clientX, clientY);
    return true;
  }

  handleRoleSlider(target, clientX, clientY, trackHint = null) {
    // ARIA sliders: update aria-valuenow when present, then one coordinate pointer sequence.
    const rect = this.getSliderMetricsRect(target, trackHint || target);
    if (rect && rect.width > 0) {
      const min = this.asNum(target.getAttribute('aria-valuemin'), 0);
      const max = this.asNum(target.getAttribute('aria-valuemax'), 100);
      const step = this.asNum(target.getAttribute('aria-step'), 1);

      const pct = this.clamp((clientX - rect.left) / rect.width, 0, 1);
      let newValue = min + pct * (max - min);

      if (step > 0) {
        const steps = Math.round((newValue - min) / step);
        newValue = min + steps * step;
      }

      newValue = this.clamp(newValue, min, max);

      const before = target.getAttribute('aria-valuenow');
      try { target.setAttribute('aria-valuenow', String(newValue)); } catch { /* ignore */ }

      if (String(newValue) !== before) {
        this.dispatchInputChange(target);
        try {
          target.dispatchEvent(new CustomEvent('sliderchange', {
            bubbles: true,
            detail: { value: newValue, previousValue: this.asNum(before, min) }
          }));
        } catch { }
      }
    }

    const seekRoot = trackHint && trackHint !== target ? trackHint : target;
    const pointTarget = this.resolveSeekPointTarget(seekRoot, clientX, clientY);
    // Prefer the semantic slider root so drag-end stays on this control (volume vs time).
    const sequenceTarget = pointTarget || target;
    this.dispatchPointerSeek(sequenceTarget, clientX, clientY);
    // Timeline only — never map volume X onto video.currentTime.
    if (!this.isVolumeOrNonSeekSlider(target) && !this.isVolumeOrNonSeekSlider(seekRoot)) {
      this.maybeSeekAssociatedMedia(seekRoot, clientX, clientY, null);
    }
    return true;
  }

  /**
   * Custom scrub tracks with no input/role (pure div progress bars).
   * One pointer sequence on the under-cursor hit target.
   */
  handleScrubberTrack(track, clientX, clientY) {
    if (!track) return false;
    return this.seekCustomScrubber(track, clientX, clientY, null);
  }

  handleTextField(target) {
    try {
      target.focus({ preventScroll: true });
    } catch {
      try { target.focus(); } catch { }
    }
    try {
      const v = target.value ?? '';
      target.setSelectionRange(v.length, v.length);
    } catch { }
    return true;
  }

  handleContentEditable(target) {
    try {
      target.focus({ preventScroll: true });
    } catch {
      try { target.focus(); } catch { }
    }

    // Try to position cursor at the end of content
    try {
      const selection = window.getSelection();
      const range = document.createRange();

      // If there's text content, position at the end
      if (target.childNodes.length > 0) {
        const lastNode = target.childNodes[target.childNodes.length - 1];
        if (lastNode.nodeType === Node.TEXT_NODE) {
          range.setStart(lastNode, lastNode.textContent.length);
        } else {
          range.setStartAfter(lastNode);
        }
      } else {
        // Empty contenteditable, position at the beginning
        range.setStart(target, 0);
      }

      range.collapse(true);
      selection.removeAllRanges();
      selection.addRange(range);
    } catch (error) {
      // Fallback: just focus without cursor positioning
      console.debug('Could not position cursor in contenteditable:', error);
    }

    return true;
  }

  handleMediaElement(target) {
    try {
      // Toggle play/pause for video and audio elements
      if (target.paused) {
        target.play();
      } else {
        target.pause();
      }
      return true;
    } catch (error) {
      console.debug('Could not control media element:', error);
      // Fallback to regular click behavior
      return false;
    }
  }

  dispatchInputChange(el) {
    const opts = { bubbles: true, composed: true };
    el.dispatchEvent(new Event('input', opts));
    el.dispatchEvent(new Event('change', opts));
  }

  clamp(n, lo, hi) {
    return Math.min(hi, Math.max(lo, n));
  }

  asNum(v, d) {
    const n = Number(v);
    return Number.isFinite(n) ? n : d;
  }
}


  // Module: src/modules/focus-detector.js
/**
 * Text field focus detection and management
 */


class FocusDetector {
  constructor(stateManager, mouseCoordinateManager = null) {
    this.state = stateManager;
    this.mouseCoordinateManager = mouseCoordinateManager;
    this.currentFocusedElement = null;
    this.textElementObserver = null; // MutationObserver for focused text element
    this.textElementResizeObserver = null; // ResizeObserver for focused text element
    this.documentObserver = null; // MutationObserver for document focus changes
    this.rafId = null; // requestAnimationFrame ID for position tracking

    // Bound handlers so start/stop can add/remove the same function references.
    this._onFocusIn = this.handleFocusIn.bind(this);
    this._onFocusOut = this.handleFocusOut.bind(this);
  }

  start() {
    console.log('[KeyPilot] FocusDetector starting...');

    // Listen for focus/blur events
    document.addEventListener('focusin', this._onFocusIn, true);
    document.addEventListener('focusout', this._onFocusOut, true);
    console.log('[KeyPilot] Focus event listeners added');

    // Set up MutationObserver for document to catch programmatic focus changes
    this.setupDocumentObserver();

    // Immediate check: google.com (and others) autofocus the search box *before*
    // our document_idle content script runs, so we never see that focusin.
    try { this.checkCurrentFocus(); } catch { /* ignore */ }

    // Follow-up checks for late autofocus / SPA replacements of the search field.
    setTimeout(() => {
      this.checkCurrentFocus();
      console.log('[KeyPilot] Initial focus check completed');
    }, 100);
    setTimeout(() => this.checkCurrentFocus(), 500);

    // Also check when DOM is fully loaded
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', () => {
        try { this.checkCurrentFocus(); } catch { /* ignore */ }
        setTimeout(() => this.checkCurrentFocus(), 100);
      });
    }

    // Check when page is fully loaded (including images, etc.)
    window.addEventListener('load', () => {
      try { this.checkCurrentFocus(); } catch { /* ignore */ }
      setTimeout(() => this.checkCurrentFocus(), 100);
    });

    // Returning to the tab: browser may restore focus without a focusin we saw.
    try {
      window.addEventListener('focus', () => {
        setTimeout(() => this.checkCurrentFocus(), 0);
      });
    } catch { /* ignore */ }
  }

  stop() {
    document.removeEventListener('focusin', this._onFocusIn, true);
    document.removeEventListener('focusout', this._onFocusOut, true);

    // Clean up observers
    this.cleanupTextElementObservers();
    this.cleanupDocumentObserver();

    // Clean up any remaining focused element reference
    if (this.currentFocusedElement) {
      this.currentFocusedElement = null;
    }
  }

  handleFocusIn(e) {
    // composedPath reaches into open shadow roots; event.target is often the host.
    const composed = kpGetComposedEventTarget(e);
    const deep = this.getDeepActiveElement();
    const candidate = this.isTextInput(composed)
      ? composed
      : (this.isTextInput(deep) ? deep : null);

    console.log(
      '[KeyPilot] FocusIn event:',
      candidate?.tagName || e.target?.tagName,
      candidate?.type || e.target?.type || 'N/A',
      'id:',
      candidate?.id || e.target?.id || 'none'
    );

    if (candidate) {
      console.log('[KeyPilot] Text input focused - setting text mode:', candidate.tagName, candidate.type || 'N/A');
      this.setTextFocus(candidate);
    } else {
      console.log('[KeyPilot] Non-text element focused - ignoring');
    }
  }

  handleFocusOut(e) {
    const composed = kpGetComposedEventTarget(e);
    const leftText =
      this.isTextInput(composed) ||
      this.isTextInput(e.target) ||
      (this.currentFocusedElement && e.target === this.currentFocusedElement);

    if (leftText) {
      console.debug('Text input blurred:', composed?.tagName || e.target?.tagName, composed?.type || e.target?.type || 'N/A');
      // Longer delay to allow for focus changes and prevent premature clearing during slider interaction
      setTimeout(() => {
        const currentlyFocused = this.getDeepActiveElement();
        console.debug('Focus check after blur - currently focused:', currentlyFocused?.tagName, currentlyFocused?.type, currentlyFocused?.id);
        if (!this.isTextInput(currentlyFocused)) {
          console.debug('Clearing text focus - no text input currently focused');
          this.clearTextFocus();
        } else {
          // Focus moved to another text field — keep/update text mode.
          if (currentlyFocused !== this.currentFocusedElement) {
            this.setTextFocus(currentlyFocused);
          } else {
            console.debug('Keeping text focus - text input still focused');
          }
        }
      }, 100); // Increased delay to handle slider interactions
    }
  }

  checkCurrentFocus() {
    const activeElement = this.getDeepActiveElement();

    if (this.isTextInput(activeElement)) {
      if (this.currentFocusedElement !== activeElement) {
        console.debug('Text focus detected during check:', activeElement.tagName, activeElement.type || 'N/A', 'ID:', activeElement.id || 'none');
        this.setTextFocus(activeElement);
      }
    } else if (this.currentFocusedElement) {
      console.debug('Text focus cleared during check');
      this.clearTextFocus();
    }
  }

  startPositionTracking() {
    if (this.rafId) {
      cancelAnimationFrame(this.rafId);
    }

    // Store initial position for comparison
    if (this.currentFocusedElement) {
      this.lastKnownRect = this.currentFocusedElement.getBoundingClientRect();
    }

    const checkPosition = () => {
      if (this.currentFocusedElement && this.state.getState().mode === 'text_focus') {
        const currentRect = this.currentFocusedElement.getBoundingClientRect();

        // Check if position OR size changed (comprehensive tracking)
        if (this.lastKnownRect.left !== currentRect.left ||
          this.lastKnownRect.top !== currentRect.top ||
          this.lastKnownRect.width !== currentRect.width ||
          this.lastKnownRect.height !== currentRect.height) {

          if (window.KEYPILOT_DEBUG) {
            console.debug('RAF position tracking detected element change:', {
              oldRect: {
                left: this.lastKnownRect.left,
                top: this.lastKnownRect.top,
                width: this.lastKnownRect.width,
                height: this.lastKnownRect.height
              },
              newRect: {
                left: currentRect.left,
                top: currentRect.top,
                width: currentRect.width,
                height: currentRect.height
              }
            });
          }

          this.lastKnownRect = currentRect;
          this.triggerOverlayUpdate();
        }

        // Continue the loop
        this.rafId = requestAnimationFrame(checkPosition);
      }
    };

    // Start the animation frame loop
    this.rafId = requestAnimationFrame(checkPosition);
    console.debug('Position tracking started with requestAnimationFrame');
  }

  stopPositionTracking() {
    if (this.rafId) {
      cancelAnimationFrame(this.rafId);
      this.rafId = null;
      console.debug('Position tracking stopped');
    }
  }

  setupDocumentObserver() {
    if (!window.MutationObserver) return;

    this.documentObserver = new MutationObserver((mutations) => {
      let shouldCheckFocus = false;

      mutations.forEach((mutation) => {
        // Check for focus-related attribute changes
        if (mutation.type === 'attributes') {
          const attrName = mutation.attributeName;
          // Watch for tabindex changes or other focus-related attributes
          if (attrName === 'contenteditable') {
            shouldCheckFocus = true;
          }
        }
      });

      if (shouldCheckFocus) {
        // Debounce focus checks to avoid excessive calls
        if (this.focusCheckTimeout) {
          clearTimeout(this.focusCheckTimeout);
        }
        this.focusCheckTimeout = setTimeout(() => {
          this.checkCurrentFocus();
          this.focusCheckTimeout = null;
        }, 100);
      }
    });

    // Observe the entire document for attribute changes
    this.documentObserver.observe(document, {
      attributes: true,
      attributeFilter: ['contenteditable'],
      subtree: true
    });

    console.debug('Document MutationObserver set up for focus-related changes');
  }

  cleanupDocumentObserver() {
    if (this.documentObserver) {
      this.documentObserver.disconnect();
      this.documentObserver = null;
    }

    if (this.focusCheckTimeout) {
      clearTimeout(this.focusCheckTimeout);
      this.focusCheckTimeout = null;
    }

    console.debug('Document observer cleaned up');
  }

  getDeepActiveElement() {
    return kpGetDeepActiveElement();
  }

  isTextInput(element) {
    if (!element || element.nodeType !== 1) return false;

    try {
      // KeyPilot omnibox is a text input, but we do NOT want it to trigger text focus mode.
      // Omnibox is its own overlay/mode, and entering text_focus here breaks its keyboard UX.
      if (element.classList?.contains?.(CSS_CLASSES.OMNIBOX_INPUT)) return false;
      const omniboxRoot = element.closest?.(`.${CSS_CLASSES.OMNIBOX_BACKDROP}`) || element.closest?.(`.${CSS_CLASSES.OMNIBOX_PANEL}`);
      if (omniboxRoot) return false;
      // Align with keyboard shortcut suppression (includes bare <input>, date types, etc.).
      return kpIsTypingContext(element);
    } catch {
      return false;
    }
  }

  setTextFocus(element) {
    console.log('[KeyPilot] Setting text focus for element:', element.tagName, element.type || 'N/A');

    // Update current focused element reference
    this.currentFocusedElement = element;

    // Set up observers for the focused text element
    this.setupTextElementObservers(element);

    // Position cursor appropriately for text focus mode
    this.positionCursorForTextFocus(element);

    // Start requestAnimationFrame loop for position tracking
    this.startPositionTracking();

    console.log('[KeyPilot] Setting state to text_focus mode');
    // Set mode and focused element in a single state update to ensure proper cursor initialization
    this.state.setState({
      mode: 'text_focus',
      focusedTextElement: element,
      focusEl: null // Clear to ensure hasClickableElement starts as false
    });

    console.log('[KeyPilot] Text focus mode set successfully');
  }

  /**
   * Position cursor for text focus mode
   * Uses stored coordinates if available, otherwise positions underneath the text field
   * @param {Element} element - The focused text element
   */
  positionCursorForTextFocus(element) {
    if (!element || !this.mouseCoordinateManager) {
      return;
    }

    // Try to get stored coordinates first
    const storedCoordinates = this.mouseCoordinateManager.lastStoredCoordinates;
    
    if (storedCoordinates) {
      // Use stored coordinates if available and valid
      const x = Math.min(storedCoordinates.x, window.innerWidth - 20);
      const y = Math.min(storedCoordinates.y, window.innerHeight - 20);
      const validX = Math.max(10, x);
      const validY = Math.max(10, y);
      
      this.state.setMousePosition(validX, validY);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Positioned cursor using stored coordinates for text focus:', {
          x: validX, y: validY
        });
      }
    } else {
      // Fallback: position cursor underneath the text field
      const rect = element.getBoundingClientRect();
      const x = rect.left + (rect.width / 2);
      const y = rect.bottom + 10; // 10px below the text field
      
      // Ensure coordinates are within viewport bounds
      const validX = Math.min(Math.max(10, x), window.innerWidth - 20);
      const validY = Math.min(Math.max(10, y), window.innerHeight - 20);
      
      this.state.setMousePosition(validX, validY);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Positioned cursor underneath text field:', {
          x: validX, y: validY, elementRect: rect
        });
      }
    }
  }

  clearTextFocus() {
    // Stop position tracking
    this.stopPositionTracking();

    // Clean up observers
    this.cleanupTextElementObservers();

    // Clear focused element reference
    this.currentFocusedElement = null;
    this.state.setState({
      mode: 'none',
      focusedTextElement: null
    });
  }

  isInTextFocus() {
    return this.state.getState().mode === 'text_focus';
  }

  getFocusedElement() {
    return this.currentFocusedElement;
  }

  setupTextElementObservers(element) {
    if (!element) return;

    // Clean up any existing observers first
    this.cleanupTextElementObservers();

    // Store initial position for comparison
    this.lastKnownRect = element.getBoundingClientRect();

    // 1. ResizeObserver for size changes
    if (window.ResizeObserver) {
      this.textElementResizeObserver = new ResizeObserver((entries) => {
        // Debounce resize updates
        if (this.resizeTimeout) {
          clearTimeout(this.resizeTimeout);
        }
        this.resizeTimeout = setTimeout(() => {
          this.triggerOverlayUpdate();
          this.resizeTimeout = null;
        }, 16); // ~60fps
      });

      // Observe the text element itself
      this.textElementResizeObserver.observe(element);
      console.debug('ResizeObserver set up for text element');
    }

    // 2. Input event listener for content changes that affect size
    this.inputEventHandler = () => {
      // Debounce input updates
      if (this.inputTimeout) {
        clearTimeout(this.inputTimeout);
      }
      this.inputTimeout = setTimeout(() => {
        console.debug('Input event detected - triggering overlay update');
        this.triggerOverlayUpdate();
        this.inputTimeout = null;
      }, 16);
    };

    element.addEventListener('input', this.inputEventHandler);
    console.debug('Input event listener set up for text element');

    // 3. MutationObserver for attribute and DOM changes
    if (window.MutationObserver) {
      this.textElementObserver = new MutationObserver((mutations) => {
        let shouldUpdate = false;

        mutations.forEach((mutation) => {
          // Watch for style and class changes that affect position/size
          if (mutation.type === 'attributes') {
            const attrName = mutation.attributeName;
            if (attrName === 'style' || attrName === 'class') {
              console.debug('MutationObserver detected attribute change:', attrName, mutation.target.tagName);
              shouldUpdate = true;
            }
            // Also watch for size-related attributes
            if (['rows', 'cols', 'width', 'height'].includes(attrName)) {
              console.debug('MutationObserver detected size attribute change:', attrName);
              shouldUpdate = true;
            }
          }

          // DOM structure changes
          if (mutation.type === 'childList') {
            console.debug('MutationObserver detected DOM structure change');
            shouldUpdate = true;
          }

          // Content changes
          if (mutation.type === 'characterData') {
            console.debug('MutationObserver detected content change');
            shouldUpdate = true;
          }
        });

        if (shouldUpdate) {
          // Debounce mutation updates
          if (this.mutationTimeout) {
            clearTimeout(this.mutationTimeout);
          }
          this.mutationTimeout = setTimeout(() => {
            this.triggerOverlayUpdate();
            this.mutationTimeout = null;
          }, 16);
        }
      });

      // Observe the element itself with comprehensive options
      this.textElementObserver.observe(element, {
        attributes: true,
        attributeOldValue: true,
        characterData: true,
        childList: true,
        subtree: true
      });

      // Observe parent elements for layout changes
      let parent = element.parentElement;
      let observedParents = 0;
      while (parent && observedParents < 2) {
        this.textElementObserver.observe(parent, {
          attributes: true,
          attributeFilter: ['style', 'class'],
          childList: true
        });
        parent = parent.parentElement;
        observedParents++;
      }

      console.debug('MutationObserver set up for element and', observedParents, 'parents');
    }

    // Position polling removed
  }


  cleanupTextElementObservers() {
    if (this.textElementObserver) {
      this.textElementObserver.disconnect();
      this.textElementObserver = null;
    }

    if (this.textElementResizeObserver) {
      this.textElementResizeObserver.disconnect();
      this.textElementResizeObserver = null;
    }

    if (this.inputEventHandler && this.currentFocusedElement) {
      this.currentFocusedElement.removeEventListener('input', this.inputEventHandler);
      this.inputEventHandler = null;
    }

    // Stop position tracking
    this.stopPositionTracking();

    if (this.resizeTimeout) {
      clearTimeout(this.resizeTimeout);
      this.resizeTimeout = null;
    }

    if (this.mutationTimeout) {
      clearTimeout(this.mutationTimeout);
      this.mutationTimeout = null;
    }

    if (this.inputTimeout) {
      clearTimeout(this.inputTimeout);
      this.inputTimeout = null;
    }

    console.debug('Text element observers cleaned up');
  }

  triggerOverlayUpdate() {
    // Trigger a state update to refresh overlays with current element position
    if (this.currentFocusedElement && this.state.getState().mode === 'text_focus') {
      // Force overlay update by triggering a state change
      this.state.setState({
        mode: 'text_focus',
        focusedTextElement: this.currentFocusedElement,
        _overlayUpdateTrigger: Date.now() // Unique value to force update
      });

      console.debug('Triggered overlay update for text element position/size change');
    }
  }
}


  // Module: src/modules/mouse-coordinate-manager.js
/**
 * Mouse coordinate storage and management
 * Stores mouse coordinates in Chrome storage when links are clicked
 * and retrieves them for cursor positioning on page load
 */

class MouseCoordinateManager {
  constructor() {
    this.storageKey = 'keypilot_last_mouse_coordinates';
    this.lastStoredCoordinates = null;
    this.isMonitoringInactive = false;
    this.inactiveMouseHandler = null;
    this.beforeUnloadHandler = null;
    this.currentMousePosition = { x: 0, y: 0 };
    
    // Initialize by loading any existing coordinates
    this.loadStoredCoordinates();
    
    // Set up beforeunload listener to store coordinates when leaving page
    this.setupBeforeUnloadListener();
  }

  /**
   * Persist coordinates to localStorage (works even when extension context is dead).
   * @param {object} coordinates
   */
  _storeCoordinatesLocally(coordinates) {
    try {
      localStorage.setItem(this.storageKey, JSON.stringify(coordinates));
    } catch {
      // Ignore quota / private-mode failures
    }
  }

  /**
   * Store mouse coordinates to Chrome storage
   * @param {number} x - X coordinate
   * @param {number} y - Y coordinate
   * @param {string} url - Current page URL for context
   */
  async storeCoordinates(x, y, url = window.location.href) {
    const coordinates = {
      x: x,
      y: y,
      url: url,
      timestamp: Date.now(),
      viewport: {
        width: window.innerWidth,
        height: window.innerHeight
      }
    };

    try {
      // After extension reload/update, chrome.storage throws "Extension context invalidated".
      // Prefer chrome storage when available; always keep a localStorage fallback.
      if (isExtensionContextValid()) {
        if (chrome?.storage?.sync) {
          await chrome.storage.sync.set({ [this.storageKey]: coordinates });
        } else if (chrome?.storage?.local) {
          await chrome.storage.local.set({ [this.storageKey]: coordinates });
        }
      }

      // Mirror to localStorage for unload / invalidated-context recovery.
      this._storeCoordinatesLocally(coordinates);
      this.lastStoredCoordinates = coordinates;
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Mouse coordinates stored:', coordinates);
      }
    } catch (error) {
      if (noteExtensionContextError(error)) {
        // Expected after extension reload; degrade to localStorage without console noise.
        this._storeCoordinatesLocally(coordinates);
        this.lastStoredCoordinates = coordinates;
        if (window.KEYPILOT_DEBUG) {
          console.warn('[KeyPilot] Extension context invalidated; stored mouse coordinates in localStorage only');
        }
        return;
      }
      console.warn('[KeyPilot] Failed to store mouse coordinates:', error);
    }
  }

  /**
   * Load stored mouse coordinates from Chrome storage
   * @returns {Promise<Object|null>} Stored coordinates or null if none found
   */
  async loadStoredCoordinates() {
    try {
      let result = null;

      // Try Chrome storage first (sync, then local) when context is valid
      if (isExtensionContextValid()) {
        if (chrome?.storage?.sync) {
          const syncResult = await chrome.storage.sync.get([this.storageKey]);
          result = syncResult[this.storageKey];
        }
        
        if (!result && chrome?.storage?.local) {
          const localResult = await chrome.storage.local.get([this.storageKey]);
          result = localResult[this.storageKey];
        }
      }
      
      // Fallback to localStorage
      if (!result) {
        const localStorageData = localStorage.getItem(this.storageKey);
        if (localStorageData) {
          result = JSON.parse(localStorageData);
        }
      }

      this.lastStoredCoordinates = result;
      
      if (window.KEYPILOT_DEBUG && result) {
        console.log('[KeyPilot] Loaded stored mouse coordinates:', result);
      }

      return result;
    } catch (error) {
      if (noteExtensionContextError(error)) {
        try {
          const localStorageData = localStorage.getItem(this.storageKey);
          if (localStorageData) {
            this.lastStoredCoordinates = JSON.parse(localStorageData);
            return this.lastStoredCoordinates;
          }
        } catch { /* ignore */ }
        return null;
      }
      console.warn('[KeyPilot] Failed to load stored mouse coordinates:', error);
      return null;
    }
  }

  /**
   * Get the best cursor position for initialization
   * Uses stored coordinates if available, otherwise falls back to upper left with margin
   * @returns {Object} Object with x, y coordinates
   */
  getInitialCursorPosition() {
    // If we have stored coordinates, use them (with viewport validation)
    if (this.lastStoredCoordinates) {
      const stored = this.lastStoredCoordinates;
      
      // Validate coordinates are within current viewport
      const x = Math.min(stored.x, window.innerWidth - 20);
      const y = Math.min(stored.y, window.innerHeight - 20);
      
      // Ensure coordinates are not negative
      const validX = Math.max(10, x);
      const validY = Math.max(10, y);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Using stored coordinates for cursor position:', {
          original: { x: stored.x, y: stored.y },
          adjusted: { x: validX, y: validY }
        });
      }
      
      return { x: validX, y: validY };
    }

    // Fallback: upper left corner with 10pt margin
    const fallbackX = 10;
    const fallbackY = 10;
    
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot] No stored coordinates, using fallback position:', {
        x: fallbackX, 
        y: fallbackY
      });
    }
    
    return { x: fallbackX, y: fallbackY };
  }

  /**
   * Update current mouse position for beforeunload storage
   * @param {number} x - X coordinate
   * @param {number} y - Y coordinate
   */
  updateCurrentMousePosition(x, y) {
    this.currentMousePosition.x = x;
    this.currentMousePosition.y = y;
  }

  /**
   * Set up beforeunload listener to store coordinates when leaving page
   */
  setupBeforeUnloadListener() {
    this.beforeUnloadHandler = () => {
      // Store current mouse position when leaving the page
      if (this.currentMousePosition.x !== 0 || this.currentMousePosition.y !== 0) {
        // Use synchronous storage for beforeunload to ensure it completes
        this.storeCoordinatesSync(this.currentMousePosition.x, this.currentMousePosition.y);
        
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot] Page unloading, stored coordinates:', this.currentMousePosition);
        }
      }
    };

    window.addEventListener('beforeunload', this.beforeUnloadHandler);
    
    // Also listen for pagehide as a more reliable alternative
    window.addEventListener('pagehide', this.beforeUnloadHandler);
    
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot] Set up beforeunload coordinate storage');
    }
  }

  /**
   * Synchronous coordinate storage for beforeunload events
   * @param {number} x - X coordinate
   * @param {number} y - Y coordinate
   */
  storeCoordinatesSync(x, y) {
    const coordinates = {
      x: x,
      y: y,
      url: window.location.href,
      timestamp: Date.now(),
      viewport: {
        width: window.innerWidth,
        height: window.innerHeight
      },
      storedOnUnload: true
    };

    try {
      // Use localStorage for synchronous storage during beforeunload
      this._storeCoordinatesLocally(coordinates);
      this.lastStoredCoordinates = coordinates;
      
      // Also try Chrome storage but don't wait for it (skip if context is dead)
      if (isExtensionContextValid() && chrome?.storage?.local) {
        chrome.storage.local.set({ [this.storageKey]: coordinates }).catch((error) => {
          noteExtensionContextError(error);
          // Ignore errors during beforeunload
        });
      }
    } catch (error) {
      // Ignore storage errors during beforeunload
      if (noteExtensionContextError(error)) return;
      if (window.KEYPILOT_DEBUG) {
        console.warn('[KeyPilot] Failed to store coordinates on beforeunload:', error);
      }
    }
  }

  /**
   * Handle link click event to store coordinates (legacy method, now optional)
   * @param {number} x - X coordinate where link was clicked
   * @param {number} y - Y coordinate where link was clicked
   * @param {Element} linkElement - The link element that was clicked
   */
  handleLinkClick(x, y, linkElement) {
    // Update current position for beforeunload storage
    this.updateCurrentMousePosition(x, y);
    
    if (linkElement && linkElement.tagName === 'A' && linkElement.href) {
      // Optionally store coordinates immediately on link click for redundancy
      this.storeCoordinates(x, y, linkElement.href);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Link clicked, storing coordinates:', {
          x, y, 
          href: linkElement.href,
          linkText: linkElement.textContent?.substring(0, 50)
        });
      }
    }
  }

  /**
   * Start monitoring mouse events when application is not active
   * Uses Page Visibility API to detect when page is not active
   */
  startInactiveMouseMonitoring() {
    if (this.isMonitoringInactive) {
      return; // Already monitoring
    }

    // Check if Page Visibility API is available
    if (typeof document.hidden === 'undefined') {
      console.warn('[KeyPilot] Page Visibility API not available, cannot monitor inactive mouse events');
      return;
    }

    this.isMonitoringInactive = true;

    // Create mouse event handler for inactive monitoring
    this.inactiveMouseHandler = (event) => {
      // Only track when page is hidden/inactive
      if (document.hidden) {
        // Store coordinates even when page is inactive
        // This helps maintain cursor position context
        this.storeCoordinates(event.clientX, event.clientY);
        
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot] Mouse event captured while page inactive:', {
            x: event.clientX,
            y: event.clientY,
            hidden: document.hidden
          });
        }
      }
    };

    // Add mouse move listener with passive option for performance
    document.addEventListener('mousemove', this.inactiveMouseHandler, { 
      passive: true,
      capture: true 
    });

    // Listen for visibility changes
    document.addEventListener('visibilitychange', () => {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Page visibility changed:', {
          hidden: document.hidden,
          visibilityState: document.visibilityState
        });
      }
    });

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot] Started inactive mouse monitoring');
    }
  }

  /**
   * Stop monitoring mouse events when application is not active
   */
  stopInactiveMouseMonitoring() {
    if (!this.isMonitoringInactive) {
      return;
    }

    this.isMonitoringInactive = false;

    if (this.inactiveMouseHandler) {
      document.removeEventListener('mousemove', this.inactiveMouseHandler, { 
        passive: true,
        capture: true 
      });
      this.inactiveMouseHandler = null;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot] Stopped inactive mouse monitoring');
    }
  }

  /**
   * Clear stored coordinates
   */
  async clearStoredCoordinates() {
    try {
      if (isExtensionContextValid()) {
        if (chrome?.storage?.sync) {
          await chrome.storage.sync.remove([this.storageKey]);
        }
        if (chrome?.storage?.local) {
          await chrome.storage.local.remove([this.storageKey]);
        }
      }
      localStorage.removeItem(this.storageKey);
      
      this.lastStoredCoordinates = null;
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot] Cleared stored mouse coordinates');
      }
    } catch (error) {
      if (noteExtensionContextError(error)) {
        try { localStorage.removeItem(this.storageKey); } catch { /* ignore */ }
        this.lastStoredCoordinates = null;
        return;
      }
      console.warn('[KeyPilot] Failed to clear stored coordinates:', error);
    }
  }

  /**
   * Get debug information about stored coordinates
   * @returns {Object} Debug information
   */
  getDebugInfo() {
    return {
      hasStoredCoordinates: !!this.lastStoredCoordinates,
      storedCoordinates: this.lastStoredCoordinates,
      isMonitoringInactive: this.isMonitoringInactive,
      storageKey: this.storageKey,
      pageVisibilitySupported: typeof document.hidden !== 'undefined',
      currentVisibility: {
        hidden: document.hidden,
        visibilityState: document.visibilityState
      }
    };
  }

  /**
   * Cleanup method to be called when the extension is disabled or unloaded
   */
  cleanup() {
    this.stopInactiveMouseMonitoring();
    
    // Remove beforeunload listeners
    if (this.beforeUnloadHandler) {
      window.removeEventListener('beforeunload', this.beforeUnloadHandler);
      window.removeEventListener('pagehide', this.beforeUnloadHandler);
      this.beforeUnloadHandler = null;
    }
  }
}



  // Module: src/modules/highlight-manager.js


/**
 * HighlightManager - Manages all highlighting functionality including overlays and selection
 */
class HighlightManager extends EventManager {
  constructor() {
    super();

    // Highlight overlays
    this.highlightOverlay = null; // Overlay for highlight mode
    this.highlightRectangleOverlay = null; // Real-time highlight rectangle overlay
    this.highlightSelectionOverlays = []; // Array of overlays for selected text regions
    this.highlightModeIndicator = null; // Visual indicator for highlight mode

    // Selection mode state
    this.selectionMode = 'character'; // 'character' or 'rectangle'
    
    // Character selection state
    this.characterSelectionActive = false;
    this.characterStartPosition = null; // Starting position for character selection
    this.characterStartTextNode = null; // Starting text node
    this.characterStartOffset = 0; // Starting character offset

    // Rectangle selection state
    this.rectOriginPoint = null; // Origin point established by first press (viewport at press time)
    this.rectOriginDocumentPoint = null; // Origin in document coordinates (scroll-stable anchor)
    // Rectangle mode start caret (node+offset) — document-anchored; do not re-resolve from frozen viewport
    this.rectangleStartCaret = null;



    // Debug HUD
    this.debugHUD = null; // Debug HUD overlay for live rectangle debugging
    this.debugUpdateCount = 0; // Counter for debug updates

    // Overlay visibility tracking
    this.overlayVisibility = {
      highlight: true,
      highlightRectangle: true
    };

    // Intersection observer for performance optimization
    this.overlayObserver = null;
  }

  /**
   * Initialize the highlight manager with intersection observer
   */
  initialize(overlayObserver) {
    this.overlayObserver = overlayObserver;
  }

  /**
   * Set the selection mode
   * @param {string} mode - 'character' or 'rectangle'
   */
  setSelectionMode(mode) {
    if (mode === 'character' || mode === 'rectangle') {
      this.selectionMode = mode;
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Selection mode set to:', mode);
      }
    }
  }

  /**
   * Get the current selection mode
   * @returns {string} - Current selection mode
   */
  getSelectionMode() {
    return this.selectionMode;
  }

  /**
   * Initialize the highlight manager with edge-only rectangle intersection observer
   * @param {RectangleIntersectionObserver} rectangleObserver - Edge-only intersection observer
   * @param {Function} notificationCallback - Callback for user notifications
   */
  initializeEdgeOnlyProcessing(rectangleObserver, notificationCallback = null) {
    this.rectangleIntersectionObserver = rectangleObserver;
    this.edgeOnlyProcessingEnabled = FEATURE_FLAGS.USE_EDGE_ONLY_SELECTION && FEATURE_FLAGS.ENABLE_EDGE_ONLY_PROCESSING;
    
    // Set up notification callback for performance monitoring
    if (notificationCallback && this.rectangleIntersectionObserver) {
      this.rectangleIntersectionObserver.setNotificationCallback(notificationCallback);
    }
    
    if (window.KEYPILOT_DEBUG && FEATURE_FLAGS.DEBUG_EDGE_ONLY_PROCESSING) {
      console.log('[KeyPilot Debug] Highlight manager initialized with edge-only processing:', {
        enabled: this.edgeOnlyProcessingEnabled,
        observer: !!this.rectangleIntersectionObserver,
        caching: FEATURE_FLAGS.ENABLE_SELECTION_CACHING,
        monitoring: FEATURE_FLAGS.ENABLE_EDGE_PERFORMANCE_MONITORING,
        notificationCallback: !!notificationCallback
      });
    }
  }

  /**
   * Create a DOM element with specified properties
   */
  createElement(tagName, properties = {}) {
    const element = document.createElement(tagName);

    if (properties.className) {
      element.className = properties.className;
    }

    if (properties.style) {
      element.style.cssText = properties.style;
    }

    return element;
  }

  /**
   * Update highlight overlay for focused element
   */
  updateHighlightOverlay(element) {
    if (!element) {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] updateHighlightOverlay: no element provided');
      }
      this.hideHighlightOverlay();
      return;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateHighlightOverlay called for:', {
        tagName: element.tagName,
        className: element.className,
        id: element.id
      });
    }

    if (!this.highlightOverlay) {
      this.highlightOverlay = this.createElement('div', {
        className: CSS_CLASSES.HIGHLIGHT_OVERLAY,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS};
          border: 3px solid ${COLORS.HIGHLIGHT_BLUE};
          box-shadow: 0 0 0 2px ${COLORS.HIGHLIGHT_SHADOW}, 0 0 12px 2px ${COLORS.HIGHLIGHT_SHADOW_BRIGHT};
          background: transparent;
          will-change: transform;
        `
      });
      document.body.appendChild(this.highlightOverlay);

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight overlay created and added to DOM:', {
          element: this.highlightOverlay,
          className: this.highlightOverlay.className,
          parent: this.highlightOverlay.parentElement?.tagName
        });
      }

      // Start observing the overlay for visibility optimization
      if (this.overlayObserver) {
        this.overlayObserver.observe(this.highlightOverlay);
      }
    }

    const rect = element.getBoundingClientRect();

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Highlight overlay positioning:', {
        rect: rect,
        overlayExists: !!this.highlightOverlay,
        overlayVisibility: this.overlayVisibility.highlight
      });
    }

    if (rect.width > 0 && rect.height > 0) {
      this.highlightOverlay.style.left = `${rect.left}px`;
      this.highlightOverlay.style.top = `${rect.top}px`;
      this.highlightOverlay.style.width = `${rect.width}px`;
      this.highlightOverlay.style.height = `${rect.height}px`;
      this.highlightOverlay.style.display = 'block';
      this.highlightOverlay.style.visibility = 'visible';

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight overlay positioned at:', {
          left: rect.left, top: rect.top, width: rect.width, height: rect.height
        });
      }
    } else {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight overlay hidden - invalid rect:', rect);
      }
      this.hideHighlightOverlay();
    }
  }

  /**
   * Hide the highlight overlay
   */
  hideHighlightOverlay() {
    if (this.highlightOverlay) {
      this.highlightOverlay.style.display = 'none';

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight overlay hidden');
      }
    }
  }

  /**
   * Update edge-only processing rectangle bounds for all workflows
   * @param {Object} rectOriginPoint - Origin point {x, y} (viewport coordinates)
   * @param {Object} currentPosition - Current cursor position {x, y} (viewport coordinates)
   */
  updateEdgeOnlyProcessingRectangle(rectOriginPoint, currentPosition) {
    if (!this.edgeOnlyProcessingEnabled || !this.rectangleIntersectionObserver) {
      return;
    }

    if (!rectOriginPoint || !currentPosition) {
      return;
    }

    try {
      // Convert viewport coordinates to document coordinates
      const scrollX = window.pageXOffset || document.documentElement.scrollLeft;
      const scrollY = window.pageYOffset || document.documentElement.scrollTop;

      const originDocX = rectOriginPoint.x + scrollX;
      const originDocY = rectOriginPoint.y + scrollY;
      const currentDocX = currentPosition.x + scrollX;
      const currentDocY = currentPosition.y + scrollY;

      // Calculate rectangle bounds in document coordinates
      const rect = {
        left: Math.min(originDocX, currentDocX),
        top: Math.min(originDocY, currentDocY),
        width: Math.abs(currentDocX - originDocX),
        height: Math.abs(currentDocY - originDocY)
      };

      // Update edge-only processing rectangle
      this.rectangleIntersectionObserver.updateRectangle(rect);

      if (window.KEYPILOT_DEBUG && FEATURE_FLAGS.DETAILED_EDGE_LOGGING) {
        console.log('[KeyPilot Debug] Edge-only processing rectangle updated:', {
          viewport: { origin: rectOriginPoint, current: currentPosition },
          document: { originDocX, originDocY, currentDocX, currentDocY },
          rect: rect
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error updating edge-only processing rectangle:', error);
    }
  }

  /**
   * Document-anchored origin converted to the current viewport (for caret resolve / overlay).
   * @returns {{x:number,y:number}|null}
   */
  getOriginViewportPoint() {
    if (!this.rectOriginDocumentPoint) return null;
    const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
    const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
    return {
      x: this.rectOriginDocumentPoint.x - scrollX,
      y: this.rectOriginDocumentPoint.y - scrollY
    };
  }

  /**
   * Ensure the selection origin is stored in document space (scroll-stable).
   * @param {Object} viewportPoint - {x,y} viewport coordinates at anchor time
   */
  ensureRectOriginDocumentPoint(viewportPoint) {
    if (this.rectOriginDocumentPoint || !viewportPoint) return;
    const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
    const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
    this.rectOriginPoint = { ...viewportPoint };
    this.rectOriginDocumentPoint = {
      x: viewportPoint.x + scrollX,
      y: viewportPoint.y + scrollY
    };
  }

  /**
   * Update the highlight rectangle overlay showing the selection area.
   * Origin is document-anchored so the rectangle stays glued to content while scrolling.
   * @param {Object} rectOriginPoint - Origin point from first press {x, y} (viewport at press; used only to seed)
   * @param {Object} currentPosition - Current cursor position {x, y} (viewport coordinates)
   */
  updateHighlightRectangleOverlay(rectOriginPoint, currentPosition) {
    if (!currentPosition) {
      this.hideHighlightRectangleOverlay();
      return;
    }

    // Seed document-space origin once; never recompute from a frozen viewport after scroll.
    if (!this.rectOriginDocumentPoint) {
      if (!rectOriginPoint) {
        this.hideHighlightRectangleOverlay();
        return;
      }
      this.ensureRectOriginDocumentPoint(rectOriginPoint);
      this.showDebugHUD();
    }

    const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
    const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;

    // Free end follows the cursor in viewport space → document via current scroll
    const currentDocumentPosition = {
      x: currentPosition.x + scrollX,
      y: currentPosition.y + scrollY
    };

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateHighlightRectangleOverlay called:', {
        rectOriginPoint,
        currentPosition,
        rectOriginDocumentPoint: this.rectOriginDocumentPoint,
        currentDocumentPosition,
        scrollX: window.scrollX,
        scrollY: window.scrollY
      });
    }

    if (!this.highlightRectangleOverlay) {
      this.highlightRectangleOverlay = this.createElement('div', {
        className: 'kpv2-highlight-rectangle-overlay',
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_BELOW_2};
          background: ${COLORS.HIGHLIGHT_SELECTION_BG};
          border: 2px dashed ${COLORS.HIGHLIGHT_BLUE};
          box-shadow: 0 0 0 1px ${COLORS.HIGHLIGHT_SHADOW}, 0 0 8px 1px ${COLORS.HIGHLIGHT_SHADOW_BRIGHT};
          will-change: transform;
          opacity: 0.8;
          box-sizing: border-box;
        `
      });
      document.body.appendChild(this.highlightRectangleOverlay);

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight rectangle overlay created and added to DOM:', {
          element: this.highlightRectangleOverlay,
          className: this.highlightRectangleOverlay.className,
          parent: this.highlightRectangleOverlay.parentElement?.tagName
        });
      }

      // Start observing the overlay for visibility optimization
      if (this.overlayObserver) {
        this.overlayObserver.observe(this.highlightRectangleOverlay);
      }
    }

    // Desktop file selection behavior: rectangle from document-anchored origin → free end
    const documentLeft = Math.min(this.rectOriginDocumentPoint.x, currentDocumentPosition.x);
    const documentTop = Math.min(this.rectOriginDocumentPoint.y, currentDocumentPosition.y);
    const width = Math.abs(currentDocumentPosition.x - this.rectOriginDocumentPoint.x);
    const height = Math.abs(currentDocumentPosition.y - this.rectOriginDocumentPoint.y);

    // Convert document coordinates back to viewport for fixed-position overlay
    const viewportLeft = documentLeft - scrollX;
    const viewportTop = documentTop - scrollY;

    // Calculate direction for debugging
    const deltaX = currentDocumentPosition.x - this.rectOriginDocumentPoint.x;
    const deltaY = currentDocumentPosition.y - this.rectOriginDocumentPoint.y;
    const quadrant = this.getQuadrant(deltaX, deltaY);

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Highlight rectangle overlay positioning:', {
        documentLeft, documentTop, width, height,
        viewportLeft, viewportTop,
        rectOriginDocumentPoint: this.rectOriginDocumentPoint,
        currentDocumentPosition,
        deltaX, deltaY, quadrant,
        direction: {
          horizontal: deltaX >= 0 ? 'right' : 'left',
          vertical: deltaY >= 0 ? 'down' : 'up'
        }
      });
    }

    // Prepare calculated values for debug HUD
    const calculatedValues = {
      documentLeft,
      documentTop,
      width,
      height,
      viewportLeft,
      viewportTop,
      quadrant
    };

    // Update debug HUD with current information
    this.updateDebugHUD(
      rectOriginPoint,
      currentPosition,
      this.rectOriginDocumentPoint,
      currentDocumentPosition,
      calculatedValues
    );

    // Edge-only stack is for rectangle mode only (character uses caret APIs).
    if (this.selectionMode === 'rectangle') {
      this.updateEdgeOnlyProcessingRectangle(rectOriginPoint, currentPosition);
    }

    // Determine if rectangle should be visible based on configuration
    const shouldShowRectangle = this.shouldShowRectangle(width, height, deltaX, deltaY);
    
    if (shouldShowRectangle) {
      this.highlightRectangleOverlay.style.left = `${viewportLeft}px`;
      this.highlightRectangleOverlay.style.top = `${viewportTop}px`;
      this.highlightRectangleOverlay.style.width = `${width}px`;
      this.highlightRectangleOverlay.style.height = `${height}px`;
      this.highlightRectangleOverlay.style.display = 'block';
      this.highlightRectangleOverlay.style.visibility = 'visible';

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight rectangle overlay positioned at:', {
          viewportLeft, viewportTop, width, height, quadrant,
          shouldShow: shouldShowRectangle,
          minWidth: RECTANGLE_SELECTION.MIN_WIDTH,
          minHeight: RECTANGLE_SELECTION.MIN_HEIGHT
        });
      }
    } else {
      this.highlightRectangleOverlay.style.display = 'none';

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight rectangle overlay hidden:', { 
          width, height, 
          shouldShow: shouldShowRectangle,
          minWidth: RECTANGLE_SELECTION.MIN_WIDTH,
          minHeight: RECTANGLE_SELECTION.MIN_HEIGHT,
          reason: width < RECTANGLE_SELECTION.MIN_WIDTH ? 'width too small' : 
                  height < RECTANGLE_SELECTION.MIN_HEIGHT ? 'height too small' : 'other'
        });
      }
    }
  }

  /**
   * Determine which quadrant the current position is relative to the origin
   * @param {number} deltaX - Horizontal distance from origin
   * @param {number} deltaY - Vertical distance from origin
   * @returns {string} - Quadrant identifier
   */
  getQuadrant(deltaX, deltaY) {
    if (deltaX >= 0 && deltaY >= 0) return 'bottom-right';
    if (deltaX < 0 && deltaY >= 0) return 'bottom-left';
    if (deltaX < 0 && deltaY < 0) return 'top-left';
    if (deltaX >= 0 && deltaY < 0) return 'top-right';
    return 'origin';
  }

  /**
   * Determine if the rectangle should be visible based on size and configuration
   * @param {number} width - Rectangle width in pixels
   * @param {number} height - Rectangle height in pixels
   * @param {number} deltaX - Horizontal distance from origin
   * @param {number} deltaY - Vertical distance from origin
   * @returns {boolean} - Whether rectangle should be shown
   */
  shouldShowRectangle(width, height, deltaX, deltaY) {
    // Always hide if zero dimensions and HIDE_ZERO_SIZE is enabled
    if (RECTANGLE_SELECTION.HIDE_ZERO_SIZE && (width === 0 || height === 0)) {
      return false;
    }

    // Show immediate feedback for any movement if enabled
    if (RECTANGLE_SELECTION.SHOW_IMMEDIATE_FEEDBACK && (Math.abs(deltaX) > 0 || Math.abs(deltaY) > 0)) {
      return true;
    }

    // Check minimum size requirements
    const meetsMinWidth = width >= RECTANGLE_SELECTION.MIN_WIDTH;
    const meetsMinHeight = height >= RECTANGLE_SELECTION.MIN_HEIGHT;
    
    // Check minimum drag distance
    const dragDistance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    const meetsMinDrag = dragDistance >= RECTANGLE_SELECTION.MIN_DRAG_DISTANCE;

    // Rectangle is visible if it meets size requirements OR minimum drag distance
    return (meetsMinWidth && meetsMinHeight) || meetsMinDrag;
  }

  /**
   * Hide the highlight rectangle overlay
   */
  hideHighlightRectangleOverlay() {
    if (this.highlightRectangleOverlay) {
      this.highlightRectangleOverlay.style.display = 'none';

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight rectangle overlay hidden');
      }
    }

    // Reset rectangle selection state
    this.resetRectangleSelection();
  }

  /**
   * Remove the highlight rectangle overlay from the DOM and drop the element
   * reference, so the next rectangle session starts with a fresh element.
   * (Hiding alone leaves a detached node that a later session would restyle
   * without re-adding it to the DOM — invisible.)
   */
  removeHighlightRectangleOverlay() {
    if (this.highlightRectangleOverlay) {
      if (this.overlayObserver) {
        try { this.overlayObserver.unobserve(this.highlightRectangleOverlay); } catch { /* ignore */ }
      }
      try { this.highlightRectangleOverlay.remove(); } catch { /* ignore */ }
      this.highlightRectangleOverlay = null;

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight rectangle overlay removed');
      }
    }

    // Reset rectangle selection state
    this.resetRectangleSelection();
  }

  /**
   * Get current selection from edge-only processing
   * @returns {Selection|null} - Browser selection object or null
   */
  getEdgeOnlySelection() {
    if (!this.edgeOnlyProcessingEnabled || !this.rectangleIntersectionObserver) {
      return null;
    }

    try {
      return this.rectangleIntersectionObserver.createSelectionFromIntersection();
    } catch (error) {
      console.warn('[KeyPilot] Error getting edge-only selection:', error);
      return null;
    }
  }

  /**
   * Start character-level selection at the given position
   * Prefer native caret APIs; fall back to findTextNode only if needed.
   * @param {Object} position - Position {x, y} in viewport coordinates
   * @param {Function} findTextNodeAtPosition - Function to find text node at position
   * @param {Function} getTextOffsetAtPosition - Function to get text offset at position
   */
  startCharacterSelection(position, findTextNodeAtPosition, getTextOffsetAtPosition) {
    if (!position || typeof position.x !== 'number' || typeof position.y !== 'number') {
      console.warn('[KeyPilot] Invalid position for character selection:', position);
      return false;
    }

    try {
      // Fresh session: drop any leftover overlay DOM from a previous run
      this.clearHighlightSelectionOverlays();

      // Document-anchored origin for the dashed guide rectangle
      this.rectOriginPoint = { ...position };
      this.rectOriginDocumentPoint = {
        x: position.x + (window.pageXOffset || document.documentElement.scrollLeft || 0),
        y: position.y + (window.pageYOffset || document.documentElement.scrollTop || 0)
      };

      // Prefer O(1) caret API over TreeWalker scan
      let textNode = null;
      let offset = 0;
      const caret = this.resolveCaretAtPoint(position.x, position.y, document);
      if (caret?.textNode) {
        textNode = caret.textNode;
        offset = caret.offset;
      } else if (typeof findTextNodeAtPosition === 'function') {
        textNode = findTextNodeAtPosition(position.x, position.y);
        if (textNode && typeof getTextOffsetAtPosition === 'function') {
          offset = getTextOffsetAtPosition(textNode, position.x, position.y);
        }
      }

      if (!textNode || textNode.nodeType !== Node.TEXT_NODE) {
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot Debug] No text node found at position for character selection');
        }
        // Keep origin for rectangle guide; selection arms when cursor hits text
        this.characterSelectionActive = false;
        this.characterStartTextNode = null;
        this.characterStartOffset = 0;
        this.characterStartPosition = { ...position };
        return false;
      }

      if (typeof offset !== 'number' || offset < 0) {
        offset = 0;
      }
      const maxOff = textNode.textContent ? textNode.textContent.length : 0;
      offset = Math.max(0, Math.min(offset, maxOff));

      // Store character selection state
      this.characterSelectionActive = true;
      this.characterStartPosition = { ...position };
      this.characterStartTextNode = textNode;
      this.characterStartOffset = offset;

      // Create initial selection range
      const ownerDocument = textNode.ownerDocument || document;
      const range = ownerDocument.createRange();
      range.setStart(textNode, offset);
      range.setEnd(textNode, offset);

      // Set browser selection (native paint — no custom overlays on the hot path)
      const selection = this.getSelectionForDocument(ownerDocument);
      if (selection) {
        selection.removeAllRanges();
        selection.addRange(range);
      }

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Character selection started:', {
          position,
          textNode: textNode.textContent?.substring(0, 50),
          offset,
          selectedText: textNode.textContent?.charAt(offset)
        });
      }

      return true;
    } catch (error) {
      console.error('[KeyPilot] Error starting character selection:', error);
      this.resetCharacterSelection();
      return false;
    }
  }

  /**
   * Resolve a caret (text node + offset) at viewport coordinates via native APIs.
   * Prefer caretRangeFromPoint / caretPositionFromPoint — O(1) vs TreeWalker scans.
   * @param {number} x
   * @param {number} y
   * @param {Document} [ownerDocument]
   * @returns {{ textNode: Text, offset: number }|null}
   */
  resolveCaretAtPoint(x, y, ownerDocument = document) {
    try {
      const doc = ownerDocument || document;

      // Chromium / Safari
      if (typeof doc.caretRangeFromPoint === 'function') {
        const caretRange = doc.caretRangeFromPoint(x, y);
        if (caretRange && caretRange.startContainer) {
          const node = caretRange.startContainer;
          if (node.nodeType === Node.TEXT_NODE) {
            return { textNode: node, offset: caretRange.startOffset };
          }
          // Element container: try first text child at offset
          if (node.nodeType === Node.ELEMENT_NODE && node.childNodes?.length) {
            const child = node.childNodes[Math.min(caretRange.startOffset, node.childNodes.length - 1)];
            if (child?.nodeType === Node.TEXT_NODE) {
              return { textNode: child, offset: 0 };
            }
          }
        }
      }

      // Firefox
      if (typeof doc.caretPositionFromPoint === 'function') {
        const pos = doc.caretPositionFromPoint(x, y);
        if (pos && pos.offsetNode) {
          const node = pos.offsetNode;
          if (node.nodeType === Node.TEXT_NODE) {
            return { textNode: node, offset: pos.offset };
          }
        }
      }
    } catch {
      // ignore
    }
    return null;
  }

  /**
   * Build a forward/backward Range from two caret points and apply it to Selection.
   * @param {{ textNode: Text, offset: number }} start
   * @param {{ textNode: Text, offset: number }} end
   * @returns {Selection|null}
   */
  applyCaretSelection(start, end) {
    if (!start?.textNode || !end?.textNode) return null;
    try {
      const ownerDocument = start.textNode.ownerDocument || document;
      const selection = this.getSelectionForDocument(ownerDocument);
      if (!selection) return null;

      // Determine document order so setStart/setEnd never throws for reverse drag.
      let first = start;
      let second = end;
      if (start.textNode === end.textNode) {
        if (start.offset > end.offset) {
          first = end;
          second = start;
        }
      } else {
        const pos = start.textNode.compareDocumentPosition(end.textNode);
        if (pos & Node.DOCUMENT_POSITION_PRECEDING) {
          // end is before start
          first = end;
          second = start;
        }
      }

      const range = ownerDocument.createRange();
      range.setStart(first.textNode, first.offset);
      range.setEnd(second.textNode, second.offset);

      selection.removeAllRanges();
      selection.addRange(range);
      return selection;
    } catch (error) {
      if (window.KEYPILOT_DEBUG) {
        console.warn('[KeyPilot] applyCaretSelection failed:', error);
      }
      return null;
    }
  }

  /**
   * Update character-level selection to the current position.
   * Fast path only: native caret APIs (like browser drag-select).
   * The legacy full-document rectangle character scan is intentionally not used
   * on the mousemove path — it freezes complex pages.
   *
   * @param {Object} currentPosition - Current position {x, y} in viewport coordinates
   * @param {Object} startPosition - Start position {x, y} in viewport coordinates  
   * @param {Function} findTextNodeAtPosition - Function to find text node at position
   * @param {Function} getTextOffsetAtPosition - Function to get text offset at position
   */
  updateCharacterSelection(currentPosition, startPosition, findTextNodeAtPosition, getTextOffsetAtPosition) {
    if (!currentPosition || typeof currentPosition.x !== 'number' || typeof currentPosition.y !== 'number') {
      return false;
    }

    // Drop detached start nodes (page re-render) so we can re-seed cleanly
    if (this.characterStartTextNode && !this.characterStartTextNode.isConnected) {
      this.characterSelectionActive = false;
      this.characterStartTextNode = null;
      this.characterStartOffset = 0;
    }

    if (!this.characterSelectionActive || !this.characterStartTextNode) {
      // Late start: first press was not on text, or start node went away.
      // Prefer caret API only (cheap). Avoid findTextNode TreeWalker on the hot path.
      const caret = this.resolveCaretAtPoint(currentPosition.x, currentPosition.y, document);
      if (caret?.textNode) {
        if (!this.rectOriginDocumentPoint && startPosition) {
          this.ensureRectOriginDocumentPoint(startPosition);
        } else if (!this.rectOriginDocumentPoint) {
          this.ensureRectOriginDocumentPoint(currentPosition);
        }
        this.characterSelectionActive = true;
        this.characterStartPosition = { ...currentPosition };
        this.characterStartTextNode = caret.textNode;
        this.characterStartOffset = caret.offset;
      } else {
        // Still draw guide rect from origin → cursor while hunting for text
        const originVp = this.getOriginViewportPoint() || startPosition;
        if (originVp) {
          this.updateHighlightRectangleOverlay(originVp, currentPosition);
        }
        return false;
      }
    }

    try {
      // Guide rect always uses document-anchored origin (scroll-stable).
      const originVp = this.getOriginViewportPoint() || startPosition;
      if (originVp) {
        this.updateHighlightRectangleOverlay(originVp, currentPosition);
      }

      const ownerDocument = this.characterStartTextNode.ownerDocument || document;
      const startCaret = {
        textNode: this.characterStartTextNode,
        offset: this.characterStartOffset
      };

      // Prefer native caret resolution at the live cursor (O(1))
      let endCaret = null;
      if (FEATURE_FLAGS.USE_NATIVE_SELECTION_API !== false) {
        endCaret = this.resolveCaretAtPoint(currentPosition.x, currentPosition.y, ownerDocument);
      }

      // Keep last good selection when cursor is between text nodes.
      // Do NOT call findTextNodeAtPosition here — it TreeWalks and freezes pages.
      if (!endCaret) {
        return true;
      }

      // Native Selection paint only — custom blue overlays thrash the main thread
      // (create/destroy dozens of nodes + IO observe on every move/scroll).
      const selection = this.applyCaretSelection(startCaret, endCaret);
      if (selection && window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Character selection updated (caret path):', {
          selectedText: selection.toString().substring(0, 100),
          rangeCount: selection.rangeCount,
          usedNativeCaret: true
        });
      }

      return true;
    } catch (error) {
      console.error('[KeyPilot] Error updating character selection:', error);
      return false;
    }
  }

  /**
   * Rectangle / intelligent selection: caret at origin → caret at current (browser drag semantics).
   * Origin caret is stored as node+offset (scroll-stable). Free end follows the mouse viewport.
   * Rectangle overlay is always document-anchored and safe to refresh during scroll.
   * @param {Object} startPosition - {x, y} viewport at press (seed only)
   * @param {Object} currentPosition - {x, y} viewport (live cursor)
   * @param {Function} [findTextNodeAtPosition]
   * @param {Function} [getTextOffsetAtPosition]
   * @returns {Selection|null}
   */
  updateRectangleSelectionFromCarets(startPosition, currentPosition, findTextNodeAtPosition, getTextOffsetAtPosition) {
    if (!currentPosition) return null;

    try {
      if (startPosition) {
        this.ensureRectOriginDocumentPoint(startPosition);
      }
      this.updateHighlightRectangleOverlay(startPosition, currentPosition);

      const ownerDocument = document;
      let startCaret = this.rectangleStartCaret;
      let endCaret = null;

      // Resolve / refresh start caret once (or if node detached after DOM change)
      const startNodeGone = startCaret?.textNode && !startCaret.textNode.isConnected;
      if (!startCaret || startNodeGone) {
        const originVp = this.getOriginViewportPoint() || startPosition;
        if (originVp) {
          if (FEATURE_FLAGS.USE_NATIVE_SELECTION_API !== false) {
            startCaret = this.resolveCaretAtPoint(originVp.x, originVp.y, ownerDocument);
          }
          if (!startCaret && typeof findTextNodeAtPosition === 'function') {
            const textNode = findTextNodeAtPosition(originVp.x, originVp.y);
            if (textNode && typeof getTextOffsetAtPosition === 'function') {
              const offset = getTextOffsetAtPosition(textNode, originVp.x, originVp.y);
              if (typeof offset === 'number' && offset >= 0) {
                startCaret = { textNode, offset };
              }
            }
          }
          if (startCaret) {
            this.rectangleStartCaret = startCaret;
          }
        }
      }

      if (FEATURE_FLAGS.USE_NATIVE_SELECTION_API !== false) {
        endCaret = this.resolveCaretAtPoint(currentPosition.x, currentPosition.y, ownerDocument);
      }

      if (!endCaret && typeof findTextNodeAtPosition === 'function') {
        const textNode = findTextNodeAtPosition(currentPosition.x, currentPosition.y);
        if (textNode && typeof getTextOffsetAtPosition === 'function') {
          const offset = getTextOffsetAtPosition(textNode, currentPosition.x, currentPosition.y);
          if (typeof offset === 'number' && offset >= 0) {
            endCaret = { textNode, offset };
          }
        }
      }

      if (!startCaret || !endCaret) {
        return null;
      }

      // Native Selection paint only (no custom per-rect overlays on hot path).
      return this.applyCaretSelection(startCaret, endCaret);
    } catch (error) {
      console.warn('[KeyPilot] Error updating rectangle selection from carets:', error);
      return null;
    }
  }

  /**
   * Reposition highlight rectangle + selection overlays after scroll without a mouse move.
   * Origin stays document-anchored; free end uses last known viewport mouse.
   * @param {Object} currentViewportMouse - {x,y}
   * @param {Function} [findTextNodeAtPosition]
   * @param {Function} [getTextOffsetAtPosition]
   * @returns {Selection|null|boolean}
   */
  syncSelectionToScroll(currentViewportMouse, findTextNodeAtPosition, getTextOffsetAtPosition) {
    if (!currentViewportMouse || !this.rectOriginDocumentPoint) {
      // Still try character path if active without rect seed
      if (this.characterSelectionActive && this.characterStartTextNode) {
        return this.updateCharacterSelection(
          currentViewportMouse,
          this.getOriginViewportPoint(),
          findTextNodeAtPosition,
          getTextOffsetAtPosition
        );
      }
      return null;
    }

    if (this.selectionMode === 'character' || this.characterSelectionActive) {
      return this.updateCharacterSelection(
        currentViewportMouse,
        this.getOriginViewportPoint(),
        findTextNodeAtPosition,
        getTextOffsetAtPosition
      );
    }

    return this.updateRectangleSelectionFromCarets(
      this.getOriginViewportPoint(),
      currentViewportMouse,
      findTextNodeAtPosition,
      getTextOffsetAtPosition
    );
  }

  /**
   * Create a character selection constrained to rectangle bounds
   * @param {Object} rectBounds - Rectangle bounds {left, top, right, bottom} in document coordinates
   * @returns {Selection|null} - Browser selection object or null
   */
  createRectangleConstrainedCharacterSelection(rectBounds) {
    try {
      const ownerDocument = this.characterStartTextNode.ownerDocument || document;
      const selection = this.getSelectionForDocument(ownerDocument);
      
      if (!selection) {
        return null;
      }

      // Clear existing selection
      selection.removeAllRanges();

      // Find the first and last character positions within the rectangle
      const { startPosition, endPosition } = this.findRectangleBoundaryPositions(rectBounds);
      
      if (!startPosition || !endPosition) {
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot Debug] No valid start/end positions found in rectangle');
        }
        return selection;
      }

      // Create a single range from start to end position
      const range = ownerDocument.createRange();
      range.setStart(startPosition.textNode, startPosition.offset);
      range.setEnd(endPosition.textNode, endPosition.offset);

      selection.addRange(range);

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Rectangle character selection created:', {
          startText: startPosition.textNode.textContent?.substring(startPosition.offset, startPosition.offset + 10),
          endText: endPosition.textNode.textContent?.substring(Math.max(0, endPosition.offset - 10), endPosition.offset),
          selectedText: selection.toString().substring(0, 100)
        });
      }

      return selection;
    } catch (error) {
      console.error('[KeyPilot] Error creating rectangle-constrained character selection:', error);
      return null;
    }
  }

  /**
   * Find the first and last character positions within the rectangle bounds
   * @param {Object} rectBounds - Rectangle bounds in document coordinates
   * @returns {Object} - {startPosition, endPosition} with textNode and offset
   */
  findRectangleBoundaryPositions(rectBounds) {
    let startPosition = null;
    let endPosition = null;

    try {
      // Find all text nodes that intersect with the rectangle
      const intersectingTextNodes = this.findTextNodesForHighlighting(rectBounds);
      
      if (intersectingTextNodes.length === 0) {
        return { startPosition: null, endPosition: null };
      }

      // Sort text nodes by document position
      intersectingTextNodes.sort((a, b) => {
        const comparison = a.compareDocumentPosition(b);
        if (comparison & Node.DOCUMENT_POSITION_FOLLOWING) {
          return -1; // a comes before b
        } else if (comparison & Node.DOCUMENT_POSITION_PRECEDING) {
          return 1; // a comes after b
        }
        return 0;
      });

      // Find the first character in the rectangle (topmost, leftmost)
      for (const textNode of intersectingTextNodes) {
        const firstCharOffset = this.findFirstCharacterInRectangle(textNode, rectBounds);
        if (firstCharOffset !== -1) {
          startPosition = { textNode, offset: firstCharOffset };
          break;
        }
      }

      // Find the last character in the rectangle (bottommost, rightmost)
      for (let i = intersectingTextNodes.length - 1; i >= 0; i--) {
        const textNode = intersectingTextNodes[i];
        const lastCharOffset = this.findLastCharacterInRectangle(textNode, rectBounds);
        if (lastCharOffset !== -1) {
          endPosition = { textNode, offset: lastCharOffset };
          break;
        }
      }

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Rectangle boundary positions:', {
          intersectingNodes: intersectingTextNodes.length,
          startPosition: startPosition ? {
            text: startPosition.textNode.textContent?.substring(0, 30),
            offset: startPosition.offset
          } : null,
          endPosition: endPosition ? {
            text: endPosition.textNode.textContent?.substring(0, 30),
            offset: endPosition.offset
          } : null
        });
      }

      return { startPosition, endPosition };
    } catch (error) {
      console.error('[KeyPilot] Error finding rectangle boundary positions:', error);
      return { startPosition: null, endPosition: null };
    }
  }

  /**
   * Find the first character in a text node that falls within the rectangle
   * @param {Text} textNode - Text node to search
   * @param {Object} rectBounds - Rectangle bounds in document coordinates
   * @returns {number} - Character offset or -1 if none found
   */
  findFirstCharacterInRectangle(textNode, rectBounds) {
    const text = textNode.textContent;
    if (!text) return -1;

    for (let i = 0; i < text.length; i++) {
      if (this.isCharacterInRectangle(textNode, i, rectBounds)) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Find the last character in a text node that falls within the rectangle
   * @param {Text} textNode - Text node to search
   * @param {Object} rectBounds - Rectangle bounds in document coordinates
   * @returns {number} - Character offset + 1 (for range end) or -1 if none found
   */
  findLastCharacterInRectangle(textNode, rectBounds) {
    const text = textNode.textContent;
    if (!text) return -1;

    for (let i = text.length - 1; i >= 0; i--) {
      if (this.isCharacterInRectangle(textNode, i, rectBounds)) {
        return i + 1; // Return offset + 1 for range end position
      }
    }
    return -1;
  }



  /**
   * Find text nodes that intersect with the rectangle
   * @param {Object} rectBounds - Rectangle bounds in document coordinates
   * @returns {Text[]} - Array of intersecting text nodes
   */
  // =============================================================================
  // VISUAL HIGHLIGHTING - Text node finding functions
  // These functions are specifically for visual highlighting that shows green
  // rectangles over text for UI feedback (not actual text selections)
  // =============================================================================

  /**
   * VISUAL HIGHLIGHTING: Finds text nodes for displaying highlight rectangles
   * Used by updateHighlightOverlay() to show green rectangles over text for UI feedback
   * @param {Object} rectBounds - Rectangle bounds with left, top, right, bottom properties
   * @returns {Array} - Array of text nodes that intersect with the rectangle
   */
  findTextNodesForHighlighting(rectBounds) {
    const textNodes = [];
    
    try {
      // Use TreeWalker to find all text nodes in the document
      const walker = document.createTreeWalker(
        document.body,
        NodeFilter.SHOW_TEXT,
        {
          acceptNode: (node) => {
            // Skip empty text nodes
            if (!node.textContent || !node.textContent.trim()) {
              return NodeFilter.FILTER_REJECT;
            }
            
            // Check if text node intersects with rectangle
            if (this.textNodeIntersectsRectangle(node, rectBounds)) {
              return NodeFilter.FILTER_ACCEPT;
            }
            
            return NodeFilter.FILTER_REJECT;
          }
        }
      );

      let node;
      while (node = walker.nextNode()) {
        textNodes.push(node);
      }
    } catch (error) {
      console.warn('[KeyPilot] Error finding text nodes in rectangle:', error);
    }

    return textNodes;
  }

  /**
   * Check if a text node intersects with the rectangle
   * @param {Text} textNode - Text node to check
   * @param {Object} rectBounds - Rectangle bounds in document coordinates
   * @returns {boolean} - True if intersects
   */
  textNodeIntersectsRectangle(textNode, rectBounds) {
    try {
      const range = document.createRange();
      range.selectNodeContents(textNode);
      const rect = range.getBoundingClientRect();
      
      // Convert viewport coordinates to document coordinates
      const scrollX = window.pageXOffset || document.documentElement.scrollLeft;
      const scrollY = window.pageYOffset || document.documentElement.scrollTop;
      
      const nodeLeft = rect.left + scrollX;
      const nodeTop = rect.top + scrollY;
      const nodeRight = nodeLeft + rect.width;
      const nodeBottom = nodeTop + rect.height;
      
      // Check for intersection
      return !(nodeRight < rectBounds.left || 
               nodeLeft > rectBounds.right || 
               nodeBottom < rectBounds.top || 
               nodeTop > rectBounds.bottom);
    } catch (error) {
      console.warn('[KeyPilot] Error checking text node intersection:', error);
      return false;
    }
  }



  /**
   * Check if a character position is within rectangle bounds
   * @param {Text} textNode - Text node containing the character
   * @param {number} offset - Character offset within the text node
   * @param {Object} rectBounds - Rectangle bounds in document coordinates
   * @returns {boolean} - True if character is within bounds
   */
  isCharacterInRectangle(textNode, offset, rectBounds) {
    try {
      const range = document.createRange();
      range.setStart(textNode, offset);
      range.setEnd(textNode, Math.min(offset + 1, textNode.textContent.length));
      
      const rect = range.getBoundingClientRect();
      
      // Skip zero-size rectangles (like at end of text)
      if (rect.width === 0 && rect.height === 0) {
        return false;
      }
      
      // Convert viewport coordinates to document coordinates
      const scrollX = window.pageXOffset || document.documentElement.scrollLeft;
      const scrollY = window.pageYOffset || document.documentElement.scrollTop;
      
      const charLeft = rect.left + scrollX;
      const charTop = rect.top + scrollY;
      const charRight = charLeft + rect.width;
      const charBottom = charTop + rect.height;
      
      // Check if character center is within rectangle bounds
      const charCenterX = charLeft + rect.width / 2;
      const charCenterY = charTop + rect.height / 2;
      
      return charCenterX >= rectBounds.left && 
             charCenterX <= rectBounds.right && 
             charCenterY >= rectBounds.top && 
             charCenterY <= rectBounds.bottom;
    } catch (error) {
      console.warn('[KeyPilot] Error checking character in rectangle:', error);
      return false;
    }
  }

  /**
   * Read selected text without tearing down session state.
   * Use before async clipboard so highlight mode can exit cleanly first.
   * @returns {string}
   */
  peekCharacterSelectedText() {
    try {
      const selection = window.getSelection();
      return selection ? (selection.toString() || '') : '';
    } catch {
      return '';
    }
  }

  /**
   * Complete character selection and return the selected text
   * @returns {string|null} - Selected text or null if no selection
   */
  completeCharacterSelection() {
    try {
      const selectedText = this.peekCharacterSelectedText();
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Character selection completed:', {
          selectedText: selectedText.substring(0, 100),
          length: selectedText.length,
          wasActive: this.characterSelectionActive
        });
      }

      return selectedText;
    } catch (error) {
      console.error('[KeyPilot] Error completing character selection:', error);
      return null;
    } finally {
      // Full teardown so the next H session starts clean
      this.clearCharacterSelection();
    }
  }

  /**
   * Clear the current character selection without completing it
   */
  clearCharacterSelection() {
    try {
      const selection = window.getSelection();
      if (selection) {
        selection.removeAllRanges();
      }
      this.clearHighlightSelectionOverlays();
      // Hide dashed guide without going through hide→reset→resetCharacter loops twice
      if (this.highlightRectangleOverlay) {
        this.highlightRectangleOverlay.style.display = 'none';
      }
      this.hideDebugHUD?.();
    } catch (error) {
      console.warn('[KeyPilot] Error clearing character selection:', error);
    } finally {
      // Always reset flags — previously leave-active state caused bad re-entry
      this.resetCharacterSelection();
    }
  }

  /**
   * Reset character selection state
   */
  resetCharacterSelection() {
    this.characterSelectionActive = false;
    this.characterStartPosition = null;
    this.characterStartTextNode = null;
    this.characterStartOffset = 0;

    // Also reset rectangle state since character selection uses rectangle overlay
    this.resetRectangleSelection();

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Character selection state reset');
    }
  }

  /**
   * Compare the document positions of two text nodes
   * @param {Text} node1 - First text node
   * @param {Text} node2 - Second text node
   * @returns {number} - Negative if node1 comes before node2, positive if after, 0 if same
   */
  compareTextNodePositions(node1, node2) {
    if (node1 === node2) {
      return 0;
    }

    try {
      const comparison = node1.compareDocumentPosition(node2);
      
      if (comparison & Node.DOCUMENT_POSITION_FOLLOWING) {
        return -1; // node1 comes before node2
      } else if (comparison & Node.DOCUMENT_POSITION_PRECEDING) {
        return 1; // node1 comes after node2
      } else {
        return 0; // Same position (shouldn't happen)
      }
    } catch (error) {
      console.warn('[KeyPilot] Error comparing text node positions:', error);
      return 0;
    }
  }

  /**
   * Get selection object for the given document context
   * @param {Document} ownerDocument - Document context
   * @returns {Selection|null} - Selection object or null
   */
  getSelectionForDocument(ownerDocument) {
    try {
      if (ownerDocument && ownerDocument.getSelection) {
        return ownerDocument.getSelection();
      }
      return window.getSelection();
    } catch (error) {
      console.warn('[KeyPilot] Error getting selection for document:', error);
      return window.getSelection();
    }
  }

  /**
   * Reset rectangle selection state
   */
  resetRectangleSelection() {
    this.rectOriginPoint = null;
    this.rectOriginDocumentPoint = null;
    this.rectangleStartCaret = null;
    this.debugUpdateCount = 0;

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Rectangle selection state reset');
    }

    // Hide debug HUD when selection ends
    this.hideDebugHUD();
  }

  /**
   * Toggle debug HUD feature flag
   */
  toggleDebugHUD(enabled) {
    // Temporarily override the feature flag
    FEATURE_FLAGS.DEBUG_RECTANGLE_HUD = enabled;

    if (!enabled) {
      this.hideDebugHUD();
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Debug HUD toggled:', enabled);
    }
  }



  /**
   * Create or show the debug HUD for rectangle selection
   */
  showDebugHUD() {
    // Enable debug HUD if KEYPILOT_DEBUG is true or feature flag is enabled
    if (!FEATURE_FLAGS.DEBUG_RECTANGLE_HUD && !window.KEYPILOT_DEBUG) {
      return;
    }

    if (this.debugHUD) {
      this.debugHUD.style.display = 'block';
      return;
    }

    this.debugHUD = this.createElement('div', {
      className: 'kpv2-rectangle-debug-hud',
      style: `
        position: fixed;
        top: 10px;
        left: 10px;
        background: rgba(0, 0, 0, 0.9);
        color: #00ff00;
        font-family: 'Courier New', monospace;
        font-size: 12px;
        padding: 15px;
        border-radius: 8px;
        z-index: ${Z_INDEX.DEBUG_HUD};
        pointer-events: none;
        white-space: pre-line;
        min-width: 400px;
        max-width: 500px;
        border: 2px solid #00ff00;
        box-shadow: 0 0 10px rgba(0, 255, 0, 0.3);
      `
    });

    document.body.appendChild(this.debugHUD);

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Debug HUD created and shown');
    }
  }

  /**
   * Hide the debug HUD
   */
  hideDebugHUD() {
    if (this.debugHUD) {
      this.debugHUD.style.display = 'none';

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Debug HUD hidden');
      }
    }
  }

  /**
   * Update the debug HUD with current rectangle information
   */
  updateDebugHUD(rectOriginPoint, currentPosition, rectOriginDocumentPoint, currentDocumentPosition, calculatedValues) {
    if ((!FEATURE_FLAGS.DEBUG_RECTANGLE_HUD && !window.KEYPILOT_DEBUG) || !this.debugHUD) {
      return;
    }

    this.debugUpdateCount++;

    const timestamp = new Date().toLocaleTimeString();
    const pageInfo = {
      scrollX: window.scrollX,
      scrollY: window.scrollY,
      innerWidth: window.innerWidth,
      innerHeight: window.innerHeight,
      documentWidth: document.documentElement.scrollWidth,
      documentHeight: document.documentElement.scrollHeight
    };

    const debugInfo = `🎯 RECTANGLE DEBUG HUD - Update #${this.debugUpdateCount}
⏰ Time: ${timestamp}

📍 VIEWPORT COORDINATES:
  Origin: (${rectOriginPoint?.x || 'null'}, ${rectOriginPoint?.y || 'null'})
  Current: (${currentPosition?.x || 'null'}, ${currentPosition?.y || 'null'})
  Delta: (${currentPosition && rectOriginPoint ? currentPosition.x - rectOriginPoint.x : 'null'}, ${currentPosition && rectOriginPoint ? currentPosition.y - rectOriginPoint.y : 'null'})

📄 DOCUMENT COORDINATES:
  Origin: (${rectOriginDocumentPoint?.x || 'null'}, ${rectOriginDocumentPoint?.y || 'null'})
  Current: (${currentDocumentPosition?.x || 'null'}, ${currentDocumentPosition?.y || 'null'})
  Delta: (${currentDocumentPosition && rectOriginDocumentPoint ? currentDocumentPosition.x - rectOriginDocumentPoint.x : 'null'}, ${currentDocumentPosition && rectOriginDocumentPoint ? currentDocumentPosition.y - rectOriginDocumentPoint.y : 'null'})

📏 CALCULATED RECTANGLE:
  Document Left: ${calculatedValues?.documentLeft || 'null'}
  Document Top: ${calculatedValues?.documentTop || 'null'}
  Width: ${calculatedValues?.width || 'null'}
  Height: ${calculatedValues?.height || 'null'}
  Viewport Left: ${calculatedValues?.viewportLeft || 'null'}
  Viewport Top: ${calculatedValues?.viewportTop || 'null'}
  Quadrant: ${calculatedValues?.quadrant || 'null'}

🌐 PAGE CONTEXT:
  Scroll: (${pageInfo.scrollX}, ${pageInfo.scrollY})
  Viewport: ${pageInfo.innerWidth} × ${pageInfo.innerHeight}
  Document: ${pageInfo.documentWidth} × ${pageInfo.documentHeight}

📐 RECTANGLE INFO:
  Area: ${(calculatedValues?.width * calculatedValues?.height) || 0}px²
  Min Width: ${RECTANGLE_SELECTION.MIN_WIDTH}px
  Min Height: ${RECTANGLE_SELECTION.MIN_HEIGHT}px
  Min Drag: ${RECTANGLE_SELECTION.MIN_DRAG_DISTANCE}px

🔧 OVERLAY CALLS:
  Position: left=${calculatedValues?.viewportLeft}px, top=${calculatedValues?.viewportTop}px
  Size: width=${calculatedValues?.width}px, height=${calculatedValues?.height}px
  Drag Distance: ${Math.sqrt((currentDocumentPosition?.x - rectOriginDocumentPoint?.x) ** 2 + (currentDocumentPosition?.y - rectOriginDocumentPoint?.y) ** 2).toFixed(1)}px
  Should Show: ${this.shouldShowRectangle(calculatedValues?.width, calculatedValues?.height, currentDocumentPosition?.x - rectOriginDocumentPoint?.x, currentDocumentPosition?.y - rectOriginDocumentPoint?.y)}
  Display: ${this.shouldShowRectangle(calculatedValues?.width, calculatedValues?.height, currentDocumentPosition?.x - rectOriginDocumentPoint?.x, currentDocumentPosition?.y - rectOriginDocumentPoint?.y) ? 'block' : 'none'}
  Visibility: ${this.shouldShowRectangle(calculatedValues?.width, calculatedValues?.height, currentDocumentPosition?.x - rectOriginDocumentPoint?.x, currentDocumentPosition?.y - rectOriginDocumentPoint?.y) ? 'visible' : 'hidden'}`;

    this.debugHUD.textContent = debugInfo;
  }

  /**
   * Update selection overlays to highlight the actual selected text regions with shadow DOM support
   * @param {Selection} selection - Browser Selection object
   */
  updateHighlightSelectionOverlays(selection) {
    // Clear existing selection overlays
    this.clearHighlightSelectionOverlays();

    if (!selection || selection.rangeCount === 0) {
      return;
    }

    try {
      // Cap overlays so huge selections cannot create thousands of DOM nodes per frame
      // (main-thread freeze). Browser native selection paint still shows the text.
      const MAX_SELECTION_OVERLAY_RECTS = 80;
      let created = 0;

      for (let i = 0; i < selection.rangeCount; i++) {
        const range = selection.getRangeAt(i);
        created += this.createSelectionOverlaysForRangeWithShadowSupport(range, MAX_SELECTION_OVERLAY_RECTS - created);
        if (created >= MAX_SELECTION_OVERLAY_RECTS) break;
      }

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Updated highlight selection overlays with shadow DOM support:', {
          rangeCount: selection.rangeCount,
          overlayCount: this.highlightSelectionOverlays.length,
          selectedText: selection.toString().substring(0, 50)
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error updating highlight selection overlays with shadow DOM support:', error);
      this.clearHighlightSelectionOverlays();
    }
  }

  /**
   * Create selection overlays for a specific range with shadow DOM support
   * @param {Range} range - DOM Range object
   * @param {number} [maxRects=80] - Max client rects to materialize as overlay nodes
   * @returns {number} - Number of overlays created
   */
  createSelectionOverlaysForRangeWithShadowSupport(range, maxRects = 80) {
    if (!range || range.collapsed) {
      return 0;
    }

    let created = 0;
    try {
      // Get all rectangles for the range (handles multi-line selections)
      const rects = this.getClientRectsWithShadowSupport(range);
      const limit = Math.max(0, maxRects);

      for (let i = 0; i < rects.length && created < limit; i++) {
        const rect = rects[i];

        // Skip zero-width or zero-height rectangles
        if (rect.width <= 0 || rect.height <= 0) {
          continue;
        }

        // Create overlay element for this rectangle
        const overlay = this.createElement('div', {
          className: 'kpv2-highlight-selection-overlay',
          style: `
            position: fixed;
            left: ${rect.left}px;
            top: ${rect.top}px;
            width: ${rect.width}px;
            height: ${rect.height}px;
            background: ${COLORS.HIGHLIGHT_SELECTION_BG};
            border: 1px solid ${COLORS.HIGHLIGHT_BLUE};
            pointer-events: none;
            z-index: ${Z_INDEX.OVERLAYS_BELOW};
            will-change: transform;
            opacity: 0.7;
          `
        });

        // Append to main document body (overlays should always be in main document)
        document.body.appendChild(overlay);
        this.highlightSelectionOverlays.push(overlay);
        created++;

        // Start observing the overlay for visibility optimization
        if (this.overlayObserver) {
          this.overlayObserver.observe(overlay);
        }
      }

      if (window.KEYPILOT_DEBUG && rects.length > 0) {
        console.log('[KeyPilot Debug] Created selection overlays for range with shadow DOM support:', {
          rectCount: rects.length,
          created,
          firstRect: {
            left: rects[0].left,
            top: rects[0].top,
            width: rects[0].width,
            height: rects[0].height
          }
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error creating selection overlays for range with shadow DOM support:', error);
    }
    return created;
  }

  /**
   * Get client rectangles for a range with shadow DOM support
   * @param {Range} range - DOM Range object
   * @returns {DOMRectList|Array} - Client rectangles
   */
  getClientRectsWithShadowSupport(range) {
    try {
      // First try the standard method
      const rects = range.getClientRects();
      if (rects && rects.length > 0) {
        return rects;
      }

      // If no rectangles found, try alternative methods for shadow DOM
      return this.getAlternativeClientRects(range);
    } catch (error) {
      console.warn('[KeyPilot] Error getting client rects with shadow DOM support:', error);
      return [];
    }
  }

  /**
   * Get alternative client rectangles for shadow DOM ranges
   * @param {Range} range - DOM Range object
   * @returns {Array} - Array of rectangle objects
   */
  getAlternativeClientRects(range) {
    try {
      const rects = [];

      // Try to get bounding rect as fallback
      const boundingRect = range.getBoundingClientRect();
      if (boundingRect && boundingRect.width > 0 && boundingRect.height > 0) {
        rects.push(boundingRect);
      }

      // For shadow DOM, we might need to manually calculate rectangles
      // by walking through the range contents
      if (rects.length === 0) {
        const shadowRects = this.calculateShadowDOMRects(range);
        rects.push(...shadowRects);
      }

      return rects;
    } catch (error) {
      console.warn('[KeyPilot] Error getting alternative client rects:', error);
      return [];
    }
  }

  /**
   * Calculate rectangles for shadow DOM ranges manually
   * @param {Range} range - DOM Range object
   * @returns {Array} - Array of rectangle objects
   */
  calculateShadowDOMRects(range) {
    try {
      const rects = [];

      // Get start and end containers
      const startContainer = range.startContainer;
      const endContainer = range.endContainer;

      if (startContainer === endContainer && startContainer.nodeType === Node.TEXT_NODE) {
        // Single text node selection
        const textRect = this.getTextNodeRect(startContainer, range.startOffset, range.endOffset);
        if (textRect) {
          rects.push(textRect);
        }
      } else {
        // Multi-node selection - this is more complex for shadow DOM
        // For now, use bounding rect as approximation
        try {
          const boundingRect = range.getBoundingClientRect();
          if (boundingRect && boundingRect.width > 0 && boundingRect.height > 0) {
            rects.push(boundingRect);
          }
        } catch (error) {
          // Ignore errors in complex shadow DOM scenarios
        }
      }

      return rects;
    } catch (error) {
      console.warn('[KeyPilot] Error calculating shadow DOM rects:', error);
      return [];
    }
  }

  /**
   * Get rectangle for a portion of a text node
   * @param {Text} textNode - Text node
   * @param {number} startOffset - Start character offset
   * @param {number} endOffset - End character offset
   * @returns {DOMRect|null} - Rectangle or null
   */
  getTextNodeRect(textNode, startOffset, endOffset) {
    try {
      const ownerDocument = textNode.ownerDocument || document;
      const tempRange = ownerDocument.createRange();
      tempRange.setStart(textNode, startOffset);
      tempRange.setEnd(textNode, endOffset);

      const rect = tempRange.getBoundingClientRect();
      return rect.width > 0 && rect.height > 0 ? rect : null;
    } catch (error) {
      console.warn('[KeyPilot] Error getting text node rect:', error);
      return null;
    }
  }

  /**
   * Create highlight selection overlay for a range (legacy method for compatibility)
   */
  createHighlightSelectionOverlay(range) {
    // Delegate to the shadow DOM-aware method
    this.createSelectionOverlaysForRangeWithShadowSupport(range);
  }

  /**
   * Clear all highlight selection overlays
   */
  clearHighlightSelectionOverlays() {
    this.highlightSelectionOverlays.forEach(overlay => {
      if (this.overlayObserver) {
        this.overlayObserver.unobserve(overlay);
      }
      overlay.remove();
    });
    this.highlightSelectionOverlays = [];

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Cleared highlight selection overlays');
    }
  }

  /**
   * Show companion instruction overlay while selection is active.
   * @param {{ finishKey?: string }} [opts] - physical key to press again (e.g. "H" or "Y")
   */
  showHighlightModeIndicator(opts = {}) {
    const finishKeyRaw = opts.finishKey || (this.selectionMode === 'character' ? 'H' : 'Y');
    const finishKey = String(finishKeyRaw).toUpperCase();
    const modeText = `Press ${finishKey} again to finish selection`;

    if (this.highlightModeIndicator) {
      this.highlightModeIndicator.textContent = modeText;
      this.highlightModeIndicator.style.display = 'block';
      return;
    }

    this.highlightModeIndicator = this.createElement('div', {
      className: 'kpv2-highlight-mode-indicator',
      style: `
        position: fixed;
        top: 20px;
        right: 20px;
        background: ${COLORS.HIGHLIGHT_BLUE};
        color: white;
        padding: 10px 14px;
        font-size: 14px;
        font-weight: bold;
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        border-radius: 6px;
        box-shadow: 0 2px 10px ${COLORS.HIGHLIGHT_SHADOW};
        z-index: ${Z_INDEX.MESSAGE_BOX};
        pointer-events: none;
        will-change: transform, opacity;
        animation: kpv2-pulse 1.5s ease-in-out infinite;
        letter-spacing: 0.01em;
      `
    });

    this.highlightModeIndicator.textContent = modeText;
    document.body.appendChild(this.highlightModeIndicator);

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Highlight mode indicator shown:', modeText);
    }
  }

  /**
   * Hide highlight mode indicator
   */
  hideHighlightModeIndicator() {
    if (this.highlightModeIndicator) {
      this.highlightModeIndicator.remove();
      this.highlightModeIndicator = null;

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Highlight mode indicator hidden');
      }
    }
  }

  /**
   * Set overlay visibility
   */
  setOverlayVisibility(overlayType, isVisible) {
    if (overlayType === 'highlight' && this.highlightOverlay) {
      this.overlayVisibility.highlight = isVisible;
      this.highlightOverlay.style.visibility = isVisible ? 'visible' : 'hidden';
    } else if (overlayType === 'highlightRectangle' && this.highlightRectangleOverlay) {
      this.overlayVisibility.highlightRectangle = isVisible;
      this.highlightRectangleOverlay.style.visibility = isVisible ? 'visible' : 'hidden';
    }
  }

  /**
   * Clean up all highlight overlays and resources
   */
  cleanup() {
    // Clean up highlight overlays
    if (this.highlightOverlay) {
      this.highlightOverlay.remove();
      this.highlightOverlay = null;
    }
    if (this.highlightRectangleOverlay) {
      this.highlightRectangleOverlay.remove();
      this.highlightRectangleOverlay = null;
    }

    // Clear highlight selection overlays
    this.clearHighlightSelectionOverlays();

    if (this.highlightModeIndicator) {
      this.highlightModeIndicator.remove();
      this.highlightModeIndicator = null;
    }

    // Clean up debug HUD
    if (this.debugHUD) {
      this.debugHUD.remove();
      this.debugHUD = null;
    }

    // Reset selection states
    this.resetCharacterSelection();
    this.resetRectangleSelection();

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] HighlightManager cleanup completed');
    }
  }
}


  // Module: src/modules/popup-manager.js


/**
 * PopupManager
 * Centralizes popup stacking + shared blurred backdrop so we don't rely on the
 * browser Popover API "top layer" (which can block our cursor/green-click overlay).
 *
 * All modal panels are resizable by default (edge + corner handles + SE grip).
 */
class PopupManager {
  /**
   * @param {object} [opts]
   * @param {Document} [opts.doc]
   * @param {(type: string, data: any) => void} [opts.onPanelChange] Optional callback for panel lifecycle events
   */
  constructor({ doc, onPanelChange } = {}) {
    this.doc = doc || document;

    /** @type {HTMLDivElement|null} */
    this._backdrop = null;
    /** @type {Array<{id: string, panel: HTMLElement, onRequestClose?: () => void, resizeDispose?: () => void}>} */
    this._stack = [];

    /** @type {(type: string, data: any) => void|null} */
    this._onPanelChange = typeof onPanelChange === 'function' ? onPanelChange : null;

    this._backdropClickHandler = this._backdropClickHandler.bind(this);
  }

  isOpen() {
    return this._stack.length > 0;
  }

  top() {
    return this._stack.length ? this._stack[this._stack.length - 1] : null;
  }

  /**
   * Request closing the topmost popup.
   * Prefers the popup's onRequestClose hook (so callers can synchronize app state),
   * and falls back to removing the modal directly.
   */
  requestCloseTop() {
    const top = this.top();
    if (!top) return;
    try {
      if (typeof top.onRequestClose === 'function') {
        top.onRequestClose();
        return;
      }
    } catch {
      // ignore and fall back
    }
    this.hideModal(top.id);
  }

  /**
   * Close every modal in the stack (launcher, settings, guide, tab history, etc.).
   * Prefers each panel's onRequestClose so app state stays in sync; force-removes
   * anything that remains.
   */
  closeAll() {
    let guard = 32;
    while (this._stack.length && guard-- > 0) {
      const top = this.top();
      if (!top) break;
      const id = top.id;
      try {
        if (typeof top.onRequestClose === 'function') {
          top.onRequestClose();
        }
      } catch {
        // ignore
      }
      // If the close handler didn't remove this entry, force-unmount it.
      if (this._stack.some((p) => p.id === id)) {
        this.hideModal(id);
      }
    }
    // Hard reset if anything is stuck.
    if (this._stack.length) {
      const leftover = this._stack.splice(0, this._stack.length);
      for (const entry of leftover) {
        try { entry?.resizeDispose?.(); } catch { /* ignore */ }
        try { entry?.panel?.remove?.(); } catch { /* ignore */ }
      }
    }
    if (this._backdrop) {
      try { this._backdrop.remove(); } catch { /* ignore */ }
      this._backdrop = null;
      if (this._onPanelChange) {
        try { this._onPanelChange('backdrop-hidden', {}); } catch { /* ignore */ }
      }
    }
  }

  /**
   * Show a modal popup panel with a shared blurred backdrop.
   * The panel is assigned a z-index *below* the click rectangle overlays.
   * Panels are resizable by default (edges + corners + SE grip icon).
   *
   * @param {object} params
   * @param {string} params.id
   * @param {HTMLElement} params.panel
   * @param {() => void} [params.onRequestClose]
   * @param {boolean} [params.resizable=true] Attach resize handles (default true)
   * @param {{ minWidth?: number, minHeight?: number, margin?: number }} [params.resizeOptions]
   */
  showModal({ id, panel, onRequestClose, resizable = true, resizeOptions } = {}) {
    if (!id || !panel) return;

    // If already open, bring to front and update close handler.
    const existingIdx = this._stack.findIndex((p) => p.id === id);
    if (existingIdx >= 0) {
      const existing = this._stack[existingIdx];
      existing.panel = panel;
      existing.onRequestClose = typeof onRequestClose === 'function' ? onRequestClose : existing.onRequestClose;
      this._stack.splice(existingIdx, 1);
      this._stack.push(existing);
      this._ensureMounted();
      this._recomputeZ();
      this._ensureResizable(existing, resizable, resizeOptions);
      return;
    }

    /** @type {{id: string, panel: HTMLElement, onRequestClose?: () => void, resizeDispose?: () => void}} */
    const entry = {
      id: String(id),
      panel,
      onRequestClose: typeof onRequestClose === 'function' ? onRequestClose : undefined
    };
    this._stack.push(entry);

    this._ensureMounted();
    this._recomputeZ();
    this._ensureResizable(entry, resizable, resizeOptions);

    // Notify about panel shown
    if (this._onPanelChange) {
      try {
        this._onPanelChange('panel-shown', { id: String(id), panel });
      } catch { /* ignore */ }
    }
  }

  /**
   * @param {{ panel: HTMLElement, resizeDispose?: () => void }} entry
   * @param {boolean} resizable
   * @param {object} [resizeOptions]
   */
  _ensureResizable(entry, resizable, resizeOptions) {
    if (!entry?.panel) return;
    if (resizable === false) {
      try { entry.resizeDispose?.(); } catch { /* ignore */ }
      entry.resizeDispose = undefined;
      return;
    }
    // Already attached for this panel instance.
    if (typeof entry.resizeDispose === 'function') return;
    try {
      const api = makePopoverResizable(entry.panel, resizeOptions || {});
      if (api && typeof api.dispose === 'function') {
        entry.resizeDispose = () => {
          try { api.dispose(); } catch { /* ignore */ }
        };
      }
    } catch (e) {
      console.warn('[KeyPilot] Failed to attach popover resize handles:', e?.message || e);
    }
  }

  /**
   * Hide a popup by id (or the top popup if id is omitted).
   * @param {string} [id]
   */
  hideModal(id) {
    const targetId = typeof id === 'string' && id ? id : (this.top()?.id || null);
    if (!targetId) return;

    const idx = this._stack.findIndex((p) => p.id === targetId);
    if (idx < 0) return;

    const removed = this._stack.splice(idx, 1)[0];

    // Tear down resize handles before unmount.
    try { removed?.resizeDispose?.(); } catch { /* ignore */ }

    // Notify about panel hidden
    if (this._onPanelChange) {
      try {
        this._onPanelChange('panel-hidden', { id: targetId, panel: removed?.panel });
      } catch { /* ignore */ }
    }

    // Synchronous unmount — View Transitions here produced a blurry cross-fade
    // snapshot of the panel that lingered then snapped away.
    try { removed?.panel?.style && (removed.panel.style.viewTransitionName = 'none'); } catch { /* ignore */ }
    try { removed?.panel?.remove?.(); } catch { /* ignore */ }

    if (!this._stack.length) {
      try {
        if (this._backdrop) this._backdrop.style.viewTransitionName = 'none';
      } catch { /* ignore */ }
      try { this._backdrop?.remove?.(); } catch { /* ignore */ }
      this._backdrop = null;

      if (this._onPanelChange) {
        try {
          this._onPanelChange('backdrop-hidden', {});
        } catch { /* ignore */ }
      }
    }

    this._recomputeZ();
  }

  _ensureMounted() {
    const doc = this.doc;
    if (!doc || !doc.body) return;

    if (!this._backdrop) {
      const el = doc.createElement('div');
      el.className = CSS_CLASSES.POPUP_BACKDROP || 'kpv2-popup-backdrop';
      Object.assign(el.style, {
        position: 'fixed',
        inset: '0',
        background: 'rgba(0,0,0,0.35)',
        // Blur only the page behind the dimmer — never the panel (sibling above).
        backdropFilter: 'blur(6px)',
        WebkitBackdropFilter: 'blur(6px)',
        outline: 'none',
        // zIndex assigned in _recomputeZ() before first paint when possible
        pointerEvents: 'auto',
        viewTransitionName: 'none'
      });
      el.addEventListener('click', this._backdropClickHandler, true);
      this._backdrop = el;
    }

    // Assign z-index before append so the first paint has correct stacking
    // (panel above backdrop — avoids a frame where the panel is blurred by
    // the backdrop-filter).
    this._recomputeZ();

    // Mount backdrop + panels in one synchronous batch. Separate
    // startViewTransition() calls used to cross-fade blurry snapshots of the
    // launcher on open.
    const backdropJustMounted = !!(this._backdrop && !this._backdrop.isConnected);
    if (backdropJustMounted) {
      try { doc.body.appendChild(this._backdrop); } catch { /* ignore */ }
      if (this._onPanelChange) {
        try {
          this._onPanelChange('backdrop-shown', { backdrop: this._backdrop });
        } catch { /* ignore */ }
      }
    }

    for (const entry of this._stack) {
      const panel = entry.panel;
      if (panel && !panel.isConnected) {
        try { panel.style.viewTransitionName = 'none'; } catch { /* ignore */ }
        try { doc.body.appendChild(panel); } catch { /* ignore */ }
      }
    }
  }

  _recomputeZ() {
    // Backdrop below panels; panels in a bounded band below overlays.
    if (this._backdrop) {
      this._backdrop.style.zIndex = String(Z_INDEX.POPUP_BACKDROP ?? Z_INDEX.VIEWPORT_MODAL_FRAME);
      // Do not participate in View Transitions (blurry snapshot morphs).
      this._backdrop.style.viewTransitionName = 'none';

      // Notify about backdrop z-index update (for negative region tracking)
      if (this._onPanelChange && this._stack.length > 0) {
        try {
          this._onPanelChange('backdrop-updated', { backdrop: this._backdrop });
        } catch { /* ignore */ }
      }
    }

    const base = Z_INDEX.POPUP_PANEL_BASE ?? (Z_INDEX.VIEWPORT_MODAL_FRAME + 2);
    const max = Z_INDEX.POPUP_PANEL_MAX ?? (Z_INDEX.OVERLAYS_BELOW_2 - 1);

    for (let i = 0; i < this._stack.length; i++) {
      const entry = this._stack[i];
      const panel = entry.panel;
      if (!panel) continue;

      const z = Math.min(base + i, max);
      panel.style.zIndex = String(z);
      panel.style.viewTransitionName = 'none';

      // Notify about panel z-index update (for negative region tracking)
      if (this._onPanelChange) {
        try {
          this._onPanelChange('panel-updated', { id: entry.id, panel });
        } catch { /* ignore */ }
      }
    }
  }

  _backdropClickHandler(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
      const top = this.top();
      if (top?.onRequestClose) top.onRequestClose();
      else this.hideModal();
    } catch {
      // ignore
    }
  }
}





  // Module: src/modules/inspector-mode.js
/**
 * Shared inspector (element-pick) mode for KeyPilot.
 *
 * Delete Mode, Cols Toggle, and future tools share one lifecycle:
 *   1. Enter inspector with a kind → special cursor + hover outline on any element
 *   2. Move pointer to choose a target (inspectorEl)
 *   3. Confirm (same key again) → kind-specific action on target
 *   4. Esc → exit pick mode only (sticky side-effects like columns stay)
 *
 * Register new tools by adding an entry to INSPECTOR_DEFS + INSPECTOR_KIND.
 */

/**
 * @typedef {{
 *   kind: string,
 *   label: string,
 *   statusMode: string,
 *   cursorMode: string,
 *   hostClass: string,
 *   borderColor: string,
 *   shadowColor: string,
 *   shadowBrightColor: string,
 *   allowHtmlBody?: boolean,
 *   actionId?: string,
 *   instructionTemplate?: string,
 * }} InspectorDef
 */

/**
 * Visual + metadata registry per inspector kind.
 * Action handlers stay in KeyPilot (or feature modules); this file owns pick chrome.
 *
 * @type {Readonly<Record<string, InspectorDef>>}
 */
const INSPECTOR_DEFS = Object.freeze({
  [INSPECTOR_KIND.DELETE]: Object.freeze({
    kind: INSPECTOR_KIND.DELETE,
    label: 'Delete',
    statusMode: 'delete',
    cursorMode: 'delete',
    hostClass: CSS_CLASSES.DELETE,
    borderColor: COLORS.DELETE_RED,
    shadowColor: COLORS.DELETE_SHADOW,
    shadowBrightColor: COLORS.DELETE_SHADOW_BRIGHT,
    allowHtmlBody: false,
    actionId: 'DELETE',
    instructionTemplate: 'Press {key} again to delete · Esc cancels'
  }),
  [INSPECTOR_KIND.COLS]: Object.freeze({
    kind: INSPECTOR_KIND.COLS,
    label: 'Cols',
    statusMode: 'cols',
    cursorMode: 'cols',
    hostClass: CSS_CLASSES.COLS,
    borderColor: COLORS.COLS_PURPLE,
    shadowColor: COLORS.COLS_SHADOW,
    shadowBrightColor: COLORS.COLS_SHADOW_BRIGHT,
    allowHtmlBody: true,
    actionId: 'COLS_TOGGLE',
    instructionTemplate: 'Hover an element · Press {key} again to columnize · Esc cancels'
  })
});

/**
 * Build the top-right instruction string for an inspector kind.
 * @param {string|null|undefined} kind
 * @param {string} [confirmKey]
 * @returns {string}
 */
function getInspectorInstructionText(kind, confirmKey) {
  const def = getInspectorDef(kind);
  const key = String(confirmKey || (kind === INSPECTOR_KIND.COLS ? '.' : 'Backspace') || '').trim() || '?';
  const template = def?.instructionTemplate || 'Press {key} again · Esc cancels';
  return template.replace(/\{key\}/g, key);
}

/**
 * @param {string|null|undefined} kind
 * @returns {InspectorDef|null}
 */
function getInspectorDef(kind) {
  if (!kind) return null;
  return INSPECTOR_DEFS[kind] || null;
}

/**
 * Cursor mode string for CursorManager (kind-specific glyph).
 * @param {string|null|undefined} kind
 * @returns {string}
 */
function getInspectorCursorMode(kind) {
  return getInspectorDef(kind)?.cursorMode || 'inspector';
}

/**
 * Status string for popup / control strip (legacy delete/cols labels).
 * @param {string|null|undefined} kind
 * @returns {string}
 */
function getInspectorStatusMode(kind) {
  return getInspectorDef(kind)?.statusMode || 'inspector';
}

/**
 * All host CSS classes that may be painted on a hovered inspector target.
 * Used when clearing previous paint after kind switches.
 * @returns {string[]}
 */
function getAllInspectorHostClasses() {
  const set = new Set();
  for (const def of Object.values(INSPECTOR_DEFS)) {
    if (def.hostClass) set.add(def.hostClass);
  }
  set.add(CSS_CLASSES.INSPECTOR);
  return [...set];
}

/**
 * Coordinates inspector enter / exit / hover / resolve-target.
 * Does not own confirm actions — callers run kind-specific effects.
 */
class InspectorModeController {
  /**
   * @param {{
   *   state: import('./state-manager.js').StateManager,
   *   deepElementFromPoint: (x: number, y: number) => Element|null,
   *   onBeforeEnter?: (kind: string) => void,
   * }} deps
   */
  constructor(deps) {
    this.state = deps.state;
    this.deepElementFromPoint = deps.deepElementFromPoint;
    this.onBeforeEnter = typeof deps.onBeforeEnter === 'function' ? deps.onBeforeEnter : null;
  }

  isActive() {
    return this.state.isInspectorMode?.() === true
      || (typeof this.state.getState === 'function' && this.state.getState().mode === 'inspector');
  }

  /**
   * @param {string} kind
   * @returns {boolean}
   */
  isKind(kind) {
    if (!this.isActive()) return false;
    return this.state.getState().inspectorKind === kind;
  }

  getKind() {
    if (!this.isActive()) return null;
    return this.state.getState().inspectorKind || null;
  }

  getTarget() {
    return this.state.getState().inspectorEl || null;
  }

  getDef() {
    return getInspectorDef(this.getKind());
  }

  /**
   * Enter (or switch to) an inspector kind. Replaces any other inspector kind.
   * @param {string} kind
   */
  enter(kind) {
    if (!getInspectorDef(kind)) {
      console.warn('[KeyPilot] Unknown inspector kind:', kind);
      return;
    }
    try {
      this.onBeforeEnter?.(kind);
    } catch (e) {
      console.warn('[KeyPilot] inspector onBeforeEnter failed:', e);
    }
    this.state.enterInspector(kind);
  }

  /**
   * Exit pick mode; clear hover target. Does not reverse sticky effects (e.g. columns).
   */
  exit() {
    this.state.exitInspector();
  }

  /**
   * Hover tracking while inspector is active.
   * @param {Element|null|undefined} under
   */
  updateHover(under) {
    const st = this.state.getState();
    if (!this.isActive()) {
      if (st.inspectorEl) this.state.setInspectorElement(null);
      return;
    }
    const next = under && under.nodeType === 1 ? under : null;
    if (next !== st.inspectorEl) {
      this.state.setInspectorElement(next);
    }
  }

  /**
   * Element under cursor for confirm: state hover, else deep hit-test.
   * @returns {Element|null}
   */
  resolveTarget() {
    const st = this.state.getState();
    if (st.inspectorEl && st.inspectorEl.isConnected) {
      return st.inspectorEl;
    }
    try {
      const { x, y } = st.lastMouse || { x: 0, y: 0 };
      return this.deepElementFromPoint?.(x, y) || null;
    } catch {
      return null;
    }
  }

  /**
   * Resolve target and exit pick mode (caller applies the action).
   * @returns {Element|null}
   */
  confirmAndExit() {
    const target = this.resolveTarget();
    this.exit();
    return target;
  }
}



  // Module: src/modules/overlay-manager.js
/**
 * Visual overlay management for focus and delete indicators
 */











/** Per-host Link Preview viewport mode: { [hostname]: 'mobile' }. Missing/default = desktop. */
const PREVIEW_VIEWPORT_BY_HOST_KEY = 'kp_link_preview_viewport_by_host';

/**
 * @param {string} url
 * @returns {string} normalized hostname (lowercase, no leading www.)
 */
function previewHostFromUrl(url) {
  try {
    let host = new URL(String(url || '')).hostname.toLowerCase();
    if (host.startsWith('www.')) host = host.slice(4);
    return host;
  } catch {
    return '';
  }
}

/**
 * Link Preview iframes generally require HTTPS. Prefer https when the URL is plain http.
 * @param {string} url
 * @returns {string}
 */
function preferHttpsForPreview(url) {
  const s = String(url || '').trim();
  if (!s) return s;
  if (/^http:\/\//i.test(s)) {
    return `https://${s.slice('http://'.length)}`;
  }
  return s;
}

class OverlayManager {
  constructor() {
    // Rendering mode configuration: 'dom' | 'canvas' | 'css-custom-props'
    this.renderingMode = 'canvas'; // Default to current DOM-based rendering

    // Canvas rendering backend
    this.canvasOverlay = null;
    this.canvasContext = null;

    // CSS Custom Properties rendering backend
    this.cssCustomPropsOverlay = null;

    this.focusOverlay = null;
    /** Shared inspector pick outline (Delete, Cols, future kinds) */
    this.inspectorOverlay = null;
    /** @type {string|null} last applied inspector kind for restyle */
    this._inspectorOverlayKind = null;
    /** Top-right companion instruction (like Text Select mode indicator) */
    this.inspectorModeIndicator = null;
    /** @type {{ kind?: string|null, confirmKey?: string }|null} */
    this._inspectorIndicatorOpts = null;
    // Legacy aliases — kept so any lingering callers don't throw during transition
    this.deleteOverlay = null;
    this.colsOverlay = null;
    this.focusedTextOverlay = null; // New overlay for focused text fields
    this.viewportModalFrame = null; // Viewport modal frame for text focus mode
    this.activeTextInputFrame = null; // Pulsing frame for active text inputs
    this.escExitLabelText = null; // ESC label for text fields
    this.escExitLabelHover = null; // ESC label for hovered elements
    this.hoverClickLabelText = 'F clicks'; // Hover label for click arming in text focus mode
    this.popoverContainer = null; // Container for popover iframe
    this.popoverIframeElement = null; // iframe element (for focus management)
    this.popoverIframeWindow = null; // contentWindow of the current popover iframe (for message validation)
    this.popoverMessageHandler = null; // message listener for iframe bridge
    this.popoverInitTimer = null; // timer for bridge init retries
    this.popoverBridgeReady = false; // whether iframe bridge has acked readiness
    this.popoverCloseButton = null; // close button element for keyboard activation (F)
    this._popoverLastMouse = { x: null, y: null }; // last known mouse position in top document
    this._popoverMouseTrackerInstalled = false;
    this._isPreviewPopover = false; // track if current popover is preview style (no backdrop)
    this._popoverArrowStyle = null; // style element for preview popover triangle
    this._popoverClickOutsideHandler = null; // click outside handler for preview popover
    this._previewPopoverDragCleanup = null; // teardown titlebar drag listeners
    this._popoverResizeDispose = null; // teardown generic resize handles
    this._popoverHybridFocusCleanup = null; // teardown chrome↔iframe focus routing
    this._previewMobileUaActive = false; // SW session rule: mobile UA for preview iframe

    // Central popup stack + blurred backdrop (kept below click overlays).
    // Note: Panel change callback will be set by KeyPilot after initialization
    this.popupManager = new PopupManager();
    this._popoverPopupId = 'kpv2-iframe-popover';

    // Initialize highlight manager
    this.highlightManager = new HighlightManager();
    
    // Intersection observer for overlay visibility optimization
    this.overlayObserver = null;
    this.resizeObserver = null; // ResizeObserver for viewport modal frame
    
    // Track overlay visibility state
    this.overlayVisibility = {
      focus: true,
      delete: true,
      focusedText: true,
      activeTextInput: true,
      escExitLabel: true
    };

    // Settings-driven UI customization (populated by KeyPilot from chrome.storage.sync).
    this._modeSettings = {
      clickMode: null,
      textMode: null
    };

    // Debug panel for performance metrics
    this.debugPanel = null;
    this.debugPanelUpdateInterval = null;

    // When DOM-hover listener mode is enabled, render non-text focus rectangles in blue so it's
    // visually obvious we're using browser-native hover targeting (vs RBush-driven hit-testing).
    this._useDomHoverFocusColors = false;

    // Text focus styling (we style the focused input + nearby wrapper parents directly).
    this._textFocusCurrentElement = null;
    this._textFocusStyledElements = new Set();
    /** @type {'left_edge'|'background_tint'|null} */
    this._textFocusAppliedStyle = null;

    // Text input hover styling (SVG "Press F…" hint on hovered fields; outline is separate).
    this._textHoverCurrentElement = null;
    this._textHoverStyledElements = new Set();

    // Last known focus target (used for F-click scale-up pulse across all render modes).
    this._lastFocusElement = null;
    this._lastFocusRect = null;

    /**
     * Active temporary click/image effect overlays.
     * Fixed-position ghosts must be torn down when the source leaves view or the
     * page navigates — otherwise they animate alone over the next screen.
     * @type {Set<{ pulse: Element, sourceEl: Element|null, originRect: DOMRect|null, rafId: number, timeoutId: number, io: IntersectionObserver|null, teardown: () => void }>}
     */
    this._activeEphemeralEffects = new Set();
    this._ephemeralEffectLifecycleInstalled = false;
    /** @type {(() => void)|null} */
    this._ephemeralEffectLifecycleDispose = null;
    
    this.setupOverlayObserver();
    
    // Initialize highlight manager with observer
    this.highlightManager.initialize(this.overlayObserver);
  }

  /**
   * Toggle alternate focus overlay colors (blue) for DOM-hover listener targeting mode.
   * @param {boolean} enabled
   */
  setDomHoverFocusColorsEnabled(enabled) {
    this._useDomHoverFocusColors = !!enabled;
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] DOM hover focus colors enabled:', this._useDomHoverFocusColors);
    }
  }

  /**
   * Focus ring palette from click-mode settings (blue | green).
   * Falls back to blue when DOM-hover element styling is active and no setting is loaded.
   * @param {'blue'|'green'|string|null|undefined} [focusColor]
   */
  _getNonTextFocusPalette(focusColor) {
    const color = focusColor === 'green' || focusColor === 'blue'
      ? focusColor
      : (this._getClickModeSettings().focusColor || 'blue');
    if (color === 'green') {
      return {
        borderColor: COLORS.FOCUS_GREEN,
        shadowColor: COLORS.GREEN_SHADOW,
        shadowBrightColor: COLORS.GREEN_SHADOW_BRIGHT,
        backgroundColor: COLORS.FOCUS_GREEN_BG_T2
      };
    }
    return {
      borderColor: COLORS.FOCUS_BLUE,
      shadowColor: COLORS.BLUE_SHADOW,
      shadowBrightColor: COLORS.BLUE_SHADOW_BRIGHT,
      backgroundColor: COLORS.FOCUS_BLUE_BG_T2
    };
  }

  /**
   * Resolve clickable category for focus chrome decisions.
   * @param {Element|null} element
   * @returns {string}
   */
  getFocusCategory(element) {
    try {
      const detector = window.keyPilot?.detector || window.keyPilot?.elementDetector;
      if (detector && typeof detector.getClickableCategory === 'function') {
        return detector.getClickableCategory(element);
      }
    } catch { /* ignore */ }
    return CLICKABLE_CATEGORY.NONE;
  }

  /**
   * Suppress the semi-transparent blue focus fill by category:
   * - slider: never (progress bars are not link-like)
   * - media with seek chrome in the player shell: no fill (players, not thumbnails)
   * - text: no blue wash
   *
   * Links / generic / media thumbnails keep the fill.
   *
   * @param {Element|null} element
   * @returns {boolean}
   */
  shouldSuppressFocusFill(element) {
    try {
      if (!element || !(element instanceof Element)) return false;
      const detector = window.keyPilot?.detector || window.keyPilot?.elementDetector;
      if (detector && typeof detector.shouldSuppressFocusFillForElement === 'function') {
        return detector.shouldSuppressFocusFillForElement(element);
      }
      const cat = this.getFocusCategory(element);
      return cat === CLICKABLE_CATEGORY.SLIDER || cat === CLICKABLE_CATEGORY.TEXT;
    } catch {
      return false;
    }
  }

  /**
   * Media / slider surfaces for F-key scale-pulse skip (not link-style).
   * @param {Element|null} element
   * @returns {boolean}
   */
  isVideoLikeElement(element) {
    try {
      if (!element || !(element instanceof Element)) return false;
      const cat = this.getFocusCategory(element);
      if (cat === CLICKABLE_CATEGORY.SLIDER || cat === CLICKABLE_CATEGORY.MEDIA) return true;
      // Fallback: raw video node if category unavailable
      if (element.tagName === 'VIDEO' || element.tagName === 'AUDIO') return true;
      return !!(element.querySelector && element.querySelector('video'));
    } catch {
      return false;
    }
  }

  // Canvas-based rendering backend
  initCanvasRenderer() {
    if (this.canvasOverlay) return;

    this.canvasOverlay = document.createElement('canvas');
    this.canvasOverlay.className = CSS_CLASSES.CANVAS_OVERLAY || 'kpv2-canvas-overlay';
    this.canvasOverlay.style.cssText = `
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      pointer-events: none;
      z-index: ${Z_INDEX.OVERLAYS};
      will-change: transform;
    `;

    // Set canvas size to viewport
    this.canvasOverlay.width = window.innerWidth;
    this.canvasOverlay.height = window.innerHeight;

    this.canvasContext = this.canvasOverlay.getContext('2d');
    document.body.appendChild(this.canvasOverlay);

    // Handle viewport resize
    this._canvasResizeHandler = () => {
      this.canvasOverlay.width = window.innerWidth;
      this.canvasOverlay.height = window.innerHeight;
    };
    window.addEventListener('resize', this._canvasResizeHandler);

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Canvas renderer initialized');
    }
  }

  cleanupCanvasRenderer() {
    if (this.canvasOverlay) {
      if (this._canvasResizeHandler) {
        window.removeEventListener('resize', this._canvasResizeHandler);
        this._canvasResizeHandler = null;
      }
      this.canvasOverlay.remove();
      this.canvasOverlay = null;
      this.canvasContext = null;
    }
  }

  updateFocusOverlayCanvas(element, mode = MODES.NONE, rectOverride = null) {
    if (!this.canvasContext || !element) {
      this.hideFocusOverlayCanvas();
      return;
    }

    // Don't outline modal/popover iframes
    try {
      if (element.tagName === 'IFRAME') {
        const isPopoverIframe = this.popoverIframeElement && element === this.popoverIframeElement;
        const isModalIframe = !!(element.classList && element.classList.contains('modal-iframe'));
        if (isPopoverIframe || isModalIframe) {
          this.hideFocusOverlayCanvas();
          return;
        }
      }
    } catch { /* ignore */ }

    const rect = (rectOverride && typeof rectOverride === 'object')
      ? rectOverride
      : this.getBestRect(element);
    if (!rect || rect.width <= 0 || rect.height <= 0) {
      this.hideFocusOverlayCanvas();
      return;
    }

    // Determine element type and colors
    const isTextInput = element.matches && element.matches(SELECTORS.FOCUSABLE_TEXT);
    const suppressFill = this.shouldSuppressFocusFill(element);
    const isVeryLarge = rect.width > 512 && rect.height > 512;

    const clickSettings = this._getClickModeSettings();
    let borderColor, shadowColor, backgroundColor;
    if (isTextInput) {
      borderColor = COLORS.ORANGE;
      shadowColor = COLORS.ORANGE_SHADOW;
      backgroundColor = 'transparent';
    } else {
      const p = this._getNonTextFocusPalette(clickSettings.focusColor);
      borderColor = p.borderColor;
      shadowColor = p.shadowColor;
      // Fill for thumbnails/links; suppress for scrubbers and players that have a seek bar.
      backgroundColor = (suppressFill || isVeryLarge) ? 'transparent' : p.backgroundColor;
    }

    // Settings-driven behavior
    const { rectangleThickness, overlayFillEnabled, overlayShadowEnabled } = clickSettings;
    if (!isTextInput && !suppressFill && !isVeryLarge && overlayFillEnabled === false) {
      backgroundColor = 'transparent';
    }

    // Clear canvas and draw rectangle
    this.canvasContext.clearRect(0, 0, this.canvasOverlay.width, this.canvasOverlay.height);

    // Draw background fill if enabled
    if (backgroundColor !== 'transparent') {
      this.canvasContext.fillStyle = backgroundColor;
      this.canvasContext.fillRect(rect.left, rect.top, rect.width, rect.height);
    }

    // Draw border
    this.canvasContext.strokeStyle = borderColor;
    this.canvasContext.lineWidth = rectangleThickness;
    this.canvasContext.strokeRect(rect.left, rect.top, rect.width, rect.height);

    // Optional soft outer glow
    if (overlayShadowEnabled !== false) {
      this.canvasContext.shadowColor = shadowColor;
      this.canvasContext.shadowBlur = 4;
      this.canvasContext.strokeRect(rect.left, rect.top, rect.width, rect.height);
      this.canvasContext.shadowBlur = 0;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Canvas focus overlay updated:', {
        rect: rect,
        borderColor: borderColor,
        backgroundColor: backgroundColor
      });
    }
  }

  hideFocusOverlayCanvas() {
    if (this.canvasContext) {
      this.canvasContext.clearRect(0, 0, this.canvasOverlay.width, this.canvasOverlay.height);
    }
  }

  // CSS Custom Properties rendering backend
  initCSSCustomPropsRenderer() {
    if (this.cssCustomPropsOverlay) return;

    this.cssCustomPropsOverlay = document.createElement('div');
    this.cssCustomPropsOverlay.className = CSS_CLASSES.CSS_PROPS_OVERLAY || 'kpv2-css-props-overlay';
    const p = this._getNonTextFocusPalette();
    this.cssCustomPropsOverlay.style.cssText = `
      --rect-x: 0px;
      --rect-y: 0px;
      --rect-width: 0px;
      --rect-height: 0px;
      --rect-border-color: ${p.borderColor};
      --rect-background: transparent;
      --rect-shadow-color: ${p.shadowColor};
      --rect-shadow-bright-color: ${p.shadowBrightColor};
      --rect-border-thickness: 2px;

      position: fixed;
      left: var(--rect-x);
      top: var(--rect-y);
      width: var(--rect-width);
      height: var(--rect-height);
      border: var(--rect-border-thickness) solid var(--rect-border-color);
      background: var(--rect-background);
      box-shadow: 0 0 0 2px var(--rect-shadow-color), 0 0 10px 2px var(--rect-shadow-bright-color);
      pointer-events: none;
      z-index: ${Z_INDEX.OVERLAYS};
      will-change: transform;
    `;

    document.body.appendChild(this.cssCustomPropsOverlay);

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] CSS Custom Properties renderer initialized');
    }
  }

  cleanupCSSCustomPropsRenderer() {
    if (this.cssCustomPropsOverlay) {
      this.cssCustomPropsOverlay.remove();
      this.cssCustomPropsOverlay = null;
    }
  }

  updateFocusOverlayCSSCustomProps(element, mode = MODES.NONE, rectOverride = null) {
    if (!this.cssCustomPropsOverlay || !element) {
      this.hideFocusOverlayCSSCustomProps();
      return;
    }

    // Don't outline modal/popover iframes
    try {
      if (element.tagName === 'IFRAME') {
        const isPopoverIframe = this.popoverIframeElement && element === this.popoverIframeElement;
        const isModalIframe = !!(element.classList && element.classList.contains('modal-iframe'));
        if (isPopoverIframe || isModalIframe) {
          this.hideFocusOverlayCSSCustomProps();
          return;
        }
      }
    } catch { /* ignore */ }

    const rect = (rectOverride && typeof rectOverride === 'object')
      ? rectOverride
      : this.getBestRect(element);
    if (!rect || rect.width <= 0 || rect.height <= 0) {
      this.hideFocusOverlayCSSCustomProps();
      return;
    }

    // Determine element type and colors
    const isTextInput = element.matches && element.matches(SELECTORS.FOCUSABLE_TEXT);
    const suppressFill = this.shouldSuppressFocusFill(element);
    const isVeryLarge = rect.width > 512 && rect.height > 512;

    const clickSettings = this._getClickModeSettings();
    let borderColor, shadowColor, backgroundColor, shadowBrightColor;
    if (isTextInput) {
      borderColor = COLORS.ORANGE;
      shadowColor = COLORS.ORANGE_SHADOW;
      shadowBrightColor = COLORS.ORANGE_SHADOW;
      backgroundColor = 'transparent';
    } else {
      const p = this._getNonTextFocusPalette(clickSettings.focusColor);
      borderColor = p.borderColor;
      shadowColor = p.shadowColor;
      shadowBrightColor = p.shadowBrightColor;
      // Fill for thumbnails/links; suppress for scrubbers and players that have a seek bar.
      backgroundColor = (suppressFill || isVeryLarge) ? 'transparent' : p.backgroundColor;
    }

    // Settings-driven behavior
    const { rectangleThickness, overlayFillEnabled, overlayShadowEnabled } = clickSettings;
    if (!isTextInput && !suppressFill && !isVeryLarge && overlayFillEnabled === false) {
      backgroundColor = 'transparent';
    }

    // Update CSS custom properties
    const overlay = this.cssCustomPropsOverlay;
    overlay.style.setProperty('--rect-x', rect.left + 'px');
    overlay.style.setProperty('--rect-y', rect.top + 'px');
    overlay.style.setProperty('--rect-width', rect.width + 'px');
    overlay.style.setProperty('--rect-height', rect.height + 'px');
    overlay.style.setProperty('--rect-border-color', borderColor);
    overlay.style.setProperty('--rect-background', backgroundColor);
    overlay.style.setProperty('--rect-shadow-color', shadowColor);
    overlay.style.setProperty('--rect-shadow-bright-color', shadowBrightColor);
    overlay.style.setProperty('--rect-border-thickness', rectangleThickness + 'px');
    if (overlayShadowEnabled === false) {
      overlay.style.boxShadow = 'none';
    } else {
      overlay.style.boxShadow =
        '0 0 0 2px var(--rect-shadow-color), 0 0 10px 2px var(--rect-shadow-bright-color)';
    }

    overlay.style.display = 'block';
    overlay.style.visibility = 'visible';
    overlay.style.opacity = '1';

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] CSS Custom Props focus overlay updated:', {
        rect: rect,
        borderColor: borderColor,
        backgroundColor: backgroundColor
      });
    }
  }

  hideFocusOverlayCSSCustomProps() {
    if (this.cssCustomPropsOverlay) {
      this.cssCustomPropsOverlay.style.setProperty('--rect-width', '0px');
      this.cssCustomPropsOverlay.style.setProperty('--rect-height', '0px');
      this.cssCustomPropsOverlay.style.display = 'none';
    }
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  setSelectionMode(mode) {
    return this.highlightManager.setSelectionMode(mode);
  }

  /**
   * Apply click/text mode settings from chrome.storage (via KeyPilot).
   * @param {object|null|undefined} settings
   */
  setModeSettings(settings) {
    const s = settings && typeof settings === 'object' ? settings : {};
    this._modeSettings = {
      clickMode: s.clickMode && typeof s.clickMode === 'object' ? s.clickMode : null,
      textMode: s.textMode && typeof s.textMode === 'object' ? s.textMode : null
    };

    // If a text field is currently styled, re-apply so focusStyle changes take effect live.
    try {
      const focused = this._textFocusCurrentElement;
      if (focused && focused.isConnected) {
        this._textFocusCurrentElement = null;
        this._applyTextFocusElementStyling(focused);
      }
    } catch { /* ignore */ }
  }

  _getClickModeSettings() {
    const cm = this._modeSettings?.clickMode && typeof this._modeSettings.clickMode === 'object'
      ? this._modeSettings.clickMode
      : {};
    const def = DEFAULT_SETTINGS.clickMode || {};
    const rectangleThickness = Number(cm.rectangleThickness);
    const thickness = Number.isFinite(rectangleThickness)
      ? Math.min(Math.max(rectangleThickness, 1), 16)
      : (Number(def.rectangleThickness) || 3);
    // Defaults: outline only (no fill / no glow) unless explicitly enabled.
    const overlayFillEnabled = cm.overlayFillEnabled === true;
    const overlayShadowEnabled = cm.overlayShadowEnabled === true;
    const focusColor = cm.focusColor === 'green' ? 'green' : 'blue';
    const rawEffect = cm.clickEffect;
    const clickEffect =
      rawEffect === 'flash' ||
      rawEffect === 'dash' ||
      rawEffect === 'marquee' ||
      rawEffect === 'scale' ||
      rawEffect === 'none'
        ? rawEffect
        : (def.clickEffect || 'flash');
    return {
      rectangleThickness: thickness,
      overlayFillEnabled,
      overlayShadowEnabled,
      focusColor,
      clickEffect
    };
  }

  _getTextModeSettings() {
    const tm = this._modeSettings?.textMode && typeof this._modeSettings.textMode === 'object'
      ? { ...DEFAULT_SETTINGS.textMode, ...this._modeSettings.textMode }
      : DEFAULT_SETTINGS.textMode;
    const strokeThickness = Number(tm.strokeThickness);
    const thickness = Number.isFinite(strokeThickness) ? Math.min(Math.max(strokeThickness, 1), 16) : 3;
    const leftEdgeRaw = Number(tm.leftEdgeWidth);
    const leftEdgeWidth = Number.isFinite(leftEdgeRaw)
      ? Math.min(Math.max(leftEdgeRaw, 1), 24)
      : (Number(DEFAULT_SETTINGS.textMode.leftEdgeWidth) || 5);
    const focusStyle = tm.focusStyle === 'background_tint' ? 'background_tint' : 'left_edge';
    return {
      strokeThickness: thickness,
      labelsEnabled: tm.labelsEnabled,
      focusStyle,
      leftEdgeWidth
    };
  }

  setHoverClickLabelText(text) {
    this.hoverClickLabelText = String(text || 'F clicks');
    // In the new UI, the hover-click text is shown as a suffix on the "Active text field" label.
    if (this.escExitLabelHover) this.escExitLabelHover.innerHTML = this.formatActiveTextFieldLabel();
  }

  formatHoverLabelText(text) {
    // Handle countdown format: "5 F clicks" -> "<span class='countdown'>5</span> <kbd>F</kbd> clicks"
    // Handle regular format: "F clicks" -> "<kbd>F</kbd> clicks"
    const countdownMatch = text.match(/^(\d+)\s+(F clicks?)$/);
    if (countdownMatch) {
      const [_, number, rest] = countdownMatch;
      return `<span class="countdown-number">${number}</span> <kbd>F</kbd> clicks`;
    } else {
      return text.replace(/^F/, '<kbd>F</kbd>');
    }
  }

  formatActiveTextFieldLabel() {
    const suffix = this.hoverClickLabelText ? this.formatHoverLabelText(this.hoverClickLabelText) : '';
    return suffix ? `Active text field &nbsp;•&nbsp; ${suffix}` : 'Active text field';
  }

  ensureTextModeLabels() {
    if (!this.escExitLabelHover) {
      // Reuse the existing "hover" label slot for the Active Text label (so cleanup + observer stays simple).
      this.escExitLabelHover = this.createElement('div', {
        className: CSS_CLASSES.ESC_EXIT_LABEL,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_ABOVE};
          will-change: transform, opacity;
        `
      });
      this.escExitLabelHover.innerHTML = this.formatActiveTextFieldLabel();
      document.body.appendChild(this.escExitLabelHover);
      if (this.overlayObserver) this.overlayObserver.observe(this.escExitLabelHover);
    } else {
      this.escExitLabelHover.innerHTML = this.formatActiveTextFieldLabel();
    }

    if (!this.escExitLabelText) {
      this.escExitLabelText = this.createElement('div', {
        className: CSS_CLASSES.ESC_EXIT_LABEL,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_ABOVE};
          will-change: transform, opacity;
        `
      });
      this.escExitLabelText.innerHTML = 'Press <kbd>ESC</kbd> to close';
      document.body.appendChild(this.escExitLabelText);
      if (this.overlayObserver) this.overlayObserver.observe(this.escExitLabelText);
    } else {
      this.escExitLabelText.innerHTML = 'Press <kbd>ESC</kbd> to close';
    }
  }

  updateTextModeLabels(element) {
    if (!element) {
      this.hideTextModeLabels();
      return;
    }

    const { labelsEnabled } = this._getTextModeSettings();
    if (!labelsEnabled) {
      this.hideTextModeLabels();
      return;
    }

    this.ensureTextModeLabels();

    const rect = this.getBestRect(element);
    if (!rect || rect.width <= 0 || rect.height <= 0) {
      this.hideTextModeLabels();
      return;
    }

    // Measure labels after setting innerHTML.
    const active = this.escExitLabelHover;
    const esc = this.escExitLabelText;
    const activeH = active?.offsetHeight || 24;
    const escH = esc?.offsetHeight || 24;
    const activeW = active?.offsetWidth || 180;
    const escW = esc?.offsetWidth || 180;
    const pad = 8;

    // Preferred placement: active above, ESC below.
    let activeTop = rect.top - activeH - pad;
    let escTop = rect.top + rect.height + pad;

    // If offscreen, flip.
    if (activeTop < pad) activeTop = rect.top + rect.height + pad;
    if (escTop + escH > window.innerHeight - pad) escTop = rect.top - escH - pad;

    // If they collide, stack them.
    if (Math.abs(activeTop - escTop) < Math.min(activeH, escH) + 4) {
      // Keep active closer to the element; push ESC away.
      if (escTop >= rect.top + rect.height) {
        escTop = escTop + activeH + 6;
      } else {
        escTop = escTop - activeH - 6;
      }
    }

    const clampLeft = (w) => Math.min(Math.max(rect.left, pad), window.innerWidth - w - pad);
    const activeLeft = clampLeft(activeW);
    const escLeft = clampLeft(escW);

    if (active) {
      active.style.left = `${activeLeft}px`;
      active.style.top = `${Math.min(Math.max(activeTop, pad), window.innerHeight - activeH - pad)}px`;
      active.style.display = 'block';
      active.style.visibility = 'visible';
    }
    if (esc) {
      esc.style.left = `${escLeft}px`;
      esc.style.top = `${Math.min(Math.max(escTop, pad), window.innerHeight - escH - pad)}px`;
      esc.style.display = 'block';
      esc.style.visibility = 'visible';
    }
  }

  hideTextModeLabels() {
    if (this.escExitLabelText) this.escExitLabelText.style.display = 'none';
    if (this.escExitLabelHover) this.escExitLabelHover.style.display = 'none';
  }

  _clearTextFocusElementStyling() {
    if (!this._textFocusStyledElements || this._textFocusStyledElements.size === 0) {
      this._textFocusCurrentElement = null;
      this._textFocusAppliedStyle = null;
      return;
    }
    try {
      for (const el of this._textFocusStyledElements) {
        if (!el || el.nodeType !== 1) continue;
        try {
          el.classList.remove(CSS_CLASSES.TEXT_FOCUS_INPUT);
          el.classList.remove(CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT);
          el.classList.remove(CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE);
        } catch { /* ignore */ }
      }
    } finally {
      this._textFocusStyledElements.clear();
      this._textFocusCurrentElement = null;
      this._textFocusAppliedStyle = null;
    }
  }

  _getNearbyInputWrappers(inputEl) {
    const parents = [];
    if (!inputEl || inputEl.nodeType !== 1) return parents;

    // Only attempt wrapper styling when we have a meaningful rect.
    const inputRect = this.getBestRect(inputEl);
    if (!inputRect || inputRect.width <= 0 || inputRect.height <= 0) return parents;

    const maxDepth = 4;
    const maxPad = 28; // px of allowed expansion around the input (rounded containers, icons, padding)
    const maxAreaMultiple = 4.0; // avoid tinting large layout containers

    let p = inputEl.parentElement;
    let depth = 0;
    while (p && depth++ < maxDepth) {
      // Never tint the whole page.
      if (p === document.body || p === document.documentElement) break;

      let r;
      try {
        r = p.getBoundingClientRect();
      } catch {
        break;
      }

      if (!r || r.width <= 0 || r.height <= 0) {
        p = p.parentElement;
        continue;
      }

      const dx = Math.max(Math.abs(r.left - inputRect.left), Math.abs(r.right - inputRect.right));
      const dy = Math.max(Math.abs(r.top - inputRect.top), Math.abs(r.bottom - inputRect.bottom));

      const inputArea = inputRect.width * inputRect.height;
      const area = r.width * r.height;

      const closeEnough = dx <= maxPad && dy <= maxPad;
      const notTooBig =
        area <= inputArea * maxAreaMultiple &&
        r.width <= inputRect.width + maxPad * 2 &&
        r.height <= inputRect.height + maxPad * 2;

      if (!closeEnough || !notTooBig) break;

      parents.push(p);
      p = p.parentElement;
    }

    return parents;
  }

  /**
   * Lazy-inject KeyPilot CSS into the open ShadowRoot that owns `el` (if any).
   * No-op for light DOM. See StyleManager.ensureStylesForNode.
   * @param {Element|null|undefined} el
   */
  _ensureStylesForElement(el) {
    try {
      const sm = window.keyPilot?.styleManager;
      if (sm && typeof sm.ensureStylesForNode === 'function') {
        sm.ensureStylesForNode(el);
      }
    } catch { /* ignore */ }
  }

  _applyTextFocusElementStyling(inputEl) {
    if (!inputEl || inputEl.nodeType !== 1) {
      this._clearTextFocusElementStyling();
      return;
    }

    const { focusStyle } = this._getTextModeSettings();
    const useLeftEdge = focusStyle !== 'background_tint';

    // Avoid thrashing the DOM on RAF-driven overlay refreshes when style is unchanged.
    if (
      this._textFocusCurrentElement === inputEl &&
      this._textFocusStyledElements.size > 0 &&
      this._textFocusAppliedStyle === focusStyle
    ) {
      try {
        inputEl.classList.add(CSS_CLASSES.TEXT_FOCUS_INPUT);
        if (useLeftEdge) inputEl.classList.add(CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE);
        else inputEl.classList.remove(CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE);
      } catch { /* ignore */ }
      return;
    }

    this._clearTextFocusElementStyling();
    this._textFocusCurrentElement = inputEl;
    this._textFocusAppliedStyle = focusStyle;

    try {
      this._ensureStylesForElement(inputEl);
      inputEl.classList.add(CSS_CLASSES.TEXT_FOCUS_INPUT);
      if (useLeftEdge) inputEl.classList.add(CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE);
      this._textFocusStyledElements.add(inputEl);
    } catch { /* ignore */ }

    // Background-tint style can wash nearby rounded wrappers; left-edge only paints the field.
    if (!useLeftEdge) {
      const parents = this._getNearbyInputWrappers(inputEl);
      for (const p of parents) {
        try {
          this._ensureStylesForElement(p);
          p.classList.add(CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT);
          this._textFocusStyledElements.add(p);
        } catch { /* ignore */ }
      }
    }
  }

  _clearTextHoverElementStyling() {
    if (!this._textHoverStyledElements || this._textHoverStyledElements.size === 0) {
      this._textHoverCurrentElement = null;
      return;
    }
    try {
      for (const el of this._textHoverStyledElements) {
        if (!el || el.nodeType !== 1) continue;
        try {
          el.classList.remove(CSS_CLASSES.TEXT_HOVER_INPUT);
          el.classList.remove(CSS_CLASSES.TEXT_HOVER_INPUT_PARENT);
        } catch { /* ignore */ }
      }
    } finally {
      this._textHoverStyledElements.clear();
      this._textHoverCurrentElement = null;
    }
  }

  _applyTextHoverElementStyling(inputEl) {
    if (!inputEl || inputEl.nodeType !== 1) {
      this._clearTextHoverElementStyling();
      return;
    }

    // Avoid thrashing while mouse is steady.
    if (this._textHoverCurrentElement === inputEl && this._textHoverStyledElements.size > 0) {
      try { inputEl.classList.add(CSS_CLASSES.TEXT_HOVER_INPUT); } catch { /* ignore */ }
      return;
    }

    this._clearTextHoverElementStyling();
    this._textHoverCurrentElement = inputEl;

    try {
      this._ensureStylesForElement(inputEl);
      inputEl.classList.add(CSS_CLASSES.TEXT_HOVER_INPUT);
      this._textHoverStyledElements.add(inputEl);
    } catch { /* ignore */ }

    // Hover is outline + SVG hint only — do not tint wrapper parents.
  }

  setupOverlayObserver() {
    // Observer to optimize overlay rendering when out of view
    this.overlayObserver = new IntersectionObserver(
      (entries) => {
        entries.forEach(entry => {
          const overlay = entry.target;
          const isVisible = entry.intersectionRatio > 0;
          
          // Optimize rendering by hiding completely out-of-view overlays
          if (overlay === this.focusOverlay) {
            this.overlayVisibility.focus = isVisible;
            // Don't hide focus overlay when DOM hover colors are enabled (it should always be visible when shown)
            if (!this._useDomHoverFocusColors) {
              overlay.style.visibility = isVisible ? 'visible' : 'hidden';
            } else {
              overlay.style.visibility = 'visible';
            }
          } else if (overlay === this.inspectorOverlay || overlay === this.deleteOverlay) {
            this.overlayVisibility.delete = isVisible;
            overlay.style.visibility = isVisible ? 'visible' : 'hidden';
          } else if (overlay === this.focusedTextOverlay) {
            this.overlayVisibility.focusedText = isVisible;
            overlay.style.visibility = isVisible ? 'visible' : 'hidden';
          } else if (overlay === this.activeTextInputFrame) {
            this.overlayVisibility.activeTextInput = isVisible;
            overlay.style.visibility = isVisible ? 'visible' : 'hidden';
          } else if (overlay === this.escExitLabelText) {
            this.overlayVisibility.escExitLabel = isVisible;
            overlay.style.visibility = isVisible ? 'visible' : 'hidden';
          } else if (overlay === this.escExitLabelHover) {
            this.overlayVisibility.escExitLabel = isVisible;
            overlay.style.visibility = isVisible ? 'visible' : 'hidden';
          }
        });
      },
      {
        rootMargin: '10px',
        threshold: [0, 1.0]
      }
    );
  }

  /**
   * @param {Element|null} focusEl
   * @param {Element|null} inspectorEl shared pick target (Delete/Cols/…)
   * @param {string} mode
   * @param {Element|null} [focusedTextElement]
   * @param {DOMRect|object|null} [focusRectOverride]
   * @param {string|null} [inspectorKind] INSPECTOR_KIND when mode is inspector
   */
  updateOverlays(focusEl, inspectorEl, mode, focusedTextElement = null, focusRectOverride = null, inspectorKind = null) {
    // Debug logging when debug mode is enabled
    if (window.KEYPILOT_DEBUG && focusEl) {
      console.log('[KeyPilot Debug] Updating overlays:', {
        focusElement: focusEl.tagName,
        mode: mode,
        willShowFocus: mode === 'none' || mode === 'text_focus' || mode === 'highlight' || mode === 'popover',
        focusedTextElement: focusedTextElement?.tagName
      });
    }
    
    // Show focus overlay in normal mode, text focus mode, highlight mode, AND popover mode.
    // Popovers are modal but still need the green rectangle so the user can F-click UI
    // affordances like the close (×) button.
    // Hide green focus rect while in shared inspector pick mode.
    if (mode === 'none' || mode === 'text_focus' || mode === 'highlight' || mode === 'popover') {
      this.updateFocusOverlay(focusEl, mode, focusRectOverride);
      
      if (mode === 'text_focus') {
        // Labels are attached to the focused text field, not the hovered element.
        if (focusedTextElement) this.updateTextModeLabels(focusedTextElement);
        else this.hideTextModeLabels();
      } else {
        this.hideTextModeLabels();
      }
    } else {
      this.hideFocusOverlay();
    }

    // If there's no focus target at all, ensure we remove any hover tint.
    if (!focusEl) {
      this._clearTextHoverElementStyling();
    }
    
    // Text focus mode styling: tint the actual focused input (and nearby wrapper parents)
    // instead of drawing an orange frame overlay.
    if (mode === 'text_focus' && focusedTextElement) {
      this._applyTextFocusElementStyling(focusedTextElement);
    } else {
      this._clearTextFocusElementStyling();
    }

    // Always suppress the legacy orange text-focus frame overlays (we now style the element itself).
    this.hideFocusedTextOverlay();
    this.hideActiveTextInputFrame();
    
    // Show viewport modal frame when in text focus mode (controlled by flag)
    this.updateViewportModalFrame(mode === 'text_focus' && FEATURE_FLAGS.SHOW_WINDOW_OUTLINE);
    
    // Shared inspector pick overlay (Delete, Cols, future kinds)
    const inInspector = mode === MODES.INSPECTOR || mode === 'delete' || mode === 'cols';
    if (inInspector) {
      const kind = inspectorKind
        || (mode === 'delete' ? 'delete' : mode === 'cols' ? 'cols' : null);
      this.updateInspectorOverlay(inspectorEl, kind);
      // Keep top-right instruction visible while pick is active
      this.showInspectorModeIndicator({ kind });
    } else {
      this.hideInspectorOverlay();
      this.hideInspectorModeIndicator();
    }
    
    // Show highlight chrome in highlight mode (instruction + optional focus ring)
    if (mode === 'highlight') {
      this.highlightManager.updateHighlightOverlay(focusEl);
      // finishKey is set explicitly in startHighlighting; keep indicator visible here
      this.highlightManager.showHighlightModeIndicator();
    } else {
      this.highlightManager.hideHighlightOverlay();
      this.highlightManager.hideHighlightModeIndicator();
      // Leaving highlight: drop any leftover dashed guide (cancel also clears this)
      this.highlightManager.hideHighlightRectangleOverlay();
    }
  }

  // Unified interface that switches between rendering modes
  updateFocusOverlay(element, mode = MODES.NONE, rectOverride = null) {
    // Remember target for activation pulse (F-click), independent of render backend.
    try {
      this._lastFocusElement = element || null;
      if (element) {
        const r = (rectOverride && typeof rectOverride === 'object')
          ? rectOverride
          : this.getBestRect(element);
        this._lastFocusRect = r && r.width > 0 && r.height > 0
          ? { left: r.left, top: r.top, width: r.width, height: r.height }
          : null;
      } else {
        this._lastFocusRect = null;
      }
    } catch {
      this._lastFocusElement = element || null;
      this._lastFocusRect = null;
    }

    // Text inputs: show orange outline AND paint the SVG "Press F to select…" hint.
    // (Do not return early — fall through so the focus rectangle still draws.)
    try {
      const isTextInput = element && element.matches && element.matches(SELECTORS.FOCUSABLE_TEXT);
      if (isTextInput) {
        this._applyTextHoverElementStyling(element);
      } else {
        // Non-text elements: ensure we remove any lingering hover hint styling.
        this._clearTextHoverElementStyling();
      }
    } catch {
      try { this._clearTextHoverElementStyling(); } catch { /* ignore */ }
    }

    // When DOM hover mode is enabled, style the element directly (fast path).
    // Clipped contexts use inset rings (negative outline-offset) — not a fixed
    // overlay — so we keep hover chrome snappy.
    if (this._useDomHoverFocusColors) {
      // Hide any fixed-position backends so we never double-paint with canvas.
      try { this.hideFocusOverlayCanvas(); } catch { /* ignore */ }
      try { this.hideFocusOverlayCSSCustomProps(); } catch { /* ignore */ }
      try { this.hideFocusOverlayDOM(); } catch { /* ignore */ }
      return this.updateFocusOverlayElementStyling(element, mode);
    }

    switch (this.renderingMode) {
      case 'canvas':
        return this.updateFocusOverlayCanvas(element, mode, rectOverride);
      case 'css-custom-props':
        return this.updateFocusOverlayCSSCustomProps(element, mode, rectOverride);
      case 'dom':
      default:
        return this.updateFocusOverlayDOM(element, mode, rectOverride);
    }
  }

  /**
   * Find ancestors that would clip an outer focus ring around `element`.
   *
   * Used only when FEATURE_FLAGS.ENABLE_FOCUS_CLIP_INSET and/or
   * ENABLE_FOCUS_TIGHT_WRAPPER_PROMOTION are on (see constants.js for tentative
   * purpose). Never opens page overflow — that broke carousels (IMDb).
   *
   * @param {Element} element
   * @returns {{
   *   clippers: Element[],
   *   tightWrapper: Element|null
   * }}
   *   clippers — ancestors that would clip a positive outline-offset ring
   *   tightWrapper — nearest clipper nearly the same size as the target
   */
  _findFocusClipContext(element) {
    /** @type {Element[]} */
    const clippers = [];
    /** @type {Element|null} */
    let tightWrapper = null;

    if (!element || element.nodeType !== 1) {
      return { clippers, tightWrapper };
    }

    let er;
    try {
      er = element.getBoundingClientRect();
    } catch {
      return { clippers, tightWrapper };
    }
    if (!er || !(er.width > 0) || !(er.height > 0)) {
      return { clippers, tightWrapper };
    }

    // Outer ring needs a little space outside the border box.
    const pad = 8;
    const needLeft = er.left - pad;
    const needTop = er.top - pad;
    const needRight = er.right + pad;
    const needBottom = er.bottom + pad;

    // Walk composed ancestors (parentElement + open shadow host hops).
    // archive.org nests clickables inside media-button → … → NAV[overflow:hidden];
    // a parentElement-only walk stops at the shadow root and misses clippers.
    const composedParent = (node) => {
      if (!node || node.nodeType !== 1) return null;
      if (node.parentElement) return node.parentElement;
      try {
        const root = typeof node.getRootNode === 'function' ? node.getRootNode() : null;
        if (root && typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot) {
          return root.host || null;
        }
      } catch { /* ignore */ }
      return null;
    };

    try {
      let n = composedParent(element);
      let depth = 0;
      // Nested open shadows (archive.org tiles/nav) often need more than 12 hops.
      while (n && n.nodeType === 1 && depth++ < 24) {
        if (n === document.documentElement || n === document.body) {
          n = composedParent(n);
          continue;
        }

        const cs = window.getComputedStyle(n);
        if (!cs) {
          n = composedParent(n);
          continue;
        }

        const ox = cs.overflowX;
        const oy = cs.overflowY;
        const clipsOverflow =
          (ox && ox !== 'visible') || (oy && oy !== 'visible');

        let cvClip = false;
        try {
          const cv = cs.contentVisibility || cs.getPropertyValue('content-visibility');
          cvClip = cv === 'auto' || cv === 'hidden';
        } catch { /* ignore */ }

        let containClip = false;
        try {
          const contain = String(cs.contain || '');
          containClip =
            contain.includes('paint') ||
            contain.includes('strict') ||
            contain === 'content' ||
            contain.split(/\s+/).includes('content');
        } catch { /* ignore */ }

        if (clipsOverflow || cvClip || containClip) {
          let ar;
          try {
            ar = n.getBoundingClientRect();
          } catch {
            clippers.push(n);
            n = composedParent(n);
            continue;
          }

          const clipsRing =
            ar.left > needLeft + 0.5 ||
            ar.top > needTop + 0.5 ||
            ar.right < needRight - 0.5 ||
            ar.bottom < needBottom - 0.5;

          const similarSize =
            Math.abs(ar.width - er.width) < 20 &&
            Math.abs(ar.height - er.height) < 20;

          if (clipsRing || (cvClip && similarSize)) {
            clippers.push(n);
            if (!tightWrapper && similarSize) {
              tightWrapper = n;
            }
          }
        }

        n = composedParent(n);
      }
    } catch { /* ignore */ }

    return { clippers, tightWrapper };
  }

  /**
   * True when an ancestor is likely to clip *outer* focus chrome.
   * @param {Element} element
   * @returns {boolean}
   */
  _isProbablyClippedByAncestorOverflow(element) {
    try {
      const ctx = this._findFocusClipContext(element);
      return !!(ctx.clippers && ctx.clippers.length);
    } catch {
      return false;
    }
  }

  /**
   * Update focus overlay using element styling (for DOM hover mode)
   * Styles the element directly instead of creating overlay elements
   */
  updateFocusOverlayElementStyling(element, mode = MODES.NONE) {
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateFocusOverlayElementStyling called with:', {
        element: element?.tagName,
        mode: mode,
        _useDomHoverFocusColors: this._useDomHoverFocusColors
      });
    }

    // Clear any existing element styling first
    this.clearElementFocusStyling();

    if (!element) {
      return;
    }

    // In the wild (e.g. Engadget), an inline <a> sometimes wraps a block/flex child (<div>),
    // which causes the anchor to be split into multiple inline fragments. Outline/box-shadow
    // then renders as disjoint pieces (often thin strips). When we detect this, we style a
    // better single-rect descendant (usually the anchor's wrapper) instead of the anchor.
    let stylingTarget = this._resolveElementForFocusStyling(element) || element;

    // Optional clip-aware paint (flags in FEATURE_FLAGS — see constants.js).
    // Never mutate page overflow (broke IMDb carousels). Prefer painting on the
    // real hover target so data-kp-focus lands on the clickable <a>, not a parent.
    let useInset = false;
    let clipCtx = { clippers: [], tightWrapper: null };
    const clipInsetOn = !!(FEATURE_FLAGS && FEATURE_FLAGS.ENABLE_FOCUS_CLIP_INSET);
    const tightPromoteOn = !!(FEATURE_FLAGS && FEATURE_FLAGS.ENABLE_FOCUS_TIGHT_WRAPPER_PROMOTION);
    if (clipInsetOn || tightPromoteOn) {
      try {
        clipCtx = this._findFocusClipContext(stylingTarget);
      } catch {
        clipCtx = { clippers: [], tightWrapper: null };
      }
      if (
        tightPromoteOn &&
        clipCtx.tightWrapper &&
        clipCtx.tightWrapper.nodeType === 1
      ) {
        stylingTarget = clipCtx.tightWrapper;
      }
      if (clipInsetOn) {
        useInset =
          !!(clipCtx.clippers && clipCtx.clippers.length) ||
          this._isProbablyClippedByAncestorOverflow(stylingTarget);
      }
    }

    // Don't style modal/popover iframes
    try {
      if (stylingTarget.tagName === 'IFRAME') {
        const isPopoverIframe = this.popoverIframeElement && stylingTarget === this.popoverIframeElement;
        const isModalIframe = !!(stylingTarget.classList && stylingTarget.classList.contains('modal-iframe'));
        if (isPopoverIframe || isModalIframe) {
          return;
        }
      }
    } catch { /* ignore */ }

    // Determine styling based on element type and mode
    // Category/fill decisions stay on the original hover target when we promoted
    // the ring to a tight wrapper.
    const categorySource = element;
    const isTextInput = categorySource.matches && categorySource.matches(SELECTORS.FOCUSABLE_TEXT);
    const suppressFill = this.shouldSuppressFocusFill(categorySource);

    // Settings-driven focus chrome (color / thickness / fill / shadow).
    const {
      rectangleThickness,
      overlayFillEnabled,
      overlayShadowEnabled,
      focusColor
    } = this._getClickModeSettings();
    const palette = this._getNonTextFocusPalette(focusColor);
    const ringColor = isTextInput ? COLORS.ORANGE : palette.borderColor;
    const ringWidthPx = Math.min(Math.max(Number(rectangleThickness) || 3, 1), 16);
    const ringWidth = `${ringWidthPx}px`;
    const shadowColor = isTextInput ? COLORS.ORANGE_SHADOW : palette.shadowColor;
    const shadowBright = isTextInput ? COLORS.ORANGE_SHADOW : palette.shadowBrightColor;
    // Translucent wash for thumbnails/links; none for scrubbers / when fill disabled.
    let ringBgColor = 'transparent';
    if (!suppressFill && overlayFillEnabled !== false) {
      ringBgColor = isTextInput
        ? 'rgba(255,140,0,0.2)'
        : (palette.backgroundColor || 'rgba(33,150,243,0.25)');
    }
    const boxShadow = overlayShadowEnabled === false
      ? 'none'
      : `0 0 0 2px ${shadowColor}, 0 0 10px 2px ${shadowBright}`;

    // Shadow DOM: document CSS does not pierce; inject into this root on first use.
    this._ensureStylesForElement(stylingTarget);

    // Apply styling using CSS custom properties
    stylingTarget.style.setProperty('--keypilot-focus-ring-color', ringColor);
    stylingTarget.style.setProperty('--keypilot-focus-ring-width', ringWidth);
    stylingTarget.style.setProperty('--keypilot-focus-shadow-color', shadowColor);
    stylingTarget.style.setProperty('--keypilot-focus-ring-bg-color', ringBgColor);
    stylingTarget.style.setProperty('--keypilot-focus-box-shadow', boxShadow);

    // Class + data attributes. Prefer data-kp-focus for paint (CSS): SPAs often
    // strip unknown classes on re-render but leave data-* alone.
    stylingTarget.classList.add('keypilot-focus-element');
    // Only wash the background when a fill color is actually set — never wipe the
    // element's own background with transparent on hover.
    if (ringBgColor !== 'transparent') {
      stylingTarget.classList.add('keypilot-focus-element--fill');
    } else {
      stylingTarget.classList.remove('keypilot-focus-element--fill');
    }
    try {
      stylingTarget.setAttribute('data-kp-focus', '1');
      if (useInset) stylingTarget.setAttribute('data-kp-focus-inset', '1');
      else stylingTarget.removeAttribute('data-kp-focus-inset');
    } catch { /* ignore */ }
    if (useInset) stylingTarget.classList.add('keypilot-focus-element--inset');
    else stylingTarget.classList.remove('keypilot-focus-element--inset');

    // Store reference for cleanup
    this._currentStyledElement = stylingTarget;

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Applied element styling:', {
        tagName: stylingTarget.tagName,
        originalTagName: element?.tagName,
        styledDifferentElement: stylingTarget !== element,
        useInset: useInset,
        clipInsetFlag: clipInsetOn,
        tightPromoteFlag: tightPromoteOn,
        tightWrapper: !!(clipCtx && clipCtx.tightWrapper),
        ringColor: ringColor,
        isTextInput: isTextInput
      });
    }
  }

  /**
   * Largest in-tree descendant with a positive box (for collapsed anchors that
   * only wrap position:absolute media — e.g. Breitbart video carousel thumbs).
   * @param {HTMLElement} element
   * @returns {HTMLElement|null}
   */
  _findLargestVisibleDescendant(element) {
    if (!element || element.nodeType !== 1) return null;
    let best = null;
    let bestArea = 0;
    let visited = 0;
    const maxNodes = 48;
    const queue = [element];
    while (queue.length && visited < maxNodes) {
      const cur = queue.shift();
      if (!cur || cur.nodeType !== 1) continue;
      visited++;
      if (cur !== element) {
        let r = null;
        try { r = cur.getBoundingClientRect(); } catch { r = null; }
        if (r && r.width >= 8 && r.height >= 8) {
          const area = r.width * r.height;
          if (area > bestArea) {
            bestArea = area;
            best = /** @type {HTMLElement} */ (cur);
          }
        }
      }
      try {
        const kids = cur.children;
        if (kids) {
          for (let i = 0; i < kids.length; i++) {
            if (kids[i]?.nodeType === 1) queue.push(kids[i]);
          }
        }
      } catch { /* ignore */ }
    }
    return best;
  }

  /**
   * Choose the best element to apply the focus ring styling to.
   *
   * Problems:
   * 1) Inline anchors that wrap only position:absolute media collapse to 0×0
   *    (Breitbart video thumbs) — outline on the <a> is invisible.
   * 2) Inline elements that contain block children can be split into multiple
   *    inline fragments, causing outline/box-shadow to render as disjoint pieces.
   *
   * Strategy:
   * - If the element has no usable box, style the largest visible descendant
   *   (or a sized parent that wraps abspos content).
   * - If `element.getClientRects()` indicates fragmentation (2+ rects) and
   *   element is inline-ish, find the largest single-rect descendant.
   * - Otherwise, return `element`.
   *
   * @param {HTMLElement} element
   * @returns {HTMLElement}
   */
  _resolveElementForFocusStyling(element) {
    if (!element || element.nodeType !== 1) return element;

    // Collapsed clickable (0×0) with visible abspos children — paint on media.
    let br = null;
    try { br = element.getBoundingClientRect(); } catch { br = null; }
    if (!br || br.width < 2 || br.height < 2) {
      const descendant = this._findLargestVisibleDescendant(element);
      if (descendant) return descendant;

      // No sized descendant: try parent that actually boxes the abspos content
      // (e.g. .video-image { position: relative } wrapping the collapsed <a>).
      try {
        let p = element.parentElement;
        let hops = 0;
        while (p && p.nodeType === 1 && hops++ < 4) {
          if (p === document.body || p === document.documentElement) break;
          let pr = null;
          try { pr = p.getBoundingClientRect(); } catch { pr = null; }
          if (pr && pr.width >= 8 && pr.height >= 8) {
            return /** @type {HTMLElement} */ (p);
          }
          p = p.parentElement;
        }
      } catch { /* ignore */ }
      return element;
    }

    let rects = null;
    try { rects = element.getClientRects(); } catch { rects = null; }
    if (!rects || rects.length < 2) return element;

    // Only apply fragmentation heuristic for inline-ish elements.
    let display = '';
    try { display = String(window.getComputedStyle(element)?.display || ''); } catch { display = ''; }
    const inlineish = display.startsWith('inline');
    if (!inlineish) return element;

    // Compute union rect (viewport coords) of the fragmented element.
    let uLeft = Infinity, uTop = Infinity, uRight = -Infinity, uBottom = -Infinity;
    try {
      for (const r of rects) {
        uLeft = Math.min(uLeft, r.left);
        uTop = Math.min(uTop, r.top);
        uRight = Math.max(uRight, r.right);
        uBottom = Math.max(uBottom, r.bottom);
      }
    } catch {
      return element;
    }
    if (!Number.isFinite(uLeft) || !Number.isFinite(uTop) || !Number.isFinite(uRight) || !Number.isFinite(uBottom)) return element;

    // Search a small subtree for a single-rect box that best matches the visible area.
    // Prefer the largest-area candidate to get the wrapper (e.g. a flex div) rather than an <img>.
    let best = null;
    let bestArea = 0;
    let nodesVisited = 0;
    const maxNodes = 40;
    const maxDepth = 4;

    const queue = [{ el: element, depth: 0 }];
    while (queue.length && nodesVisited < maxNodes) {
      const cur = queue.shift();
      const curEl = cur?.el;
      const d = cur?.depth ?? 0;
      if (!curEl || curEl.nodeType !== 1) continue;
      nodesVisited++;

      if (curEl !== element) {
        let cr = null;
        try { cr = curEl.getClientRects(); } catch { cr = null; }
        if (cr && cr.length === 1) {
          const r0 = cr[0];
          const w = Number(r0.width);
          const h = Number(r0.height);
          const area = (Number.isFinite(w) ? w : 0) * (Number.isFinite(h) ? h : 0);
          if (area > bestArea) {
            // Ensure candidate overlaps union rect and is plausibly the visual wrapper.
            const overlaps = !(r0.right <= uLeft || r0.left >= uRight || r0.bottom <= uTop || r0.top >= uBottom);
            if (overlaps) {
              bestArea = area;
              best = curEl;
            }
          }
        }
      }

      if (d >= maxDepth) continue;
      try {
        const kids = curEl.children;
        if (kids && kids.length) {
          for (let i = 0; i < kids.length; i++) {
            const k = kids[i];
            if (k && k.nodeType === 1) queue.push({ el: /** @type {HTMLElement} */ (k), depth: d + 1 });
            if (queue.length > maxNodes) break;
          }
        }
      } catch { /* ignore */ }
    }

    return best || element;
  }

  /**
   * Strip KeyPilot focus ring classes/vars from a single element.
   * @param {Element|null|undefined} el
   */
  _stripFocusStylingFromElement(el) {
    if (!el || el.nodeType !== 1) return;
    try {
      el.classList.remove('keypilot-focus-element');
      el.classList.remove('keypilot-focus-element--inset');
      el.classList.remove('keypilot-focus-element--fill');
      try {
        el.removeAttribute('data-kp-focus');
        el.removeAttribute('data-kp-focus-inset');
      } catch { /* ignore */ }
      el.style.removeProperty('--keypilot-focus-ring-color');
      el.style.removeProperty('--keypilot-focus-ring-width');
      el.style.removeProperty('--keypilot-focus-shadow-color');
      el.style.removeProperty('--keypilot-focus-ring-bg-color');
      el.style.removeProperty('--keypilot-focus-box-shadow');
      el.style.removeProperty('filter');
    } catch { /* ignore */ }
  }

  /**
   * Clear focus styling from the currently styled element.
   * Also sweeps the owning open ShadowRoot (and document) for leftovers —
   * Lit re-renders / resolve-target swaps can strand the class on a node we
   * no longer hold in `_currentStyledElement`, which looks like "stuck" hover.
   *
   * @param {{ deep?: boolean }} [opts] - deep: walk all open shadow roots
   *   (used on full hide / mode switch to kill ghost rings on archive.org).
   */
  clearElementFocusStyling(opts = {}) {
    const deep = !!(opts && opts.deep);
    const primary = this._currentStyledElement;
    this._currentStyledElement = null;

    this._stripFocusStylingFromElement(primary);

    // Sweep the last styled tree scope for any stranded focus rings.
    let root = null;
    try {
      root = primary && typeof primary.getRootNode === 'function'
        ? primary.getRootNode()
        : null;
    } catch { root = null; }

    const roots = [];
    if (root && typeof root.querySelectorAll === 'function') roots.push(root);
    // Always also check the document — light-DOM leftovers.
    try { roots.push(document); } catch { /* ignore */ }

    for (const r of roots) {
      try {
        const stranded = r.querySelectorAll(
          '.keypilot-focus-element, .keypilot-focus-element--inset, [data-kp-focus]'
        );
        for (const el of stranded) {
          this._stripFocusStylingFromElement(el);
        }
      } catch { /* ignore */ }
    }

    if (deep) {
      try { this._deepStripFocusMarkers(document); } catch { /* ignore */ }
    }
  }

  /**
   * Walk open shadow trees and strip any leftover hover-ring markers.
   * @param {Document|ShadowRoot|Element} root
   * @param {number} [depth]
   */
  _deepStripFocusMarkers(root, depth = 0) {
    if (!root || depth > 24) return;

    try {
      if (typeof root.querySelectorAll === 'function') {
        const stranded = root.querySelectorAll(
          '.keypilot-focus-element, .keypilot-focus-element--inset, [data-kp-focus]'
        );
        for (const el of stranded) {
          this._stripFocusStylingFromElement(el);
        }
      }
    } catch { /* ignore */ }

    try {
      const base = (root.nodeType === 9) ? root.documentElement : root;
      if (!base) return;

      if (base.nodeType === 1 && base.shadowRoot) {
        this._deepStripFocusMarkers(base.shadowRoot, depth + 1);
      }

      const w = document.createTreeWalker(base, NodeFilter.SHOW_ELEMENT, null);
      let n;
      while ((n = w.nextNode())) {
        if (n.shadowRoot) {
          this._deepStripFocusMarkers(n.shadowRoot, depth + 1);
        }
      }
    } catch { /* ignore */ }
  }

  updateFocusOverlayDOM(element, mode = MODES.NONE, rectOverride = null) {
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateFocusOverlayDOM called with:', {
        element: element?.tagName,
        mode: mode,
        rectOverride: rectOverride,
        _useDomHoverFocusColors: this._useDomHoverFocusColors
      });
    }

    if (!element) {
      this.hideFocusOverlay();
      return;
    }

    // Don't outline modal/popover iframes. The top document cannot see inside an iframe,
    // so outlining the iframe itself is usually distracting (especially for extension-page
    // iframes like Settings/Guide).
    try {
      if (element.tagName === 'IFRAME') {
        const isPopoverIframe = this.popoverIframeElement && element === this.popoverIframeElement;
        const isModalIframe = !!(element.classList && element.classList.contains('modal-iframe'));
        if (isPopoverIframe || isModalIframe) {
          this.hideFocusOverlay();
          return;
        }
      }
    } catch { /* ignore */ }

    // Determine if this is a text input element
    const isTextInput = element.matches && element.matches(SELECTORS.FOCUSABLE_TEXT);
    const suppressFill = this.shouldSuppressFocusFill(element);

    // We'll use this rect both for sizing/positioning and for deciding whether to render a fill.
    const rect = (rectOverride && typeof rectOverride === 'object')
      ? rectOverride
      : this.getBestRect(element);
    // If the hover target is extremely large, a filled overlay becomes distracting; keep just the frame.
    const isVeryLarge = rect && rect.width > 512 && rect.height > 512;
    
    // Determine overlay color based on element type
    let borderColor, shadowColor, backgroundColor;
    if (isTextInput) {
      // Orange color for text inputs in both normal mode and text focus mode
      borderColor = COLORS.ORANGE;
      shadowColor = COLORS.ORANGE_SHADOW;
      backgroundColor = 'transparent';
    } else {
      const p = this._getNonTextFocusPalette();
      borderColor = p.borderColor;
      shadowColor = p.shadowColor;
      // Fill for thumbnails/links; suppress for scrubbers and players that have a seek bar.
      backgroundColor = (suppressFill || isVeryLarge) ? 'transparent' : p.backgroundColor;
    }

    // Settings-driven behavior for Click Mode focus rectangle.
    const {
      rectangleThickness,
      overlayFillEnabled,
      overlayShadowEnabled,
      focusColor
    } = this._getClickModeSettings();
    if (!isTextInput && !suppressFill && !isVeryLarge && overlayFillEnabled === false) {
      backgroundColor = 'transparent';
    }
    // Re-resolve palette colors when settings focusColor is available (non-text).
    if (!isTextInput) {
      const p = this._getNonTextFocusPalette(focusColor);
      borderColor = p.borderColor;
      shadowColor = p.shadowColor;
      if (overlayFillEnabled !== false && !suppressFill && !isVeryLarge) {
        backgroundColor = p.backgroundColor;
      }
    }

    // Debug logging
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateFocusOverlay called for:', {
        tagName: element.tagName,
        className: element.className,
        text: element.textContent?.substring(0, 30),
        mode: mode,
        isTextInput: isTextInput,
        suppressFill: suppressFill,
        isVeryLarge: isVeryLarge,
        borderColor: borderColor
      });
    }

    if (!this.focusOverlay) {
      this.focusOverlay = this.createElement('div', {
        className: CSS_CLASSES.FOCUS_OVERLAY,
        style: `
          position: fixed;
          left: 0;
          top: 0;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS};
          background: ${backgroundColor};
          will-change: transform, width, height;
        `
      });
      document.body.appendChild(this.focusOverlay);
      
      // Debug logging for overlay creation
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Focus overlay created and added to DOM:', {
          element: this.focusOverlay,
          className: this.focusOverlay.className,
          style: this.focusOverlay.style.cssText,
          parent: this.focusOverlay.parentElement?.tagName
        });
      }
      
      // Start observing the overlay for visibility optimization
      if (this.overlayObserver) {
        this.overlayObserver.observe(this.focusOverlay);
      }
    }

    // Update overlay colors based on current context
    // Ensure any previous fade-out is cancelled when we re-show/update the overlay.
    this.focusOverlay.style.opacity = '1';
    this.focusOverlay.style.border = `${rectangleThickness}px solid ${borderColor}`;
    this.focusOverlay.style.background = backgroundColor;
    if (overlayShadowEnabled === false) {
      this.focusOverlay.style.boxShadow = 'none';
    } else {
      const brightShadowColor = isTextInput
        ? COLORS.ORANGE_SHADOW
        : this._getNonTextFocusPalette(focusColor).shadowBrightColor;
      this.focusOverlay.style.boxShadow = `0 0 0 2px ${shadowColor}, 0 0 10px 2px ${brightShadowColor}`;
    }
    
    // Debug logging for positioning
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Focus overlay positioning:', {
        rect: rect,
        overlayExists: !!this.focusOverlay,
        overlayVisibility: this.overlayVisibility.focus
      });
    }
    
    if (rect.width > 0 && rect.height > 0) {
      // Position via transform to reduce layout work (fixed + translate3d).
      this.focusOverlay.style.transform = `translate3d(${rect.left}px, ${rect.top}px, 0)`;
      this.focusOverlay.style.width = `${rect.width}px`;
      this.focusOverlay.style.height = `${rect.height}px`;
      this.focusOverlay.style.display = 'block';
      this.focusOverlay.style.visibility = 'visible';
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Focus overlay positioned at:', {
          left: rect.left,
          top: rect.top,
          width: rect.width,
          height: rect.height
        });
      }
    } else {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Focus overlay hidden - invalid rect:', rect);
      }
      this.hideFocusOverlay();
    }
  }

  // Unified hide interface that switches between rendering modes
  hideFocusOverlay() {
    // When DOM hover mode is enabled, clear element styling only (no canvas ring).
    if (this._useDomHoverFocusColors) {
      this.clearElementFocusStyling({ deep: false });
      return;
    }

    switch (this.renderingMode) {
      case 'canvas':
        return this.hideFocusOverlayCanvas();
      case 'css-custom-props':
        return this.hideFocusOverlayCSSCustomProps();
      case 'dom':
      default:
        return this.hideFocusOverlayDOM();
    }
  }

  hideFocusOverlayDOM() {
    if (this.focusOverlay) {
      this.focusOverlay.style.display = 'none';
    }
  }

  /**
   * Set the rendering mode for overlays
   * @param {string} mode - 'dom', 'canvas', or 'css-custom-props'
   */
  setRenderingMode(mode) {
    if (!['dom', 'canvas', 'css-custom-props'].includes(mode)) {
      console.warn('[KeyPilot] Invalid rendering mode:', mode);
      return;
    }

    // Cleanup current renderer
    this.cleanupRenderingMode();

    // Set new mode and initialize
    this.renderingMode = mode;
    this.initRenderingMode();

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Rendering mode changed to:', mode);
    }
  }

  initRenderingMode() {
    switch (this.renderingMode) {
      case 'canvas':
        this.initCanvasRenderer();
        break;
      case 'css-custom-props':
        this.initCSSCustomPropsRenderer();
        break;
      case 'dom':
      default:
        // DOM renderer doesn't need explicit initialization
        break;
    }
  }

  cleanupRenderingMode() {
    switch (this.renderingMode) {
      case 'canvas':
        this.cleanupCanvasRenderer();
        break;
      case 'css-custom-props':
        this.cleanupCSSCustomPropsRenderer();
        break;
      case 'dom':
      default:
        // DOM renderer cleanup happens in cleanup() method
        break;
    }
  }

  /**
   * Fade out the focus overlay rect, then hide it.
   * Useful after clicks that mutate the DOM (accordions, menus, etc.) so we don't leave a stale rect.
   */
  fadeOutFocusOverlay(durationMs = 120) {
    if (this.renderingMode === 'dom' && this.focusOverlay) {
      if (this.focusOverlay.style.display === 'none') return;

      // Avoid stacking transitions; we'll reset after the fade completes.
      this.focusOverlay.style.transition = `opacity ${durationMs}ms ease-out`;
      this.focusOverlay.style.opacity = '0';

      window.setTimeout(() => {
        if (!this.focusOverlay) return;
        // Only hide if we're still faded out (another update may have brought it back).
        if (this.focusOverlay.style.opacity === '0') {
          this.hideFocusOverlay();
        }
        // Clear transition so other overlay updates don't inherit this timing.
        if (this.focusOverlay) {
          this.focusOverlay.style.transition = '';
        }
      }, durationMs);
    } else {
      // For canvas and CSS custom props, just hide immediately
      this.hideFocusOverlay();
    }
  }

  /**
   * Shared inspector outline for any pick tool kind.
   * @param {Element|null|undefined} element
   * @param {string|null|undefined} kind
   */
  updateInspectorOverlay(element, kind = null) {
    if (!element) {
      this.hideInspectorOverlay();
      return;
    }

    const def = getInspectorDef(kind);
    const border = def?.borderColor || COLORS.DELETE_RED;
    const shadow = def?.shadowColor || COLORS.DELETE_SHADOW;
    const shadowBright = def?.shadowBrightColor || COLORS.DELETE_SHADOW_BRIGHT;

    if (!this.inspectorOverlay) {
      this.inspectorOverlay = this.createElement('div', {
        className: CSS_CLASSES.INSPECTOR_OVERLAY || CSS_CLASSES.DELETE_OVERLAY,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS};
          border: 3px solid ${border};
          box-shadow: 0 0 0 2px ${shadow}, 0 0 12px 2px ${shadowBright};
          background: transparent;
          will-change: transform;
        `
      });
      document.body.appendChild(this.inspectorOverlay);
      // Back-compat alias
      this.deleteOverlay = this.inspectorOverlay;
      if (this.overlayObserver) {
        this.overlayObserver.observe(this.inspectorOverlay);
      }
    }

    // Restyle when kind changes
    if (kind && kind !== this._inspectorOverlayKind) {
      this._inspectorOverlayKind = kind;
      this.inspectorOverlay.style.border = `3px solid ${border}`;
      this.inspectorOverlay.style.boxShadow =
        `0 0 0 2px ${shadow}, 0 0 12px 2px ${shadowBright}`;
      try {
        this.inspectorOverlay.setAttribute('data-kp-inspector-kind', kind);
      } catch { /* ignore */ }
    }

    const rect = this.getBestRect(element);
    if (rect.width > 0 && rect.height > 0) {
      this.inspectorOverlay.style.left = `${rect.left}px`;
      this.inspectorOverlay.style.top = `${rect.top}px`;
      this.inspectorOverlay.style.width = `${rect.width}px`;
      this.inspectorOverlay.style.height = `${rect.height}px`;
      this.inspectorOverlay.style.display = 'block';
      this.inspectorOverlay.style.visibility = 'visible';
    } else {
      this.hideInspectorOverlay();
    }
  }

  hideInspectorOverlay() {
    if (this.inspectorOverlay) {
      this.inspectorOverlay.style.display = 'none';
    }
  }

  /**
   * Remember confirm-key / kind for the top-right instruction chip.
   * Call when entering inspector so updateOverlays can keep the chip visible.
   * @param {{ kind?: string|null, confirmKey?: string }} opts
   */
  setInspectorModeIndicatorOpts(opts = {}) {
    this._inspectorIndicatorOpts = {
      kind: opts.kind || null,
      confirmKey: opts.confirmKey || ''
    };
  }

  /**
   * Top-right companion instruction while inspector pick is active
   * (same pattern as Text Select "Press H again to finish selection").
   * @param {{ kind?: string|null, confirmKey?: string, message?: string }} [opts]
   */
  showInspectorModeIndicator(opts = {}) {
    const prev = this._inspectorIndicatorOpts || {};
    const kind = opts.kind || prev.kind || null;
    const confirmKey = opts.confirmKey || prev.confirmKey || '';
    if (opts.kind || opts.confirmKey) {
      this._inspectorIndicatorOpts = { kind, confirmKey };
    }

    const def = getInspectorDef(kind);
    const modeText = (opts.message && String(opts.message).trim())
      || getInspectorInstructionText(kind, confirmKey);

    const bg = def?.borderColor || COLORS.COLS_PURPLE;
    const shadow = def?.shadowColor || COLORS.COLS_SHADOW;

    if (this.inspectorModeIndicator) {
      this.inspectorModeIndicator.textContent = modeText;
      this.inspectorModeIndicator.style.display = 'block';
      this.inspectorModeIndicator.style.background = bg;
      this.inspectorModeIndicator.style.boxShadow = `0 2px 10px ${shadow}`;
      try {
        if (kind) this.inspectorModeIndicator.setAttribute('data-kp-inspector-kind', kind);
      } catch { /* ignore */ }
      return;
    }

    this.inspectorModeIndicator = this.createElement('div', {
      className: CSS_CLASSES.INSPECTOR_MODE_INDICATOR || 'kpv2-inspector-mode-indicator',
      style: `
        position: fixed;
        top: 20px;
        right: 20px;
        background: ${bg};
        color: white;
        padding: 10px 14px;
        font-size: 14px;
        font-weight: bold;
        font-family: ${KP_UI_FONT};
        border-radius: 6px;
        box-shadow: 0 2px 10px ${shadow};
        z-index: ${Z_INDEX.MESSAGE_BOX};
        pointer-events: none;
        max-width: min(360px, calc(100vw - 40px));
        line-height: 1.35;
        will-change: transform, opacity;
        animation: kpv2-pulse 1.5s ease-in-out infinite;
        letter-spacing: 0.01em;
      `
    });
    try {
      if (kind) this.inspectorModeIndicator.setAttribute('data-kp-inspector-kind', kind);
    } catch { /* ignore */ }
    this.inspectorModeIndicator.textContent = modeText;
    try {
      document.body.appendChild(this.inspectorModeIndicator);
    } catch {
      try { document.documentElement.appendChild(this.inspectorModeIndicator); } catch { /* ignore */ }
    }
  }

  hideInspectorModeIndicator() {
    if (this.inspectorModeIndicator) {
      try { this.inspectorModeIndicator.remove(); } catch { /* ignore */ }
      this.inspectorModeIndicator = null;
    }
    this._inspectorIndicatorOpts = null;
  }

  /** @deprecated use updateInspectorOverlay(el, kind) */
  updateDeleteOverlay(element) {
    this.updateInspectorOverlay(element, 'delete');
  }

  /** @deprecated use hideInspectorOverlay() */
  hideDeleteOverlay() {
    this.hideInspectorOverlay();
  }

  /** @deprecated use updateInspectorOverlay(el, 'cols') */
  updateColsOverlay(element) {
    this.updateInspectorOverlay(element, 'cols');
  }

  /** @deprecated use hideInspectorOverlay() */
  hideColsOverlay() {
    this.hideInspectorOverlay();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  updateHighlightRectangleOverlay(startPosition, currentPosition) {
    return this.highlightManager.updateHighlightRectangleOverlay(startPosition, currentPosition);
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  hideHighlightRectangleOverlay() {
    return this.highlightManager.hideHighlightRectangleOverlay();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  removeHighlightRectangleOverlay() {
    return this.highlightManager.removeHighlightRectangleOverlay();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  updateHighlightSelectionOverlays(selection) {
    return this.highlightManager.updateHighlightSelectionOverlays(selection);
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  clearHighlightSelectionOverlays() {
    return this.highlightManager.clearHighlightSelectionOverlays();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  setSelectionMode(mode) {
    return this.highlightManager.setSelectionMode(mode);
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  getSelectionMode() {
    return this.highlightManager.getSelectionMode();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  startCharacterSelection(position, findTextNodeAtPosition, getTextOffsetAtPosition) {
    return this.highlightManager.startCharacterSelection(position, findTextNodeAtPosition, getTextOffsetAtPosition);
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  updateCharacterSelection(currentPosition, startPosition, findTextNodeAtPosition, getTextOffsetAtPosition) {
    return this.highlightManager.updateCharacterSelection(currentPosition, startPosition, findTextNodeAtPosition, getTextOffsetAtPosition);
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  updateRectangleSelectionFromCarets(startPosition, currentPosition, findTextNodeAtPosition, getTextOffsetAtPosition) {
    return this.highlightManager.updateRectangleSelectionFromCarets(
      startPosition,
      currentPosition,
      findTextNodeAtPosition,
      getTextOffsetAtPosition
    );
  }

  /**
   * Live-refresh highlight rectangle + selection during scroll (document-anchored origin).
   */
  syncHighlightSelectionToScroll(currentViewportMouse, findTextNodeAtPosition, getTextOffsetAtPosition) {
    return this.highlightManager.syncSelectionToScroll(
      currentViewportMouse,
      findTextNodeAtPosition,
      getTextOffsetAtPosition
    );
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  peekCharacterSelectedText() {
    return this.highlightManager.peekCharacterSelectedText();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  completeCharacterSelection() {
    return this.highlightManager.completeCharacterSelection();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  clearCharacterSelection() {
    return this.highlightManager.clearCharacterSelection();
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  /**
   * Create selection overlays for a specific range with shadow DOM support
   * @param {Range} range - DOM Range object
   */
  createSelectionOverlaysForRangeWithShadowSupport(range) {
    if (!range || range.collapsed) {
      return;
    }

    try {
      // Get all rectangles for the range (handles multi-line selections)
      const rects = this.getClientRectsWithShadowSupport(range);
      
      for (let i = 0; i < rects.length; i++) {
        const rect = rects[i];
        
        // Skip zero-width or zero-height rectangles
        if (rect.width <= 0 || rect.height <= 0) {
          continue;
        }

        // Create overlay element for this rectangle
        const overlay = this.createElement('div', {
          className: CSS_CLASSES.HIGHLIGHT_SELECTION,
          style: `
            position: fixed;
            left: ${rect.left}px;
            top: ${rect.top}px;
            width: ${rect.width}px;
            height: ${rect.height}px;
            background: ${COLORS.HIGHLIGHT_SELECTION_BG};
            border: 1px solid ${COLORS.HIGHLIGHT_SELECTION_BORDER};
            pointer-events: none;
            z-index: ${Z_INDEX.OVERLAYS_BELOW};
            will-change: transform;
          `
        });

        // Append to main document body (overlays should always be in main document)
        document.body.appendChild(overlay);
        this.highlightSelectionOverlays.push(overlay);

        // Start observing the overlay for visibility optimization
        if (this.overlayObserver) {
          this.overlayObserver.observe(overlay);
        }
      }

      if (window.KEYPILOT_DEBUG && rects.length > 0) {
        console.log('[KeyPilot Debug] Created selection overlays for range with shadow DOM support:', {
          rectCount: rects.length,
          firstRect: {
            left: rects[0].left,
            top: rects[0].top,
            width: rects[0].width,
            height: rects[0].height
          }
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error creating selection overlays for range with shadow DOM support:', error);
    }
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  /**
   * Get client rectangles for a range with shadow DOM support
   * @param {Range} range - DOM Range object
   * @returns {DOMRectList|Array} - Client rectangles
   */
  getClientRectsWithShadowSupport(range) {
    try {
      // First try the standard method
      const rects = range.getClientRects();
      if (rects && rects.length > 0) {
        return rects;
      }

      // If no rectangles found, try alternative methods for shadow DOM
      return this.getAlternativeClientRects(range);
    } catch (error) {
      console.warn('[KeyPilot] Error getting client rects with shadow DOM support:', error);
      return [];
    }
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  /**
   * Get alternative client rectangles for shadow DOM ranges
   * @param {Range} range - DOM Range object
   * @returns {Array} - Array of rectangle objects
   */
  getAlternativeClientRects(range) {
    try {
      const rects = [];
      
      // Try to get bounding rect as fallback
      const boundingRect = range.getBoundingClientRect();
      if (boundingRect && boundingRect.width > 0 && boundingRect.height > 0) {
        rects.push(boundingRect);
      }
      
      // For shadow DOM, we might need to manually calculate rectangles
      // by walking through the range contents
      if (rects.length === 0) {
        const shadowRects = this.calculateShadowDOMRects(range);
        rects.push(...shadowRects);
      }
      
      return rects;
    } catch (error) {
      console.warn('[KeyPilot] Error getting alternative client rects:', error);
      return [];
    }
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  /**
   * Calculate rectangles for shadow DOM ranges manually
   * @param {Range} range - DOM Range object
   * @returns {Array} - Array of rectangle objects
   */
  calculateShadowDOMRects(range) {
    try {
      const rects = [];
      
      // Get start and end containers
      const startContainer = range.startContainer;
      const endContainer = range.endContainer;
      
      if (startContainer === endContainer && startContainer.nodeType === Node.TEXT_NODE) {
        // Single text node selection
        const textRect = this.getTextNodeRect(startContainer, range.startOffset, range.endOffset);
        if (textRect) {
          rects.push(textRect);
        }
      } else {
        // Multi-node selection - this is more complex for shadow DOM
        // For now, use bounding rect as approximation
        try {
          const boundingRect = range.getBoundingClientRect();
          if (boundingRect && boundingRect.width > 0 && boundingRect.height > 0) {
            rects.push(boundingRect);
          }
        } catch (error) {
          // Ignore errors in complex shadow DOM scenarios
        }
      }
      
      return rects;
    } catch (error) {
      console.warn('[KeyPilot] Error calculating shadow DOM rects:', error);
      return [];
    }
  }

  // SELECTION RECTANGLE FUNCTIONALITY ONLY
  /**
   * Get rectangle for a portion of a text node
   * @param {Text} textNode - Text node
   * @param {number} startOffset - Start character offset
   * @param {number} endOffset - End character offset
   * @returns {DOMRect|null} - Rectangle or null
   */
  getTextNodeRect(textNode, startOffset, endOffset) {
    try {
      const ownerDocument = textNode.ownerDocument || document;
      const tempRange = ownerDocument.createRange();
      tempRange.setStart(textNode, startOffset);
      tempRange.setEnd(textNode, endOffset);
      
      const rect = tempRange.getBoundingClientRect();
      return rect.width > 0 && rect.height > 0 ? rect : null;
    } catch (error) {
      console.warn('[KeyPilot] Error getting text node rect:', error);
      return null;
    }
  }

  /**
   * Create selection overlays for a specific range (legacy method for compatibility)
   * @param {Range} range - DOM Range object
   */
  createSelectionOverlaysForRange(range) {
    // Delegate to the shadow DOM-aware method
    this.createSelectionOverlaysForRangeWithShadowSupport(range);
  }



  updateFocusedTextOverlay(element) {
    console.log('[KeyPilot] updateFocusedTextOverlay called with element:', element?.tagName, element?.type);

    if (!element) {
      console.log('[KeyPilot] No element provided, hiding overlay');
      this.hideFocusedTextOverlay();
      return;
    }

    console.log('[KeyPilot] Creating focused text overlay');
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateFocusedTextOverlay called for:', {
        tagName: element.tagName,
        className: element.className,
        id: element.id
      });
    }

    if (!this.focusedTextOverlay) {
      this.focusedTextOverlay = this.createElement('div', {
        className: CSS_CLASSES.FOCUSED_TEXT_OVERLAY || 'kpv2-focused-text-overlay',
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_BELOW};
          background: transparent;
          will-change: transform;
        `
      });
      document.body.appendChild(this.focusedTextOverlay);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Focused text overlay created and added to DOM:', {
          element: this.focusedTextOverlay,
          className: this.focusedTextOverlay.className,
          parent: this.focusedTextOverlay.parentElement?.tagName
        });
      }
      
      // Start observing the overlay for visibility optimization
      if (this.overlayObserver) {
        this.overlayObserver.observe(this.focusedTextOverlay);
      }
    }

    // Darkened orange color for focused text fields
    const borderColor = COLORS.ORANGE_SHADOW_DARK; // Slightly more opaque
    const shadowColor = COLORS.ORANGE_SHADOW_LIGHT; // Darker shadow
    
    const { strokeThickness } = this._getTextModeSettings();
    this.focusedTextOverlay.style.border = `${strokeThickness}px solid ${borderColor}`;
    this.focusedTextOverlay.style.boxShadow = `0 0 0 2px ${shadowColor}, 0 0 10px 2px ${COLORS.ORANGE_BORDER}`;

    // Always get fresh rect to handle dynamic position/size changes
    const rect = this.getBestRect(element);
    
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Focused text overlay positioning:', {
        rect: rect,
        overlayExists: !!this.focusedTextOverlay,
        overlayVisibility: this.overlayVisibility.focusedText,
        timestamp: Date.now()
      });
    }
    
    if (rect.width > 0 && rect.height > 0) {
      // Position the overlay with fresh coordinates
      this.focusedTextOverlay.style.left = `${rect.left}px`;
      this.focusedTextOverlay.style.top = `${rect.top}px`;
      this.focusedTextOverlay.style.width = `${rect.width}px`;
      this.focusedTextOverlay.style.height = `${rect.height}px`;
      this.focusedTextOverlay.style.display = 'block';
      this.focusedTextOverlay.style.visibility = 'visible';
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Focused text overlay positioned at:', {
          left: rect.left,
          top: rect.top,
          width: rect.width,
          height: rect.height,
          timestamp: Date.now()
        });
      }
    } else {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Focused text overlay hidden - invalid rect:', rect);
      }
      this.hideFocusedTextOverlay();
    }
  }

  hideFocusedTextOverlay() {
    if (this.focusedTextOverlay) {
      this.focusedTextOverlay.style.display = 'none';
    }
  }

  updateActiveTextInputFrame(element) {
    if (!element) {
      this.hideActiveTextInputFrame();
      return;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateActiveTextInputFrame called for:', {
        tagName: element.tagName,
        className: element.className,
        id: element.id
      });
    }

    if (!this.activeTextInputFrame) {
      this.activeTextInputFrame = this.createElement('div', {
        className: CSS_CLASSES.ACTIVE_TEXT_INPUT_FRAME,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_ABOVE};
          background: transparent;
          will-change: transform, opacity;
        `
      });
      document.body.appendChild(this.activeTextInputFrame);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Active text input frame created and added to DOM:', {
          element: this.activeTextInputFrame,
          className: this.activeTextInputFrame.className,
          parent: this.activeTextInputFrame.parentElement?.tagName
        });
      }
      
      // Start observing the overlay for visibility optimization
      if (this.overlayObserver) {
        this.overlayObserver.observe(this.activeTextInputFrame);
      }
    }

    // Always get fresh rect to handle dynamic position/size changes
    const rect = this.getBestRect(element);
    
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Active text input frame positioning:', {
        rect: rect,
        overlayExists: !!this.activeTextInputFrame,
        overlayVisibility: this.overlayVisibility.activeTextInput,
        timestamp: Date.now()
      });
    }
    
    if (rect.width > 0 && rect.height > 0) {
      // Position the pulsing frame with fresh coordinates
      this.activeTextInputFrame.style.left = `${rect.left}px`;
      this.activeTextInputFrame.style.top = `${rect.top}px`;
      this.activeTextInputFrame.style.width = `${rect.width}px`;
      this.activeTextInputFrame.style.height = `${rect.height}px`;
      this.activeTextInputFrame.style.display = 'block';
      this.activeTextInputFrame.style.visibility = 'visible';
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Active text input frame positioned at:', {
          left: rect.left,
          top: rect.top,
          width: rect.width,
          height: rect.height,
          timestamp: Date.now()
        });
      }
    } else {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Active text input frame hidden - invalid rect:', rect);
      }
      this.hideActiveTextInputFrame();
    }
  }

  hideActiveTextInputFrame() {
    if (this.activeTextInputFrame) {
      this.activeTextInputFrame.style.display = 'none';
    }
  }

  calculateLabelPosition(elementRect, labelHeight) {
    const viewportHeight = window.innerHeight;
    const viewportWidth = window.innerWidth;
    
    // Try placing below the element first
    const belowPosition = {
      left: elementRect.left,
      top: elementRect.top + elementRect.height + 8,
      position: 'below'
    };
    
    // Check if below position is off-screen
    if (belowPosition.top + labelHeight > viewportHeight) {
      // Try above the element
      const abovePosition = {
        left: elementRect.left,
        top: elementRect.top - labelHeight - 8,
        position: 'above'
      };
      
      if (abovePosition.top < 0) {
        // Try right side if both above/below don't work
        return {
          left: elementRect.left + elementRect.width + 8,
          top: elementRect.top,
          position: 'right'
        };
      }
      return abovePosition;
    }
    return belowPosition;
  }

  updateEscExitLabelText(element) {
    if (!element) {
      this.hideEscExitLabelText();
      return;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateEscExitLabelText called for:', {
        tagName: element.tagName,
        className: element.className,
        id: element.id
      });
    }

    if (!this.escExitLabelText) {
      this.escExitLabelText = this.createElement('div', {
        className: CSS_CLASSES.ESC_EXIT_LABEL,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_ABOVE};
          will-change: transform, opacity;
          font-family: Arial, sans-serif;
          font-size: 14px;
          padding: 4px 8px;
          border-radius: 4px;
          white-space: nowrap;
          background-color: ${COLORS.ORANGE_BG};
          color: ${COLORS.ORANGE_TEXT};
          border: 1px solid ${COLORS.ORANGE_BORDER};
        `
      });
      this.escExitLabelText.innerHTML = 'Press <kbd>ESC</kbd> to exit';
      document.body.appendChild(this.escExitLabelText);
      this.labelHeight = this.escExitLabelText.offsetHeight;
      if (this.overlayObserver) this.overlayObserver.observe(this.escExitLabelText);
    }

    const rect = this.getBestRect(element);
    if (rect.width > 0 && rect.height > 0) {
      const position = this.calculateLabelPosition(rect, this.labelHeight);
      this.escExitLabelText.style.left = `${position.left}px`;
      this.escExitLabelText.style.top = `${position.top}px`;
      this.escExitLabelText.style.display = 'block';
      this.escExitLabelText.style.visibility = 'visible';
    } else {
      this.hideEscExitLabelText();
    }
  }

  updateEscExitLabelHover(element) {
    if (!element) {
      this.hideEscExitLabelHover();
      return;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] updateEscExitLabelHover called for:', {
        tagName: element.tagName,
        className: element.className,
        id: element.id
      });
    }

    if (!this.escExitLabelHover) {
      this.escExitLabelHover = this.createElement('div', {
        className: CSS_CLASSES.ESC_EXIT_LABEL,
        style: `
          position: fixed;
          pointer-events: none;
          z-index: ${Z_INDEX.OVERLAYS_ABOVE};
          will-change: transform, opacity;
          font-family: Arial, sans-serif;
          font-size: 14px;
          padding: 4px 8px;
          border-radius: 4px;
          white-space: nowrap;
          background-color: ${COLORS.FOCUS_GREEN_BG};
          color: ${COLORS.FOCUS_GREEN_TEXT};
          border: 1px solid ${COLORS.FOCUS_GREEN};
        `
      });
      this.escExitLabelHover.innerHTML = this.formatHoverLabelText(this.hoverClickLabelText || 'F clicks');
      document.body.appendChild(this.escExitLabelHover);
      this.labelHeight = this.escExitLabelHover.offsetHeight;
      if (this.overlayObserver) this.overlayObserver.observe(this.escExitLabelHover);
    } else {
      // Keep label text fresh (e.g. countdown updates) even if element is reused.
      this.escExitLabelHover.innerHTML = this.formatHoverLabelText(this.hoverClickLabelText || 'F clicks');
    }

    const rect = this.getBestRect(element);
    if (rect.width > 0 && rect.height > 0) {
      const position = this.calculateLabelPosition(rect, this.labelHeight);
      this.escExitLabelHover.style.left = `${position.left}px`;
      this.escExitLabelHover.style.top = `${position.top}px`;
      this.escExitLabelHover.style.display = 'block';
      this.escExitLabelHover.style.visibility = 'visible';
    } else {
      this.hideEscExitLabelHover();
    }
  }

  hideEscExitLabelText() {
    if (this.escExitLabelText) this.escExitLabelText.style.display = 'none';
  }

  hideEscExitLabelHover() {
    if (this.escExitLabelHover) this.escExitLabelHover.style.display = 'none';
  }

  hideEscExitLabel() {
    this.hideEscExitLabelText();
    this.hideEscExitLabelHover();
  }

  /**
   * Host-element class paint for focus + inspector hover.
   * @param {Element|null} focusEl
   * @param {Element|null} inspectorEl
   * @param {Element|null} prevFocusEl
   * @param {Element|null} prevInspectorEl
   * @param {string|null} [inspectorKind]
   */
  updateElementClasses(focusEl, inspectorEl, prevFocusEl, prevInspectorEl, inspectorKind = null) {
    const hostClasses = getAllInspectorHostClasses();

    // Remove previous classes
    if (prevFocusEl && prevFocusEl !== focusEl) {
      prevFocusEl.classList.remove(CSS_CLASSES.FOCUS);
    }
    if (prevInspectorEl && prevInspectorEl !== inspectorEl) {
      for (const c of hostClasses) {
        try { prevInspectorEl.classList.remove(c); } catch { /* ignore */ }
      }
    }

    // Add new classes (ensure shadow styles first so brightness filter applies in open roots)
    if (focusEl) {
      this._ensureStylesForElement(focusEl);
      focusEl.classList.add(CSS_CLASSES.FOCUS);
    }
    if (inspectorEl) {
      this._ensureStylesForElement(inspectorEl);
      const def = getInspectorDef(inspectorKind);
      try { inspectorEl.classList.add(CSS_CLASSES.INSPECTOR); } catch { /* ignore */ }
      if (def?.hostClass) {
        try { inspectorEl.classList.add(def.hostClass); } catch { /* ignore */ }
      }
    }
  }

  getBestRect(element) {
    if (!element) return { left: 0, top: 0, width: 0, height: 0 };
    
    let rect = element.getBoundingClientRect();
    
    // Debug logging
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] getBestRect for element:', {
        tagName: element.tagName,
        originalRect: {
          left: rect.left,
          top: rect.top,
          width: rect.width,
          height: rect.height
        }
      });
    }
    
    // If the element has no height (common with links containing other elements),
    // try to find a child element with height
    if (rect.height === 0 && element.children.length > 0) {
      for (const child of element.children) {
        const childRect = child.getBoundingClientRect();
        if (childRect.height > 0) {
          // Use the child's rect but keep the parent's left position if it's a link
          if (element.tagName.toLowerCase() === 'a') {
            const finalRect = {
              left: Math.min(rect.left, childRect.left),
              top: childRect.top,
              width: Math.max(rect.width, childRect.width),
              height: childRect.height
            };
            if (window.KEYPILOT_DEBUG) {
              console.log('[KeyPilot Debug] Using child rect for link:', finalRect);
            }
            return finalRect;
          }
          if (window.KEYPILOT_DEBUG) {
            console.log('[KeyPilot Debug] Using child rect:', childRect);
          }
          return childRect;
        }
      }
    }
    
    // If still no height, try to get text content dimensions
    if (rect.height === 0 && element.textContent && element.textContent.trim()) {
      // For text-only elements, use a minimum height
      const finalRect = {
        left: rect.left,
        top: rect.top,
        width: Math.max(rect.width, 20), // Minimum width
        height: Math.max(rect.height, 20) // Minimum height
      };
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Using minimum dimensions:', finalRect);
      }
      return finalRect;
    }
    
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Using original rect:', rect);
    }
    return rect;
  }

  /**
   * Read a non-zero computed border-radius from an element.
   * @param {Element|null|undefined} el
   * @returns {string|null}
   */
  _readNonZeroBorderRadius(el) {
    try {
      if (!el || el.nodeType !== 1) return null;
      const cs = window.getComputedStyle(el);
      if (!cs) return null;

      let radius = String(cs.borderRadius || '').trim();
      if (!radius) {
        const tl = cs.borderTopLeftRadius || '0';
        const tr = cs.borderTopRightRadius || '0';
        const br = cs.borderBottomRightRadius || '0';
        const bl = cs.borderBottomLeftRadius || '0';
        radius = `${tl} ${tr} ${br} ${bl}`.trim();
      }
      if (!radius) return null;

      // Collapse "0px 0px 0px 0px" / "0" / "0%" to null so callers can try children.
      const tokens = radius.replace(/,/g, ' ').split(/\s+/).filter(Boolean);
      if (
        tokens.length > 0 &&
        tokens.every((t) => /^(0|0px|0%)$/i.test(t))
      ) {
        return null;
      }
      return radius;
    } catch {
      return null;
    }
  }

  /**
   * Resolve a CSS border-radius that matches the visual shape of the activation target.
   * Prefer the element itself; if it is not rounded (common for <a> wrapping a card/image),
   * use a large rounded descendant that fills most of the host box.
   *
   * @param {Element|null|undefined} element
   * @returns {string|null} CSS border-radius value, or null to keep stylesheet default
   */
  _resolveElementBorderRadius(element) {
    if (!element || element.nodeType !== 1) return null;

    const own = this._readNonZeroBorderRadius(element);
    if (own) return own;

    try {
      let parentArea = 0;
      try {
        const pr = element.getBoundingClientRect();
        parentArea = Math.max(0, (pr.width || 0) * (pr.height || 0));
      } catch {
        parentArea = 0;
      }

      const candidates = [];
      try {
        if (element.children && element.children.length) {
          for (const child of element.children) candidates.push(child);
        }
      } catch { /* ignore */ }

      // Media often carries the visible corner radius on image links / cards.
      try {
        const media = element.querySelectorAll?.('img, svg, video, picture');
        if (media && media.length) {
          for (const m of media) candidates.push(m);
        }
      } catch { /* ignore */ }

      let best = null;
      let bestArea = 0;
      for (const c of candidates) {
        const radius = this._readNonZeroBorderRadius(c);
        if (!radius) continue;
        let area = 0;
        try {
          const cr = c.getBoundingClientRect();
          area = Math.max(0, (cr.width || 0) * (cr.height || 0));
        } catch {
          area = 0;
        }
        if (area >= bestArea) {
          bestArea = area;
          best = radius;
        }
      }

      // Avoid picking a tiny decorative rounded icon inside a larger link.
      if (best && (parentArea <= 0 || bestArea >= parentArea * 0.35)) {
        return best;
      }
    } catch { /* ignore */ }

    return null;
  }

  /**
   * Resolve the viewport rect of the current focus outline for activation feedback.
   * Works across DOM-hover element styling, DOM overlay, CSS-custom-props, and canvas modes.
   * @returns {{ left: number, top: number, width: number, height: number }|null}
   */
  _getFocusPulseRect() {
    // Prefer live geometry from active visuals.
    try {
      if (this.focusOverlay && this.focusOverlay.style.display !== 'none') {
        const r = this.focusOverlay.getBoundingClientRect();
        if (r && r.width > 0 && r.height > 0) {
          return { left: r.left, top: r.top, width: r.width, height: r.height };
        }
      }
    } catch { /* ignore */ }

    try {
      if (this.cssCustomPropsOverlay && this.cssCustomPropsOverlay.style.display !== 'none') {
        const r = this.cssCustomPropsOverlay.getBoundingClientRect();
        if (r && r.width > 0 && r.height > 0) {
          return { left: r.left, top: r.top, width: r.width, height: r.height };
        }
      }
    } catch { /* ignore */ }

    try {
      const el = this._currentStyledElement || this._lastFocusElement;
      if (el && el.nodeType === 1 && el.isConnected) {
        const r = this.getBestRect(el);
        if (r && r.width > 0 && r.height > 0) {
          return { left: r.left, top: r.top, width: r.width, height: r.height };
        }
      }
    } catch { /* ignore */ }

    // Last known rect from hover tracking (may be slightly stale after scroll).
    if (this._lastFocusRect && this._lastFocusRect.width > 0 && this._lastFocusRect.height > 0) {
      return { ...this._lastFocusRect };
    }
    return null;
  }

  /**
   * Install one-shot page lifecycle hooks that wipe fixed-position click effects
   * during navigation / hide (SPA soft-nav, full load, bfcache, tab background).
   */
  _installEphemeralEffectLifecycle() {
    if (this._ephemeralEffectLifecycleInstalled) return;
    this._ephemeralEffectLifecycleInstalled = true;

    const clear = () => {
      try { this.clearEphemeralEffects(); } catch { /* ignore */ }
    };

    /** @type {Array<[EventTarget, string, EventListenerOrEventListenerObject, boolean|AddEventListenerOptions|undefined]>} */
    const bindings = [];
    const bind = (target, type, handler, options) => {
      try {
        target.addEventListener(type, handler, options);
        bindings.push([target, type, handler, options]);
      } catch { /* ignore */ }
    };

    bind(window, 'pagehide', clear, true);
    bind(window, 'beforeunload', clear, true);
    bind(window, 'popstate', clear, true);
    bind(window, 'hashchange', clear, true);
    bind(document, 'visibilitychange', () => {
      if (document.visibilityState === 'hidden') clear();
    }, true);
    bind(window, 'pageshow', (e) => {
      // bfcache restore can leave stale fixed ghosts from the prior visit.
      if (e && e.persisted) clear();
    }, true);

    // Same-document SPA navigations: clear only after the history entry changes.
    // (Avoid Navigation API "navigate" — it fires at click time and would kill
    // the intentional flash before the user sees it.)
    // popstate/hashchange already bound; also poll while effects are live via rAF.
    try {
      this._ephemeralLastHref = String(location.href || '');
      this._ephemeralOnUrlMaybeChanged = () => {
        const next = String(location.href || '');
        if (next !== this._ephemeralLastHref) {
          this._ephemeralLastHref = next;
          clear();
        }
      };
    } catch { /* ignore */ }

    this._ephemeralEffectLifecycleDispose = () => {
      try {
        this._ephemeralLastHref = null;
        this._ephemeralOnUrlMaybeChanged = null;
      } catch { /* ignore */ }
      for (const [target, type, handler, options] of bindings) {
        try { target.removeEventListener(type, handler, options); } catch { /* ignore */ }
      }
      bindings.length = 0;
      this._ephemeralEffectLifecycleInstalled = false;
      this._ephemeralEffectLifecycleDispose = null;
    };
  }

  /**
   * Remove every temporary click / image-copy effect overlay immediately.
   */
  clearEphemeralEffects() {
    const entries = Array.from(this._activeEphemeralEffects || []);
    for (const entry of entries) {
      try { entry.teardown?.(); } catch { /* ignore */ }
    }
    this._activeEphemeralEffects?.clear?.();
  }

  /**
   * Track a temporary fixed overlay so it is removed when the source target is
   * gone/hidden or the page navigates — not only when the CSS animation ends.
   *
   * @param {Element} pulse
   * @param {Element|null|undefined} sourceEl
   * @param {{ left: number, top: number, width: number, height: number }|null|undefined} originRect
   * @param {number} cleanupMs
   */
  _trackEphemeralEffect(pulse, sourceEl, originRect, cleanupMs) {
    if (!pulse) return;
    this._installEphemeralEffectLifecycle();

    const entry = {
      pulse,
      sourceEl: sourceEl && sourceEl.nodeType === 1 ? sourceEl : null,
      originRect: originRect
        ? {
            left: originRect.left,
            top: originRect.top,
            width: originRect.width,
            height: originRect.height
          }
        : null,
      rafId: 0,
      timeoutId: 0,
      io: /** @type {IntersectionObserver|null} */ (null),
      teardown: () => {}
    };

    let tornDown = false;
    const teardown = () => {
      if (tornDown) return;
      tornDown = true;
      this._activeEphemeralEffects.delete(entry);
      if (entry.rafId) {
        try { cancelAnimationFrame(entry.rafId); } catch { /* ignore */ }
        entry.rafId = 0;
      }
      if (entry.timeoutId) {
        try { clearTimeout(entry.timeoutId); } catch { /* ignore */ }
        entry.timeoutId = 0;
      }
      if (entry.io) {
        try { entry.io.disconnect(); } catch { /* ignore */ }
        entry.io = null;
      }
      try {
        if (pulse.isConnected) pulse.remove();
      } catch { /* ignore */ }
    };
    entry.teardown = teardown;
    this._activeEphemeralEffects.add(entry);

    // Source left the viewport / was covered away → drop the ghost immediately.
    if (entry.sourceEl) {
      try {
        entry.io = new IntersectionObserver(
          (records) => {
            for (const r of records) {
              if (!r.isIntersecting || r.intersectionRatio <= 0) {
                teardown();
                return;
              }
            }
          },
          { root: null, threshold: 0 }
        );
        entry.io.observe(entry.sourceEl);
      } catch {
        entry.io = null;
      }
    }

    // Poll for disconnect / zero-size / large layout jump (SPA transition, scroll-away).
    const origin = entry.originRect;
    const tick = () => {
      if (tornDown) return;

      // SPA soft-nav often keeps the document alive but changes the URL.
      try {
        const onUrl = this._ephemeralOnUrlMaybeChanged;
        if (typeof onUrl === 'function') onUrl();
        if (tornDown) return;
      } catch { /* ignore */ }

      const src = entry.sourceEl;
      if (src) {
        if (!src.isConnected) {
          teardown();
          return;
        }
        try {
          const r = src.getBoundingClientRect();
          if (!(r.width > 0) || !(r.height > 0)) {
            teardown();
            return;
          }
          // Source moved far from the frozen ghost rect → page is transitioning.
          if (origin) {
            const dx = Math.abs(r.left - origin.left);
            const dy = Math.abs(r.top - origin.top);
            const dw = Math.abs(r.width - origin.width);
            const dh = Math.abs(r.height - origin.height);
            if (dx > 48 || dy > 48 || dw > 64 || dh > 64) {
              teardown();
              return;
            }
          }
          // Covered / opacity-0 during view transitions.
          try {
            const cs = window.getComputedStyle(src);
            if (cs) {
              const op = parseFloat(cs.opacity);
              if (cs.display === 'none' || cs.visibility === 'hidden' || (Number.isFinite(op) && op < 0.05)) {
                teardown();
                return;
              }
            }
          } catch { /* ignore */ }
        } catch {
          teardown();
          return;
        }
      }
      // Host document gone / pulse orphaned.
      if (!pulse.isConnected) {
        teardown();
        return;
      }
      entry.rafId = requestAnimationFrame(tick);
    };
    entry.rafId = requestAnimationFrame(tick);

    try {
      pulse.addEventListener('animationend', teardown, { once: true });
    } catch { /* ignore */ }

    const ms = Number.isFinite(cleanupMs) ? Math.max(200, cleanupMs) : 1000;
    entry.timeoutId = setTimeout(teardown, ms);
  }

  /**
   * Approximate border-radius in CSS px for SVG rx/ry from a CSS border-radius string.
   * @param {string|null|undefined} borderRadius
   * @param {number} width
   * @param {number} height
   * @returns {number}
   */
  _borderRadiusToSvgRx(borderRadius, width, height) {
    if (!borderRadius) return 3;
    const first = String(borderRadius).trim().split(/\s+/)[0] || '';
    if (!first) return 3;
    if (first.endsWith('%')) {
      const p = parseFloat(first);
      if (!Number.isFinite(p)) return 3;
      return Math.max(0, Math.min(width, height) * (p / 100));
    }
    const n = parseFloat(first);
    return Number.isFinite(n) ? Math.max(0, n) : 3;
  }

  /**
   * Map clickEffect id → CSS class + safety cleanup ms.
   * @param {string} clickEffect
   * @returns {{ className: string, cleanupMs: number } | null}
   */
  _clickEffectPresentation(clickEffect) {
    switch (clickEffect) {
      case 'flash':
        return { className: CSS_CLASSES.FOCUS_FLASH, cleanupMs: 500 };
      case 'scale':
        return { className: CSS_CLASSES.FOCUS_PULSE, cleanupMs: 800 };
      case 'marquee':
        return { className: CSS_CLASSES.FOCUS_MARQUEE, cleanupMs: 1200 };
      case 'dash':
        return { className: CSS_CLASSES.FOCUS_DASH, cleanupMs: 1100 };
      default:
        return null;
    }
  }

  /**
   * Build the dash-chase SVG overlay (dashed stroke travels the perimeter).
   * @param {{ left: number, top: number, width: number, height: number }} rect
   * @param {string|null|undefined} borderRadius
   * @returns {SVGSVGElement}
   */
  _createDashChasePulse(rect, borderRadius) {
    const w = Math.max(1, rect.width);
    const h = Math.max(1, rect.height);
    const stroke = 3;
    const inset = stroke / 2;
    const rw = Math.max(1, w - stroke);
    const rh = Math.max(1, h - stroke);
    const rx = Math.min(this._borderRadiusToSvgRx(borderRadius, w, h), rw / 2, rh / 2);

    const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    svg.setAttribute('class', CSS_CLASSES.FOCUS_DASH);
    svg.setAttribute('aria-hidden', 'true');
    svg.setAttribute('data-kp-ephemeral-effect', 'dash');
    svg.setAttribute('width', String(w));
    svg.setAttribute('height', String(h));
    svg.style.left = `${rect.left}px`;
    svg.style.top = `${rect.top}px`;
    svg.style.width = `${w}px`;
    svg.style.height = `${h}px`;

    const shape = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
    shape.setAttribute('x', String(inset));
    shape.setAttribute('y', String(inset));
    shape.setAttribute('width', String(rw));
    shape.setAttribute('height', String(rh));
    shape.setAttribute('rx', String(rx));
    shape.setAttribute('ry', String(rx));
    shape.setAttribute('fill', 'none');
    shape.setAttribute('stroke', COLORS.FLASH_GREEN);
    shape.setAttribute('stroke-width', String(stroke));
    shape.setAttribute('stroke-linecap', 'round');
    shape.setAttribute('vector-effect', 'non-scaling-stroke');

    // Perimeter of rounded rect (good enough for dash patterning).
    const straight = 2 * (Math.max(0, rw - 2 * rx) + Math.max(0, rh - 2 * rx));
    const corners = 2 * Math.PI * rx;
    const peri = Math.max(24, straight + corners);
    const dash = Math.max(10, Math.min(36, peri * 0.09));
    const gap = Math.max(6, Math.min(22, peri * 0.055));
    shape.setAttribute('stroke-dasharray', `${dash} ${gap}`);
    shape.style.setProperty('--kp-dash-peri', String(peri));
    shape.classList.add(`${CSS_CLASSES.FOCUS_DASH}-stroke`);

    svg.appendChild(shape);
    return svg;
  }

  /**
   * F-key activation feedback for link-style targets.
   * Uses a temporary floating rectangle so it works in every render mode
   * (including DOM-hover element styling, where there is no focusOverlay div).
   *
   * Effect style comes from settings (clickMode.clickEffect):
   *   - flash (default): hard strobe on the outline
   *   - dash: dashed border chases around the perimeter
   *   - marquee: solid chaser light travels around the perimeter
   *   - scale: outline expands and fades out
   *   - none: no animation
   *
   * Whether any effect runs is decided solely by CLICKABLE_CATEGORY via
   * ElementDetector.isLinkStyleCategory (LINK + GENERIC only). Sliders, media,
   * buttons, text, and controls never get the pulse.
   *
   * @param {Element|null} [activationTarget] - Element that was F-activated.
   *   Prefer this over last-hover focus so category matches the real target.
   */
  flashFocusOverlay(activationTarget = null) {
    const el = (activationTarget && activationTarget.nodeType === 1)
      ? activationTarget
      : (this._currentStyledElement || this._lastFocusElement);

    try {
      const detector = window.keyPilot?.detector || window.keyPilot?.elementDetector;
      const cat = (detector && typeof detector.getClickableCategory === 'function')
        ? detector.getClickableCategory(el)
        : this.getFocusCategory(el);

      const showEffect = (detector && typeof detector.isLinkStyleCategory === 'function')
        ? detector.isLinkStyleCategory(cat)
        : (cat === CLICKABLE_CATEGORY.LINK || cat === CLICKABLE_CATEGORY.GENERIC);

      if (!showEffect) return;
    } catch { /* ignore */ }

    const { clickEffect } = this._getClickModeSettings();
    if (clickEffect === 'none') return;

    const presentation = this._clickEffectPresentation(clickEffect);
    if (!presentation) return;

    const rect = this._getFocusPulseRect();
    if (!rect) return;

    // Optional: briefly brighten the persistent DOM overlay if present.
    try {
      if (this.focusOverlay && this.focusOverlay.style.display !== 'none') {
        const originalBorder = this.focusOverlay.style.border;
        const originalBoxShadow = this.focusOverlay.style.boxShadow;
        this.focusOverlay.style.border = `3px solid ${COLORS.FLASH_GREEN}`;
        this.focusOverlay.style.boxShadow =
          `0 0 0 2px ${COLORS.FLASH_GREEN_SHADOW}, 0 0 20px 4px ${COLORS.FLASH_GREEN_GLOW}`;
        this.focusOverlay.style.transition = 'border 0.15s ease-out, box-shadow 0.15s ease-out';
        setTimeout(() => {
          if (!this.focusOverlay) return;
          this.focusOverlay.style.border = originalBorder;
          this.focusOverlay.style.boxShadow = originalBoxShadow;
          setTimeout(() => {
            if (this.focusOverlay) this.focusOverlay.style.transition = '';
          }, 150);
        }, 150);
      }
    } catch { /* ignore */ }

    // Click-effect ghost (link-style categories only).
    try {
      // Prefer a live rect from the activation target when available so tracking
      // compares against the same box we paint.
      let liveRect = rect;
      if (el && el.nodeType === 1) {
        try {
          const r = el.getBoundingClientRect();
          if (r && r.width > 0 && r.height > 0) {
            liveRect = {
              left: r.left,
              top: r.top,
              width: r.width,
              height: r.height
            };
          }
        } catch { /* keep last-focus rect */ }
      }

      // Don't start an effect if the target is already gone / not painted.
      if (el && el.nodeType === 1) {
        if (!el.isConnected) return;
        try {
          const cs = window.getComputedStyle(el);
          if (cs && (cs.display === 'none' || cs.visibility === 'hidden')) return;
        } catch { /* ignore */ }
      }

      const borderRadius = this._resolveElementBorderRadius(el);
      /** @type {HTMLElement|SVGSVGElement} */
      let pulse;
      if (clickEffect === 'dash') {
        pulse = this._createDashChasePulse(liveRect, borderRadius);
      } else {
        pulse = document.createElement('div');
        pulse.className = presentation.className;
        pulse.setAttribute('aria-hidden', 'true');
        pulse.setAttribute('data-kp-ephemeral-effect', clickEffect);
        // Position via left/top/width/height; CSS animation scales from transform-origin center.
        pulse.style.left = `${liveRect.left}px`;
        pulse.style.top = `${liveRect.top}px`;
        pulse.style.width = `${liveRect.width}px`;
        pulse.style.height = `${liveRect.height}px`;
        // Match corners to the clicked element (pill links, rounded cards, circular avatars).
        if (borderRadius) {
          pulse.style.borderRadius = borderRadius;
        }
      }
      document.body.appendChild(pulse);
      this._trackEphemeralEffect(pulse, el, liveRect, presentation.cleanupMs);
    } catch (e) {
      if (window.KEYPILOT_DEBUG) {
        console.warn('[KeyPilot] focus pulse failed:', e);
      }
    }
  }

  /**
   * Image-copy feedback: blue photo-frame over the source element that flashes,
   * pops slightly, then shrinks away (distinct from the green F-click expand pulse).
   *
   * @param {Element|null|undefined} element - Image or background-image host element
   */
  flashImageCopyPulse(element) {
    if (!element || element.nodeType !== 1) return;

    let rect = null;
    try {
      rect = element.getBoundingClientRect();
    } catch {
      return;
    }
    if (!rect) return;

    // Ignore degenerate / off-screen-ish rects.
    const w = Number(rect.width) || 0;
    const h = Number(rect.height) || 0;
    if (w < 2 || h < 2) return;

    // Cap extreme full-page backgrounds so the pulse stays readable.
    const maxW = Math.max(48, (typeof window !== 'undefined' ? window.innerWidth : 1200) * 0.92);
    const maxH = Math.max(48, (typeof window !== 'undefined' ? window.innerHeight : 800) * 0.92);
    let left = rect.left;
    let top = rect.top;
    let width = w;
    let height = h;
    if (width > maxW) {
      left += (width - maxW) / 2;
      width = maxW;
    }
    if (height > maxH) {
      top += (height - maxH) / 2;
      height = maxH;
    }

    try {
      if (!element.isConnected) return;
      const pulse = document.createElement('div');
      pulse.className = CSS_CLASSES.IMAGE_COPY_PULSE;
      pulse.setAttribute('aria-hidden', 'true');
      pulse.setAttribute('data-kp-ephemeral-effect', 'image-copy');
      pulse.style.left = `${left}px`;
      pulse.style.top = `${top}px`;
      pulse.style.width = `${width}px`;
      pulse.style.height = `${height}px`;
      // Match corners to the source image/host when it has a non-default radius.
      const borderRadius = this._resolveElementBorderRadius(element);
      if (borderRadius) {
        pulse.style.borderRadius = borderRadius;
      }
      document.body.appendChild(pulse);
      this._trackEphemeralEffect(
        pulse,
        element,
        { left, top, width, height },
        900
      );
    } catch (e) {
      if (window.KEYPILOT_DEBUG) {
        console.warn('[KeyPilot] image-copy pulse failed:', e);
      }
    }
  }

  createViewportModalFrame() {
    if (this.viewportModalFrame) {
      return this.viewportModalFrame;
    }

    this.viewportModalFrame = this.createElement('div', {
      className: CSS_CLASSES.VIEWPORT_MODAL_FRAME,
      style: `
        display: none;
      `
    });

    document.body.appendChild(this.viewportModalFrame);

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Viewport modal frame created and added to DOM:', {
        element: this.viewportModalFrame,
        className: this.viewportModalFrame.className,
        parent: this.viewportModalFrame.parentElement?.tagName
      });
    }

    return this.viewportModalFrame;
  }

  showViewportModalFrame() {
    if (!this.viewportModalFrame) {
      this.createViewportModalFrame();
    }

    this.viewportModalFrame.style.display = 'block';

    // Set up ResizeObserver to handle viewport changes with enhanced monitoring
    if (!this.resizeObserver && window.ResizeObserver) {
      this.resizeObserver = new ResizeObserver((entries) => {
        // Debounce resize updates to avoid excessive calls during continuous resizing
        if (this.resizeTimeout) {
          clearTimeout(this.resizeTimeout);
        }
        this.resizeTimeout = setTimeout(() => {
          this.updateViewportModalFrameSize();
          this.resizeTimeout = null;
        }, 16); // ~60fps for smooth updates
      });
      
      // Observe both document element and body for comprehensive viewport tracking
      this.resizeObserver.observe(document.documentElement);
      if (document.body) {
        this.resizeObserver.observe(document.body);
      }
    }

    // Enhanced fallback to window resize events if ResizeObserver is not available
    if (!window.ResizeObserver) {
      this.windowResizeHandler = this.debounce(() => {
        this.updateViewportModalFrameSize();
      }, 16);
      window.addEventListener('resize', this.windowResizeHandler);
      window.addEventListener('orientationchange', this.windowResizeHandler);
    }

    // Listen for fullscreen changes
    this.fullscreenHandler = () => {
      // Small delay to allow fullscreen transition to complete
      setTimeout(() => {
        this.updateViewportModalFrameSize();
      }, 100);
    };
    document.addEventListener('fullscreenchange', this.fullscreenHandler);
    document.addEventListener('webkitfullscreenchange', this.fullscreenHandler);
    document.addEventListener('mozfullscreenchange', this.fullscreenHandler);
    document.addEventListener('MSFullscreenChange', this.fullscreenHandler);

    // Listen for zoom changes (via visual viewport API if available)
    if (window.visualViewport) {
      this.visualViewportHandler = () => {
        this.updateViewportModalFrameSize();
      };
      window.visualViewport.addEventListener('resize', this.visualViewportHandler);
    }

    // Initial size update
    this.updateViewportModalFrameSize();

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Viewport modal frame shown with enhanced resize handling');
    }
  }

  hideViewportModalFrame() {
    if (this.viewportModalFrame) {
      this.viewportModalFrame.style.display = 'none';
    }

    // Clean up ResizeObserver
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
      this.resizeObserver = null;
    }

    // Clean up resize timeout
    if (this.resizeTimeout) {
      clearTimeout(this.resizeTimeout);
      this.resizeTimeout = null;
    }

    // Remove window resize listener fallback
    if (this.windowResizeHandler) {
      window.removeEventListener('resize', this.windowResizeHandler);
      window.removeEventListener('orientationchange', this.windowResizeHandler);
      this.windowResizeHandler = null;
    }

    // Remove fullscreen change listeners
    if (this.fullscreenHandler) {
      document.removeEventListener('fullscreenchange', this.fullscreenHandler);
      document.removeEventListener('webkitfullscreenchange', this.fullscreenHandler);
      document.removeEventListener('mozfullscreenchange', this.fullscreenHandler);
      document.removeEventListener('MSFullscreenChange', this.fullscreenHandler);
      this.fullscreenHandler = null;
    }

    // Remove visual viewport listener
    if (this.visualViewportHandler && window.visualViewport) {
      window.visualViewport.removeEventListener('resize', this.visualViewportHandler);
      this.visualViewportHandler = null;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Viewport modal frame hidden and all listeners cleaned up');
    }
  }

  updateViewportModalFrame(show) {
    if (show) {
      this.showViewportModalFrame();
    } else {
      this.hideViewportModalFrame();
    }
  }

  updateViewportModalFrameSize() {
    if (!this.viewportModalFrame || this.viewportModalFrame.style.display === 'none') {
      return;
    }

    // Get current viewport dimensions with fallbacks
    let viewportWidth, viewportHeight;

    // Use visual viewport API if available (handles zoom and mobile keyboards)
    if (window.visualViewport) {
      viewportWidth = window.visualViewport.width;
      viewportHeight = window.visualViewport.height;
    } else {
      // Fallback to standard viewport dimensions
      viewportWidth = window.innerWidth || document.documentElement.clientWidth;
      viewportHeight = window.innerHeight || document.documentElement.clientHeight;
    }

    // Handle fullscreen mode detection
    const isFullscreen = !!(
      document.fullscreenElement ||
      document.webkitFullscreenElement ||
      document.mozFullScreenElement ||
      document.msFullscreenElement
    );

    // Adjust for developer tools if not in fullscreen
    if (!isFullscreen) {
      // Check if developer tools might be open by comparing window dimensions
      const windowWidth = window.outerWidth;
      const windowHeight = window.outerHeight;
      
      // If there's a significant difference, dev tools might be open
      const widthDiff = Math.abs(windowWidth - viewportWidth);
      const heightDiff = Math.abs(windowHeight - viewportHeight);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Viewport size analysis:', {
          viewportWidth,
          viewportHeight,
          windowWidth,
          windowHeight,
          widthDiff,
          heightDiff,
          isFullscreen,
          visualViewportAvailable: !!window.visualViewport
        });
      }
    }

    // Update frame dimensions using calculated viewport size
    this.viewportModalFrame.style.width = `${viewportWidth}px`;
    this.viewportModalFrame.style.height = `${viewportHeight}px`;

    // Ensure frame stays positioned at viewport origin
    this.viewportModalFrame.style.left = '0px';
    this.viewportModalFrame.style.top = '0px';

    // Handle zoom level changes by ensuring the frame covers the visible area
    if (window.visualViewport) {
      // Adjust position for visual viewport offset (mobile keyboards, etc.)
      this.viewportModalFrame.style.left = `${window.visualViewport.offsetLeft}px`;
      this.viewportModalFrame.style.top = `${window.visualViewport.offsetTop}px`;
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Viewport modal frame size updated:', {
        width: `${viewportWidth}px`,
        height: `${viewportHeight}px`,
        left: this.viewportModalFrame.style.left,
        top: this.viewportModalFrame.style.top,
        isFullscreen,
        zoomLevel: window.devicePixelRatio || 1
      });
    }
  }

  cleanup() {
    // Close any open iframe/settings/guide/preview popovers and shared modal stack.
    try { this.hidePopover(); } catch { /* ignore */ }
    try { this.popupManager?.closeAll?.(); } catch { /* ignore */ }

    // Drop fixed click/image effect ghosts + their page lifecycle listeners.
    try { this.clearEphemeralEffects(); } catch { /* ignore */ }
    try { this._ephemeralEffectLifecycleDispose?.(); } catch { /* ignore */ }

    if (this.overlayObserver) {
      this.overlayObserver.disconnect();
      this.overlayObserver = null;
    }
    
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
      this.resizeObserver = null;
    }

    // Clean up resize timeout
    if (this.resizeTimeout) {
      clearTimeout(this.resizeTimeout);
      this.resizeTimeout = null;
    }

    // Clean up window resize handlers
    if (this.windowResizeHandler) {
      window.removeEventListener('resize', this.windowResizeHandler);
      window.removeEventListener('orientationchange', this.windowResizeHandler);
      this.windowResizeHandler = null;
    }

    // Clean up fullscreen handlers
    if (this.fullscreenHandler) {
      document.removeEventListener('fullscreenchange', this.fullscreenHandler);
      document.removeEventListener('webkitfullscreenchange', this.fullscreenHandler);
      document.removeEventListener('mozfullscreenchange', this.fullscreenHandler);
      document.removeEventListener('MSFullscreenChange', this.fullscreenHandler);
      this.fullscreenHandler = null;
    }

    // Clean up visual viewport handler
    if (this.visualViewportHandler && window.visualViewport) {
      window.visualViewport.removeEventListener('resize', this.visualViewportHandler);
      this.visualViewportHandler = null;
    }
    
    // Clean up all rendering backends
    this.cleanupRenderingMode();

    if (this.focusOverlay) {
      this.focusOverlay.remove();
      this.focusOverlay = null;
    }
    if (this.inspectorOverlay) {
      this.inspectorOverlay.remove();
      this.inspectorOverlay = null;
    }
    this.deleteOverlay = null;
    this.colsOverlay = null;
    this._inspectorOverlayKind = null;
    this.hideInspectorModeIndicator();
    // Clean up highlight manager
    if (this.highlightManager) {
      this.highlightManager.cleanup();
    }
    if (this.focusedTextOverlay) {
      this.focusedTextOverlay.remove();
      this.focusedTextOverlay = null;
    }
    if (this.viewportModalFrame) {
      this.viewportModalFrame.remove();
      this.viewportModalFrame = null;
    }
    if (this.activeTextInputFrame) {
      this.activeTextInputFrame.remove();
      this.activeTextInputFrame = null;
    }
    if (this.escExitLabelText) {
      this.escExitLabelText.remove();
      this.escExitLabelText = null;
    }
    if (this.escExitLabelHover) {
      this.escExitLabelHover.remove();
      this.escExitLabelHover = null;
    }

    // Clean up debug panel
    this.cleanupDebugPanel();
  }

  createElement(tag, props = {}) {
    const element = document.createElement(tag);
    for (const [key, value] of Object.entries(props)) {
      if (key === 'className') {
        element.className = value;
      } else if (key === 'style') {
        element.style.cssText = value;
      } else {
        element.setAttribute(key, value);
      }
    }
    return element;
  }

  // Utility method for debouncing function calls
  debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout);
        func(...args);
      };
      clearTimeout(timeout);
      timeout = setTimeout(later, wait);
    };
  }

  /**
   * Focus the popover iframe so in-frame KeyPilot receives keys without a user click.
   * @param {HTMLIFrameElement|null|undefined} iframe
   */
  _focusPopoverIframe(iframe) {
    if (!iframe) return;
    try { iframe.focus(); } catch { /* ignore */ }
    try { iframe.contentWindow?.focus?.(); } catch { /* ignore */ }
  }

  /**
   * Hybrid focus: keys drive the iframe page when the pointer is over content;
   * when the pointer is over chrome (titlebar / actions / close), focus returns
   * to the parent so Esc/E/P and chrome controls stay reliable.
   *
   * @param {{
   *   iframe: HTMLIFrameElement,
   *   chromeEls?: Array<HTMLElement|null|undefined>,
   *   focusChromeEl?: HTMLElement|null
   * }} opts
   */
  _installPopoverHybridFocus({ iframe, chromeEls, focusChromeEl } = {}) {
    try { this._popoverHybridFocusCleanup?.(); } catch { /* ignore */ }
    this._popoverHybridFocusCleanup = null;

    if (!iframe) return;

    const chrome = (Array.isArray(chromeEls) ? chromeEls : []).filter(Boolean);
    /** @type {'iframe'|'chrome'|null} */
    let zone = null;

    const focusChrome = () => {
      const el = focusChromeEl || chrome[0] || this.popoverContainer;
      try { el?.focus?.(); } catch { /* ignore */ }
    };

    const focusIframe = () => this._focusPopoverIframe(iframe);

    const setZone = (next) => {
      if (next === zone) return;
      zone = next;
      if (next === 'iframe') focusIframe();
      else if (next === 'chrome') focusChrome();
    };

    const onChromeEnter = () => setZone('chrome');
    const onIframeEnter = () => setZone('iframe');

    for (const el of chrome) {
      try { el.addEventListener('pointerenter', onChromeEnter, true); } catch { /* ignore */ }
    }
    try { iframe.addEventListener('pointerenter', onIframeEnter, true); } catch { /* ignore */ }

    this._popoverHybridFocusCleanup = () => {
      for (const el of chrome) {
        try { el.removeEventListener('pointerenter', onChromeEnter, true); } catch { /* ignore */ }
      }
      try { iframe.removeEventListener('pointerenter', onIframeEnter, true); } catch { /* ignore */ }
    };
  }

  /**
   * Show popover with iframe containing the linked page.
   * Uses the shared {@link createPopoverTitlebar} chrome: single titlebar with
   * title, optional close-key hint, and uniform × close when enabled.
   *
   * @param {string} url - The URL to load in the popover
   * @param {object} [opts]
   * @param {string} [opts.title] - Optional title for the titlebar (defaults to url)
   * @param {string} [opts.hintKeyLabel] - Optional key label in the titlebar hint (defaults to 'P')
   * @param {boolean} [opts.showClose=true] - Whether to show the titlebar close button
   * @param {string|Node|null} [opts.titlebarHint] - Override titlebar hint (string or Node)
   * @param {string[]} [opts.closeKeys] - Keys forwarded from iframe that should request close (defaults to ['Escape','p','P'])
   * @param {string} [opts.width] - Optional fixed width (e.g., '920px', overrides default 80vw)
   * @param {string} [opts.height] - Optional fixed height (e.g., '600px', overrides default 80vh)
   */
  showPopover(url, opts = {}) {
    // Remove existing popover if any
    this.hidePopover();

    const titleText = (opts && typeof opts.title === 'string' && opts.title.trim()) ? opts.title.trim() : String(url || '');
    const hintKeyLabel = (opts && typeof opts.hintKeyLabel === 'string' && opts.hintKeyLabel.trim()) ? opts.hintKeyLabel.trim() : 'P';
    const closeKeys = Array.isArray(opts?.closeKeys) && opts.closeKeys.length
      ? opts.closeKeys.map(String)
      : ['Escape', 'p', 'P'];

    // Centralized close request:
    // Always prefer going through KeyPilot so state (mode/popoverOpen) is updated.
    // Fall back to direct DOM cleanup if KeyPilot isn't available for some reason.
    const requestClosePopover = () => {
      try {
        if (window.__KeyPilotInstance && typeof window.__KeyPilotInstance.handleClosePopover === 'function') {
          window.__KeyPilotInstance.handleClosePopover();
          return;
        }
      } catch (_e) {
        // Ignore and fall back to direct hide
      }
      this.hidePopover();
    };

    const ensureTopMouseTracking = () => {
      if (this._popoverMouseTrackerInstalled) return;
      this._popoverMouseTrackerInstalled = true;
      const update = (e) => {
        try {
          if (!e) return;
          if (typeof e.clientX === 'number') this._popoverLastMouse.x = e.clientX;
          if (typeof e.clientY === 'number') this._popoverLastMouse.y = e.clientY;
        } catch {
          // ignore
        }
      };
      try { document.addEventListener('mousemove', update, true); } catch { /* ignore */ }
      try { document.addEventListener('pointermove', update, true); } catch { /* ignore */ }
    };

    const clickCloseIfHovered = () => {
      try {
        const btn = this.popoverCloseButton;
        if (!btn) return false;
        const x = this._popoverLastMouse.x;
        const y = this._popoverLastMouse.y;
        if (typeof x !== 'number' || typeof y !== 'number') return false;
        const el = document.elementFromPoint(x, y);
        if (!el) return false;
        if (el === btn || btn.contains(el)) {
          try { btn.click(); } catch { /* ignore */ }
          return true;
        }
        return false;
      } catch {
        return false;
      }
    };

    // Create popover container (NOT using the native Popover API).
    // The Popover API uses the browser "top layer", which can sit above our cursor /
    // green click rectangle regardless of z-index, breaking F-to-click on popover UI.
    this.popoverContainer = this.createElement('div', {
      className: 'kpv2-popover-container',
      tabindex: '-1',
      role: 'dialog',
      'aria-modal': 'true',
      style: `
        position: fixed;
        inset: 0;                  /* top: 0; left: 0; bottom: 0; right: 0; */
        width: ${opts.width || '80vw'};
        height: ${opts.height || '80vh'};
        max-width: 100vw;          /* prevents overflow on very small screens */
        max-height: 100vh;
        margin: auto;              /* this is what centers it perfectly */
        background: linear-gradient(rgb(18, 18, 18) 0%, rgb(11, 11, 11) 100%);
        border-radius: 8px;
        border: 1px solid rgb(43, 43, 43);
        box-shadow: rgba(0, 0, 0, 0.65) 0px 8px 24px;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        font-family: ${KP_UI_FONT};
        font-size: 14px;
        line-height: 1.3;
        letter-spacing: normal;
      `
    });




    // Store iframe reference for focus management
    let iframeRef = null;
    this.popoverBridgeReady = false;

    // Single standard titlebar: title + close hint + uniform × close (no second header bar).
    const showClose = opts?.showClose !== false;
    const titlebarHint = opts?.titlebarHint !== undefined
      ? opts.titlebarHint
      : createTitlebarCloseHint({
        keys: [hintKeyLabel, 'Esc'],
        suffix: 'Use the same keyboard navigation controls.'
      });
    const titlebarApi = createPopoverTitlebar({
      title: titleText,
      variant: 'modal',
      showClose,
      onClose: requestClosePopover,
      closeTitle: 'Close (Esc)',
      hint: titlebarHint,
      className: 'kpv2-popover-titlebar'
    });
    const header = titlebarApi.titlebar;
    const closeButton = titlebarApi.closeButton;
    this.popoverCloseButton = closeButton;
    ensureTopMouseTracking();

    // Create error message container (initially hidden)
    const errorContainer = this.createElement('div', {
      className: 'kpv2-popover-error',
      style: `
        flex: 1;
        display: none;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 40px;
        text-align: center;
        background: #f9f9f9;
      `
    });

    const errorIcon = this.createElement('div', {
      style: `
        font-size: 48px;
        margin-bottom: 16px;
        color: #999;
      `
    });
    errorIcon.textContent = '🚫';
    errorContainer.appendChild(errorIcon);

    const errorTitle = this.createElement('div', {
      style: `
        font-size: 18px;
        font-weight: 600;
        color: #333;
        margin-bottom: 8px;
      `
    });
    errorTitle.textContent = 'Cannot Display Page';
    errorContainer.appendChild(errorTitle);

    const errorMessage = this.createElement('div', {
      style: `
        font-size: 14px;
        color: #666;
        margin-bottom: 24px;
        max-width: 400px;
      `
    });
    errorMessage.textContent = 'This website prevents embedding in iframes for security reasons.';
    errorContainer.appendChild(errorMessage);

    const openInTabButton = this.createElement('button', {
      style: `
        background: #4CAF50;
        color: white;
        border: none;
        padding: 10px 20px;
        border-radius: 4px;
        cursor: pointer;
        font-size: 14px;
        font-weight: 500;
      `
    });
    openInTabButton.textContent = 'Open in New Tab';
    openInTabButton.onclick = () => {
      window.open(url, '_blank');
      requestClosePopover();
    };
    errorContainer.appendChild(openInTabButton);

    // Create iframe
    const iframe = this.createElement('iframe', {
      src: url,
      tabindex: '0',
      style: `
        flex: 1;
        border: none;
        width: 100%;
        height: 100%;
      `
    });
    iframeRef = iframe;
    this.popoverIframeElement = iframe;
    this.popoverIframeWindow = iframe.contentWindow || null;

    // Initialize the iframe bridge (content script running inside the iframe).
    // We retry a few times because content scripts in the frame may not be ready immediately,
    // and some pages navigate/redirect after initial load.
    // Pass closeKeys so Esc/P work inside the focused iframe without a host click.
    const sendBridgeInit = () => {
      try {
        iframe.contentWindow?.postMessage({
          type: 'KP_POPOVER_BRIDGE_INIT',
          closeKeys
        }, '*');
      } catch {
        // Ignore
      }
    };

    // Detect iframe load errors
    // Note: We can't reliably detect X-Frame-Options blocking for cross-origin iframes
    // due to same-origin policy. The declarativeNetRequest rules should handle most cases.
    // Only show error on actual load failure (onerror event).
    iframe.onerror = () => {
      console.log('[KeyPilot] Iframe load error detected');
      iframe.style.display = 'none';
      errorContainer.style.display = 'flex';
    };

    // Optional: Very long timeout as last resort (30 seconds) for cases where
    // iframe never fires onload/onerror (shouldn't happen with declarativeNetRequest)
    const loadTimeout = setTimeout(() => {
      // Only show error if iframe hasn't loaded at all (no onload fired)
      // This is a fallback for edge cases
      console.log('[KeyPilot] Iframe load timeout - showing error as fallback');
      iframe.style.display = 'none';
      errorContainer.style.display = 'flex';
    }, 30000);

    iframe.onload = () => {
      clearTimeout(loadTimeout);
      // Iframe loaded successfully - keep it visible
      // Note: We can't check contentDocument for cross-origin iframes,
      // but if onload fired, the iframe should be working
      console.log('[KeyPilot] Iframe loaded successfully');
      sendBridgeInit();
    };

    this.popoverContainer.appendChild(header);
    this.popoverContainer.appendChild(iframe);
    this.popoverContainer.appendChild(errorContainer);
    // Mount via PopupManager so the backdrop + stacking are consistent across popups.
    // This also keeps the popup in the normal DOM stacking context (no Popover API top-layer),
    // so KeyPilot overlays (green click rectangle) can sit above it by z-index.
    this.popupManager?.showModal?.({
      id: this._popoverPopupId,
      panel: this.popoverContainer,
      onRequestClose: requestClosePopover
    });
    sendBridgeInit();

    // Short retry window to cover slow frames / initial about:blank then navigation
    try {
      let attemptsLeft = 6; // ~1.5s total
      this.popoverInitTimer = setInterval(() => {
        if (!this.popoverContainer || attemptsLeft <= 0) {
          clearInterval(this.popoverInitTimer);
          this.popoverInitTimer = null;
          return;
        }
        attemptsLeft -= 1;
        sendBridgeInit();
      }, 250);
    } catch {
      // Ignore
    }

    // Prevent body scroll when popover is open
    document.body.style.overflow = 'hidden';

    // Add keyboard event listeners directly to catch Escape and F key
    // This ensures they work even when iframe has focus
    const handlePopoverKeyDown = (e) => {
      console.log('[KeyPilot] Popover key event:', e.key, 'Target:', e.target, 'Active element:', document.activeElement);
      
      // Escape key - close popover (always, regardless of where it's pressed)
      if (e.key === 'Escape') {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        requestClosePopover();
        return;
      }
      
      // NOTE: We intentionally do NOT repurpose "F" to close popovers.
      // "F" is KeyPilot's click key, and users expect it to click popover UI (e.g. ×)
      // once the green rectangle can sit above popovers via z-index stacking.
    };

    // Add listeners to document and popover container with capture phase
    // This ensures we catch events even when iframe or other elements have focus
    document.addEventListener('keydown', handlePopoverKeyDown, true);
    this.popoverContainer.addEventListener('keydown', handlePopoverKeyDown, true);
    
    // Store cleanup function and backdrop reference
    this.popoverKeyHandler = handlePopoverKeyDown;

    // Listen for key events forwarded from the iframe (content script bridge).
    // This enables closing the popover even after the user clicks into the iframe,
    // where the parent document can no longer observe keydown events directly.
    this.popoverMessageHandler = (event) => {
      const data = event?.data;
      if (!data || typeof data.type !== 'string') return;
      if (this.popoverIframeWindow && event.source !== this.popoverIframeWindow) return;

      if (data.type === 'KP_POPOVER_BRIDGE_READY') {
        this.popoverBridgeReady = true;
        // Auto-focus iframe so full KeyPilot works inside without a user click.
        // Close keys (Esc/P) are handled by the iframe bridge → parent.
        this._focusPopoverIframe(iframeRef);
        this._installPopoverHybridFocus({
          iframe: iframeRef,
          chromeEls: [header, closeButton].filter(Boolean),
          focusChromeEl: closeButton || header
        });
        return;
      }

      if (data.type === 'KP_POPOVER_REQUEST_CLOSE') {
        // Close on configured keys forwarded by the iframe bridge.
        if (closeKeys.includes(String(data.key))) requestClosePopover();
      }

      if (data.type === 'KP_POPOVER_LAUNCH_WALKTHROUGH') {
        // Guide "Launch Walkthrough": close this popover, then open tutorial from reset.
        requestClosePopover();
        try {
          const ob = window.__KeyPilotOnboarding;
          if (ob && typeof ob.resetTutorial === 'function') {
            void ob.resetTutorial();
          }
        } catch {
          // ignore
        }
        return;
      }

      if (data.type === 'KP_POPOVER_BRIDGE_KEYDOWN') {
        const k = String(data.key || '');
        if (k === 'f' || k === 'F') {
          // Prefer "click close button if hovered" so users can use F on the × affordance
          // even when focus is inside the iframe (keydown doesn't propagate to parent).
          clickCloseIfHovered();
        }
      }
    };
    window.addEventListener('message', this.popoverMessageHandler, true);

    // Until the bridge is ready, keep focus on chrome (then hybrid focus takes over).
    try {
      (closeButton || header)?.focus?.();
    } catch (_e) {
      try {
        this.popoverContainer.focus();
      } catch (_e2) {
        // Ignore
      }
    }
  }

  /**
   * Hide the popover
   */
  /**
   * Remembered viewport mode for a host. Default is desktop; only mobile is stored.
   * @param {string} hostname
   * @returns {Promise<'mobile'|'desktop'>}
   */
  async _getPreviewViewportModeForHost(hostname) {
    const host = String(hostname || '').toLowerCase();
    if (!host) return 'desktop';
    try {
      const map = await storageGetValue(PREVIEW_VIEWPORT_BY_HOST_KEY, {});
      if (map && typeof map === 'object' && map[host] === 'mobile') {
        return 'mobile';
      }
    } catch (e) {
      console.warn('[KeyPilot] Failed to read preview viewport prefs:', e?.message || e);
    }
    return 'desktop';
  }

  /**
   * Persist viewport mode for a host. Desktop clears the override (default).
   * @param {string} hostname
   * @param {'mobile'|'desktop'} mode
   * @returns {Promise<void>}
   */
  async _setPreviewViewportModeForHost(hostname, mode) {
    const host = String(hostname || '').toLowerCase();
    if (!host) return;
    try {
      const prev = await storageGetValue(PREVIEW_VIEWPORT_BY_HOST_KEY, {});
      const map = (prev && typeof prev === 'object' && !Array.isArray(prev))
        ? { ...prev }
        : {};
      if (mode === 'mobile') {
        map[host] = 'mobile';
      } else {
        delete map[host];
      }
      await storageSetValue(PREVIEW_VIEWPORT_BY_HOST_KEY, map);
    } catch (e) {
      console.warn('[KeyPilot] Failed to save preview viewport prefs:', e?.message || e);
    }
  }

  /**
   * Ask the service worker to enable/disable mobile User-Agent for this tab's
   * sub_frame requests (Link Preview Mobile mode).
   * @param {boolean} enabled
   * @returns {Promise<boolean>}
   */
  async _setPreviewMobileUa(enabled) {
    const next = !!enabled;
    try {
      if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
        this._previewMobileUaActive = false;
        return false;
      }
      const res = await chrome.runtime.sendMessage({
        type: MSG.SET_PREVIEW_MOBILE_UA,
        enabled: next
      });
      if (res?.type === MSG.ERROR) {
        console.warn('[KeyPilot] Preview mobile UA not applied:', res.error || res);
        this._previewMobileUaActive = false;
        return false;
      }
      this._previewMobileUaActive = next;
      return true;
    } catch (e) {
      console.warn('[KeyPilot] Preview mobile UA message failed:', e?.message || e);
      this._previewMobileUaActive = false;
      return false;
    }
  }

  /**
   * Move focus out of the popover (esp. its iframe) before the node is removed.
   * Otherwise Chrome often hands focus to the browser omnibox, which steals keys
   * from the page (notably on New Tab after a second E closes Link Preview).
   */
  _restoreFocusFromPopover() {
    const iframe = this.popoverIframeElement;
    const container = this.popoverContainer;
    if (!iframe && !container) return;

    let active = null;
    try { active = document.activeElement; } catch { /* ignore */ }

    const focusInPopover =
      !!(iframe && active === iframe) ||
      !!(container && active instanceof Node && container.contains(active));

    // Always try to leave the iframe before removal; focus-in-iframe is the
    // main omnibox-steal case even when activeElement reporting is odd.
    try { iframe?.blur?.(); } catch { /* ignore */ }
    if (focusInPopover) {
      try { active?.blur?.(); } catch { /* ignore */ }
    }

    try { window.focus(); } catch { /* ignore */ }

    // Park focus on a surviving element. Prefer body (make it programmatically
    // focusable) so the page keeps keyboard ownership after teardown.
    try {
      const body = document.body;
      if (body) {
        if (!body.hasAttribute('tabindex')) {
          body.setAttribute('tabindex', '-1');
        }
        body.focus({ preventScroll: true });
      }
    } catch { /* ignore */ }
  }

  hidePopover() {
    // Drop mobile UA session rule so host-page iframes are not affected after close.
    if (this._previewMobileUaActive) {
      this._previewMobileUaActive = false;
      try {
        void this._setPreviewMobileUa(false);
      } catch { /* ignore */ }
    }

    // Capture focus back onto the page *before* removing a focused iframe.
    try { this._restoreFocusFromPopover(); } catch { /* ignore */ }

    // Stop bridge init retries
    if (this.popoverInitTimer) {
      try {
        clearInterval(this.popoverInitTimer);
      } catch {
        // Ignore
      }
      this.popoverInitTimer = null;
    }

    // Remove iframe bridge message listener
    if (this.popoverMessageHandler) {
      try {
        window.removeEventListener('message', this.popoverMessageHandler, true);
      } catch {
        // Ignore
      }
      this.popoverMessageHandler = null;
    }
    this.popoverIframeWindow = null;
    this.popoverIframeElement = null;
    this.popoverBridgeReady = false;
    this.popoverCloseButton = null;

    // Remove keyboard event listeners
    if (this.popoverKeyHandler) {
      document.removeEventListener('keydown', this.popoverKeyHandler, true);

      if (this.popoverContainer) {
        this.popoverContainer.removeEventListener('keydown', this.popoverKeyHandler, true);
      }

      this.popoverKeyHandler = null;
    }

    // Remove preview popover arrow style if exists
    if (this._popoverArrowStyle) {
      try {
        this._popoverArrowStyle.remove();
      } catch { /* ignore */ }
      this._popoverArrowStyle = null;
    }

    // Remove click-outside handler if exists
    if (this._popoverClickOutsideHandler) {
      try {
        document.removeEventListener('mousedown', this._popoverClickOutsideHandler, true);
      } catch { /* ignore */ }
      this._popoverClickOutsideHandler = null;
    }

    // Tear down preview titlebar drag handlers
    if (this._previewPopoverDragCleanup) {
      try {
        this._previewPopoverDragCleanup();
      } catch { /* ignore */ }
      this._previewPopoverDragCleanup = null;
    }

    // Tear down resize handles (preview mounts outside PopupManager)
    if (this._popoverResizeDispose) {
      try {
        this._popoverResizeDispose();
      } catch { /* ignore */ }
      this._popoverResizeDispose = null;
    }

    // Tear down hybrid chrome/iframe focus routing
    if (this._popoverHybridFocusCleanup) {
      try {
        this._popoverHybridFocusCleanup();
      } catch { /* ignore */ }
      this._popoverHybridFocusCleanup = null;
    }

    if (this.popoverContainer) {
      // For preview popover (direct mount), remove directly
      // For regular popover, unmount via PopupManager
      if (this._isPreviewPopover) {
        try {
          this.popoverContainer.remove();
        } catch { /* ignore */ }
      } else {
        // Unmount via PopupManager (removes panel + shared backdrop when last popup closes).
        try {
          this.popupManager?.hideModal?.(this._popoverPopupId);
        } catch {
          // Fallback: direct remove
          try { this.popoverContainer.remove(); } catch { /* ignore */ }
        }
      }
      this.popoverContainer = null;
    }

    this._isPreviewPopover = false;

    // After DOM teardown, re-assert page focus (container may have held it).
    try {
      window.focus();
      if (document.body && document.activeElement !== document.body) {
        document.body.focus({ preventScroll: true });
      }
    } catch { /* ignore */ }

    // Restore body scroll (only needed for regular popover, but doesn't hurt)
    document.body.style.overflow = '';
  }

  /**
   * Post a message to the popover iframe bridge (if present).
   * @param {any} message
   * @returns {boolean} Whether a postMessage was attempted successfully
   */
  postMessageToPopoverIframe(message) {
    const win = this.popoverIframeWindow;
    if (!win) return false;
    try {
      win.postMessage(message, '*');
      return true;
    } catch {
      return false;
    }
  }

  /**
   * Scroll the popover iframe by a delta (in pixels).
   * @param {number} deltaY
   * @param {'smooth'|'auto'} behavior
   */
  scrollPopoverBy(deltaY, behavior = 'smooth') {
    return this.postMessageToPopoverIframe({
      type: 'KP_POPOVER_SCROLL',
      command: 'scrollBy',
      delta: deltaY,
      behavior
    });
  }

  scrollPopoverToTop(behavior = 'smooth') {
    return this.postMessageToPopoverIframe({
      type: 'KP_POPOVER_SCROLL',
      command: 'scrollToTop',
      behavior
    });
  }

  scrollPopoverToBottom(behavior = 'smooth') {
    return this.postMessageToPopoverIframe({
      type: 'KP_POPOVER_SCROLL',
      command: 'scrollToBottom',
      behavior
    });
  }

  /**
   * Check if popover is currently open
   * @returns {boolean}
   */
  isPopoverOpen() {
    return this.popoverContainer !== null;
  }

  /**
   * Show a preview popover near the cursor (picture-in-picture style)
   * @param {string} url - URL to load in iframe
   * @param {Object} opts - Options including mouseX, mouseY for positioning
   * @param {'mobile'|'desktop'} [opts.viewportMode] - Override mode; otherwise host preference or desktop default
   */
  async showPreviewPopover(url, opts = {}) {
    // Remove existing popover if any
    this.hidePopover();

    // Prefer HTTPS for preview embeds (many sites refuse insecure framing / mixed content).
    url = preferHttpsForPreview(url);

    const mouseX = opts.mouseX ?? window.innerWidth / 2;
    const popoverWidth = 600;
    const arrowSize = 10; // Kept for drag cleanup / style vars if user resizes later
    const margin = 20; // Margin from viewport edges
    const previewHost = previewHostFromUrl(url);

    // Full viewport height with the existing edge margins (top + bottom).
    const popoverHeight = Math.max(200, window.innerHeight - margin * 2);
    const top = margin;

    // Center horizontally under cursor, but clamp to viewport
    let left = mouseX - popoverWidth / 2;
    left = Math.max(margin, Math.min(left, window.innerWidth - popoverWidth - margin));

    // No top/bottom caret on a full-height panel (would sit outside the viewport).
    const arrowLeft = Math.max(20, Math.min(mouseX - left, popoverWidth - 20));

    const titleText = 'Link Preview';
    const closeKeys = Array.isArray(opts?.closeKeys) && opts.closeKeys.length
      ? opts.closeKeys.map(String)
      : ['Escape', 'e', 'E'];

    // Default desktop; restore per-host Mobile if the user chose it before.
    /** @type {'mobile'|'desktop'} */
    let viewportMode = 'desktop';
    if (opts.viewportMode === 'mobile' || opts.viewportMode === 'desktop') {
      viewportMode = opts.viewportMode;
    } else {
      try {
        viewportMode = await this._getPreviewViewportModeForHost(previewHost);
      } catch {
        viewportMode = 'desktop';
      }
    }

    // Centralized close request
    const requestClosePopover = () => {
      try {
        if (window.__KeyPilotInstance && typeof window.__KeyPilotInstance.handleClosePopover === 'function') {
          window.__KeyPilotInstance.handleClosePopover();
          return;
        }
      } catch (_e) {
        // Ignore and fall back to direct hide
      }
      this.hidePopover();
    };

    const ensureTopMouseTracking = () => {
      if (this._popoverMouseTrackerInstalled) return;
      this._popoverMouseTrackerInstalled = true;
      const update = (e) => {
        try {
          if (!e) return;
          if (typeof e.clientX === 'number') this._popoverLastMouse.x = e.clientX;
          if (typeof e.clientY === 'number') this._popoverLastMouse.y = e.clientY;
        } catch {
          // ignore
        }
      };
      try { document.addEventListener('mousemove', update, true); } catch { /* ignore */ }
      try { document.addEventListener('pointermove', update, true); } catch { /* ignore */ }
    };

    const clickCloseIfHovered = () => {
      try {
        const btn = this.popoverCloseButton;
        if (!btn) return false;
        const x = this._popoverLastMouse.x;
        const y = this._popoverLastMouse.y;
        if (typeof x !== 'number' || typeof y !== 'number') return false;
        const el = document.elementFromPoint(x, y);
        if (!el) return false;
        if (el === btn || btn.contains(el)) {
          try { btn.click(); } catch { /* ignore */ }
          return true;
        }
        return false;
      } catch {
        return false;
      }
    };

    // Create popover container (full viewport height; no top/bottom caret).
    this.popoverContainer = this.createElement('div', {
      className: 'kpv2-preview-popover-container',
      tabindex: '-1',
      role: 'dialog',
      'aria-modal': 'true',
      style: `
        position: fixed;
        top: ${top}px;
        left: ${left}px;
        width: ${popoverWidth}px;
        height: ${popoverHeight}px;
        background: linear-gradient(rgb(18, 18, 18) 0%, rgb(11, 11, 11) 100%);
        border-radius: 8px;
        border: 1px solid rgb(43, 43, 43);
        box-shadow: rgba(0, 0, 0, 0.65) 0px 8px 24px;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        --arrow-left: ${arrowLeft}px;
        z-index: ${Z_INDEX.POPUP_PANEL_BASE};
        font-family: ${KP_UI_FONT};
        font-size: 12px;
        line-height: 1.3;
        letter-spacing: normal;
      `
    });

    // Inject triangle arrow CSS
    const arrowStyle = this.createElement('style');
    arrowStyle.textContent = `
      .kpv2-preview-popover-container::before {
        content: "";
        position: absolute;
        width: 0;
        height: 0;
        left: var(--arrow-left, 50%);
        margin-left: -${arrowSize}px;
        border: ${arrowSize}px solid transparent;
        z-index: 1;
      }
      .kpv2-preview-popover-container::after {
        content: "";
        position: absolute;
        width: 0;
        height: 0;
        left: var(--arrow-left, 50%);
        margin-left: -${arrowSize - 1}px;
        border: ${arrowSize - 1}px solid transparent;
        z-index: 2;
      }
      .kpv2-preview-popover-container[data-placement="bottom"]::before {
        top: -${arrowSize * 2}px;
        border-bottom-color: rgb(43, 43, 43);
      }
      .kpv2-preview-popover-container[data-placement="bottom"]::after {
        top: -${arrowSize * 2 - 1}px;
        border-bottom-color: rgb(18, 18, 18);
      }
      .kpv2-preview-popover-container[data-placement="top"]::before {
        bottom: -${arrowSize * 2}px;
        border-top-color: rgb(43, 43, 43);
      }
      .kpv2-preview-popover-container[data-placement="top"]::after {
        bottom: -${arrowSize * 2 - 1}px;
        border-top-color: rgb(11, 11, 11);
      }
    `;
    document.head.appendChild(arrowStyle);
    this._popoverArrowStyle = arrowStyle;

    // Store iframe reference for focus management
    let iframeRef = null;
    this.popoverBridgeReady = false;

    // Full-bleed viewport shell (mobile and desktop both fill the popover width).
    const iframeViewport = this.createElement('div', {
      className: 'kpv2-preview-popover-viewport',
      style: `
        flex: 1;
        min-height: 0;
        display: flex;
        align-items: stretch;
        justify-content: stretch;
        overflow: hidden;
        background: #0a0a0a;
      `
    });

    // Shared outline Open / Open in New Tab controls (also used by Launcher preview).
    const { actions: previewOpenActions } = createPreviewOpenActionButtons({
      getUrl: () => url,
      afterOpen: () => requestClosePopover(),
      afterOpenNewTab: () => requestClosePopover()
    });

    /**
     * Mobile = mobile User-Agent + client hints (true mobile pages).
     * Desktop = normal desktop UA.
     * Both modes fill the popover width; reload is required for UA to take effect.
     * @param {'mobile'|'desktop'} mode
     * @param {{ reload?: boolean }} [opts]
     */
    const applyViewportMode = async (mode, opts = {}) => {
      const next = mode === 'desktop' ? 'desktop' : 'mobile';
      const prev = viewportMode;
      const shouldReload = opts.reload !== false && prev !== next && !!iframeRef;
      viewportMode = next;
      try {
        this.popoverContainer?.setAttribute('data-kp-preview-viewport', viewportMode);
      } catch { /* ignore */ }

      // Full-width frame in both modes (no side letterboxing).
      if (iframeRef) {
        iframeRef.style.width = '100%';
        iframeRef.style.maxWidth = 'none';
        iframeRef.style.flex = '1 1 auto';
        iframeRef.style.alignSelf = 'stretch';
        iframeRef.style.height = '100%';
      }
      iframeViewport.style.justifyContent = 'stretch';

      // Install / clear mobile UA before any navigation so the document request sees it.
      if (viewportMode === 'mobile') {
        await this._setPreviewMobileUa(true);
      } else {
        await this._setPreviewMobileUa(false);
      }

      if (shouldReload && iframeRef) {
        try {
          // Reset load error UI if we were showing it.
          iframeViewport.style.display = '';
          errorContainer.style.display = 'none';
        } catch { /* ignore */ }
        try {
          armLoadTimeout();
        } catch { /* ignore — armLoadTimeout defined later; first apply never reloads */ }
        try {
          // Force a real navigation with the new UA (same-url assignment can be a no-op).
          iframeRef.src = 'about:blank';
          iframeRef.src = url;
          this.popoverIframeWindow = iframeRef.contentWindow || null;
        } catch {
          try {
            iframeRef.src = url;
            this.popoverIframeWindow = iframeRef.contentWindow || null;
          } catch { /* ignore */ }
        }
      }
    };

    const viewportModeControl = createSegmentedControl({
      value: viewportMode,
      ariaLabel: 'Preview viewport mode',
      className: 'kpv2-preview-viewport-mode',
      options: [
        {
          value: 'mobile',
          label: 'Mobile',
          title: 'Mobile site (mobile User-Agent). Remembered for this website.',
          ariaLabel: 'Mobile preview'
        },
        {
          value: 'desktop',
          label: 'Desktop',
          title: 'Desktop site (default). Remembered for this website.',
          ariaLabel: 'Desktop preview'
        }
      ],
      onChange: (value) => {
        void (async () => {
          await applyViewportMode(value, { reload: true });
          // Persist per website so future Link Previews open in the same mode.
          try {
            await this._setPreviewViewportModeForHost(previewHost, value === 'mobile' ? 'mobile' : 'desktop');
          } catch { /* ignore */ }
        })();
      }
    });

    // Single standard titlebar (drag handle): title + hint + mode + actions + uniform × close.
    const titlebarApi = createPopoverTitlebar({
      title: titleText,
      variant: 'preview',
      draggable: true,
      titleAttr: 'Drag to move',
      showClose: true,
      onClose: requestClosePopover,
      closeTitle: 'Close (Esc)',
      hint: 'Press Esc / E to hide',
      actions: [viewportModeControl.root, previewOpenActions],
      className: 'kpv2-preview-popover-titlebar'
    });
    const header = titlebarApi.titlebar;
    const closeButton = titlebarApi.closeButton;
    this.popoverCloseButton = closeButton;
    ensureTopMouseTracking();

    // Titlebar drag: move the preview popover; clamp to viewport; hide caret once moved.
    let dragState = null;
    const DRAG_MOVE_THRESHOLD_PX = 3;

    const clampPopoverPosition = (leftPx, topPx) => {
      const el = this.popoverContainer;
      if (!el) return { left: leftPx, top: topPx };
      const w = el.offsetWidth || popoverWidth;
      const h = el.offsetHeight || popoverHeight;
      const maxLeft = Math.max(margin, window.innerWidth - w - margin);
      const maxTop = Math.max(margin, window.innerHeight - h - margin);
      return {
        left: Math.max(margin, Math.min(leftPx, maxLeft)),
        top: Math.max(margin, Math.min(topPx, maxTop))
      };
    };

    const hidePreviewArrow = () => {
      const el = this.popoverContainer;
      if (!el || el.dataset.kpDragged === '1') return;
      el.dataset.kpDragged = '1';
      el.removeAttribute('data-placement');
    };

    const onDragPointerMove = (e) => {
      if (!dragState || !this.popoverContainer) return;
      const dx = e.clientX - dragState.startX;
      const dy = e.clientY - dragState.startY;
      if (!dragState.moved) {
        if (Math.abs(dx) < DRAG_MOVE_THRESHOLD_PX && Math.abs(dy) < DRAG_MOVE_THRESHOLD_PX) {
          return;
        }
        dragState.moved = true;
        hidePreviewArrow();
        header.style.cursor = 'grabbing';
        // Prevent iframe from swallowing pointer events mid-drag
        if (this.popoverIframeElement) {
          this.popoverIframeElement.style.pointerEvents = 'none';
        }
      }
      const next = clampPopoverPosition(dragState.originLeft + dx, dragState.originTop + dy);
      this.popoverContainer.style.left = `${next.left}px`;
      this.popoverContainer.style.top = `${next.top}px`;
    };

    const endDrag = (e) => {
      if (!dragState) return;
      const pointerId = dragState.pointerId;
      dragState = null;
      header.style.cursor = 'grab';
      if (this.popoverIframeElement) {
        this.popoverIframeElement.style.pointerEvents = '';
      }
      try {
        if (e && typeof e.pointerId === 'number') {
          header.releasePointerCapture(e.pointerId);
        } else if (typeof pointerId === 'number') {
          header.releasePointerCapture(pointerId);
        }
      } catch { /* ignore */ }
      document.removeEventListener('pointermove', onDragPointerMove, true);
      document.removeEventListener('pointerup', endDrag, true);
      document.removeEventListener('pointercancel', endDrag, true);
    };

    const onTitlebarPointerDown = (e) => {
      // Action / close buttons keep their own click behavior (don't start a drag)
      const interactive = titlebarApi.getInteractiveElements();
      for (const btn of interactive) {
        if (e.target === btn || (btn.contains && btn.contains(e.target))) {
          return;
        }
      }
      // Primary button only for mouse
      if (e.pointerType === 'mouse' && e.button !== 0) return;
      if (!this.popoverContainer) return;

      e.preventDefault();
      e.stopPropagation();

      const rect = this.popoverContainer.getBoundingClientRect();
      dragState = {
        startX: e.clientX,
        startY: e.clientY,
        originLeft: rect.left,
        originTop: rect.top,
        pointerId: e.pointerId,
        moved: false
      };

      try {
        header.setPointerCapture(e.pointerId);
      } catch { /* ignore */ }

      document.addEventListener('pointermove', onDragPointerMove, true);
      document.addEventListener('pointerup', endDrag, true);
      document.addEventListener('pointercancel', endDrag, true);
    };

    header.addEventListener('pointerdown', onTitlebarPointerDown);

    this._previewPopoverDragCleanup = () => {
      endDrag();
      try {
        header.removeEventListener('pointerdown', onTitlebarPointerDown);
      } catch { /* ignore */ }
    };

    // Generic resize handles (edges + SE grip). Shared util used by all popovers.
    try {
      this._popoverResizeDispose?.();
    } catch { /* ignore */ }
    try {
      const resizeApi = makePopoverResizable(this.popoverContainer, {
        minWidth: 280,
        minHeight: 200,
        margin,
        onResizeStart: () => {
          hidePreviewArrow();
        }
      });
      this._popoverResizeDispose = resizeApi?.dispose || null;
    } catch (e) {
      console.warn('[KeyPilot] Failed to make preview popover resizable:', e?.message || e);
      this._popoverResizeDispose = null;
    }

    // Create error message container (initially hidden)
    const errorContainer = this.createElement('div', {
      className: 'kpv2-popover-error',
      style: `
        flex: 1;
        display: none;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 20px;
        text-align: center;
        background: #f9f9f9;
      `
    });

    const errorIcon = this.createElement('div', {
      style: `
        font-size: 32px;
        margin-bottom: 12px;
        color: #999;
      `
    });
    errorIcon.textContent = '🚫';
    errorContainer.appendChild(errorIcon);

    const errorTitle = this.createElement('div', {
      style: `
        font-size: 14px;
        font-weight: 600;
        color: #333;
        margin-bottom: 6px;
      `
    });
    errorTitle.textContent = 'Cannot Display Page';
    errorContainer.appendChild(errorTitle);

    const errorMessage = this.createElement('div', {
      style: `
        font-size: 12px;
        color: #666;
        margin-bottom: 16px;
        max-width: 300px;
      `
    });
    errorMessage.textContent = 'This website prevents embedding in iframes.';
    errorContainer.appendChild(errorMessage);

    const openInTabButton = this.createElement('button', {
      style: `
        background: #4CAF50;
        color: white;
        border: none;
        padding: 8px 16px;
        border-radius: 4px;
        cursor: pointer;
        font-size: 12px;
        font-weight: 500;
      `
    });
    openInTabButton.textContent = 'Open in New Tab';
    openInTabButton.onclick = () => {
      window.open(url, '_blank');
      requestClosePopover();
    };
    errorContainer.appendChild(openInTabButton);

    // Create iframe full-bleed; src is set only after mobile UA is installed (when needed).
    const iframe = this.createElement('iframe', {
      tabindex: '0',
      style: `
        border: none;
        width: 100%;
        max-width: none;
        height: 100%;
        flex: 1 1 auto;
        align-self: stretch;
        background: #fff;
      `
    });
    iframeRef = iframe;
    this.popoverIframeElement = iframe;
    this.popoverIframeWindow = iframe.contentWindow || null;

    // Initialize the iframe bridge; pass closeKeys so Esc/E close from inside the frame.
    const sendBridgeInit = () => {
      try {
        iframe.contentWindow?.postMessage({
          type: 'KP_POPOVER_BRIDGE_INIT',
          closeKeys
        }, '*');
      } catch {
        // Ignore
      }
    };

    // Detect iframe load errors / hangs
    /** @type {ReturnType<typeof setTimeout>|null} */
    let loadTimeout = null;
    const armLoadTimeout = () => {
      if (loadTimeout) {
        try { clearTimeout(loadTimeout); } catch { /* ignore */ }
      }
      loadTimeout = setTimeout(() => {
        console.log('[KeyPilot] Iframe load timeout - showing error as fallback');
        iframeViewport.style.display = 'none';
        errorContainer.style.display = 'flex';
      }, 30000);
    };
    const clearLoadTimeout = () => {
      if (!loadTimeout) return;
      try { clearTimeout(loadTimeout); } catch { /* ignore */ }
      loadTimeout = null;
    };

    iframe.onerror = () => {
      console.log('[KeyPilot] Iframe load error detected');
      clearLoadTimeout();
      iframeViewport.style.display = 'none';
      errorContainer.style.display = 'flex';
    };

    iframe.onload = () => {
      // Ignore the intermediate about:blank used when switching UA modes.
      try {
        const srcAttr = iframe.getAttribute('src') || '';
        if (srcAttr === 'about:blank' || iframe.src === 'about:blank') {
          return;
        }
      } catch { /* ignore */ }
      clearLoadTimeout();
      console.log('[KeyPilot] Iframe loaded successfully');
      sendBridgeInit();
    };

    iframeViewport.appendChild(iframe);
    this.popoverContainer.appendChild(header);
    this.popoverContainer.appendChild(iframeViewport);
    this.popoverContainer.appendChild(errorContainer);

    // Mark this as a preview popover (for cleanup logic)
    this._isPreviewPopover = true;

    // Mount directly to document.body (no backdrop/blur for preview popover)
    try {
      document.body.appendChild(this.popoverContainer);
    } catch (e) {
      console.error('[KeyPilot] Failed to mount preview popover:', e);
      this._isPreviewPopover = false;
      return;
    }

    // Apply UA for remembered/default mode before first navigation (desktop = no spoof).
    void (async () => {
      try {
        // First apply: set UA without reload; then assign src once.
        await applyViewportMode(viewportMode, { reload: false });
      } catch (e) {
        console.warn('[KeyPilot] Failed to prepare preview viewport mode:', e?.message || e);
      }
      try {
        armLoadTimeout();
        iframe.src = url;
        this.popoverIframeWindow = iframe.contentWindow || null;
      } catch (e) {
        console.error('[KeyPilot] Failed to load preview URL:', e?.message || e);
        clearLoadTimeout();
        iframeViewport.style.display = 'none';
        errorContainer.style.display = 'flex';
      }
    })();

    // Add click-outside-to-close handler for preview popover
    this._popoverClickOutsideHandler = (e) => {
      // Check if click is outside the popover container
      if (this.popoverContainer && !this.popoverContainer.contains(e.target)) {
        console.log('[KeyPilot] Click outside preview popover, closing');
        requestClosePopover();
      }
    };
    // Use a small delay to avoid immediately closing if the preview key click triggered this
    setTimeout(() => {
      if (this.popoverContainer) {
        document.addEventListener('mousedown', this._popoverClickOutsideHandler, true);
      }
    }, 100);

    sendBridgeInit();

    // Retry window for iframe bridge init
    try {
      let attemptsLeft = 6;
      this.popoverInitTimer = setInterval(() => {
        if (!this.popoverContainer || attemptsLeft <= 0) {
          clearInterval(this.popoverInitTimer);
          this.popoverInitTimer = null;
          return;
        }
        attemptsLeft -= 1;
        sendBridgeInit();
      }, 250);
    } catch {
      // Ignore
    }

    // Don't prevent body scroll for preview popover - page should remain interactive

    // Parent-side close while focus is on chrome (titlebar / buttons).
    // When focus is in the iframe, Esc/E are handled by the bridge → REQUEST_CLOSE.
    const handlePopoverKeyDown = (e) => {
      console.log('[KeyPilot] Preview popover key event:', e.key);

      if (e.key === 'Escape' || closeKeys.includes(String(e.key))) {
        // Only handle here if focus is still in the parent document.
        // (If focus is in the iframe, the bridge already owns these keys.)
        const ae = document.activeElement;
        const focusInIframe = ae === iframeRef || ae === iframe;
        if (focusInIframe) return;

        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        requestClosePopover();
        return;
      }
    };

    document.addEventListener('keydown', handlePopoverKeyDown, true);
    this.popoverContainer.addEventListener('keydown', handlePopoverKeyDown, true);

    this.popoverKeyHandler = handlePopoverKeyDown;

    // Listen for messages from iframe bridge
    this.popoverMessageHandler = (event) => {
      const data = event?.data;
      if (!data || typeof data.type !== 'string') return;
      if (this.popoverIframeWindow && event.source !== this.popoverIframeWindow) return;

      if (data.type === 'KP_POPOVER_BRIDGE_READY') {
        this.popoverBridgeReady = true;
        // Auto-focus iframe so full KeyPilot works on the preview page without a click.
        // Esc/E close via bridge → parent; hybrid focus returns to chrome on titlebar hover.
        this._focusPopoverIframe(iframeRef);
        this._installPopoverHybridFocus({
          iframe: iframeRef,
          chromeEls: [
            header,
            closeButton,
            viewportModeControl.root,
            previewOpenActions
          ].filter(Boolean),
          focusChromeEl: closeButton || header
        });
        return;
      }

      if (data.type === 'KP_POPOVER_REQUEST_CLOSE') {
        if (closeKeys.includes(String(data.key))) requestClosePopover();
      }

      if (data.type === 'KP_POPOVER_LAUNCH_WALKTHROUGH') {
        requestClosePopover();
        try {
          const ob = window.__KeyPilotOnboarding;
          if (ob && typeof ob.resetTutorial === 'function') {
            void ob.resetTutorial();
          }
        } catch {
          // ignore
        }
        return;
      }

      if (data.type === 'KP_POPOVER_BRIDGE_KEYDOWN') {
        const k = String(data.key || '');
        if (k === 'f' || k === 'F') {
          clickCloseIfHovered();
        }
      }
    };
    window.addEventListener('message', this.popoverMessageHandler, true);

    // Until bridge ready, park focus on chrome.
    try {
      (closeButton || header)?.focus?.();
    } catch (_e) {
      try {
        this.popoverContainer.focus();
      } catch (_e2) {
        // Ignore
      }
    }
  }

  // =============================================================================
  // DEBUG PANEL - Performance metrics display in upper-right corner
  // =============================================================================

  /**
   * Initialize debug panel if enabled
   */
  initDebugPanel() {
    if (!FEATURE_FLAGS.ENABLE_DEBUG_PANEL) return;

    this.createDebugPanel();
    this.startDebugPanelUpdates();
  }

  /**
   * Create the debug panel element
   */
  createDebugPanel() {
    if (this.debugPanel) return;

    this.debugPanel = document.createElement('div');
    this.debugPanel.id = 'kpv2-debug-panel';
    this.debugPanel.style.cssText = `
      position: fixed;
      top: 10px;
      right: 10px;
      background: rgba(0, 0, 0, 0.9);
      color: #00ff00;
      font-family: monospace;
      font-size: 11px;
      padding: 8px 12px;
      border-radius: 4px;
      border: 1px solid #333;
      z-index: ${Z_INDEX.DEBUG_HUD};
      pointer-events: none;
      max-width: 300px;
      white-space: pre-wrap;
      line-height: 1.4;
    `;

    document.body.appendChild(this.debugPanel);
  }

  /**
   * Start periodic updates of debug panel data
   */
  startDebugPanelUpdates() {
    if (!this.debugPanel) return;

    // Update immediately
    this.updateDebugPanel();

    // Update every 2 seconds
    this.debugPanelUpdateInterval = setInterval(() => {
      this.updateDebugPanel();
    }, 2000);
  }

  /**
   * Update debug panel with current metrics
   */
  updateDebugPanel() {
    if (!this.debugPanel || !FEATURE_FLAGS.ENABLE_DEBUG_PANEL) return;

    const data = this.collectDebugData();
    const content = this.formatDebugContent(data);

    this.debugPanel.textContent = content;
  }

  /**
   * Collect debug data from various sources
   */
  collectDebugData() {
    const intersectionManager = window.keyPilot?.intersectionManager;
    const complexPageDetector = intersectionManager?.complexPageDetector;
    const currentState = window.keyPilot?.state?.getState?.() || {};

    // Get hover element information
    const hoverElement = currentState.focusEl;
    let hoverInfo = 'None';
    let clickableReasons = [];
    if (hoverElement) {
      const tagName = hoverElement.tagName?.toLowerCase() || 'unknown';
      const className = hoverElement.className ? `.${hoverElement.className.split(' ')[0]}` : '';
      const id = hoverElement.id ? `#${hoverElement.id}` : '';
      const href = hoverElement.href ? ` → ${hoverElement.href.substring(0, 30)}...` : '';
      const text = hoverElement.textContent?.trim().substring(0, 20) || '';
      hoverInfo = `${tagName}${id}${className}${href ? href : text ? ` "${text}..."` : ''}`;

      // Analyze why this element is clickable
      clickableReasons = this.analyzeClickableReasons(hoverElement);
    }

    const isComplexPage = complexPageDetector ? complexPageDetector.complexityLevel !== 'low' : false;

    return {
      // Page complexity
      complexityLevel: complexPageDetector?.complexityLevel || 'unknown',
      isComplexPage: isComplexPage,

      // Hover information
      hoverElement: hoverInfo,
      clickableReasons: clickableReasons,

      // Element counts
      totalElements: document.querySelectorAll('*').length,
      interactiveElements: document.querySelectorAll(
        'a[href], button, input, select, textarea, [role="button"], [role="link"], [contenteditable="true"], [onclick]'
      ).length,

      // IO metrics
      ioObservations: intersectionManager?.observedInteractiveElements?.size || 0,
      visibleElements: intersectionManager?.visibleInteractiveElements?.size || 0,

      // RBush metrics
      rbushEnabled: intersectionManager?._rtreeEnabled?.() || false,
      rbushItems: intersectionManager?._rtreeItemsByElement?.size || 0,
      rbushQueries: intersectionManager?.metrics?.rtreeQueries || 0,
      rbushHits: intersectionManager?.metrics?.rtreeHits || 0,

      // Performance metrics
      cacheHits: intersectionManager?.metrics?.cacheHits || 0,
      cacheMisses: intersectionManager?.metrics?.cacheMisses || 0,
      observerUpdates: intersectionManager?.metrics?.observerUpdates || 0,

      // Culling stats (only for medium/high complexity sites)
      culledCount: isComplexPage ? (intersectionManager?.metrics?.culledCount || 0) : 0,
      totalCulled: isComplexPage ? (intersectionManager?.metrics?.totalCulled || 0) : 0,

      // Rendering mode
      renderingMode: this.renderingMode || 'unknown',

      // Memory estimate
      estimatedMemory: this.estimateMemoryUsage()
    };
  }

  /**
   * Estimate memory usage of various components
   */
  estimateMemoryUsage() {
    const intersectionManager = window.keyPilot?.intersectionManager;
    let memoryKB = 0;

    // Estimate RBush memory (rough approximation)
    if (intersectionManager?._rtreeItemsByElement) {
      memoryKB += intersectionManager._rtreeItemsByElement.size * 0.5; // ~0.5KB per item
    }

    // Estimate element cache memory
    if (intersectionManager?.elementPositionCache) {
      memoryKB += intersectionManager.elementPositionCache.size * 1; // ~1KB per cached element
    }

    return Math.round(memoryKB);
  }

  /**
   * Analyze why an element is considered clickable
   */
  analyzeClickableReasons(element) {
    if (!element || element.nodeType !== 1) return [];

    const reasons = [];

    // Check selector matches
    const focusableSel = 'a[href], button, input, select, textarea, video, audio, [contenteditable="true"], [role="button"], [role="link"], [role="checkbox"], [role="radio"], [role="tab"], [data-action], [data-toggle], [data-click], [data-href], [data-link], [vue-click], [ng-click]';
    const matchesSelector = element.matches && element.matches(focusableSel);
    if (matchesSelector) {
      reasons.push('CSS Selector Match');
    }

    // Check role attribute
    const role = (element.getAttribute && (element.getAttribute('role') || '').trim().toLowerCase()) || '';
    const clickableRoles = ['link', 'button', 'slider', 'checkbox', 'radio', 'tab', 'menuitem', 'option', 'switch', 'treeitem', 'combobox', 'spinbutton'];
    const hasRole = role && clickableRoles.includes(role);
    if (hasRole) {
      reasons.push(`ARIA Role: "${role}"`);
    }

    // Check click handlers
    const hasOnClick = element.onclick || element.getAttribute('onclick');
    const elementDetector = window.keyPilot?.elementDetector;
    const hasTrackedClickHandler = elementDetector?.hasTrackedClickHandler?.(element);
    if (hasOnClick) {
      reasons.push('onclick Attribute');
    }
    if (hasTrackedClickHandler) {
      reasons.push('Event Listener (click)');
    }

    // Check cursor style (always check, not just when no other reasons found).
    // Use ElementDetector so CUSTOM_CURSORS mode still reports real page pointers.
    try {
      const hasCursor = elementDetector && typeof elementDetector.hasExplicitCursorPointer === 'function'
        ? elementDetector.hasExplicitCursorPointer(element)
        : !!(window.getComputedStyle && window.getComputedStyle(element).cursor === 'pointer');
      if (hasCursor) {
        reasons.push('CSS cursor: pointer');
      }
    } catch {
      // Ignore getComputedStyle errors
    }

    // If element is determined to be clickable by isLikelyInteractive but we haven't found any reasons,
    // add a generic reason to ensure we show something
    if (reasons.length === 0) {
      try {
        const elementDetector = window.keyPilot?.elementDetector;
        if (elementDetector && typeof elementDetector.isLikelyInteractive === 'function') {
          const isClickable = elementDetector.isLikelyInteractive(element);
          if (isClickable) {
            // If it's clickable but we didn't find a specific reason, check more thoroughly
            // This handles cases where the tracked click handler might not be detected yet
            // or where cursor pointer wasn't checked due to other conditions
            reasons.push('Likely Interactive (detected by element detector)');
          }
        }
      } catch (e) {
        // If isLikelyInteractive check fails, still try to provide a reason
        // Check if element is in the intersection observer's observed elements
        const intersectionManager = window.keyPilot?.intersectionManager;
        if (intersectionManager?.observedInteractiveElements?.has?.(element)) {
          reasons.push('Observed as Interactive Element');
        }
      }
    }

    return reasons;
  }

  /**
   * Format debug data into readable display content
   */
  formatDebugContent(data) {
    const cacheHitRate = data.cacheHits + data.cacheMisses > 0
      ? Math.round((data.cacheHits / (data.cacheHits + data.cacheMisses)) * 100)
      : 0;

    const rbushHitRate = data.rbushQueries > 0
      ? Math.round((data.rbushHits / data.rbushQueries) * 100)
      : 0;

    const cullingSection = data.isComplexPage ? `

🗑️  Spatial Culling
Last Cull: ${data.culledCount.toLocaleString()}
Total Culled: ${data.totalCulled.toLocaleString()}` : '';

    const clickableReasonsSection = data.clickableReasons.length > 0
      ? `\n🔍 Clickable Because:\n${data.clickableReasons.map(reason => `  • ${reason}`).join('\n')}`
      : '';

    return `KeyPilot Debug Panel
───────────────────
Complexity: ${data.complexityLevel.toUpperCase()} ${data.isComplexPage ? '🔴' : '🟢'}
Renderer: ${data.renderingMode.toUpperCase()}

🎯 Hover: ${data.hoverElement}${clickableReasonsSection}

📊 Elements
Total: ${data.totalElements.toLocaleString()}
Interactive: ${data.interactiveElements.toLocaleString()}

👁️  IO Observer
Observed: ${data.ioObservations.toLocaleString()}
Visible: ${data.visibleElements.toLocaleString()}

🌳 RBush Index
Enabled: ${data.rbushEnabled ? '✅' : '❌'}
Items: ${data.rbushItems.toLocaleString()}
Queries: ${data.rbushQueries.toLocaleString()}
Hit Rate: ${rbushHitRate}%${cullingSection}

⚡ Performance
Cache Hits: ${data.cacheHits.toLocaleString()} (${cacheHitRate}%)
Observer Updates: ${data.observerUpdates.toLocaleString()}

💾 Memory: ~${data.estimatedMemory}KB`;
  }

  /**
   * Clean up debug panel
   */
  cleanupDebugPanel() {
    if (this.debugPanelUpdateInterval) {
      clearInterval(this.debugPanelUpdateInterval);
      this.debugPanelUpdateInterval = null;
    }

    if (this.debugPanel) {
      this.debugPanel.remove();
      this.debugPanel = null;
    }
  }
}


  // Module: src/modules/style-manager.js
/**
 * CSS injection and style management
 */

const BLUE_TINT_FILTER_ID = 'keypilot-blue-tint-filter';
const BLUE_TINT_SVG_ID = 'keypilot-blue-tint-filter-svg';

/**
 * Print / Save-as-PDF CSS for KeyPilot.
 *
 * Two tiers (critical):
 * - Tier A: hide pure injected chrome (control strip, overlays, popovers, …).
 * - Tier B: neutralize decorations applied to host page elements (hover rings,
 *   focus filters, text-input tints). Never display:none those — they are real
 *   page content with KP classes temporarily attached.
 *
 * Keep early-inject EARLY_CSS print block in sync with this helper.
 *
 * @returns {string}
 */
function buildKeyPilotPrintCss() {
  const hide = [
    // Cursor + style filter host
    `#${ELEMENT_IDS.CURSOR}`,
    `#${BLUE_TINT_SVG_ID}`,
    // OverlayManager backends
    `.${CSS_CLASSES.CANVAS_OVERLAY}`,
    `.${CSS_CLASSES.CSS_PROPS_OVERLAY}`,
    `.${CSS_CLASSES.FOCUS_OVERLAY}`,
    `.${CSS_CLASSES.DELETE_OVERLAY}`,
    `.${CSS_CLASSES.INSPECTOR_OVERLAY}`,
    `.${CSS_CLASSES.INSPECTOR_MODE_INDICATOR}`,
    `.${CSS_CLASSES.COLS_OVERLAY}`,
    `.${CSS_CLASSES.COLS_SLIP_BAR}`,
    `.${CSS_CLASSES.HIGHLIGHT_OVERLAY}`,
    `.${CSS_CLASSES.HIGHLIGHT_SELECTION}`,
    `.${CSS_CLASSES.FOCUSED_TEXT_OVERLAY}`,
    `.${CSS_CLASSES.RIPPLE}`,
    `.${CSS_CLASSES.FOCUS_PULSE}`,
    `.${CSS_CLASSES.FOCUS_FLASH}`,
    `.${CSS_CLASSES.FOCUS_DASH}`,
    `.${CSS_CLASSES.FOCUS_MARQUEE}`,
    `.${CSS_CLASSES.IMAGE_COPY_PULSE}`,
    `.${CSS_CLASSES.VIEWPORT_MODAL_FRAME}`,
    `.${CSS_CLASSES.ACTIVE_TEXT_INPUT_FRAME}`,
    `.${CSS_CLASSES.ESC_EXIT_LABEL}`,
    // Omnibox + modal chrome
    `.${CSS_CLASSES.OMNIBOX_BACKDROP}`,
    `.${CSS_CLASSES.OMNIBOX_PANEL}`,
    `.${CSS_CLASSES.POPUP_BACKDROP}`,
    // Other injected chrome (hard-coded class names in modules)
    '.kpv2-toggle-notification',
    '.kpv2-flash-notification',
    '.kpv2-tab-history-panel',
    '.kpv2-popover-container',
    '.kpv2-preview-popover-container',
    '#kpv2-debug-panel',
    '#kpv2-rectangle-intersection-root',
    // Light-DOM UI roots (kp-*)
    '.kp-control-strip',
    '.kp-floating-keyboard-help',
    '.kp-onboarding-panel',
    '.kp-launcher-container',
    '.kp-keybindings-popover',
    // Early-inject / attribute markers
    '[data-kp-control-strip]',
    '[data-kp-early-control-strip]',
    '[data-kp-early-floating-keyboard]',
    '[data-kp-ephemeral-effect]'
  ].join(',\n    ');

  return `
      /* Hide KeyPilot chrome + neutralize host decorations when printing / saving as PDF.
         Placed last so !important print rules beat earlier screen !important rules. */
      @media print {
        ${hide} {
          display: none !important;
          visibility: hidden !important;
        }

        /* Tier B: host-page hover / mode decorations — reset paint, keep the element. */
        .keypilot-focus-element,
        [data-kp-focus],
        .${CSS_CLASSES.FOCUS},
        .${CSS_CLASSES.DELETE},
        .${CSS_CLASSES.COLS},
        .${CSS_CLASSES.INSPECTOR},
        .${CSS_CLASSES.HIGHLIGHT} {
          outline: none !important;
          outline-offset: 0 !important;
          box-shadow: none !important;
          filter: none !important;
        }

        .keypilot-focus-element,
        [data-kp-focus="1"] {
          background: transparent !important;
        }

        .${CSS_CLASSES.TEXT_HOVER_INPUT},
        .${CSS_CLASSES.TEXT_HOVER_INPUT_PARENT},
        .${CSS_CLASSES.TEXT_FOCUS_INPUT},
        .${CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT} {
          background-image: none !important;
          background-color: revert !important;
          color: revert !important;
          text-shadow: none !important;
          caret-color: auto !important;
          filter: none !important;
          box-shadow: none !important;
          outline: none !important;
        }

        .${CSS_CLASSES.TEXT_HOVER_INPUT}::placeholder,
        .${CSS_CLASSES.TEXT_FOCUS_INPUT}::placeholder {
          color: revert !important;
          text-shadow: none !important;
        }
      }
  `;
}

/**
 * Build a CSS `url("data:image/svg+xml,...")` background for tiny upper-left
 * hint text painted on text inputs (not a DOM overlay).
 *
 * @param {string} text
 * @param {{ fill?: string, fontSize?: number }} [opts]
 * @returns {string}
 */
function buildTextInputHintDataUri(text, opts = {}) {
  const raw = String(text || '').trim() || ' ';
  const fill = String(opts.fill || 'rgba(90, 45, 0, 0.78)');
  const fontSize = Number.isFinite(opts.fontSize) ? opts.fontSize : 11;
  // Escape for XML text content.
  const safe = raw
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;');
  // Approximate width (system UI is roughly 0.55–0.62em per glyph at this size).
  const width = Math.max(48, Math.ceil(raw.length * fontSize * 0.58) + 10);
  const height = fontSize + 5;
  const svg =
    `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">` +
    `<text x="0" y="${fontSize}" ` +
    `font-family="-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif" ` +
    `font-size="${fontSize}" font-weight="600" fill="${fill}" ` +
    `paint-order="stroke" stroke="rgba(255,255,255,0.55)" stroke-width="2.5">${safe}</text>` +
    `</svg>`;
  return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`;
}

class StyleManager {
  constructor() {
    this.injectedStyles = new Set();
    this.shadowRootStyles = new Map(); // Track shadow root styles for cleanup
    this.isEnabled = true; // Track if styles should be active
    // When false, KeyPilot must not override the page cursor at all.
    this.cursorOverridesEnabled = false;

    // SVG background-image hints for orange text inputs (hover / focus).
    this._textHoverHintLabel = 'Press F to select text field';
    this._textFocusHintLabel = 'press Esc to exit';
    this._textHoverHintUri = buildTextInputHintDataUri(this._textHoverHintLabel);
    this._textFocusHintUri = buildTextInputHintDataUri(this._textFocusHintLabel);
  }

  /**
   * Update layout-aware hint copy and push CSS variables onto the document.
   * @param {{ hover?: string, focus?: string }} labels
   */
  setTextInputHintLabels(labels = {}) {
    if (typeof labels.hover === 'string' && labels.hover.trim()) {
      this._textHoverHintLabel = labels.hover.trim();
      this._textHoverHintUri = buildTextInputHintDataUri(this._textHoverHintLabel);
    }
    if (typeof labels.focus === 'string' && labels.focus.trim()) {
      this._textFocusHintLabel = labels.focus.trim();
      this._textFocusHintUri = buildTextInputHintDataUri(this._textFocusHintLabel);
    }
    this._applyTextInputHintCssVars();
  }

  /**
   * Write hint background-image CSS vars on documentElement (and any tracked shadow roots).
   */
  _applyTextInputHintCssVars() {
    const hoverUri = this._textHoverHintUri || 'none';
    const focusUri = this._textFocusHintUri || 'none';
    try {
      document.documentElement.style.setProperty('--kpv2-text-hover-hint-image', hoverUri);
      document.documentElement.style.setProperty('--kpv2-text-focus-hint-image', focusUri);
    } catch { /* ignore */ }

    // Shadow roots inherit CSS variables from the host in most cases, but
    // open roots with closed inheritance may need the vars on :host — set on
    // the host element when we know it.
    try {
      for (const shadowRoot of this.shadowRootStyles.keys()) {
        const host = shadowRoot?.host;
        if (host && host.style) {
          host.style.setProperty('--kpv2-text-hover-hint-image', hoverUri);
          host.style.setProperty('--kpv2-text-focus-hint-image', focusUri);
        }
      }
    } catch { /* ignore */ }
  }

  setCursorOverridesEnabled(enabled) {
    const next = !!enabled;
    if (this.cursorOverridesEnabled === next) return;
    this.cursorOverridesEnabled = next;

    // Keep the html class in sync immediately.
    try {
      if (this.cursorOverridesEnabled) {
        document.documentElement.classList.add(CSS_CLASSES.CURSOR_HIDDEN);
      } else {
        document.documentElement.classList.remove(CSS_CLASSES.CURSOR_HIDDEN);
      }
    } catch {
      // ignore
    }

    // If we've already injected styles, update them in place so we don't require a full teardown.
    try {
      const mainStyle = document.getElementById(ELEMENT_IDS.STYLE);
      if (mainStyle && typeof mainStyle.textContent === 'string') {
        mainStyle.textContent = this._buildMainCSS();
      }
    } catch {
      // ignore
    }

    // Update any shadow-root styles we previously injected.
    try {
      for (const [shadowRoot, styleEl] of this.shadowRootStyles) {
        if (!shadowRoot || !styleEl) continue;
        try {
          styleEl.textContent = this._buildShadowCSS();
        } catch {
          // ignore
        }
      }
    } catch {
      // ignore
    }
  }

  _buildMainCSS() {
    const cursorCSS = this.cursorOverridesEnabled
      ? `
      html.${CSS_CLASSES.CURSOR_HIDDEN} * {
        cursor: var(--kpv2-cursor, auto) !important;
      }

      /* Ensure cursor overrides work on all interactive elements */
      html.${CSS_CLASSES.CURSOR_HIDDEN} a[href],
      html.${CSS_CLASSES.CURSOR_HIDDEN} button,
      html.${CSS_CLASSES.CURSOR_HIDDEN} input,
      html.${CSS_CLASSES.CURSOR_HIDDEN} select,
      html.${CSS_CLASSES.CURSOR_HIDDEN} textarea,
      html.${CSS_CLASSES.CURSOR_HIDDEN} [role="button"],
      html.${CSS_CLASSES.CURSOR_HIDDEN} [role="link"],
      html.${CSS_CLASSES.CURSOR_HIDDEN} [onclick],
      html.${CSS_CLASSES.CURSOR_HIDDEN} [tabindex] {
        cursor: var(--kpv2-cursor, auto) !important;
      }
      `
      : '';

    return `
      ${cursorCSS}
      
      .${CSS_CLASSES.FOCUS} { 
        filter: brightness(1.2) !important; 
      }
      
      .${CSS_CLASSES.DELETE} { 
        filter: brightness(0.8) contrast(1.2) !important; 
      }

      .${CSS_CLASSES.COLS} {
        filter: brightness(1.05) saturate(1.15) !important;
      }
      
      .${CSS_CLASSES.HIDDEN} { 
        display: none !important; 
      }
      
      @keyframes kpv2-ripple { 
        0% { transform: translate(-50%, -50%) scale(0.25); opacity: 0.35; }
        60% { transform: translate(-50%, -50%) scale(1); opacity: 0.2; }
        100% { transform: translate(-50%, -50%) scale(1.6); opacity: 0; }
      }

      /* F-key activation: outline scales up and fades out from the click target. */
      @keyframes kpv2-focus-pulse {
        0% {
          transform: scale(1);
          opacity: 1;
        }
        55% {
          transform: scale(1.12);
          opacity: 0.85;
        }
        100% {
          transform: scale(1.28);
          opacity: 0;
        }
      }

      /* F-key activation: hard strobe on the outline (minimal motion). */
      @keyframes kpv2-focus-flash {
        0% {
          opacity: 0;
          border-color: ${COLORS.FLASH_GREEN};
          box-shadow:
            0 0 0 0 transparent,
            0 0 0 0 transparent;
        }
        10% {
          opacity: 1;
          border-color: #ffffff;
          box-shadow:
            0 0 0 1px ${COLORS.FLASH_GREEN},
            0 0 8px 1px ${COLORS.FLASH_GREEN_GLOW};
        }
        40% {
          opacity: 1;
          border-color: ${COLORS.FLASH_GREEN};
          box-shadow:
            0 0 0 1px ${COLORS.FLASH_GREEN_SHADOW},
            0 0 5px 0 ${COLORS.FLASH_GREEN_GLOW};
        }
        100% {
          opacity: 0;
          border-color: ${COLORS.FLASH_GREEN};
          box-shadow:
            0 0 0 0 transparent,
            0 0 0 0 transparent;
        }
      }

      /* F-key activation: marquee chaser travels once around the perimeter, then fades. */
      @keyframes kpv2-focus-marquee-spin {
        0% {
          transform: translate(-50%, -50%) rotate(0deg);
          opacity: 1;
        }
        78% {
          opacity: 1;
        }
        100% {
          transform: translate(-50%, -50%) rotate(360deg);
          opacity: 0;
        }
      }

      @keyframes kpv2-focus-marquee-fade {
        0%, 70% {
          opacity: 1;
        }
        100% {
          opacity: 0;
        }
      }

      /* F-key activation: dashed stroke marches around the rect perimeter. */
      @keyframes kpv2-focus-dash-chase {
        0% {
          stroke-dashoffset: 0;
          opacity: 1;
        }
        78% {
          opacity: 1;
        }
        100% {
          stroke-dashoffset: calc(-1 * var(--kp-dash-peri, 200));
          opacity: 0;
        }
      }

      /*
       * Image copy (I-key): shutter flash → slight pop → shrink away
       * (read as "captured / pocketed" rather than the F-click expand-out pulse).
       */
      @keyframes kpv2-image-copy-pulse {
        0% {
          transform: scale(0.98);
          opacity: 0;
          background-color: ${COLORS.IMAGE_COPY_FLASH};
        }
        14% {
          transform: scale(1);
          opacity: 1;
          background-color: ${COLORS.IMAGE_COPY_FLASH};
        }
        32% {
          transform: scale(1.06);
          opacity: 1;
          background-color: ${COLORS.IMAGE_COPY_FILL};
        }
        100% {
          transform: scale(0.58);
          opacity: 0;
          background-color: ${COLORS.IMAGE_COPY_FILL};
        }
      }
      
      .${CSS_CLASSES.RIPPLE} { 
        position: fixed; 
        left: 0; 
        top: 0; 
        z-index: ${Z_INDEX.OVERLAYS}; 
        pointer-events: none; 
        width: 46px; 
        height: 46px; 
        border-radius: 50%; 
        background: radial-gradient(circle, ${COLORS.RIPPLE_GREEN} 0%, ${COLORS.RIPPLE_GREEN_MID} 60%, ${COLORS.RIPPLE_GREEN_TRANSPARENT} 70%); 
        animation: kpv2-ripple 420ms ease-out forwards; 
      }

      .${CSS_CLASSES.FOCUS_PULSE} {
        position: fixed;
        left: 0;
        top: 0;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        box-sizing: border-box;
        border: 3px solid ${COLORS.FLASH_GREEN};
        border-radius: 3px;
        box-shadow:
          0 0 0 2px ${COLORS.FLASH_GREEN_SHADOW},
          0 0 18px 4px ${COLORS.FLASH_GREEN_GLOW};
        background: transparent;
        transform-origin: center center;
        will-change: transform, opacity;
        animation: kpv2-focus-pulse 420ms cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
      }

      /* Flash / strobe: brief bright border + glow, almost no geometry motion. */
      .${CSS_CLASSES.FOCUS_FLASH} {
        position: fixed;
        left: 0;
        top: 0;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        box-sizing: border-box;
        border: 3px solid ${COLORS.FLASH_GREEN};
        border-radius: 3px;
        background: transparent;
        will-change: opacity, box-shadow, border-color;
        animation: kpv2-focus-flash 320ms ease-out forwards;
      }

      /* Dash chase: SVG host; stroke animation lives on the child rect. */
      .${CSS_CLASSES.FOCUS_DASH} {
        position: fixed;
        left: 0;
        top: 0;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        overflow: visible;
        filter: drop-shadow(0 0 6px ${COLORS.FLASH_GREEN_GLOW});
      }

      .${CSS_CLASSES.FOCUS_DASH}-stroke {
        will-change: stroke-dashoffset, opacity;
        animation: kpv2-focus-dash-chase 720ms linear forwards;
      }

      /*
       * Marquee click effect: a bright segment races around the element border
       * (theater marquee / runway lights). Mask keeps only a thin ring visible.
       */
      .${CSS_CLASSES.FOCUS_MARQUEE} {
        position: fixed;
        left: 0;
        top: 0;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        box-sizing: border-box;
        border-radius: 3px;
        overflow: hidden;
        padding: 3px;
        background: transparent;
        /* Ring-only mask: paint border, punch out the interior. */
        -webkit-mask:
          linear-gradient(#fff 0 0) content-box,
          linear-gradient(#fff 0 0);
        -webkit-mask-composite: xor;
        mask:
          linear-gradient(#fff 0 0) content-box,
          linear-gradient(#fff 0 0);
        mask-composite: exclude;
        animation: kpv2-focus-marquee-fade 720ms ease-out forwards;
      }

      .${CSS_CLASSES.FOCUS_MARQUEE}::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 220%;
        height: 220%;
        transform: translate(-50%, -50%) rotate(0deg);
        background: conic-gradient(
          from 0deg,
          transparent 0deg,
          transparent 250deg,
          ${COLORS.FLASH_GREEN_SHADOW} 280deg,
          ${COLORS.FLASH_GREEN} 310deg,
          #ffffff 328deg,
          ${COLORS.FLASH_GREEN} 342deg,
          transparent 360deg
        );
        box-shadow: 0 0 14px 2px ${COLORS.FLASH_GREEN_GLOW};
        will-change: transform, opacity;
        animation: kpv2-focus-marquee-spin 720ms linear forwards;
      }

      .${CSS_CLASSES.IMAGE_COPY_PULSE} {
        position: fixed;
        left: 0;
        top: 0;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        box-sizing: border-box;
        border: 3px solid ${COLORS.IMAGE_COPY_FRAME};
        border-radius: 6px;
        box-shadow:
          0 0 0 2px ${COLORS.IMAGE_COPY_FRAME_SHADOW},
          0 0 22px 6px ${COLORS.IMAGE_COPY_FRAME_GLOW};
        background-color: ${COLORS.IMAGE_COPY_FILL};
        transform-origin: center center;
        will-change: transform, opacity, background-color;
        animation: kpv2-image-copy-pulse 520ms cubic-bezier(0.2, 0.75, 0.25, 1) forwards;
      }
      
      .${CSS_CLASSES.FOCUS_OVERLAY} { 
        position: fixed; 
        pointer-events: none; 
        z-index: ${Z_INDEX.OVERLAYS}; 
        border: 3px solid ${COLORS.FOCUS_GREEN}; 
        box-shadow: 0 0 0 2px ${COLORS.GREEN_SHADOW}, 0 0 10px 2px ${COLORS.GREEN_SHADOW_BRIGHT}; 
        background: transparent; 
      }
      
      .${CSS_CLASSES.DELETE_OVERLAY} { 
        position: fixed; 
        pointer-events: none; 
        z-index: ${Z_INDEX.OVERLAYS}; 
        border: 3px solid ${COLORS.DELETE_RED}; 
        box-shadow: 0 0 0 2px ${COLORS.DELETE_SHADOW}, 0 0 12px 2px ${COLORS.DELETE_SHADOW_BRIGHT}; 
        background: transparent; 
      }

      .${CSS_CLASSES.COLS_OVERLAY},
      .${CSS_CLASSES.INSPECTOR_OVERLAY} {
        position: fixed;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS};
        border: 3px solid ${COLORS.COLS_PURPLE};
        box-shadow: 0 0 0 2px ${COLORS.COLS_SHADOW}, 0 0 12px 2px ${COLORS.COLS_SHADOW_BRIGHT};
        background: transparent;
      }

      .${CSS_CLASSES.INSPECTOR} {
        filter: brightness(1.05) saturate(1.1) !important;
      }

      /* Applied multicol target — variables set by ColumnLayoutManager */
      .${CSS_CLASSES.COLS_ACTIVE} {
        column-width: var(--kpv2-cols-width, 40ch) !important;
        column-gap: var(--kpv2-cols-gap, 1.5rem) !important;
        column-fill: auto !important;
        height: var(--kpv2-cols-height, 100vh) !important;
        max-height: var(--kpv2-cols-height, 100vh) !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        box-sizing: border-box !important;
        width: 100% !important;
        max-width: 100% !important;
        scrollbar-width: none !important;
      }
      .${CSS_CLASSES.COLS_ACTIVE}::-webkit-scrollbar {
        display: none !important;
        width: 0 !important;
        height: 0 !important;
      }

      /* Column widget shell (outline + integrated slip bar) */
      .${CSS_CLASSES.COLS_SHELL} {
        box-sizing: border-box;
      }
      .${CSS_CLASSES.COLS_BODY} {
        min-width: 0;
        min-height: 0;
      }
      .${CSS_CLASSES.COLS_SLIP_BAR} {
        box-sizing: border-box;
      }
      .${CSS_CLASSES.COLS_SLIP_BAR}[data-kp-slip-empty="1"] .${CSS_CLASSES.COLS_SLIP_TRACK} {
        opacity: 0.45;
      }
      .${CSS_CLASSES.COLS_EXPAND_BTN}:focus-visible,
      .${CSS_CLASSES.COLS_CLOSE_BTN}:focus-visible {
        outline: 2px solid ${COLORS.COLS_PURPLE_BRIGHT};
        outline-offset: 1px;
      }

      /* Page-mode markers (html/body): critical layout is set inline by ColumnLayoutManager. */
      
      .${CSS_CLASSES.HIGHLIGHT_OVERLAY} { 
        position: fixed; 
        pointer-events: none; 
        z-index: ${Z_INDEX.OVERLAYS}; 
        border: 3px solid ${COLORS.HIGHLIGHT_BLUE}; 
        box-shadow: 0 0 0 2px ${COLORS.HIGHLIGHT_SHADOW}, 0 0 12px 2px ${COLORS.HIGHLIGHT_SHADOW_BRIGHT}; 
        background: transparent; 
      }
      
      .${CSS_CLASSES.HIGHLIGHT_SELECTION} { 
        position: fixed; 
        pointer-events: none; 
        z-index: ${Z_INDEX.HIGHLIGHT_SELECTION}; 
        background: ${COLORS.HIGHLIGHT_SELECTION_BG}; 
        border: 1px solid ${COLORS.HIGHLIGHT_SELECTION_BORDER}; 
      }

      /* Omnibox overlay */
      .${CSS_CLASSES.OMNIBOX_BACKDROP} {
        position: fixed;
        inset: 0;
        z-index: ${Z_INDEX.OMNIBOX};
        background: rgba(0,0,0,0.35);
        backdrop-filter: blur(6px);
        -webkit-backdrop-filter: blur(6px);
      }
      .${CSS_CLASSES.OMNIBOX_PANEL} {
        position: absolute;
        top: 18vh;
        left: 50%;
        transform: translateX(-50%);
        width: min(880px, calc(100vw - 32px));
        border-radius: 14px;
        border: 1px solid ${COLORS.ORANGE_BORDER};
        box-shadow: 0 18px 60px rgba(0,0,0,0.55);
        background: rgba(20, 20, 20, 0.88);
        overflow: hidden;
      }
      .${CSS_CLASSES.OMNIBOX_INPUT} {
        width: 100%;
        box-sizing: border-box;
        border: none;
        outline: none;
        padding: 14px 16px;
        font-size: 18px;
        font-weight: 500;
        color: ${COLORS.TEXT_WHITE_PRIMARY};
        background: rgba(0,0,0,0);
        font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
      }
      .${CSS_CLASSES.OMNIBOX_SUGGESTIONS} {
        border-top: 1px solid rgba(255,140,0,0.15);
        max-height: 40vh;
        overflow-y: auto;
      }
      .${CSS_CLASSES.OMNIBOX_SUGGESTION} {
        padding: 10px 16px;
        cursor: default;
      }
      .${CSS_CLASSES.OMNIBOX_EMPTY} {
        padding: 10px 16px;
        font-size: 13px;
        color: ${COLORS.TEXT_WHITE_SECONDARY};
        font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
      }
      
      #${ELEMENT_IDS.CURSOR} { 
        position: fixed !important; 
        left: var(--cursor-x, 0) !important; 
        top: var(--cursor-y, 0) !important; 
        transform: translate(-50%, -50%) !important; 
        z-index: ${Z_INDEX.CURSOR} !important; 
        pointer-events: none !important;
        display: block !important;
        visibility: visible !important;
        will-change: transform, left, top !important;
      }
      
      .${CSS_CLASSES.VIEWPORT_MODAL_FRAME} {
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        border: 9pt solid ${COLORS.ORANGE};
        opacity: 0.7;
        pointer-events: none;
        z-index: ${Z_INDEX.VIEWPORT_MODAL_FRAME};
        box-sizing: border-box;
        will-change: transform;
      }
      
      @keyframes kpv2-pulse { 
        0% { opacity: 0.7; }
        50% { opacity: 1; }
        100% { opacity: 0.7; }
      }

      /* Text-mode focus: inset left-edge bar pulse (width via --kpv2-text-left-edge-width). */
      @keyframes kpv2-text-left-edge-pulse {
        0%, 100% {
          box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE_SHADOW};
        }
        50% {
          box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE};
        }
      }
      
      .${CSS_CLASSES.ACTIVE_TEXT_INPUT_FRAME} {
        position: fixed;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        border: var(--kpv2-text-stroke-width, 3px) solid ${COLORS.ORANGE};
        box-shadow: 0 0 0 2px ${COLORS.ORANGE_SHADOW}, 0 0 10px 2px ${COLORS.ORANGE_SHADOW_DARK};
        background: transparent;
        animation: kpv2-pulse 1.5s ease-in-out infinite;
        will-change: transform, opacity;
      }
      
      .${CSS_CLASSES.ACTIVE_TEXT_INPUT_FRAME}::before {
        content: "";
        display: none;
      }
      
      .${CSS_CLASSES.ESC_EXIT_LABEL} {
        position: fixed;
        pointer-events: none;
        z-index: ${Z_INDEX.OVERLAYS_ABOVE};
        background: ${COLORS.ORANGE};
        color: white;
        padding: 4px 8px;
        font-size: 12px;
        font-weight: bold;
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        white-space: nowrap;
        border-radius: 2px;
        animation: kpv2-pulse 1.5s ease-in-out infinite;
        will-change: transform, opacity;
      }

      .${CSS_CLASSES.ESC_EXIT_LABEL} kbd {
        background: rgba(255, 255, 255, 0.2);
        border: 1px solid rgba(255, 255, 255, 0.3);
        border-radius: 2px;
        padding: 1px 3px;
        font-family: monospace;
        font-size: 11px;
        font-weight: bold;
        color: white;
      }

      .${CSS_CLASSES.ESC_EXIT_LABEL} .countdown-number {
        background: rgba(0, 0, 0, 0.8);
        color: white;
        padding: 1px 3px;
        border-radius: 2px;
        font-weight: bold;
        font-size: 11px;
      }
      
      /* Add left padding to focused text inputs */
      input:focus,
      textarea:focus,
      [contenteditable="true"]:focus,
      [contenteditable=""]:focus {
        padding-left: 5pt !important;
      }

      /* Text inputs: hover / focus treatment + SVG hint labels.
         - Hover: orange outline only + SVG "Press F to select…" (no background wash)
         - Focus background_tint: full orange wash + SVG "press Esc to exit"
         - Focus left_edge (default): pulsating left inset bar + Esc SVG
         Hint copy is an SVG background-image (upper-left) — not a DOM overlay. */
      .${CSS_CLASSES.TEXT_FOCUS_INPUT},
      .${CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT} {
        color: var(--kpv2-text-input-color, rgba(55, 55, 55, 0.96)) !important;
        text-shadow: var(--kpv2-text-input-text-shadow, 1px 1px 0 rgba(255, 255, 255, 0.95)) !important;
      }

      /* Background-tint focus parents get a wash; left-edge parents stay clean. */
      .${CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT}:not(.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE}) {
        background-color: var(--kpv2-text-input-focus-bg, rgba(255, 140, 0, 0.42)) !important;
      }

      /* Hover: SVG hint only — no background wash (outline is separate). */
      .${CSS_CLASSES.TEXT_HOVER_INPUT} {
        background-image: var(--kpv2-text-hover-hint-image, none) !important;
        background-repeat: no-repeat !important;
        background-position: left 6px top 3px !important;
        background-size: auto 12px !important;
      }

      /* Focus (background tint style): wash + Esc SVG. */
      .${CSS_CLASSES.TEXT_FOCUS_INPUT}:not(.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE}) {
        background-color: var(--kpv2-text-input-focus-bg, rgba(255, 140, 0, 0.42)) !important;
        background-image: var(--kpv2-text-focus-hint-image, none) !important;
        background-repeat: no-repeat !important;
        background-position: left 6px top 3px !important;
        background-size: auto 12px !important;
      }

      /* Focus (default left-edge style): inset orange bar that pulses + Esc SVG. */
      .${CSS_CLASSES.TEXT_FOCUS_INPUT}.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE} {
        padding-left: calc(var(--kpv2-text-left-edge-width, 5px) + 4px) !important;
        background-color: transparent !important;
        background-image: var(--kpv2-text-focus-hint-image, none) !important;
        background-repeat: no-repeat !important;
        background-position: left calc(var(--kpv2-text-left-edge-width, 5px) + 6px) top 3px !important;
        background-size: auto 12px !important;
        box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE} !important;
        animation: kpv2-text-left-edge-pulse 1.5s ease-in-out infinite !important;
        will-change: box-shadow;
      }

      .${CSS_CLASSES.TEXT_FOCUS_INPUT} {
        caret-color: var(--kpv2-text-input-color, rgba(55, 55, 55, 0.96)) !important;
      }

      /* Keep placeholders readable when we force text color on focus wash. */
      .${CSS_CLASSES.TEXT_FOCUS_INPUT}::placeholder {
        color: var(--kpv2-text-input-placeholder-color, rgba(55, 55, 55, 0.62)) !important;
        text-shadow: var(--kpv2-text-input-text-shadow, 1px 1px 0 rgba(255, 255, 255, 0.85)) !important;
      }

      /*
       * Element styling for DOM hover mode.
       * Prefer [data-kp-focus] over class alone: SPAs (e.g. X) often reconcile
       * className on hover and strip unknown classes, while data-* attributes
       * usually survive. Keep the class for backwards compatibility / DevTools.
       * Outline-first — many sites transition/zero box-shadow.
       */
      .keypilot-focus-element,
      [data-kp-focus="1"] {
        outline: var(--keypilot-focus-ring-width, 3px) solid var(--keypilot-focus-ring-color, #2196f3) !important;
        outline-offset: 2px !important;
        box-shadow: var(--keypilot-focus-box-shadow, none) !important;
        filter: none !important;
      }

      .keypilot-focus-element.keypilot-focus-element--inset,
      [data-kp-focus="1"][data-kp-focus-inset="1"] {
        outline: var(--keypilot-focus-ring-width, 3px) solid var(--keypilot-focus-ring-color, #2196f3) !important;
        outline-offset: calc(-1 * var(--keypilot-focus-ring-width, 3px)) !important;
        box-shadow: var(--keypilot-focus-box-shadow, none) !important;
        filter: none !important;
      }

      /* Translucent fill wash (thumbnails/links) only when a fill color is set.
         Base ring rules never override the element's own background on hover. */
      .keypilot-focus-element--fill {
        background: var(--keypilot-focus-ring-bg-color, transparent) !important;
      }

      /* Keep left-edge pulse when DOM-hover also puts a ring on the focused field. */
      .keypilot-focus-element.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE},
      [data-kp-focus="1"].${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE} {
        box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE} !important;
        animation: kpv2-text-left-edge-pulse 1.5s ease-in-out infinite !important;
      }

      ${buildKeyPilotPrintCss()}
    `;
  }

  _buildShadowCSS() {
    const cursorCSS = this.cursorOverridesEnabled
      ? `
      /* Cursor override inside shadow DOM (archive.org and other web-components).
         Shadow roots don't inherit document-level CSS selectors like html.kpv2-cursor-hidden *,
         so we mirror the cursor rule via :host-context(). */
      :host-context(html.${CSS_CLASSES.CURSOR_HIDDEN}),
      :host-context(html.${CSS_CLASSES.CURSOR_HIDDEN}) * {
        cursor: var(--kpv2-cursor, auto) !important;
      }
      `
      : '';

    return `
      ${cursorCSS}

      .${CSS_CLASSES.FOCUS} { 
        filter: brightness(1.2) !important; 
      }
      
      .${CSS_CLASSES.DELETE} { 
        filter: brightness(0.8) contrast(1.2) !important; 
      }

      .${CSS_CLASSES.COLS} {
        filter: brightness(1.05) saturate(1.15) !important;
      }
      
      .${CSS_CLASSES.HIDDEN} { 
        display: none !important; 
      }
      
      /* Add left padding to focused text inputs in shadow DOM */
      input:focus,
      textarea:focus,
      [contenteditable="true"]:focus,
      [contenteditable=""]:focus {
        padding-left: 5pt !important;
      }

      @keyframes kpv2-text-left-edge-pulse {
        0%, 100% {
          box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE_SHADOW};
        }
        50% {
          box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE};
        }
      }

      /* Text inputs: same hover/focus + SVG hint treatment inside shadow DOM */
      .${CSS_CLASSES.TEXT_FOCUS_INPUT},
      .${CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT} {
        color: var(--kpv2-text-input-color, rgba(55, 55, 55, 0.96)) !important;
        text-shadow: var(--kpv2-text-input-text-shadow, 1px 1px 0 rgba(255, 255, 255, 0.95)) !important;
      }

      .${CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT}:not(.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE}) {
        background-color: var(--kpv2-text-input-focus-bg, rgba(255, 140, 0, 0.42)) !important;
      }

      .${CSS_CLASSES.TEXT_HOVER_INPUT} {
        background-image: var(--kpv2-text-hover-hint-image, none) !important;
        background-repeat: no-repeat !important;
        background-position: left 6px top 3px !important;
        background-size: auto 12px !important;
      }

      .${CSS_CLASSES.TEXT_FOCUS_INPUT}:not(.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE}) {
        background-color: var(--kpv2-text-input-focus-bg, rgba(255, 140, 0, 0.42)) !important;
        background-image: var(--kpv2-text-focus-hint-image, none) !important;
        background-repeat: no-repeat !important;
        background-position: left 6px top 3px !important;
        background-size: auto 12px !important;
      }

      .${CSS_CLASSES.TEXT_FOCUS_INPUT}.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE} {
        padding-left: calc(var(--kpv2-text-left-edge-width, 5px) + 4px) !important;
        background-color: transparent !important;
        background-image: var(--kpv2-text-focus-hint-image, none) !important;
        background-repeat: no-repeat !important;
        background-position: left calc(var(--kpv2-text-left-edge-width, 5px) + 6px) top 3px !important;
        background-size: auto 12px !important;
        box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE} !important;
        animation: kpv2-text-left-edge-pulse 1.5s ease-in-out infinite !important;
        will-change: box-shadow;
      }

      .${CSS_CLASSES.TEXT_FOCUS_INPUT} {
        caret-color: var(--kpv2-text-input-color, rgba(55, 55, 55, 0.96)) !important;
      }

      .${CSS_CLASSES.TEXT_FOCUS_INPUT}::placeholder {
        color: var(--kpv2-text-input-placeholder-color, rgba(55, 55, 55, 0.62)) !important;
        text-shadow: var(--kpv2-text-input-text-shadow, 1px 1px 0 rgba(255, 255, 255, 0.85)) !important;
      }

      /* Element styling for DOM hover mode in shadow DOM (settings-driven ring). */
      .keypilot-focus-element,
      [data-kp-focus="1"] {
        outline: var(--keypilot-focus-ring-width, 3px) solid var(--keypilot-focus-ring-color, #2196f3) !important;
        outline-offset: 2px !important;
        box-shadow: var(--keypilot-focus-box-shadow, none) !important;
        filter: none !important;
      }

      .keypilot-focus-element.keypilot-focus-element--inset,
      [data-kp-focus="1"][data-kp-focus-inset="1"] {
        outline: var(--keypilot-focus-ring-width, 3px) solid var(--keypilot-focus-ring-color, #2196f3) !important;
        outline-offset: calc(-1 * var(--keypilot-focus-ring-width, 3px)) !important;
        box-shadow: var(--keypilot-focus-box-shadow, none) !important;
        filter: none !important;
      }

      /* Translucent fill wash (thumbnails/links) only when a fill color is set.
         Base ring rules never override the element's own background on hover. */
      .keypilot-focus-element--fill {
        background: var(--keypilot-focus-ring-bg-color, transparent) !important;
      }

      /* Keep left-edge pulse when DOM-hover also puts a ring on the focused field. */
      .keypilot-focus-element.${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE},
      [data-kp-focus="1"].${CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE} {
        box-shadow: inset var(--kpv2-text-left-edge-width, 5px) 0 0 0 ${COLORS.ORANGE} !important;
        animation: kpv2-text-left-edge-pulse 1.5s ease-in-out infinite !important;
      }

      ${buildKeyPilotPrintCss()}
    `;
  }

  injectSharedStyles() {
    if (this.injectedStyles.has('main') || !this.isEnabled) return;

    const css = this._buildMainCSS();

    this.injectCSS(css, ELEMENT_IDS.STYLE);
    this.injectedStyles.add('main');

    // Ensure the SVG filter exists as a real DOM node (CSS alone can't define it).
    this._ensureBlueTintFilterInDocument();

    // Paint default text-input hint SVG background-images (layout can override later).
    this._applyTextInputHintCssVars();

    // Only hide/override the cursor when explicitly enabled.
    if (this.cursorOverridesEnabled) {
      document.documentElement.classList.add(CSS_CLASSES.CURSOR_HIDDEN);
    } else {
      document.documentElement.classList.remove(CSS_CLASSES.CURSOR_HIDDEN);
    }
  }

  _ensureBlueTintFilterInDocument() {
    this._ensureBlueTintFilterInRoot(document);
  }

  _ensureBlueTintFilterInShadowRoot(shadowRoot) {
    this._ensureBlueTintFilterInRoot(shadowRoot);
  }

  _ensureBlueTintFilterInRoot(root) {
    try {
      const queryRoot = root && typeof root.querySelector === 'function' ? root : document;
      const existing = queryRoot.querySelector(`#${BLUE_TINT_SVG_ID}`);
      if (existing) return;

      const ns = 'http://www.w3.org/2000/svg';
      const svg = document.createElementNS(ns, 'svg');
      svg.setAttribute('id', BLUE_TINT_SVG_ID);
      svg.setAttribute('aria-hidden', 'true');
      // Hidden, but present in the DOM for url(#...) references.
      svg.style.position = 'absolute';
      svg.style.width = '0';
      svg.style.height = '0';
      svg.style.overflow = 'hidden';
      svg.style.pointerEvents = 'none';

      const defs = document.createElementNS(ns, 'defs');
      const filter = document.createElementNS(ns, 'filter');
      filter.setAttribute('id', BLUE_TINT_FILTER_ID);
      // Consistent, non-"hue rotate" blue cast.
      filter.setAttribute('color-interpolation-filters', 'sRGB');

      const matrix = document.createElementNS(ns, 'feColorMatrix');
      matrix.setAttribute('type', 'matrix');
      // 4x5 matrix (RGBA). Stronger blue cast + slightly reduced red/green.
      // R' = 0.82R + 0.06G + 0.06B
      // G' = 0.06R + 0.82G + 0.06B
      // B' = 0.12R + 0.12G + 1.05B + 0.12
      matrix.setAttribute(
        'values',
        '0.82 0.06 0.06 0 0  0.06 0.82 0.06 0 0  0.12 0.12 1.05 0 0.12  0 0 0 1 0'
      );

      // Slight brightness + contrast boost to keep tinted elements punchy.
      const transfer = document.createElementNS(ns, 'feComponentTransfer');
      const fr = document.createElementNS(ns, 'feFuncR');
      const fg = document.createElementNS(ns, 'feFuncG');
      const fb = document.createElementNS(ns, 'feFuncB');
      // slope > 1 increases contrast, intercept lifts brightness.
      fr.setAttribute('type', 'linear');
      fr.setAttribute('slope', '1.08');
      fr.setAttribute('intercept', '0.03');
      fg.setAttribute('type', 'linear');
      fg.setAttribute('slope', '1.08');
      fg.setAttribute('intercept', '0.03');
      fb.setAttribute('type', 'linear');
      fb.setAttribute('slope', '1.10');
      fb.setAttribute('intercept', '0.04');
      transfer.appendChild(fr);
      transfer.appendChild(fg);
      transfer.appendChild(fb);

      filter.appendChild(matrix);
      filter.appendChild(transfer);
      defs.appendChild(filter);
      svg.appendChild(defs);

      const parent = root instanceof ShadowRoot
        ? root
        : (document.body || document.documentElement || document);
      parent.appendChild(svg);
    } catch {
      // If this fails, the hover ring still works; we just won't apply the tint.
    }
  }

  injectCSS(css, id) {
    const existing = document.getElementById(id);
    if (existing) return;

    try {
      const style = document.createElement('style');
      style.id = id;
      style.textContent = css;
      document.head.appendChild(style);
    } catch (error) {
      // On chrome:// pages and other restricted contexts, DOM modifications may be blocked
      console.warn('[StyleManager] Cannot inject CSS on this page:', error.message);
      // Continue without styles - KeyPilot will still work but without visual enhancements
    }
  }

  /**
   * Ensure KeyPilot CSS applies for a node that may live inside open shadow DOM.
   * Document-level styles do not pierce shadow boundaries, so we inject into the
   * owning open ShadowRoot on first use (lazy). Safe / idempotent.
   *
   * @param {Node|null|undefined} node
   * @returns {boolean} true if styles are available for this node (document or injected shadow)
   */
  ensureStylesForNode(node) {
    if (!this.isEnabled || !node) return false;

    let root = null;
    try {
      root = typeof node.getRootNode === 'function' ? node.getRootNode() : null;
    } catch {
      root = null;
    }

    // Light DOM / document: main stylesheet already covers these nodes.
    if (!root || root === document || root instanceof Document) {
      return true;
    }

    if (typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot) {
      const ok = this.injectIntoShadowRoot(root);
      // Keep optional IO discovery set in sync when we lazy-inject.
      if (ok) {
        try {
          window.keyPilot?.shadowDOMManager?.trackShadowRoot?.(root);
        } catch { /* ignore */ }
      }
      return ok;
    }

    // Closed shadow or unknown root: cannot inject.
    return false;
  }

  /**
   * Inject KeyPilot shadow CSS + blue-tint filter into an open shadow root.
   * Idempotent and wipe-resistant: Lit/SPA re-renders often replace shadow
   * children and drop our <style>. We always re-check the DOM (not only our
   * session Set) so archive.org-style nested open roots keep focus outlines.
   *
   * Used by:
   * - attachShadow pre-warm (ShadowDOMManager)
   * - lazy ensureStylesForNode on first hover/focus styling
   *
   * @param {ShadowRoot} shadowRoot
   * @returns {boolean}
   */
  injectIntoShadowRoot(shadowRoot) {
    if (!shadowRoot || !this.isEnabled) return false;

    try {
      // Prefer live DOM over injectedStyles Set — hosts may wipe our nodes.
      let style = typeof shadowRoot.querySelector === 'function'
        ? shadowRoot.querySelector('#keypilot-shadow-styles')
        : null;

      if (!style) {
        const css = this._buildShadowCSS();
        style = document.createElement('style');
        style.id = 'keypilot-shadow-styles';
        style.textContent = css;
        shadowRoot.appendChild(style);
      }

      // Filter must live in this shadow tree for url(#...) resolution on focus rings.
      this._ensureBlueTintFilterInShadowRoot(shadowRoot);

      this.injectedStyles.add(shadowRoot);
      this.shadowRootStyles.set(shadowRoot, style);

      // Keep text-input hint vars available on the host for CSS var resolution.
      try {
        const host = shadowRoot.host;
        if (host?.style) {
          host.style.setProperty('--kpv2-text-hover-hint-image', this._textHoverHintUri || 'none');
          host.style.setProperty('--kpv2-text-focus-hint-image', this._textFocusHintUri || 'none');
        }
      } catch { /* ignore */ }

      return true;
    } catch (error) {
      if (window.KEYPILOT_DEBUG) {
        console.warn('[StyleManager] Failed to inject styles into shadow root:', error);
      }
      return false;
    }
  }

  /**
   * Completely remove all KeyPilot CSS styles from the page
   * Used when extension is toggled off
   */
  removeAllStyles() {
    // Remove cursor hidden class
    document.documentElement.classList.remove(CSS_CLASSES.CURSOR_HIDDEN);

    // Remove main stylesheet
    const mainStyle = document.getElementById(ELEMENT_IDS.STYLE);
    if (mainStyle) {
      mainStyle.remove();
    }

    // Remove all shadow root styles
    for (const [shadowRoot, styleElement] of this.shadowRootStyles) {
      if (styleElement && styleElement.parentNode) {
        styleElement.remove();
      }
    }

    // Remove our injected SVG filters (document + tracked shadow roots)
    try {
      const svg = document.getElementById(BLUE_TINT_SVG_ID);
      if (svg) svg.remove();
    } catch {
      // ignore
    }
    for (const shadowRoot of this.shadowRootStyles.keys()) {
      try {
        const svg = shadowRoot && shadowRoot.querySelector
          ? shadowRoot.querySelector(`#${BLUE_TINT_SVG_ID}`)
          : null;
        if (svg) svg.remove();
      } catch {
        // ignore
      }
    }

    // Remove all KeyPilot classes from elements
    this.removeAllKeyPilotClasses();

    // Clear tracking
    this.injectedStyles.clear();
    this.shadowRootStyles.clear();
    this.isEnabled = false;
  }

  /**
   * Restore all KeyPilot CSS styles to the page
   * Used when extension is toggled back on
   */
  restoreAllStyles() {
    this.isEnabled = true;

    // Re-inject main styles
    this.injectSharedStyles();

    // Re-apply text-input hint SVG background vars after restore.
    this._applyTextInputHintCssVars();

    // Re-inject shadow root styles for any shadow roots we previously tracked
    // Note: We'll need to re-discover shadow roots since they may have changed
    // This will be handled by the shadow DOM manager during normal operation
  }

  /**
   * Remove all KeyPilot CSS classes from DOM elements
   */
  removeAllKeyPilotClasses() {
    const classesToRemove = [
      CSS_CLASSES.FOCUS,
      CSS_CLASSES.DELETE,
      CSS_CLASSES.COLS,
      CSS_CLASSES.INSPECTOR,
      CSS_CLASSES.COLS_ACTIVE,
      CSS_CLASSES.COLS_PAGE,
      CSS_CLASSES.HIGHLIGHT,
      CSS_CLASSES.HIDDEN,
      CSS_CLASSES.RIPPLE,
      CSS_CLASSES.VIEWPORT_MODAL_FRAME,
      CSS_CLASSES.ACTIVE_TEXT_INPUT_FRAME,
      CSS_CLASSES.ESC_EXIT_LABEL,
      CSS_CLASSES.HIGHLIGHT_OVERLAY,
      CSS_CLASSES.HIGHLIGHT_SELECTION,
      CSS_CLASSES.TEXT_FOCUS_INPUT,
      CSS_CLASSES.TEXT_FOCUS_INPUT_PARENT,
      CSS_CLASSES.TEXT_FOCUS_LEFT_EDGE,
      CSS_CLASSES.TEXT_HOVER_INPUT,
      CSS_CLASSES.TEXT_HOVER_INPUT_PARENT
    ];

    // Remove classes from main document
    classesToRemove.forEach(className => {
      const elements = document.querySelectorAll(`.${className}`);
      elements.forEach(el => el.classList.remove(className));
    });

    // Remove classes from shadow roots
    for (const shadowRoot of this.shadowRootStyles.keys()) {
      classesToRemove.forEach(className => {
        const elements = shadowRoot.querySelectorAll(`.${className}`);
        elements.forEach(el => el.classList.remove(className));
      });
    }
  }

  /**
   * Check if styles are currently enabled
   */
  isStylesEnabled() {
    return this.isEnabled;
  }

  cleanup() {
    this.removeAllStyles();
  }
}


  // Module: src/modules/column-layout-manager.js
/**
 * Column layout + NLE-style slip edit for Cols Toggle.
 *
 * Wraps the chosen element in a GUI widget shell (outline, padding, slip bar
 * as bottom chrome like a playback slider). Slip offset scrubs which slice of
 * content occupies the fixed column frame. Expand promotes the shell into a
 * floating popover (same resize/chrome utilities as Link Preview). Close
 * restores the original element.
 *
 * Height invariant (core UX):
 *   The widget shell (and the multicol content frame inside it) must never be
 *   taller than the viewport. Vertical document scroll is converted into
 *   multi-column horizontal flow; overflow continues sideways and is scrubbed
 *   with the slip bar — not by growing the shell past 100vh.
 *
 * Progressive / infinite-scroll content (best-effort):
 *   - MutationObserver remeasures slip when the target DOM grows.
 *   - Near the end of the slip range, we nudge scroll/IO so site “load more”
 *     hooks can fire; new nodes reflow into additional columns.
 *   - Virtualized feeds (tiny live DOM) cannot be fully supported.
 *
 * Sites with heavy flex/grid/sticky chrome may reflow poorly — best-effort v1.
 */


/** Slip bar height (product: ~20pt rectangular track). */
const SLIP_BAR_HEIGHT = '28px';
const SLIP_BAR_HEIGHT_PX_FALLBACK = 28;
const SHELL_PAD_PX = 10;
const COL_GAP_PX = 24;
/** Readable measure target for auto column-width. */
const COL_WIDTH_MIN_PX = 280;
const COL_WIDTH_MAX_PX = 420;
const COL_WIDTH_IDEAL_PX = 360;

/** Treat slip as “near end” when past this fraction of max (wake loaders). */
const SLIP_END_RATIO = 0.86;
/** Or when within this many px of the slip max. */
const SLIP_END_PX = 160;
/** Min gap between progressive-load wake attempts. */
const WAKE_THROTTLE_MS = 850;
/** Debounce for MutationObserver → remeasure. */
const MUTATION_REMEASURE_MS = 80;

class ColumnLayoutManager {
  constructor() {
    /** @type {boolean} */
    this._active = false;
    /** @type {Element|null} */
    this._target = null;
    /** @type {boolean} */
    this._pageMode = false;
    /** @type {boolean} shell floated as a popover */
    this._popoverMode = false;
    /** @type {Array<{ el: Element, styleAttr: string|null }>} */
    this._snapshots = [];
    /** @type {number} slip offset in px (horizontal scroll of multicol frame) */
    this._slipOffset = 0;
    /** @type {number} */
    this._maxSlip = 0;
    /** @type {HTMLElement|null} */
    this._shell = null;
    /** @type {HTMLElement|null} */
    this._body = null;
    /** @type {HTMLElement|null} */
    this._placeholder = null;
    /** @type {HTMLElement|null} */
    this._slipBar = null;
    /** @type {HTMLElement|null} */
    this._track = null;
    /** @type {HTMLElement|null} */
    this._knob = null;
    /** @type {HTMLElement|null} */
    this._expandBtn = null;
    /** @type {HTMLElement|null} */
    this._closeBtn = null;
    /** @type {{ dispose: () => void }|null} */
    this._resizeApi = null;
    /** @type {boolean} */
    this._dragging = false;
    /** @type {((e: PointerEvent) => void)|null} */
    this._onPointerMove = null;
    /** @type {((e: PointerEvent) => void)|null} */
    this._onPointerUp = null;
    /** @type {(() => void)|null} */
    this._onResize = null;
    /** @type {ResizeObserver|null} */
    this._resizeObserver = null;
    /** @type {MutationObserver|null} */
    this._mutationObserver = null;
    /** @type {number} */
    this._mutationTimer = 0;
    /** @type {number} */
    this._measureRaf = 0;
    /** @type {number} last progressive-load wake (ms) */
    this._lastWakeAt = 0;
    /** @type {boolean} */
    this._wakeInFlight = false;
    /** @type {number} */
    this._wakeRestoreTimer = 0;
    /** @type {ParentNode|null} original parent of target before wrap */
    this._origParent = null;
    /** @type {Node|null} original nextSibling of target before wrap */
    this._origNext = null;
  }

  isActive() {
    return this._active;
  }

  isPageMode() {
    return this._active && this._pageMode;
  }

  isPopoverMode() {
    return this._active && this._popoverMode;
  }

  getTarget() {
    return this._target;
  }

  /**
   * @param {Element|null|undefined} element
   * @returns {boolean} true if columns were applied
   */
  apply(element) {
    if (this._active) {
      this.clear();
    }

    let target = element;
    if (!target || !target.isConnected) return false;

    // Skip KeyPilot chrome.
    if (this._isKeyPilotNode(target)) return false;

    let pageMode = false;
    try {
      if (target === document.documentElement || target === document.body) {
        target = document.body;
        pageMode = true;
      }
    } catch {
      return false;
    }

    if (!target || !target.isConnected) return false;

    // Element mode requires a parent we can wrap.
    if (!pageMode) {
      try {
        if (!target.parentNode) return false;
      } catch {
        return false;
      }
    }

    this._snapshots = [];
    this._snapshot(target);
    if (pageMode) {
      try { this._snapshot(document.documentElement); } catch { /* ignore */ }
    }

    this._target = target;
    this._pageMode = pageMode;
    this._popoverMode = false;
    this._slipOffset = 0;
    this._maxSlip = 0;

    try {
      if (pageMode) {
        this._buildPageShell();
      } else {
        this._wrapTargetInShell(target);
      }
    } catch (err) {
      console.warn('[KeyPilot] Column shell failed:', err);
      this.clear();
      return false;
    }

    this._applyMetrics();
    try {
      target.classList.add(CSS_CLASSES.COLS_ACTIVE);
      if (pageMode) {
        document.documentElement.classList.add(CSS_CLASSES.COLS_PAGE);
        document.body.classList.add(CSS_CLASSES.COLS_PAGE);
      }
    } catch {
      this.clear();
      return false;
    }

    this._active = true;
    this._bindResize();
    this._bindContentObserver();
    this._scheduleRemeasure();
    // After layout: if shell is max-height or clipped by the viewport, pin its
    // top to the top of the viewport so the full widget is in view.
    this._scheduleScrollShellIntoViewport();
    // Short first pages / infinite feeds: one progressive wake so loaders can
    // append more content into the column stream.
    try {
      setTimeout(() => {
        if (this._active) this._maybeWakeInfiniteScrollLoaders({ force: true });
      }, 120);
    } catch { /* ignore */ }
    return true;
  }

  clear() {
    this._unbindContentObserver();
    this._unbindResize();
    this._disposeResizeApi();
    this._cancelWakeLoaders();
    this._teardownSlipBar();

    if (this._target) {
      try { this._target.classList.remove(CSS_CLASSES.COLS_ACTIVE); } catch { /* ignore */ }
    }
    try {
      document.documentElement.classList.remove(CSS_CLASSES.COLS_PAGE);
      document.body.classList.remove(CSS_CLASSES.COLS_PAGE);
    } catch { /* ignore */ }

    this._unwrapShell();
    this._restoreSnapshots();

    try {
      document.documentElement.style.removeProperty('--kpv2-cols-width');
      document.documentElement.style.removeProperty('--kpv2-cols-gap');
      document.documentElement.style.removeProperty('--kpv2-cols-height');
      document.documentElement.style.removeProperty('--kpv2-cols-slip-reserve');
    } catch { /* ignore */ }

    this._active = false;
    this._target = null;
    this._pageMode = false;
    this._popoverMode = false;
    this._slipOffset = 0;
    this._maxSlip = 0;
    this._snapshots = [];
    this._origParent = null;
    this._origNext = null;
    this._lastWakeAt = 0;
    this._wakeInFlight = false;
  }

  /**
   * @param {number} offsetPx
   */
  setSlipOffset(offsetPx) {
    if (!this._active || !this._target) return;
    const max = this._maxSlip > 0 ? this._maxSlip : 0;
    const next = Math.max(0, Math.min(max, Number(offsetPx) || 0));
    this._slipOffset = next;
    this._applySlipToTarget();
    this._updateKnobPosition();
    this._maybeWakeInfiniteScrollLoaders();
  }

  /**
   * Normalized 0..1 slip position.
   * @param {number} t
   */
  setSlipNormalized(t) {
    const max = this._maxSlip > 0 ? this._maxSlip : 0;
    const n = Math.max(0, Math.min(1, Number(t) || 0));
    this.setSlipOffset(n * max);
  }

  // ---------------------------------------------------------------------------
  // Internals
  // ---------------------------------------------------------------------------

  /**
   * @param {Element} el
   * @returns {boolean}
   */
  _isKeyPilotNode(el) {
    try {
      if (!el || el.nodeType !== 1) return true;
      if (el.closest?.('[data-kp-control-strip], [data-kp-early-control-strip], [data-kp-early-floating-keyboard]')) {
        return true;
      }
      if (el.closest?.(`.${CSS_CLASSES.COLS_SHELL}, .${CSS_CLASSES.COLS_SLIP_BAR}`)) {
        return true;
      }
      const cls = typeof el.className === 'string' ? el.className : '';
      if (cls.includes('kpv2-') || cls.includes('kp-')) return true;
      // Walk up a few levels for class markers on ancestors.
      let cur = el.parentElement;
      let depth = 0;
      while (cur && depth < 6) {
        const c = typeof cur.className === 'string' ? cur.className : '';
        if (
          c.includes('kpv2-cols-slip') ||
          c.includes('kpv2-cols-shell') ||
          c.includes('kp-control-strip') ||
          c.includes('kp-floating-keyboard') ||
          c.includes('kpv2-omnibox') ||
          c.includes('kpv2-popup')
        ) {
          return true;
        }
        cur = cur.parentElement;
        depth += 1;
      }
    } catch { /* ignore */ }
    return false;
  }

  /**
   * @param {Element} el
   */
  _snapshot(el) {
    if (!el) return;
    let styleAttr = null;
    try {
      styleAttr = el.getAttribute('style');
    } catch {
      styleAttr = null;
    }
    this._snapshots.push({ el, styleAttr });
  }

  _restoreSnapshots() {
    for (const snap of this._snapshots) {
      try {
        if (!snap?.el) continue;
        if (snap.styleAttr == null || snap.styleAttr === '') {
          snap.el.removeAttribute('style');
        } else {
          snap.el.setAttribute('style', snap.styleAttr);
        }
      } catch { /* ignore */ }
    }
    this._snapshots = [];
  }

  /**
   * Wrap a non-body target in the widget shell (content + slip chrome).
   * @param {Element} target
   */
  _wrapTargetInShell(target) {
    const parent = target.parentNode;
    if (!parent) throw new Error('no parent');

    this._origParent = parent;
    this._origNext = target.nextSibling;

    const shell = document.createElement('div');
    shell.className = CSS_CLASSES.COLS_SHELL;
    shell.setAttribute('data-kp-cols-shell', 'true');
    shell.setAttribute('role', 'region');
    shell.setAttribute('aria-label', 'Column layout');

    const body = document.createElement('div');
    body.className = CSS_CLASSES.COLS_BODY;
    body.setAttribute('data-kp-cols-body', 'true');

    let contentHeight = 0;
    try {
      contentHeight = target.getBoundingClientRect().height || 0;
    } catch {
      contentHeight = 0;
    }

    this._applyShellChrome(shell, body, { pageMode: false, contentHeight });

    parent.insertBefore(shell, target);
    body.appendChild(target);
    shell.appendChild(body);

    this._shell = shell;
    this._body = body;

    this._buildSlipBar(shell);
    this._attachResize(shell);
  }

  /**
   * Page mode: body is the column target; shell is a fixed viewport frame that
   * holds the slip bar as bottom chrome (content is not reparented).
   */
  _buildPageShell() {
    const shell = document.createElement('div');
    shell.className = `${CSS_CLASSES.COLS_SHELL} ${CSS_CLASSES.COLS_SHELL}--page`;
    shell.setAttribute('data-kp-cols-shell', 'true');
    shell.setAttribute('data-kp-cols-page-frame', 'true');
    shell.setAttribute('role', 'region');
    shell.setAttribute('aria-label', 'Page column layout');

    // Page frame is only chrome (slip + buttons); content stays on body.
    // Positioned as a bottom dock that is the full width of the slip bar / widget.
    Object.assign(shell.style, {
      position: 'fixed',
      left: '12px',
      right: '12px',
      bottom: '10px',
      top: 'auto',
      height: 'auto',
      zIndex: String(Z_INDEX.COLS_SLIP_BAR),
      display: 'flex',
      flexDirection: 'column',
      boxSizing: 'border-box',
      padding: '0',
      borderRadius: '8px',
      border: `2px solid ${COLORS.COLS_PURPLE}`,
      background: 'linear-gradient(180deg, rgba(28, 18, 36, 0.92) 0%, rgba(18, 12, 24, 0.94) 100%)',
      boxShadow: `0 8px 28px ${COLORS.COLS_SHADOW}, 0 0 0 1px ${COLORS.COLS_SHADOW_BRIGHT}`,
      fontFamily: KP_UI_FONT,
      overflow: 'hidden',
      pointerEvents: 'auto',
      backdropFilter: 'blur(10px)',
      WebkitBackdropFilter: 'blur(10px)'
    });

    // Outline the page content area; reserve bottom space so the docked slip
    // chrome is part of the frame rather than overlaying content.
    try {
      const slipH = this._slipBarHeightPx();
      const bs = document.body.style;
      bs.setProperty('outline', `2px solid ${COLORS.COLS_PURPLE}`, 'important');
      bs.setProperty('outline-offset', '-2px', 'important');
      bs.setProperty('border-radius', '6px', 'important');
      bs.setProperty('box-shadow', `inset 0 0 0 1px ${COLORS.COLS_SHADOW_BRIGHT}`, 'important');
      bs.setProperty('padding-bottom', `${slipH + 20}px`, 'important');
      bs.setProperty('box-sizing', 'border-box', 'important');
    } catch { /* ignore */ }

    this._shell = shell;
    this._body = null;

    this._buildSlipBar(shell);

    try {
      document.documentElement.appendChild(shell);
    } catch {
      try { document.body.appendChild(shell); } catch { /* ignore */ }
    }

    // Page frame is not content-resized the same way; still allow grip for height of bar strip only if needed.
    // Expand promotes a note — page is already full-bleed; expand becomes popover chrome maximize.
  }

  /**
   * Max shell height in CSS px — never taller than the visual viewport.
   * Leaves a small margin so the widget doesn't kiss browser chrome.
   * @returns {number}
   */
  _maxShellHeightPx() {
    const vv = window.visualViewport;
    const vh = Math.max(200, Math.floor((vv && vv.height) || window.innerHeight || 768));
    return Math.max(180, vh - 24);
  }

  /**
   * @param {HTMLElement} shell
   * @param {HTMLElement} body
   * @param {{ pageMode?: boolean, contentHeight?: number }} [opts]
   */
  _applyShellChrome(shell, body, opts = {}) {
    const vv = window.visualViewport;
    const vh = Math.max(200, Math.floor((vv && vv.height) || window.innerHeight || 768));
    const slipH = this._slipBarHeightPx();
    const maxShellH = this._maxShellHeightPx();
    // Original element height is only a *starting* size. Tall content is capped
    // to the viewport — overflow becomes extra columns + horizontal slip, not a
    // taller shell.
    const contentH = Math.max(120, Math.floor(Number(opts.contentHeight) || vh * 0.45));
    const shellH = Math.min(maxShellH, contentH + SHELL_PAD_PX + slipH + 8);

    Object.assign(shell.style, {
      position: 'relative',
      display: 'flex',
      flexDirection: 'column',
      width: '100%',
      maxWidth: '100%',
      maxHeight: `${maxShellH}px`,
      height: `${shellH}px`,
      boxSizing: 'border-box',
      padding: `${SHELL_PAD_PX}px`,
      paddingBottom: '0',
      margin: '8px 0',
      borderRadius: '8px',
      border: `2px solid ${COLORS.COLS_PURPLE}`,
      background: 'linear-gradient(180deg, rgba(28, 18, 36, 0.14) 0%, rgba(18, 12, 24, 0.10) 100%)',
      boxShadow: `0 8px 28px ${COLORS.COLS_SHADOW}, 0 0 0 1px ${COLORS.COLS_SHADOW_BRIGHT}`,
      fontFamily: KP_UI_FONT,
      overflow: 'hidden',
      zIndex: 'auto',
      pointerEvents: 'auto'
    });

    Object.assign(body.style, {
      flex: '1 1 auto',
      minHeight: '0',
      minWidth: '0',
      width: '100%',
      overflow: 'hidden',
      boxSizing: 'border-box',
      position: 'relative',
      // Inner pad so content doesn't hug the outline
      padding: '4px',
      borderRadius: '4px'
    });
  }

  /**
   * Re-assert shell height ≤ viewport after window resize / user drag.
   */
  _clampShellToViewport() {
    if (!this._shell || this._pageMode) return;
    const maxShellH = this._maxShellHeightPx();
    try {
      const s = this._shell.style;
      s.maxHeight = `${maxShellH}px`;
      const rect = this._shell.getBoundingClientRect();
      if (rect.height > maxShellH + 1) {
        s.height = `${maxShellH}px`;
      }
    } catch { /* ignore */ }
  }

  /**
   * After Cols Toggle: if the shell is at maxHeight (fills the viewport band)
   * or its vertical extent is clipped by the viewport, scroll the document so
   * the top of the widget sits at the top of the viewport.
   *
   * Skipped for page mode / floating popover (those are fixed to the viewport).
   */
  _scrollShellIntoViewportIfNeeded() {
    if (!this._active || !this._shell || this._pageMode || this._popoverMode) return;
    try {
      if (!this._shell.isConnected) return;

      const rect = this._shell.getBoundingClientRect();
      if (!(rect.width > 0 && rect.height > 0)) return;

      const vv = window.visualViewport;
      const vh = Math.max(1, Math.floor((vv && vv.height) || window.innerHeight || 0));
      const maxShellH = this._maxShellHeightPx();

      // At (or effectively at) the max shell height.
      const atMaxHeight = rect.height >= maxShellH - 2;

      // Any vertical overflow relative to the visual viewport.
      const outOfViewport =
        rect.top < -1 ||
        rect.bottom > vh + 1 ||
        rect.bottom < 0 ||
        rect.top > vh;

      if (!atMaxHeight && !outOfViewport) return;

      // Document Y of the shell's top edge.
      const pageY =
        (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
      // visualViewport.offsetTop accounts for mobile URL-bar / pinch-zoom offset.
      const vvOffsetTop = (vv && typeof vv.offsetTop === 'number') ? vv.offsetTop : 0;
      const targetTop = Math.max(0, Math.round(pageY + rect.top - vvOffsetTop));

      // Instant scroll so the user lands on the widget immediately after toggle.
      try {
        window.scrollTo({ top: targetTop, left: window.pageXOffset || 0, behavior: 'auto' });
      } catch {
        window.scrollTo(window.pageXOffset || 0, targetTop);
      }
    } catch { /* ignore */ }
  }

  /**
   * Wait one or two frames so shell metrics/layout settle before scrolling.
   */
  _scheduleScrollShellIntoViewport() {
    try {
      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          this._scrollShellIntoViewportIfNeeded();
        });
      });
    } catch {
      this._scrollShellIntoViewportIfNeeded();
    }
  }

  _applyMetrics() {
    const slipReserve = this._slipBarHeightPx();
    const vv = window.visualViewport;
    const vw = Math.max(320, Math.floor((vv && vv.width) || window.innerWidth || 1024));
    const vh = Math.max(200, Math.floor((vv && vv.height) || window.innerHeight || 768));
    // Absolute ceiling for multicol frame: never taller than the viewport.
    const maxFrameH = Math.max(80, vh - 8);

    this._clampShellToViewport();

    // Prefer measuring the content body inside the shell (element mode).
    let availableW = Math.max(200, vw - 16);
    let frameH = Math.max(120, Math.min(maxFrameH, vh - slipReserve - 24));

    if (this._shell && !this._pageMode) {
      try {
        const shellRect = this._shell.getBoundingClientRect();
        if (shellRect.width > 40) {
          availableW = Math.max(160, Math.floor(shellRect.width - SHELL_PAD_PX * 2 - 8));
        }
        if (this._body) {
          const bodyRect = this._body.getBoundingClientRect();
          if (bodyRect.height > 40) {
            frameH = Math.max(80, Math.floor(bodyRect.height));
          } else if (shellRect.height > 40) {
            frameH = Math.max(80, Math.floor(shellRect.height - slipReserve - SHELL_PAD_PX));
          }
        }
      } catch { /* ignore */ }
    } else if (this._pageMode) {
      // Viewport minus the docked slip chrome — page columns fill the screen,
      // never taller than vh.
      frameH = Math.max(120, Math.min(maxFrameH, vh - slipReserve - 24));
      availableW = Math.max(200, vw - 32);
    }

    // Final clamp: multicol frame height ≤ viewport (horizontal overflow only).
    frameH = Math.min(frameH, maxFrameH);

    // Auto column-width: fit as many readable columns as width allows.
    let colW = COL_WIDTH_IDEAL_PX;
    const nAtIdeal = Math.max(1, Math.floor((availableW + COL_GAP_PX) / (COL_WIDTH_IDEAL_PX + COL_GAP_PX)));
    if (nAtIdeal <= 1) {
      colW = Math.min(COL_WIDTH_MAX_PX, Math.max(COL_WIDTH_MIN_PX, availableW));
    } else {
      const totalGaps = (nAtIdeal - 1) * COL_GAP_PX;
      colW = Math.min(
        COL_WIDTH_MAX_PX,
        Math.max(COL_WIDTH_MIN_PX, Math.floor((availableW - totalGaps) / nAtIdeal))
      );
    }

    try {
      const root = document.documentElement;
      root.style.setProperty('--kpv2-cols-width', `${colW}px`);
      root.style.setProperty('--kpv2-cols-gap', `${COL_GAP_PX}px`);
      root.style.setProperty('--kpv2-cols-height', `${frameH}px`);
      root.style.setProperty('--kpv2-cols-slip-reserve', `${slipReserve}px`);
    } catch { /* ignore */ }

    const t = this._target;
    if (!t) return;
    try {
      const s = /** @type {HTMLElement} */ (t).style;
      s.setProperty('column-width', `${colW}px`, 'important');
      s.setProperty('column-gap', `${COL_GAP_PX}px`, 'important');
      s.setProperty('column-fill', 'auto', 'important');
      s.setProperty('height', `${frameH}px`, 'important');
      s.setProperty('max-height', `${frameH}px`, 'important');
      s.setProperty('overflow-x', 'auto', 'important');
      s.setProperty('overflow-y', 'hidden', 'important');
      s.setProperty('box-sizing', 'border-box', 'important');
      s.setProperty('width', '100%', 'important');
      s.setProperty('max-width', '100%', 'important');
      // Hide native horizontal scrollbar — slip bar is the scrubber.
      s.setProperty('scrollbar-width', 'none', 'important');
      try { s.setProperty('msOverflowStyle', 'none', 'important'); } catch { /* ignore */ }

      if (this._pageMode) {
        s.setProperty('margin', '0', 'important');
        s.setProperty('padding-bottom', '0', 'important');
        try {
          const hs = document.documentElement.style;
          hs.setProperty('overflow', 'hidden', 'important');
          hs.setProperty('height', `${vh}px`, 'important');
          hs.setProperty('max-height', `${vh}px`, 'important');
        } catch { /* ignore */ }
      }
    } catch { /* ignore */ }
  }

  _slipBarHeightPx() {
    try {
      if (this._slipBar) {
        const h = this._slipBar.getBoundingClientRect().height;
        if (h > 0) return Math.ceil(h);
      }
    } catch { /* ignore */ }
    return SLIP_BAR_HEIGHT_PX_FALLBACK;
  }

  _applySlipToTarget() {
    if (!this._target) return;
    try {
      // Multicol overflow extends horizontally — slip = scrollLeft (NLE content window).
      /** @type {HTMLElement} */ (this._target).scrollLeft = this._slipOffset;
    } catch { /* ignore */ }
  }

  _scheduleRemeasure() {
    if (this._measureRaf) {
      try { cancelAnimationFrame(this._measureRaf); } catch { /* ignore */ }
    }
    this._measureRaf = requestAnimationFrame(() => {
      this._measureRaf = 0;
      this._remeasureSlip();
    });
  }

  _remeasureSlip() {
    if (!this._active || !this._target) return;
    try {
      const el = /** @type {HTMLElement} */ (this._target);
      const prevMax = this._maxSlip;
      const max = Math.max(0, (el.scrollWidth || 0) - (el.clientWidth || 0));
      this._maxSlip = max;
      if (this._slipOffset > max) {
        this._slipOffset = max;
        this._applySlipToTarget();
      } else {
        this._applySlipToTarget();
      }
      this._updateKnobPosition();
      if (this._slipBar) {
        this._slipBar.setAttribute('data-kp-slip-empty', max <= 0 ? '1' : '0');
        // Hint UI when more content may still stream in
        try {
          this._slipBar.setAttribute(
            'data-kp-slip-growing',
            max > prevMax + 8 ? '1' : '0'
          );
        } catch { /* ignore */ }
      }
      // Content grew while user was already near the end — try another load cycle.
      if (max > prevMax + 8 && this._isNearSlipEnd()) {
        this._maybeWakeInfiniteScrollLoaders({ force: false });
      }
    } catch { /* ignore */ }
  }

  /**
   * Watch target DOM growth (infinite-scroll appends, lazy sections).
   * ResizeObserver only sees border-box changes; scrollWidth growth needs this.
   */
  _bindContentObserver() {
    this._unbindContentObserver();
    if (!this._target || typeof MutationObserver === 'undefined') return;

    try {
      this._mutationObserver = new MutationObserver((records) => {
        if (!this._active) return;
        // Ignore pure attribute noise on our own chrome if it ever nests.
        let meaningful = false;
        for (const rec of records) {
          if (rec.type === 'childList' &&
              ((rec.addedNodes && rec.addedNodes.length) ||
               (rec.removedNodes && rec.removedNodes.length))) {
            meaningful = true;
            break;
          }
          if (rec.type === 'characterData') {
            meaningful = true;
            break;
          }
        }
        if (!meaningful) return;

        if (this._mutationTimer) {
          try { clearTimeout(this._mutationTimer); } catch { /* ignore */ }
        }
        this._mutationTimer = setTimeout(() => {
          this._mutationTimer = 0;
          if (!this._active) return;
          // New nodes may change column count / width metrics slightly.
          this._applyMetrics();
          this._scheduleRemeasure();
        }, MUTATION_REMEASURE_MS);
      });

      this._mutationObserver.observe(this._target, {
        childList: true,
        subtree: true,
        characterData: true
      });
    } catch {
      this._mutationObserver = null;
    }
  }

  _unbindContentObserver() {
    if (this._mutationTimer) {
      try { clearTimeout(this._mutationTimer); } catch { /* ignore */ }
      this._mutationTimer = 0;
    }
    if (this._mutationObserver) {
      try { this._mutationObserver.disconnect(); } catch { /* ignore */ }
      this._mutationObserver = null;
    }
  }

  /**
   * @returns {boolean}
   */
  _isNearSlipEnd() {
    const max = this._maxSlip;
    // No horizontal overflow yet: treat as "at end" so short first pages can
    // still request more content (caller still throttles wakes).
    if (!(max > 0)) return true;
    const offset = this._slipOffset;
    if (offset >= Math.max(0, max - SLIP_END_PX)) return true;
    if (offset / max >= SLIP_END_RATIO) return true;
    return false;
  }

  /**
   * When the user slips near the end of columnized content, nudge the page so
   * infinite-scroll / IntersectionObserver loaders can fetch more. New nodes
   * are picked up by MutationObserver and extend the slip range.
   *
   * @param {{ force?: boolean }} [opts]
   */
  _maybeWakeInfiniteScrollLoaders(opts = {}) {
    if (!this._active || !this._target) return;
    if (!opts.force && !this._isNearSlipEnd()) return;

    const now = Date.now();
    if (!opts.force && now - this._lastWakeAt < WAKE_THROTTLE_MS) return;
    if (this._wakeInFlight) return;

    this._lastWakeAt = now;
    this._wakeInFlight = true;

    try {
      this._wakeInfiniteScrollLoaders();
    } catch (err) {
      if (window.KEYPILOT_DEBUG) {
        console.warn('[KeyPilot] Cols progressive wake failed:', err);
      }
    } finally {
      // Allow another wake after throttle even if restore is still finishing.
      try {
        if (this._wakeRestoreTimer) clearTimeout(this._wakeRestoreTimer);
      } catch { /* ignore */ }
      this._wakeRestoreTimer = setTimeout(() => {
        this._wakeRestoreTimer = 0;
        this._wakeInFlight = false;
        if (this._active) this._scheduleRemeasure();
      }, 320);
    }
  }

  /**
   * Best-effort: fire scroll listeners + bring tail content toward the
   * viewport so IntersectionObserver sentinels can activate.
   */
  _wakeInfiniteScrollLoaders() {
    const target = /** @type {HTMLElement|null} */ (this._target);
    if (!target) return;

    const savedSlip = this._slipOffset;
    const savedPageX = window.pageXOffset || 0;
    const savedPageY =
      window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

    // 1) Ensure multicol is scrolled to the current slip (usually near end).
    try {
      target.scrollLeft = savedSlip;
    } catch { /* ignore */ }

    // 2) Dispatch scroll events many feeds listen for.
    try {
      window.dispatchEvent(new Event('scroll', { bubbles: false }));
      document.dispatchEvent(new Event('scroll', { bubbles: true }));
      target.dispatchEvent(new Event('scroll', { bubbles: false }));
    } catch { /* ignore */ }

    // 3) Nudge document scroll slightly (or to near bottom) then restore.
    //    Skipped when we fully freeze the page (page mode) — use temp unlock.
    const html = document.documentElement;
    let unlockedPage = false;
    let prevHtmlOverflow = null;
    let prevHtmlHeight = null;
    try {
      if (this._pageMode) {
        prevHtmlOverflow = html.style.getPropertyValue('overflow') || null;
        prevHtmlHeight = html.style.getPropertyValue('height') || null;
        html.style.setProperty('overflow', 'auto', 'important');
        html.style.removeProperty('height');
        html.style.removeProperty('max-height');
        unlockedPage = true;
      }

      const maxY = Math.max(
        0,
        Math.max(
          document.documentElement.scrollHeight || 0,
          document.body?.scrollHeight || 0
        ) - (window.innerHeight || 0)
      );

      // Prefer a small nudge from current position; if already at bottom, micro-jitter.
      let nudgeY = savedPageY + 48;
      if (nudgeY > maxY) nudgeY = Math.max(0, maxY);
      if (Math.abs(nudgeY - savedPageY) < 1 && maxY > 0) {
        nudgeY = Math.max(0, savedPageY - 1);
      }

      try {
        window.scrollTo({ top: nudgeY, left: savedPageX, behavior: 'auto' });
      } catch {
        window.scrollTo(savedPageX, nudgeY);
      }
      try {
        window.dispatchEvent(new Event('scroll', { bubbles: false }));
      } catch { /* ignore */ }
    } catch { /* ignore */ }

    // 4) Scroll a tail sentinel into view (IO-based infinite scroll).
    const tail = this._findTailSentinel(target);
    if (tail) {
      try {
        // nearest keeps page jump smaller; still often enough for IO.
        tail.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'auto' });
      } catch {
        try { /** @type {any} */ (tail).scrollIntoView(false); } catch { /* ignore */ }
      }
    }

    // 5) Restore our horizontal slip + document scroll position.
    const restore = () => {
      try {
        if (unlockedPage) {
          if (prevHtmlOverflow) {
            html.style.setProperty('overflow', prevHtmlOverflow, 'important');
          } else {
            html.style.setProperty('overflow', 'hidden', 'important');
          }
          if (prevHtmlHeight) {
            html.style.setProperty('height', prevHtmlHeight, 'important');
            html.style.setProperty('max-height', prevHtmlHeight, 'important');
          } else {
            // Re-apply page metrics
            this._applyMetrics();
          }
        }
        try {
          window.scrollTo({ top: savedPageY, left: savedPageX, behavior: 'auto' });
        } catch {
          window.scrollTo(savedPageX, savedPageY);
        }
        this._slipOffset = savedSlip;
        this._applySlipToTarget();
        this._scheduleRemeasure();
      } catch { /* ignore */ }
    };

    try {
      requestAnimationFrame(() => {
        requestAnimationFrame(restore);
      });
    } catch {
      setTimeout(restore, 32);
    }

    // Late remeasure after network/DOM appends settle.
    try {
      setTimeout(() => { if (this._active) this._scheduleRemeasure(); }, 200);
      setTimeout(() => { if (this._active) this._scheduleRemeasure(); }, 600);
    } catch { /* ignore */ }
  }

  _cancelWakeLoaders() {
    this._wakeInFlight = false;
    if (this._wakeRestoreTimer) {
      try { clearTimeout(this._wakeRestoreTimer); } catch { /* ignore */ }
      this._wakeRestoreTimer = 0;
    }
  }

  /**
   * Prefer a substantial element near the end of the columnized tree
   * (infinite-scroll sentinels often sit after the last card).
   * @param {Element} root
   * @returns {Element|null}
   */
  _findTailSentinel(root) {
    if (!root) return null;
    try {
      const nodes = root.querySelectorAll('*');
      const start = Math.max(0, nodes.length - 60);
      for (let i = nodes.length - 1; i >= start; i--) {
        const n = nodes[i];
        if (!n || n.nodeType !== 1) continue;
        if (this._isKeyPilotNode(n)) continue;
        try {
          const r = n.getBoundingClientRect();
          // Accept zero-size sentinels (1×1 or 0-height IO markers) near the end.
          if (r.width >= 0 && r.height >= 0) {
            // Prefer something with a bit of box if available.
            if (r.width > 1 || r.height > 1 || i > nodes.length - 8) {
              return n;
            }
          }
        } catch { /* continue */ }
      }
      return root.lastElementChild || root;
    } catch {
      return root.lastElementChild || root;
    }
  }

  _bindResize() {
    this._unbindResize();
    this._onResize = () => {
      if (!this._active) return;
      this._applyMetrics();
      this._scheduleRemeasure();
    };
    try {
      window.addEventListener('resize', this._onResize, { passive: true });
      if (window.visualViewport) {
        window.visualViewport.addEventListener('resize', this._onResize, { passive: true });
      }
    } catch { /* ignore */ }

    try {
      if (typeof ResizeObserver !== 'undefined') {
        this._resizeObserver = new ResizeObserver(() => {
          this._applyMetrics();
          this._scheduleRemeasure();
        });
        if (this._shell) this._resizeObserver.observe(this._shell);
        if (this._body) this._resizeObserver.observe(this._body);
        if (this._target) this._resizeObserver.observe(this._target);
      }
    } catch {
      this._resizeObserver = null;
    }
  }

  _unbindResize() {
    if (this._onResize) {
      try { window.removeEventListener('resize', this._onResize); } catch { /* ignore */ }
      try {
        if (window.visualViewport) {
          window.visualViewport.removeEventListener('resize', this._onResize);
        }
      } catch { /* ignore */ }
      this._onResize = null;
    }
    if (this._resizeObserver) {
      try { this._resizeObserver.disconnect(); } catch { /* ignore */ }
      this._resizeObserver = null;
    }
    if (this._measureRaf) {
      try { cancelAnimationFrame(this._measureRaf); } catch { /* ignore */ }
      this._measureRaf = 0;
    }
  }

  /**
   * @param {HTMLElement} shell
   */
  _attachResize(shell) {
    this._disposeResizeApi();
    try {
      // Resize is allowed, but popover-resize already clamps to viewport − margin.
      // We re-assert maxHeight on every metrics pass as a belt-and-suspenders guard.
      this._resizeApi = makePopoverResizable(shell, {
        minWidth: 280,
        minHeight: 180,
        margin: 8,
        zIndex: 30,
        onResize: () => {
          this._clampShellToViewport();
          this._applyMetrics();
          this._scheduleRemeasure();
        },
        onResizeEnd: () => {
          this._clampShellToViewport();
          this._applyMetrics();
          this._scheduleRemeasure();
        }
      });
      // Explicit CSS cap so even non-handle size changes stay in-viewport.
      try {
        shell.style.maxHeight = `${this._maxShellHeightPx()}px`;
      } catch { /* ignore */ }
    } catch {
      this._resizeApi = null;
    }
  }

  _disposeResizeApi() {
    if (this._resizeApi) {
      try { this._resizeApi.dispose(); } catch { /* ignore */ }
      this._resizeApi = null;
    }
    if (this._shell) {
      try {
        if (this._shell.dataset) delete this._shell.dataset.kpResizable;
      } catch { /* ignore */ }
    }
  }

  /**
   * @param {HTMLElement} parent
   */
  _buildSlipBar(parent) {
    this._teardownSlipBar();

    const bar = document.createElement('div');
    bar.className = CSS_CLASSES.COLS_SLIP_BAR;
    bar.setAttribute('data-kp-cols-slip', 'true');
    bar.setAttribute('role', 'group');
    bar.setAttribute('aria-label', 'Column slip controls');
    Object.assign(bar.style, {
      position: 'relative',
      left: 'auto',
      right: 'auto',
      bottom: 'auto',
      // Bleed to shell edges (cancel shell horizontal padding when present)
      marginLeft: this._pageMode ? '0' : `-${SHELL_PAD_PX}px`,
      marginRight: this._pageMode ? '0' : `-${SHELL_PAD_PX}px`,
      width: this._pageMode ? '100%' : `calc(100% + ${SHELL_PAD_PX * 2}px)`,
      maxWidth: 'none',
      flex: '0 0 auto',
      height: SLIP_BAR_HEIGHT,
      minHeight: SLIP_BAR_HEIGHT,
      zIndex: '2',
      display: 'flex',
      alignItems: 'center',
      gap: '8px',
      padding: '0 10px',
      boxSizing: 'border-box',
      borderRadius: '0 0 6px 6px',
      borderTop: `1px solid ${COLORS.COLS_SHADOW_BRIGHT}`,
      background: 'linear-gradient(180deg, rgba(40, 24, 52, 0.96) 0%, rgba(24, 14, 34, 0.98) 100%)',
      boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.06)',
      fontFamily: KP_UI_FONT,
      color: 'rgba(255,255,255,0.9)',
      userSelect: 'none',
      pointerEvents: 'auto',
      backdropFilter: 'none',
      WebkitBackdropFilter: 'none'
    });

    const label = document.createElement('span');
    label.className = CSS_CLASSES.COLS_SLIP_LABEL;
    label.textContent = 'Slip';
    Object.assign(label.style, {
      fontSize: '11px',
      fontWeight: '600',
      letterSpacing: '0.04em',
      textTransform: 'uppercase',
      opacity: '0.85',
      flex: '0 0 auto',
      color: COLORS.COLS_PURPLE_BRIGHT
    });

    const track = document.createElement('div');
    track.className = CSS_CLASSES.COLS_SLIP_TRACK;
    track.setAttribute('role', 'slider');
    track.setAttribute('aria-label', 'Slip edit — shift column content');
    track.setAttribute('aria-valuemin', '0');
    track.setAttribute('aria-valuemax', '100');
    track.setAttribute('aria-valuenow', '0');
    track.tabIndex = 0;
    Object.assign(track.style, {
      position: 'relative',
      flex: '1 1 auto',
      minWidth: '48px',
      height: '10px',
      borderRadius: '2px',
      background: 'rgba(255,255,255,0.12)',
      border: '1px solid rgba(255,255,255,0.14)',
      cursor: 'pointer'
    });

    // Rectangular knob (not circular) — product requirement.
    const knob = document.createElement('div');
    knob.className = CSS_CLASSES.COLS_SLIP_KNOB;
    Object.assign(knob.style, {
      position: 'absolute',
      top: '50%',
      left: '0%',
      width: '28px',
      height: '16px',
      marginTop: '-8px',
      marginLeft: '-14px',
      borderRadius: '2px',
      background: COLORS.COLS_PURPLE_BRIGHT,
      border: '1px solid rgba(255,255,255,0.35)',
      boxShadow: `0 1px 4px ${COLORS.COLS_SHADOW_BRIGHT}`,
      cursor: 'grab',
      boxSizing: 'border-box',
      touchAction: 'none'
    });

    track.appendChild(knob);

    const expandBtn = this._createChromeButton({
      className: CSS_CLASSES.COLS_EXPAND_BTN,
      title: 'Open columns in popover',
      ariaLabel: 'Open columns in full-size popover',
      onClick: () => this._promoteToPopover()
    });
    expandBtn.appendChild(this._createViewportIcon());

    const closeBtn = this._createChromeButton({
      className: CSS_CLASSES.COLS_CLOSE_BTN,
      title: 'Close columns (restore element)',
      ariaLabel: 'Close columns and restore element',
      onClick: () => this.clear(),
      label: '×',
      semiTransparent: true
    });

    bar.appendChild(label);
    bar.appendChild(track);
    bar.appendChild(expandBtn);
    bar.appendChild(closeBtn);

    const onPointerDown = (e) => {
      if (e.button != null && e.button !== 0) return;
      e.preventDefault();
      e.stopPropagation();
      this._dragging = true;
      try { knob.style.cursor = 'grabbing'; } catch { /* ignore */ }
      try { track.setPointerCapture?.(e.pointerId); } catch { /* ignore */ }
      this._slipFromClientX(e.clientX);
    };

    this._onPointerMove = (e) => {
      if (!this._dragging) return;
      e.preventDefault();
      this._slipFromClientX(e.clientX);
    };
    this._onPointerUp = (e) => {
      if (!this._dragging) return;
      this._dragging = false;
      try { knob.style.cursor = 'grab'; } catch { /* ignore */ }
      try { track.releasePointerCapture?.(e.pointerId); } catch { /* ignore */ }
    };

    track.addEventListener('pointerdown', onPointerDown);
    knob.addEventListener('pointerdown', onPointerDown);
    window.addEventListener('pointermove', this._onPointerMove, { passive: false });
    window.addEventListener('pointerup', this._onPointerUp, { passive: true });
    window.addEventListener('pointercancel', this._onPointerUp, { passive: true });

    // Store for teardown.
    bar._kpOnPointerDown = onPointerDown;

    parent.appendChild(bar);

    this._slipBar = bar;
    this._track = track;
    this._knob = knob;
    this._expandBtn = expandBtn;
    this._closeBtn = closeBtn;
    this._updateKnobPosition();
  }

  /**
   * @param {{
   *   className: string,
   *   title: string,
   *   ariaLabel: string,
   *   onClick: () => void,
   *   label?: string,
   *   semiTransparent?: boolean
   * }} opts
   * @returns {HTMLButtonElement}
   */
  _createChromeButton(opts) {
    const btn = document.createElement('button');
    btn.type = 'button';
    btn.className = opts.className;
    btn.title = opts.title;
    btn.setAttribute('aria-label', opts.ariaLabel);
    Object.assign(btn.style, {
      margin: '0',
      appearance: 'none',
      WebkitAppearance: 'none',
      display: 'inline-flex',
      alignItems: 'center',
      justifyContent: 'center',
      boxSizing: 'border-box',
      flex: '0 0 auto',
      width: '26px',
      height: '22px',
      padding: '0',
      borderRadius: '4px',
      border: `1px solid ${COLORS.COLS_PURPLE_BRIGHT}`,
      background: opts.semiTransparent
        ? 'rgba(156, 39, 176, 0.28)'
        : 'rgba(156, 39, 176, 0.45)',
      color: 'rgba(255,255,255,0.92)',
      fontFamily: KP_UI_FONT,
      fontSize: opts.label ? '16px' : '12px',
      fontWeight: '500',
      lineHeight: '1',
      cursor: 'pointer',
      opacity: opts.semiTransparent ? '0.72' : '0.92',
      boxShadow: `0 1px 3px ${COLORS.COLS_SHADOW}`,
      transition: 'opacity 120ms ease, background 120ms ease'
    });
    if (opts.label) btn.textContent = opts.label;
    btn.addEventListener('mouseenter', () => {
      btn.style.opacity = '1';
      btn.style.background = 'rgba(156, 39, 176, 0.55)';
    });
    btn.addEventListener('mouseleave', () => {
      btn.style.opacity = opts.semiTransparent ? '0.72' : '0.92';
      btn.style.background = opts.semiTransparent
        ? 'rgba(156, 39, 176, 0.28)'
        : 'rgba(156, 39, 176, 0.45)';
    });
    btn.addEventListener('click', (e) => {
      e.preventDefault();
      e.stopPropagation();
      try { opts.onClick(); } catch (err) {
        console.warn('[KeyPilot] Cols chrome action failed:', err);
      }
    });
    return btn;
  }

  /**
   * Full-size / expand-to-viewport icon (corner brackets).
   * @returns {SVGSVGElement}
   */
  _createViewportIcon() {
    const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    svg.setAttribute('viewBox', '0 0 16 16');
    svg.setAttribute('width', '14');
    svg.setAttribute('height', '14');
    svg.setAttribute('aria-hidden', 'true');
    svg.style.cssText = 'display:block; pointer-events:none;';

    // Outer frame + expand corners (full-size viewport metaphor).
    const paths = [
      // frame
      'M3 5 V3 H5',
      'M11 3 H13 V5',
      'M13 11 V13 H11',
      'M5 13 H3 V11',
      // inner diamond-ish rectangle
      'M5 5 H11 V11 H5 Z'
    ];
    for (const d of paths) {
      const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
      path.setAttribute('d', d);
      path.setAttribute('fill', 'none');
      path.setAttribute('stroke', 'currentColor');
      path.setAttribute('stroke-width', '1.4');
      path.setAttribute('stroke-linecap', 'round');
      path.setAttribute('stroke-linejoin', 'round');
      svg.appendChild(path);
    }
    return svg;
  }

  /**
   * Promote the column widget into a floating popover (Link Preview–style shell).
   */
  _promoteToPopover() {
    if (!this._active || !this._shell) return;

    // Page mode: already viewport-docked; promote body content isn't practical —
    // grow the dock into a larger centered panel that still only holds the slip chrome,
    // and re-apply body metrics for a larger column frame.
    if (this._pageMode) {
      this._popoverMode = true;
      try {
        Object.assign(this._shell.style, {
          left: '10vw',
          right: '10vw',
          bottom: '8vh',
          width: 'auto',
          maxWidth: 'none',
          zIndex: String(Z_INDEX.POPOVER_IFRAME_MODAL)
        });
      } catch { /* ignore */ }
      this._applyMetrics();
      this._scheduleRemeasure();
      return;
    }

    if (this._popoverMode) {
      // Already a popover — bump to near-full viewport.
      this._sizeShellAsPopover({ large: true });
      this._applyMetrics();
      this._scheduleRemeasure();
      return;
    }

    const shell = this._shell;
    let rect;
    try {
      rect = shell.getBoundingClientRect();
    } catch {
      rect = { width: 480, height: 360, left: 40, top: 40 };
    }

    // Leave a placeholder so the page layout doesn't collapse.
    try {
      if (!this._placeholder && shell.parentNode) {
        const ph = document.createElement('div');
        ph.className = CSS_CLASSES.COLS_PLACEHOLDER;
        ph.setAttribute('data-kp-cols-placeholder', 'true');
        ph.setAttribute('aria-hidden', 'true');
        Object.assign(ph.style, {
          width: `${Math.max(1, Math.floor(rect.width))}px`,
          height: `${Math.max(1, Math.floor(rect.height))}px`,
          margin: shell.style.margin || '8px 0',
          boxSizing: 'border-box',
          pointerEvents: 'none',
          visibility: 'hidden'
        });
        shell.parentNode.insertBefore(ph, shell);
        this._placeholder = ph;
      }
    } catch { /* ignore */ }

    // Mount shell at document root so it isn't clipped by ancestors.
    try {
      document.documentElement.appendChild(shell);
    } catch {
      try { document.body.appendChild(shell); } catch { /* ignore */ }
    }

    this._popoverMode = true;
    this._sizeShellAsPopover({ large: false, fromRect: rect });

    // Popover look: solid chrome matching modal / Link Preview panels.
    try {
      Object.assign(shell.style, {
        background: 'linear-gradient(180deg, rgb(22, 16, 28) 0%, rgb(14, 10, 18) 100%)',
        border: `1px solid ${COLORS.COLS_PURPLE}`,
        boxShadow: `0 12px 40px rgba(0,0,0,0.65), 0 0 0 1px ${COLORS.COLS_SHADOW_BRIGHT}`,
        zIndex: String(Z_INDEX.POPOVER_IFRAME_MODAL)
      });
    } catch { /* ignore */ }

    // Re-attach resize (shell moved; ensure handles still work).
    this._attachResize(shell);
    this._applyMetrics();
    this._scheduleRemeasure();
  }

  /**
   * @param {{ large?: boolean, fromRect?: { width: number, height: number, left?: number, top?: number } }} [opts]
   */
  _sizeShellAsPopover(opts = {}) {
    const shell = this._shell;
    if (!shell) return;
    const margin = 16;
    const vw = window.innerWidth || 1024;
    const vh = window.innerHeight || 768;
    const large = !!opts.large;
    const w = large
      ? Math.max(320, vw - margin * 2)
      : Math.min(Math.max(420, opts.fromRect?.width || 640), Math.floor(vw * 0.8));
    const h = large
      ? Math.max(240, vh - margin * 2)
      : Math.min(Math.max(280, opts.fromRect?.height || 480), Math.floor(vh * 0.8));
    const left = Math.max(margin, Math.floor((vw - w) / 2));
    const top = Math.max(margin, Math.floor((vh - h) / 2));

    // Keep maxHeight tied to the viewport so expanded popovers still cannot
    // grow past 100vh (horizontal slip remains the overflow strategy).
    const maxShellH = this._maxShellHeightPx();
    Object.assign(shell.style, {
      position: 'fixed',
      left: `${left}px`,
      top: `${top}px`,
      right: 'auto',
      bottom: 'auto',
      width: `${w}px`,
      height: `${Math.min(h, maxShellH)}px`,
      maxWidth: `${Math.max(280, vw - margin * 2)}px`,
      maxHeight: `${maxShellH}px`,
      margin: '0',
      transform: 'none',
      inset: 'auto'
    });
  }

  /**
   * @param {number} clientX
   */
  _slipFromClientX(clientX) {
    if (!this._track) return;
    try {
      const rect = this._track.getBoundingClientRect();
      if (rect.width <= 0) return;
      const t = (clientX - rect.left) / rect.width;
      this.setSlipNormalized(t);
    } catch { /* ignore */ }
  }

  _updateKnobPosition() {
    if (!this._knob || !this._slipBar) return;
    const max = this._maxSlip > 0 ? this._maxSlip : 0;
    const t = max > 0 ? this._slipOffset / max : 0;
    const pct = Math.max(0, Math.min(1, t)) * 100;
    try {
      this._knob.style.left = `${pct}%`;
      this._track?.setAttribute('aria-valuenow', String(Math.round(pct)));
      this._slipBar.setAttribute('aria-valuenow', String(Math.round(pct)));
    } catch { /* ignore */ }
  }

  _teardownSlipBar() {
    this._dragging = false;
    if (this._onPointerMove) {
      try { window.removeEventListener('pointermove', this._onPointerMove); } catch { /* ignore */ }
      this._onPointerMove = null;
    }
    if (this._onPointerUp) {
      try { window.removeEventListener('pointerup', this._onPointerUp); } catch { /* ignore */ }
      try { window.removeEventListener('pointercancel', this._onPointerUp); } catch { /* ignore */ }
      this._onPointerUp = null;
    }
    if (this._slipBar) {
      try {
        const down = this._slipBar._kpOnPointerDown;
        if (down && this._track) {
          this._track.removeEventListener('pointerdown', down);
          this._knob?.removeEventListener('pointerdown', down);
        }
      } catch { /* ignore */ }
      try { this._slipBar.remove(); } catch { /* ignore */ }
    }
    this._slipBar = null;
    this._track = null;
    this._knob = null;
    this._expandBtn = null;
    this._closeBtn = null;
  }

  /**
   * Restore target to original parent and remove shell / placeholder.
   */
  _unwrapShell() {
    const target = this._target;
    const shell = this._shell;
    const placeholder = this._placeholder;

    // Element mode: move target back out of shell.
    if (target && shell && !this._pageMode) {
      try {
        const body = this._body;
        // Prefer original parent; fall back to placeholder parent / shell parent.
        let parent = this._origParent;
        let next = this._origNext;
        if (!parent || !parent.isConnected) {
          if (placeholder && placeholder.parentNode) {
            parent = placeholder.parentNode;
            next = placeholder;
          } else if (shell.parentNode) {
            parent = shell.parentNode;
            next = shell.nextSibling;
          }
        }
        if (parent) {
          if (next && next.parentNode === parent) {
            parent.insertBefore(target, next);
          } else {
            parent.appendChild(target);
          }
        } else if (body && body.contains(target)) {
          // last resort: leave target where shell is
          try {
            if (shell.parentNode) shell.parentNode.insertBefore(target, shell);
          } catch { /* ignore */ }
        }
      } catch { /* ignore */ }
    }

    if (placeholder) {
      try { placeholder.remove(); } catch { /* ignore */ }
    }
    if (shell) {
      try { shell.remove(); } catch { /* ignore */ }
    }

    this._shell = null;
    this._body = null;
    this._placeholder = null;
  }
}



  // Module: src/modules/shadow-dom-manager.js
/**
 * Shadow DOM support and patching
 *
 * Style injection strategy (cheap + correct):
 * 1. Pre-warm: patch attachShadow so new open roots get styles at creation.
 * 2. Pre-warm: inject into open roots already present in the *light* DOM at setup
 *    (shallow — nested roots inside those shadows are NOT eagerly walked).
 * 3. Lazy (primary correctness): StyleManager.ensureStylesForNode() injects into
 *    the owning open ShadowRoot on first hover/focus styling. No MutationObserver;
 *    infinite-scroll / SPA content is covered when the user first targets it.
 */
class ShadowDOMManager {
  constructor(styleManager) {
    this.styleManager = styleManager;
    this.shadowRoots = new Set();
    this.originalAttachShadow = null;
  }

  setup() {
    this.patchAttachShadow();
    this.processExistingShadowRoots();
  }

  /**
   * Track a shadow root we've injected (or intend to use) for optional IO discovery.
   * @param {ShadowRoot} root
   */
  trackShadowRoot(root) {
    if (root) {
      try { this.shadowRoots.add(root); } catch { /* ignore */ }
    }
  }

  patchAttachShadow() {
    if (this.originalAttachShadow) return; // Already patched

    const original = Element.prototype.attachShadow;
    this.originalAttachShadow = original;

    const styleManager = this.styleManager;
    const manager = this;

    // Important: do NOT bind this function to the manager.
    // The receiver (`this`) must remain the element instance so original attachShadow works.
    Element.prototype.attachShadow = function attachShadowPatched(init) {
      // Call native attachShadow on the element instance.
      const root = original.call(this, init);

      // Only open shadow roots are accessible to content scripts.
      try {
        if (init && init.mode === 'open' && root) {
          // Optional pre-warm: O(1) per attach. Nested roots created after setup
          // get styles without waiting for first hover.
          styleManager.injectIntoShadowRoot(root);
          manager.trackShadowRoot(root);
        }
      } catch (error) {
        console.warn('[KeyPilot] Failed to inject styles into shadow root:', error);
      }

      return root;
    };
  }

  /**
   * Pre-warm open shadow roots already in the tree, including nested roots
   * (archive.org: app-root → ia-topnav → primary-nav → media-button → …).
   * Lazy ensureStylesForNode remains the wipe-resistant correctness path.
   */
  processExistingShadowRoots() {
    const injectTree = (root) => {
      if (!root) return;
      const walker = document.createTreeWalker(
        root,
        NodeFilter.SHOW_ELEMENT,
        null,
        false
      );

      let node;
      while ((node = walker.nextNode())) {
        if (!node.shadowRoot) continue;
        try {
          this.styleManager.injectIntoShadowRoot(node.shadowRoot);
          this.trackShadowRoot(node.shadowRoot);
          // Recurse into this open root so nested hosts get CSS before first hover.
          injectTree(node.shadowRoot);
        } catch (error) {
          console.warn('[KeyPilot] Failed to inject styles into existing shadow root:', error);
        }
      }
    };

    injectTree(document.documentElement);
  }

  cleanup() {
    // Restore original attachShadow
    if (this.originalAttachShadow) {
      Element.prototype.attachShadow = this.originalAttachShadow;
      this.originalAttachShadow = null;
    }
    
    this.shadowRoots.clear();
  }
}


  // Module: src/modules/intersection-observer-manager.js

/**
 * Intersection Observer-based performance optimization manager
 * Tracks element visibility and reduces expensive DOM queries
 */
class IntersectionObserverManager {
  constructor(elementDetector) {
    this.elementDetector = elementDetector;
    this.shadowDOMManager = null; // Will be set by KeyPilot
    
    // Observer for tracking interactive elements in viewport
    this.interactiveObserver = null;
    
    // Observer for tracking overlay visibility
    this.overlayObserver = null;
    
    // Cache of interactive elements currently in viewport
    this.visibleInteractiveElements = new Set();
    
    // Cache of element positions for quick lookups
    this.elementPositionCache = new Map();
    
    // ---- DOM hover listener mode (alternative hover target selection) ----
    // When enabled, we track a single "currently hovered" element using browser-native
    // hover targeting (occlusion + clipping). We prefer *delegated* pointer events
    // on `document` (one listener) over per-element listeners (many listeners) so we
    // don't need to constantly attach/detach listeners as the page mutates.
    this._domHoverEnabled = false;
    this._domHoveredElement = null;
    this._domHoverOnChange = null; // (HTMLElement|null) => void
    this._domHoverUseDelegation = true;
    this._domHoverDelegationAttached = false;
    this._domHoverAttachedElements = new Set(); // legacy per-element mode
    this._domHoverMetrics = { attached: 0, skipped: 0, delegated: 0 };
    // rAF-coalesced re-paint when SPA/Lit strips hover markers while pointer stays put.
    this._domHoverRehealRAF = 0;
    this._boundDocPointerMoveReheal = () => {
      try {
        if (!this._domHoverEnabled || !this._domHoveredElement) return;
        if (this._domHoverRehealRAF) return;
        this._domHoverRehealRAF = requestAnimationFrame(() => {
          this._domHoverRehealRAF = 0;
          try {
            this._rehealDomHoverFocusStyling(this._domHoveredElement);
          } catch { /* ignore */ }
        });
      } catch { /* ignore */ }
    };

    this._boundDomHoverEnter = (e) => {
      try {
        if (!this._domHoverEnabled) return;
        const el = e?.currentTarget;
        if (!el || el.nodeType !== 1) return;
        // Keep a global debug hook for the "currently hovered" element.
        try { window.__KP_HOVERED_INTERACTIVE_EL = el; } catch { /* ignore */ }
        this._domHoveredElement = el;
        if (typeof this._domHoverOnChange === 'function') {
          try { this._domHoverOnChange(/** @type {HTMLElement} */ (el)); } catch { /* ignore */ }
        }
      } catch { /* ignore */ }
    };

    this._boundDomHoverLeave = (e) => {
      try {
        if (!this._domHoverEnabled) return;
        const el = e?.currentTarget;
        if (!el || el.nodeType !== 1) return;
        if (this._domHoveredElement !== el) return;
        this._domHoveredElement = null;
        try { window.__KP_HOVERED_INTERACTIVE_EL = null; } catch { /* ignore */ }
        if (typeof this._domHoverOnChange === 'function') {
          try { this._domHoverOnChange(null); } catch { /* ignore */ }
        }
      } catch { /* ignore */ }
    };

    this._boundDocPointerOver = (e) => {
      try {
        if (!this._domHoverEnabled) return;
        const path = (e && typeof e.composedPath === 'function') ? e.composedPath() : null;
        let raw = null;
        if (Array.isArray(path)) {
          for (const n of path) {
            if (n && n.nodeType === 1) { raw = n; break; }
          }
        }
        if (!raw && e?.target && e.target.nodeType === 1) raw = e.target;
        if (!raw || raw.nodeType !== 1) return;

        const el = /** @type {HTMLElement} */ (raw);
        if (this._isKeyPilotUiElement(el)) return;

        // Stable hover target: prefer host row/tab over nested More/sub-links,
        // and stick while the pointer stays inside the previous host.
        let clickable = null;
        try {
          if (this.elementDetector?.resolveHoverFocusTarget) {
            clickable = this.elementDetector.resolveHoverFocusTarget(
              el,
              this._domHoveredElement
            );
          } else if (this.elementDetector?.findClickable) {
            clickable = this.elementDetector.findClickable(el);
          } else {
            clickable = el;
          }
        } catch {
          clickable = el;
        }

        // If no clickable found and we're in a shadow DOM context, try shadow-piercing query
        if (!clickable && el.getRootNode() instanceof ShadowRoot) {
          try {
            // Search for interactive elements near the cursor position using shadow-piercing
            const shadowElements = this.queryInteractiveAtPoint(e.clientX, e.clientY, 20);
            if (shadowElements.length > 0) {
              const shadowLeaf = shadowElements[0];
              clickable = this.elementDetector?.resolveHoverFocusTarget
                ? this.elementDetector.resolveHoverFocusTarget(shadowLeaf, this._domHoveredElement)
                : shadowLeaf;
            }
          } catch (error) {
            // Shadow query failed, continue with null clickable
            if (window.KEYPILOT_DEBUG) {
              console.warn('[KeyPilot] Shadow-piercing query failed:', error);
            }
          }
        }

        // Optional composite promotion (after host stabilization).
        if (clickable) {
          clickable = this._findParentContainerForClickable(clickable);
        }

        let next = (clickable && clickable.nodeType === 1) ? /** @type {HTMLElement} */ (clickable) : null;
        // html/body are never useful targets; treat as clear so hover chrome does not stick.
        try {
          if (next && (next.tagName === 'HTML' || next.tagName === 'BODY')) next = null;
        } catch { /* ignore */ }

        // Drop a disconnected previous target (Lit can replace the <a> node;
        // the old reference must not stick).
        if (this._domHoveredElement && !this._domHoveredElement.isConnected) {
          this._domHoveredElement = null;
        }

        // Sticky host targeting (resolveHoverFocusTarget) already keeps focus while
        // the pointer moves across nested children — no clear-debounce needed.
        if (next === this._domHoveredElement) {
          // Same target: re-ensure CSS + markers (Lit may wipe while pointer stays put).
          this._rehealDomHoverFocusStyling(next);
          return;
        }
        this._setDomHoveredElement(next);
      } catch { /* ignore */ }
    };

    this._boundDocPointerOut = (e) => {
      try {
        if (!this._domHoverEnabled) return;
        // Primary leave path is pointerover with next=null (non-clickable under cursor).
        // Also clear when leaving the document/window (relatedTarget null), or when
        // relatedTarget is html/body (sites with body-level click handlers used to
        // early-return and leave the previous card stuck).
        const rt = e?.relatedTarget;
        if (rt && rt.nodeType === 1) {
          try {
            if (rt.tagName === 'HTML' || rt.tagName === 'BODY') {
              // fall through to clear
            } else {
              return; // still in-document; pointerover will update/clear
            }
          } catch {
            return;
          }
        }
        if (!this._domHoveredElement) return;
        this._setDomHoveredElement(null);
      } catch { /* ignore */ }
    };

    this._boundWindowBlur = () => {
      try {
        if (!this._domHoverEnabled) return;
        if (!this._domHoveredElement) return;
        this._setDomHoveredElement(null);
      } catch { /* ignore */ }
    };

    // Debounced cache update
    this.cacheUpdateTimeout = null;

    // Selector used to discover "interactive" elements cheaply (no computed style).
    // Note: this intentionally doesn't include cursor:pointer-only elements.
    // Include role=tab so discovery/observers see profile tab strips, etc.
    this.interactiveSelector =
      'a[href], button, input, select, textarea, [role="button"], [role="link"], [role="tab"], [contenteditable="true"], [onclick]';

    // Track elements we have asked the IntersectionObserver to observe.
    // (An element may be observed but not currently intersecting/visible.)
    this.observedInteractiveElements = new Set();

    // MutationObserver-based incremental discovery for dynamic pages.
    this.mutationObserver = null;
    this._pendingAddedRoots = new Set();
    this._pendingRemovedRoots = [];
    this._pendingAttributeTargets = new Set();
    this._mutationProcessScheduled = false;
    this._mutationIdleHandle = null;

    // Background discovery scheduling (avoid doing heavy querySelectorAll during hot startup)
    this._discoverScheduled = false;
    this._discoverIdleHandle = null;
    this._discoverWalker = null;
    this._discoverCursor = null;
    this._discoverDone = false;

    // ---- Spatial index (RBush) — RETIRED ----
    // Product decision: DOM-hover only. Vendor `src/vendor/rbush.js` removed.
    // Residual fields/methods stay as inert stubs so call sites remain safe until
    // a later P3 tree-shake removes them entirely.
    this._rtree = null;
    this._rtreeItemsByElement = new Map(); // Element -> item object (kept by reference for removal)
    this._rtreeReady = false;
    this._rtreeDisabledByDomHover = true; // permanently disabled
    this._rtreeMaxEntries = 16;
    this._rtreeRemoveEquals = null; // unused (reference removal)

    // Destination index for link grouping / display coalescing.
    // Key: normalized destination (string), Value: Set<HTMLElement>
    this._destIndex = new Map();
    // Cache for display-rect coalescing (computed on demand).
    this._displayRectCache = {
      version: 0,
      destination: null,
      anchorEl: null,
      // Store the rect in *page coordinates* (minX/minY/maxX/maxY). We convert to viewport
      // coordinates on demand so scrolling doesn't stale-cache the overlay position.
      pageRect: null
    };
    this._rtreeVersion = 0;
    
    // Performance metrics
    this.metrics = {
      cacheHits: 0,
      cacheMisses: 0,
      observerUpdates: 0,
      rtreeQueries: 0,
      rtreeHits: 0,
      rtreeFallbacks: 0,
      culledCount: 0,
      totalCulled: 0,
    };

    // RBush debug overlays (only when ENABLE_DEBUG_PANEL is true)
    this.rbushDebugOverlays = [];
    this.rbushDebugOverlayTimeout = null;

    // Complex page detection and adaptation
    this.complexPageDetector = {
      isAnalyzed: false,
      complexityLevel: 'unknown',
      lastAnalysis: 0,
      analysisInterval: 30000, // Re-analyze every 30 seconds (cheap, but no need to spam)

      // Static metrics (computed once)
      staticMetrics: {
        hasInfiniteScroll: false,
        isSocialMedia: false,
        urlPatterns: []
      },

      // Dynamic metrics (updated periodically)
      dynamicMetrics: {
        observerCount: 0,
        visibleCount: 0,
        rbushItems: 0,
        pendingMutations: 0,
        rtreeHitRate: 0,
      },

      // IO adaptation settings
      ioAdaptation: {
        rootMargin: '100px',
        threshold: [0, 0.1, 0.5, 1.0],
        maxObservations: 1000,
        spatialCullDistance: 200,
        batchSize: 50
      }
    };
  }

  /**
   * Check if a parent container should be focused instead of an individual clickable element.
   * If the hovered element is clickable and its parent has similar clickable properties,
   * and all children of the parent have similar properties, focus the parent instead.
   * @param {HTMLElement} element
   * @returns {HTMLElement}
   */
  _findParentContainerForClickable(element) {
    if (!element || element.nodeType !== 1) return element;

    try {
      // Only apply this logic to elements that are actually clickable
      if (!this.elementDetector?.isLikelyInteractive(element)) return element;

      const parent = element.parentElement;
      if (!parent || parent.nodeType !== 1) return element;

      // Never promote leaf controls into composite containers (tablist, nav, menu…).
      // Those hosts often use delegated clicks; focusing the whole master list is wrong.
      try {
        if (this.elementDetector.isCompositeClickContainer?.(parent)) return element;
      } catch { /* ignore */ }

      // Check if parent is also clickable
      if (!this.elementDetector.isLikelyInteractive(parent)) return element;

      // Get the clickability profiles for comparison
      const elementProfile = this._getClickabilityProfile(element);
      const parentProfile = this._getClickabilityProfile(parent);

      // Check if parent has similar clickable characteristics
      if (!this._profilesAreSimilar(elementProfile, parentProfile)) return element;

      // Check if all clickable children of parent have similar profiles
      const children = Array.from(parent.children);
      const clickableChildren = children.filter(child => {
        if (child.nodeType !== 1) return false;
        return this.elementDetector.isLikelyInteractive(child);
      });

      // If no other clickable children, return original element
      if (clickableChildren.length === 0) return element;

      // Check if all clickable children have similar profiles
      const allChildrenSimilar = clickableChildren.every(child => {
        const childProfile = this._getClickabilityProfile(child);
        return this._profilesAreSimilar(elementProfile, childProfile);
      });

      // If all children have similar clickable characteristics, focus the parent
      if (allChildrenSimilar) {
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot Debug] Focusing parent container instead of child element:', {
            originalElement: element,
            parentElement: parent,
            elementProfile: elementProfile,
            parentProfile: parentProfile
          });
        }
        return parent;
      }
    } catch (error) {
      // If anything fails, return the original element
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Error in parent container detection:', error);
      }
    }

    return element;
  }

  /**
   * Get a profile of what makes an element clickable
   * @param {HTMLElement} element
   * @returns {Object} profile object
   */
  _getClickabilityProfile(element) {
    const profile = {
      tagName: element.tagName,
      href: '',
      role: '',
      hasClickHandler: false,
      hasCursorPointer: false,
      inputType: '',
      isContentEditable: false
    };

    try {
      // Get href for links
      if (element.tagName === 'A') {
        profile.href = this._getNormalizedDestination(element);
      }

      // Get role attribute
      profile.role = (element.getAttribute && element.getAttribute('role') || '').trim().toLowerCase();

      // Check for click handlers
      profile.hasClickHandler = !!(element.onclick ||
                                   element.getAttribute('onclick') ||
                                   this.elementDetector?.hasTrackedClickHandler(element));

      // Check for cursor pointer (only if other conditions don't apply).
      // Prefer ElementDetector so custom-cursor mode still sees real page pointers.
      if (!profile.href && !profile.role && !profile.hasClickHandler) {
        try {
          if (this.elementDetector && typeof this.elementDetector.hasExplicitCursorPointer === 'function') {
            profile.hasCursorPointer = !!this.elementDetector.hasExplicitCursorPointer(element);
          } else {
            profile.hasCursorPointer = !!(window.getComputedStyle &&
                                         window.getComputedStyle(element).cursor === 'pointer');
          }
        } catch {
          profile.hasCursorPointer = false;
        }
      }

      // Get input type for form elements
      if (element.tagName === 'INPUT') {
        profile.inputType = (element.getAttribute('type') || 'text').toLowerCase();
      }

      // Check if content editable
      profile.isContentEditable = !!(element.isContentEditable ||
                                    element.getAttribute('contenteditable') === 'true');

    } catch (error) {
      // Ignore errors when building profile
    }

    return profile;
  }

  /**
   * Check if two clickability profiles are similar enough to be considered
   * part of the same interactive unit
   * @param {Object} profile1
   * @param {Object} profile2
   * @returns {boolean}
   */
  _profilesAreSimilar(profile1, profile2) {
    // Same tag name (button, input, etc.)
    if (profile1.tagName === profile2.tagName) {
      // For inputs, same type
      if (profile1.tagName === 'INPUT' && profile1.inputType === profile2.inputType) {
        return true;
      }
      // For other matching tags
      if (profile1.tagName !== 'INPUT') {
        return true;
      }
    }

    // Same role attribute
    if (profile1.role && profile1.role === profile2.role) {
      return true;
    }

    // Same href (for links)
    if (profile1.href && profile1.href === profile2.href) {
      return true;
    }

    // Both have click handlers (similar interaction pattern)
    if (profile1.hasClickHandler && profile2.hasClickHandler) {
      return true;
    }

    // Both have cursor pointer (similar visual cue)
    if (profile1.hasCursorPointer && profile2.hasCursorPointer) {
      return true;
    }

    // Both are content editable
    if (profile1.isContentEditable && profile2.isContentEditable) {
      return true;
    }

    return false;
  }

  /**
   * Extract and normalize a destination string for an element.
   * For now we only use <a href> to keep semantics reliable.
   * @param {HTMLElement} element
   * @returns {string} normalized destination or '' if none
   */
  _getNormalizedDestination(element) {
    try {
      if (!element || element.nodeType !== 1) return '';
      if (element.tagName !== 'A') return '';
      const hrefAttr = element.getAttribute && element.getAttribute('href');
      if (!hrefAttr) return '';
      // Prefer the fully-resolved absolute href when available (handles base tags).
      const resolved = element.href || hrefAttr;
      // Normalize via URL when possible.
      try {
        return new URL(resolved, window.location.href).href;
      } catch {
        return String(resolved || '');
      }
    } catch {
      return '';
    }
  }

  /**
   * Set the shadow DOM manager for shadow root discovery
   * @param {Object} shadowDOMManager
   */
  setShadowDOMManager(shadowDOMManager) {
    this.shadowDOMManager = shadowDOMManager;
  }

  /**
   * Discover interactive elements inside tracked shadow roots
   * @param {number} maxElements - Maximum elements to discover this slice
   * @param {Function} timeRemaining - Function to check remaining idle time
   * @returns {number} - Number of elements discovered
   */
  discoverShadowDOMElements(maxElements = 50, timeRemaining = () => 0) {
    if (!this.shadowDOMManager?.shadowRoots?.size) return 0;
    // Check if shadow DOM query functions are available (bundled globally)
    if (typeof querySelectorAllDeep !== 'function') return 0;

    let discovered = 0;
    const cap = Math.max(0, Number(this.getIOAdaptation().maxObservations) || 0);

    for (const shadowRoot of this.shadowDOMManager.shadowRoots) {
      if (discovered >= maxElements) break;
      if (timeRemaining() < 1) break;

      try {
        // Use shadow-piercing query to find interactive elements in this shadow root
        const elements = querySelectorAllDeep(this.interactiveSelector, shadowRoot);

        for (const el of elements) {
          if (discovered >= maxElements) break;
          if (cap > 0 && this.observedInteractiveElements.size >= cap) break;

          if (!this.isElementObserved(el)) {
            this.observeInteractiveElement(el);
            discovered++;
          }
        }
      } catch (error) {
        // Skip problematic shadow roots
        if (window.KEYPILOT_DEBUG) {
          console.warn('[KeyPilot] Error discovering elements in shadow root:', error);
        }
      }
    }

    return discovered;
  }

  /**
   * Publish a new DOM-hover focus target (and debug hook).
   * @param {HTMLElement|null} next
   */
  _setDomHoveredElement(next) {
    const el = next && next.nodeType === 1 ? /** @type {HTMLElement} */ (next) : null;
    this._domHoveredElement = el;
    try { window.__KP_HOVERED_INTERACTIVE_EL = el; } catch { /* ignore */ }
    if (typeof this._domHoverOnChange === 'function') {
      try { this._domHoverOnChange(el); } catch { /* ignore */ }
    }
  }

  /**
   * Re-ensure shadow CSS and re-apply focus ring markers if a SPA wiped them
   * while the pointer stayed on the same clickable (archive.org tiles, etc.).
   * Cheap no-op when markers are already present.
   * @param {Element|null|undefined} el
   */
  _rehealDomHoverFocusStyling(el) {
    if (!el || el.nodeType !== 1 || !el.isConnected) return;

    try {
      window.keyPilot?.styleManager?.ensureStylesForNode?.(el);
    } catch { /* ignore */ }

    let missingFocus = false;
    try {
      missingFocus =
        !el.hasAttribute?.('data-kp-focus') &&
        !(el.classList && el.classList.contains('keypilot-focus-element'));
    } catch {
      missingFocus = true;
    }
    if (!missingFocus) return;

    try {
      const om = window.keyPilot?.overlayManager;
      if (om && typeof om.updateFocusOverlayElementStyling === 'function') {
        om.updateFocusOverlayElementStyling(el);
        return;
      }
    } catch { /* ignore */ }

    try {
      window.keyPilot?.state?.setState?.({ _overlayUpdateTrigger: Date.now() });
    } catch { /* ignore */ }
  }

  /**
   * Enable/disable DOM hover listener mode.
   * @param {boolean} enabled
   * @param {(el: HTMLElement|null) => void} [onChange]
   */
  setDomHoverListenersEnabled(enabled, onChange) {
    // DOM-hover is the permanent targeting path; RBush stays disabled.
    const next = !!enabled;
    this._domHoverEnabled = next;
    this._domHoverOnChange = typeof onChange === 'function' ? onChange : null;
    this._rtreeDisabledByDomHover = true;

    // Prefer delegated events on the document (one-time attach).
    if (this._domHoverUseDelegation) {
      if (next) {
        this._domHoverAttachDelegated();
        // Check if mouse is currently over a clickable element
        this._checkInitialMousePosition();
      } else {
        this._domHoverDetachDelegated();
      }
    } else {
      // Legacy per-element attach/detach for already-observed elements.
      try {
        for (const el of this.observedInteractiveElements) {
          if (next) this._domHoverAttach(el);
          else this._domHoverDetach(el);
        }
      } catch { /* ignore */ }
    }

    if (!next) {
      this._domHoveredElement = null;
      try { window.__KP_HOVERED_INTERACTIVE_EL = null; } catch { /* ignore */ }
      if (typeof this._domHoverOnChange === 'function') {
        try { this._domHoverOnChange(null); } catch { /* ignore */ }
      }
    }
  }

  /**
   * @returns {HTMLElement|null}
   */
  getDomHoveredElement() {
    return (this._domHoveredElement && this._domHoveredElement.nodeType === 1)
      ? /** @type {HTMLElement} */ (this._domHoveredElement)
      : null;
  }

  _rectsRoughlyEqual(a, b, tolPx = 1) {
    const t = Math.max(0, Number(tolPx) || 0);
    if (!a || !b) return false;
    return (
      Math.abs(a.left - b.left) <= t &&
      Math.abs(a.top - b.top) <= t &&
      Math.abs(a.right - b.right) <= t &&
      Math.abs(a.bottom - b.bottom) <= t
    );
  }

  _domHoverShouldAttach(el) {
    try {
      if (!el || el.nodeType !== 1) return false;
      // Omit attaching to <a> links whose rect matches an interactive parent.
      // This reduces duplicate hover targeting on "full-row link" UIs.
      if (el.tagName === 'A') {
        const parent = el.parentElement;
        if (parent && parent.nodeType === 1) {
          let parentMatches = false;
          try { parentMatches = !!(parent.matches && parent.matches(this.interactiveSelector)); } catch { parentMatches = false; }
          if (parentMatches) {
            let r1 = null;
            let r2 = null;
            try { r1 = el.getBoundingClientRect(); } catch { r1 = null; }
            try { r2 = parent.getBoundingClientRect(); } catch { r2 = null; }
            if (r1 && r2 && this._rectsRoughlyEqual(r1, r2, 1)) return false;
          }
        }
      }
      return true;
    } catch {
      return false;
    }
  }

  _domHoverAttach(el) {
    if (this._domHoverUseDelegation) return;
    if (!this._domHoverEnabled) return;
    if (!el || el.nodeType !== 1) return;
    if (this._domHoverAttachedElements.has(el)) return;
    if (!this._domHoverShouldAttach(el)) {
      this._domHoverMetrics.skipped++;
      return;
    }
    try {
      // Use mouseenter/mouseleave to avoid event bubbling noise.
      el.addEventListener('mouseenter', this._boundDomHoverEnter, true);
      el.addEventListener('mouseleave', this._boundDomHoverLeave, true);
      this._domHoverAttachedElements.add(el);
      this._domHoverMetrics.attached++;
    } catch {
      // ignore
    }
  }

  _domHoverDetach(el) {
    if (this._domHoverUseDelegation) return;
    if (!el || el.nodeType !== 1) return;
    if (!this._domHoverAttachedElements.has(el)) return;
    try {
      el.removeEventListener('mouseenter', this._boundDomHoverEnter, true);
      el.removeEventListener('mouseleave', this._boundDomHoverLeave, true);
    } catch { /* ignore */ }
    this._domHoverAttachedElements.delete(el);
    // If we detach the currently hovered element, clear it.
    if (this._domHoveredElement === el) {
      this._domHoveredElement = null;
      try { window.__KP_HOVERED_INTERACTIVE_EL = null; } catch { /* ignore */ }
      if (typeof this._domHoverOnChange === 'function') {
        try { this._domHoverOnChange(null); } catch { /* ignore */ }
      }
    }
  }

  _isKeyPilotUiElement(el) {
    try {
      let n = el;
      let guard = 0;
      while (n && n.nodeType === 1 && guard++ < 12) {
        const id = typeof n.id === 'string' ? n.id : '';
        if (id && id.startsWith('kpv2-')) return true;
        const cl = n.classList;
        if (cl && cl.length) {
          for (const c of cl) {
            if (typeof c === 'string' && c.startsWith('kpv2-')) return true;
          }
        }
        n = n.parentElement;
      }
    } catch { /* ignore */ }
    return false;
  }

  _domHoverAttachDelegated() {
    if (!this._domHoverEnabled) return;
    if (this._domHoverDelegationAttached) return;
    try {
      // Attach to `window` in capture phase so we're earlier than document-level capture listeners
      // that might call stopImmediatePropagation(). Also attach mouseover/mouseout as a fallback
      // for environments where PointerEvents are unavailable or suppressed.
      window.addEventListener('pointerover', this._boundDocPointerOver, true);
      window.addEventListener('pointerout', this._boundDocPointerOut, true);
      window.addEventListener('mouseover', this._boundDocPointerOver, true);
      window.addEventListener('mouseout', this._boundDocPointerOut, true);
      // pointermove: re-heal focus markers after SPA wipes without a new pointerover
      // (common on archive.org collection tiles while the pointer rests on a card).
      window.addEventListener('pointermove', this._boundDocPointerMoveReheal, true);
      window.addEventListener('blur', this._boundWindowBlur, true);
      this._domHoverDelegationAttached = true;
      this._domHoverMetrics.delegated++;
    } catch { /* ignore */ }
  }

  _domHoverDetachDelegated() {
    if (!this._domHoverDelegationAttached) return;
    try {
      window.removeEventListener('pointerover', this._boundDocPointerOver, true);
      window.removeEventListener('pointerout', this._boundDocPointerOut, true);
      window.removeEventListener('mouseover', this._boundDocPointerOver, true);
      window.removeEventListener('mouseout', this._boundDocPointerOut, true);
      window.removeEventListener('pointermove', this._boundDocPointerMoveReheal, true);
      window.removeEventListener('blur', this._boundWindowBlur, true);
    } catch { /* ignore */ }
    if (this._domHoverRehealRAF) {
      try { cancelAnimationFrame(this._domHoverRehealRAF); } catch { /* ignore */ }
      this._domHoverRehealRAF = 0;
    }
    this._domHoverDelegationAttached = false;
  }

  /**
   * Check if mouse is currently positioned over a clickable element when DOM hover mode is enabled
   * Adds a one-time mousemove listener to capture the first mouse movement
   */
  _checkInitialMousePosition() {
    if (!this._domHoverEnabled) return;

    // Add a one-time mousemove listener to capture the first mouse movement
    const handleFirstMousemove = (e) => {
      try {
        // Remove this listener immediately to avoid repeated triggers
        window.removeEventListener('mousemove', handleFirstMousemove, true);

        // Get mouse position from the event
        const mouseX = e.clientX;
        const mouseY = e.clientY;

        // Find the element at the mouse position
        const elementAtPoint = this.elementDetector?.deepElementFromPoint
          ? this.elementDetector.deepElementFromPoint(mouseX, mouseY)
          : document.elementFromPoint(mouseX, mouseY);

        if (!elementAtPoint || elementAtPoint.nodeType !== 1) return;

        // Skip KeyPilot UI elements
        if (this._isKeyPilotUiElement(elementAtPoint)) return;

        // Find the clickable element (stable host preferred)
        let clickable = null;
        try {
          clickable = this.elementDetector?.resolveHoverFocusTarget
            ? this.elementDetector.resolveHoverFocusTarget(elementAtPoint, this._domHoveredElement)
            : (this.elementDetector?.findClickable
              ? this.elementDetector.findClickable(elementAtPoint)
              : elementAtPoint);
        } catch {
          clickable = elementAtPoint;
        }

        // Check if we should focus the parent container instead
        if (clickable) clickable = this._findParentContainerForClickable(clickable);

        // Handle shadow DOM elements
        if (!clickable && elementAtPoint.getRootNode() instanceof ShadowRoot) {
          try {
            const shadowElements = this.queryInteractiveAtPoint(mouseX, mouseY, 20);
            if (shadowElements.length > 0) {
              const leaf = shadowElements[0];
              clickable = this.elementDetector?.resolveHoverFocusTarget
                ? this.elementDetector.resolveHoverFocusTarget(leaf, this._domHoveredElement)
                : leaf;
            }
          } catch (error) {
            if (window.KEYPILOT_DEBUG) {
              console.warn('[KeyPilot] Shadow-piercing query failed:', error);
            }
          }
        }

        // If we found a clickable element, trigger the hover callback
        if (clickable && clickable.nodeType === 1) {
          let finalClickable = /** @type {HTMLElement} */ (clickable);
          try {
            if (finalClickable.tagName === 'HTML' || finalClickable.tagName === 'BODY') {
              finalClickable = null;
            }
          } catch { /* ignore */ }

          if (!finalClickable) return;

          // Update hover state and trigger callback
          if (finalClickable !== this._domHoveredElement) {
            this._domHoveredElement = finalClickable;
            try { window.__KP_HOVERED_INTERACTIVE_EL = finalClickable; } catch { /* ignore */ }
            if (typeof this._domHoverOnChange === 'function') {
              try { this._domHoverOnChange(finalClickable); } catch { /* ignore */ }
            }
          }
        }
      } catch (error) {
        if (window.KEYPILOT_DEBUG) {
          console.warn('[KeyPilot] Error in first mousemove check:', error);
        }
      }
    };

    // Add the one-time mousemove listener with capture
    window.addEventListener('mousemove', handleFirstMousemove, true);
  }

  // =============================================================================
  // COMPLEX PAGE DETECTION - Adaptive IO behavior for performance optimization
  // Detects complex pages (Twitter, Facebook, etc.) and adapts IO settings accordingly
  // =============================================================================

  /**
   * Analyze page complexity and determine optimal IO settings
   */
  analyzePageComplexity() {
    const now = Date.now();
    if (this.complexPageDetector.isAnalyzed &&
        now - this.complexPageDetector.lastAnalysis < this.complexPageDetector.analysisInterval) {
      return this.complexPageDetector.complexityLevel;
    }

    this.performStaticAnalysis();
    this.performDynamicAnalysis();
    this.determineComplexityLevel();
    this.adaptIOStrategy();

    this.complexPageDetector.isAnalyzed = true;
    this.complexPageDetector.lastAnalysis = now;

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Complex page analysis completed:', {
        complexityLevel: this.complexPageDetector.complexityLevel,
        staticMetrics: this.complexPageDetector.staticMetrics,
        dynamicMetrics: this.complexPageDetector.dynamicMetrics,
        ioAdaptation: this.complexPageDetector.ioAdaptation
      });
    }

    return this.complexPageDetector.complexityLevel;
  }

  /**
   * Static analysis: URL patterns, initial DOM structure, known complex sites
   */
  performStaticAnalysis() {
    const url = window.location.href;
    const hostname = window.location.hostname;

    // URL pattern detection for known complex sites
    this.complexPageDetector.staticMetrics.urlPatterns = [];
    if (url.includes('twitter.com') || url.includes('x.com')) {
      this.complexPageDetector.staticMetrics.urlPatterns.push('twitter');
      this.complexPageDetector.staticMetrics.isSocialMedia = true;
    }
    if (url.includes('facebook.com') || url.includes('instagram.com')) {
      this.complexPageDetector.staticMetrics.urlPatterns.push('facebook');
      this.complexPageDetector.staticMetrics.isSocialMedia = true;
    }
    if (url.includes('reddit.com')) {
      this.complexPageDetector.staticMetrics.urlPatterns.push('reddit');
      this.complexPageDetector.staticMetrics.isSocialMedia = true;
    }

    // Detect infinite scroll patterns
    this.complexPageDetector.staticMetrics.hasInfiniteScroll =
      hostname.includes('twitter') || hostname.includes('x.com') || hostname.includes('facebook') ||
      hostname.includes('instagram') || hostname.includes('reddit');

    // Avoid expensive DOM-wide counting here. We lean on hostname heuristics + internal counters.
  }

  /**
   * Dynamic analysis: Current DOM state, performance metrics, user interaction patterns
   */
  performDynamicAnalysis() {
    this.complexPageDetector.dynamicMetrics.observerCount = this.observedInteractiveElements.size;
    this.complexPageDetector.dynamicMetrics.visibleCount = this.visibleInteractiveElements.size;
    this.complexPageDetector.dynamicMetrics.rbushItems = this._rtreeItemsByElement?.size || 0;
    this.complexPageDetector.dynamicMetrics.pendingMutations =
      (this._pendingAddedRoots?.size || 0) +
      (this._pendingRemovedRoots?.length || 0) +
      (this._pendingAttributeTargets?.size || 0);

    const queries = Number(this.metrics?.rtreeQueries) || 0;
    const hits = Number(this.metrics?.rtreeHits) || 0;
    this.complexPageDetector.dynamicMetrics.rtreeHitRate = queries > 0 ? (hits / queries) : 0;

    // Note: we intentionally avoid expensive mutation-rate estimation via DOM-wide counts.
  }

  /**
   * Determine complexity level based on all metrics
   */
  determineComplexityLevel() {
    const staticMetrics = this.complexPageDetector.staticMetrics;
    const dynamicMetrics = this.complexPageDetector.dynamicMetrics;

    let complexityScore = 0;

    // Static factors (high weight)
    if (staticMetrics.isSocialMedia) complexityScore += 30;
    if (staticMetrics.hasInfiniteScroll) complexityScore += 20;

    // Performance factors (high weight)
    if (dynamicMetrics.observerCount > 200) complexityScore += 20;
    if (dynamicMetrics.observerCount > 600) complexityScore += 20;
    if (dynamicMetrics.rbushItems > 500) complexityScore += 10;
    if (dynamicMetrics.rbushItems > 2000) complexityScore += 20;
    if (dynamicMetrics.pendingMutations > 200) complexityScore += 15;
    if (dynamicMetrics.pendingMutations > 1000) complexityScore += 25;

    // Determine level
    if (complexityScore >= 50) {
      this.complexPageDetector.complexityLevel = 'high';
    } else if (complexityScore >= 25) {
      this.complexPageDetector.complexityLevel = 'medium';
    } else {
      this.complexPageDetector.complexityLevel = 'low';
    }
  }

  /**
   * Adapt IO strategy based on complexity level
   */
  adaptIOStrategy() {
    const level = this.complexPageDetector.complexityLevel;
    const adaptation = this.complexPageDetector.ioAdaptation;

    switch (level) {
      case 'high':
        // Aggressive optimization for complex sites like Twitter
        adaptation.rootMargin = '25px';  // Much smaller observation area
        adaptation.threshold = [0, 1.0]; // Simplified thresholds
        adaptation.maxObservations = 200; // Limit observations
        adaptation.spatialCullDistance = 100; // Cull distant elements
        adaptation.batchSize = 20; // Smaller processing batches
        break;

      case 'medium':
        // Moderate optimization
        adaptation.rootMargin = '50px';
        adaptation.threshold = [0, 0.5, 1.0];
        adaptation.maxObservations = 500;
        adaptation.spatialCullDistance = 150;
        adaptation.batchSize = 30;
        break;

      case 'low':
      default:
        // Default behavior for simple pages
        adaptation.rootMargin = '100px';
        adaptation.threshold = [0, 0.1, 0.5, 1.0];
        adaptation.maxObservations = 1000;
        adaptation.spatialCullDistance = 200;
        adaptation.batchSize = 50;
        break;
    }
  }

  /**
   * Check if current page is complex
   */
  isComplexPage() {
    return this.analyzePageComplexity() !== 'low';
  }

  /**
   * Get current IO adaptation settings
   */
  getIOAdaptation() {
    return this.complexPageDetector.ioAdaptation;
  }

  /**
   * Get extended viewport bounds for spatial culling
   * @param {number} margin - Additional margin around viewport in pixels
   * @returns {Object} - Bounds object with left, top, right, bottom
   */
  getViewportBounds(margin = 200) {
    const viewportWidth = window.innerWidth;
    const viewportHeight = window.innerHeight;
    const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
    const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;

    return {
      left: scrollX - margin,
      top: scrollY - margin,
      right: scrollX + viewportWidth + margin,
      bottom: scrollY + viewportHeight + margin,
      width: viewportWidth + (2 * margin),
      height: viewportHeight + (2 * margin)
    };
  }

  /**
   * Find interactive elements within spatial bounds (for complex page optimization)
   * @param {Object} bounds - Viewport bounds with left, top, right, bottom
   * @param {number} maxElements - Maximum number of elements to return
   * @returns {Array} - Array of elements within bounds
   */
  findElementsInSpatialBounds(bounds, maxElements = 500) {
    const interactiveElements = document.querySelectorAll(this.interactiveSelector);
    const elementsInBounds = [];

    for (const element of interactiveElements) {
      if (elementsInBounds.length >= maxElements) break;

      try {
        const rect = element.getBoundingClientRect();
        const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
        const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;

        // Convert to document coordinates
        const elementLeft = rect.left + scrollX;
        const elementTop = rect.top + scrollY;
        const elementRight = rect.right + scrollX;
        const elementBottom = rect.bottom + scrollY;

        // Check if element intersects with viewport bounds
        if (elementRight > bounds.left &&
            elementLeft < bounds.right &&
            elementBottom > bounds.top &&
            elementTop < bounds.bottom) {
          elementsInBounds.push(element);
        }
      } catch (error) {
        // Skip elements that cause errors (e.g., detached elements)
        continue;
      }
    }

    return elementsInBounds;
  }

  /**
   * Periodically cull observations for elements outside viewport (complex pages only)
   */
  startSpatialCulling() {
    if (!this.isComplexPage()) return;

    // Clear any existing culling interval
    if (this._spatialCullingInterval) {
      clearInterval(this._spatialCullingInterval);
    }

    const adaptation = this.getIOAdaptation();

    // Cull every 3 seconds on complex pages
    this._spatialCullingInterval = setInterval(() => {
      if (!this.interactiveObserver || !this.isComplexPage()) {
        this.stopSpatialCulling();
        return;
      }

      const viewportBounds = this.getViewportBounds(adaptation.spatialCullDistance);
      const elementsToCull = [];

      // Find observed elements outside viewport bounds
      for (const element of this.observedInteractiveElements) {
        try {
          const rect = element.getBoundingClientRect();
          const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
          const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;

          const elementLeft = rect.left + scrollX;
          const elementTop = rect.top + scrollY;
          const elementRight = rect.right + scrollX;
          const elementBottom = rect.bottom + scrollY;

          // Check if element is completely outside viewport bounds
          if (elementRight < viewportBounds.left ||
              elementLeft > viewportBounds.right ||
              elementBottom < viewportBounds.top ||
              elementTop > viewportBounds.bottom) {
            elementsToCull.push(element);
          }
        } catch (error) {
          // Element might be detached, mark for cleanup
          elementsToCull.push(element);
        }
      }

      // Remove observations for out-of-bounds elements
      if (elementsToCull.length > 0) {
        elementsToCull.forEach(element => {
          this.unobserveInteractiveElement(element);
        });

        // Update culling metrics
        this.metrics.culledCount = elementsToCull.length;
        this.metrics.totalCulled += elementsToCull.length;

        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot Debug] Spatial culling removed observations:', {
            culledCount: elementsToCull.length,
            totalCulled: this.metrics.totalCulled,
            remainingObservations: this.observedInteractiveElements.size
          });
        }
      }
    }, 3000); // Check every 3 seconds
  }

  /**
   * Stop spatial culling
   */
  stopSpatialCulling() {
    if (this._spatialCullingInterval) {
      clearInterval(this._spatialCullingInterval);
      this._spatialCullingInterval = null;
    }
  }

  async init() {
    // Setup spatial index and ensure it's ready before callers rely on RBush queries.
    // KeyPilot.enable() awaits init(); without awaiting here, the extension can re-enable
    // with RBush still uninitialized, causing hover selection to temporarily fail.
    await this.setupSpatialIndex();

    this.setupInteractiveElementObserver();
    this.setupOverlayObserver();
    this.setupMutationObserver();

    // Only start periodic updates after observers are set up
    if (this.interactiveObserver && this.overlayObserver) {
      this.startPeriodicCacheUpdate();

      // Start spatial culling for complex pages
      if (this.isComplexPage()) {
        this.startSpatialCulling();
      }
    }
  }

  async setupSpatialIndex() {
    // RBush retired (DOM-hover only). Keep stubs inert; do not init or wait for vendor.
    this._rtree = null;
    this._rtreeReady = false;
    this._rtreeDisabledByDomHover = true;
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] RBush spatial index skipped - DOM-hover only targeting');
    }
  }

  /**
   * @deprecated RBush retired; no-op kept for API compatibility until tree-shake.
   */
  waitForRBush() {
    return Promise.resolve();
  }

  setupMutationObserver() {
    if (!window.MutationObserver) return;
    if (this.mutationObserver) return;

    try {
      this.mutationObserver = new MutationObserver((mutations) => {
        let sawWork = false;

        for (const mutation of mutations) {
          if (mutation.type === 'childList') {
            // Added nodes: discover any interactive descendants.
            for (const n of mutation.addedNodes) {
              if (n && n.nodeType === 1) {
                this._pendingAddedRoots.add(n);
                sawWork = true;
              }
            }

            // Removed nodes: unobserve any interactive descendants we were tracking.
            for (const n of mutation.removedNodes) {
              if (n && n.nodeType === 1) {
                this._pendingRemovedRoots.push(n);
                sawWork = true;
              }
            }
          } else if (mutation.type === 'attributes') {
            const t = mutation.target;
            if (t && t.nodeType === 1) {
              this._pendingAttributeTargets.add(t);
              sawWork = true;
            }
          }
        }

        if (sawWork) {
          this.scheduleProcessMutations();
        }
      });

      // Watch for subtree changes and attribute changes that commonly flip "interactivity".
      // We avoid watching class/style because it's extremely noisy on many sites.
      this.mutationObserver.observe(document, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['href', 'role', 'onclick', 'contenteditable', 'aria-disabled', 'disabled']
      });
    } catch (e) {
      console.warn('[KeyPilot] Failed to set up MutationObserver for interactive discovery:', e);
      this.mutationObserver = null;
    }
  }

  scheduleProcessMutations() {
    if (this._mutationProcessScheduled) return;
    this._mutationProcessScheduled = true;

    const run = (deadline) => {
      this._mutationProcessScheduled = false;
      this._mutationIdleHandle = null;
      this.processPendingMutations(deadline);
    };

    // Run in idle time to avoid interfering with input/animation.
    if (typeof window.requestIdleCallback === 'function') {
      this._mutationIdleHandle = window.requestIdleCallback(run, { timeout: 500 });
    } else {
      this._mutationIdleHandle = window.setTimeout(() => run({ timeRemaining: () => 0 }), 0);
    }
  }

  processPendingMutations(deadline) {
    if (!this.interactiveObserver) {
      // Clear queues if we can't act on them.
      this._pendingAddedRoots.clear();
      this._pendingRemovedRoots.length = 0;
      this._pendingAttributeTargets.clear();
      return;
    }

    const timeRemaining = typeof deadline?.timeRemaining === 'function'
      ? () => deadline.timeRemaining()
      : () => 0;

    // 1) Process removals first to avoid holding onto detached elements.
    while (this._pendingRemovedRoots.length > 0) {
      const root = this._pendingRemovedRoots.pop();
      this.unobserveInteractiveInSubtree(root);
      if (timeRemaining() < 2) break;
    }

    // 2) Process attribute changes (can flip interactivity on existing nodes).
    if (timeRemaining() >= 2 && this._pendingAttributeTargets.size > 0) {
      // Copy out a chunk to avoid iterating a Set while mutating it.
      const batch = [];
      for (const el of this._pendingAttributeTargets) {
        batch.push(el);
        this._pendingAttributeTargets.delete(el);
        if (batch.length >= 50) break;
      }
      for (const el of batch) {
        this.refreshInteractiveObservationForElement(el);
        if (timeRemaining() < 2) break;
      }
    }

    // 3) Process additions (discover interactive descendants).
    if (timeRemaining() >= 2 && this._pendingAddedRoots.size > 0) {
      const batchRoots = [];
      for (const r of this._pendingAddedRoots) {
        batchRoots.push(r);
        this._pendingAddedRoots.delete(r);
        if (batchRoots.length >= 10) break;
      }
      for (const r of batchRoots) {
        this.observeInteractiveInSubtree(r);
        if (timeRemaining() < 2) break;
      }
    }

    // If more work remains, schedule another slice.
    if (this._pendingRemovedRoots.length > 0 || this._pendingAddedRoots.size > 0 || this._pendingAttributeTargets.size > 0) {
      this.scheduleProcessMutations();
    }
  }

  refreshInteractiveObservationForElement(el) {
    if (!el || el.nodeType !== 1) return;

    let shouldObserve = false;
    try {
      // Fast path: selector match.
      shouldObserve = !!(el.matches && el.matches(this.interactiveSelector));
    } catch {
      shouldObserve = false;
    }

    if (!shouldObserve) {
      // Broader heuristic: includes onclick property, tabindex, and (as a last resort) cursor:pointer.
      try {
        shouldObserve = !!this.elementDetector?.isLikelyInteractive?.(el);
      } catch {
        shouldObserve = false;
      }
    }

    if (shouldObserve) {
      this.observeInteractiveElement(el);
    } else {
      this.unobserveInteractiveElement(el);
    }
  }

  observeInteractiveElement(el) {
    if (!this.interactiveObserver || !el || el.nodeType !== 1) return;
    if (this.observedInteractiveElements.has(el)) return;
    try {
      this.interactiveObserver.observe(el);
      this.observedInteractiveElements.add(el);
      this._domHoverAttach(el);
    } catch {
      // Ignore failures on weird nodes
    }
  }

  unobserveInteractiveElement(el) {
    if (!this.interactiveObserver || !el || el.nodeType !== 1) return;
    if (!this.observedInteractiveElements.has(el)) return;
    try {
      this.interactiveObserver.unobserve(el);
    } catch {
      // Ignore
    }
    this._domHoverDetach(el);
    this.observedInteractiveElements.delete(el);
    this.visibleInteractiveElements.delete(el);
    this.elementPositionCache.delete(el);
    this._rtreeRemoveElement(el);
  }

  observeInteractiveInSubtree(root) {
    if (!root || root.nodeType !== 1) return;

    try {
      if (root.matches && root.matches(this.interactiveSelector)) {
        this.observeInteractiveElement(root);
      }
    } catch {
      // Ignore
    }

    // Query inside the newly added subtree only (incremental, not full-document).
    try {
      if (root.querySelectorAll) {
        const matches = root.querySelectorAll(this.interactiveSelector);
        matches.forEach((el) => this.observeInteractiveElement(el));
      }
    } catch {
      // Ignore
    }
  }

  unobserveInteractiveInSubtree(root) {
    if (!root || root.nodeType !== 1) return;

    // Root itself
    this.unobserveInteractiveElement(root);

    // Descendants
    try {
      if (root.querySelectorAll) {
        const matches = root.querySelectorAll(this.interactiveSelector);
        matches.forEach((el) => this.unobserveInteractiveElement(el));
      }
    } catch {
      // Ignore
    }
  }

  setupInteractiveElementObserver() {
    // Analyze page complexity to determine optimal IO settings
    this.analyzePageComplexity();
    const adaptation = this.getIOAdaptation();

    try {
      // Observer for interactive elements with adaptive settings based on page complexity
      this.interactiveObserver = new IntersectionObserver(
        (entries) => {
          this.metrics.observerUpdates++;

          entries.forEach(entry => {
            const element = entry.target;

            if (entry.isIntersecting) {
              this.visibleInteractiveElements.add(element);
              this.updateElementPositionCache(element, element.getBoundingClientRect());
            } else {
              this.visibleInteractiveElements.delete(element);
              this.elementPositionCache.delete(element);
              this._rtreeRemoveElement(element);
            }
          });
        },
        {
          // Adaptive root margin based on page complexity
          rootMargin: adaptation.rootMargin,
          // Adaptive thresholds based on page complexity
          threshold: adaptation.threshold
        }
      );

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] IntersectionObserver created with adaptive settings:', {
          complexityLevel: this.complexPageDetector.complexityLevel,
          rootMargin: adaptation.rootMargin,
          threshold: adaptation.threshold,
          maxObservations: adaptation.maxObservations
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Failed to create IntersectionObserver for interactive elements:', error);
      this.interactiveObserver = null;
    }
  }

  setupOverlayObserver() {
    try {
      // Observer specifically for overlay elements to optimize repositioning
      this.overlayObserver = new IntersectionObserver(
        (entries) => {
          entries.forEach(entry => {
            const overlay = entry.target;
            
            // Hide overlays that are completely out of view to save rendering
            if (entry.intersectionRatio === 0) {
              overlay.style.visibility = 'hidden';
            } else {
              overlay.style.visibility = 'visible';
            }
          });
        },
        {
          rootMargin: '50px',
          threshold: [0, 1.0]
        }
      );
    } catch (error) {
      console.warn('[KeyPilot] Failed to create IntersectionObserver for overlays:', error);
      this.overlayObserver = null;
    }
  }

  startPeriodicCacheUpdate() {
    // Periodic cache updates removed - cache updated on-demand.
    // Initial discovery can still be expensive on huge pages, so schedule it for idle time.
    this.scheduleDiscoverInteractiveElements();
  }

  scheduleDiscoverInteractiveElements() {
    if (!this.interactiveObserver) return;
    if (this._discoverScheduled) return;
    this._discoverScheduled = true;

    const run = (deadline) => {
      this._discoverScheduled = false;
      this._discoverIdleHandle = null;
      try {
        this.discoverInteractiveElements(deadline);
      } catch (e) {
        console.warn('[KeyPilot] Failed to discover interactive elements:', e);
      }
    };

    // Prefer requestIdleCallback (Background Tasks API) when available.
    if (typeof window.requestIdleCallback === 'function') {
      this._discoverIdleHandle = window.requestIdleCallback(run, { timeout: 1000 });
    } else {
      this._discoverIdleHandle = window.setTimeout(() => run({ timeRemaining: () => 0 }), 0);
    }
  }

  resetDiscoveryAndSchedule() {
    // Reset the incremental discovery cursor so we can re-seed observations around the
    // current viewport (useful after scroll-end, major DOM changes, SPA navigations, etc.).
    this._discoverWalker = null;
    this._discoverCursor = null;
    this._discoverDone = false;
    this._discoverScheduled = false;
    this.scheduleDiscoverInteractiveElements();

    // Refresh RBush positions for all currently visible elements after scroll
    // (IntersectionObserver only fires for elements entering/leaving, not for position changes)
    this.refreshVisibleElementPositions();
  }

  refreshVisibleElementPositions() {
    // Update RBush spatial index with new positions for all visible interactive elements
    // This is needed after scrolling because elements that remain visible don't trigger
    // IntersectionObserver callbacks, but their viewport coordinates have changed
    if (!this._rtreeEnabled()) return;

    try {
      for (const element of this.visibleInteractiveElements) {
        if (!element || element.nodeType !== 1) continue;
        try {
          const rect = element.getBoundingClientRect();
          this.updateElementPositionCache(element, rect);
        } catch { /* ignore stale/detached elements */ }
      }
    } catch { /* ignore */ }
  }

  _ensureDiscoverWalker() {
    if (this._discoverWalker && this._discoverCursor) return;
    try {
      const root = document.body || document.documentElement;
      if (!root) return;
      this._discoverWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
      this._discoverCursor = this._discoverWalker.currentNode;
    } catch {
      this._discoverWalker = null;
      this._discoverCursor = null;
    }
  }

  discoverInteractiveElements(deadline) {
    // Skip if observer is not initialized
    if (!this.interactiveObserver) {
      return;
    }

    const adaptation = this.getIOAdaptation();

    // Locality-first, incremental discovery:
    // - Avoid full-page synchronous querySelectorAll.
    // - Walk the DOM in idle slices, but only observe elements near the viewport.
    // - Respect a global max observation cap (adaptive).
    const timeRemaining = typeof deadline?.timeRemaining === 'function'
      ? () => deadline.timeRemaining()
      : () => 0;

    const cap = Math.max(0, Number(adaptation.maxObservations) || 0);
    if (cap > 0 && this.observedInteractiveElements.size >= cap) {
      this._discoverDone = true;
      return;
    }

    this._ensureDiscoverWalker();
    if (!this._discoverWalker) return;

    const margin = Math.max(0, Number(adaptation.spatialCullDistance) || 0);
    const vw = window.innerWidth || 0;
    const vh = window.innerHeight || 0;

    let observedThisSlice = 0;
    const maxPerSlice = Math.max(10, Number(adaptation.batchSize) || 50);

    while (this._discoverCursor) {
      // Stay responsive: stop when idle budget is low and we already did some work.
      if (observedThisSlice >= maxPerSlice) break;
      if (observedThisSlice > 0 && timeRemaining() < 2) break;

      const el = this._discoverCursor;
      // Advance cursor early so errors don't stall scanning.
      try {
        this._discoverCursor = this._discoverWalker.nextNode();
      } catch {
        this._discoverCursor = null;
      }

      if (!el || el.nodeType !== 1) continue;
      if (cap > 0 && this.observedInteractiveElements.size >= cap) { this._discoverDone = true; break; }
      if (this.isElementObserved(el)) continue;

      let matches = false;
      try { matches = !!(el.matches && el.matches(this.interactiveSelector)); } catch { matches = false; }
      if (!matches) continue;

      // Viewport-first: only observe if the element is near the viewport in viewport coordinates.
      // (This avoids scrollX/scrollY and keeps discovery locality-first.)
      let rect;
      try { rect = el.getBoundingClientRect(); } catch { rect = null; }
      if (!rect) continue;
      if (rect.bottom < -margin || rect.top > vh + margin || rect.right < -margin || rect.left > vw + margin) continue;

      this.observeInteractiveElement(el);
      observedThisSlice++;
    }

    // Also discover elements in shadow DOM
    if (timeRemaining() > 1) {
      const shadowDiscovered = this.discoverShadowDOMElements(maxPerSlice - observedThisSlice, timeRemaining);
      observedThisSlice += shadowDiscovered;
    }

    // If scanning is not done and we haven't hit cap, schedule another idle slice.
    if (!this._discoverDone && this._discoverCursor && (cap === 0 || this.observedInteractiveElements.size < cap)) {
      this.scheduleDiscoverInteractiveElements();
    } else {
      this._discoverDone = true;
    }

    // Clean up observers for removed elements
    this.cleanupRemovedElements();
  }

  isElementObserved(element) {
    // Check if element is already being observed
    return this.observedInteractiveElements.has(element) ||
           this.visibleInteractiveElements.has(element) || 
           this.elementPositionCache.has(element);
  }

  cleanupRemovedElements() {
    // Skip if observer is not initialized
    if (!this.interactiveObserver) {
      return;
    }

    // Remove elements that are no longer in the DOM
    for (const element of this.observedInteractiveElements) {
      try {
        if (element && element.isConnected === false) {
          this.unobserveInteractiveElement(element);
          continue;
        }
      } catch { /* ignore */ }
      // Fallback for older environments / weird nodes.
      if (!document.contains(element)) {
        this.unobserveInteractiveElement(element);
      }
    }
  }

  updateElementPositionCache(element, rect) {
    this.elementPositionCache.set(element, {
      left: rect.left,
      top: rect.top,
      right: rect.right,
      bottom: rect.bottom,
      width: rect.width,
      height: rect.height,
      timestamp: Date.now()
    });

    // Keep spatial index in sync with clipped bounds (accounts for parent clipping).
    this._rtreeUpsertElementRect(element);
  }

  _rtreeEnabled() {
    // Permanently disabled: DOM-hover only product decision.
    return false;
  }

  _getScrollXY() {
    const scrollX = (typeof window !== 'undefined' && typeof window.scrollX === 'number')
      ? window.scrollX
      : (window.pageXOffset || 0);
    const scrollY = (typeof window !== 'undefined' && typeof window.scrollY === 'number')
      ? window.scrollY
      : (window.pageYOffset || 0);
    return { scrollX, scrollY };
  }

  /**
   * Extract and normalize a destination string for an element.
   * For now we only use <a href> to keep semantics reliable.
   * @param {HTMLElement} element
   * @returns {string} normalized destination or '' if none
   */
  _getNormalizedDestination(element) {
    try {
      if (!element || element.nodeType !== 1) return '';
      if (element.tagName !== 'A') return '';
      const hrefAttr = element.getAttribute && element.getAttribute('href');
      if (!hrefAttr) return '';
      // Prefer the fully-resolved absolute href when available (handles base tags).
      const resolved = element.href || hrefAttr;
      // Normalize via URL when possible.
      try {
        return new URL(resolved, window.location.href).href;
      } catch {
        return String(resolved || '');
      }
    } catch {
      return '';
    }
  }

  _destIndexAdd(element, destination) {
    if (!destination) return;
    let set = this._destIndex.get(destination);
    if (!set) {
      set = new Set();
      this._destIndex.set(destination, set);
    }
    set.add(element);
  }

  _destIndexRemove(element, destination) {
    if (!destination) return;
    const set = this._destIndex.get(destination);
    if (!set) return;
    set.delete(element);
    if (set.size === 0) this._destIndex.delete(destination);
  }

  /**
   * Compute z-index for an element (including handling auto and stacking contexts)
   * @param {HTMLElement} element
   * @returns {number}
   */
  _computeZIndex(element) {
    if (!element || element.nodeType !== 1) return 0;
    try {
      const style = window.getComputedStyle(element);
      const zIndex = style.zIndex;
      if (zIndex === 'auto') return 0;
      const parsed = parseInt(zIndex, 10);
      return Number.isFinite(parsed) ? parsed : 0;
    } catch {
      return 0;
    }
  }

  _rtreeUpsertElementRect(element) {
    if (!this._rtreeEnabled()) return;
    if (!element || element.nodeType !== 1) return;

    // Use getClientRects() to get visible rectangles (accounts for clipping by parent containers)
    // This ensures we only index the actually clickable portions of elements
    let clientRects;
    try {
      clientRects = element.getClientRects();
    } catch (e) {
      // Fallback to getBoundingClientRect if getClientRects fails
      const rect = element.getBoundingClientRect();
      clientRects = rect ? [rect] : [];
    }

    if (!clientRects || clientRects.length === 0) return;

    // Compute union of all visible client rectangles
    let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
    for (const rect of clientRects) {
      minX = Math.min(minX, rect.left);
      minY = Math.min(minY, rect.top);
      maxX = Math.max(maxX, rect.right);
      maxY = Math.max(maxY, rect.bottom);
    }

    // Convert viewport coordinates to page/document coordinates
    // This ensures the spatial index remains valid across page scrolls
    const { scrollX, scrollY } = this._getScrollXY();

    minX = Number(minX) + scrollX;
    minY = Number(minY) + scrollY;
    maxX = Number(maxX) + scrollX;
    maxY = Number(maxY) + scrollY;

    if (!Number.isFinite(minX) || !Number.isFinite(minY) || !Number.isFinite(maxX) || !Number.isFinite(maxY)) return;

    // Ensure non-negative/valid box.
    if (maxX <= minX || maxY <= minY) return;

    const zIndex = this._computeZIndex(element);
    const existing = this._rtreeItemsByElement.get(element);
    if (existing) {
      // RBush doesn't have an explicit update; remove + reinsert by reference.
      try { this._rtree.remove(existing); } catch { /* ignore */ }

      // Maintain destination index if it changed.
      const nextDest = this._getNormalizedDestination(element);
      const prevDest = existing.destination || '';
      if (prevDest !== nextDest) {
        this._destIndexRemove(element, prevDest);
        this._destIndexAdd(element, nextDest);
        existing.destination = nextDest;
      }

      existing.minX = minX;
      existing.minY = minY;
      existing.maxX = maxX;
      existing.maxY = maxY;
      existing.zIndex = zIndex;
      try { this._rtree.insert(existing); } catch { /* ignore */ }
      this._rtreeVersion++;
      return;
    }

    // =============================================================================
    // RBUSH OPTIMIZATION: omit enclosed elements with same link destination
    // If this element is a link and is completely enclosed by a larger element
    // with the same link destination, omit the smaller element to reduce redundancy
    // =============================================================================
    const destination = this._getNormalizedDestination(element);
    if (destination) {
      // Query for elements that might contain this element
      const containingElements = this._rtree.search({
        minX: minX - 1, // Slight expansion to handle edge cases
        minY: minY - 1,
        maxX: maxX + 1,
        maxY: maxY + 1
      }).filter(item => {
        // Must contain this element
        return item.element !== element && // Not the same element
               item.minX <= minX &&
               item.minY <= minY &&
               item.maxX >= maxX &&
               item.maxY >= maxY &&
               item.element.tagName === 'A' &&
               (item.destination || '') === destination;
      });

      // If any larger element with same link destination contains this element, skip it
      if (containingElements.length > 0) {
        if (window.KEYPILOT_DEBUG) {
          console.log('[KeyPilot Debug] Omitting enclosed link element:', {
            smallerElement: element,
            linkDestination: destination,
            enclosedBy: containingElements[0].element
          });
        }
        return; // Don't add this smaller element
      }

      // Order-independent improvement:
      // If THIS element encloses already-indexed smaller link elements with the same destination,
      // remove the smaller ones before inserting the larger one. This prevents duplicate boxes
      // and reduces hover flicker due to insertion order.
      try {
        const enclosedCandidates = this._rtree.search({
          minX: minX - 1,
          minY: minY - 1,
          maxX: maxX + 1,
          maxY: maxY + 1
        }) || [];
        for (const it of enclosedCandidates) {
          const el2 = it && it.element;
          if (!el2 || el2 === element) continue;
          if (it.minX >= minX &&
              it.minY >= minY &&
              it.maxX <= maxX &&
              it.maxY <= maxY &&
              (it.destination || '') === destination) {
            // Don't remove equal-sized rects (could be the same link rendered twice with same bbox).
            const strictlyInside = (it.minX > minX || it.minY > minY || it.maxX < maxX || it.maxY < maxY);
            if (!strictlyInside) continue;
            this._rtreeRemoveElement(el2);
          }
        }
      } catch { /* ignore */ }
    }

    // =============================================================================
    // RBUSH OPTIMIZATION: expand link bounds to include contained images
    // When an <img> is inside an <a href=""> and the link's computed box is smaller
    // than the image's computed box, expand the link's bounds to match the image
    // =============================================================================
    if (element.tagName === 'A') {
      try {
        const images = element.querySelectorAll('img');
        for (const img of images) {
          // Use getClientRects for images too, for consistency with clipping handling
          let imgRects;
          try {
            imgRects = img.getClientRects();
          } catch (e) {
            const imgRect = img.getBoundingClientRect();
            imgRects = imgRect ? [imgRect] : [];
          }

          if (imgRects && imgRects.length > 0) {
            // Compute union of image's visible rectangles
            let imgMinX = Infinity, imgMinY = Infinity, imgMaxX = -Infinity, imgMaxY = -Infinity;
            for (const rect of imgRects) {
              imgMinX = Math.min(imgMinX, rect.left);
              imgMinY = Math.min(imgMinY, rect.top);
              imgMaxX = Math.max(imgMaxX, rect.right);
              imgMaxY = Math.max(imgMaxY, rect.bottom);
            }

            // Convert image viewport coordinates to page coordinates
            imgMinX = imgMinX + scrollX;
            imgMinY = imgMinY + scrollY;
            imgMaxX = imgMaxX + scrollX;
            imgMaxY = imgMaxY + scrollY;

            // Expand link bounds to include image if image extends beyond link bounds
            if (imgMinX < minX) minX = imgMinX;
            if (imgMinY < minY) minY = imgMinY;
            if (imgMaxX > maxX) maxX = imgMaxX;
            if (imgMaxY > maxY) maxY = imgMaxY;
          }
        }
      } catch (e) {
        // Ignore errors when accessing image bounds
      }
    }

    const item = { minX, minY, maxX, maxY, element, zIndex, destination };
    this._rtreeItemsByElement.set(element, item);
    try { this._rtree.insert(item); } catch { /* ignore */ }
    this._destIndexAdd(element, destination);
    this._rtreeVersion++;
  }

  _rtreeRemoveElement(element) {
    if (!this._rtreeReady || !this._rtree) return;
    const item = this._rtreeItemsByElement.get(element);
    if (!item) return;
    this._rtreeItemsByElement.delete(element);
    try { this._rtree.remove(item); } catch { /* ignore */ }
    try { this._destIndexRemove(element, item.destination || ''); } catch { /* ignore */ }
    this._rtreeVersion++;
  }

  _rectIntersects(a, b) {
    if (!a || !b) return false;
    return !(a.maxX <= b.minX || a.minX >= b.maxX || a.maxY <= b.minY || a.minY >= b.maxY);
  }

  _rectExpandedIntersects(a, b, tolPx = 0) {
    const t = Math.max(0, Number(tolPx) || 0);
    const aa = {
      minX: a.minX - t,
      minY: a.minY - t,
      maxX: a.maxX + t,
      maxY: a.maxY + t
    };
    return this._rectIntersects(aa, b);
  }

  _unionRects(items) {
    let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
    for (const it of items) {
      if (!it) continue;
      minX = Math.min(minX, Number(it.minX));
      minY = Math.min(minY, Number(it.minY));
      maxX = Math.max(maxX, Number(it.maxX));
      maxY = Math.max(maxY, Number(it.maxY));
    }
    if (!Number.isFinite(minX) || !Number.isFinite(minY) || !Number.isFinite(maxX) || !Number.isFinite(maxY)) return null;
    if (maxX <= minX || maxY <= minY) return null;
    return { minX, minY, maxX, maxY };
  }

  _rectContainsPoint(rect, px, py) {
    return px >= rect.minX && px <= rect.maxX && py >= rect.minY && py <= rect.maxY;
  }

  _toViewportRect(pageRect) {
    if (!pageRect) return null;
    const { scrollX, scrollY } = this._getScrollXY();
    const left = pageRect.minX - scrollX;
    const top = pageRect.minY - scrollY;
    const width = pageRect.maxX - pageRect.minX;
    const height = pageRect.maxY - pageRect.minY;
    if (!Number.isFinite(left) || !Number.isFinite(top) || !Number.isFinite(width) || !Number.isFinite(height)) return null;
    if (width <= 0 || height <= 0) return null;
    return { left, top, width, height };
  }

  /**
   * Compute a "display rectangle" for an element that may be part of a cluster
   * of adjacent/overlapping links with the same destination.
   *
   * This is display-only: we still select a real DOM element for click behavior.
   *
   * Safety rule:
   * - Do NOT return a coalesced rect if it would intersect any RBush item that has
   *   a different destination (conservative: avoids obscuring other links).
   *
   * @param {HTMLElement} element
   * @param {object} [opts]
   * @param {number} [opts.tolerancePx] proximity threshold for clustering
   * @returns {{left:number,top:number,width:number,height:number}|null}
   */
  getDisplayRectForElement(element, opts = {}) {
    if (!this._rtreeEnabled()) return null;
    if (!element || element.nodeType !== 1) return null;

    const destination = this._getNormalizedDestination(element);
    if (!destination) return null;

    // Cache: same destination + same anchor element + same RBush version.
    if (this._displayRectCache &&
        this._displayRectCache.version === this._rtreeVersion &&
        this._displayRectCache.destination === destination &&
        this._displayRectCache.anchorEl === element) {
      // Convert cached page rect to viewport rect using current scroll offsets.
      return this._toViewportRect(this._displayRectCache.pageRect) || null;
    }

    const anchorItem = this._rtreeItemsByElement.get(element);
    if (!anchorItem) return null;

    const tol = Math.max(0, Number(opts.tolerancePx));
    const tolerancePx = Number.isFinite(tol) ? tol : 6;

    const set = this._destIndex.get(destination);
    if (!set || set.size < 2) {
      this._displayRectCache = { version: this._rtreeVersion, destination, anchorEl: element, pageRect: anchorItem };
      return this._toViewportRect(anchorItem);
    }

    // Build items list for this destination.
    const items = [];
    for (const el of set) {
      const it = this._rtreeItemsByElement.get(el);
      if (!it) continue;
      // Skip stale nodes proactively.
      try {
        if (el && el.isConnected === false) {
          this._rtreeRemoveElement(el);
          continue;
        }
      } catch { /* ignore */ }
      items.push(it);
    }
    if (items.length < 2) {
      this._displayRectCache = { version: this._rtreeVersion, destination, anchorEl: element, pageRect: anchorItem };
      return this._toViewportRect(anchorItem);
    }

    // Cluster by proximity/overlap; return the connected component that includes anchorItem.
    const visited = new Set();
    const queue = [anchorItem];
    visited.add(anchorItem);

    while (queue.length) {
      const cur = queue.pop();
      for (const other of items) {
        if (visited.has(other)) continue;
        if (this._rectExpandedIntersects(cur, other, tolerancePx) || this._rectExpandedIntersects(other, cur, tolerancePx)) {
          visited.add(other);
          queue.push(other);
        }
      }
    }

    // If there wasn't actually a multi-rect cluster, return the anchor rect.
    if (visited.size < 2) {
      this._displayRectCache = { version: this._rtreeVersion, destination, anchorEl: element, pageRect: anchorItem };
      return this._toViewportRect(anchorItem);
    }

    const component = Array.from(visited);
    const unionPageRect = this._unionRects(component);
    if (!unionPageRect) {
      this._displayRectCache = { version: this._rtreeVersion, destination, anchorEl: element, pageRect: anchorItem };
      return this._toViewportRect(anchorItem);
    }

    // Conservative conflict check: if union intersects any other-destination item, don't coalesce.
    try {
      const hits = this._rtree.search(unionPageRect) || [];
      for (const it of hits) {
        if (!it || !it.element) continue;
        if (component.includes(it)) continue;
        const otherDest = it.destination || '';
        // If the other item has no destination, ignore it (buttons, inputs, etc.).
        // Only enforce the "don't obscure" rule across destinations we can reason about.
        if (otherDest && otherDest !== destination) {
          this._displayRectCache = { version: this._rtreeVersion, destination, anchorEl: element, pageRect: anchorItem };
          return this._toViewportRect(anchorItem);
        }
      }
    } catch { /* ignore */ }

    this._displayRectCache = { version: this._rtreeVersion, destination, anchorEl: element, pageRect: unionPageRect };
    return this._toViewportRect(unionPageRect);
  }

  // =============================================================================
  // OCCLUSION NOTE
  // We intentionally do NOT maintain custom “negative regions” in RBush anymore.
  //
  // Generalized occlusion is handled by pairing RBush (fast bbox candidate generation)
  // with a single DOM hit-test (`elementFromPoint` / `deepElementFromPoint`) at the
  // cursor. We only accept candidates that are in the ancestor chain of the topmost
  // hit-tested element. This naturally respects third-party modals, menus, lightboxes,
  // backdrops, and any other overlays.
  // =============================================================================

  // =============================================================================
  // RBUSH SPATIAL INDEX - Mouse coordinate detection functions
  // These functions use the RBush spatial index to quickly find interactive
  // elements at specific mouse coordinates for hover/focus detection
  // =============================================================================

  /**
   * MOUSE COORDINATE DETECTION: Query indexed interactive elements that intersect a point
   * Uses RBush spatial index for fast coordinate-based lookups during mouse movement
   * @param {number} x - X coordinate
   * @param {number} y - Y coordinate
   * @param {number} radiusPx - Optional radius for point queries
   * @returns {Array} - Array of interactive elements at the coordinates
   */
  queryInteractiveAtPoint(x, y, radiusPx = 0) {
    if (!this._rtreeEnabled()) return [];

    const px = Number(x);
    const py = Number(y);
    const r = Math.max(0, Number(radiusPx) || 0);
    if (!Number.isFinite(px) || !Number.isFinite(py)) return [];

    // Convert viewport coordinates to page/document coordinates to match stored rects
    // Prefer `scrollX/scrollY` (fast path) and avoid touching layout-backed properties.
    const scrollX = (typeof window !== 'undefined' && typeof window.scrollX === 'number')
      ? window.scrollX
      : (window.pageXOffset || 0);
    const scrollY = (typeof window !== 'undefined' && typeof window.scrollY === 'number')
      ? window.scrollY
      : (window.pageYOffset || 0);
    
    const pageX = px + scrollX;
    const pageY = py + scrollY;

    this.metrics.rtreeQueries++;

    const bbox = { minX: pageX - r, minY: pageY - r, maxX: pageX + r, maxY: pageY + r };
    let items = [];
    try {
      items = this._rtree.search(bbox) || [];
    } catch {
      items = [];
    }

    if (!items.length) return [];
    this.metrics.rtreeHits++;

    // Return all positive elements (occlusion is handled by DOM hit-test gating at selection time).
    // Also opportunistically clean up stale items. Prefer `isConnected` over `document.contains()`.
    const out = [];
    for (const it of items) {
      const el = it && it.element;
      if (!el || el.nodeType !== 1) continue;
      try {
        if (el.isConnected === false) {
          this._rtreeRemoveElement(el);
          continue;
        }
      } catch { /* ignore */ }
      out.push(el);
    }

    // Show debug overlays for elements found via RBush tree query
    if (FEATURE_FLAGS.ENABLE_DEBUG_PANEL && out.length > 0) {
      this.showRBushDebugOverlays(out);
    }

    return out;
  }

  /**
   * Get z-index for an element from the rbush item (if available) or compute it
   * @param {HTMLElement} element
   * @returns {number}
   */
  getZIndexForElement(element) {
    if (!element) return 0;
    const item = this._rtreeItemsByElement.get(element);
    if (item && typeof item.zIndex === 'number') {
      return item.zIndex;
    }
    return this._computeZIndex(element);
  }

  /**
   * Pick the best interactive element from a set of RBush candidates, gated by the
   * topmost DOM hit-test element under the cursor (`underEl`).
   *
   * Rules:
   * - If `underEl` exists, ONLY accept candidates that are on the ancestor chain of `underEl`
   *   (shadow-host aware). If none match, return null (prevents “clicking through” overlays).
   * - If `underEl` is null/unknown, fall back to smallest-area candidate.
   *
   * @param {HTMLElement[]} candidates
   * @param {HTMLElement|null} underEl
   * @returns {HTMLElement|null}
   */
  pickBestInteractiveFromCandidates(candidates, underEl) {
    if (!candidates || !candidates.length) return null;

    // If we know what the browser considers topmost, never “click through” it.
    if (underEl && underEl.nodeType === 1) {
      // Avoid allocating a Set for the common case where candidate count is small.
      const useSet = candidates.length > 16;
      const candSet = useSet ? new Set(candidates) : null;
      let n = underEl;
      let depth = 0;
      while (n && depth++ < 20) {
        if (useSet) {
          if (candSet.has(n)) return n;
        } else {
          if (candidates.includes(n)) return n;
        }
        // Prefer parentElement, but handle shadow root hosts as well.
        try {
          const root = n.getRootNode && n.getRootNode();
          n = n.parentElement || (root instanceof ShadowRoot ? root.host : null);
        } catch {
          n = n.parentElement;
        }
      }
      // Topmost element is not inside any RBush candidate -> treat as occluded / not interactive.
      return null;
    }

    // No underEl: choose smallest area rect (best-effort).
    let best = null;
    let bestArea = Infinity;
    for (const el of candidates) {
      const rect = this.elementPositionCache.get(el);
      const w = rect && Number(rect.width);
      const h = rect && Number(rect.height);
      const area = (Number.isFinite(w) ? w : 0) * (Number.isFinite(h) ? h : 0);
      if (area > 0 && area < bestArea) {
        bestArea = area;
        best = el;
      }
    }
    return best;
  }

  /**
   * MOUSE COORDINATE DETECTION: Best-effort mapping from cursor position to interactive element
   * Uses RBush spatial index as a fast pre-filter to find the most likely interactive element
   * at mouse coordinates, avoiding expensive DOM queries during mouse movement.
   *
   * Strategy:
   * - Query spatial index at point.
   * - Prefer a candidate that is `underEl` or an ancestor of `underEl` (shadow-host aware).
   * - If none match the `underEl` chain, return null (prevents “click through” on overlays).
   * - If `underEl` is null/unknown, fall back to smallest-area candidate (best-effort).
   */
  findBestInteractiveForUnderPoint({ x, y, underEl }) {
    if (!this._rtreeEnabled()) return null;

    const candidates = this.queryInteractiveAtPoint(x, y, 0);
    const best = this.pickBestInteractiveFromCandidates(candidates, underEl);
    return best || null;
  }

  /**
   * MOUSE COORDINATE DETECTION: Track element at mouse position for performance metrics and caching
   * Maintains RBush spatial index and performance tracking during mouse movement
   * Optionally accepts pre-computed values to avoid redundant DOM queries in hot paths
   */
  trackElementAtPoint(x, y, element = null, clickable = null) {

    // Important: callers on hot paths may provide `clickable` but intentionally omit `element`
    // to avoid a DOM hit-test (`elementFromPoint`). Preserve that optimization here.
    let resolvedElement = null;
    let resolvedClickable = null;

    if (!element && !clickable) {
      resolvedElement = this.elementDetector.deepElementFromPoint(x, y);
      resolvedClickable = this.elementDetector.findClickable(resolvedElement);
    } else {
      resolvedElement = element || clickable || null;
      resolvedClickable = clickable || (resolvedElement ? this.elementDetector.findClickable(resolvedElement) : null);
    }
    
    // Check if we found this element in our cache (for metrics)
    if (resolvedClickable && this.visibleInteractiveElements.has(resolvedClickable)) {
      this.metrics.cacheHits++;
    } else {
      this.metrics.cacheMisses++;
    }
    
    // Add to cache if it's interactive and visible but not already cached
    if (resolvedClickable && this.interactiveObserver && !this.visibleInteractiveElements.has(resolvedClickable)) {
      // Avoid redundant layout reads: compute rect once and reuse for visibility + caching.
      let rect = null;
      try { rect = resolvedClickable.getBoundingClientRect(); } catch { rect = null; }

      if (rect &&
          rect.width > 0 &&
          rect.height > 0 &&
          rect.bottom > 0 &&
          rect.right > 0 &&
          rect.top < window.innerHeight &&
          rect.left < window.innerWidth) {
        this.visibleInteractiveElements.add(resolvedClickable);
        try { this.interactiveObserver.observe(resolvedClickable); } catch { /* ignore */ }
        this.updateElementPositionCache(resolvedClickable, rect);
      }
    }
    
    return resolvedClickable;
  }

  // Legacy method name for compatibility
  findInteractiveElementAtPoint(x, y) {
    return this.trackElementAtPoint(x, y);
  }

  isPointInRect(x, y, rect) {
    return x >= rect.left && 
           x <= rect.right && 
           y >= rect.top && 
           y <= rect.bottom;
  }

  rectsAreClose(rect1, rect2, tolerance = 5) {
    return Math.abs(rect1.left - rect2.left) <= tolerance &&
           Math.abs(rect1.top - rect2.top) <= tolerance &&
           Math.abs(rect1.width - rect2.width) <= tolerance &&
           Math.abs(rect1.height - rect2.height) <= tolerance;
  }

  isElementVisible(element) {
    const rect = element.getBoundingClientRect();
    return rect.width > 0 && 
           rect.height > 0 && 
           rect.bottom > 0 && 
           rect.right > 0 && 
           rect.top < window.innerHeight && 
           rect.left < window.innerWidth;
  }

  // Observe overlay elements for visibility optimization
  observeOverlay(overlayElement) {
    if (this.overlayObserver && overlayElement) {
      this.overlayObserver.observe(overlayElement);
    }
  }

  unobserveOverlay(overlayElement) {
    if (this.overlayObserver && overlayElement) {
      this.overlayObserver.unobserve(overlayElement);
    }
  }

  // Get performance metrics
  getMetrics() {
    const totalQueries = this.metrics.cacheHits + this.metrics.cacheMisses;
    const cacheHitRate = totalQueries > 0 ? (this.metrics.cacheHits / totalQueries * 100).toFixed(1) : 0;
    
    return {
      ...this.metrics,
      cacheHitRate: `${cacheHitRate}%`,
      visibleElements: this.visibleInteractiveElements.size,
      cachedPositions: this.elementPositionCache.size
    };
  }

  /**
   * Show blue debug overlays for elements returned from RBush tree queries
   * Only active when ENABLE_DEBUG_PANEL is true
   */
  showRBushDebugOverlays(elements) {
    if (!FEATURE_FLAGS.ENABLE_DEBUG_PANEL || !elements?.length) return;

    // Clear any existing overlays
    this.clearRBushDebugOverlays();

    // Create blue overlays for each element
    elements.forEach(element => {
      try {
        const rect = element.getBoundingClientRect();
        if (rect.width === 0 || rect.height === 0) return;

        const overlay = document.createElement('div');
        overlay.style.cssText = `
          position: fixed;
          left: ${rect.left}px;
          top: ${rect.top}px;
          width: ${rect.width}px;
          height: ${rect.height}px;
          background-color: rgba(0,120,255,0.2);
          border: 2px solid rgba(0,120,255,0.9);
          pointer-events: none;
          z-index: 2147483040;
          box-sizing: border-box;
        `;

        document.body.appendChild(overlay);
        this.rbushDebugOverlays.push(overlay);
      } catch (e) {
        // Ignore errors for detached elements
      }
    });

    // Auto-clear overlays after 2 seconds
    if (this.rbushDebugOverlayTimeout) {
      clearTimeout(this.rbushDebugOverlayTimeout);
    }
    this.rbushDebugOverlayTimeout = setTimeout(() => {
      this.clearRBushDebugOverlays();
    }, 2000);
  }

  /**
   * Clear all RBush debug overlays
   */
  clearRBushDebugOverlays() {
    this.rbushDebugOverlays.forEach(overlay => {
      if (overlay && overlay.parentNode) {
        overlay.parentNode.removeChild(overlay);
      }
    });
    this.rbushDebugOverlays = [];

    if (this.rbushDebugOverlayTimeout) {
      clearTimeout(this.rbushDebugOverlayTimeout);
      this.rbushDebugOverlayTimeout = null;
    }
  }

  // Cleanup method
  cleanup() {
    // Detach DOM hover listeners first (before we clear the observed set)
    try {
      // This handles both delegated + legacy per-element modes.
      this.setDomHoverListenersEnabled(false, null);
    } catch { /* ignore */ }
    try { this._domHoverAttachedElements.clear(); } catch { /* ignore */ }

    if (this.mutationObserver) {
      try {
        this.mutationObserver.disconnect();
      } catch { }
      this.mutationObserver = null;
    }

    if (this._mutationIdleHandle) {
      try {
        if (typeof window.cancelIdleCallback === 'function') {
          window.cancelIdleCallback(this._mutationIdleHandle);
        } else {
          clearTimeout(this._mutationIdleHandle);
        }
      } catch { }
      this._mutationIdleHandle = null;
    }
    this._pendingAddedRoots.clear();
    this._pendingRemovedRoots.length = 0;
    this._pendingAttributeTargets.clear();
    this._mutationProcessScheduled = false;

    if (this._discoverIdleHandle) {
      try {
        if (typeof window.cancelIdleCallback === 'function') {
          window.cancelIdleCallback(this._discoverIdleHandle);
        } else {
          clearTimeout(this._discoverIdleHandle);
        }
      } catch { }
      this._discoverIdleHandle = null;
    }
    this._discoverScheduled = false;
    this._discoverWalker = null;
    this._discoverCursor = null;
    this._discoverDone = false;

    if (this.interactiveObserver) {
      this.interactiveObserver.disconnect();
      this.interactiveObserver = null;
    }
    
    if (this.overlayObserver) {
      this.overlayObserver.disconnect();
      this.overlayObserver = null;
    }
    
    if (this.cacheUpdateTimeout) {
      clearTimeout(this.cacheUpdateTimeout);
      this.cacheUpdateTimeout = null;
    }
    
    if (this.cacheUpdateInterval) {
      clearInterval(this.cacheUpdateInterval);
      this.cacheUpdateInterval = null;
    }
    
    this.visibleInteractiveElements.clear();
    this.elementPositionCache.clear();
    this.observedInteractiveElements.clear();

    if (this._rtree && this._rtree.clear) {
      try { this._rtree.clear(); } catch { /* ignore */ }
    }
    this._rtree = null;
    this._rtreeReady = false;
    this._rtreeItemsByElement.clear();

    // Stop spatial culling
    this.stopSpatialCulling();

    // Clear RBush debug overlays
    this.clearRBushDebugOverlays();
  }
}


  // Module: src/modules/optimized-scroll-manager.js
/**
 * Optimized scroll management using Intersection Observer
 * Reduces expensive operations during scroll events
 */
class OptimizedScrollManager {
  /**
   * @param {*} overlayManager
   * @param {*} stateManager
   * @param {{ onScrollFrame?: () => void }} [hooks] - optional live scroll hooks (e.g. highlight refresh)
   */
  constructor(overlayManager, stateManager, hooks = {}) {
    this.overlayManager = overlayManager;
    this.stateManager = stateManager;
    this.onScrollFrame = typeof hooks?.onScrollFrame === 'function' ? hooks.onScrollFrame : null;
    
    // Scroll state tracking
    this.isScrolling = false;
    this.scrollTimeout = null;
    this.scrollStartTime = 0;
    
    // Intersection observer for scroll-sensitive elements
    this.scrollObserver = null;
    
    // Elements that need position updates during scroll
    this.scrollSensitiveElements = new Set();
    
    // Throttled scroll handler
    this.throttledScrollHandler = this.throttle(this.handleScrollThrottled.bind(this), 16); // ~60fps
    
    // Bound event handlers (so cleanup can reliably remove listeners)
    this.boundScrollHandler = null;
    this.boundWheelHandler = null;

    // Performance tracking
    this.scrollMetrics = {
      scrollEvents: 0,
      overlayUpdates: 0,
      throttledCalls: 0,
      averageScrollDuration: 0
    };
  }

  init() {
    this.setupScrollObserver();
    this.setupScrollListeners();
    this.setupStateSubscription();
    this.observeCurrentStateElements();
  }

  observeCurrentStateElements() {
    // Ensure we start tracking the currently focused elements even if state was
    // already populated before OptimizedScrollManager initialized.
    const currentState = this.stateManager?.getState?.();
    if (!currentState) return;

    this.observeElementForScroll(currentState.focusEl);
    this.observeElementForScroll(currentState.inspectorEl || currentState.deleteEl);
    this.observeElementForScroll(currentState.focusedTextElement);
  }

  setupScrollObserver() {
    // Observer to track when elements move in/out of view during scroll
    this.scrollObserver = new IntersectionObserver(
      (entries) => {
        entries.forEach(entry => {
          const element = entry.target;
          
          if (entry.isIntersecting) {
            // Element is visible, may need overlay updates
            this.scrollSensitiveElements.add(element);
          } else {
            // Element is out of view, can skip overlay updates
            this.scrollSensitiveElements.delete(element);
            
            // Hide any overlays for out-of-view elements
            this.hideOverlaysForElement(element);
          }
        });
      },
      {
        rootMargin: '20px', // Small margin to catch elements just entering/leaving
        threshold: [0, 1.0]
      }
    );
  }

  setupScrollListeners() {
    // Use passive listener for better performance
    this.boundScrollHandler = this.handleScroll.bind(this);
    this.boundWheelHandler = this.handleWheel.bind(this);

    document.addEventListener('scroll', this.boundScrollHandler, { 
      passive: true,
      capture: true 
    });
    
    // Also listen for wheel events to predict scroll direction
    document.addEventListener('wheel', this.boundWheelHandler, { 
      passive: true 
    });
  }

  setupStateSubscription() {
    // Subscribe to state changes to track new elements
    this.stateUnsubscribe = this.stateManager.subscribe((newState, prevState) => {
      // Start observing new elements
      if (newState.focusEl !== prevState.focusEl) {
        if (prevState.focusEl) {
          this.unobserveElementForScroll(prevState.focusEl);
        }
        if (newState.focusEl) {
          this.observeElementForScroll(newState.focusEl);
        }
      }
      
      // Shared inspector pick target (Delete / Cols / future kinds)
      const prevInsp = prevState.inspectorEl || prevState.deleteEl || prevState.colsEl;
      const nextInsp = newState.inspectorEl || newState.deleteEl || newState.colsEl;
      if (nextInsp !== prevInsp) {
        if (prevInsp) {
          this.unobserveElementForScroll(prevInsp);
        }
        if (nextInsp) {
          this.observeElementForScroll(nextInsp);
        }
      }
      
      if (newState.focusedTextElement !== prevState.focusedTextElement) {
        if (prevState.focusedTextElement) {
          this.unobserveElementForScroll(prevState.focusedTextElement);
        }
        if (newState.focusedTextElement) {
          this.observeElementForScroll(newState.focusedTextElement);
        }
      }
    });
  }

  handleScroll(event) {
    this.scrollMetrics.scrollEvents++;

    // Mark scroll start
    if (!this.isScrolling) {
      this.isScrolling = true;
      this.scrollStartTime = performance.now();

      // Hide focus overlay immediately when scrolling starts (for R-Tree mode)
      // This prevents the green rectangle from staying visible during scroll
      this.overlayManager?.hideFocusOverlay?.();
    }

    // Use throttled handler for performance
    this.throttledScrollHandler(event);

    // Clear existing timeout and set new one
    if (this.scrollTimeout) {
      clearTimeout(this.scrollTimeout);
    }

    // Detect scroll end
    this.scrollTimeout = setTimeout(() => {
      this.handleScrollEnd();
    }, 100);
  }

  handleScrollThrottled(event) {
    this.scrollMetrics.throttledCalls++;
    
    const currentState = this.stateManager.getState();
    
    // Always update overlay positions during scroll so they track the page movement,
    // even if IntersectionObserver tracking hasn't been established for the element yet.
    // (This fixes the "hover/click rectangle sticks during scroll" issue.)
    if (currentState.focusEl && currentState.focusEl.isConnected) {
      this.updateOverlayPosition(currentState.focusEl, 'focus');
    }

    const inspectorEl = currentState.inspectorEl || currentState.deleteEl || currentState.colsEl;
    if (inspectorEl && inspectorEl.isConnected) {
      this.updateOverlayPosition(inspectorEl, 'inspector');
    }

    // Update focused text element overlays (both focused text overlay and active text input frame)
    if (currentState.focusedTextElement && currentState.focusedTextElement.isConnected) {
      this.updateOverlayPosition(currentState.focusedTextElement, 'focusedText');
    }

    // Text / rectangle select: keep dashed selection rect + carets live with scroll
    // (document-anchored origin). Without this, overlays only jumped on scroll-end.
    if (currentState.mode === 'highlight' && this.onScrollFrame) {
      try {
        this.onScrollFrame();
      } catch (e) {
        // Non-fatal — selection refresh should never break scroll handling
      }
    }
    
    this.scrollMetrics.overlayUpdates++;
  }

  handleWheel(event) {
    // Predict scroll direction and prepare for smooth updates
    const direction = event.deltaY > 0 ? 'down' : 'up';
    
    // Pre-emptively update overlay positions based on predicted scroll
    this.prepareForScroll(direction);
  }

  prepareForScroll(direction) {
    // Pre-calculate positions for smooth scrolling
    const currentState = this.stateManager.getState();
    
    if (currentState.focusEl) {
      this.observeElementForScroll(currentState.focusEl);
    }
    
    const inspectorEl = currentState.inspectorEl || currentState.deleteEl || currentState.colsEl;
    if (inspectorEl) {
      this.observeElementForScroll(inspectorEl);
    }
    
    if (currentState.focusedTextElement) {
      this.observeElementForScroll(currentState.focusedTextElement);
    }
  }

  observeElementForScroll(element) {
    if (element && this.scrollObserver) {
      this.scrollObserver.observe(element);
      this.scrollSensitiveElements.add(element);
    }
  }

  unobserveElementForScroll(element) {
    if (element && this.scrollObserver) {
      this.scrollObserver.unobserve(element);
      this.scrollSensitiveElements.delete(element);
    }
  }

  updateOverlayPosition(element, type) {
    if (!element) return;
    
    // Animation removed - update overlays immediately
    if (type === 'focus') {
      this.overlayManager.updateFocusOverlay(element);
    } else if (type === 'inspector' || type === 'delete' || type === 'cols') {
      const kind = this.stateManager.getState()?.inspectorKind
        || (type === 'cols' ? 'cols' : type === 'delete' ? 'delete' : null);
      if (typeof this.overlayManager.updateInspectorOverlay === 'function') {
        this.overlayManager.updateInspectorOverlay(element, kind);
      } else {
        this.overlayManager.updateDeleteOverlay?.(element);
      }
    } else if (type === 'focusedText') {
      // Update both the focused text overlay and active text input frame
      this.overlayManager.updateFocusedTextOverlay(element);
      this.overlayManager.updateActiveTextInputFrame(element);
    }
  }

  hideOverlaysForElement(element) {
    const currentState = this.stateManager.getState();
    
    // Hide overlays if they're for elements that are out of view
    if (currentState.focusEl === element) {
      this.overlayManager.hideFocusOverlay();
    }

    const inspectorEl = currentState.inspectorEl || currentState.deleteEl || currentState.colsEl;
    if (inspectorEl === element) {
      if (typeof this.overlayManager.hideInspectorOverlay === 'function') {
        this.overlayManager.hideInspectorOverlay();
      } else {
        this.overlayManager.hideDeleteOverlay?.();
      }
    }
    
    if (currentState.focusedTextElement === element) {
      this.overlayManager.hideFocusedTextOverlay();
      this.overlayManager.hideActiveTextInputFrame();
    }
  }

  handleScrollEnd() {
    const scrollDuration = performance.now() - this.scrollStartTime;
    
    // Update average scroll duration
    this.scrollMetrics.averageScrollDuration = 
      (this.scrollMetrics.averageScrollDuration + scrollDuration) / 2;
    
    this.isScrolling = false;
    
    // Force a complete overlay update after scroll ends
    const currentState = this.stateManager.getState();
    
    // Re-query elements at current mouse position for accuracy
    if (currentState.lastMouse.x >= 0 && currentState.lastMouse.y >= 0) {
      // Dispatch a custom event to trigger element re-query
      document.dispatchEvent(new CustomEvent('keypilot:scroll-end', {
        detail: {
          mouseX: currentState.lastMouse.x,
          mouseY: currentState.lastMouse.y
        }
      }));
    }
    
    // Clean up observers for elements that are no longer relevant
    this.cleanupScrollObservers();
  }

  cleanupScrollObservers() {
    // Remove observers for elements that are no longer in the DOM.
    // Use isConnected (not document.contains) so open-shadow nodes are kept.
    for (const element of this.scrollSensitiveElements) {
      if (!element || !element.isConnected) {
        this.unobserveElementForScroll(element);
      }
    }
  }

  // Throttle utility function
  throttle(func, limit) {
    let inThrottle;
    return function(...args) {
      if (!inThrottle) {
        func.apply(this, args);
        inThrottle = true;
        setTimeout(() => inThrottle = false, limit);
      }
    };
  }

  // Get scroll performance metrics
  getScrollMetrics() {
    const throttleRatio = this.scrollMetrics.scrollEvents > 0 ? 
      (this.scrollMetrics.throttledCalls / this.scrollMetrics.scrollEvents * 100).toFixed(1) : 0;
    
    return {
      ...this.scrollMetrics,
      throttleRatio: `${throttleRatio}%`,
      activeSensitiveElements: this.scrollSensitiveElements.size,
      isCurrentlyScrolling: this.isScrolling
    };
  }

  // Cleanup method
  cleanup() {
    if (this.scrollObserver) {
      this.scrollObserver.disconnect();
      this.scrollObserver = null;
    }
    
    if (this.scrollTimeout) {
      clearTimeout(this.scrollTimeout);
      this.scrollTimeout = null;
    }
    
    if (this.stateUnsubscribe) {
      this.stateUnsubscribe();
      this.stateUnsubscribe = null;
    }
    
    this.scrollSensitiveElements.clear();
    
    // Remove event listeners
    if (this.boundScrollHandler) {
      document.removeEventListener('scroll', this.boundScrollHandler, { capture: true });
      this.boundScrollHandler = null;
    }
    if (this.boundWheelHandler) {
      document.removeEventListener('wheel', this.boundWheelHandler);
      this.boundWheelHandler = null;
    }
  }
}


  // Module: src/modules/popover-iframe-bridge.js
/**
 * Shared popover iframe bridge (parent ↔ iframe postMessage).
 *
 * Used by:
 * - content-script.js (page iframes inside KeyPilot popovers; may start full KeyPilot)
 * - pages/popover-bridge.js (chrome-extension:// pages; no content-script injection)
 *
 * Handshake: POPOVER_BRIDGE_INIT → POPOVER_BRIDGE_READY
 * Parent commands: POPOVER_SCROLL
 * Child → parent: POPOVER_REQUEST_CLOSE, POPOVER_BRIDGE_KEYDOWN
 */




/**
 * @typedef {object} PopoverIframeBridgeOptions
 * @property {boolean} [treatSelectAsTyping=false] - extension pages with <select> controls
 * @property {boolean} [closeOnQuote=false] - also treat `'` as close (extension pages)
 * @property {boolean} [enableFClickBeforeKeyPilot=false] - F-activate link before full KP starts
 * @property {() => void} [onBridgeInit] - called once when parent sends INIT (e.g. start KeyPilot)
 * @property {(err: unknown) => void} [onError]
 */

/**
 * Install the popover iframe bridge if this window is an iframe (or always when
 * `force` is implied by caller for extension pages that always run as iframe content).
 *
 * @param {PopoverIframeBridgeOptions} [options]
 * @returns {{ dispose: () => void }|null}
 */
function installPopoverIframeBridge(options = {}) {
  const {
    treatSelectAsTyping = false,
    closeOnQuote = false,
    enableFClickBeforeKeyPilot = false,
    onBridgeInit = null,
    onError = null
  } = options;

  try {
    let bridgeActive = false;
    let keyPilotStarted = false;
    let mouseInsideFrame = true;
    let lastMouse = { x: null, y: null };
    // Close keys from parent INIT (defaults cover open-popover P + link-preview E).
    /** @type {Set<string>} */
    let closeKeySet = new Set(['Escape', 'e', 'E', 'p', 'P']);

    const scrollByY = (deltaY, behavior = 'smooth') => {
      try {
        const el = document.scrollingElement || document.documentElement || document.body;
        if (el && typeof el.scrollBy === 'function') {
          el.scrollBy({ top: deltaY, behavior });
        } else {
          window.scrollBy({ top: deltaY, behavior });
        }
      } catch {
        // ignore
      }
    };

    const scrollToY = (top, behavior = 'smooth') => {
      try {
        window.scrollTo({ top, behavior });
      } catch {
        // ignore
      }
    };

    const deepElementFromPoint = (x, y) => {
      try {
        let el = document.elementFromPoint(x, y);
        while (el && el.shadowRoot && typeof el.shadowRoot.elementFromPoint === 'function') {
          const inner = el.shadowRoot.elementFromPoint(x, y);
          if (!inner || inner === el) break;
          el = inner;
        }
        return el;
      } catch {
        return null;
      }
    };

    const updateMouse = (e) => {
      try {
        if (!e) return;
        if (typeof e.clientX === 'number') lastMouse.x = e.clientX;
        if (typeof e.clientY === 'number') lastMouse.y = e.clientY;
      } catch {
        // ignore
      }
    };

    const setInside = (v) => {
      mouseInsideFrame = !!v;
    };

    const typingAt = (target) =>
      isTypingContext(target, treatSelectAsTyping ? { treatSelectAsTyping: true } : undefined);

    const resolveScrollParams = () => {
      const kp = window.__KeyPilotInstance;
      const pagePx = (typeof kp?._getPageScrollPx === 'function')
        ? kp._getPageScrollPx()
        : SCROLL.PAGE_PX;
      const halfPx = (typeof kp?._getHalfPageScrollPx === 'function')
        ? kp._getHalfPageScrollPx()
        : SCROLL.HALF_PAGE_PX;
      const behavior = (typeof kp?._getScrollBehavior === 'function')
        ? kp._getScrollBehavior()
        : (SCROLL.BEHAVIOR || 'smooth');
      return { pagePx, halfPx, behavior };
    };

    const onMessage = (event) => {
      const data = event?.data;
      if (!data || typeof data.type !== 'string') return;

      if (data.type === MSG.POPOVER_BRIDGE_INIT) {
        bridgeActive = true;
        // Parent can pass layout-aware close keys (e.g. E for preview, P for open-popover).
        try {
          if (Array.isArray(data.closeKeys) && data.closeKeys.length) {
            closeKeySet = new Set(data.closeKeys.map(String));
            // Always allow Escape even if omitted.
            closeKeySet.add('Escape');
          }
        } catch { /* ignore */ }
        // Expose for in-frame KeyPilot (may register keydown after us and win capture order).
        try {
          window.__KP_POPOVER_IFRAME = true;
          window.__KP_POPOVER_CLOSE_KEYS = Array.from(closeKeySet);
        } catch { /* ignore */ }
        try {
          window.parent.postMessage({ type: MSG.POPOVER_BRIDGE_READY }, '*');
        } catch {
          // ignore
        }

        if (typeof onBridgeInit === 'function' && !keyPilotStarted) {
          keyPilotStarted = true;
          try {
            onBridgeInit();
          } catch {
            // ignore
          }
        }
        return;
      }

      if (!bridgeActive) return;

      if (data.type === MSG.POPOVER_SCROLL) {
        const behavior = data.behavior === 'auto' ? 'auto' : 'smooth';
        if (data.command === 'scrollBy') {
          const delta = Number(data.delta) || 0;
          scrollByY(delta, behavior);
        } else if (data.command === 'scrollToTop') {
          scrollToY(0, behavior);
        } else if (data.command === 'scrollToBottom') {
          const height = document.documentElement?.scrollHeight || document.body?.scrollHeight || 0;
          scrollToY(height, behavior);
        }
      }
    };

    const onKeyDown = (e) => {
      if (!bridgeActive) return;
      if (hasModifierKeys(e)) return;

      const key = e.key;
      const typing = typingAt(e.target);

      const requestClose = () => {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        try {
          window.parent.postMessage({ type: MSG.POPOVER_REQUEST_CLOSE, key }, '*');
        } catch {
          // ignore
        }
      };

      // F outside iframe → parent (e.g. close button on chrome).
      if (!typing && !mouseInsideFrame && (key === 'f' || key === 'F')) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        try {
          window.parent.postMessage({ type: MSG.POPOVER_BRIDGE_KEYDOWN, key }, '*');
        } catch {
          // ignore
        }
        return;
      }

      // Close keys from parent (Esc + open/preview toggle keys). Capture-phase so
      // in-frame KeyPilot does not steal E/P for nested actions before we close.
      if (!typing && closeKeySet.has(key)) return requestClose();
      if (key === 'Escape') return requestClose();
      if (closeOnQuote && !typing && key === "'") return requestClose();

      // Pre-KeyPilot F: click link under cursor inside the iframe.
      if (
        enableFClickBeforeKeyPilot &&
        !keyPilotStarted &&
        !typing &&
        (key === 'f' || key === 'F')
      ) {
        let x = lastMouse.x;
        let y = lastMouse.y;
        if (typeof x !== 'number' || typeof y !== 'number') {
          x = Math.floor(window.innerWidth / 2);
          y = Math.floor(window.innerHeight / 2);
        }

        const target = deepElementFromPoint(x, y);
        const link = target?.closest?.('a[href]') || null;
        if (link) {
          e.preventDefault();
          e.stopPropagation();
          e.stopImmediatePropagation();
          try {
            link.click();
          } catch {
            // ignore
          }
        }
        return;
      }

      if (typing) return;

      const { pagePx, halfPx, behavior } = resolveScrollParams();

      // Historical bridge mapping (Z/X page, C/V half, B/N top/bottom).
      if (key === 'z' || key === 'Z') {
        e.preventDefault();
        scrollByY(-pagePx, behavior);
      } else if (key === 'x' || key === 'X') {
        e.preventDefault();
        scrollByY(pagePx, behavior);
      } else if (key === 'c' || key === 'C' || key === 'v' || key === 'V') {
        // Nested overflow under the cursor first; page fallback (same as top-frame C/V).
        e.preventDefault();
        let mx = lastMouse.x;
        let my = lastMouse.y;
        if (typeof mx !== 'number' || typeof my !== 'number') {
          mx = Math.floor(window.innerWidth / 2);
          my = Math.floor(window.innerHeight / 2);
        }
        const sign = (key === 'c' || key === 'C') ? -1 : 1;
        scrollAtPoint(mx, my, sign, halfPx, behavior);
      } else if (key === 'b' || key === 'B') {
        e.preventDefault();
        scrollToY(0, behavior);
      } else if (key === 'n' || key === 'N') {
        e.preventDefault();
        const height = document.documentElement?.scrollHeight || document.body?.scrollHeight || 0;
        scrollToY(height, behavior);
      }
    };

    // Mouse tracking (needed for F-click before KP and inside/outside detection).
    document.addEventListener('mousemove', updateMouse, true);
    document.addEventListener('pointermove', updateMouse, true);
    document.addEventListener('mouseenter', () => setInside(true), true);
    document.addEventListener('mouseleave', () => setInside(false), true);
    try {
      if (document.documentElement) {
        document.documentElement.addEventListener('mouseenter', () => setInside(true), true);
        document.documentElement.addEventListener('mouseleave', () => setInside(false), true);
      }
    } catch {
      // ignore
    }

    window.addEventListener('message', onMessage, true);
    document.addEventListener('keydown', onKeyDown, true);

    return {
      dispose() {
        try {
          window.removeEventListener('message', onMessage, true);
          document.removeEventListener('keydown', onKeyDown, true);
          document.removeEventListener('mousemove', updateMouse, true);
          document.removeEventListener('pointermove', updateMouse, true);
        } catch {
          // ignore
        }
      }
    };
  } catch (error) {
    if (typeof onError === 'function') {
      try {
        onError(error);
      } catch {
        // ignore
      }
    } else {
      console.warn('[KeyPilot] Failed to install popover iframe bridge:', error);
    }
    return null;
  }
}



  // Module: src/modules/keypilot-toggle-handler.js
/**
 * KeyPilotToggleHandler - Manages global toggle functionality for KeyPilot
 * Wraps the KeyPilot instance and provides enable/disable control
 */


class KeyPilotToggleHandler extends EventManager {
  constructor(keyPilotInstance) {
    super();
    
    this.keyPilot = keyPilotInstance;
    this.enabled = true;
    this.initialized = false;
    this.globalToggleKeyHandler = null;
    this.ENABLED_STORAGE_KEY = 'keypilot_enabled';
    this._onRuntimeMessage = this._onRuntimeMessage.bind(this);
    this._onStorageChanged = this._onStorageChanged.bind(this);
    
    // Store original methods for restoration
    this.originalMethods = {
      handleKeyDown: null,
      handleMouseMove: null,
      handleScroll: null
    };
  }

  /**
   * Initialize the toggle handler
   * Queries service worker for current state and sets up message listener
   */
  async initialize() {
    try {
      // Query service worker for current extension state
      const response = await chrome.runtime.sendMessage({ type: 'KP_GET_STATE' });
      
      if (response && typeof response.enabled === 'boolean') {
        this.setEnabled(response.enabled, false); // Don't show notification during initialization
      } else {
        // Default to enabled if no response or invalid response
        this.setEnabled(true, false); // Don't show notification during initialization
      }
    } catch (error) {
      console.warn('[KeyPilotToggleHandler] Failed to query service worker state:', error);
      // Default to enabled on communication failure
      this.setEnabled(true, false); // Don't show notification during initialization
    }

    // Broadcast path: service worker → tabs.sendMessage(KP_TOGGLE_STATE, { enabled }).
    try {
      chrome.runtime.onMessage.addListener(this._onRuntimeMessage);
    } catch {
      // ignore
    }

    // Storage path: keypilot_enabled is the cross-tab source of truth when messages miss
    // (bfcache, sleeping tabs, race with content-script load).
    try {
      if (chrome?.storage?.onChanged?.addListener) {
        chrome.storage.onChanged.addListener(this._onStorageChanged);
      }
    } catch {
      // ignore
    }

    // Always-on hotkeys: when KeyPilot is disabled it won't have key listeners installed,
    // so we keep a separate capture listener for re-enable and control-strip restore.
    this.globalToggleKeyHandler = (e) => {
      try {
        // Avoid double-toggling if another handler already processed this event.
        if (e && e.__kpToggleHandled) return;

        // Alt+K (case-insensitive). e.code==='AltRight' is not needed here; we rely on e.altKey.
        if (e && e.altKey && (e.key === 'k' || e.key === 'K' || e.code === 'KeyK')) {
          e.__kpToggleHandled = true;
          e.preventDefault();
          e.stopPropagation();
          e.stopImmediatePropagation();

          if (typeof chrome !== 'undefined' && chrome.runtime) {
            chrome.runtime.sendMessage({ type: 'KP_TOGGLE_STATE' }).catch(() => {
              // Ignore errors if background script is not available
            });
          }
          return;
        }

        // Alt+J: toggle control strip (works while KeyPilot is disabled).
        if (e && e.altKey && (e.key === 'j' || e.key === 'J' || e.code === 'KeyJ')) {
          if (e.__kpControlStripHandled) return;
          e.__kpControlStripHandled = true;
          e.preventDefault();
          e.stopPropagation();
          e.stopImmediatePropagation();
          try {
            const kp = this.keyPilot;
            if (typeof kp?.toggleControlStripFromHotkey === 'function') {
              kp.toggleControlStripFromHotkey();
            } else {
              kp?.showControlStripFromHotkey?.();
            }
          } catch {
            // Ignore
          }
        }
      } catch {
        // Ignore
      }
    };
    document.addEventListener('keydown', this.globalToggleKeyHandler, { capture: true });

    this.initialized = true;
  }

  /**
   * @param {any} message
   * @param {chrome.runtime.MessageSender} _sender
   * @param {(response?: any) => void} sendResponse
   */
  _onRuntimeMessage(message, _sender, sendResponse) {
    try {
      // Broadcast from SW includes `enabled`. Ignore bare toggle *requests* (no enabled)
      // that are only meant for the service worker.
      if (message?.type === 'KP_TOGGLE_STATE' || message?.type === 'KP_UPDATE_STATE') {
        if (typeof message.enabled === 'boolean') {
          // Message path already represents a user-initiated global change.
          void this.setEnabled(message.enabled, true);
          try { sendResponse({ success: true }); } catch { /* ignore */ }
          return true;
        }
      }
    } catch {
      // ignore
    }
    return false;
  }

  /**
   * @param {Record<string, chrome.storage.StorageChange>} changes
   * @param {string} area
   */
  _onStorageChanged(changes, area) {
    try {
      if (area !== 'sync' && area !== 'local') return;
      if (!changes || !Object.prototype.hasOwnProperty.call(changes, this.ENABLED_STORAGE_KEY)) return;
      const entry = changes[this.ENABLED_STORAGE_KEY];
      const next = entry?.newValue;
      if (typeof next !== 'boolean') return;
      // Storage is the durable cross-tab source of truth; no toast (message path may toast).
      void this.setEnabled(next, false);
    } catch {
      // ignore
    }
  }

  /**
   * Keep the control strip On/Off segment aligned with extension state.
   * Safe when the strip is not yet constructed (no-op).
   * @param {boolean} enabled
   */
  _syncControlStripEnabled(enabled) {
    try {
      const on = !!enabled;
      this.keyPilot?.controlStrip?.setEnabledState?.(on);
      if (!on) {
        this.keyPilot?.controlStrip?.setKeyboardHelpActive?.(false);
      } else {
        this.keyPilot?.controlStrip?.setKeyboardHelpActive?.(!!this.keyPilot?._keyboardHelpVisible);
      }
    } catch {
      // Ignore
    }
  }

  /**
   * Enable or disable KeyPilot functionality
   * @param {boolean} enabled - Whether KeyPilot should be enabled
   * @param {boolean} showNotification - Whether to show toggle notification (default: true)
   */
  async setEnabled(enabled, showNotification = true) {
    const next = !!enabled;

    // Always refresh the control strip indicator (even if functional state is unchanged)
    // so late-created strips and other-tab storage races stay visually correct.
    this._syncControlStripEnabled(next);

    if (this.enabled === next) {
      // Still keep KeyPilot's flag aligned if strip-only refresh was needed.
      try {
        if (this.keyPilot && this.keyPilot.enabled !== next) {
          if (next) await this.enableKeyPilot();
          else this.disableKeyPilot();
        }
      } catch {
        // ignore
      }
      return;
    }

    // Sync with early injection cursor immediately
    if (window.KEYPILOT_EARLY) {
      window.KEYPILOT_EARLY.setEnabled(next);
    }

    this.enabled = next;

    // Emit a semantic action for onboarding / telemetry consumers.
    try {
      document.dispatchEvent(new CustomEvent('keypilot:action', {
        detail: {
          action: 'toggleExtension',
          enabled: next,
          timestamp: Date.now()
        }
      }));
    } catch {
      // ignore
    }

    if (next) {
      await this.enableKeyPilot();
    } else {
      this.disableKeyPilot();
    }

    // Re-sync after enable/disable in case strip was (re)created during the transition.
    this._syncControlStripEnabled(next);

    // Show notification to user only if requested
    if (showNotification) {
      this.showToggleNotification(next);
    }
  }

  /**
   * Enable KeyPilot functionality
   * Restores event listeners, CSS styles, and visual elements
   */
  async enableKeyPilot() {
    if (!this.keyPilot) return;

    try {
      // Keep KeyPilot's own enabled flag in sync (disableKeyPilot sets it false).
      try { this.keyPilot.enabled = true; } catch { /* ignore */ }

      // If we previously disabled via this handler, we may have fully cleaned up the
      // OverlayManager (including tearing down the canvas renderer). Re-enable must
      // explicitly revive the overlay renderer; otherwise hover rectangles won't draw
      // even though key events work.

      // Restore all CSS styles first
      if (this.keyPilot.styleManager) {
        try {
          this.keyPilot.styleManager.restoreAllStyles();
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot restore styles on this page:', error.message);
        }
      }

      // Restore event listeners
      try {
        this.keyPilot.start();
      } catch (error) {
        console.warn('[KeyPilotToggleHandler] Cannot start event listeners on this page:', error.message);
        // On chrome:// pages, continue with limited functionality
      }

      // Ensure cursor is visible
      if (this.keyPilot.cursor) {
        try {
          this.keyPilot.cursor.ensure();
          // Explicitly show cursor after ensuring it exists
          this.keyPilot.cursor.show();
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot show cursor on this page:', error.message);
        }
      }

      // Restore focus detector
      if (this.keyPilot.focusDetector) {
        try {
          this.keyPilot.focusDetector.start();
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot start focus detector on this page:', error.message);
        }
      }

      // Restore intersection manager + permanent DOM-hover targeting.
      // cleanup() detaches delegated hover listeners; without re-enabling them,
      // normal-mode focus outlines never return (mousemove hit-testing is skipped
      // while KeyPilot._domHoverListenersEnabled stays true).
      if (this.keyPilot.intersectionManager) {
        try {
          if (typeof this.keyPilot.intersectionManager.setDomHoverListenersEnabled === 'function') {
            this.keyPilot.intersectionManager.setDomHoverListenersEnabled(
              true,
              (el) => this.keyPilot._handleDomHoverChange?.(el)
            );
          }
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot restore DOM hover listeners:', error.message);
        }
        try {
          await this.keyPilot.intersectionManager.init();
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot initialize intersection manager on this page:', error.message);
        }
      }

      // Restore scroll manager
      if (this.keyPilot.scrollManager) {
        try {
          this.keyPilot.scrollManager.init();
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot initialize scroll manager on this page:', error.message);
        }
      }

      // Restore overlay manager + rendering backend (canvas/DOM/etc.)
      if (this.keyPilot.overlayManager) {
        try {
          // Recreate the overlay observer (it is disconnected + nulled in overlayManager.cleanup()).
          this.keyPilot.overlayManager.setupOverlayObserver();
          // Re-init highlight manager with the new observer.
          if (this.keyPilot.overlayManager.highlightManager) {
            this.keyPilot.overlayManager.highlightManager.initialize(this.keyPilot.overlayManager.overlayObserver);
          }
          // Re-init the active renderer (canvas overlay element, CSS custom props, etc.)
          this.keyPilot.overlayManager.initRenderingMode();
          // DOM-hover mode styles elements directly (blue focus ring) — re-apply after cleanup.
          if (typeof this.keyPilot.overlayManager.setDomHoverFocusColorsEnabled === 'function') {
            this.keyPilot.overlayManager.setDomHoverFocusColorsEnabled(true);
          }
          // Debug panel (if enabled) lives inside overlay manager
          this.keyPilot.overlayManager.initDebugPanel?.();
        } catch (error) {
          console.warn('[KeyPilotToggleHandler] Cannot restore overlays on this page:', error.message);
        }
      }

      // Force an immediate hover refresh so the focus outline reappears without requiring
      // the user to move the mouse past the threshold gate. Prefer DOM-hover's initial
      // under-cursor check (setDomHoverListenersEnabled already runs it); also seed via
      // elementFromPoint for modes / paths that still use updateElementsUnderCursor.
      try {
        const st = this.keyPilot.state?.getState?.();
        const x = Number(st?.lastMouse?.x);
        const y = Number(st?.lastMouse?.y);
        if (Number.isFinite(x) && Number.isFinite(y) && (x !== 0 || y !== 0)) {
          this.keyPilot.updateElementsUnderCursor?.(x, y, false, null);
        }
      } catch { /* ignore */ }

      // Restore floating keyboard reference (state is persisted in storage)
      try {
        this.keyPilot.refreshKeyboardHelpVisibilityFromStorage?.();
      } catch {
        // Ignore
      }

      console.log('[KeyPilotToggleHandler] KeyPilot enabled');
    } catch (error) {
      console.error('[KeyPilotToggleHandler] Error enabling KeyPilot:', error);
      // Continue with partial functionality even if some components fail
    }
  }

  /**
   * Disable KeyPilot functionality
   * Removes event listeners, CSS styles, and all visual elements
   */
  disableKeyPilot() {
    if (!this.keyPilot) return;

    try {
      // Close launcher / omnibox / popovers / onboarding before tearing down DOM.
      try {
        if (typeof this.keyPilot.dismissActiveUI === 'function') {
          this.keyPilot.dismissActiveUI();
        } else if (typeof this.keyPilot.disable === 'function' && this.keyPilot.enabled) {
          // Fallback: full disable path also dismisses UI.
          this.keyPilot.disable();
        }
      } catch (e) {
        console.warn('[KeyPilotToggleHandler] dismissActiveUI failed:', e);
      }

      // Keep KeyPilot's own enabled flag in sync even when only this path runs.
      try { this.keyPilot.enabled = false; } catch { /* ignore */ }

      // Stop event listeners first
      this.keyPilot.stop();

      // Remove the floating keyboard reference widget, if present
      if (this.keyPilot.floatingKeyboardHelp) {
        try {
          this.keyPilot.floatingKeyboardHelp.cleanup();
        } catch {
          // Ignore cleanup errors
        }
        this.keyPilot.floatingKeyboardHelp = null;
      }

      // Clean up cursor completely (remove from DOM)
      if (this.keyPilot.cursor) {
        this.keyPilot.cursor.cleanup();
      }

      // Stop focus detector
      if (this.keyPilot.focusDetector) {
        this.keyPilot.focusDetector.stop();
      }

      // Clean up overlays completely (also closes popover stack)
      if (this.keyPilot.overlayManager) {
        this.keyPilot.overlayManager.cleanup();
      }

      // Clean up intersection manager
      if (this.keyPilot.intersectionManager) {
        this.keyPilot.intersectionManager.cleanup();
      }

      // Clean up scroll manager
      if (this.keyPilot.scrollManager) {
        this.keyPilot.scrollManager.cleanup();
      }

      // Reset state to normal mode
      if (this.keyPilot.state) {
        this.keyPilot.state.reset();
      }

      // Remove ALL CSS styles and classes - this is the critical fix
      if (this.keyPilot.styleManager) {
        this.keyPilot.styleManager.removeAllStyles();
      }

      console.log('[KeyPilotToggleHandler] KeyPilot disabled - all styles and elements removed');
    } catch (error) {
      console.error('[KeyPilotToggleHandler] Error disabling KeyPilot:', error);
      // Continue with cleanup even if some components fail
    }
  }

  /**
   * Show toggle notification to user
   * @param {boolean} enabled - Whether KeyPilot was enabled or disabled
   */
  showToggleNotification(enabled) {
    // Create notification overlay
    const notification = document.createElement('div');
    notification.className = 'kpv2-toggle-notification';
    notification.textContent = enabled ? 'KeyPilot Enabled' : 'KeyPilot Disabled';
    
    // Style the notification
    Object.assign(notification.style, {
      position: 'fixed',
      top: '20px',
      left: '50%',
      transform: 'translateX(-50%)',
      backgroundColor: enabled ? COLORS.NOTIFICATION_SUCCESS : COLORS.NOTIFICATION_ERROR,
      color: 'white',
      padding: '12px 24px',
      borderRadius: '6px',
      fontSize: '14px',
      fontWeight: '500',
      fontFamily: 'system-ui, -apple-system, sans-serif',
      zIndex: String(Z_INDEX.NOTIFICATION),
      boxShadow: `0 4px 12px ${COLORS.NOTIFICATION_SHADOW}`,
      opacity: '0',
      transition: 'opacity 0.3s ease-in-out',
      pointerEvents: 'none'
    });

    // Add to document
    document.body.appendChild(notification);

    // Animation removed - show immediately
    notification.style.opacity = '1';

    // Remove after 2 seconds
    setTimeout(() => {
      if (notification.parentNode) {
        notification.parentNode.removeChild(notification);
      }
    }, 2000);
  }

  /**
   * Get current enabled state
   * @returns {boolean} Whether KeyPilot is currently enabled
   */
  isEnabled() {
    return this.enabled;
  }

  /**
   * Clean up the toggle handler
   */
  cleanup() {
    // Remove message listeners
    try {
      if (chrome?.runtime?.onMessage?.removeListener) {
        chrome.runtime.onMessage.removeListener(this._onRuntimeMessage);
      }
    } catch {
      // ignore
    }
    try {
      if (chrome?.storage?.onChanged?.removeListener) {
        chrome.storage.onChanged.removeListener(this._onStorageChanged);
      }
    } catch {
      // ignore
    }
    // Legacy no-op path if an old handleMessage field existed
    if (chrome.runtime && chrome.runtime.onMessage && this.handleMessage) {
      chrome.runtime.onMessage.removeListener(this.handleMessage);
    }

    // Remove global toggle hotkey listener
    if (this.globalToggleKeyHandler) {
      try {
        document.removeEventListener('keydown', this.globalToggleKeyHandler, { capture: true });
      } catch {
        // Ignore
      }
      this.globalToggleKeyHandler = null;
    }

    // Clean up KeyPilot if disabled
    if (!this.enabled && this.keyPilot) {
      this.keyPilot.cleanup();
    }
  }
}


  // Module: src/modules/settings-manager.js
/**
 * Settings storage + helpers.
 *
 * Stored in chrome.storage.sync so values sync across Chrome profiles and across tabs.
 */




const SETTINGS_STORAGE_KEY = 'kp_settings_v1';

// Re-export search engine catalog so consumers can keep importing from settings-manager.

/** @typedef {import('../config/search-engines.js').SearchEngineId} SearchEngine */

/** @typedef {'crosshair'|'native_arrow'|'native_pointer'} ClickCursorType */
/** @typedef {'t_square'|'crosshair'} TextCursorType */
/** @typedef {'left_edge'|'background_tint'} TextFocusStyle */
/** @typedef {typeof CURSOR_MODE[keyof typeof CURSOR_MODE]} CursorMode */
/** @typedef {'smooth'|'instant'} ScrollSpeed */
/** @typedef {'flash'|'dash'|'marquee'|'scale'|'none'} ClickEffect */

/** Valid text-mode focus field styles (order is settings UI preference). */
const TEXT_FOCUS_STYLE_IDS = Object.freeze(/** @type {const} */ ([
  'left_edge',
  'background_tint'
]));

/** Valid F-key click activation effects (order is settings UI preference). */
const CLICK_EFFECT_IDS = Object.freeze(/** @type {const} */ ([
  'flash',
  'dash',
  'marquee',
  'scale',
  'none'
]));

/**
 * @typedef {{
 *   type: ClickCursorType,
 *   lineWidth: number,
 *   sizePixels: number,
 *   gap: number
 * }} ClickCursorSettings
 */

/** @typedef {'blue'|'green'} FocusColor */

/**
 * @typedef {{
 *   cursor: ClickCursorSettings,
 *   focusColor: FocusColor,
 *   overlayFillEnabled: boolean,
 *   overlayShadowEnabled: boolean,
 *   rectangleThickness: number,
 *   clickEffect: ClickEffect,
 *   keyboardLinkHoverHints: boolean
 * }} ClickModeSettings
 */

/**
 * @typedef {{
 *   cursorType: TextCursorType,
 *   labelsEnabled: boolean,
 *   strokeThickness: number,
 *   focusStyle: TextFocusStyle,
 *   leftEdgeWidth: number
 * }} TextModeSettings
 */

/**
 * @typedef {{
 *   halfPagePx: number,
 *   speed: ScrollSpeed
 * }} ScrollSettings
 */

/**
 * @typedef {{
 *   visible: boolean,
 *   collapsed: boolean
 * }} ControlStripSettings
 */

/**
 * Saved fixed-panel dock / free position (see utils/panel-position.js).
 * Prefer `anchor` when set so layout adapts across viewport sizes.
 * @typedef {{
 *   left?: number,
 *   top?: number,
 *   anchor?: string|null
 * }} PanelPositionSettings
 */

/**
 * Named panel slots that share the generalized positioning system.
 * @typedef {{
 *   keyboardReference: PanelPositionSettings,
 *   controlStrip: PanelPositionSettings
 * }} PanelPositionsSettings
 */

/**
 * @typedef {{
 *   searchEngine: SearchEngine,
 *   cursorMode: CursorMode,
 *   keyboardLayoutId: string,
 *   keyboardReferenceKeyFeedback: boolean,
 *   controlStrip: ControlStripSettings,
 *   panelPositions: PanelPositionsSettings,
 *   clickMode: ClickModeSettings,
 *   textMode: TextModeSettings,
 *   scroll: ScrollSettings
 * }} KeyPilotSettings
 */

/** @type {KeyPilotSettings} */
const DEFAULT_SETTINGS = Object.freeze({
  searchEngine: DEFAULT_SEARCH_ENGINE_ID,
  cursorMode: CURSOR_MODE.NO_CUSTOM_CURSORS,
  keyboardLayoutId: DEFAULT_KEYBOARD_LAYOUT_ID,
  // When true, the floating keyboard reference panel highlights keys on keydown/keyup.
  keyboardReferenceKeyFeedback: true,
  // Floating Control Strip (upper-left): visibility + collapsed (On/Off-only) state.
  controlStrip: Object.freeze({
    visible: true,
    collapsed: true
  }),
  // Dock / free positions for movable chrome (keyboard reference, control strip, …).
  // Anchors re-resolve on resize; free left/top reclamps inside the viewport margin.
  panelPositions: Object.freeze({
    keyboardReference: Object.freeze({ anchor: 'bottom-left' }),
    controlStrip: Object.freeze({ anchor: 'top-left' })
  }),
  clickMode: Object.freeze({
    cursor: Object.freeze({
      type: 'crosshair',
      // Cursor SVG stroke width. Slider range: 1–12.
      lineWidth: 4,
      // Cursor size in pixels. Default is half of previous (was ~30px, now 15px).
      sizePixels: 10,
      // Gap between center and crosshair bars in pixels. 0 = intersecting lines, >0 = separate bars.
      gap: 6
    }),
    // Hover focus ring color (DOM-hover mode default is blue).
    focusColor: 'blue',
    // When true, the focus rectangle can include a translucent fill (where applicable).
    overlayFillEnabled: false,
    // When true, draw a soft outer glow/shadow on the focus rectangle.
    overlayShadowEnabled: false,
    // Focus rectangle border thickness in px.
    rectangleThickness: 3,
    // F-key activation feedback on link-style targets (flash is the default).
    clickEffect: 'flash',
    // When true, hovering a link glows matching green keys on the Keyboard Reference.
    // Off by default (opt-in via Settings → Click Mode).
    keyboardLinkHoverHints: false
  }),
  textMode: Object.freeze({
    cursorType: 't_square',
    // When true, show both labels: "Active text field" + "Press ESC to close".
    labelsEnabled: false,
    // Stroke thickness in px for orange text-mode rectangles.
    strokeThickness: 3,
    // How the focused text field is styled while in text mode.
    // left_edge: pulsating orange bar on the left inset edge (default).
    // background_tint: full-field orange wash (legacy).
    focusStyle: 'left_edge',
    // Width of the left-edge pulse bar in px (when focusStyle is left_edge).
    leftEdgeWidth: 5
  }),
  scroll: Object.freeze({
    // C / V scroll distance in pixels (default = prior 400 × 1.25).
    halfPagePx: SCROLL.HALF_PAGE_PX,
    // Animation speed for keyboard scrolling: smooth (animated) or instant (jump).
    speed: SCROLL.BEHAVIOR === 'smooth' ? 'smooth' : 'instant'
  })
});

/**
 * @param {any} raw
 * @returns {SearchEngine}
 */
function normalizeSearchEngine(raw) {
  return normalizeSearchEngineId(raw);
}

/**
 * @param {any} raw
 * @returns {CursorMode}
 */
function normalizeCursorMode(raw) {
  if (raw === CURSOR_MODE.NO_CUSTOM_CURSORS || raw === CURSOR_MODE.CUSTOM_CURSORS) return raw;
  return DEFAULT_SETTINGS.cursorMode;
}

/**
 * @param {any} raw
 * @param {boolean} fallback
 */
function normalizeBoolean(raw, fallback) {
  if (raw === true || raw === false) return raw;
  if (raw === 'true') return true;
  if (raw === 'false') return false;
  return !!fallback;
}

/**
 * @param {any} raw
 * @param {number} fallback
 * @param {number} min
 * @param {number} max
 */
function normalizeNumber(raw, fallback, min, max) {
  const n = typeof raw === 'number' ? raw : (typeof raw === 'string' ? Number(raw) : NaN);
  const v = Number.isFinite(n) ? n : fallback;
  const clamped = Math.min(Math.max(v, min), max);
  return clamped;
}

/**
 * @param {any} raw
 * @returns {ClickCursorType}
 */
function normalizeClickCursorType(raw) {
  if (raw === 'crosshair' || raw === 'native_arrow' || raw === 'native_pointer') return raw;
  return DEFAULT_SETTINGS.clickMode.cursor.type;
}

/**
 * @param {any} raw
 * @returns {ClickEffect}
 */
function normalizeClickEffect(raw) {
  if (raw === 'flash' || raw === 'dash' || raw === 'marquee' || raw === 'scale' || raw === 'none') {
    return raw;
  }
  return DEFAULT_SETTINGS.clickMode.clickEffect;
}

/**
 * @param {any} raw
 * @returns {TextCursorType}
 */
function normalizeTextCursorType(raw) {
  if (raw === 't_square' || raw === 'crosshair') return raw;
  return DEFAULT_SETTINGS.textMode.cursorType;
}

/**
 * @param {any} raw
 * @returns {TextFocusStyle}
 */
function normalizeTextFocusStyle(raw) {
  if (raw === 'left_edge' || raw === 'background_tint') return raw;
  return DEFAULT_SETTINGS.textMode.focusStyle;
}

/**
 * @param {any} raw
 * @returns {FocusColor}
 */
function normalizeFocusColor(raw) {
  if (raw === 'blue' || raw === 'green') return raw;
  return DEFAULT_SETTINGS.clickMode.focusColor;
}

/**
 * @param {any} raw
 * @returns {ClickModeSettings}
 */
function normalizeClickMode(raw) {
  const stored = raw && typeof raw === 'object' ? raw : {};
  const storedCursor = stored.cursor && typeof stored.cursor === 'object' ? stored.cursor : {};
  return {
    cursor: {
      type: normalizeClickCursorType(storedCursor.type),
      lineWidth: normalizeNumber(
        storedCursor.lineWidth,
        DEFAULT_SETTINGS.clickMode.cursor.lineWidth,
        1,
        12
      ),
      sizePixels: normalizeNumber(
        storedCursor.sizePixels,
        DEFAULT_SETTINGS.clickMode.cursor.sizePixels,
        5,
        60
      ),
      gap: normalizeNumber(
        storedCursor.gap,
        DEFAULT_SETTINGS.clickMode.cursor.gap,
        0,
        20
      )
    },
    focusColor: normalizeFocusColor(stored.focusColor),
    overlayFillEnabled: normalizeBoolean(
      stored.overlayFillEnabled,
      DEFAULT_SETTINGS.clickMode.overlayFillEnabled
    ),
    overlayShadowEnabled: normalizeBoolean(
      stored.overlayShadowEnabled,
      DEFAULT_SETTINGS.clickMode.overlayShadowEnabled
    ),
    rectangleThickness: normalizeNumber(
      stored.rectangleThickness,
      DEFAULT_SETTINGS.clickMode.rectangleThickness,
      1,
      16
    ),
    clickEffect: normalizeClickEffect(stored.clickEffect),
    keyboardLinkHoverHints: normalizeBoolean(
      stored.keyboardLinkHoverHints,
      DEFAULT_SETTINGS.clickMode.keyboardLinkHoverHints
    )
  };
}

/**
 * @param {any} raw
 * @returns {TextModeSettings}
 */
function normalizeTextMode(raw) {
  const stored = raw && typeof raw === 'object' ? raw : {};
  return {
    cursorType: normalizeTextCursorType(stored.cursorType),
    labelsEnabled: normalizeBoolean(stored.labelsEnabled, DEFAULT_SETTINGS.textMode.labelsEnabled),
    strokeThickness: normalizeNumber(
      stored.strokeThickness,
      DEFAULT_SETTINGS.textMode.strokeThickness,
      1,
      16
    ),
    focusStyle: normalizeTextFocusStyle(stored.focusStyle),
    leftEdgeWidth: normalizeNumber(
      stored.leftEdgeWidth,
      DEFAULT_SETTINGS.textMode.leftEdgeWidth,
      1,
      24
    )
  };
}

/**
 * @param {any} raw
 * @returns {ScrollSpeed}
 */
function normalizeScrollSpeed(raw) {
  if (raw === 'smooth' || raw === 'instant') return raw;
  // Accept CSS scroll-behavior aliases from older/local experiments.
  if (raw === 'auto') return 'instant';
  return DEFAULT_SETTINGS.scroll.speed;
}

/**
 * @param {any} raw
 * @returns {ScrollSettings}
 */
function normalizeScroll(raw) {
  const stored = raw && typeof raw === 'object' ? raw : {};
  return {
    halfPagePx: normalizeNumber(
      stored.halfPagePx,
      DEFAULT_SETTINGS.scroll.halfPagePx,
      50,
      2000
    ),
    speed: normalizeScrollSpeed(stored.speed)
  };
}

/**
 * @param {any} raw
 * @returns {ControlStripSettings}
 */
function normalizeControlStrip(raw) {
  const stored = raw && typeof raw === 'object' ? raw : {};
  return {
    visible: normalizeBoolean(stored.visible, DEFAULT_SETTINGS.controlStrip.visible),
    collapsed: normalizeBoolean(stored.collapsed, DEFAULT_SETTINGS.controlStrip.collapsed)
  };
}

const PANEL_ANCHOR_IDS = new Set([
  'top-left',
  'top-center',
  'top-right',
  'middle-left',
  'middle-right',
  'bottom-left',
  'bottom-center',
  'bottom-right'
]);

/**
 * @param {any} raw
 * @param {PanelPositionSettings} fallback
 * @returns {PanelPositionSettings}
 */
function normalizePanelPositionEntry(raw, fallback) {
  const fb = fallback && typeof fallback === 'object' ? fallback : {};
  if (!raw || typeof raw !== 'object') {
    return {
      left: Number.isFinite(fb.left) ? fb.left : undefined,
      top: Number.isFinite(fb.top) ? fb.top : undefined,
      anchor: typeof fb.anchor === 'string' ? fb.anchor : (fb.anchor === null ? null : undefined)
    };
  }
  /** @type {PanelPositionSettings} */
  const out = {};
  const left = typeof raw.left === 'number' ? raw.left : (typeof raw.left === 'string' ? Number(raw.left) : NaN);
  const top = typeof raw.top === 'number' ? raw.top : (typeof raw.top === 'string' ? Number(raw.top) : NaN);
  if (Number.isFinite(left)) out.left = left;
  if (Number.isFinite(top)) out.top = top;
  if (raw.anchor === null) {
    out.anchor = null;
  } else if (typeof raw.anchor === 'string' && PANEL_ANCHOR_IDS.has(raw.anchor.trim())) {
    out.anchor = raw.anchor.trim();
  } else if (typeof fb.anchor === 'string' && !Number.isFinite(left) && !Number.isFinite(top)) {
    out.anchor = fb.anchor;
  }
  // If nothing useful, fall back to default entry.
  if (out.left === undefined && out.top === undefined && out.anchor === undefined) {
    return {
      left: Number.isFinite(fb.left) ? fb.left : undefined,
      top: Number.isFinite(fb.top) ? fb.top : undefined,
      anchor: typeof fb.anchor === 'string' ? fb.anchor : (fb.anchor === null ? null : undefined)
    };
  }
  return out;
}

/**
 * @param {any} raw
 * @returns {PanelPositionsSettings}
 */
function normalizePanelPositions(raw) {
  const stored = raw && typeof raw === 'object' ? raw : {};
  return {
    keyboardReference: normalizePanelPositionEntry(
      stored.keyboardReference,
      DEFAULT_SETTINGS.panelPositions.keyboardReference
    ),
    controlStrip: normalizePanelPositionEntry(
      stored.controlStrip,
      DEFAULT_SETTINGS.panelPositions.controlStrip
    )
  };
}

/**
 * Map Settings scroll speed to ScrollOptions.behavior.
 * @param {ScrollSpeed|string|undefined|null} speed
 * @returns {'smooth'|'auto'}
 */
function scrollBehaviorFromSpeed(speed) {
  return normalizeScrollSpeed(speed) === 'instant' ? 'auto' : 'smooth';
}

/**
 * @returns {Promise<KeyPilotSettings>}
 */
async function getSettings() {
  try {
    let stored = await storageGetValue(SETTINGS_STORAGE_KEY, null);
    if (!stored || typeof stored !== 'object') stored = {};
    return {
      ...DEFAULT_SETTINGS,
      ...stored,
      searchEngine: normalizeSearchEngine(stored?.searchEngine),
      cursorMode: normalizeCursorMode(stored?.cursorMode),
      keyboardLayoutId: normalizeKeyboardLayoutId(stored?.keyboardLayoutId),
      keyboardReferenceKeyFeedback: normalizeBoolean(
        stored?.keyboardReferenceKeyFeedback,
        DEFAULT_SETTINGS.keyboardReferenceKeyFeedback
      ),
      controlStrip: normalizeControlStrip(stored?.controlStrip),
      panelPositions: normalizePanelPositions(stored?.panelPositions),
      clickMode: normalizeClickMode(stored?.clickMode),
      textMode: normalizeTextMode(stored?.textMode),
      scroll: normalizeScroll(stored?.scroll)
    };
  } catch (_e) {
    return {
      ...DEFAULT_SETTINGS,
      controlStrip: { ...DEFAULT_SETTINGS.controlStrip },
      panelPositions: {
        keyboardReference: { ...DEFAULT_SETTINGS.panelPositions.keyboardReference },
        controlStrip: { ...DEFAULT_SETTINGS.panelPositions.controlStrip }
      },
      clickMode: { ...DEFAULT_SETTINGS.clickMode, cursor: { ...DEFAULT_SETTINGS.clickMode.cursor } },
      textMode: { ...DEFAULT_SETTINGS.textMode },
      scroll: { ...DEFAULT_SETTINGS.scroll }
    };
  }
}

/**
 * @param {Partial<KeyPilotSettings>} partial
 * @returns {Promise<KeyPilotSettings>}
 */
async function setSettings(partial) {
  const current = await getSettings();
  const p = partial && typeof partial === 'object' ? partial : {};

  // Shallow merge for top-level, plus deep merge for known nested settings.
  const pPositions = p.panelPositions && typeof p.panelPositions === 'object' ? p.panelPositions : null;

  /** @type {KeyPilotSettings} */
  const next = {
    ...current,
    ...p,
    controlStrip: {
      ...current.controlStrip,
      ...(p.controlStrip && typeof p.controlStrip === 'object' ? p.controlStrip : {})
    },
    panelPositions: {
      keyboardReference: {
        ...current.panelPositions.keyboardReference,
        ...(pPositions?.keyboardReference && typeof pPositions.keyboardReference === 'object'
          ? pPositions.keyboardReference
          : {})
      },
      controlStrip: {
        ...current.panelPositions.controlStrip,
        ...(pPositions?.controlStrip && typeof pPositions.controlStrip === 'object'
          ? pPositions.controlStrip
          : {})
      }
    },
    clickMode: {
      ...current.clickMode,
      ...(p.clickMode && typeof p.clickMode === 'object' ? p.clickMode : {}),
      cursor: {
        ...current.clickMode.cursor,
        ...(p.clickMode && typeof p.clickMode === 'object' && p.clickMode.cursor && typeof p.clickMode.cursor === 'object'
          ? p.clickMode.cursor
          : {})
      }
    },
    textMode: {
      ...current.textMode,
      ...(p.textMode && typeof p.textMode === 'object' ? p.textMode : {})
    },
    scroll: {
      ...current.scroll,
      ...(p.scroll && typeof p.scroll === 'object' ? p.scroll : {})
    }
  };
  next.searchEngine = normalizeSearchEngine(next.searchEngine);
  next.cursorMode = normalizeCursorMode(next.cursorMode);
  next.keyboardLayoutId = normalizeKeyboardLayoutId(next.keyboardLayoutId);
  next.keyboardReferenceKeyFeedback = normalizeBoolean(
    next.keyboardReferenceKeyFeedback,
    DEFAULT_SETTINGS.keyboardReferenceKeyFeedback
  );
  next.controlStrip = normalizeControlStrip(next.controlStrip);
  next.panelPositions = normalizePanelPositions(next.panelPositions);
  next.clickMode = normalizeClickMode(next.clickMode);
  next.textMode = normalizeTextMode(next.textMode);
  next.scroll = normalizeScroll(next.scroll);
  await storageSetValue(SETTINGS_STORAGE_KEY, next);
  return next;
}

/**
 * @param {SearchEngine} engine
 * @param {string} query
 */
function buildSearchUrl(engine, query) {
  const meta = getSearchEngineMeta(engine);
  const q = typeof query === 'string' ? query : '';
  return `${meta.searchUrlPrefix}${encodeURIComponent(q)}`;
}

/**
 * @param {SearchEngine} engine
 */
function getEngineHomeUrl(engine) {
  return getSearchEngineMeta(engine).homeUrl;
}





  // Module: src/modules/frame-click-agent.js
/**
 * Thin cross-origin iframe click + hover agent.
 *
 * Runs only in non-top frames. Stays light (no full KeyPilot):
 *  1. postMessage / runtime KP_FRAME_ACTIVATE from the parent (top-frame F/B/G)
 *  2. Activate keybinds when this frame has focus
 *  3. Blue hover outline on clickable targets under the pointer (rAF-throttled;
 *     matches top-frame DOM-hover focus palette)
 *  4. postMessage / runtime KP_FRAME_SCROLL from parent (C/V under this iframe)
 *     plus local C/V when this frame has focus — nested overflow first
 *
 * Full KeyPilot still initializes only in the top frame. When full KP is also
 * running in this frame (KeyPilot popover), local key + hover handling is skipped.
 */






/**
 * @typedef {{ openInNewTab?: boolean, background?: boolean }} FrameActivateOptions
 */

const CLICKABLE_SEL =
  'a[href], button, [role="button"], [role="link"], [role="menuitem"], [role="option"], [role="tab"], [role="checkbox"], [role="radio"], [role="switch"], summary, [onclick], input, select, textarea, label';

/**
 * Shadow-DOM–aware elementFromPoint (no iframe piercing — that is recursive via postMessage).
 * @param {number} x
 * @param {number} y
 * @returns {Element|null}
 */
function deepElementFromPoint(x, y) {
  try {
    let el = document.elementFromPoint(x, y);
    let guard = 0;
    while (el && el.shadowRoot && guard++ < 10) {
      const nested = el.shadowRoot.elementFromPoint(x, y);
      if (!nested || nested === el) break;
      el = nested;
    }
    return el || null;
  } catch {
    return null;
  }
}

/**
 * Read computed styles with KeyPilot custom-cursor override suspended so
 * cursor:pointer on the page is still visible when CUSTOM_CURSORS is on.
 * @template T
 * @param {() => T} fn
 * @returns {T}
 */
function withNativePageCursors(fn) {
  let html = null;
  try { html = document.documentElement; } catch { /* ignore */ }
  if (!html || !html.classList) return fn();
  const hadHidden = html.classList.contains(CSS_CLASSES.CURSOR_HIDDEN);
  if (hadHidden) {
    try { html.classList.remove(CSS_CLASSES.CURSOR_HIDDEN); } catch { /* ignore */ }
  }
  try {
    return fn();
  } finally {
    if (hadHidden) {
      try { html.classList.add(CSS_CLASSES.CURSOR_HIDDEN); } catch { /* ignore */ }
    }
  }
}

/**
 * @param {Element|null} el
 * @returns {Element|null}
 */
function resolveClickable(el) {
  if (!el || el.nodeType !== 1) return null;
  try {
    if (el.tagName === 'IFRAME') return el;
    if (el.id === 'kpv2-frame-hover' || el.closest?.('#kpv2-frame-hover')) return null;
    const specific = typeof el.closest === 'function' ? el.closest(CLICKABLE_SEL) : null;
    if (specific) return specific;
    // cursor:pointer on this node only (not inherited from body).
    // Suspend custom-cursor override — otherwise getComputedStyle always reports
    // the KeyPilot crosshair and pointer-only targets never outline.
    try {
      if (el !== document.body && el !== document.documentElement) {
        return withNativePageCursors(() => {
          const cs = window.getComputedStyle(el);
          if (cs.cursor === 'pointer' && cs.pointerEvents !== 'none') {
            const parent = el.parentElement;
            if (!parent || window.getComputedStyle(parent).cursor !== 'pointer') {
              return el;
            }
          }
          return null;
        });
      }
    } catch { /* ignore */ }
    return null;
  } catch {
    return null;
  }
}

/**
 * Coordinate-carrying click sequence (mirrors ActivationHandler.dispatchClickSequence).
 * @param {EventTarget} target
 * @param {number} clientX
 * @param {number} clientY
 */
function dispatchClickSequence(target, clientX, clientY) {
  if (!target) return;

  const common = {
    bubbles: true,
    cancelable: true,
    composed: true,
    view: window,
    clientX,
    clientY,
    button: 0,
    buttons: 1
  };

  const hasPointer = typeof window.PointerEvent === 'function';
  if (hasPointer) {
    const pCommon = { ...common, pointerId: 1, pointerType: 'mouse', isPrimary: true };
    try { target.dispatchEvent(new PointerEvent('pointerover', pCommon)); } catch { /* ignore */ }
    try { target.dispatchEvent(new PointerEvent('pointerenter', pCommon)); } catch { /* ignore */ }
    try { target.dispatchEvent(new PointerEvent('pointerdown', pCommon)); } catch { /* ignore */ }
  } else {
    try { target.dispatchEvent(new MouseEvent('pointerover', common)); } catch { /* ignore */ }
    try { target.dispatchEvent(new MouseEvent('pointerenter', common)); } catch { /* ignore */ }
    try { target.dispatchEvent(new MouseEvent('pointerdown', common)); } catch { /* ignore */ }
  }

  try { target.dispatchEvent(new MouseEvent('mouseover', common)); } catch { /* ignore */ }
  try { target.dispatchEvent(new MouseEvent('mouseenter', common)); } catch { /* ignore */ }
  try { target.dispatchEvent(new MouseEvent('mousemove', common)); } catch { /* ignore */ }
  try { target.dispatchEvent(new MouseEvent('mousedown', common)); } catch { /* ignore */ }

  const commonUp = { ...common, buttons: 0 };
  if (hasPointer) {
    const pUp = { ...commonUp, pointerId: 1, pointerType: 'mouse', isPrimary: true };
    try { target.dispatchEvent(new PointerEvent('pointerup', pUp)); } catch { /* ignore */ }
  } else {
    try { target.dispatchEvent(new MouseEvent('pointerup', commonUp)); } catch { /* ignore */ }
  }
  try { target.dispatchEvent(new MouseEvent('mouseup', commonUp)); } catch { /* ignore */ }
  try { target.dispatchEvent(new MouseEvent('click', commonUp)); } catch { /* ignore */ }
}

/**
 * @param {Element|null} el
 * @returns {HTMLAnchorElement|null}
 */
function closestLink(el) {
  try {
    if (!el || el.nodeType !== 1) return null;
    if (el.tagName === 'A' && /** @type {HTMLAnchorElement} */ (el).href) {
      return /** @type {HTMLAnchorElement} */ (el);
    }
    const a = typeof el.closest === 'function' ? el.closest('a[href]') : null;
    return a && a.tagName === 'A' ? /** @type {HTMLAnchorElement} */ (a) : null;
  } catch {
    return null;
  }
}

/**
 * @param {string} url
 * @param {{ background?: boolean }} opts
 * @returns {boolean}
 */
function openUrlViaRuntime(url, opts = {}) {
  if (!url) return false;
  try {
    if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) return false;
    const type = opts.background ? MSG.OPEN_URL_BACKGROUND : MSG.OPEN_URL_FOREGROUND;
    chrome.runtime.sendMessage({ type, url }).catch(() => { /* ignore */ });
    return true;
  } catch {
    return false;
  }
}

/**
 * True when full KeyPilot is running in this frame (e.g. KP popover iframe).
 * @returns {boolean}
 */
function hasFullKeyPilot() {
  try {
    return !!(window.keyPilot || window.__KeyPilotInstance || window.__KeyPilotToggleHandler);
  } catch {
    return false;
  }
}

/**
 * Install the frame click agent in a child frame.
 * @returns {{ dispose: () => void }|null}
 */
function installFrameClickAgent() {
  try {
    if (window === window.top) return null;

    /** @type {boolean} */
    let enabled = true;
    /** @type {{ x: number|null, y: number|null }} */
    let lastMouse = { x: null, y: null };
    /** @type {ReturnType<typeof buildKeybindingsForLayout>} */
    let keybindings = buildKeybindingsForLayout(DEFAULT_KEYBOARD_LAYOUT_ID);
    /** @type {number} */
    let halfPagePx = SCROLL.HALF_PAGE_PX;
    /** @type {'smooth'|'auto'} */
    let scrollBehavior = SCROLL.BEHAVIOR === 'smooth' ? 'smooth' : 'auto';

    /** @type {HTMLElement|null} */
    let hoverEl = null;
    /** @type {Element|null} */
    let hoverTarget = null;
    /** @type {number} */
    let hoverRaf = 0;
    /** @type {boolean} */
    let pointerInside = false;
    /** @type {{ focusColor: string, overlayFillEnabled: boolean, overlayShadowEnabled: boolean, rectangleThickness: number }} */
    let focusChrome = {
      focusColor: 'blue',
      overlayFillEnabled: false,
      overlayShadowEnabled: false,
      rectangleThickness: 3
    };

    const paletteFor = (color) => {
      if (color === 'green') {
        return {
          border: COLORS.FOCUS_GREEN || 'rgba(0,180,0,0.95)',
          shadow: COLORS.GREEN_SHADOW || 'rgba(0,180,0,0.45)',
          shadowBright: COLORS.GREEN_SHADOW_BRIGHT || 'rgba(0,180,0,0.5)',
          fill: COLORS.FOCUS_GREEN_BG_T2 || 'rgba(46, 204, 113, 0.4)'
        };
      }
      return {
        border: COLORS.FOCUS_BLUE || 'rgba(33,150,243,0.95)',
        shadow: COLORS.BLUE_SHADOW || 'rgba(33,150,243,0.35)',
        shadowBright: COLORS.BLUE_SHADOW_BRIGHT || 'rgba(33,150,243,0.45)',
        fill: COLORS.FOCUS_BLUE_BG_T2 || 'rgba(33,150,243,0.25)'
      };
    };

    const applyFocusChromeToHoverEl = () => {
      if (!hoverEl) return;
      const p = paletteFor(focusChrome.focusColor);
      const thickness = Math.min(Math.max(Number(focusChrome.rectangleThickness) || 3, 1), 16);
      try {
        hoverEl.style.border = `${thickness}px solid ${p.border}`;
        hoverEl.style.background =
          focusChrome.overlayFillEnabled === false ? 'transparent' : p.fill;
        hoverEl.style.boxShadow = focusChrome.overlayShadowEnabled === false
          ? 'none'
          : `0 0 0 1px ${p.shadow}, 0 0 8px ${p.shadowBright}`;
      } catch { /* ignore */ }
    };

    const keyIn = (assignment, key) => {
      try {
        const keys = assignment?.keys;
        return Array.isArray(keys) && keys.includes(key);
      } catch {
        return false;
      }
    };

    const refreshKeybindings = async () => {
      try {
        const settings = await getSettings();
        const layoutId = normalizeKeyboardLayoutId(settings?.keyboardLayoutId);
        keybindings = buildKeybindingsForLayout(layoutId);
        const cm = settings?.clickMode || {};
        focusChrome = {
          focusColor: cm.focusColor === 'green' ? 'green' : 'blue',
          overlayFillEnabled: cm.overlayFillEnabled === true,
          overlayShadowEnabled: cm.overlayShadowEnabled === true,
          rectangleThickness: Number(cm.rectangleThickness) || 3
        };
        const half = Number(settings?.scroll?.halfPagePx);
        if (Number.isFinite(half) && half > 0) halfPagePx = half;
        else halfPagePx = SCROLL.HALF_PAGE_PX;
        try {
          scrollBehavior = scrollBehaviorFromSpeed(
            settings?.scroll?.speed ?? DEFAULT_SETTINGS.scroll.speed
          );
        } catch {
          scrollBehavior = SCROLL.BEHAVIOR === 'smooth' ? 'smooth' : 'auto';
        }
        applyFocusChromeToHoverEl();
        if (pointerInside && enabled) scheduleHoverUpdate();
      } catch {
        // keep previous / default
      }
    };

    /**
     * C / V scroll under the pointer (or given coords): nested overflow first.
     * @param {number} clientX
     * @param {number} clientY
     * @param {number} sign  -1 up/left, +1 down/right
     * @param {number} [deltaPx]
     * @param {ScrollBehavior} [behavior]
     * @returns {boolean}
     */
    const scrollAt = (clientX, clientY, sign, deltaPx, behavior) => {
      if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return false;
      const amount = Math.abs(Number(deltaPx));
      const delta = Number.isFinite(amount) && amount > 0 ? amount : halfPagePx;
      const s = sign < 0 ? -1 : 1;
      const beh = behavior === 'auto' || behavior === 'instant' ? 'auto' : (behavior || scrollBehavior);

      // Nested iframe under point: re-forward into child agent.
      try {
        const under = deepElementFromPoint(clientX, clientY);
        if (under && under.tagName === 'IFRAME') {
          const iframe = /** @type {HTMLIFrameElement} */ (under);
          const rect = iframe.getBoundingClientRect();
          const localX = clientX - rect.left;
          const localY = clientY - rect.top;
          if (
            localX >= 0 && localY >= 0 &&
            localX <= rect.width && localY <= rect.height &&
            iframe.contentWindow
          ) {
            iframe.contentWindow.postMessage({
              type: MSG.FRAME_SCROLL,
              clientX: localX,
              clientY: localY,
              sign: s,
              deltaPx: delta,
              behavior: beh,
              frameName: typeof iframe.name === 'string' ? iframe.name : ''
            }, '*');
            return true;
          }
        }
      } catch { /* fall through */ }

      const result = scrollAtPoint(clientX, clientY, s, delta, beh);
      return !!result?.scrolled;
    };

    const setEnabled = (next) => {
      enabled = !!next;
      if (!enabled) hideHover();
    };

    const syncEnabledFromRuntime = async () => {
      try {
        if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) return;
        const response = await chrome.runtime.sendMessage({ type: MSG.GET_STATE });
        if (response && typeof response.enabled === 'boolean') {
          setEnabled(response.enabled);
        }
      } catch {
        // Default enabled on communication failure (matches toggle handler).
        setEnabled(true);
      }
    };

    const ensureHoverEl = () => {
      if (hoverEl && hoverEl.isConnected) return hoverEl;
      try {
        const el = document.createElement('div');
        el.id = 'kpv2-frame-hover';
        el.setAttribute('aria-hidden', 'true');
        el.style.cssText = [
          'position:fixed',
          'left:0',
          'top:0',
          'width:0',
          'height:0',
          'margin:0',
          'padding:0',
          'box-sizing:border-box',
          'pointer-events:none',
          `z-index:${typeof Z_INDEX?.OVERLAYS === 'number' ? Z_INDEX.OVERLAYS : 2147483020}`,
          'border-radius:2px',
          'display:none',
          'opacity:1'
        ].join(';');
        (document.documentElement || document.body)?.appendChild(el);
        hoverEl = el;
        applyFocusChromeToHoverEl();
        return el;
      } catch {
        return null;
      }
    };

    const hideHover = () => {
      hoverTarget = null;
      if (hoverRaf) {
        try { cancelAnimationFrame(hoverRaf); } catch { /* ignore */ }
        hoverRaf = 0;
      }
      if (hoverEl) {
        try {
          hoverEl.style.display = 'none';
          hoverEl.style.width = '0px';
          hoverEl.style.height = '0px';
        } catch { /* ignore */ }
      }
    };

    /**
     * @param {Element|null} target
     */
    const paintHover = (target) => {
      if (!target || !(target instanceof Element)) {
        hideHover();
        return;
      }
      // Don't outline nested iframes (child agent / shell only).
      if (target.tagName === 'IFRAME') {
        hideHover();
        return;
      }
      let rect;
      try {
        rect = target.getBoundingClientRect();
      } catch {
        hideHover();
        return;
      }
      if (!rect || rect.width <= 0 || rect.height <= 0) {
        hideHover();
        return;
      }
      // Skip absurd full-viewport fills (often body/html mistaken for clickable).
      try {
        if (rect.width >= window.innerWidth * 0.95 && rect.height >= window.innerHeight * 0.95) {
          hideHover();
          return;
        }
      } catch { /* ignore */ }

      const el = ensureHoverEl();
      if (!el) return;
      hoverTarget = target;
      try {
        el.style.display = 'block';
        el.style.transform = `translate(${Math.round(rect.left)}px, ${Math.round(rect.top)}px)`;
        el.style.width = `${Math.round(rect.width)}px`;
        el.style.height = `${Math.round(rect.height)}px`;
      } catch { /* ignore */ }
    };

    const scheduleHoverUpdate = () => {
      if (hoverRaf) return;
      hoverRaf = requestAnimationFrame(() => {
        hoverRaf = 0;
        try {
          if (!enabled || !pointerInside || hasFullKeyPilot()) {
            hideHover();
            return;
          }
          const x = lastMouse.x;
          const y = lastMouse.y;
          if (typeof x !== 'number' || typeof y !== 'number') {
            hideHover();
            return;
          }
          const under = deepElementFromPoint(x, y);
          const clickable = resolveClickable(under);
          if (clickable === hoverTarget && hoverEl && hoverEl.style.display === 'block') {
            // Same target — refresh rect in case of scroll/layout shift.
            paintHover(clickable);
            return;
          }
          paintHover(clickable);
        } catch {
          hideHover();
        }
      });
    };

    /** Prevent double-activate when parent uses both postMessage and SW fan-out. */
    let lastActivateAt = 0;

    /**
     * @param {number} clientX
     * @param {number} clientY
     * @param {FrameActivateOptions} [opts]
     * @returns {boolean}
     */
    const activateAt = (clientX, clientY, opts = {}) => {
      if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return false;

      const now = Date.now();
      if (now - lastActivateAt < 100) return false;
      lastActivateAt = now;

      const el = deepElementFromPoint(clientX, clientY);
      if (!el) return false;

      // Nested iframe: re-forward with coordinates local to the nested frame.
      if (el.tagName === 'IFRAME') {
        try {
          const iframe = /** @type {HTMLIFrameElement} */ (el);
          const rect = iframe.getBoundingClientRect();
          const localX = clientX - rect.left;
          const localY = clientY - rect.top;
          if (
            localX >= 0 && localY >= 0 &&
            localX <= rect.width && localY <= rect.height &&
            iframe.contentWindow
          ) {
            iframe.contentWindow.postMessage({
              type: MSG.FRAME_ACTIVATE,
              clientX: localX,
              clientY: localY,
              openInNewTab: !!opts.openInNewTab,
              background: !!opts.background
            }, '*');
            return true;
          }
        } catch {
          // Fall through to click the iframe element itself.
        }
      }

      const openInNewTab = !!opts.openInNewTab;
      const background = !!opts.background;
      const link = closestLink(el);

      if (link && (openInNewTab || background)) {
        const url = link.href;
        if (openUrlViaRuntime(url, { background })) return true;
        try {
          if (background) {
            window.open(url, '_blank', 'noopener,noreferrer');
          } else {
            const originalTarget = link.target;
            link.target = '_blank';
            try { link.click(); } catch { window.open(url, '_blank', 'noopener,noreferrer'); }
            if (originalTarget !== undefined && originalTarget !== null && originalTarget !== '') {
              link.target = originalTarget;
            } else {
              link.removeAttribute('target');
            }
          }
          return true;
        } catch {
          return false;
        }
      }

      const activator = resolveClickable(el) || el;

      // Same-window link: programmatic click preserves site handlers better than location assign.
      if (activator.tagName === 'A' && /** @type {HTMLAnchorElement} */ (activator).href && !openInNewTab && !background) {
        try {
          /** @type {HTMLAnchorElement} */ (activator).click();
          return true;
        } catch { /* fall through to event sequence */ }
      }

      // <summary> toggles <details> only via activation behavior (HTMLElement.click() /
      // trusted click). Synthetic events alone do not open/close the accordion.
      try {
        let summary = null;
        if (activator && activator.tagName === 'SUMMARY') summary = activator;
        else if (el && typeof el.closest === 'function') {
          const s = el.closest('summary');
          if (s && s.tagName === 'SUMMARY') summary = s;
        } else if (activator && activator.tagName === 'DETAILS') {
          summary = activator.querySelector(':scope > summary');
        }
        if (summary && typeof summary.click === 'function') {
          summary.click();
          return true;
        }
      } catch { /* fall through */ }

      dispatchClickSequence(el, clientX, clientY);
      try {
        if (
          activator &&
          activator !== el &&
          !(typeof activator.contains === 'function' && activator.contains(el))
        ) {
          dispatchClickSequence(activator, clientX, clientY);
        }
      } catch { /* ignore */ }

      return true;
    };

    /**
     * Shared gate for parent → child frame messages (activate + scroll).
     * postMessage source checks are unreliable across content-script isolated
     * worlds (`event.source === window.parent` often fails). Accept only when
     * framed and payload is well-formed; optional frameName targets a specific
     * iframe (e.g. Google name="account").
     * @param {MessageEvent|null} event
     * @param {any} data
     * @param {string} type
     * @returns {boolean}
     */
    const acceptFramePayload = (event, data, type) => {
      if (!data || data.type !== type) return false;
      if (!enabled) return false;
      // Must be embedded (not top-level).
      try {
        if (window === window.top) return false;
      } catch {
        // Access to top can throw in rare sandboxes — treat as framed.
      }
      // Reject self-posted messages when we can tell.
      try {
        if (event && event.source === window) return false;
      } catch { /* ignore */ }
      // Optional name targeting (parent includes iframe.name when set).
      try {
        const want = typeof data.frameName === 'string' ? data.frameName : '';
        if (want && window.name && want !== window.name) return false;
      } catch { /* ignore */ }
      return Number.isFinite(Number(data.clientX)) && Number.isFinite(Number(data.clientY));
    };

    /**
     * @param {MessageEvent|null} event
     * @param {any} data
     * @returns {boolean}
     */
    const acceptActivatePayload = (event, data) =>
      acceptFramePayload(event, data, MSG.FRAME_ACTIVATE);

    /**
     * @param {MessageEvent|null} event
     * @param {any} data
     * @returns {boolean}
     */
    const acceptScrollPayload = (event, data) =>
      acceptFramePayload(event, data, MSG.FRAME_SCROLL);

    /** @param {MessageEvent} event */
    const onMessage = (event) => {
      try {
        const data = event?.data;
        if (acceptActivatePayload(event, data)) {
          const x = Number(data.clientX);
          const y = Number(data.clientY);
          activateAt(x, y, {
            openInNewTab: !!data.openInNewTab,
            background: !!data.background
          });
          return;
        }
        if (acceptScrollPayload(event, data)) {
          const x = Number(data.clientX);
          const y = Number(data.clientY);
          const sign = Number(data.sign) < 0 ? -1 : 1;
          const delta = Number(data.deltaPx);
          const beh = data.behavior === 'auto' || data.behavior === 'instant'
            ? 'auto'
            : (data.behavior || scrollBehavior);
          scrollAt(x, y, sign, delta, beh);
        }
      } catch {
        // ignore
      }
    };

    /** @param {MouseEvent|PointerEvent} e */
    const onPointer = (e) => {
      try {
        if (typeof e.clientX === 'number') lastMouse.x = e.clientX;
        if (typeof e.clientY === 'number') lastMouse.y = e.clientY;
        pointerInside = true;
        if (enabled && !hasFullKeyPilot()) scheduleHoverUpdate();
      } catch {
        // ignore
      }
    };

    const onPointerLeave = () => {
      pointerInside = false;
      hideHover();
    };

    const onScroll = () => {
      if (pointerInside && enabled) scheduleHoverUpdate();
    };

    /** @param {KeyboardEvent} e */
    const onKeyDown = (e) => {
      try {
        if (!enabled) return;
        // Full KeyPilot in this frame owns activate / scroll keys.
        if (hasFullKeyPilot()) return;
        if (hasModifierKeys(e)) return;
        if (isTypingContext(e.target)) return;

        const key = e.key;
        const kb = keybindings || {};
        let mode = null;
        /** @type {number|null} */
        let scrollSign = null;
        if (keyIn(kb.ACTIVATE, key)) mode = 'activate';
        else if (keyIn(kb.ACTIVATE_NEW_TAB, key)) mode = 'newTab';
        else if (keyIn(kb.ACTIVATE_NEW_TAB_BACKGROUND, key)) mode = 'background';
        else if (keyIn(kb.PAGE_UP_INSTANT, key)) scrollSign = -1;
        else if (keyIn(kb.PAGE_DOWN_INSTANT, key)) scrollSign = 1;
        else return;

        let x = lastMouse.x;
        let y = lastMouse.y;
        if (typeof x !== 'number' || typeof y !== 'number') {
          x = Math.floor(window.innerWidth / 2);
          y = Math.floor(window.innerHeight / 2);
        }

        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();

        if (scrollSign !== null) {
          scrollAt(x, y, scrollSign, halfPagePx, scrollBehavior);
          return;
        }

        activateAt(x, y, {
          openInNewTab: mode === 'newTab',
          background: mode === 'background'
        });
      } catch {
        // ignore
      }
    };

    /**
     * @param {any} message
     * @param {chrome.runtime.MessageSender} _sender
     * @param {(response?: any) => void} sendResponse
     */
    const onRuntimeMessage = (message, _sender, sendResponse) => {
      try {
        if (message?.type === MSG.TOGGLE_STATE || message?.type === MSG.UPDATE_STATE) {
          if (typeof message.enabled === 'boolean') {
            setEnabled(message.enabled);
          }
          return false;
        }

        // Backup path: SW can fan-out FRAME_ACTIVATE / FRAME_SCROLL to subframes
        // (postMessage is primary).
        if (message?.type === MSG.FRAME_ACTIVATE) {
          if (!acceptActivatePayload(null, message)) {
            try { sendResponse({ ok: false }); } catch { /* ignore */ }
            return true;
          }
          const ok = activateAt(Number(message.clientX), Number(message.clientY), {
            openInNewTab: !!message.openInNewTab,
            background: !!message.background
          });
          try { sendResponse({ ok: !!ok, href: String(location.href || '').slice(0, 120) }); } catch { /* ignore */ }
          return true;
        }

        if (message?.type === MSG.FRAME_SCROLL) {
          if (!acceptScrollPayload(null, message)) {
            try { sendResponse({ ok: false }); } catch { /* ignore */ }
            return true;
          }
          const sign = Number(message.sign) < 0 ? -1 : 1;
          const ok = scrollAt(
            Number(message.clientX),
            Number(message.clientY),
            sign,
            Number(message.deltaPx),
            message.behavior
          );
          try { sendResponse({ ok: !!ok, href: String(location.href || '').slice(0, 120) }); } catch { /* ignore */ }
          return true;
        }
      } catch {
        // ignore
      }
      return false;
    };

    /** @param {Record<string, chrome.storage.StorageChange>} changes @param {string} area */
    const onStorageChanged = (changes, area) => {
      try {
        if (area !== 'sync' && area !== 'local') return;
        if (changes?.keypilot_enabled && typeof changes.keypilot_enabled.newValue === 'boolean') {
          setEnabled(changes.keypilot_enabled.newValue);
        }
        if (changes && Object.prototype.hasOwnProperty.call(changes, SETTINGS_STORAGE_KEY)) {
          void refreshKeybindings();
        }
      } catch {
        // ignore
      }
    };

    window.addEventListener('message', onMessage, true);
    document.addEventListener('mousemove', onPointer, { capture: true, passive: true });
    document.addEventListener('pointermove', onPointer, { capture: true, passive: true });
    document.addEventListener('mouseleave', onPointerLeave, true);
    document.addEventListener('scroll', onScroll, { capture: true, passive: true });
    window.addEventListener('scroll', onScroll, { capture: true, passive: true });
    document.addEventListener('keydown', onKeyDown, true);

    try {
      chrome.runtime?.onMessage?.addListener(onRuntimeMessage);
    } catch { /* ignore */ }
    try {
      chrome.storage?.onChanged?.addListener(onStorageChanged);
    } catch { /* ignore */ }

    // Visible to page-world diagnostics (isolated world cannot expose JS globals to the page).
    try {
      document.documentElement?.setAttribute('data-kp-frame-agent', '1');
    } catch { /* ignore */ }

    void syncEnabledFromRuntime();
    void refreshKeybindings();

    return {
      dispose() {
        hideHover();
        try {
          if (hoverEl) hoverEl.remove();
        } catch { /* ignore */ }
        hoverEl = null;
        try {
          window.removeEventListener('message', onMessage, true);
          document.removeEventListener('mousemove', onPointer, true);
          document.removeEventListener('pointermove', onPointer, true);
          document.removeEventListener('mouseleave', onPointerLeave, true);
          document.removeEventListener('scroll', onScroll, true);
          window.removeEventListener('scroll', onScroll, true);
          document.removeEventListener('keydown', onKeyDown, true);
        } catch { /* ignore */ }
        try {
          chrome.runtime?.onMessage?.removeListener(onRuntimeMessage);
        } catch { /* ignore */ }
        try {
          chrome.storage?.onChanged?.removeListener(onStorageChanged);
        } catch { /* ignore */ }
        try {
          document.documentElement?.removeAttribute('data-kp-frame-agent');
        } catch { /* ignore */ }
      }
    };
  } catch (error) {
    console.warn('[KeyPilot] Failed to install frame click agent:', error);
    return null;
  }
}



  // Module: src/modules/omnibox-manager.js
/**
 * Omnibox-like centered address bar overlay.
 * - Opens via KeyPilot (Alt+L)
 * - Suggests from History + Bookmarks via service worker
 * - Navigates current tab; non-URL becomes selected Search Engine query
 */



class OmniboxManager {
  /**
   * @param {object} opts
   * @param {() => void} [opts.onClose]
   */
  constructor({ onClose } = {}) {
    this.onClose = typeof onClose === 'function' ? onClose : null;

    this._open = false;
    this._backdrop = null;
    this._panel = null;
    this._input = null;
    this._list = null;

    /** @type {Array<{title: string, url: string, source: string}>} */
    this._suggestions = [];
    this._selectedIndex = -1;
    // Track whether the user has explicitly moved into the list via Arrow keys.
    // We use this to avoid "Enter always commits first row" unless that's intended.
    this._userNavigatedList = false;
    this._debounceTimer = null;
    this._lastQuery = '';

    /**
     * Prefer keyboard arrow selection over a stationary cursor sitting on a row.
     * Hover only updates selection after the user actually moves the mouse
     * (or after keyboard nav, until the next real mouse move).
     * This also prevents re-renders from re-firing mouseenter under the cursor
     * and undoing arrow-key selection.
     */
    this._mouseNavEnabled = false;
    /** @type {number|null} */
    this._lastMouseX = null;
    /** @type {number|null} */
    this._lastMouseY = null;

    /** @type {import('./settings-manager.js').SearchEngine} */
    this._searchEngine = 'brave';
    this._settingsListenerInstalled = false;

    this._onBackdropClick = this._onBackdropClick.bind(this);
    this._onInput = this._onInput.bind(this);
    this._onKeyDown = this._onKeyDown.bind(this);
    this._onListMouseMove = this._onListMouseMove.bind(this);

    // Load settings once and keep local cache updated.
    this._installSettingsListener();
    this._refreshSettings();
  }

  isOpen() {
    return this._open;
  }

  show(initialValue = '') {
    if (this._open) {
      if (this._input) {
        this._input.focus();
        this._input.select();
      }
      return;
    }

    this._open = true;
    this._ensureDom();
    this._userNavigatedList = false;
    // Stationary cursor over the suggestion list must not steal selection.
    this._mouseNavEnabled = false;
    this._lastMouseX = null;
    this._lastMouseY = null;

    // Set value and focus.
    if (this._input) {
      this._input.value = typeof initialValue === 'string' ? initialValue : '';
      this._input.focus();
      try { this._input.select(); } catch { /* ignore */ }
    }

    // Populate initial suggestions (e.g. recent history) even when empty.
    this._scheduleSuggest();
  }

  hide() {
    if (!this._open) return;
    this._open = false;

    this._clearDebounce();
    this._suggestions = [];
    this._selectedIndex = -1;
    this._userNavigatedList = false;
    this._mouseNavEnabled = false;
    this._lastMouseX = null;
    this._lastMouseY = null;
    this._lastQuery = '';

    try {
      this._backdrop?.removeEventListener('click', this._onBackdropClick, true);
      this._input?.removeEventListener('input', this._onInput, true);
      this._input?.removeEventListener('keydown', this._onKeyDown, true);
      this._list?.removeEventListener('mousemove', this._onListMouseMove, true);
    } catch {
      // ignore
    }

    try { this._backdrop?.remove(); } catch { /* ignore */ }

    this._backdrop = null;
    this._panel = null;
    this._input = null;
    this._list = null;

    if (this.onClose) {
      try { this.onClose(); } catch { /* ignore */ }
    }
  }

  _ensureDom() {
    if (this._backdrop && this._input) return;

    const doc = document;

    const backdrop = doc.createElement('div');
    backdrop.className = CSS_CLASSES.OMNIBOX_BACKDROP;
    Object.assign(backdrop.style, {
      position: 'fixed',
      inset: '0',
      zIndex: String(Z_INDEX.OMNIBOX),
      background: 'rgba(0,0,0,0.35)',
      backdropFilter: 'blur(6px)',
      WebkitBackdropFilter: 'blur(6px)'
    });
    backdrop.addEventListener('click', this._onBackdropClick, true);

    const panel = doc.createElement('div');
    panel.className = CSS_CLASSES.OMNIBOX_PANEL;
    Object.assign(panel.style, {
      position: 'absolute',
      top: '18vh',
      left: '50%',
      transform: 'translateX(-50%)',
      width: 'min(880px, calc(100vw - 32px))',
      borderRadius: '14px',
      border: '1px solid rgba(255,140,0,0.35)',
      boxShadow: '0 18px 60px rgba(0,0,0,0.55)',
      background: 'rgba(20, 20, 20, 0.88)',
      overflow: 'hidden'
    });

    // Stop clicks inside panel from closing.
    panel.addEventListener('click', (e) => {
      e.stopPropagation();
    }, true);

    const input = doc.createElement('input');
    input.className = CSS_CLASSES.OMNIBOX_INPUT;
    input.setAttribute('type', 'text');
    input.setAttribute('spellcheck', 'false');
    input.setAttribute('autocomplete', 'off');
    input.setAttribute('autocapitalize', 'off');
    input.setAttribute('autocorrect', 'off');
    input.setAttribute('inputmode', 'search');
    input.setAttribute('placeholder', 'Search or enter address');
    Object.assign(input.style, {
      width: '100%',
      boxSizing: 'border-box',
      border: 'none',
      outline: 'none',
      padding: '14px 16px',
      fontSize: '18px',
      fontWeight: '500',
      color: 'rgba(255,255,255,0.95)',
      background: 'rgba(0,0,0,0)',
      fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif'
    });
    input.addEventListener('input', this._onInput, true);
    input.addEventListener('keydown', this._onKeyDown, true);

    const list = createUrlListingContainer({
      doc,
      view: 'list',
      className: CSS_CLASSES.OMNIBOX_SUGGESTIONS,
      maxHeight: '40vh',
      useInlineStyles: true,
      style: {
        borderTop: '1px solid rgba(255,140,0,0.15)'
      }
    });
    // Track real pointer movement so hover selection can resume after keyboard nav
    // (or after open with the cursor already over a row).
    list.addEventListener('mousemove', this._onListMouseMove, { passive: true, capture: true });

    panel.appendChild(input);
    panel.appendChild(list);
    backdrop.appendChild(panel);
    doc.body.appendChild(backdrop);

    this._backdrop = backdrop;
    this._panel = panel;
    this._input = input;
    this._list = list;

    this._renderSuggestions();
  }

  _onBackdropClick(e) {
    // Click outside panel closes.
    e.preventDefault();
    e.stopPropagation();
    this.hide();
  }

  _onInput() {
    this._scheduleSuggest();
  }

  _onKeyDown(e) {
    if (!this._open) return;

    // Prevent the page from reacting to navigation keys while omnibox is open.
    const key = e.key;

    if (key === 'Escape') {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      this.hide();
      return;
    }

    if (key === 'ArrowDown' || key === 'ArrowUp') {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      this._userNavigatedList = true;
      // Keyboard wins until the pointer actually moves again.
      this._mouseNavEnabled = false;
      this._moveSelection(key === 'ArrowDown' ? 1 : -1);
      return;
    }

    if (key === 'Enter') {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      this._commit();
      return;
    }
  }

  _moveSelection(delta) {
    const count = this._suggestions.length;
    if (!count) {
      this._selectedIndex = -1;
      this._renderSuggestions();
      return;
    }

    // Special behavior to make omnibox feel like a real address bar:
    // - When the "input row" is active (_selectedIndex === -1), ArrowDown moves into the list at 0.
    // - When the first row is selected, ArrowUp returns to input (clears selection).
    let next = this._selectedIndex;
    if (delta < 0 && next === 0) {
      next = -1;
    } else if (next === -1) {
      next = delta > 0 ? 0 : -1;
    } else {
      next = (next + delta + count) % count;
    }

    this._selectedIndex = next;
    this._renderSuggestions();
    this._scrollSelectedIntoView();
  }

  /**
   * Enable mouse-driven selection only after a real pointer move.
   * A cursor that is merely resting on a row (common when the omnibox opens)
   * must not override keyboard arrow selection.
   * @param {MouseEvent} e
   */
  _onListMouseMove(e) {
    if (!this._open) return;

    const x = e.clientX;
    const y = e.clientY;

    if (this._lastMouseX === null || this._lastMouseY === null) {
      // Seed position on first move event; still treat this as intentional movement
      // so a user who wiggles the mouse can hover immediately.
      this._lastMouseX = x;
      this._lastMouseY = y;
      this._mouseNavEnabled = true;
      this._selectRowUnderPoint(x, y);
      return;
    }

    if (x === this._lastMouseX && y === this._lastMouseY) return;

    this._lastMouseX = x;
    this._lastMouseY = y;
    this._mouseNavEnabled = true;
    this._selectRowUnderPoint(x, y);
  }

  /**
   * Select the suggestion row currently under the pointer (if any).
   * Used after enabling mouse nav so hover works even when mouseenter does not
   * re-fire (e.g. cursor stayed inside the same row across a re-render).
   * @param {number} clientX
   * @param {number} clientY
   */
  _selectRowUnderPoint(clientX, clientY) {
    if (!this._list || !this._suggestions.length) return;

    try {
      const el = document.elementFromPoint(clientX, clientY);
      const row = el?.closest?.('[data-kp-omnibox-index]');
      if (!row || !this._list.contains(row)) return;

      const idx = Number.parseInt(row.dataset.kpOmniboxIndex || '', 10);
      if (!Number.isFinite(idx) || idx < 0 || idx >= this._suggestions.length) return;
      if (idx === this._selectedIndex) return;

      this._selectedIndex = idx;
      this._userNavigatedList = true;
      this._renderSuggestions();
    } catch {
      // ignore
    }
  }

  _scrollSelectedIntoView() {
    try {
      const el = this._list?.querySelector?.(`[data-kp-omnibox-index="${this._selectedIndex}"]`);
      if (el && typeof el.scrollIntoView === 'function') {
        el.scrollIntoView({ block: 'nearest' });
      }
    } catch {
      // ignore
    }
  }

  _commit() {
    const raw = (this._input?.value || '').trim();
    const selected = this._selectedIndex >= 0 ? this._suggestions[this._selectedIndex] : null;
    // If the user hasn't explicitly navigated the list, treat Enter as committing the input,
    // UNLESS the selected row is our "closest domain" convenience row (source === 'domain').
    const allowSelected =
      this._selectedIndex >= 0 &&
      (this._userNavigatedList || selected?.source === 'domain');
    const target = allowSelected && selected?.url ? selected.url : raw;
    if (!target) {
      this.hide();
      return;
    }

    const url = this._toUrlOrSearch(target);
    this.hide();

    try {
      // Navigate current tab (top frame).
      window.location.assign(url);
    } catch {
      try { window.location.href = url; } catch { /* ignore */ }
    }
  }

  _installSettingsListener() {
    if (this._settingsListenerInstalled) return;
    this._settingsListenerInstalled = true;
    try {
      chrome.storage.onChanged.addListener((changes, area) => {
        if (area !== 'sync') return;
        if (!changes || !changes[SETTINGS_STORAGE_KEY]) return;
        this._refreshSettings();
      });
    } catch {
      // ignore
    }
  }

  async _refreshSettings() {
    try {
      const settings = await getSettings();
      this._searchEngine = normalizeSearchEngine(settings?.searchEngine);
    } catch {
      this._searchEngine = 'brave';
    }
  }

  _toUrlOrSearch(input) {
    const text = String(input || '').trim();
    if (!text) return getEngineHomeUrl(this._searchEngine);

    // Already a URL with a scheme.
    if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(text)) return text;

    // If it looks like a filename (e.g. "library.js"), treat it as search text
    // rather than a host. This avoids turning common file queries into bogus URLs.
    if (isLikelyFilenameQuery(text)) {
      return buildSearchUrl(this._searchEngine, text);
    }

    // If input contains spaces, it's likely a search query even if it contains domain-like text
    // This handles cases like "alice in wonderland archive.org" - treat as search, not URL
    if (/\s/.test(text)) {
      return buildSearchUrl(this._searchEngine, text);
    }

    // Looks like a host/path: domain.tld, localhost, or IP (+ optional port/path)
    const looksLikeHost =
      /(^localhost\b)/i.test(text) ||
      /(^\d{1,3}(\.\d{1,3}){3}\b)/.test(text) ||
      /([a-zA-Z0-9-]+\.[a-zA-Z]{2,})([\/:?#]|$)/.test(text);

    if (looksLikeHost) {
      return `https://${text}`;
    }

    return buildSearchUrl(this._searchEngine, text);
  }

  _clearDebounce() {
    if (this._debounceTimer) {
      clearTimeout(this._debounceTimer);
      this._debounceTimer = null;
    }
  }

  _scheduleSuggest() {
    this._clearDebounce();
    this._debounceTimer = setTimeout(() => {
      this._debounceTimer = null;
      this._fetchSuggestions();
    }, 90);
  }

  async _fetchSuggestions() {
    if (!this._open) return;

    const query = (this._input?.value || '').trim();
    this._lastQuery = query;

    /** @type {Array<{title: string, url: string, source: string}>} */
    let suggestions = [];

    try {
      if (typeof chrome !== 'undefined' && chrome.runtime && typeof chrome.runtime.sendMessage === 'function') {
        const resp = await chrome.runtime.sendMessage({
          type: 'KP_OMNIBOX_SUGGEST',
          query,
          maxResults: 12
        });

        if (resp && resp.type === 'KP_OMNIBOX_SUGGESTIONS' && Array.isArray(resp.suggestions)) {
          suggestions = resp.suggestions
            .filter((s) => s && typeof s.url === 'string' && s.url.trim())
            .slice(0, 12);
        }
      }
    } catch {
      // ignore
    }

    // Stale guard (if user typed more while we waited).
    if (!this._open) return;
    const current = (this._input?.value || '').trim();
    if (current !== this._lastQuery) return;

    this._suggestions = suggestions;
    // Default selection: nothing selected. User must press ArrowDown (or hover/click)
    // to move into the list.
    this._selectedIndex = -1;
    this._userNavigatedList = false;
    this._renderSuggestions();
  }

  _renderSuggestions() {
    if (!this._list) return;

    renderUrlListing({
      container: this._list,
      items: this._suggestions,
      view: 'list',
      useInlineStyles: true,
      emptyText: 'No suggestions',
      getTitle: (s) => s.title || s.url,
      getUrl: (s) => s.url,
      showFavicon: true,
      showMetaLine: false,
      showUrlLine: true,
      selectedIndex: this._selectedIndex,
      decorateRow: ({ row, item, idx, parts }) => {
        row.className = CSS_CLASSES.OMNIBOX_SUGGESTION;
        row.dataset.kpOmniboxIndex = String(idx);

        // Override the shared defaults to match the omnibox styling.
        const selected = idx === this._selectedIndex;
        Object.assign(row.style, {
          padding: '10px 16px',
          cursor: 'default',
          background: selected ? 'rgba(255,140,0,0.18)' : 'rgba(0,0,0,0)',
          borderLeft: selected ? '3px solid rgba(255,140,0,0.85)' : '3px solid transparent',
          borderTop: 'none',
          borderRight: 'none',
          borderBottom: 'none',
          borderRadius: '0',
          margin: '0',
          border: 'none'
        });

        Object.assign(parts.titleEl.style, {
          fontSize: '14px',
          fontWeight: '600'
        });
        Object.assign(parts.urlEl.style, {
          fontSize: '12px'
        });

        // Slight spacing tuning now that we show favicons.
        if (parts.faviconEl) {
          Object.assign(parts.faviconEl.style, {
            width: '18px',
            height: '18px',
            borderRadius: '4px'
          });
        }
      },
      onRowMouseEnter: ({ idx }) => {
        // Ignore hover until the user has actually moved the mouse.
        // Prevents a stationary cursor (or re-render mouseenter) from fighting
        // arrow-key selection.
        if (!this._mouseNavEnabled) return;
        if (idx === this._selectedIndex) return;
        this._selectedIndex = idx;
        this._userNavigatedList = true;
        this._renderSuggestions();
      },
      onRowMouseDown: ({ idx, event }) => {
        // Use mousedown to commit before input loses focus.
        event.preventDefault();
        event.stopPropagation();
        this._selectedIndex = idx;
        this._userNavigatedList = true;
        this._mouseNavEnabled = true;
        this._commit();
      }
    });
  }
}

/**
 * Heuristic: user typed something that is far more likely a filename than a hostname.
 * We only apply this when there is no scheme (handled earlier).
 *
 * Examples we want to treat as search:
 * - library.js
 * - README.md
 * - config.json
 *
 * Examples we still want to treat as URLs:
 * - example.com
 * - foo.io/path
 * - localhost:3000
 */
function isLikelyFilenameQuery(text) {
  const s = String(text || '').trim();
  if (!s) return false;
  if (/\s/.test(s)) return false; // filenames can, but this is almost always a search anyway
  if (/[\/?#:]/.test(s)) return false; // paths/ports/query/hash are much more URL-like
  if (!/^[^.\s@]+\.[a-z0-9]+$/i.test(s)) return false; // one dot, simple "name.ext"

  const ext = s.split('.').pop()?.toLowerCase() || '';
  // Keep this list intentionally small to avoid false negatives for real TLDs.
  // These extensions are extremely common in "search for a file" queries.
  const filenameExts = new Set([
    'js',
    'mjs',
    'cjs',
    'css',
    'html',
    'htm',
    'json',
    'xml',
    'txt',
    'md'
  ]);

  return filenameExts.has(ext);
}





  // Module: src/modules/tab-history-popover.js
/**
 * TabHistoryPopover
 * - Renders a branching, per-tab navigation history (tracked by service worker).
 * - Full-width panel with two stacked horizontal card rails (Tab + Browser).
 * - Uses PopupManager to keep z-index below click overlays and to enable View Transitions.
 */

function renderThreeLineUrlListingEntry({ item, parts }) {
  const url = String(item?.url || '').trim();
  const title = String(item?.title || '').trim();
  const { domain, path } = parseUrlForThreeLineDisplay(url);

  // Order requirement:
  // 1) domain
  // 2) page title
  // 3) path
  parts.titleEl.textContent = domain || url || '';
  parts.metaEl.textContent = title;
  parts.urlEl.textContent = path;
}

/** Shared classNames for horizontal history card rails. */
const HISTORY_CARD_CLASS_NAMES = {
  row: 'kp-url-row',
  rowSelected: 'kp-url-row--selected',
  content: 'kp-url-content',
  text: 'kp-url-text',
  title: 'kp-url-domain',
  meta: 'kp-url-title',
  url: 'kp-url-path',
  favicon: 'kp-url-favicon'
};

class TabHistoryPopover {
  /**
   * @param {object} opts
   * @param {import('./popup-manager.js').PopupManager} opts.popupManager
   * @param {(open: boolean) => void} [opts.onStateChange] - Called when popover opens/closes
   */
  constructor({ popupManager, onStateChange } = {}) {
    this.popupManager = popupManager || null;
    this._onStateChange = typeof onStateChange === 'function' ? onStateChange : null;
    this._popupId = 'kpv2-tab-history-popover';

    /** @type {HTMLElement|null} */
    this._panel = null;
    /** @type {HTMLElement|null} */
    this._tabList = null;
    /** @type {HTMLElement|null} */
    this._tabStatus = null;
    /** @type {HTMLElement|null} */
    this._browserList = null;
    /** @type {HTMLElement|null} */
    this._browserStatus = null;

    this._open = false;
    this._lastGraph = null;
  }

  isOpen() {
    return this._open;
  }

  toggle() {
    if (this._open) this.hide();
    else this.show();
  }

  show() {
    if (!this.popupManager) return;
    if (this._open) return;
    this._open = true;

    this._ensureDom();
    this._injectStyles();
    // Full-width rails: disable free-form resize so layout stays intentional.
    this.popupManager.showModal({
      id: this._popupId,
      panel: this._panel,
      onRequestClose: () => this.hide(),
      resizable: false
    });

    // Notify that popover opened
    this._onStateChange?.(true);

    this._loadAndRender();
  }

  hide() {
    if (!this.popupManager) return;
    if (!this._open) return;
    this._open = false;

    // Notify that popover closed
    this._onStateChange?.(false);

    try {
      this.popupManager.hideModal(this._popupId);
    } catch {
      // ignore
    }
  }

  _injectStyles() {
    const styleId = 'kpv2-tab-history-styles';
    // Always refresh styles so layout redesigns take effect after reloads without a stale cache.
    const existing = document.getElementById(styleId);
    if (existing) existing.remove();

    const style = document.createElement('style');
    style.id = styleId;
    style.textContent = `
      .kpv2-tab-history-panel {
        --kp-history-side-margin: 24px;
        --kp-history-card-w: 140px;
        box-sizing: border-box;
        position: fixed;
        left: 50%;
        top: 12vh;
        transform: translateX(-50%);
        width: calc(100vw - (2 * var(--kp-history-side-margin)));
        max-width: none;
        max-height: 80vh;
        display: flex;
        flex-direction: column;
        border-radius: 14px;
        border: 1px solid rgba(255,140,0,0.25);
        background: rgba(18, 18, 18, 0.92);
        box-shadow: 0 18px 60px rgba(0,0,0,0.55);
        overflow: hidden;
        outline: none;
        font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
      }

      .kpv2-tab-history-panel .kpv2-history-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 12px 16px;
        border-bottom: 1px solid rgba(255,140,0,0.14);
        background: linear-gradient(180deg, rgba(28,28,28,0.95) 0%, rgba(16,16,16,0.95) 100%);
        flex: 0 0 auto;
      }

      .kpv2-tab-history-panel .kpv2-history-title-wrap {
        display: flex;
        align-items: baseline;
        gap: 10px;
        min-width: 0;
      }

      .kpv2-tab-history-panel .kpv2-history-title {
        font-size: 14px;
        font-weight: 700;
        color: rgba(255,255,255,0.92);
      }

      .kpv2-tab-history-panel .kpv2-history-subtitle {
        font-size: 12px;
        font-weight: 600;
        color: rgba(255,140,0,0.85);
        white-space: nowrap;
      }

      .kpv2-tab-history-panel .kpv2-history-close {
        margin: 0;
        appearance: none;
        -webkit-appearance: none;
        box-sizing: border-box;
        width: 34px;
        height: 34px;
        border-radius: 10px;
        border: 1px solid rgba(255,255,255,0.14);
        background: rgba(0,0,0,0.25);
        color: rgba(255,255,255,0.9);
        font-size: 22px;
        line-height: 1;
        cursor: pointer;
        flex: 0 0 auto;
        box-shadow: none;
        text-shadow: none;
        padding: 0;
      }

      .kpv2-tab-history-panel .kpv2-history-body {
        display: flex;
        flex-direction: column;
        gap: 12px;
        padding: 12px;
        flex: 1 1 auto;
        min-height: 0;
        overflow: auto;
      }

      .kpv2-tab-history-panel .kpv2-history-section {
        display: flex;
        flex-direction: column;
        min-width: 0;
        border: 1px solid rgba(255,255,255,0.08);
        border-radius: 12px;
        overflow: hidden;
        background: rgba(0,0,0,0.18);
        flex: 0 0 auto;
      }

      .kpv2-tab-history-panel .kpv2-history-section-header {
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        gap: 10px;
        padding: 10px 12px;
        border-bottom: 1px solid rgba(255,255,255,0.08);
        background: rgba(0,0,0,0.18);
      }

      .kpv2-tab-history-panel .kpv2-history-section-title {
        font-size: 13px;
        font-weight: 800;
        color: rgba(255,255,255,0.9);
      }

      .kpv2-tab-history-panel .kpv2-history-section-hint {
        font-size: 11px;
        font-weight: 600;
        color: rgba(255,255,255,0.55);
        white-space: nowrap;
      }

      .kpv2-tab-history-panel .kpv2-history-section-status {
        padding: 6px 12px;
        border-bottom: 1px solid rgba(255,255,255,0.06);
        font-size: 12px;
        color: rgba(255,255,255,0.65);
      }

      /* Horizontal card rail */
      .kpv2-tab-history-panel .kpv2-history-rail {
        display: flex !important;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: stretch;
        gap: 10px;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 10px 10px 12px;
        scroll-snap-type: x proximity;
        scrollbar-width: thin;
        scrollbar-color: rgba(255,255,255,0.2) rgba(0,0,0,0.1);
        min-height: calc(var(--kp-history-card-w) * 4 / 3 + 22px);
      }

      /* Vertical history cards */
      .kpv2-tab-history-panel .kp-url-row {
        box-sizing: border-box;
        display: block;
        position: relative;
        flex: 0 0 auto;
        width: var(--kp-history-card-w);
        aspect-ratio: 3 / 4;
        height: auto;
        padding: 12px 10px;
        border-radius: 12px;
        cursor: pointer;
        text-decoration: none;
        color: inherit;
        user-select: none;
        scroll-snap-align: start;
        background: linear-gradient(180deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.05) 100%);
        border: 1px solid rgba(255,255,255,0.12);
        box-shadow: 0 4px 12px rgba(0,0,0,0.25), 0 2px 4px rgba(0,0,0,0.15);
        margin: 0;
        min-width: 0;
        overflow: hidden;
        transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease, background 0.12s ease;
      }

      .kpv2-tab-history-panel .kp-url-row:hover {
        background: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(255,255,255,0.08) 100%);
        box-shadow: 0 8px 18px rgba(0,0,0,0.38), 0 3px 6px rgba(0,0,0,0.20);
        transform: translateY(-2px);
      }

      .kpv2-tab-history-panel .kp-url-row:focus-visible {
        outline: none;
        box-shadow: 0 4px 12px rgba(0,0,0,0.25), 0 2px 4px rgba(0,0,0,0.15), 0 0 0 3px rgba(255,140,0,0.22);
        border-color: rgba(255,140,0,0.55);
      }

      .kpv2-tab-history-panel .kp-url-row--selected {
        background: linear-gradient(180deg, rgba(255,140,0,0.22) 0%, rgba(255,140,0,0.10) 100%);
        border-color: rgba(255,140,0,0.55);
        box-shadow: 0 4px 14px rgba(255,140,0,0.18), 0 2px 4px rgba(0,0,0,0.15);
      }

      .kpv2-tab-history-panel .kp-url-row--selected:hover {
        background: linear-gradient(180deg, rgba(255,140,0,0.28) 0%, rgba(255,140,0,0.12) 100%);
      }

      .kpv2-tab-history-panel .kp-url-content {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
        min-width: 0;
        height: 100%;
      }

      .kpv2-tab-history-panel .kp-url-favicon {
        width: 32px;
        height: 32px;
        border-radius: 8px;
        background: rgba(255,255,255,0.08);
        flex: 0 0 auto;
      }

      .kpv2-tab-history-panel .kp-url-text {
        min-width: 0;
        width: 100%;
        flex: 1 1 auto;
        display: flex;
        flex-direction: column;
        gap: 3px;
        overflow: hidden;
      }

      .kpv2-tab-history-panel .kp-url-domain {
        font-size: 11px;
        font-weight: 800;
        letter-spacing: 0.15px;
        color: rgba(255, 200, 130, 0.92);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 100%;
      }

      .kpv2-tab-history-panel .kp-url-title {
        font-size: 12px;
        font-weight: 650;
        color: rgba(255,255,255,0.92);
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        line-clamp: 3;
        white-space: normal;
        word-break: break-word;
        max-width: 100%;
        line-height: 1.25;
      }

      .kpv2-tab-history-panel .kp-url-path {
        font-size: 10px;
        color: rgba(255,255,255,0.55);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 100%;
        margin-top: auto;
      }

      .kpv2-tab-history-panel .kp-url-branch-badge {
        position: absolute;
        top: 8px;
        right: 8px;
        font-size: 10px;
        font-weight: 700;
        padding: 2px 7px;
        border-radius: 999px;
        border: 1px solid rgba(255,140,0,0.35);
        background: rgba(255,140,0,0.16);
        color: rgba(255,140,0,0.95);
        line-height: 1.2;
        pointer-events: none;
      }

      /* Scrollbars (vertical body + horizontal rails) */
      .kpv2-tab-history-panel ::-webkit-scrollbar {
        width: 8px;
        height: 8px;
      }
      .kpv2-tab-history-panel ::-webkit-scrollbar-track {
        background: rgba(0, 0, 0, 0.1);
      }
      .kpv2-tab-history-panel ::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.2);
        border-radius: 4px;
      }
      .kpv2-tab-history-panel ::-webkit-scrollbar-thumb:hover {
        background: rgba(255, 255, 255, 0.3);
      }
    `;
    document.head.appendChild(style);
  }

  _ensureDom() {
    if (this._panel && this._tabList && this._tabStatus && this._browserList && this._browserStatus) return;

    const doc = document;
    const panel = doc.createElement('div');
    panel.className = 'kpv2-tab-history-panel';
    panel.setAttribute('role', 'dialog');
    panel.setAttribute('aria-label', 'History');

    const header = doc.createElement('div');
    header.className = 'kpv2-history-header';

    const titleWrap = doc.createElement('div');
    titleWrap.className = 'kpv2-history-title-wrap';

    const title = doc.createElement('div');
    title.className = 'kpv2-history-title';
    title.textContent = 'History';

    const subtitle = doc.createElement('div');
    subtitle.className = 'kpv2-history-subtitle';
    subtitle.textContent = 'Browser history · Tab history';

    titleWrap.appendChild(title);
    titleWrap.appendChild(subtitle);

    const closeBtn = doc.createElement('button');
    closeBtn.type = 'button';
    closeBtn.className = 'kpv2-history-close';
    closeBtn.textContent = '×';
    closeBtn.setAttribute('aria-label', 'Close');
    closeBtn.addEventListener('click', (e) => {
      e.preventDefault();
      e.stopPropagation();
      this.hide();
    }, true);

    header.appendChild(titleWrap);
    header.appendChild(closeBtn);

    const body = doc.createElement('div');
    body.className = 'kpv2-history-body';

    const makeSection = ({ titleText }) => {
      const section = doc.createElement('section');
      section.className = 'kpv2-history-section';

      const sectionHeader = doc.createElement('div');
      sectionHeader.className = 'kpv2-history-section-header';

      const sectionTitle = doc.createElement('div');
      sectionTitle.className = 'kpv2-history-section-title';
      sectionTitle.textContent = titleText;

      const sectionHint = doc.createElement('div');
      sectionHint.className = 'kpv2-history-section-hint';
      sectionHint.textContent = 'Click to navigate';

      sectionHeader.appendChild(sectionTitle);
      sectionHeader.appendChild(sectionHint);

      const sectionStatus = doc.createElement('div');
      sectionStatus.className = 'kpv2-history-section-status';
      sectionStatus.textContent = 'Loading…';

      const rail = createUrlListingContainer({
        doc,
        view: 'list',
        useInlineStyles: false,
        scrollY: false,
        className: 'kpv2-history-rail',
        style: {
          // Rail layout is owned by CSS; keep a minimal fallback if styles lag.
          display: 'flex',
          flexDirection: 'row',
          overflowX: 'auto',
          overflowY: 'hidden'
        }
      });
      rail.setAttribute('role', 'list');

      section.appendChild(sectionHeader);
      section.appendChild(sectionStatus);
      section.appendChild(rail);
      return { section, sectionStatus, rail };
    };

    const tabSection = makeSection({ titleText: 'Tab history' });
    const browserSection = makeSection({ titleText: 'Browser history' });

    panel.appendChild(header);
    // Browser history above Tab history
    body.appendChild(browserSection.section);
    body.appendChild(tabSection.section);
    panel.appendChild(body);

    this._panel = panel;
    this._tabStatus = tabSection.sectionStatus;
    this._tabList = tabSection.rail;
    this._browserStatus = browserSection.sectionStatus;
    this._browserList = browserSection.rail;
  }

  async _loadAndRender() {
    if (!this._open) return;
    if (!this._tabStatus || !this._tabList || !this._browserStatus || !this._browserList) return;

    this._tabStatus.textContent = 'Loading tab history…';
    this._tabList.textContent = '';
    this._browserStatus.textContent = 'Loading browser history…';
    this._browserList.textContent = '';

    await Promise.allSettled([
      this._loadAndRenderTabHistory(),
      this._loadAndRenderBrowserHistory()
    ]);
  }

  async _loadAndRenderTabHistory() {
    if (!this._open) return;
    if (!this._tabStatus || !this._tabList) return;

    let resp = null;
    try {
      resp = await chrome.runtime.sendMessage({ type: 'KP_NAVGRAPH_GET' });
    } catch {
      resp = null;
    }

    if (!this._open) return;

    if (!resp || resp.type !== 'KP_NAVGRAPH_GRAPH' || !resp.graph) {
      this._tabStatus.textContent = 'Tab history unavailable.';
      return;
    }

    this._lastGraph = resp.graph;
    this._renderGraph(resp.graph);
  }

  async _loadAndRenderBrowserHistory() {
    if (!this._open) return;
    if (!this._browserStatus || !this._browserList) return;

    let resp = null;
    try {
      // Recent history; background uses chrome.history (not available in content scripts).
      resp = await chrome.runtime.sendMessage({
        type: 'KP_BROWSER_HISTORY_GET',
        query: '',
        maxResults: 40,
        days: 14
      });
    } catch {
      resp = null;
    }

    if (!this._open) return;

    const items = Array.isArray(resp?.items) ? resp.items : [];
    if (!resp || resp.type !== 'KP_BROWSER_HISTORY_RESULT') {
      this._browserStatus.textContent = 'Browser history unavailable.';
      return;
    }

    if (!items.length) {
      this._browserStatus.textContent = 'No recent browser history.';
      this._browserList.textContent = '';
      return;
    }

    this._browserStatus.textContent = 'Click an entry to navigate. Press Esc to close.';
    this._renderBrowserHistory(items);
  }

  _renderGraph(graph) {
    if (!this._tabStatus || !this._tabList) return;

    const nodes = Array.isArray(graph.nodes) ? graph.nodes : [];
    const edges = Array.isArray(graph.edges) ? graph.edges : [];
    const cursorId = graph.cursorId;

    if (!nodes.length) {
      this._tabStatus.textContent = 'No tab history yet.';
      return;
    }

    this._tabStatus.textContent = 'Click an entry to navigate. Press Esc to close.';

    const nodeById = new Map();
    for (const n of nodes) {
      if (n && typeof n.id === 'number') nodeById.set(n.id, n);
    }

    const structuralKinds = new Set(['navigate', 'programmatic', 'reload']);
    const children = new Map();
    const incoming = new Map();

    for (const e of edges) {
      if (!e || !structuralKinds.has(e.kind)) continue;
      const fromId = e.fromId;
      const toId = e.toId;
      if (typeof fromId !== 'number' || typeof toId !== 'number') continue;
      if (!nodeById.has(fromId) || !nodeById.has(toId)) continue;
      if (!children.has(fromId)) children.set(fromId, []);
      children.get(fromId).push(toId);
      incoming.set(toId, (incoming.get(toId) || 0) + 1);
    }

    // Roots are nodes with no incoming structural edge.
    const roots = [];
    for (const n of nodes) {
      if (!n || typeof n.id !== 'number') continue;
      if (!incoming.get(n.id)) roots.push(n.id);
    }

    const getLastSeen = (id) => {
      const n = nodeById.get(id);
      return Number(n?.tsLastSeen) || Number(n?.tsCreated) || 0;
    };

    // Score roots by most recent activity in their subtree.
    const subtreeScoreCache = new Map();
    const computeSubtreeScore = (id, seen = new Set()) => {
      if (subtreeScoreCache.has(id)) return subtreeScoreCache.get(id);
      if (seen.has(id)) return getLastSeen(id);
      seen.add(id);
      let best = getLastSeen(id);
      const kids = children.get(id) || [];
      for (const k of kids) {
        best = Math.max(best, computeSubtreeScore(k, seen));
      }
      subtreeScoreCache.set(id, best);
      return best;
    };

    roots.sort((a, b) => computeSubtreeScore(b) - computeSubtreeScore(a));

    const rows = [];
    const visited = new Set();
    const walk = (id, depth) => {
      if (visited.has(id)) return;
      visited.add(id);
      rows.push({ id, depth: Math.min(depth, 12) });
      const kids = (children.get(id) || []).slice();
      kids.sort((a, b) => getLastSeen(b) - getLastSeen(a));
      for (const k of kids) walk(k, depth + 1);
    };

    for (const r of roots) walk(r, 0);

    // If graph has nodes not reachable via structural edges (rare), append them by recency.
    if (rows.length < nodeById.size) {
      const remaining = [];
      for (const id of nodeById.keys()) {
        if (!visited.has(id)) remaining.push(id);
      }
      remaining.sort((a, b) => getLastSeen(b) - getLastSeen(a));
      for (const id of remaining) rows.push({ id, depth: 0 });
    }

    /** @type {Array<{id: number, depth: number, node: any, branchCount: number}>} */
    const renderItems = [];
    for (const r of rows) {
      const n = nodeById.get(r.id);
      if (!n) continue;
      renderItems.push({
        id: r.id,
        depth: r.depth,
        node: n,
        branchCount: (children.get(r.id) || []).length
      });
    }

    const doRender = () => {
      renderUrlListing({
        container: this._tabList,
        items: renderItems,
        view: 'list',
        useInlineStyles: false,
        classNames: HISTORY_CARD_CLASS_NAMES,
        rowTag: 'a',
        getTitle: (it) => (it.node?.title || it.node?.url || '').toString(),
        getUrl: (it) => String(it.node?.url || ''),
        showFavicon: true,
        showMetaLine: true,
        showUrlLine: true,
        isSelected: (it) => it.id === cursorId,
        onRowClick: async ({ item, event }) => {
          event.preventDefault();
          event.stopPropagation();
          const url = typeof item?.node?.url === 'string' ? item.node.url : '';
          if (!url) return;
          try {
            await chrome.runtime.sendMessage({ type: 'KP_NAVGRAPH_JUMP', url });
          } catch {
            // ignore
          }
          this.hide();
        },
        decorateRow: ({ row, item, idx, parts }) => {
          row.dataset.kpTabHistoryId = String(item.id);
          row.setAttribute('role', 'listitem');

          // Use three-line layout like newtab
          renderThreeLineUrlListingEntry({ item: { url: item.node?.url, title: item.node?.title }, parts });

          // Branch badge (+N) — corner of card
          if (item.branchCount > 1) {
            const badge = document.createElement('div');
            badge.className = 'kp-url-branch-badge';
            badge.textContent = `+${item.branchCount - 1}`;
            badge.setAttribute('aria-label', `${item.branchCount - 1} more branches`);
            row.appendChild(badge);
          }

          // Keep dataset index for debugging/consistency.
          row.dataset.kpUrlListingIndex = String(idx);
        }
      });

      // Keep rail flex layout after renderUrlListing.
      if (this._tabList) {
        this._tabList.classList.add('kpv2-history-rail');
        // Scroll selected card into view when present.
        const selected = this._tabList.querySelector('.kp-url-row--selected');
        if (selected && typeof selected.scrollIntoView === 'function') {
          try {
            selected.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'smooth' });
          } catch {
            try { selected.scrollIntoView(); } catch { /* ignore */ }
          }
        }
      }
    };

    try {
      if (typeof document !== 'undefined' && typeof document.startViewTransition === 'function') {
        document.startViewTransition(() => doRender());
      } else {
        doRender();
      }
    } catch {
      doRender();
    }
  }

  _renderBrowserHistory(items) {
    if (!this._browserList) return;
    /** @type {Set<string>} */
    const seen = new Set();
    /** @type {Array<{url: string, title: string, lastVisitTime: number}>} */
    const deduped = [];
    for (const it of items) {
      const url = typeof it?.url === 'string' ? it.url.trim() : '';
      if (!url) continue;
      if (seen.has(url)) continue;
      seen.add(url);
      deduped.push({
        url,
        title: (it?.title || url || '').toString(),
        lastVisitTime: Number(it?.lastVisitTime) || 0
      });
    }

    const doRender = () => {
      renderUrlListing({
        container: this._browserList,
        items: deduped,
        view: 'list',
        useInlineStyles: false,
        classNames: HISTORY_CARD_CLASS_NAMES,
        rowTag: 'a',
        getTitle: (it) => it.title,
        getUrl: (it) => it.url,
        showFavicon: true,
        showMetaLine: true,
        showUrlLine: true,
        onRowClick: async ({ item, event }) => {
          event.preventDefault();
          event.stopPropagation();
          try {
            await chrome.runtime.sendMessage({ type: 'KP_NAVGRAPH_JUMP', url: item.url });
          } catch {
            try { window.location.assign(item.url); } catch { /* ignore */ }
          }
          this.hide();
        },
        decorateRow: ({ row, item, parts }) => {
          row.setAttribute('role', 'listitem');
          renderThreeLineUrlListingEntry({ item, parts });
        }
      });

      if (this._browserList) {
        this._browserList.classList.add('kpv2-history-rail');
      }
    };

    try {
      if (typeof document !== 'undefined' && typeof document.startViewTransition === 'function') {
        document.startViewTransition(() => doRender());
      } else {
        doRender();
      }
    } catch {
      doRender();
    }
  }
}



  // Module: src/modules/launcher-popover.js
/**
 * LauncherPopover
 *
 * A popover that provides quick access to websites organized in categories.
 * Features a tab list on the left for categories (social media, news, etc.)
 * and a grid of website items on the right. Supports V/C key scrolling through
 * sheets of grid items.
 */





class LauncherPopover {
  constructor(keypilot) {
    this._keypilot = keypilot;
    this._container = null;
    this._tabListContainer = null;
    this._gridContainer = null;
    this._searchInput = null;
    this._currentCategory = 'favorites';
    this._categorySubTabs = {}; // Store per-category sub-tab selection
    this._currentSheet = 0;
    this._itemsPerSheet = 60; // Increased from 12 to 60 items per page
    this._categories = null;
    this._isOpen = false;
    /** Bumps on every show/hide so in-flight async loads can abort cleanly. */
    this._openGen = 0;
    this._searchQuery = '';
    this._categoryOrder = ['favorites', 'bookmarks', 'history', 'social', 'news', 'productivity', 'videos', 'entertainment', 'shopping', 'ai', 'archive', 'searches'];
    this._showDefaultSites = true; // Checkbox state for showing default sites (only affects favorites tab)
    /** Categories whose domain history has been fetched this open session. */
    this._historyLoaded = Object.create(null);
    /** Shared recent history rows (top sites + search extraction). */
    this._cachedTopSites = null;
    this._boundContainerKeyDown = null;

    // Define available sub-tabs for each category (extensible for future types)
    // Order matters: first in array is the default if history is empty
    this._categorySubTabConfig = {
      favorites: ['favorites', 'history'],
      bookmarks: ['favorites', 'history'],
      history: ['favorites', 'history'],
      social: ['favorites', 'history'],
      news: ['favorites', 'history'],
      productivity: ['favorites', 'history'],
      videos: ['favorites', 'history'],
      entertainment: ['favorites', 'history'],
      shopping: ['favorites', 'history'],
      ai: ['favorites', 'history'],
      archive: ['favorites', 'history'],
      searches: ['favorites', 'history']
    };
    // Preview-related properties
    this._previewError = null;
    this._errorTitle = null;
    this._errorMessage = null;
    this._currentPreviewUrl = null;
    this._previewBridgeTimer = null;
    // Default sites per category
    this._defaultSites = {
      social: [
        { title: 'Instagram', url: 'https://instagram.com', isDefault: true },
        { title: 'Facebook', url: 'https://facebook.com', isDefault: true },
        { title: 'X (Twitter)', url: 'https://x.com', isDefault: true },
        { title: 'Reddit', url: 'https://reddit.com', isDefault: true },
        { title: 'Bluesky', url: 'https://bsky.app', isDefault: true },
        { title: 'LinkedIn', url: 'https://linkedin.com', isDefault: true },
        { title: 'Threads', url: 'https://threads.net', isDefault: true },
        { title: 'Mastodon', url: 'https://mastodon.social', isDefault: true }
      ],
      videos: [
        { title: 'YouTube', url: 'https://youtube.com', isDefault: true },
        { title: 'Rumble', url: 'https://rumble.com', isDefault: true },
        { title: 'Twitch', url: 'https://twitch.tv', isDefault: true },
        { title: 'Vimeo', url: 'https://vimeo.com', isDefault: true },
        { title: 'Dailymotion', url: 'https://dailymotion.com', isDefault: true },
        { title: 'Odysee', url: 'https://odysee.com', isDefault: true }
      ],
      entertainment: [
        { title: 'Netflix', url: 'https://netflix.com', isDefault: true },
        { title: 'Disney+', url: 'https://disneyplus.com', isDefault: true },
        { title: 'Hulu', url: 'https://hulu.com', isDefault: true },
        { title: 'YouTube', url: 'https://youtube.com', isDefault: true },
        { title: 'HBO Max', url: 'https://max.com', isDefault: true },
        { title: 'Prime Video', url: 'https://primevideo.com', isDefault: true },
        { title: 'Paramount+', url: 'https://paramountplus.com', isDefault: true },
        { title: 'Peacock', url: 'https://peacocktv.com', isDefault: true }
      ],
      news: [
        { title: 'CNN', url: 'https://cnn.com', isDefault: true },
        { title: 'BBC News', url: 'https://bbc.com/news', isDefault: true },
        { title: 'NY Times', url: 'https://nytimes.com', isDefault: true },
        { title: 'Reuters', url: 'https://reuters.com', isDefault: true },
        { title: 'The Guardian', url: 'https://theguardian.com', isDefault: true },
        { title: 'AP News', url: 'https://apnews.com', isDefault: true }
      ],
      productivity: [
        { title: 'Gmail', url: 'https://gmail.com', isDefault: true },
        { title: 'Google Calendar', url: 'https://calendar.google.com', isDefault: true },
        { title: 'Google Drive', url: 'https://drive.google.com', isDefault: true },
        { title: 'Google Docs', url: 'https://docs.google.com', isDefault: true },
        { title: 'Notion', url: 'https://notion.so', isDefault: true },
        { title: 'Slack', url: 'https://slack.com', isDefault: true },
        { title: 'Trello', url: 'https://trello.com', isDefault: true }
      ],
      shopping: [
        { title: 'Amazon', url: 'https://amazon.com', isDefault: true },
        { title: 'eBay', url: 'https://ebay.com', isDefault: true },
        { title: 'Walmart', url: 'https://walmart.com', isDefault: true },
        { title: 'Target', url: 'https://target.com', isDefault: true },
        { title: 'Etsy', url: 'https://etsy.com', isDefault: true }
      ],
      archive: [
        { title: 'Internet Archive', url: 'https://archive.org', isDefault: true },
        { title: 'Wayback Machine', url: 'https://web.archive.org', isDefault: true }
      ],
      ai: [
        { title: 'ChatGPT', url: 'https://chat.openai.com', isDefault: true },
        { title: 'Claude', url: 'https://claude.ai', isDefault: true },
        { title: 'Grok', url: 'https://grok.com', isDefault: true },
        { title: 'Gemini', url: 'https://gemini.google.com', isDefault: true },
        { title: 'Copilot', url: 'https://copilot.microsoft.com', isDefault: true },
        { title: 'Perplexity', url: 'https://perplexity.ai', isDefault: true },
        { title: 'Poe', url: 'https://poe.com', isDefault: true },
        { title: 'Character.AI', url: 'https://character.ai', isDefault: true },
        { title: 'Hugging Face', url: 'https://huggingface.co/chat', isDefault: true }
      ],
      // SSOT: search-engines.js (shared with settings default-engine catalog)
      searches: LAUNCHER_SEARCH_SITES.map((s) => ({ ...s }))
    };
  }

  /**
   * Show the launcher popover immediately, then enrich data in the background.
   * Blocking on bookmarks/history before paint made open feel very slow and
   * allowed Escape-during-load to race with a late UI mount.
   */
  async show() {
    if (this._isOpen) return;
    this._isOpen = true;
    const gen = ++this._openGen;
    this._historyLoaded = Object.create(null);
    this._cachedTopSites = null;

    // Paint shell with default sites first (no network / history APIs).
    this._initCategoriesWithDefaults();
    this._buildUI();
    if (!this._stillOpen(gen)) return;

    this._keypilot.overlayManager?.popupManager?.showModal({
      id: 'launcher-popover',
      panel: this._container,
      onRequestClose: () => this.hide()
    });

    this._renderCategory(this._currentCategory);

    // Progressive load: shared data first, then history for the active category.
    await this._loadSharedData(gen);
    if (!this._stillOpen(gen)) return;
    await this._ensureCategoryHistory(this._currentCategory, gen);
  }

  /**
   * Show the launcher popover with search input focused
   */
  async showWithSearchFocus() {
    await this.show();
    const gen = this._openGen;

    // Focus after paint so PopupManager mount settles; abort if closed meanwhile.
    setTimeout(() => {
      if (!this._stillOpen(gen) || !this._searchInput) return;
      try { this._searchInput.focus(); } catch { /* ignore */ }
    }, 0);
  }

  /**
   * Hide the launcher popover
   */
  hide() {
    if (!this._isOpen && !this._container) return;
    this._isOpen = false;
    // Invalidate any in-flight show() / history loads.
    this._openGen++;

    // Clear any pending bridge initialization
    if (this._previewBridgeTimer) {
      clearInterval(this._previewBridgeTimer);
      this._previewBridgeTimer = null;
    }

    // Leave text-focus mode that the search field may have entered.
    try { this._searchInput?.blur?.(); } catch { /* ignore */ }
    try { this._keypilot?.focusDetector?.clearTextFocus?.(); } catch { /* ignore */ }

    if (this._container && this._boundContainerKeyDown) {
      try { this._container.removeEventListener('keydown', this._boundContainerKeyDown, true); } catch { /* ignore */ }
    }
    this._boundContainerKeyDown = null;

    this._keypilot.overlayManager?.popupManager?.hideModal('launcher-popover');

    if (this._container && this._container.parentNode) {
      try { this._container.parentNode.removeChild(this._container); } catch { /* ignore */ }
    }

    this._container = null;
    this._tabListContainer = null;
    this._gridContainer = null;
    this._searchInput = null;
    this._clearBtn = null;
    this._previewArea = null;
    this._previewIframe = null;
    this._currentSheet = 0;
    this._searchQuery = '';
    this._historyLoaded = Object.create(null);
    this._cachedTopSites = null;
  }

  /** @param {number} gen */
  _stillOpen(gen) {
    return this._isOpen && this._openGen === gen;
  }

  /**
   * Check if launcher is open
   */
  isOpen() {
    return this._isOpen;
  }

  /**
   * Extract domains from default site list for a category
   */
  _getDefaultDomains(categoryKey) {
    if (!this._defaultSites[categoryKey]) return [];
    return this._defaultSites[categoryKey]
      .map((site) => extractDomain(site.url))
      .filter((domain) => domain !== '');
  }

  /**
   * Immediate category shell using default sites only (no Chrome history APIs).
   * Domain-history tabs start empty and fill in when first selected.
   */
  _initCategoriesWithDefaults() {
    const defaults = (key) => (this._showDefaultSites && this._defaultSites[key]
      ? [...this._defaultSites[key]]
      : []);

    this._categories = {
      favorites: {
        label: 'Favorites',
        icon: '⭐',
        history: [],
        favorites: []
      },
      bookmarks: {
        label: 'Bookmarks',
        icon: '📑',
        history: [],
        favorites: []
      },
      history: {
        label: 'Recent',
        icon: '🕐',
        history: [],
        favorites: []
      },
      social: {
        label: 'Social Media',
        icon: '💬',
        history: [],
        favorites: defaults('social')
      },
      news: {
        label: 'News',
        icon: '📰',
        history: [],
        favorites: defaults('news')
      },
      productivity: {
        label: 'Productivity',
        icon: '⚡',
        history: [],
        favorites: defaults('productivity')
      },
      videos: {
        label: 'Videos',
        icon: '📹',
        history: [],
        favorites: defaults('videos')
      },
      entertainment: {
        label: 'Entertainment',
        icon: '🎬',
        history: [],
        favorites: defaults('entertainment')
      },
      shopping: {
        label: 'Shopping',
        icon: '🛒',
        history: [],
        favorites: defaults('shopping')
      },
      ai: {
        label: 'AI',
        icon: '🤖',
        history: [],
        favorites: defaults('ai')
      },
      archive: {
        label: 'Internet Archive',
        icon: '📚',
        history: [],
        favorites: defaults('archive')
      },
      searches: {
        label: 'Searches',
        icon: '🔍',
        history: [],
        favorites: defaults('searches')
      }
    };

    this._initDefaultSubTabs();
  }

  /**
   * Pick default sub-tab per category from current item counts.
   * @param {{ force?: boolean, onlyKeys?: string[] }} [opts]
   */
  _initDefaultSubTabs(opts = {}) {
    const force = !!opts.force;
    const onlyKeys = Array.isArray(opts.onlyKeys) ? new Set(opts.onlyKeys) : null;

    for (const categoryKey in this._categories) {
      if (onlyKeys && !onlyKeys.has(categoryKey)) continue;
      if (!force && this._categorySubTabs[categoryKey]) continue;

      const subTabConfig = this._categorySubTabConfig[categoryKey] || ['favorites', 'history'];
      const historyCount = this._categories[categoryKey].history?.length || 0;
      const favoritesCount = this._categories[categoryKey].favorites?.length || 0;

      if (favoritesCount > 0) {
        this._categorySubTabs[categoryKey] = subTabConfig[0];
      } else if (historyCount > 0) {
        this._categorySubTabs[categoryKey] = subTabConfig[1] || subTabConfig[0];
      } else {
        // Prefer history for data-backed tabs while empty so progressive load
        // lands on the list that is about to fill (bookmarks/top sites).
        this._categorySubTabs[categoryKey] = subTabConfig.includes('history')
          ? 'history'
          : subTabConfig[0];
      }
    }
  }

  /**
   * Load bookmarks + top sites once (parallel). Domain history is lazy per category.
   * @param {number} gen
   */
  async _loadSharedData(gen) {
    try {
      const [bookmarks, topSites] = await Promise.all([
        this._getBookmarks(),
        this._getTopSites()
      ]);
      if (!this._stillOpen(gen) || !this._categories) return;

      this._cachedTopSites = topSites;
      const recentSearches = this._extractRecentSearches(topSites);

      this._categories.favorites.history = [
        ...bookmarks.slice(0, 10),
        ...topSites.slice(0, 10)
      ];
      this._categories.bookmarks.history = bookmarks;
      this._categories.history.history = topSites;
      this._categories.searches.history = recentSearches;

      // Shared lists don't need domain history fetches.
      this._historyLoaded.favorites = true;
      this._historyLoaded.bookmarks = true;
      this._historyLoaded.history = true;
      this._historyLoaded.searches = true;

      // Re-pick defaults for shared categories now that history rows exist.
      this._initDefaultSubTabs({
        force: true,
        onlyKeys: ['favorites', 'bookmarks', 'history', 'searches']
      });
      try { this._updateSubTabsUI?.(); } catch { /* ignore */ }
      try { this._updateSubTabStyles?.(); } catch { /* ignore */ }

      if (this._gridContainer) {
        this._renderCategory(this._currentCategory);
      }
    } catch (error) {
      console.error('[LauncherPopover] Error loading shared data:', error);
    }
  }

  /**
   * Fetch domain history for a category the first time it's viewed.
   * @param {string} categoryKey
   * @param {number} [gen]
   */
  async _ensureCategoryHistory(categoryKey, gen = this._openGen) {
    if (!categoryKey || !this._categories?.[categoryKey]) return;
    if (this._historyLoaded[categoryKey]) return;

    // Categories backed by bookmarks/top-sites are filled in _loadSharedData.
    const domainCategories = new Set([
      'social', 'news', 'productivity', 'videos',
      'entertainment', 'shopping', 'ai', 'archive'
    ]);
    if (!domainCategories.has(categoryKey)) {
      this._historyLoaded[categoryKey] = true;
      return;
    }

    // Mark in-flight so rapid tab switches don't double-fetch.
    this._historyLoaded[categoryKey] = 'pending';
    try {
      const domains = this._getDefaultDomains(categoryKey);
      const history = domains.length
        ? await this._getHistoryForDomains(domains)
        : [];

      if (!this._stillOpen(gen) || !this._categories?.[categoryKey]) return;

      this._categories[categoryKey].history = history;
      this._historyLoaded[categoryKey] = true;

      const favoritesCount = this._categories[categoryKey].favorites?.length || 0;
      if (history.length > 0 && favoritesCount === 0) {
        const subTabConfig = this._categorySubTabConfig[categoryKey] || ['favorites', 'history'];
        this._categorySubTabs[categoryKey] = subTabConfig[1] || subTabConfig[0];
        try { this._updateSubTabsUI?.(); } catch { /* ignore */ }
        try { this._updateSubTabStyles?.(); } catch { /* ignore */ }
      }

      if (this._currentCategory === categoryKey && this._gridContainer) {
        this._renderCategory(categoryKey);
      }
    } catch (error) {
      console.error('[LauncherPopover] Error loading category history:', categoryKey, error);
      this._historyLoaded[categoryKey] = true;
    }
  }

  /**
   * Reload favorites lists when "Show default sites" toggles (no full history re-fetch).
   */
  _applyDefaultSitesVisibility() {
    if (!this._categories) return;
    const keys = ['social', 'news', 'productivity', 'videos', 'entertainment', 'shopping', 'ai', 'archive', 'searches'];
    for (const key of keys) {
      if (!this._categories[key]) continue;
      this._categories[key].favorites = this._showDefaultSites && this._defaultSites[key]
        ? [...this._defaultSites[key]]
        : [];
    }
  }

  /**
   * Get bookmarks via message passing to background script
   */
  async _getBookmarks() {
    try {
      const response = await chrome.runtime.sendMessage({
        type: 'KP_GET_BOOKMARKS'
      });

      if (response && response.success && response.bookmarks) {
        return response.bookmarks;
      }
      console.warn('[LauncherPopover] Failed to get bookmarks:', response?.error);
      return [];
    } catch (error) {
      console.warn('[LauncherPopover] Bookmarks message failed:', error);
      return [];
    }
  }

  /**
   * Get top visited sites from history via message passing
   */
  async _getTopSites() {
    try {
      const response = await chrome.runtime.sendMessage({
        type: 'KP_GET_TOP_SITES',
        // Background already aggregates to top 100 domains; 250 raw hits is enough.
        maxResults: 250,
        days: 30
      });

      if (response && response.success && response.topSites) {
        return response.topSites;
      }
      console.warn('[LauncherPopover] Failed to get top sites:', response?.error);
      return [];
    } catch (error) {
      console.error('[LauncherPopover] Error getting top sites:', error);
      return [];
    }
  }

  /**
   * Get history for specific domains via message passing
   */
  async _getHistoryForDomains(domains) {
    try {
      const response = await chrome.runtime.sendMessage({
        type: 'KP_GET_HISTORY_FOR_DOMAINS',
        domains: domains,
        days: 30
      });

      if (response && response.success && response.history) {
        return response.history;
      }
      console.warn('[LauncherPopover] Failed to get history for domains:', response?.error);
      return [];
    } catch (error) {
      console.error('[LauncherPopover] Error getting history for domains:', error);
      return [];
    }
  }

  /**
   * Extract recent search queries from already-fetched top-site rows (no extra API call).
   * @param {Array<{title?: string, url?: string}>} topSites
   */
  _extractRecentSearches(topSites) {
    if (!Array.isArray(topSites)) return [];

    const searches = [];
    const seenQueries = new Set();

    for (const item of topSites) {
      if (!item?.url) continue;
      try {
        const url = new URL(item.url);
        let query = null;

        if (url.hostname.includes('google.com') && url.pathname === '/search') {
          query = url.searchParams.get('q');
        } else if (url.hostname.includes('bing.com') && url.pathname === '/search') {
          query = url.searchParams.get('q');
        } else if (url.hostname.includes('duckduckgo.com')) {
          query = url.searchParams.get('q');
        } else if (url.hostname.includes('yahoo.com') && url.pathname === '/search') {
          query = url.searchParams.get('p');
        }

        if (query && !seenQueries.has(query)) {
          seenQueries.add(query);
          searches.push({
            title: query,
            url: item.url
          });
        }
      } catch {
        // Skip invalid URLs
      }
    }

    return searches.slice(0, 50);
  }

  /**
   * Filter items by matching domains
   */
  _filterByDomains(items, domains) {
    return items.filter((item) => {
      const itemDomain = extractDomain(item.url);
      if (!itemDomain) return false;
      return domains.some((domain) => itemDomain === domain || itemDomain.endsWith('.' + domain));
    });
  }

  /**
   * Filter items by keywords in URL or title
   */
  _filterByKeywords(items, keywords) {
    return items.filter(item => {
      const searchText = `${item.title} ${item.url}`.toLowerCase();
      return keywords.some(keyword => searchText.includes(keyword));
    });
  }

  /**
   * Inject light-DOM CSS that scopes/resets host-page rules for launcher controls.
   *
   * Host sites (e.g. firefox.com) style bare `button` / `[role="button"]` with large
   * border-radius tokens. Because the launcher is mounted in the page document (not a
   * shadow root), those rules paint our subtabs, clear btn, and card preview buttons
   * unless we win the cascade. High-specificity selectors beat typical host `button`
   * rules without !important so intentional inline border-radius values still win.
   *
   * @param {Document} doc
   * @param {HTMLElement} container
   */
  _injectScopedStyles(doc, container) {
    if (!doc || !container) return;
    const style = doc.createElement('style');
    style.setAttribute('data-kp-launcher-scope', 'true');
    style.textContent = `
      /* Reset form/control radii so host page button styles cannot leak in.
         Specificity (html body .kp-launcher-container button) beats bare button /
         [role=button] host rules; element inline styles still override this. */
      html body .kp-launcher-container button,
      html body .kp-launcher-container [role="button"],
      html body .kp-launcher-container input,
      html body .kp-launcher-container select,
      html body .kp-launcher-container textarea {
        border-radius: 0;
      }
    `;
    container.appendChild(style);
  }

  /**
   * Build the launcher UI
   */
  _buildUI() {
    const doc = document;

    // Main container
    this._container = doc.createElement('div');
    this._container.className = 'kp-launcher-container';
    this._container.setAttribute('role', 'dialog');
    this._container.setAttribute('aria-label', 'Launcher');
    // Allow Escape to close even when the search field has focus and page handlers interfere.
    this._container.tabIndex = -1;
    this._container.style.cssText = `
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) translateZ(0);
      width: 80vw;
      max-width: 1400px;
      height: 80vh;
      background: rgb(26, 26, 26);
      border: 2px solid #333;
      border-radius: 12px;
      display: flex;
      overflow: hidden;
      box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
      isolation: isolate;
      contain: layout style paint;
      will-change: transform;
      backface-visibility: hidden;
      -webkit-backface-visibility: hidden;
      -webkit-transform: translate(-50%, -50%) translateZ(0);
    `;

    // Scope/reset host-page control styles before children mount.
    this._injectScopedStyles(doc, this._container);

    this._boundContainerKeyDown = (e) => {
      if (e.key === 'Escape' || e.code === 'Escape') {
        e.preventDefault();
        e.stopPropagation();
        try { e.stopImmediatePropagation(); } catch { /* ignore */ }
        this.hide();
      }
    };
    this._container.addEventListener('keydown', this._boundContainerKeyDown, true);

    // Left sidebar (tab list)
    const sidebar = doc.createElement('div');
    sidebar.className = 'kp-launcher-sidebar';
    sidebar.style.cssText = `
      width: 200px;
      background: #0f0f0f;
      border-right: 1px solid #333;
      display: flex;
      flex-direction: column;
      overflow-y: auto;
    `;

    // Tab list
    this._tabListContainer = doc.createElement('div');
    this._tabListContainer.className = 'kp-launcher-tabs';
    this._tabListContainer.style.cssText = `
      display: flex;
      flex-direction: column;
      padding: 12px 0;
    `;

    // Render tabs
    if (this._categories) {
      Object.keys(this._categories).forEach(categoryKey => {
        const category = this._categories[categoryKey];
        const tab = this._createTab(categoryKey, category);
        this._tabListContainer.appendChild(tab);
      });
    }

    sidebar.appendChild(this._tabListContainer);

    // Checkbox for showing default sites
    const checkboxContainer = doc.createElement('div');
    checkboxContainer.style.cssText = `
      padding: 12px 16px;
      border-top: 1px solid #333;
      margin-top: auto;
    `;

    const checkboxLabel = doc.createElement('label');
    checkboxLabel.style.cssText = `
      display: flex;
      align-items: center;
      gap: 8px;
      color: #888;
      font-size: 12px;
      cursor: pointer;
    `;

    const checkbox = doc.createElement('input');
    checkbox.type = 'checkbox';
    checkbox.checked = this._showDefaultSites;
    checkbox.style.cssText = `
      cursor: pointer;
    `;

    checkbox.addEventListener('change', (e) => {
      this._showDefaultSites = e.target.checked;
      this._applyDefaultSitesVisibility();
      this._renderCategory(this._currentCategory);
    });

    const checkboxText = doc.createElement('span');
    checkboxText.textContent = 'Show default sites';

    checkboxLabel.appendChild(checkbox);
    checkboxLabel.appendChild(checkboxText);
    checkboxContainer.appendChild(checkboxLabel);

    sidebar.appendChild(checkboxContainer);

    // Right content area
    const contentArea = doc.createElement('div');
    contentArea.className = 'kp-launcher-content';
    contentArea.style.cssText = `
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
    `;

    // Header
    const header = doc.createElement('div');
    header.className = 'kp-launcher-header';
    header.style.cssText = `
      padding: 20px 24px;
      border-bottom: 1px solid #333;
      background: #0f0f0f;
    `;

    const title = doc.createElement('h2');
    title.className = 'kp-launcher-title';
    title.textContent = 'Launcher';
    title.style.cssText = `
      margin: 0;
      font-size: 24px;
      font-weight: 600;
      color: #fff;
    `;

    const subtitle = doc.createElement('p');
    subtitle.className = 'kp-launcher-subtitle';
    subtitle.textContent = 'Quick access to your favorite sites';
    subtitle.style.cssText = `
      margin: 4px 0 0 0;
      font-size: 14px;
      color: #888;
    `;

    // Search input container
    const searchContainer = doc.createElement('div');
    searchContainer.className = 'kp-launcher-search-container';
    searchContainer.style.cssText = `
      margin-top: 16px;
      position: relative;
    `;

    // Search input
    this._searchInput = doc.createElement('input');
    this._searchInput.type = 'text';
    this._searchInput.placeholder = 'Search sites...';
    this._searchInput.className = 'kp-launcher-search-input';
    this._searchInput.style.cssText = `
      width: 100%;
      padding: 10px 36px 10px 12px;
      background: #2a2a2a;
      border: 1px solid #444;
      border-radius: 6px;
      color: #fff;
      font-size: 14px;
      outline: none;
      transition: all 0.2s;
    `;

    // Search input event listeners
    this._searchInput.addEventListener('input', (e) => {
      this._searchQuery = e.target.value.toLowerCase().trim();
      this._currentSheet = 0;
      this._renderCategory(this._currentCategory);
    });

    // Escape must close while typing in the search field (typing context often
    // swallows KeyPilot global shortcuts if this is missing).
    this._searchInput.addEventListener('keydown', (e) => {
      if (e.key === 'Escape' || e.code === 'Escape') {
        e.preventDefault();
        e.stopPropagation();
        try { e.stopImmediatePropagation(); } catch { /* ignore */ }
        this.hide();
      }
    });

    this._searchInput.addEventListener('focus', () => {
      this._searchInput.style.borderColor = '#666';
      this._searchInput.style.background = '#333';
    });

    this._searchInput.addEventListener('blur', () => {
      this._searchInput.style.borderColor = '#444';
      this._searchInput.style.background = '#2a2a2a';
    });

    // Clear button
    const clearBtn = doc.createElement('button');
    clearBtn.textContent = '×';
    clearBtn.className = 'kp-launcher-search-clear';
    clearBtn.style.cssText = `
      position: absolute;
      right: 8px;
      top: 50%;
      transform: translateY(-50%);
      margin: 0;
      appearance: none;
      -webkit-appearance: none;
      background: transparent;
      border: none;
      box-shadow: none;
      color: #888;
      font-size: 20px;
      cursor: pointer;
      padding: 4px 8px;
      display: ${this._searchQuery ? 'block' : 'none'};
      transition: color 0.2s;
    `;

    clearBtn.addEventListener('click', () => {
      this._searchQuery = '';
      this._searchInput.value = '';
      this._currentSheet = 0;
      clearBtn.style.display = 'none';
      this._renderCategory(this._currentCategory);
      this._searchInput.focus();
    });

    clearBtn.addEventListener('mouseenter', () => {
      clearBtn.style.color = '#fff';
    });

    clearBtn.addEventListener('mouseleave', () => {
      clearBtn.style.color = '#888';
    });

    // Store clear button reference for showing/hiding
    this._clearBtn = clearBtn;

    // Update search input listener to show/hide clear button
    this._searchInput.addEventListener('input', (e) => {
      this._searchQuery = e.target.value.toLowerCase().trim();
      this._currentSheet = 0;
      this._clearBtn.style.display = this._searchQuery ? 'block' : 'none';
      this._renderCategory(this._currentCategory);
    });

    searchContainer.appendChild(this._searchInput);
    searchContainer.appendChild(clearBtn);

    header.appendChild(title);
    header.appendChild(subtitle);
    header.appendChild(searchContainer);

    // Sub-tabs container (dynamically populated based on category config)
    this._subTabContainer = doc.createElement('div');
    this._subTabContainer.className = 'kp-launcher-subtabs';
    this._subTabContainer.style.cssText = `
      display: flex;
      gap: 8px;
      padding: 16px 24px 0;
      border-bottom: 1px solid #333;
      background: #0f0f0f;
    `;

    // Sub-tabs will be populated dynamically when category changes
    this._updateSubTabsUI();

    // Grid container
    this._gridContainer = doc.createElement('div');
    this._gridContainer.className = 'kp-launcher-grid-container';
    this._gridContainer.style.cssText = `
      flex: 1;
      padding: 24px;
      overflow-y: auto;
      overflow-x: hidden;
      min-height: 0;
    `;

    // Footer with navigation controls
    const footer = doc.createElement('div');
    footer.className = 'kp-launcher-footer';
    footer.style.cssText = `
      padding: 16px 24px;
      border-top: 1px solid #333;
      background: #0f0f0f;
      display: flex;
      justify-content: space-between;
      align-items: center;
    `;

    const hint = doc.createElement('div');
    hint.style.cssText = 'color: #888; font-size: 13px;';
    hint.innerHTML = 'Press <strong>↑↓</strong> for tabs • <strong>/</strong> to search • <strong>F</strong> to open • <strong>Esc</strong> to close';

    const navControls = doc.createElement('div');
    navControls.className = 'kp-launcher-nav-controls';
    navControls.style.cssText = `
      display: flex;
      gap: 12px;
      align-items: center;
    `;

    // Up button
    const upBtn = this._createNavButton('↑', 'C', () => this._scrollUp());
    navControls.appendChild(upBtn);

    // Down button
    const downBtn = this._createNavButton('↓', 'V', () => this._scrollDown());
    navControls.appendChild(downBtn);

    footer.appendChild(hint);
    footer.appendChild(navControls);

    // Assemble content area
    contentArea.appendChild(header);
    contentArea.appendChild(this._subTabContainer);
    contentArea.appendChild(this._gridContainer);
    contentArea.appendChild(footer);

    // Preview area (iframe)
    const previewArea = doc.createElement('div');
    previewArea.className = 'kp-launcher-preview-area';
    previewArea.style.cssText = `
      width: 0;
      background: #0f0f0f;
      border-left: 1px solid #333;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      transition: width 0.3s ease;
    `;

    // Preview header
    const previewHeader = doc.createElement('div');
    previewHeader.className = 'kp-launcher-preview-header';
    previewHeader.style.cssText = `
      padding: 8px 12px;
      border-bottom: 1px solid #333;
      background: #0f0f0f;
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 8px;
    `;

    const previewTitle = doc.createElement('div');
    previewTitle.className = 'kp-launcher-preview-title';
    previewTitle.textContent = 'Preview';
    previewTitle.style.cssText = `
      color: #fff;
      font-size: 14px;
      font-weight: 500;
      flex: 1;
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    `;

    // Same Open / Open in New Tab controls as Link Preview titlebar
    const { actions: previewOpenActions } = createPreviewOpenActionButtons({
      doc,
      getUrl: () => this._currentPreviewUrl,
      afterOpen: () => {
        try { this.hide(); } catch { /* ignore */ }
      },
      afterOpenNewTab: () => {
        // Keep launcher open after spawning a tab so the user can keep browsing.
      }
    });

    // Rightward collapse control (preview slides in from the right).
    const previewCloseBtn = doc.createElement('button');
    previewCloseBtn.type = 'button';
    previewCloseBtn.className = 'kp-launcher-preview-close';
    previewCloseBtn.title = 'Collapse preview';
    previewCloseBtn.setAttribute('aria-label', 'Collapse preview');
    previewCloseBtn.style.cssText = `
      display: inline-flex;
      align-items: center;
      justify-content: center;
      margin: 0;
      appearance: none;
      -webkit-appearance: none;
      box-sizing: border-box;
      background: transparent;
      border: 1px solid transparent;
      box-shadow: none;
      color: #888;
      cursor: pointer;
      padding: 0;
      width: 26px;
      height: 26px;
      border-radius: 5px;
      flex-shrink: 0;
      transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
    `;
    // Outline chevron pointing right (collapse panel toward the right edge).
    const collapseIcon = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
    collapseIcon.setAttribute('viewBox', '0 0 24 24');
    collapseIcon.setAttribute('width', '16');
    collapseIcon.setAttribute('height', '16');
    collapseIcon.setAttribute('aria-hidden', 'true');
    collapseIcon.style.cssText = 'display: block; pointer-events: none;';
    const collapsePath = doc.createElementNS('http://www.w3.org/2000/svg', 'path');
    collapsePath.setAttribute('d', 'M9 6l6 6-6 6');
    collapsePath.setAttribute('fill', 'none');
    collapsePath.setAttribute('stroke', 'currentColor');
    collapsePath.setAttribute('stroke-width', '2');
    collapsePath.setAttribute('stroke-linecap', 'round');
    collapsePath.setAttribute('stroke-linejoin', 'round');
    collapseIcon.appendChild(collapsePath);
    previewCloseBtn.appendChild(collapseIcon);

    previewCloseBtn.addEventListener('click', () => {
      previewArea.style.width = '0';
      this._currentPreviewUrl = null;
      if (this._previewIframe) {
        this._previewIframe.src = 'about:blank';
      }
    });

    previewCloseBtn.addEventListener('mouseenter', () => {
      previewCloseBtn.style.color = '#fff';
      previewCloseBtn.style.background = 'rgba(255,255,255,0.06)';
      previewCloseBtn.style.borderColor = '#3a3a3a';
    });

    previewCloseBtn.addEventListener('mouseleave', () => {
      previewCloseBtn.style.color = '#888';
      previewCloseBtn.style.background = 'transparent';
      previewCloseBtn.style.borderColor = 'transparent';
    });

    previewHeader.appendChild(previewTitle);
    previewHeader.appendChild(previewOpenActions);
    previewHeader.appendChild(previewCloseBtn);

    // Preview iframe
    this._previewIframe = doc.createElement('iframe');
    this._previewIframe.className = 'kp-launcher-preview-iframe';
    this._previewIframe.style.cssText = `
      flex: 1;
      border: none;
      background: #fff;
    `;

    // Create error message container (initially hidden)
    this._previewError = doc.createElement('div');
    this._previewError.className = 'kp-launcher-preview-error';
    this._previewError.style.cssText = `
      flex: 1;
      display: none;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 20px;
      text-align: center;
      background: #f9f9f9;
      border-radius: 8px;
      margin: 8px;
    `;

    const errorIcon = doc.createElement('div');
    errorIcon.style.cssText = `
      font-size: 32px;
      margin-bottom: 12px;
      color: #999;
    `;
    errorIcon.textContent = '🚫';
    this._previewError.appendChild(errorIcon);

    this._errorTitle = doc.createElement('div');
    this._errorTitle.style.cssText = `
      font-size: 16px;
      font-weight: 600;
      color: #333;
      margin-bottom: 6px;
    `;
    this._errorTitle.textContent = 'Cannot Display Page';
    this._previewError.appendChild(this._errorTitle);

    this._errorMessage = doc.createElement('div');
    this._errorMessage.style.cssText = `
      font-size: 13px;
      color: #666;
      margin-bottom: 16px;
      max-width: 300px;
    `;
    this._errorMessage.textContent = 'This website prevents embedding in iframes for security reasons.';
    this._previewError.appendChild(this._errorMessage);

    const openInTabButton = doc.createElement('button');
    openInTabButton.style.cssText = `
      margin: 0;
      appearance: none;
      -webkit-appearance: none;
      box-sizing: border-box;
      background: #4CAF50;
      color: white;
      border: none;
      box-shadow: none;
      padding: 8px 16px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 13px;
      font-weight: 500;
    `;
    openInTabButton.textContent = 'Open in New Tab';
    openInTabButton.onclick = () => {
      if (this._currentPreviewUrl) {
        window.open(this._currentPreviewUrl, '_blank');
      }
    };
    this._previewError.appendChild(openInTabButton);

    previewArea.appendChild(previewHeader);
    previewArea.appendChild(this._previewIframe);
    previewArea.appendChild(this._previewError);

    this._previewArea = previewArea;

    // Assemble container
    this._container.appendChild(sidebar);
    this._container.appendChild(contentArea);
    this._container.appendChild(previewArea);

    doc.body.appendChild(this._container);
  }

  /**
   * Create a tab button
   */
  _createTab(categoryKey, category) {
    const doc = document;
    const tab = doc.createElement('button');
    tab.className = 'kp-launcher-tab';
    tab.dataset.category = categoryKey;

    const isActive = categoryKey === this._currentCategory;
    tab.style.cssText = `
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 12px 20px;
      margin: 4px 8px;
      background: ${isActive ? '#2a2a2a' : 'transparent'};
      border: 1px solid ${isActive ? '#444' : 'transparent'};
      border-radius: 8px;
      color: ${isActive ? '#fff' : '#888'};
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.2s;
      text-align: left;
    `;

    const icon = doc.createElement('span');
    icon.textContent = category.icon;
    icon.style.fontSize = '18px';

    const label = doc.createElement('span');
    label.textContent = category.label;

    tab.appendChild(icon);
    tab.appendChild(label);

    tab.addEventListener('click', () => {
      this._currentCategory = categoryKey;
      this._currentSheet = 0;
      this._updateSubTabsUI(); // Rebuild sub-tabs for new category
      this._renderCategory(categoryKey);
      this._updateTabStyles();
      this._updateSubTabStyles();
      void this._ensureCategoryHistory(categoryKey);
    });

    tab.addEventListener('mouseenter', () => {
      if (categoryKey !== this._currentCategory) {
        tab.style.background = '#1f1f1f';
        tab.style.borderColor = '#333';
      }
    });

    tab.addEventListener('mouseleave', () => {
      if (categoryKey !== this._currentCategory) {
        tab.style.background = 'transparent';
        tab.style.borderColor = 'transparent';
      }
    });

    return tab;
  }

  /**
   * Create a sub-tab button for history/favorites
   */
  _createSubTab(type, label) {
    const doc = document;
    const subTab = doc.createElement('button');
    subTab.className = 'kp-launcher-subtab';
    subTab.dataset.type = type;

    const currentSubTab = this._categorySubTabs[this._currentCategory] || 'history';
    const isActive = type === currentSubTab;
    subTab.style.cssText = `
      padding: 10px 20px;
      background: ${isActive ? '#2a2a2a' : 'transparent'};
      border: none;
      border-bottom: 2px solid ${isActive ? '#4CAF50' : 'transparent'};
      color: ${isActive ? '#fff' : '#888'};
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.2s;
    `;

    subTab.textContent = label;

    subTab.addEventListener('click', () => {
      this._categorySubTabs[this._currentCategory] = type;
      this._currentSheet = 0;
      this._renderCategory(this._currentCategory);
      this._updateSubTabStyles();
    });

    subTab.addEventListener('mouseenter', () => {
      const currentSubTab = this._categorySubTabs[this._currentCategory] || 'history';
      if (type !== currentSubTab) {
        subTab.style.color = '#fff';
      }
    });

    subTab.addEventListener('mouseleave', () => {
      const currentSubTab = this._categorySubTabs[this._currentCategory] || 'history';
      if (type !== currentSubTab) {
        subTab.style.color = '#888';
      }
    });

    return subTab;
  }

  /**
   * Update sub-tab styles to reflect active sub-tab
   */
  _updateSubTabStyles() {
    const subTabs = this._subTabContainer.querySelectorAll('.kp-launcher-subtab');
    const currentSubTab = this._categorySubTabs[this._currentCategory] || 'favorites';
    subTabs.forEach(subTab => {
      const isActive = subTab.dataset.type === currentSubTab;
      subTab.style.background = isActive ? '#2a2a2a' : 'transparent';
      subTab.style.borderBottomColor = isActive ? '#4CAF50' : 'transparent';
      subTab.style.color = isActive ? '#fff' : '#888';
    });
  }

  /**
   * Update sub-tabs UI based on current category configuration
   */
  _updateSubTabsUI() {
    if (!this._subTabContainer) return;

    // Clear existing sub-tabs
    this._subTabContainer.innerHTML = '';

    // Get sub-tab configuration for current category
    const subTabConfig = this._categorySubTabConfig[this._currentCategory] || ['favorites', 'history'];

    // Create sub-tabs based on configuration
    const subTabLabels = {
      'favorites': 'Favorites',
      'history': 'History'
      // Future sub-tab types can be added here
    };

    subTabConfig.forEach(subTabType => {
      const label = subTabLabels[subTabType] || subTabType;
      const subTab = this._createSubTab(subTabType, label);
      this._subTabContainer.appendChild(subTab);
    });
  }

  /**
   * Create navigation button (up/down)
   */
  _createNavButton(arrow, keyLabel, onClick) {
    const doc = document;
    const btn = doc.createElement('button');
    btn.style.cssText = `
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 16px;
      background: #2a2a2a;
      border: 1px solid #444;
      border-radius: 6px;
      color: #fff;
      font-size: 14px;
      cursor: pointer;
      transition: all 0.2s;
    `;

    const arrowSpan = doc.createElement('span');
    arrowSpan.textContent = arrow;
    arrowSpan.style.fontSize = '16px';

    const keySpan = doc.createElement('kbd');
    keySpan.textContent = keyLabel;
    keySpan.style.cssText = `
      padding: 2px 6px;
      background: #1a1a1a;
      border: 1px solid #555;
      border-radius: 3px;
      font-size: 12px;
      font-family: monospace;
    `;

    btn.appendChild(arrowSpan);
    btn.appendChild(keySpan);

    btn.addEventListener('click', onClick);
    btn.addEventListener('mouseenter', () => {
      btn.style.background = '#333';
      btn.style.borderColor = '#555';
    });
    btn.addEventListener('mouseleave', () => {
      btn.style.background = '#2a2a2a';
      btn.style.borderColor = '#444';
    });

    return btn;
  }

  /**
   * Update tab styles to reflect active category
   */
  _updateTabStyles() {
    const tabs = this._tabListContainer.querySelectorAll('.kp-launcher-tab');
    tabs.forEach(tab => {
      const isActive = tab.dataset.category === this._currentCategory;
      tab.style.background = isActive ? '#2a2a2a' : 'transparent';
      tab.style.borderColor = isActive ? '#444' : 'transparent';
      tab.style.color = isActive ? '#fff' : '#888';
    });
  }

  /**
   * Render a category's items in the grid
   */
  _renderCategory(categoryKey) {
    if (!this._categories || !this._categories[categoryKey]) return;

    const category = this._categories[categoryKey];
    // Get items from the current sub-tab (history or favorites) for this category
    const currentSubTab = this._categorySubTabs[categoryKey] || 'history';
    let items = category[currentSubTab] || [];

    // Filter items based on search query
    if (this._searchQuery) {
      items = items.filter(item => {
        const title = (item.title || '').toLowerCase();
        const url = (item.url || '').toLowerCase();
        return title.includes(this._searchQuery) || url.includes(this._searchQuery);
      });
    }

    // Clear grid
    this._gridContainer.innerHTML = '';

    // Calculate sheet range
    const startIdx = this._currentSheet * this._itemsPerSheet;
    const endIdx = startIdx + this._itemsPerSheet;
    const sheetItems = items.slice(startIdx, endIdx);

    // Create grid
    const grid = document.createElement('div');
    grid.className = 'kp-launcher-grid';
    grid.style.cssText = `
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
      gap: 16px;
      align-content: start;
    `;

    // Render items
    sheetItems.forEach(item => {
      const card = this._createGridCard(item);
      grid.appendChild(card);
    });

    // Show empty state if no items
    if (sheetItems.length === 0) {
      const empty = document.createElement('div');
      empty.style.cssText = `
        grid-column: 1 / -1;
        text-align: center;
        padding: 40px;
        color: #666;
        font-size: 14px;
      `;
      empty.textContent = this._searchQuery ? 'No sites match your search' : 'No items in this category';
      grid.appendChild(empty);
    }

    this._gridContainer.appendChild(grid);
  }

  /**
   * Create a grid card for a website
   */
  _createGridCard(item) {
    const doc = document;
    // Shared URL helpers (same as omnibox / history listings)
    const domain = extractDomain(item.url) || String(item.url || '');
    const path = extractPath(item.url);
    const isDefault = item.isDefault === true;

    // Container — solid fallback until YouTube/official or captured page thumb loads.
    const container = doc.createElement('div');
    container.className = 'kp-launcher-card-container';
    container.style.cssText = `
      display: flex;
      background: ${isDefault ? '#3a3a3a' : '#2a2a2a'};
      border: 1px solid ${isDefault ? '#444' : '#333'};
      border-radius: 8px;
      overflow: hidden;
      min-height: 100px;
      transition: all 0.2s;
      position: relative;
    `;

    // Darkened YouTube / page-screenshot background (async when capture exists).
    applyCardBackground(container, item.url, {
      fallbackSolid: isDefault ? '#3a3a3a' : '#2a2a2a',
      hoverSolid: isDefault ? '#444' : '#333',
      manageHover: true,
      youtubePrefer: true
    });

    // Main link area (3/4 width) - add min-width: 0 to allow shrinking
    const mainLink = doc.createElement('a');
    mainLink.href = item.url;
    mainLink.target = '_blank';
    mainLink.rel = 'noopener noreferrer';
    mainLink.className = 'kp-launcher-card-main';
    mainLink.style.cssText = `
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
      padding: 20px;
      text-decoration: none;
      color: inherit;
      cursor: pointer;
      overflow: hidden;
      position: relative;
      z-index: 1;
    `;

    // Favicon via shared url-listing helper (SW fallback + generic icon)
    const favicon = createFaviconImg(doc, item.url, { size: 32 });
    favicon.style.cssText = `
      width: 32px;
      height: 32px;
      margin-bottom: 12px;
      border-radius: 6px;
      flex-shrink: 0;
    `;

    // Title
    const title = doc.createElement('div');
    title.textContent = item.title || domain;
    title.style.cssText = `
      font-size: 14px;
      font-weight: 500;
      color: #fff;
      margin-bottom: 4px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    `;

    // Domain
    const domainEl = doc.createElement('div');
    domainEl.textContent = domain;
    domainEl.style.cssText = `
      font-size: 12px;
      color: #888;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    `;

    // Path
    const pathEl = doc.createElement('div');
    pathEl.textContent = path;
    pathEl.style.cssText = `
      font-size: 11px;
      color: #666;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-top: 2px;
      ${path ? '' : 'display: none;'}
    `;

    mainLink.appendChild(favicon);
    mainLink.appendChild(title);
    mainLink.appendChild(domainEl);
    mainLink.appendChild(pathEl);

    // Preview button (fixed width instead of flex)
    const previewBtn = doc.createElement('button');
    previewBtn.className = 'kp-launcher-card-preview';
    previewBtn.style.cssText = `
      width: 80px;
      flex-shrink: 0;
      background: #1f1f1f;
      border: none;
      border-left: 1px solid #333;
      color: #888;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
      transition: all 0.2s;
    `;
    previewBtn.innerHTML = '👁';
    previewBtn.title = 'Preview in iframe';

    previewBtn.addEventListener('click', (e) => {
      e.preventDefault();
      e.stopPropagation();
      this._showPreview(item.url);
    });

    previewBtn.addEventListener('mouseenter', () => {
      previewBtn.style.background = '#2a2a2a';
      previewBtn.style.color = '#fff';
    });

    previewBtn.addEventListener('mouseleave', () => {
      previewBtn.style.background = '#1f1f1f';
      previewBtn.style.color = '#888';
    });

    // Assemble container
    container.appendChild(mainLink);
    container.appendChild(previewBtn);

    // Lift + border on hover (background handled by applyCardBackground)
    container.addEventListener('mouseenter', () => {
      container.style.borderColor = isDefault ? '#555' : '#444';
      container.style.transform = 'translateY(-2px)';
    });

    container.addEventListener('mouseleave', () => {
      container.style.borderColor = isDefault ? '#444' : '#333';
      container.style.transform = 'translateY(0)';
    });

    // Main link click closes launcher
    mainLink.addEventListener('click', (e) => {
      e.stopPropagation();
      this.hide();
    });

    return container;
  }

  /**
   * Show preview iframe with URL using advanced bridge system
   */
  _showPreview(url) {
    if (!this._previewArea || !this._previewIframe) return;

    // Prevent CSP violation by blocking file:// URLs
    if (url && url.startsWith('file://')) {
      console.warn('[LauncherPopover] Cannot preview file:// URLs due to CSP restrictions');
      this._showPreviewError('Cannot preview local files');
      return;
    }

    // Track current preview URL for error recovery
    this._currentPreviewUrl = url;

    this._previewArea.style.width = '40%';
    this._previewIframe.style.display = 'flex';
    if (this._previewError) {
      this._previewError.style.display = 'none';
    }

    // Clear any existing bridge initialization
    if (this._previewBridgeTimer) {
      clearInterval(this._previewBridgeTimer);
      this._previewBridgeTimer = null;
    }

    // Initialize the iframe bridge (content script running inside the iframe)
    // We retry a few times because content scripts in the frame may not be ready immediately
    const sendBridgeInit = () => {
      try {
        this._previewIframe.contentWindow?.postMessage({ type: 'KP_POPOVER_BRIDGE_INIT' }, '*');
      } catch {
        // Ignore
      }
    };

    // Handle iframe load errors (X-Frame-Options blocking, network errors, etc.)
    this._previewIframe.onerror = () => {
      console.log('[LauncherPopover] Preview iframe load error detected');
      this._showPreviewError();
    };

    // Handle successful iframe load
    this._previewIframe.onload = () => {
      console.log('[LauncherPopover] Preview iframe loaded successfully');
      sendBridgeInit();
    };

    // Set the URL to start loading
    this._previewIframe.src = url;

    // Send initial bridge init attempt
    sendBridgeInit();

    // Short retry window to cover slow frames / initial about:blank then navigation
    try {
      let attemptsLeft = 6; // ~1.5s total
      this._previewBridgeTimer = setInterval(() => {
        if (attemptsLeft <= 0 || !this._previewIframe || !this._isOpen) {
          if (this._previewBridgeTimer) {
            clearInterval(this._previewBridgeTimer);
            this._previewBridgeTimer = null;
          }
          return;
        }
        attemptsLeft -= 1;
        sendBridgeInit();
      }, 250);
    } catch {
      // Ignore
    }
  }

  /**
   * Show preview error message
   */
  _showPreviewError(message = null) {
    if (!this._previewArea) return;

    this._previewArea.style.width = '40%';
    this._previewIframe.style.display = 'none';
    this._previewError.style.display = 'flex';

    if (message) {
      this._errorMessage.textContent = message;
    } else {
      this._errorMessage.textContent = 'This website prevents embedding in iframes for security reasons.';
    }
  }

  /**
   * Scroll to previous sheet
   */
  _scrollUp() {
    if (this._currentSheet > 0) {
      this._currentSheet--;
      this._renderCategory(this._currentCategory);
    }
  }

  /**
   * Scroll to next sheet
   */
  _scrollDown() {
    if (!this._categories || !this._categories[this._currentCategory]) return;

    const category = this._categories[this._currentCategory];
    const currentSubTab = this._categorySubTabs[this._currentCategory] || 'history';
    const items = category[currentSubTab] || [];
    const maxSheets = Math.ceil(items.length / this._itemsPerSheet);

    if (this._currentSheet < maxSheets - 1) {
      this._currentSheet++;
      this._renderCategory(this._currentCategory);
    }
  }

  /**
   * Navigate to previous category tab
   */
  _navigateToPreviousTab() {
    const currentIndex = this._categoryOrder.indexOf(this._currentCategory);
    if (currentIndex > 0) {
      const newCategory = this._categoryOrder[currentIndex - 1];
      this._currentCategory = newCategory;
      this._currentSheet = 0;
      this._updateSubTabsUI(); // Rebuild sub-tabs for new category
      this._renderCategory(newCategory);
      this._updateTabStyles();
      this._updateSubTabStyles();
      void this._ensureCategoryHistory(newCategory);
    }
  }

  /**
   * Navigate to next category tab
   */
  _navigateToNextTab() {
    const currentIndex = this._categoryOrder.indexOf(this._currentCategory);
    if (currentIndex < this._categoryOrder.length - 1) {
      const newCategory = this._categoryOrder[currentIndex + 1];
      this._currentCategory = newCategory;
      this._currentSheet = 0;
      this._updateSubTabsUI(); // Rebuild sub-tabs for new category
      this._renderCategory(newCategory);
      this._updateTabStyles();
      this._updateSubTabStyles();
      void this._ensureCategoryHistory(newCategory);
    }
  }

  /**
   * Handle keyboard events (C/V for scrolling, Arrow keys for tab navigation, Esc to close)
   */
  handleKeyDown(e) {
    if (!this._isOpen) return false;

    const key = e.key.toLowerCase();

    // Always allow Escape to close
    if (key === 'escape') {
      e.preventDefault();
      e.stopPropagation();
      this.hide();
      return true;
    }

    // If search input is focused, don't intercept most keys (let user type)
    const isSearchFocused = this._searchInput && document.activeElement === this._searchInput;
    if (isSearchFocused && key !== 'escape') {
      return false;
    }

    // Forward slash focuses search
    if (key === '/' && !isSearchFocused) {
      e.preventDefault();
      e.stopPropagation();
      if (this._searchInput) {
        this._searchInput.focus();
      }
      return true;
    }

    // Arrow keys for tab navigation
    if (key === 'arrowup') {
      e.preventDefault();
      e.stopPropagation();
      this._navigateToPreviousTab();
      return true;
    }

    if (key === 'arrowdown') {
      e.preventDefault();
      e.stopPropagation();
      this._navigateToNextTab();
      return true;
    }

    // C/V keys for scrolling through grid sheets
    if (key === 'c') {
      e.preventDefault();
      e.stopPropagation();
      this._scrollUp();
      return true;
    }

    if (key === 'v') {
      e.preventDefault();
      e.stopPropagation();
      this._scrollDown();
      return true;
    }

    return false;
  }
}



  // Module: src/modules/text-element-filter.js
/**
 * TextElementFilter - Identifies and filters elements that contain or are likely to contain text
 * 
 * This class optimizes rectangle selection by only targeting text-containing elements,
 * dramatically reducing the number of elements that need to be observed and processed.
 */

class TextElementFilter {
  constructor() {
    // Get configuration from constants
    const config = EDGE_ONLY_SELECTION.SMART_TARGETING;
    
    this.textContainingTags = new Set(config.TEXT_ELEMENT_TAGS);
    this.skipElementTags = new Set(config.SKIP_ELEMENT_TAGS);
    this.minTextLength = config.MIN_TEXT_LENGTH;
    this.checkComputedStyle = config.CHECK_COMPUTED_STYLE;
    this.includeAriaLabels = config.INCLUDE_ARIA_LABELS;
    this.maxElementsToObserve = config.MAX_ELEMENTS_TO_OBSERVE;
    
    // Performance tracking
    this.stats = {
      totalElementsScanned: 0,
      textElementsFound: 0,
      elementsSkipped: 0,
      processingTime: 0
    };
  }

  /**
   * Find all text-containing elements within a root element
   * @param {Element} rootElement - The root element to search within
   * @returns {Array<Element>} Array of elements that contain or are likely to contain text
   */
  findTextElements(rootElement) {
    if (!FEATURE_FLAGS.ENABLE_TEXT_ELEMENT_FILTER) {
      // Return all elements if filter is disabled
      return Array.from(rootElement.querySelectorAll('*'));
    }

    const startTime = performance.now();
    const textElements = [];
    let elementsScanned = 0;
    let elementsSkipped = 0;

    // Use TreeWalker for efficient DOM traversal
    const walker = document.createTreeWalker(
      rootElement,
      NodeFilter.SHOW_ELEMENT,
      {
        acceptNode: (element) => {
          elementsScanned++;
          
          // Stop if we've reached the maximum number of elements
          if (textElements.length >= this.maxElementsToObserve) {
            return NodeFilter.FILTER_REJECT;
          }

          if (this.isTextContaining(element)) {
            return NodeFilter.FILTER_ACCEPT;
          } else {
            elementsSkipped++;
            return NodeFilter.FILTER_SKIP;
          }
        }
      }
    );

    let element;
    while (element = walker.nextNode()) {
      textElements.push(element);
    }

    // Update performance statistics
    const processingTime = performance.now() - startTime;
    this.stats.totalElementsScanned += elementsScanned;
    this.stats.textElementsFound += textElements.length;
    this.stats.elementsSkipped += elementsSkipped;
    this.stats.processingTime += processingTime;

    return textElements;
  }

  /**
   * Check if an element contains or is likely to contain text
   * @param {Element} element - The element to check
   * @returns {boolean} True if the element contains or is likely to contain text
   */
  isTextContaining(element) {
    // Skip elements that definitely don't contain text
    if (this.skipElementTags.has(element.tagName.toLowerCase())) {
      return false;
    }

    // Check if element is visible (if configured to do so)
    if (this.checkComputedStyle && !this.isElementVisible(element)) {
      return false;
    }

    // Check tag type - elements that commonly contain text, but only if they have some text content
    if (this.textContainingTags.has(element.tagName.toLowerCase())) {
      // For common text tags, also check if there's any meaningful text content
      const textContent = element.textContent ? element.textContent.trim() : '';
      if (textContent.length >= this.minTextLength) {
        return true;
      }
    }

    // Check if element has direct text content
    if (this.hasDirectTextContent(element)) {
      return true;
    }

    // Check for ARIA labels if configured
    if (this.includeAriaLabels && this.hasAriaText(element)) {
      return true;
    }

    // Check for input elements with text values
    if (this.isTextInput(element)) {
      return true;
    }

    // Check for elements with text-like content
    if (this.hasTextLikeContent(element)) {
      return true;
    }

    return false;
  }

  /**
   * Check if an element has direct text content (not from child elements)
   * @param {Element} element - The element to check
   * @returns {boolean} True if the element has direct text content
   */
  hasDirectTextContent(element) {
    // Get only direct text nodes (not from children)
    const textNodes = Array.from(element.childNodes).filter(
      node => node.nodeType === Node.TEXT_NODE
    );
    
    const textContent = textNodes
      .map(node => node.textContent)
      .join('')
      .trim();
    
    return textContent.length >= this.minTextLength;
  }

  /**
   * Check if an element has ARIA text content
   * @param {Element} element - The element to check
   * @returns {boolean} True if the element has ARIA text content
   */
  hasAriaText(element) {
    const ariaLabel = element.getAttribute('aria-label');
    const ariaLabelledBy = element.getAttribute('aria-labelledby');
    const ariaDescribedBy = element.getAttribute('aria-describedby');
    
    if (ariaLabel && ariaLabel.trim().length >= this.minTextLength) {
      return true;
    }
    
    if (ariaLabelledBy) {
      const labelElement = document.getElementById(ariaLabelledBy);
      if (labelElement && labelElement.textContent.trim().length >= this.minTextLength) {
        return true;
      }
    }
    
    if (ariaDescribedBy) {
      const descElement = document.getElementById(ariaDescribedBy);
      if (descElement && descElement.textContent.trim().length >= this.minTextLength) {
        return true;
      }
    }
    
    return false;
  }

  /**
   * Check if an element is a text input
   * @param {Element} element - The element to check
   * @returns {boolean} True if the element is a text input
   */
  isTextInput(element) {
    const tagName = element.tagName.toLowerCase();
    
    if (tagName === 'textarea') {
      return true;
    }
    
    if (tagName === 'input') {
      const type = element.type ? element.type.toLowerCase() : 'text';
      const textInputTypes = [
        'text', 'search', 'url', 'email', 'tel', 'password', 'number'
      ];
      return textInputTypes.includes(type);
    }
    
    // Check for contenteditable elements
    if (element.contentEditable === 'true') {
      return true;
    }
    
    return false;
  }

  /**
   * Check if an element has text-like content (placeholder, title, alt text, etc.)
   * @param {Element} element - The element to check
   * @returns {boolean} True if the element has text-like content
   */
  hasTextLikeContent(element) {
    const textAttributes = ['placeholder', 'title', 'alt'];
    
    for (const attr of textAttributes) {
      const value = element.getAttribute(attr);
      if (value && value.trim().length >= this.minTextLength) {
        return true;
      }
    }
    
    // Check value property for input elements (but only for text-like inputs)
    if (element.tagName.toLowerCase() === 'input' && this.isTextInput(element) && 
        element.value && element.value.trim().length >= this.minTextLength) {
      return true;
    }
    
    return false;
  }

  /**
   * Check if an element is visible using computed styles
   * @param {Element} element - The element to check
   * @returns {boolean} True if the element is visible
   */
  isElementVisible(element) {
    try {
      // If element is not in the DOM (common in tests), assume visible
      if (!element.isConnected) {
        return true;
      }
      
      // In test environment (when document.body is empty or minimal), be more lenient
      if (document.body.children.length <= 1) {
        return true;
      }
      
      const style = window.getComputedStyle(element);
      
      // Check if element is hidden
      if (style.display === 'none' || 
          style.visibility === 'hidden' || 
          style.opacity === '0') {
        return false;
      }
      
      // Check if element has zero dimensions (but allow for test scenarios)
      const rect = element.getBoundingClientRect();
      if (rect.width === 0 && rect.height === 0 && element.isConnected) {
        return false;
      }
      
      return true;
    } catch (error) {
      // If we can't get computed style, assume visible
      return true;
    }
  }

  /**
   * Filter an array of elements to only include text-containing ones
   * @param {Array<Element>} elements - Array of elements to filter
   * @returns {Array<Element>} Filtered array of text-containing elements
   */
  filterTextElements(elements) {
    if (!FEATURE_FLAGS.ENABLE_TEXT_ELEMENT_FILTER) {
      return elements;
    }

    const startTime = performance.now();
    const filteredElements = elements.filter(element => this.isTextContaining(element));
    
    // Update statistics
    const processingTime = performance.now() - startTime;
    this.stats.totalElementsScanned += elements.length;
    this.stats.textElementsFound += filteredElements.length;
    this.stats.elementsSkipped += elements.length - filteredElements.length;
    this.stats.processingTime += processingTime;
    
    return filteredElements;
  }

  /**
   * Get performance statistics for the filter
   * @returns {Object} Performance statistics
   */
  getStats() {
    const totalElements = this.stats.totalElementsScanned;
    const reductionPercentage = totalElements > 0 
      ? ((this.stats.elementsSkipped / totalElements) * 100).toFixed(1)
      : 0;
    
    return {
      totalElementsScanned: this.stats.totalElementsScanned,
      textElementsFound: this.stats.textElementsFound,
      elementsSkipped: this.stats.elementsSkipped,
      reductionPercentage: `${reductionPercentage}%`,
      averageProcessingTime: this.stats.processingTime / Math.max(1, this.stats.totalElementsScanned),
      totalProcessingTime: this.stats.processingTime
    };
  }

  /**
   * Reset performance statistics
   */
  resetStats() {
    this.stats = {
      totalElementsScanned: 0,
      textElementsFound: 0,
      elementsSkipped: 0,
      processingTime: 0
    };
  }

  /**
   * Update configuration from constants (useful for runtime configuration changes)
   */
  updateConfiguration() {
    const config = EDGE_ONLY_SELECTION.SMART_TARGETING;
    
    this.textContainingTags = new Set(config.TEXT_ELEMENT_TAGS);
    this.skipElementTags = new Set(config.SKIP_ELEMENT_TAGS);
    this.minTextLength = config.MIN_TEXT_LENGTH;
    this.checkComputedStyle = config.CHECK_COMPUTED_STYLE;
    this.includeAriaLabels = config.INCLUDE_ARIA_LABELS;
    this.maxElementsToObserve = config.MAX_ELEMENTS_TO_OBSERVE;
  }
}


  // Module: src/modules/edge-character-detector.js
/**
 * EdgeCharacterDetector - Detects characters at the precise boundary level when elements intersect
 * 
 * This class provides character-level precision for rectangle selection by using the Range API
 * to calculate exact character positions and determine which characters intersect with rectangle boundaries.
 */

class EdgeCharacterDetector {
  constructor() {
    // Get configuration from constants
    const config = EDGE_ONLY_SELECTION.CHARACTER_DETECTION;
    
    this.enabled = config.ENABLED;
    this.useRangeAPI = config.USE_RANGE_API;
    this.cacheCharacterPositions = config.CACHE_CHARACTER_POSITIONS;
    this.characterCacheSize = config.CHARACTER_CACHE_SIZE;
    this.boundaryDetectionPrecision = config.BOUNDARY_DETECTION_PRECISION;
    this.batchCharacterProcessing = config.BATCH_CHARACTER_PROCESSING;
    this.characterBatchSize = config.CHARACTER_BATCH_SIZE;
    this.maxCharactersPerElement = config.MAX_CHARACTERS_PER_ELEMENT;
    
    // WeakMap cache for character positions to avoid recalculation
    this.characterCache = new WeakMap();
    
    // Performance tracking
    this.stats = {
      charactersDetected: 0,
      cacheHits: 0,
      cacheMisses: 0,
      processingTime: 0,
      elementsProcessed: 0
    };
  }

  /**
   * Detect characters at the boundary level when an element intersects with a rectangle
   * @param {Element} element - The element to detect characters in
   * @param {Object} rectangle - The rectangle bounds {left, top, right, bottom}
   * @returns {Array<Object>} Array of character objects that intersect with the rectangle
   */
  detectAtBoundary(element, rectangle) {
    if (!FEATURE_FLAGS.ENABLE_EDGE_CHARACTER_DETECTOR || !this.enabled) {
      return [];
    }

    const startTime = performance.now();
    let characterPositions = [];

    try {
      // Get cached character positions or calculate them
      if (this.cacheCharacterPositions) {
        characterPositions = this.characterCache.get(element);
        if (characterPositions) {
          this.stats.cacheHits++;
        } else {
          this.stats.cacheMisses++;
          characterPositions = this.calculateCharacterPositions(element);
          this.characterCache.set(element, characterPositions);
        }
      } else {
        characterPositions = this.calculateCharacterPositions(element);
      }

      // Find characters that intersect with rectangle boundaries
      const intersectingCharacters = this.findIntersectingCharacters(characterPositions, rectangle);
      
      // Update statistics
      const processingTime = performance.now() - startTime;
      this.stats.charactersDetected += intersectingCharacters.length;
      this.stats.processingTime += processingTime;
      this.stats.elementsProcessed++;

      return intersectingCharacters;
    } catch (error) {
      console.warn('[EdgeCharacterDetector] Error detecting characters at boundary:', error);
      return [];
    }
  }

  /**
   * Calculate character positions for all text nodes within an element
   * @param {Element} element - The element to calculate character positions for
   * @returns {Array<Object>} Array of character position objects
   */
  calculateCharacterPositions(element) {
    const characters = [];
    const textNodes = this.getTextNodes(element);
    
    for (const textNode of textNodes) {
      const nodeCharacters = this.calculateTextNodeCharacterPositions(textNode);
      characters.push(...nodeCharacters);
      
      // Respect maximum characters per element limit
      if (characters.length >= this.maxCharactersPerElement) {
        break;
      }
    }
    
    return characters.slice(0, this.maxCharactersPerElement);
  }

  /**
   * Calculate character positions for a specific text node
   * @param {Text} textNode - The text node to process
   * @returns {Array<Object>} Array of character position objects
   */
  calculateTextNodeCharacterPositions(textNode) {
    const characters = [];
    const text = textNode.textContent;
    
    if (!text || text.length === 0) {
      return characters;
    }

    if (this.batchCharacterProcessing) {
      return this.calculateCharacterPositionsBatched(textNode, text);
    } else {
      return this.calculateCharacterPositionsSequential(textNode, text);
    }
  }

  /**
   * Calculate character positions using batched processing
   * @param {Text} textNode - The text node to process
   * @param {string} text - The text content
   * @returns {Array<Object>} Array of character position objects
   */
  calculateCharacterPositionsBatched(textNode, text) {
    const characters = [];
    const batchSize = this.characterBatchSize;
    
    for (let i = 0; i < text.length; i += batchSize) {
      const endIndex = Math.min(i + batchSize, text.length);
      const batchCharacters = this.calculateCharacterBatch(textNode, text, i, endIndex);
      characters.push(...batchCharacters);
    }
    
    return characters;
  }

  /**
   * Calculate character positions for a batch of characters
   * @param {Text} textNode - The text node to process
   * @param {string} text - The text content
   * @param {number} startIndex - Start index of the batch
   * @param {number} endIndex - End index of the batch
   * @returns {Array<Object>} Array of character position objects
   */
  calculateCharacterBatch(textNode, text, startIndex, endIndex) {
    const characters = [];
    
    for (let i = startIndex; i < endIndex; i++) {
      const char = text[i];
      
      // Skip whitespace characters that don't contribute to selection
      if (char.trim().length === 0) {
        continue;
      }
      
      try {
        const characterData = this.calculateSingleCharacterPosition(textNode, i, char);
        if (characterData) {
          characters.push(characterData);
        }
      } catch (error) {
        // Skip characters that can't be positioned
        continue;
      }
    }
    
    return characters;
  }

  /**
   * Calculate character positions using sequential processing
   * @param {Text} textNode - The text node to process
   * @param {string} text - The text content
   * @returns {Array<Object>} Array of character position objects
   */
  calculateCharacterPositionsSequential(textNode, text) {
    const characters = [];
    
    for (let i = 0; i < text.length; i++) {
      const char = text[i];
      
      // Skip whitespace characters that don't contribute to selection
      if (char.trim().length === 0) {
        continue;
      }
      
      try {
        const characterData = this.calculateSingleCharacterPosition(textNode, i, char);
        if (characterData) {
          characters.push(characterData);
        }
      } catch (error) {
        // Skip characters that can't be positioned
        continue;
      }
    }
    
    return characters;
  }

  /**
   * Calculate position for a single character using Range API
   * @param {Text} textNode - The text node containing the character
   * @param {number} index - Index of the character within the text node
   * @param {string} char - The character
   * @returns {Object|null} Character position object or null if positioning fails
   */
  calculateSingleCharacterPosition(textNode, index, char) {
    if (!this.useRangeAPI) {
      return null;
    }

    try {
      const range = document.createRange();
      range.setStart(textNode, index);
      range.setEnd(textNode, index + 1);
      
      const rect = range.getBoundingClientRect();
      
      // Only include characters with valid dimensions
      if (rect.width > 0 && rect.height > 0) {
        return {
          char: char,
          node: textNode,
          index: index,
          rect: {
            left: rect.left,
            top: rect.top,
            right: rect.right,
            bottom: rect.bottom,
            width: rect.width,
            height: rect.height
          },
          range: range.cloneRange()
        };
      }
    } catch (error) {
      // Range creation failed, skip this character
    }
    
    return null;
  }

  /**
   * Find characters that intersect with the given rectangle
   * @param {Array<Object>} characterPositions - Array of character position objects
   * @param {Object} rectangle - The rectangle bounds {left, top, right, bottom}
   * @returns {Array<Object>} Array of intersecting character objects
   */
  findIntersectingCharacters(characterPositions, rectangle) {
    const intersectingCharacters = [];
    
    for (const character of characterPositions) {
      if (this.isCharacterInRectangle(character, rectangle)) {
        intersectingCharacters.push(character);
      }
    }
    
    return intersectingCharacters;
  }

  /**
   * Check if a character intersects with a rectangle
   * @param {Object} character - Character position object
   * @param {Object} rectangle - Rectangle bounds {left, top, right, bottom}
   * @returns {boolean} True if the character intersects with the rectangle
   */
  isCharacterInRectangle(character, rectangle) {
    const charRect = character.rect;
    const precision = this.boundaryDetectionPrecision;
    
    // Check for intersection with precision tolerance
    return !(
      charRect.right < rectangle.left - precision ||
      charRect.left > rectangle.right + precision ||
      charRect.bottom < rectangle.top - precision ||
      charRect.top > rectangle.bottom + precision
    );
  }

  /**
   * Get all text nodes within an element
   * @param {Element} element - The element to search for text nodes
   * @returns {Array<Text>} Array of text nodes
   */
  getTextNodes(element) {
    const textNodes = [];
    
    const walker = document.createTreeWalker(
      element,
      NodeFilter.SHOW_TEXT,
      {
        acceptNode: (node) => {
          // Only include text nodes with meaningful content
          return node.textContent && node.textContent.trim().length > 0
            ? NodeFilter.FILTER_ACCEPT 
            : NodeFilter.FILTER_REJECT;
        }
      }
    );
    
    let node;
    while (node = walker.nextNode()) {
      textNodes.push(node);
    }
    
    return textNodes;
  }

  /**
   * Clear the character position cache
   */
  clearCache() {
    this.characterCache = new WeakMap();
  }

  /**
   * Get performance statistics
   * @returns {Object} Performance statistics
   */
  getStats() {
    const totalCacheAccess = this.stats.cacheHits + this.stats.cacheMisses;
    const cacheHitRate = totalCacheAccess > 0 
      ? ((this.stats.cacheHits / totalCacheAccess) * 100).toFixed(1)
      : 0;
    
    return {
      charactersDetected: this.stats.charactersDetected,
      cacheHits: this.stats.cacheHits,
      cacheMisses: this.stats.cacheMisses,
      cacheHitRate: `${cacheHitRate}%`,
      averageProcessingTime: this.stats.processingTime / Math.max(1, this.stats.elementsProcessed),
      totalProcessingTime: this.stats.processingTime,
      elementsProcessed: this.stats.elementsProcessed
    };
  }

  /**
   * Reset performance statistics
   */
  resetStats() {
    this.stats = {
      charactersDetected: 0,
      cacheHits: 0,
      cacheMisses: 0,
      processingTime: 0,
      elementsProcessed: 0
    };
  }

  /**
   * Update configuration from constants (useful for runtime configuration changes)
   */
  updateConfiguration() {
    const config = EDGE_ONLY_SELECTION.CHARACTER_DETECTION;
    
    this.enabled = config.ENABLED;
    this.useRangeAPI = config.USE_RANGE_API;
    this.cacheCharacterPositions = config.CACHE_CHARACTER_POSITIONS;
    this.characterCacheSize = config.CHARACTER_CACHE_SIZE;
    this.boundaryDetectionPrecision = config.BOUNDARY_DETECTION_PRECISION;
    this.batchCharacterProcessing = config.BATCH_CHARACTER_PROCESSING;
    this.characterBatchSize = config.CHARACTER_BATCH_SIZE;
    this.maxCharactersPerElement = config.MAX_CHARACTERS_PER_ELEMENT;
  }

  /**
   * Create a selection range from intersecting characters
   * @param {Array<Object>} characters - Array of character objects
   * @returns {Range|null} DOM Range covering the characters, or null if no characters
   */
  createSelectionRange(characters) {
    if (!characters || characters.length === 0) {
      return null;
    }

    try {
      // Sort characters by their position in the document
      const sortedCharacters = this.sortCharactersByDocumentOrder(characters);
      
      const firstChar = sortedCharacters[0];
      const lastChar = sortedCharacters[sortedCharacters.length - 1];
      
      const range = document.createRange();
      range.setStart(firstChar.node, firstChar.index);
      range.setEnd(lastChar.node, lastChar.index + 1);
      
      return range;
    } catch (error) {
      console.warn('[EdgeCharacterDetector] Error creating selection range:', error);
      return null;
    }
  }

  /**
   * Sort characters by their position in the document
   * @param {Array<Object>} characters - Array of character objects
   * @returns {Array<Object>} Sorted array of character objects
   */
  sortCharactersByDocumentOrder(characters) {
    return characters.sort((a, b) => {
      // Compare by node position first
      const nodeComparison = a.node.compareDocumentPosition(b.node);
      
      if (nodeComparison & Node.DOCUMENT_POSITION_FOLLOWING) {
        return -1; // a comes before b
      } else if (nodeComparison & Node.DOCUMENT_POSITION_PRECEDING) {
        return 1; // a comes after b
      } else {
        // Same node, compare by index
        return a.index - b.index;
      }
    });
  }

  /**
   * Get character count for an element (useful for performance estimation)
   * @param {Element} element - The element to count characters in
   * @returns {number} Approximate character count
   */
  getCharacterCount(element) {
    const textNodes = this.getTextNodes(element);
    let totalCharacters = 0;
    
    for (const textNode of textNodes) {
      totalCharacters += textNode.textContent.trim().length;
    }
    
    return Math.min(totalCharacters, this.maxCharactersPerElement);
  }
}


  // Module: src/modules/rectangle-intersection-observer.js



/**
 * Rectangle Intersection Observer - Uses IntersectionObserver for efficient rectangle selection
 * 
 * This approach leverages the browser's optimized intersection calculations to detect
 * text elements within a live selection rectangle, providing performance comparable
 * to manual browser selection.
 */
class RectangleIntersectionObserver {
  constructor() {
    // Main intersection observer for text elements
    this.textObserver = null;
    
    // Dynamic root element that represents the selection rectangle
    this.rectangleRoot = null;
    
    // Set of text elements currently intersecting the rectangle
    this.intersectingTextElements = new Set();
    
    // Set of text nodes found within intersecting elements
    this.intersectingTextNodes = new Set();
    
    // Callback for when intersection changes
    this.onIntersectionChange = null;

    // Enhanced integration components (Task 2) - controlled by master flag
    this.enhancedIntegrationEnabled = FEATURE_FLAGS.ENABLE_ENHANCED_RECTANGLE_OBSERVER;
    
    if (this.enhancedIntegrationEnabled) {
      // Smart element targeting (Task 2.1)
      this.textElementFilter = new TextElementFilter();
      this.observedElements = new Set();
      this.smartTargetingEnabled = EDGE_ONLY_SELECTION.SMART_TARGETING.ENABLED;

      // Edge-level character detection (Task 2.2)
      this.edgeCharacterDetector = new EdgeCharacterDetector();
      this.edgeCharacterDetectionEnabled = EDGE_ONLY_SELECTION.CHARACTER_DETECTION.ENABLED;

      // Performance monitoring removed
    } else {
      // Disable all enhanced features if master flag is off
      this.textElementFilter = null;
      this.observedElements = null;
      this.smartTargetingEnabled = false;
      this.edgeCharacterDetector = null;
      this.edgeCharacterDetectionEnabled = false;
    };
    
    // Adaptive Processing System (Task 2.1)
    this.adaptiveProcessor = {
      enabled: EDGE_ONLY_SELECTION.ENABLE_ADAPTIVE_PROCESSING,
      
      // Page complexity analysis
      pageComplexity: {
        elementCount: 0,
        domDepth: 0,
        textNodeDensity: 0,
        complexityLevel: 'medium', // low, medium, high
        lastAnalysis: 0,
        analysisInterval: EDGE_ONLY_SELECTION.PAGE_COMPLEXITY_ANALYSIS.COMPLEXITY_CHECK_INTERVAL
      },
      
      // Frame rate monitoring
      frameRateMonitor: {
        targetFps: EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.TARGET_FPS,
        frameTimeBudget: EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.FRAME_TIME_BUDGET_MS,
        processingTimeBudget: EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.PROCESSING_TIME_BUDGET_MS,
        frameTimes: [],
        currentFps: 60,
        lastFrameTime: 0,
        frameCount: 0,
        monitoringWindow: EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.FRAME_RATE_MONITORING_WINDOW
      },
      
      // Batch processing optimization
      batchProcessor: {
        enabled: EDGE_ONLY_SELECTION.BATCH_PROCESSING.ENABLE_BATCH_PROCESSING,
        currentBatchSize: EDGE_ONLY_SELECTION.BATCH_PROCESSING.DEFAULT_BATCH_SIZE,
        maxBatchSize: EDGE_ONLY_SELECTION.BATCH_PROCESSING.MAX_BATCH_SIZE,
        minBatchSize: EDGE_ONLY_SELECTION.BATCH_PROCESSING.MIN_BATCH_SIZE,
        batchTimeout: EDGE_ONLY_SELECTION.BATCH_PROCESSING.BATCH_TIMEOUT_MS,
        pendingBatch: [],
        batchTimer: null,
        adaptiveSizing: EDGE_ONLY_SELECTION.BATCH_PROCESSING.ADAPTIVE_BATCH_SIZING
      },
      
      // Quality adjustment system
      qualityController: {
        enabled: EDGE_ONLY_SELECTION.QUALITY_ADJUSTMENTS.ENABLE_QUALITY_ADJUSTMENTS,
        currentQuality: 'high', // high, medium, low
        qualityHistory: [],
        adjustmentHysteresis: EDGE_ONLY_SELECTION.QUALITY_ADJUSTMENTS.QUALITY_ADJUSTMENT_HYSTERESIS,
        hysteresisCounter: 0,
        thresholds: {
          high: EDGE_ONLY_SELECTION.QUALITY_ADJUSTMENTS.HIGH_QUALITY_TIME_THRESHOLD,
          medium: EDGE_ONLY_SELECTION.QUALITY_ADJUSTMENTS.MEDIUM_QUALITY_TIME_THRESHOLD,
          lowProcessingLimit: EDGE_ONLY_SELECTION.QUALITY_ADJUSTMENTS.LOW_QUALITY_PROCESSING_LIMIT
        }
      }
    };

    // Predictive Caching System (Task 2.2)
    this.predictiveCaching = {
      enabled: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.ENABLE_PREDICTIVE_CACHING,
      
      // User behavior analysis
      behaviorAnalyzer: {
        enabled: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.ENABLE_USER_BEHAVIOR_ANALYSIS,
        interactionHistory: [],
        patterns: new Map(), // Pattern type -> confidence score
        lastAnalysis: 0,
        analysisInterval: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.PATTERN_ANALYSIS_INTERVAL,
        patternWindow: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.BEHAVIOR_PATTERN_WINDOW,
        interactionTimeout: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.INTERACTION_TIMEOUT_MS,
        minConfidence: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.MIN_PATTERN_CONFIDENCE
      },
      
      // Viewport-based caching
      viewportCaching: {
        enabled: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.ENABLE_VIEWPORT_BASED_CACHING,
        currentViewport: { x: 0, y: 0, width: 0, height: 0 },
        cacheMargin: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.VIEWPORT_CACHE_MARGIN,
        cacheSectors: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.VIEWPORT_CACHE_SECTORS,
        sectorCache: new Map(), // Sector ID -> cached elements
        lastViewportUpdate: 0,
        updateThrottle: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.VIEWPORT_UPDATE_THROTTLE
      },
      
      // Scroll prediction
      scrollPredictor: {
        enabled: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.ENABLE_SCROLL_PREDICTION,
        scrollHistory: [],
        currentVelocity: { x: 0, y: 0 },
        predictedPosition: { x: 0, y: 0 },
        velocitySamples: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.SCROLL_VELOCITY_SAMPLES,
        predictionDistance: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.SCROLL_PREDICTION_DISTANCE,
        minVelocity: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.MIN_SCROLL_VELOCITY,
        directionThreshold: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.SCROLL_DIRECTION_THRESHOLD,
        lastScrollTime: 0
      },
      
      // Cache preloading
      preloader: {
        enabled: true,
        preloadQueue: [],
        preloadCache: new WeakMap(), // Element -> preload data
        batchSize: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.PRELOAD_BATCH_SIZE,
        throttleMs: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.PRELOAD_THROTTLE_MS,
        maxElements: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.MAX_PRELOAD_ELEMENTS,
        priorityThreshold: EDGE_ONLY_SELECTION.PREDICTIVE_CACHING.PRELOAD_PRIORITY_THRESHOLD,
        lastPreloadTime: 0,
        preloadTimer: null
      }
    };
    
    // Legacy performance monitoring removed
    
    // Fallback mechanism to spatial intersection
    this.fallbackHandler = {
      active: false,
      reason: null,
      fallbackStartTime: null,
      spatialIntersectionCallback: null,
      recoveryAttempts: 0,
      maxRecoveryAttempts: 3,
      recoveryTestInterval: 30000, // Test recovery every 30 seconds
      lastRecoveryTest: 0
    };
    
    // User notification system for performance mode changes
    this.notificationSystem = {
      enabled: true,
      lastNotificationTime: 0,
      notificationCooldownMs: 10000, // 10 second cooldown between notifications
      showPerformanceNotifications: FEATURE_FLAGS.SHOW_SELECTION_METHOD_IN_UI,
      notificationCallback: null
    };
    
    // Enhanced performance metrics for edge-only processing
    this.metrics = {
      elementsObserved: 0,
      intersectionUpdates: 0,
      textNodesFound: 0,
      lastUpdateTime: 0,
      
      // Edge-only processing metrics
      edgeProcessingTime: 0,
      edgeElementsProcessed: 0,
      elementsEntering: 0,
      elementsLeaving: 0,
      
      // Text node traversal metrics
      totalTraversalTime: 0,
      traversalCount: 0,
      cacheHits: 0,
      cacheMisses: 0,
      
      // Performance comparison metrics
      estimatedFullTraversalTime: 0,
      actualEdgeProcessingTime: 0,
      efficiencyGainPercent: 0,
      
      // Smart targeting metrics (Task 2.1)
      totalElementsScanned: 0,
      elementsSkipped: 0,
      elementReductionPercentage: 0,
      smartTargetingTime: 0,
      dynamicElementsAdded: 0,
      
      // Character detection metrics (Task 2.2)
      charactersDetected: 0,
      characterDetectionTime: 0,
      characterCacheHits: 0,
      characterCacheMisses: 0
    };
    
    // Cache for text node extraction with hit/miss tracking and LRU eviction
    this.textNodeCache = new WeakMap();
    
    // Enhanced LRU cache management with WeakRef to prevent memory leaks
    this.cacheAccessOrder = new WeakMap(); // element -> { timestamp, accessCount, elementId }

    // LRU ordering with WeakRefs for automatic cleanup
    this.lruRefs = new Map(); // elementId -> WeakRef(element)
    this.lruTimestamps = new Map(); // elementId -> timestamp for ordering

    // Finalization registry for automatic cleanup when elements are GC'd
    this.cacheCleanupRegistry = new FinalizationRegistry((elementId) => {
      this.lruRefs.delete(elementId);
      this.lruTimestamps.delete(elementId);
      this.cacheEfficiencyMetrics.hotElements.delete(elementId);
      this.cacheEfficiencyMetrics.coldElements.delete(elementId);
      this.cacheSize = Math.max(0, this.cacheSize - 1);
    });
    this.cacheSize = 0;
    this.maxCacheSize = EDGE_ONLY_SELECTION.CACHE_SIZE_LIMIT;
    this.cleanupThreshold = EDGE_ONLY_SELECTION.CACHE_CLEANUP_THRESHOLD;
    this.cleanupBatchSize = EDGE_ONLY_SELECTION.CACHE_CLEANUP_BATCH_SIZE;
    
    // Initialize cache management timers
    this.cacheManagementTimer = null;
    this.memoryMonitoringTimer = null;
    this.lastCacheOptimization = Date.now();
    
    // Enhanced cache efficiency tracking
    this.cacheEfficiencyMetrics = {
      totalCacheOperations: 0,
      cacheHitStreak: 0,
      longestHitStreak: 0,
      cacheMissStreak: 0,
      longestMissStreak: 0,
      averageAccessTime: 0,
      totalAccessTime: 0,
      hotElements: new Map(), // Track frequently accessed elements
      coldElements: new Set(), // Track rarely accessed elements
      evictionHistory: [], // Track evicted elements for analysis
      memoryPressureEvents: 0,
      adaptiveCleanupTriggers: 0
    };
    
    // State management for proper edge-only processing
    this.elementStates = new Map(); // Track element intersection states
    this.lastRectangleUpdate = 0;
    
    // Enhanced analytics removed

    // Debug logging configuration
    this.debugLogging = window.KEYPILOT_DEBUG || false;
  }

  /**
   * Initialize the rectangle intersection observer
   * @param {Function} onIntersectionChange - Callback when intersection changes
   * @param {Function} notificationCallback - Optional callback for user notifications
   */
  initialize(onIntersectionChange, notificationCallback = null) {
    this.onIntersectionChange = onIntersectionChange;
    
    // Set up notification system
    if (notificationCallback) {
      this.setNotificationCallback(notificationCallback);
    }
    
    this.createRectangleRoot();
    this.setupTextObserver();
    this.discoverTextElements();
    this.startCacheManagement();
    
    // Performance monitoring removed
    
    // Initialize adaptive processing (Task 2.1)
    if (this.adaptiveProcessor.enabled) {
      this.initializeAdaptiveProcessing();
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Rectangle intersection observer initialized with performance monitoring:', {
        performanceMonitoringEnabled: this.legacyPerformanceMonitor.enabled,
        fallbackThreshold: this.legacyPerformanceMonitor.thresholdMs + 'ms',
        maxConsecutiveSlowOps: this.legacyPerformanceMonitor.maxConsecutiveSlowOps,
        notificationSystemEnabled: this.notificationSystem.enabled,
        adaptiveProcessingEnabled: this.adaptiveProcessor.enabled
      });
    }
  }

  /**
   * Initialize adaptive processing system (Task 2.1)
   */
  initializeAdaptiveProcessing() {
    // Perform initial page complexity analysis
    this.analyzePageComplexity();

    // Frame rate monitoring removed
    
    // Initialize batch processing
    if (this.adaptiveProcessor.batchProcessor.enabled) {
      this.initializeBatchProcessing();
    }
    
    // Initialize predictive caching (Task 2.2)
    if (this.predictiveCaching.enabled) {
      this.initializePredictiveCaching();
    }
    
    // Enhanced analytics removed (Task 2.3)
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Adaptive processing initialized:', {
        pageComplexity: this.adaptiveProcessor.pageComplexity.complexityLevel,
        elementCount: this.adaptiveProcessor.pageComplexity.elementCount,
        domDepth: this.adaptiveProcessor.pageComplexity.domDepth,
        textNodeDensity: this.adaptiveProcessor.pageComplexity.textNodeDensity,
        batchProcessingEnabled: this.adaptiveProcessor.batchProcessor.enabled,
        currentBatchSize: this.adaptiveProcessor.batchProcessor.currentBatchSize,
        predictiveCachingEnabled: this.predictiveCaching.enabled,
        enhancedAnalyticsEnabled: this.enhancedAnalytics.enabled
      });
    }
  }

  /**
   * Analyze page complexity for adaptive processing (Task 2.1)
   */
  analyzePageComplexity() {
    const startTime = performance.now();
    
    // Count total elements in the document
    const allElements = document.querySelectorAll('*');
    this.adaptiveProcessor.pageComplexity.elementCount = allElements.length;
    
    // Calculate DOM depth
    let maxDepth = 0;
    const calculateDepth = (element, depth = 0) => {
      maxDepth = Math.max(maxDepth, depth);
      for (const child of element.children) {
        calculateDepth(child, depth + 1);
      }
    };
    calculateDepth(document.body);
    this.adaptiveProcessor.pageComplexity.domDepth = maxDepth;
    
    // Calculate text node density (text nodes / total elements)
    const textElements = document.querySelectorAll('p, div, span, h1, h2, h3, h4, h5, h6, li, td, th, a');
    const textElementsWithContent = Array.from(textElements).filter(el => 
      el.textContent && el.textContent.trim().length > 0
    );
    this.adaptiveProcessor.pageComplexity.textNodeDensity = 
      textElementsWithContent.length / Math.max(allElements.length, 1);
    
    // Determine complexity level
    const { elementCount, domDepth, textNodeDensity } = this.adaptiveProcessor.pageComplexity;
    const config = EDGE_ONLY_SELECTION.PAGE_COMPLEXITY_ANALYSIS;
    
    if (elementCount > config.ELEMENT_COUNT_THRESHOLD_HIGH || 
        domDepth > config.DOM_DEPTH_THRESHOLD_HIGH) {
      this.adaptiveProcessor.pageComplexity.complexityLevel = 'high';
    } else if (elementCount > config.ELEMENT_COUNT_THRESHOLD_LOW || 
               domDepth > config.DOM_DEPTH_THRESHOLD_LOW) {
      this.adaptiveProcessor.pageComplexity.complexityLevel = 'medium';
    } else {
      this.adaptiveProcessor.pageComplexity.complexityLevel = 'low';
    }
    
    // Adjust processing parameters based on complexity
    this.adjustProcessingForComplexity();
    
    this.adaptiveProcessor.pageComplexity.lastAnalysis = Date.now();
    
    const analysisTime = performance.now() - startTime;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Page complexity analysis completed:', {
        elementCount,
        domDepth,
        textNodeDensity: textNodeDensity.toFixed(3),
        complexityLevel: this.adaptiveProcessor.pageComplexity.complexityLevel,
        analysisTime: analysisTime.toFixed(2) + 'ms',
        adjustedBatchSize: this.adaptiveProcessor.batchProcessor.currentBatchSize,
        adjustedProcessingBudget: this.adaptiveProcessor.frameRateMonitor.processingTimeBudget
      });
    }
  }

  /**
   * Adjust processing parameters based on page complexity (Task 2.1)
   */
  adjustProcessingForComplexity() {
    const complexity = this.adaptiveProcessor.pageComplexity.complexityLevel;
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    const frameMonitor = this.adaptiveProcessor.frameRateMonitor;
    
    switch (complexity) {
      case 'high':
        // Reduce batch size and processing budget for complex pages
        batchProcessor.currentBatchSize = Math.max(
          batchProcessor.minBatchSize,
          Math.floor(batchProcessor.currentBatchSize * 0.6)
        );
        frameMonitor.processingTimeBudget = Math.floor(frameMonitor.processingTimeBudget * 0.7);
        break;
        
      case 'medium':
        // Moderate adjustments
        batchProcessor.currentBatchSize = Math.floor(
          (batchProcessor.minBatchSize + batchProcessor.maxBatchSize) / 2
        );
        frameMonitor.processingTimeBudget = Math.floor(frameMonitor.processingTimeBudget * 0.85);
        break;
        
      case 'low':
        // Allow larger batches and more processing time for simple pages
        batchProcessor.currentBatchSize = Math.min(
          batchProcessor.maxBatchSize,
          Math.floor(batchProcessor.currentBatchSize * 1.2)
        );
        frameMonitor.processingTimeBudget = EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.PROCESSING_TIME_BUDGET_MS;
        break;
    }
    
    // Ensure batch size stays within bounds
    batchProcessor.currentBatchSize = Math.max(
      batchProcessor.minBatchSize,
      Math.min(batchProcessor.maxBatchSize, batchProcessor.currentBatchSize)
    );
  }

  /**
   * Start frame rate monitoring for adaptive processing (Task 2.1)
   */
  startFrameRateMonitoring() {
    const monitor = this.adaptiveProcessor.frameRateMonitor;
    
    const updateFrameRate = () => {
      const currentTime = performance.now();
      
      if (monitor.lastFrameTime > 0) {
        const frameTime = currentTime - monitor.lastFrameTime;
        monitor.frameTimes.push(frameTime);
        
        // Keep only recent frame times
        if (monitor.frameTimes.length > monitor.monitoringWindow) {
          monitor.frameTimes.shift();
        }
        
        // Calculate current FPS
        if (monitor.frameTimes.length > 0) {
          const avgFrameTime = monitor.frameTimes.reduce((a, b) => a + b, 0) / monitor.frameTimes.length;
          monitor.currentFps = Math.min(60, 1000 / avgFrameTime);
        }
        
        // Adjust processing if frame rate drops
        this.adjustProcessingForFrameRate();
      }
      
      monitor.lastFrameTime = currentTime;
      monitor.frameCount++;
      
      // Continue monitoring
      requestAnimationFrame(updateFrameRate);
    };
    
    // Start monitoring
    requestAnimationFrame(updateFrameRate);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Frame rate monitoring started:', {
        targetFps: monitor.targetFps,
        frameTimeBudget: monitor.frameTimeBudget + 'ms',
        processingTimeBudget: monitor.processingTimeBudget + 'ms'
      });
    }
  }

  /**
   * Adjust processing parameters based on frame rate (Task 2.1)
   */
  adjustProcessingForFrameRate() {
    const monitor = this.adaptiveProcessor.frameRateMonitor;
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    const qualityController = this.adaptiveProcessor.qualityController;
    const minAcceptableFps = EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.MIN_ACCEPTABLE_FPS;
    
    if (monitor.currentFps < minAcceptableFps) {
      // Frame rate is too low - reduce processing load
      const adjustmentFactor = EDGE_ONLY_SELECTION.FRAME_RATE_PROCESSING.FRAME_RATE_ADJUSTMENT_FACTOR;
      
      // Reduce batch size
      if (batchProcessor.adaptiveSizing) {
        batchProcessor.currentBatchSize = Math.max(
          batchProcessor.minBatchSize,
          Math.floor(batchProcessor.currentBatchSize * adjustmentFactor)
        );
      }
      
      // Reduce processing time budget
      monitor.processingTimeBudget = Math.max(
        2, // Minimum 2ms
        Math.floor(monitor.processingTimeBudget * adjustmentFactor)
      );
      
      // Lower quality if enabled
      if (qualityController.enabled && qualityController.currentQuality !== 'low') {
        this.adjustProcessingQuality('low');
      }
      
      if (this.debugLogging) {
        console.warn('[KeyPilot Debug] Frame rate adjustment triggered:', {
          currentFps: monitor.currentFps.toFixed(1),
          minAcceptableFps,
          newBatchSize: batchProcessor.currentBatchSize,
          newProcessingBudget: monitor.processingTimeBudget + 'ms',
          newQuality: qualityController.currentQuality
        });
      }
    } else if (monitor.currentFps > monitor.targetFps * 0.9) {
      // Frame rate is good - can increase processing if needed
      if (batchProcessor.currentBatchSize < batchProcessor.maxBatchSize) {
        batchProcessor.currentBatchSize = Math.min(
          batchProcessor.maxBatchSize,
          batchProcessor.currentBatchSize + 1
        );
      }
      
      // Restore quality if it was lowered
      if (qualityController.enabled && qualityController.currentQuality !== 'high') {
        this.adjustProcessingQuality('high');
      }
    }
  }

  /**
   * Initialize batch processing system (Task 2.1)
   */
  initializeBatchProcessing() {
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    
    // Clear any existing timer
    if (batchProcessor.batchTimer) {
      clearTimeout(batchProcessor.batchTimer);
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Batch processing initialized:', {
        enabled: batchProcessor.enabled,
        defaultBatchSize: batchProcessor.currentBatchSize,
        maxBatchSize: batchProcessor.maxBatchSize,
        batchTimeout: batchProcessor.batchTimeout + 'ms',
        adaptiveSizing: batchProcessor.adaptiveSizing
      });
    }
  }

  /**
   * Adjust processing quality based on available time (Task 2.1)
   * @param {string} targetQuality - Target quality level ('high', 'medium', 'low')
   */
  adjustProcessingQuality(targetQuality) {
    const qualityController = this.adaptiveProcessor.qualityController;
    
    if (!qualityController.enabled) {
      return;
    }
    
    // Apply hysteresis to prevent rapid quality changes
    if (qualityController.currentQuality !== targetQuality) {
      qualityController.hysteresisCounter++;
      
      if (qualityController.hysteresisCounter >= qualityController.adjustmentHysteresis) {
        qualityController.currentQuality = targetQuality;
        qualityController.hysteresisCounter = 0;
        
        // Record quality change
        qualityController.qualityHistory.push({
          quality: targetQuality,
          timestamp: Date.now()
        });
        
        // Keep only recent history
        if (qualityController.qualityHistory.length > 10) {
          qualityController.qualityHistory.shift();
        }
        
        if (this.debugLogging) {
          console.log('[KeyPilot Debug] Processing quality adjusted:', {
            newQuality: targetQuality,
            hysteresisCounter: qualityController.hysteresisCounter,
            qualityHistory: qualityController.qualityHistory.length
          });
        }
      }
    } else {
      // Reset hysteresis counter if target matches current
      qualityController.hysteresisCounter = 0;
    }
  }

  /**
   * Initialize predictive caching system (Task 2.2)
   */
  initializePredictiveCaching() {
    // Initialize viewport caching
    if (this.predictiveCaching.viewportCaching.enabled) {
      this.initializeViewportCaching();
    }
    
    // Initialize scroll prediction
    if (this.predictiveCaching.scrollPredictor.enabled) {
      this.initializeScrollPrediction();
    }
    
    // User behavior analysis removed
    
    // Start cache preloading
    this.startCachePreloading();
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Predictive caching initialized:', {
        viewportCaching: this.predictiveCaching.viewportCaching.enabled,
        scrollPrediction: this.predictiveCaching.scrollPredictor.enabled,
        behaviorAnalysis: this.predictiveCaching.behaviorAnalyzer.enabled,
        preloading: this.predictiveCaching.preloader.enabled
      });
    }
  }

  /**
   * Initialize viewport-based caching (Task 2.2)
   */
  initializeViewportCaching() {
    const viewportCaching = this.predictiveCaching.viewportCaching;
    
    // Get initial viewport dimensions
    this.updateViewportInfo();
    
    // Set up viewport change monitoring
    const handleViewportChange = () => {
      const now = Date.now();
      if (now - viewportCaching.lastViewportUpdate > viewportCaching.updateThrottle) {
        this.updateViewportInfo();
        this.warmViewportCache();
        viewportCaching.lastViewportUpdate = now;
      }
    };
    
    // Monitor viewport changes
    window.addEventListener('resize', handleViewportChange);
    window.addEventListener('scroll', handleViewportChange);
    
    // Initial cache warming
    this.warmViewportCache();
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Viewport caching initialized:', {
        viewport: viewportCaching.currentViewport,
        cacheMargin: viewportCaching.cacheMargin,
        cacheSectors: viewportCaching.cacheSectors
      });
    }
  }

  /**
   * Update viewport information (Task 2.2)
   */
  updateViewportInfo() {
    const viewportCaching = this.predictiveCaching.viewportCaching;
    
    viewportCaching.currentViewport = {
      x: window.scrollX,
      y: window.scrollY,
      width: window.innerWidth,
      height: window.innerHeight
    };
  }

  /**
   * Warm cache for elements in and around the viewport (Task 2.2)
   */
  warmViewportCache() {
    const viewportCaching = this.predictiveCaching.viewportCaching;
    const viewport = viewportCaching.currentViewport;
    const margin = viewportCaching.cacheMargin;
    
    // Calculate extended viewport bounds for caching
    const cacheArea = {
      left: viewport.x - margin,
      top: viewport.y - margin,
      right: viewport.x + viewport.width + margin,
      bottom: viewport.y + viewport.height + margin
    };
    
    // Find elements in the cache area
    const elementsInArea = this.findElementsInArea(cacheArea);
    
    // Preload text nodes for elements in cache area
    elementsInArea.forEach(element => {
      if (!this.textNodeCache.has(element)) {
        this.preloadElementTextNodes(element, 'viewport');
      }
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Viewport cache warmed:', {
        cacheArea,
        elementsFound: elementsInArea.length,
        cacheSize: this.cacheSize
      });
    }
  }

  /**
   * Initialize scroll prediction (Task 2.2)
   */
  initializeScrollPrediction() {
    const scrollPredictor = this.predictiveCaching.scrollPredictor;
    
    let lastScrollTime = 0;
    let lastScrollPosition = { x: window.scrollX, y: window.scrollY };
    
    const handleScroll = () => {
      const currentTime = performance.now();
      const currentPosition = { x: window.scrollX, y: window.scrollY };
      
      if (lastScrollTime > 0) {
        const deltaTime = currentTime - lastScrollTime;
        const deltaX = currentPosition.x - lastScrollPosition.x;
        const deltaY = currentPosition.y - lastScrollPosition.y;
        
        // Calculate velocity
        if (deltaTime > 0) {
          const velocityX = (deltaX / deltaTime) * 1000; // px/s
          const velocityY = (deltaY / deltaTime) * 1000; // px/s
          
          this.updateScrollVelocity(velocityX, velocityY);
          this.predictScrollPosition();
          this.preloadForScrollDirection();
        }
      }
      
      lastScrollTime = currentTime;
      lastScrollPosition = currentPosition;
      scrollPredictor.lastScrollTime = currentTime;
    };
    
    // Throttle scroll events
    let scrollTimeout;
    window.addEventListener('scroll', () => {
      if (scrollTimeout) {
        clearTimeout(scrollTimeout);
      }
      scrollTimeout = setTimeout(handleScroll, 16); // ~60fps
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Scroll prediction initialized');
    }
  }

  /**
   * Update scroll velocity tracking (Task 2.2)
   * @param {number} velocityX - Horizontal velocity in px/s
   * @param {number} velocityY - Vertical velocity in px/s
   */
  updateScrollVelocity(velocityX, velocityY) {
    const scrollPredictor = this.predictiveCaching.scrollPredictor;
    
    // Add to velocity history
    scrollPredictor.scrollHistory.push({
      velocityX,
      velocityY,
      timestamp: performance.now()
    });
    
    // Keep only recent samples
    if (scrollPredictor.scrollHistory.length > scrollPredictor.velocitySamples) {
      scrollPredictor.scrollHistory.shift();
    }
    
    // Calculate average velocity
    if (scrollPredictor.scrollHistory.length > 0) {
      const avgVelocityX = scrollPredictor.scrollHistory.reduce((sum, sample) => 
        sum + sample.velocityX, 0) / scrollPredictor.scrollHistory.length;
      const avgVelocityY = scrollPredictor.scrollHistory.reduce((sum, sample) => 
        sum + sample.velocityY, 0) / scrollPredictor.scrollHistory.length;
      
      scrollPredictor.currentVelocity = { x: avgVelocityX, y: avgVelocityY };
    }
  }

  /**
   * Predict future scroll position (Task 2.2)
   */
  predictScrollPosition() {
    const scrollPredictor = this.predictiveCaching.scrollPredictor;
    const currentPosition = { x: window.scrollX, y: window.scrollY };
    const velocity = scrollPredictor.currentVelocity;
    
    // Predict position based on current velocity
    const predictionTimeMs = 500; // Predict 500ms ahead
    const predictionTimeS = predictionTimeMs / 1000;
    
    scrollPredictor.predictedPosition = {
      x: currentPosition.x + (velocity.x * predictionTimeS),
      y: currentPosition.y + (velocity.y * predictionTimeS)
    };
    
    if (this.debugLogging && (Math.abs(velocity.x) > scrollPredictor.minVelocity || 
                              Math.abs(velocity.y) > scrollPredictor.minVelocity)) {
      console.log('[KeyPilot Debug] Scroll prediction updated:', {
        currentPosition,
        velocity,
        predictedPosition: scrollPredictor.predictedPosition,
        predictionTime: predictionTimeMs + 'ms'
      });
    }
  }

  /**
   * Preload elements in predicted scroll direction (Task 2.2)
   */
  preloadForScrollDirection() {
    const scrollPredictor = this.predictiveCaching.scrollPredictor;
    const velocity = scrollPredictor.currentVelocity;
    const minVelocity = scrollPredictor.minVelocity;
    
    // Only preload if scrolling fast enough
    if (Math.abs(velocity.x) < minVelocity && Math.abs(velocity.y) < minVelocity) {
      return;
    }
    
    const currentPosition = { x: window.scrollX, y: window.scrollY };
    const predictionDistance = scrollPredictor.predictionDistance;
    
    // Calculate preload area based on scroll direction
    let preloadArea;
    if (Math.abs(velocity.y) > Math.abs(velocity.x)) {
      // Vertical scrolling
      if (velocity.y > 0) {
        // Scrolling down
        preloadArea = {
          left: currentPosition.x,
          top: currentPosition.y + window.innerHeight,
          right: currentPosition.x + window.innerWidth,
          bottom: currentPosition.y + window.innerHeight + predictionDistance
        };
      } else {
        // Scrolling up
        preloadArea = {
          left: currentPosition.x,
          top: currentPosition.y - predictionDistance,
          right: currentPosition.x + window.innerWidth,
          bottom: currentPosition.y
        };
      }
    } else {
      // Horizontal scrolling
      if (velocity.x > 0) {
        // Scrolling right
        preloadArea = {
          left: currentPosition.x + window.innerWidth,
          top: currentPosition.y,
          right: currentPosition.x + window.innerWidth + predictionDistance,
          bottom: currentPosition.y + window.innerHeight
        };
      } else {
        // Scrolling left
        preloadArea = {
          left: currentPosition.x - predictionDistance,
          top: currentPosition.y,
          right: currentPosition.x,
          bottom: currentPosition.y + window.innerHeight
        };
      }
    }
    
    // Find and preload elements in the predicted area
    const elementsToPreload = this.findElementsInArea(preloadArea);
    elementsToPreload.forEach(element => {
      if (!this.textNodeCache.has(element)) {
        this.preloadElementTextNodes(element, 'scroll_prediction');
      }
    });
    
    if (this.debugLogging && elementsToPreload.length > 0) {
      console.log('[KeyPilot Debug] Scroll-based preloading:', {
        velocity,
        preloadArea,
        elementsPreloaded: elementsToPreload.length
      });
    }
  }

  /**
   * Initialize user behavior analysis (Task 2.2)
   */
  initializeBehaviorAnalysis() {
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    
    // Track rectangle selection patterns
    const originalUpdateRectangle = this.updateRectangle.bind(this);
    this.updateRectangle = (rect) => {
      this.recordUserInteraction('rectangle_update', { rect });
      return originalUpdateRectangle(rect);
    };
    
    // Start pattern analysis
    setInterval(() => {
      this.analyzeBehaviorPatterns();
    }, behaviorAnalyzer.analysisInterval);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Behavior analysis initialized');
    }
  }

  /**
   * Record user interaction for behavior analysis (Task 2.2)
   * @param {string} type - Type of interaction
   * @param {Object} data - Interaction data
   */
  recordUserInteraction(type, data) {
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    
    const interaction = {
      type,
      data,
      timestamp: Date.now(),
      viewport: {
        x: window.scrollX,
        y: window.scrollY,
        width: window.innerWidth,
        height: window.innerHeight
      }
    };
    
    behaviorAnalyzer.interactionHistory.push(interaction);
    
    // Keep only recent interactions
    if (behaviorAnalyzer.interactionHistory.length > behaviorAnalyzer.patternWindow) {
      behaviorAnalyzer.interactionHistory.shift();
    }
  }

  /**
   * Analyze user behavior patterns (Task 2.2)
   */
  analyzeBehaviorPatterns() {
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    const interactions = behaviorAnalyzer.interactionHistory;
    
    if (interactions.length < 3) {
      return; // Need at least 3 interactions to detect patterns
    }
    
    // Analyze rectangle selection patterns
    this.analyzeRectanglePatterns(interactions);
    
    // Analyze scroll patterns
    this.analyzeScrollPatterns(interactions);
    
    // Apply predictive caching based on patterns
    this.applyPredictiveCaching();
    
    behaviorAnalyzer.lastAnalysis = Date.now();
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Behavior patterns analyzed:', {
        interactions: interactions.length,
        patterns: Array.from(behaviorAnalyzer.patterns.entries())
      });
    }
  }

  /**
   * Analyze rectangle selection patterns (Task 2.2)
   * @param {Array} interactions - User interaction history
   */
  analyzeRectanglePatterns(interactions) {
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    const rectangleInteractions = interactions.filter(i => i.type === 'rectangle_update');
    
    if (rectangleInteractions.length < 2) {
      return;
    }
    
    // Analyze rectangle size patterns
    const rectangleSizes = rectangleInteractions.map(i => ({
      width: i.data.rect.width,
      height: i.data.rect.height,
      area: i.data.rect.width * i.data.rect.height
    }));
    
    // Calculate average rectangle size
    const avgArea = rectangleSizes.reduce((sum, size) => sum + size.area, 0) / rectangleSizes.length;
    const sizeVariance = rectangleSizes.reduce((sum, size) => 
      sum + Math.pow(size.area - avgArea, 2), 0) / rectangleSizes.length;
    
    // Low variance indicates consistent rectangle sizes
    const consistentSizing = sizeVariance < (avgArea * 0.1);
    if (consistentSizing) {
      behaviorAnalyzer.patterns.set('consistent_rectangle_size', 0.8);
    }
    
    // Analyze rectangle position patterns
    const positions = rectangleInteractions.map(i => ({
      x: i.data.rect.left,
      y: i.data.rect.top
    }));
    
    // Check for repeated areas
    const positionClusters = this.findPositionClusters(positions);
    if (positionClusters.length > 0) {
      behaviorAnalyzer.patterns.set('repeated_areas', 0.7);
    }
  }

  /**
   * Find position clusters in user interactions (Task 2.2)
   * @param {Array} positions - Array of {x, y} positions
   * @returns {Array} - Array of position clusters
   */
  findPositionClusters(positions) {
    const clusters = [];
    const clusterRadius = 100; // pixels
    
    positions.forEach(pos => {
      let addedToCluster = false;
      
      for (const cluster of clusters) {
        const distance = Math.sqrt(
          Math.pow(pos.x - cluster.center.x, 2) + 
          Math.pow(pos.y - cluster.center.y, 2)
        );
        
        if (distance <= clusterRadius) {
          cluster.positions.push(pos);
          // Update cluster center
          cluster.center.x = cluster.positions.reduce((sum, p) => sum + p.x, 0) / cluster.positions.length;
          cluster.center.y = cluster.positions.reduce((sum, p) => sum + p.y, 0) / cluster.positions.length;
          addedToCluster = true;
          break;
        }
      }
      
      if (!addedToCluster) {
        clusters.push({
          center: { x: pos.x, y: pos.y },
          positions: [pos]
        });
      }
    });
    
    // Return clusters with multiple positions
    return clusters.filter(cluster => cluster.positions.length > 1);
  }

  /**
   * Analyze scroll patterns (Task 2.2)
   * @param {Array} interactions - User interaction history
   */
  analyzeScrollPatterns(interactions) {
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    
    // Analyze viewport changes to detect scroll patterns
    const viewportChanges = interactions.map(i => i.viewport);
    
    if (viewportChanges.length < 3) {
      return;
    }
    
    // Detect consistent scroll directions
    let verticalScrolls = 0;
    let horizontalScrolls = 0;
    
    for (let i = 1; i < viewportChanges.length; i++) {
      const prev = viewportChanges[i - 1];
      const curr = viewportChanges[i];
      
      const deltaY = Math.abs(curr.y - prev.y);
      const deltaX = Math.abs(curr.x - prev.x);
      
      if (deltaY > 10) verticalScrolls++;
      if (deltaX > 10) horizontalScrolls++;
    }
    
    if (verticalScrolls > horizontalScrolls * 2) {
      behaviorAnalyzer.patterns.set('vertical_scrolling_preference', 0.8);
    } else if (horizontalScrolls > verticalScrolls * 2) {
      behaviorAnalyzer.patterns.set('horizontal_scrolling_preference', 0.8);
    }
  }

  /**
   * Apply predictive caching based on detected patterns (Task 2.2)
   */
  applyPredictiveCaching() {
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    const patterns = behaviorAnalyzer.patterns;
    
    // Apply caching strategies based on patterns
    patterns.forEach((confidence, pattern) => {
      if (confidence >= behaviorAnalyzer.minConfidence) {
        switch (pattern) {
          case 'consistent_rectangle_size':
            this.preloadForConsistentSizes();
            break;
          case 'repeated_areas':
            this.preloadRepeatedAreas();
            break;
          case 'vertical_scrolling_preference':
            this.preloadVerticalScrollAreas();
            break;
          case 'horizontal_scrolling_preference':
            this.preloadHorizontalScrollAreas();
            break;
        }
      }
    });
  }

  /**
   * Start cache preloading system (Task 2.2)
   */
  startCachePreloading() {
    const preloader = this.predictiveCaching.preloader;
    
    const processPreloadQueue = () => {
      if (preloader.preloadQueue.length === 0) {
        return;
      }
      
      const now = Date.now();
      if (now - preloader.lastPreloadTime < preloader.throttleMs) {
        return;
      }
      
      // Process a batch of preload requests
      const batch = preloader.preloadQueue.splice(0, preloader.batchSize);
      batch.forEach(request => {
        this.executePreloadRequest(request);
      });
      
      preloader.lastPreloadTime = now;
      
      if (this.debugLogging && batch.length > 0) {
        console.log('[KeyPilot Debug] Preload batch processed:', {
          batchSize: batch.length,
          remainingQueue: preloader.preloadQueue.length
        });
      }
    };
    
    // Process preload queue regularly
    preloader.preloadTimer = setInterval(processPreloadQueue, preloader.throttleMs);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Cache preloading started');
    }
  }

  /**
   * Start cache management and monitoring timers
   */
  startCacheManagement() {
    // Start memory monitoring if enabled
    if (EDGE_ONLY_SELECTION.ENABLE_MEMORY_MONITORING) {
      this.memoryMonitoringTimer = setInterval(() => {
        this.monitorMemoryUsage();
      }, EDGE_ONLY_SELECTION.MEMORY_CHECK_INTERVAL);
    }
    
    // Start cache optimization timer
    this.cacheManagementTimer = setInterval(() => {
      this.optimizeCacheEfficiency();
    }, EDGE_ONLY_SELECTION.PERFORMANCE_MONITORING_INTERVAL);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Cache management timers started:', {
        memoryMonitoring: EDGE_ONLY_SELECTION.ENABLE_MEMORY_MONITORING,
        memoryCheckInterval: EDGE_ONLY_SELECTION.MEMORY_CHECK_INTERVAL + 'ms',
        cacheOptimizationInterval: EDGE_ONLY_SELECTION.PERFORMANCE_MONITORING_INTERVAL + 'ms'
      });
    }
  }

  /**
   * Create a dynamic root element that represents the selection rectangle
   */
  createRectangleRoot() {
    // Create an invisible element that will serve as the intersection root
    this.rectangleRoot = document.createElement('div');
    this.rectangleRoot.id = 'kpv2-rectangle-intersection-root';
    this.rectangleRoot.style.cssText = `
      position: fixed;
      pointer-events: none;
      z-index: -1;
      background: transparent;
      border: none;
      visibility: hidden;
      left: 0px;
      top: 0px;
      width: 0px;
      height: 0px;
    `;
    
    document.body.appendChild(this.rectangleRoot);
    
    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Rectangle intersection root created');
    }
  }

  /**
   * Setup the intersection observer for text elements
   */
  setupTextObserver() {
    try {
      this.textObserver = new IntersectionObserver(
        (entries) => this.handleIntersectionChanges(entries),
        {
          root: this.rectangleRoot,
          rootMargin: '0px',
          threshold: [0, 0.1, 0.5, 1.0] // Multiple thresholds for better granularity
        }
      );
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Text intersection observer created');
      }
    } catch (error) {
      console.warn('[KeyPilot] Failed to create IntersectionObserver for rectangle selection:', error);
      this.textObserver = null;
    }
  }

  /**
   * Handle intersection changes from the observer - ENHANCED EDGE-ONLY PROCESSING WITH ADAPTIVE PROCESSING
   * Only processes elements entering/leaving rectangle boundaries for maximum efficiency
   * @param {Array} entries - Intersection observer entries
   */
  handleIntersectionChanges(entries) {
    this.metrics.intersectionUpdates++;
    this.metrics.lastUpdateTime = performance.now();
    
    // Use adaptive processing if enabled (Task 2.1)
    if (this.adaptiveProcessor.enabled) {
      this.handleAdaptiveIntersectionChanges(entries);
    } else {
      this.handleStandardIntersectionChanges(entries);
    }
  }

  /**
   * Handle intersection changes with adaptive processing (Task 2.1)
   * @param {Array} entries - Intersection observer entries
   */
  handleAdaptiveIntersectionChanges(entries) {
    const startTime = performance.now();
    const frameMonitor = this.adaptiveProcessor.frameRateMonitor;
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    const qualityController = this.adaptiveProcessor.qualityController;
    
    // Check if we have time budget for processing
    const availableTime = frameMonitor.processingTimeBudget;
    const estimatedProcessingTime = entries.length * 2; // Rough estimate: 2ms per entry
    
    // Adjust quality based on available time
    if (qualityController.enabled) {
      if (estimatedProcessingTime > qualityController.thresholds.medium) {
        this.adjustProcessingQuality('low');
      } else if (estimatedProcessingTime > qualityController.thresholds.high) {
        this.adjustProcessingQuality('medium');
      } else {
        this.adjustProcessingQuality('high');
      }
    }
    
    // Use batch processing if enabled and we have multiple entries
    if (batchProcessor.enabled && entries.length > batchProcessor.currentBatchSize) {
      this.processBatchedIntersectionChanges(entries, startTime, availableTime);
    } else {
      this.processImmediateIntersectionChanges(entries, startTime, availableTime);
    }
    
    // Update page complexity periodically
    const now = Date.now();
    if (now - this.adaptiveProcessor.pageComplexity.lastAnalysis > 
        this.adaptiveProcessor.pageComplexity.analysisInterval) {
      // Schedule complexity analysis for next frame to avoid blocking
      requestAnimationFrame(() => this.analyzePageComplexity());
    }
  }

  /**
   * Process intersection changes in batches (Task 2.1)
   * @param {Array} entries - Intersection observer entries
   * @param {number} startTime - Processing start time
   * @param {number} availableTime - Available processing time budget
   */
  processBatchedIntersectionChanges(entries, startTime, availableTime) {
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    const qualityController = this.adaptiveProcessor.qualityController;
    
    // Add entries to pending batch
    batchProcessor.pendingBatch.push(...entries);
    
    // Clear existing batch timer
    if (batchProcessor.batchTimer) {
      clearTimeout(batchProcessor.batchTimer);
    }
    
    // Process batch immediately if it's full or we're in low quality mode
    const shouldProcessImmediately = 
      batchProcessor.pendingBatch.length >= batchProcessor.currentBatchSize ||
      qualityController.currentQuality === 'low';
    
    if (shouldProcessImmediately) {
      this.processPendingBatch(startTime, availableTime);
    } else {
      // Schedule batch processing
      batchProcessor.batchTimer = setTimeout(() => {
        this.processPendingBatch(performance.now(), availableTime);
      }, batchProcessor.batchTimeout);
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Batch processing scheduled:', {
        entriesAdded: entries.length,
        pendingBatchSize: batchProcessor.pendingBatch.length,
        currentBatchSize: batchProcessor.currentBatchSize,
        processedImmediately: shouldProcessImmediately,
        qualityLevel: qualityController.currentQuality
      });
    }
  }

  /**
   * Process the pending batch of intersection changes (Task 2.1)
   * @param {number} startTime - Processing start time
   * @param {number} availableTime - Available processing time budget
   */
  processPendingBatch(startTime, availableTime) {
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    const qualityController = this.adaptiveProcessor.qualityController;
    
    if (batchProcessor.pendingBatch.length === 0) {
      return;
    }
    
    // Take entries to process
    const entriesToProcess = batchProcessor.pendingBatch.splice(0, batchProcessor.currentBatchSize);
    
    // Apply quality-based processing limits
    let maxElementsToProcess = entriesToProcess.length;
    if (qualityController.enabled && qualityController.currentQuality === 'low') {
      maxElementsToProcess = Math.min(
        maxElementsToProcess,
        qualityController.thresholds.lowProcessingLimit
      );
    }
    
    // Process the batch
    const processedEntries = entriesToProcess.slice(0, maxElementsToProcess);
    this.processIntersectionEntries(processedEntries, startTime);
    
    // If there are remaining entries in the batch, schedule them for next frame
    if (batchProcessor.pendingBatch.length > 0) {
      requestAnimationFrame(() => {
        this.processPendingBatch(performance.now(), availableTime);
      });
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Batch processed:', {
        entriesProcessed: processedEntries.length,
        remainingInBatch: batchProcessor.pendingBatch.length,
        qualityLevel: qualityController.currentQuality,
        maxElementsLimit: maxElementsToProcess,
        processingTime: (performance.now() - startTime).toFixed(2) + 'ms'
      });
    }
  }

  /**
   * Process intersection changes immediately without batching (Task 2.1)
   * @param {Array} entries - Intersection observer entries
   * @param {number} startTime - Processing start time
   * @param {number} availableTime - Available processing time budget
   */
  processImmediateIntersectionChanges(entries, startTime, availableTime) {
    const qualityController = this.adaptiveProcessor.qualityController;
    
    // Apply quality-based processing limits
    let maxElementsToProcess = entries.length;
    if (qualityController.enabled && qualityController.currentQuality === 'low') {
      maxElementsToProcess = Math.min(
        maxElementsToProcess,
        qualityController.thresholds.lowProcessingLimit
      );
    }
    
    // Process entries up to the limit
    const entriesToProcess = entries.slice(0, maxElementsToProcess);
    this.processIntersectionEntries(entriesToProcess, startTime);
    
    // If we had to skip entries due to quality limits, log it
    if (maxElementsToProcess < entries.length && this.debugLogging) {
      console.log('[KeyPilot Debug] Quality-limited processing:', {
        totalEntries: entries.length,
        processedEntries: maxElementsToProcess,
        skippedEntries: entries.length - maxElementsToProcess,
        qualityLevel: qualityController.currentQuality
      });
    }
  }

  /**
   * Process intersection entries with standard logic (Task 2.1)
   * @param {Array} entries - Intersection observer entries to process
   * @param {number} startTime - Processing start time
   */
  processIntersectionEntries(entries, startTime) {
    let elementsProcessed = 0;
    let elementsEntering = 0;
    let elementsLeaving = 0;
    let hasChanges = false;
    
    // Enhanced edge-only processing with proper state management
    entries.forEach(entry => {
      const element = entry.target;
      const elementId = this.getElementId(element);
      const wasIntersecting = this.elementStates.get(elementId) || false;
      const isIntersecting = entry.isIntersecting;
      
      elementsProcessed++;
      
      // Only process actual state changes (entering/leaving)
      if (isIntersecting && !wasIntersecting) {
        // Element ENTERING rectangle - use edge-level character detection (Task 2.2)
        this.elementStates.set(elementId, true);
        this.intersectingTextElements.add(element);
        
        if (this.enhancedIntegrationEnabled && this.edgeCharacterDetectionEnabled && this.edgeCharacterDetector) {
          this.handleElementEnteringWithCharacterDetection(element);
        } else {
          this.extractTextNodesFromElement(element);
        }
        
        elementsEntering++;
        hasChanges = true;
        
        if (this.debugLogging) {
          console.log('[KeyPilot Debug] Element ENTERING rectangle:', {
            element: element.tagName,
            className: element.className || 'none',
            id: element.id || 'none',
            textLength: element.textContent?.length || 0,
            boundingRect: element.getBoundingClientRect(),
            intersectionRatio: entry.intersectionRatio
          });
        }
      } else if (!isIntersecting && wasIntersecting) {
        // Element LEAVING rectangle - use edge-level character detection (Task 2.2)
        this.elementStates.set(elementId, false);
        this.intersectingTextElements.delete(element);
        
        if (this.enhancedIntegrationEnabled && this.edgeCharacterDetectionEnabled && this.edgeCharacterDetector) {
          this.handleElementLeavingWithCharacterDetection(element);
        } else {
          this.removeTextNodesFromElement(element);
        }
        
        elementsLeaving++;
        hasChanges = true;
        
        if (this.debugLogging) {
          console.log('[KeyPilot Debug] Element LEAVING rectangle:', {
            element: element.tagName,
            className: element.className || 'none',
            id: element.id || 'none',
            textLength: element.textContent?.length || 0,
            intersectionRatio: entry.intersectionRatio
          });
        }
      }
      // Ignore elements that remain in the same state (no processing needed)
    });
    
    const processingTime = performance.now() - startTime;
    
    // Update comprehensive metrics
    this.metrics.edgeProcessingTime = processingTime;
    this.metrics.edgeElementsProcessed = elementsProcessed;
    this.metrics.elementsEntering += elementsEntering;
    this.metrics.elementsLeaving += elementsLeaving;
    this.metrics.actualEdgeProcessingTime += processingTime;
    
    // Performance monitoring removed
    
    // Calculate performance comparison
    this.updatePerformanceComparison(elementsProcessed, processingTime);
    
    // Notify callback if there were changes
    if (hasChanges && this.onIntersectionChange) {
      this.onIntersectionChange({
        intersectingElements: Array.from(this.intersectingTextElements),
        intersectingTextNodes: Array.from(this.intersectingTextNodes),
        metrics: this.getMetrics(),
        edgeProcessing: {
          elementsProcessed,
          elementsEntering,
          elementsLeaving,
          processingTime,
          efficiency: elementsProcessed > 0 ? (processingTime / elementsProcessed).toFixed(2) + 'ms/element' : 'N/A',
          performanceGain: this.calculatePerformanceGain()
        },
        adaptiveProcessing: this.adaptiveProcessor.enabled ? {
          pageComplexity: this.adaptiveProcessor.pageComplexity.complexityLevel,
          currentFps: this.adaptiveProcessor.frameRateMonitor.currentFps.toFixed(1),
          batchSize: this.adaptiveProcessor.batchProcessor.currentBatchSize,
          qualityLevel: this.adaptiveProcessor.qualityController.currentQuality
        } : null
      });
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Enhanced edge-only intersection update with adaptive processing:', {
        entriesProcessed: entries.length,
        elementsProcessed,
        elementsEntering,
        elementsLeaving,
        processingTime: processingTime.toFixed(2) + 'ms',
        intersectingElements: this.intersectingTextElements.size,
        intersectingTextNodes: this.intersectingTextNodes.size,
        hasChanges,
        efficiency: elementsProcessed > 0 ? (processingTime / elementsProcessed).toFixed(2) + 'ms/element' : 'N/A',
        cacheHitRatio: this.getCacheHitRatio(),
        performanceGain: this.calculatePerformanceGain(),
        adaptiveProcessing: this.adaptiveProcessor.enabled ? {
          pageComplexity: this.adaptiveProcessor.pageComplexity.complexityLevel,
          currentFps: this.adaptiveProcessor.frameRateMonitor.currentFps.toFixed(1),
          batchSize: this.adaptiveProcessor.batchProcessor.currentBatchSize,
          qualityLevel: this.adaptiveProcessor.qualityController.currentQuality
        } : null
      });
    }
  }

  /**
   * Handle intersection changes with standard processing (fallback)
   * @param {Array} entries - Intersection observer entries
   */
  handleStandardIntersectionChanges(entries) {
    const startTime = performance.now();
    this.processIntersectionEntries(entries, startTime);
  }

  /**
   * Extract text nodes from an element and add to intersection set
   * ENHANCED EDGE-ONLY OPTIMIZATION: Only called for elements entering rectangle
   * @param {Element} element - Element to extract text nodes from
   */
  extractTextNodesFromElement(element) {
    const startTime = performance.now();
    
    // Check cache first - major performance optimization
    if (this.textNodeCache.has(element)) {
      const cachedNodes = this.textNodeCache.get(element);
      cachedNodes.forEach(node => this.intersectingTextNodes.add(node));
      this.metrics.cacheHits++;
      
      // Update cache hit streak tracking
      this.cacheEfficiencyMetrics.cacheHitStreak++;
      this.cacheEfficiencyMetrics.cacheMissStreak = 0;
      this.cacheEfficiencyMetrics.longestHitStreak = Math.max(
        this.cacheEfficiencyMetrics.longestHitStreak,
        this.cacheEfficiencyMetrics.cacheHitStreak
      );
      
      // Update LRU access order
      this.updateCacheAccess(element);
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Used cached text nodes (FAST PATH):', {
          element: element.tagName,
          className: element.className || 'none',
          cachedNodes: cachedNodes.length,
          processingTime: (performance.now() - startTime).toFixed(2) + 'ms',
          cacheHitRatio: this.getCacheHitRatio(),
          cacheSize: this.cacheSize,
          hitStreak: this.cacheEfficiencyMetrics.cacheHitStreak
        });
      }
      return;
    }
    
    // Cache miss - perform TreeWalker traversal
    this.metrics.cacheMisses++;
    
    // Update cache miss streak tracking
    this.cacheEfficiencyMetrics.cacheMissStreak++;
    this.cacheEfficiencyMetrics.cacheHitStreak = 0;
    this.cacheEfficiencyMetrics.longestMissStreak = Math.max(
      this.cacheEfficiencyMetrics.longestMissStreak,
      this.cacheEfficiencyMetrics.cacheMissStreak
    );
    const textNodes = [];
    const walker = document.createTreeWalker(
      element,
      NodeFilter.SHOW_TEXT,
      {
        acceptNode: (node) => {
          // Skip empty or whitespace-only text nodes
          if (!node.textContent || !node.textContent.trim()) {
            return NodeFilter.FILTER_REJECT;
          }
          
          // Skip text nodes in non-content elements
          const parent = node.parentElement;
          if (parent && this.isNonContentElement(parent)) {
            return NodeFilter.FILTER_REJECT;
          }
          
          return NodeFilter.FILTER_ACCEPT;
        }
      }
    );
    
    let node;
    while (node = walker.nextNode()) {
      textNodes.push(node);
      this.intersectingTextNodes.add(node);
    }
    
    // Cache the text nodes for this element - critical for performance
    this.cacheTextNodes(element, textNodes);
    
    const processingTime = performance.now() - startTime;
    this.metrics.textNodesFound += textNodes.length;
    this.metrics.totalTraversalTime += processingTime;
    this.metrics.traversalCount++;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Extracted text nodes from NEW element (SLOW PATH):', {
        element: element.tagName,
        className: element.className || 'none',
        textNodesFound: textNodes.length,
        processingTime: processingTime.toFixed(2) + 'ms',
        totalTextNodes: this.intersectingTextNodes.size,
        avgTraversalTime: (this.metrics.totalTraversalTime / this.metrics.traversalCount).toFixed(2) + 'ms',
        cacheHitRatio: this.getCacheHitRatio(),
        cacheEfficiency: this.calculateCacheEfficiency()
      });
    }
  }

  /**
   * Remove text nodes from an element from the intersection set
   * @param {Element} element - Element to remove text nodes from
   */
  removeTextNodesFromElement(element) {
    const cachedNodes = this.textNodeCache.get(element);
    if (cachedNodes) {
      cachedNodes.forEach(node => this.intersectingTextNodes.delete(node));
    }
  }

  /**
   * Check if an element is a non-content element that should be excluded
   * @param {Element} element - Element to check
   * @returns {boolean} - True if element should be excluded
   */
  isNonContentElement(element) {
    const tagName = (element.tagName || '').toLowerCase();
    const excludedTags = ['script', 'style', 'meta', 'link', 'title', 'noscript', 'template'];
    
    if (tagName && excludedTags.includes(tagName)) {
      return true;
    }
    
    // Check for common non-content classes
    // `element.className` is not always a string (e.g. SVG uses SVGAnimatedString).
    // Normalize to a safe string so `.includes()` doesn't throw.
    const rawClassName = element.className;
    const className =
      typeof rawClassName === 'string'
        ? rawClassName
        : rawClassName && typeof rawClassName.baseVal === 'string'
          ? rawClassName.baseVal
          : (typeof element.getAttribute === 'function' ? (element.getAttribute('class') || '') : '');
    const excludedClasses = ['footer', 'header', 'nav', 'sidebar', 'advertisement'];
    
    return excludedClasses.some(cls => className.includes(cls));
  }

  /**
   * Generate a unique identifier for an element for state tracking
   * @param {Element} element - Element to generate ID for
   * @returns {string} - Unique element identifier
   */
  getElementId(element) {
    // Use element reference as key - most reliable for state tracking
    if (!element._keypilotId) {
      element._keypilotId = 'kp_' + Math.random().toString(36).substr(2, 9);
    }
    return element._keypilotId;
  }

  /**
   * Update performance comparison metrics
   * @param {number} elementsProcessed - Number of elements processed in this update
   * @param {number} processingTime - Time taken for this update
   */
  updatePerformanceComparison(elementsProcessed, processingTime) {
    // Estimate what full DOM traversal would have cost
    const avgElementTime = this.metrics.traversalCount > 0 
      ? this.metrics.totalTraversalTime / this.metrics.traversalCount 
      : 5; // Default estimate: 5ms per element
    
    const estimatedFullTime = avgElementTime * this.metrics.elementsObserved;
    this.metrics.estimatedFullTraversalTime += estimatedFullTime;
    
    // Calculate efficiency gain
    if (this.metrics.estimatedFullTraversalTime > 0) {
      this.metrics.efficiencyGainPercent = 
        ((this.metrics.estimatedFullTraversalTime - this.metrics.actualEdgeProcessingTime) / 
         this.metrics.estimatedFullTraversalTime * 100);
    }
    
    // Record performance comparison (Task 2.3)
    if (this.enhancedIntegrationEnabled && this.performanceMonitoringEnabled) {
      this.recordPerformanceComparison({
        estimatedTraditionalTime: estimatedFullTime,
        actualOptimizedTime: processingTime
      });
    }
  }

  /**
   * Calculate current cache hit ratio
   * @returns {string} - Cache hit ratio as percentage
   */
  getCacheHitRatio() {
    const totalCacheAttempts = this.metrics.cacheHits + this.metrics.cacheMisses;
    if (totalCacheAttempts === 0) return '0%';
    
    return ((this.metrics.cacheHits / totalCacheAttempts) * 100).toFixed(1) + '%';
  }

  /**
   * Calculate cache efficiency metrics
   * @returns {Object} - Cache efficiency data
   */
  calculateCacheEfficiency() {
    return this.getCacheEfficiencyMetrics();
  }

  /**
   * Calculate overall performance gain from edge-only processing
   * @returns {string} - Performance gain description
   */
  calculatePerformanceGain() {
    if (this.metrics.estimatedFullTraversalTime === 0) {
      return 'Calculating...';
    }
    
    const actualTime = this.metrics.actualEdgeProcessingTime;
    const estimatedTime = this.metrics.estimatedFullTraversalTime;
    
    if (estimatedTime <= actualTime) {
      return 'No gain detected';
    }
    
    const gainPercent = ((estimatedTime - actualTime) / estimatedTime * 100);
    const speedupFactor = estimatedTime / actualTime;
    
    return `${gainPercent.toFixed(1)}% faster (${speedupFactor.toFixed(1)}x speedup)`;
  }

  /**
   * Get element reduction metrics from smart targeting (Task 2.1)
   * @returns {Object} Element reduction performance metrics
   */
  getElementReductionMetrics() {
    if (!this.enhancedIntegrationEnabled || !this.textElementFilter) {
      return {
        totalElementsScanned: 0,
        elementsObserved: this.metrics.elementsObserved,
        elementsSkipped: 0,
        reductionPercentage: 0,
        smartTargetingEnabled: false,
        averageProcessingTime: 0,
        totalProcessingTime: 0,
        dynamicElementsAdded: 0
      };
    }
    
    const filterStats = this.textElementFilter.getStats();
    
    // Update metrics with latest filter stats
    this.metrics.totalElementsScanned = filterStats.totalElementsScanned;
    this.metrics.elementsSkipped = filterStats.elementsSkipped;
    this.metrics.elementReductionPercentage = parseFloat(filterStats.reductionPercentage);
    this.metrics.smartTargetingTime = filterStats.totalProcessingTime;
    
    return {
      totalElementsScanned: this.metrics.totalElementsScanned,
      elementsObserved: this.metrics.elementsObserved,
      elementsSkipped: this.metrics.elementsSkipped,
      reductionPercentage: this.metrics.elementReductionPercentage,
      smartTargetingEnabled: this.smartTargetingEnabled,
      averageProcessingTime: filterStats.averageProcessingTime,
      totalProcessingTime: this.metrics.smartTargetingTime,
      dynamicElementsAdded: this.metrics.dynamicElementsAdded
    };
  }

  /**
   * Get character detection metrics from edge-level processing (Task 2.2)
   * @returns {Object} Character detection performance metrics
   */
  getCharacterDetectionMetrics() {
    const detectorStats = this.edgeCharacterDetector.getStats();
    
    // Update metrics with latest detector stats
    this.metrics.characterDetectionTime = detectorStats.totalProcessingTime;
    this.metrics.characterCacheHits = detectorStats.cacheHits;
    this.metrics.characterCacheMisses = detectorStats.cacheMisses;
    
    return {
      charactersDetected: this.metrics.charactersDetected,
      characterDetectionEnabled: this.edgeCharacterDetectionEnabled,
      cacheHits: this.metrics.characterCacheHits,
      cacheMisses: this.metrics.characterCacheMisses,
      cacheHitRate: detectorStats.cacheHitRate,
      averageProcessingTime: detectorStats.averageProcessingTime,
      totalProcessingTime: this.metrics.characterDetectionTime,
      elementsProcessed: detectorStats.elementsProcessed
    };
  }

  /**
   * Get comprehensive performance metrics from PerformanceMonitor (Task 2.3)
   * @returns {Object} Comprehensive performance metrics
   */
  getPerformanceMetrics() {
    return this.getMetrics();
  }

  /**
   * Get performance comparison statistics (Task 2.3)
   * @returns {Object} Performance comparison between optimized and traditional approaches
   */
  getPerformanceComparison() {
    return this.getComparisonStats();
  }

  /**
   * Get trend analysis results (Task 2.3)
   * @returns {Object} Performance trend analysis
   */
  getPerformanceTrends() {
    return this.getTrendAnalysis();
  }

  /**
   * Export complete performance data for analysis (Task 2.3)
   * @returns {Object} Complete performance data export
   */
  exportPerformanceData() {
    if (!this.performanceMonitoringEnabled) {
      return null;
    }
    
    return {
      performanceData: this.exportData(),
      elementReduction: this.getElementReductionMetrics(),
      characterDetection: this.getCharacterDetectionMetrics(),
      legacyMetrics: this.getMetrics(),
      configuration: {
        smartTargetingEnabled: this.smartTargetingEnabled,
        edgeCharacterDetectionEnabled: this.edgeCharacterDetectionEnabled,
        performanceMonitoringEnabled: this.performanceMonitoringEnabled
      }
    };
  }

  /**
   * Update the rectangle bounds for intersection detection
   * @param {Object} rect - Rectangle with left, top, width, height properties
   */
  updateRectangle(rect) {
    if (!this.rectangleRoot) {
      return;
    }
    
    // Store current rectangle for character detection (Task 2.2)
    this.currentRectangle = {
      left: rect.left,
      top: rect.top,
      right: rect.left + rect.width,
      bottom: rect.top + rect.height,
      width: rect.width,
      height: rect.height
    };
    
    // Update the root element to match the selection rectangle
    this.rectangleRoot.style.left = `${rect.left}px`;
    this.rectangleRoot.style.top = `${rect.top}px`;
    this.rectangleRoot.style.width = `${rect.width}px`;
    this.rectangleRoot.style.height = `${rect.height}px`;
    
    this.lastRectangleUpdate = performance.now();
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Rectangle bounds updated:', {
        left: rect.left,
        top: rect.top,
        width: rect.width,
        height: rect.height,
        observedElements: this.metrics.elementsObserved,
        characterDetectionEnabled: this.edgeCharacterDetectionEnabled
      });
    }
  }

  /**
   * Discover and observe all text-containing elements in the document
   * Uses smart element targeting to reduce observed elements by 95%+
   */
  discoverTextElements() {
    if (!this.textObserver) {
      return;
    }
    
    const startTime = performance.now();
    let textElements;
    
    if (this.enhancedIntegrationEnabled && this.smartTargetingEnabled && this.textElementFilter) {
      // Use TextElementFilter for smart targeting (Task 2.1)
      textElements = this.textElementFilter.findTextElements(document.body);
      
      // Track performance metrics for element reduction
      const filterStats = this.textElementFilter.getStats();
      this.metrics.elementsObserved = 0; // Reset counter
      
      textElements.forEach(element => {
        this.textObserver.observe(element);
        this.observedElements.add(element);
        this.metrics.elementsObserved++;
      });
      
      // Record smart targeting performance (Task 2.3)
      if (this.enhancedIntegrationEnabled && this.performanceMonitoringEnabled) {
        this.recordSmartTargeting({
          elementReductionPercentage: parseFloat(filterStats.reductionPercentage),
          smartTargetingTime: performance.now() - startTime,
          dynamicElementsAdded: 0
        });
      }
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Smart element targeting completed:', {
          totalElementsScanned: filterStats.totalElementsScanned,
          textElementsFound: filterStats.textElementsFound,
          reductionPercentage: filterStats.reductionPercentage,
          observedElements: this.metrics.elementsObserved,
          processingTime: (performance.now() - startTime).toFixed(2) + 'ms'
        });
      }
    } else {
      // Fallback to original selector-based approach
      const textContainerSelectors = [
        'p', 'div', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
        'li', 'td', 'th', 'blockquote', 'pre', 'code',
        'a', 'strong', 'em', 'b', 'i', 'u',
        'article', 'section', 'main', 'aside',
        '[contenteditable]'
      ].join(', ');
      
      const allElements = document.querySelectorAll(textContainerSelectors);
      textElements = Array.from(allElements).filter(element => this.elementContainsText(element));
      
      textElements.forEach(element => {
        this.textObserver.observe(element);
        this.observedElements.add(element);
        this.metrics.elementsObserved++;
      });
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Traditional element discovery completed:', {
          totalElements: allElements.length,
          observedElements: this.metrics.elementsObserved,
          processingTime: (performance.now() - startTime).toFixed(2) + 'ms'
        });
      }
    }
  }

  /**
   * Handle element entering rectangle with edge-level character detection (Task 2.2)
   * @param {Element} element - Element entering the rectangle
   */
  handleElementEnteringWithCharacterDetection(element) {
    if (!this.currentRectangle) {
      // Fallback to traditional method if no rectangle is set
      this.extractTextNodesFromElement(element);
      return;
    }

    const startTime = performance.now();
    
    try {
      // Detect characters at the boundary level
      const intersectingCharacters = this.edgeCharacterDetector.detectAtBoundary(
        element, 
        this.currentRectangle
      );
      
      // Add characters to selection
      this.addCharactersToSelection(intersectingCharacters);
      
      // Update metrics
      const processingTime = performance.now() - startTime;
      this.metrics.edgeProcessingTime += processingTime;
      
      // Record character detection performance (Task 2.3)
      if (this.enhancedIntegrationEnabled && this.performanceMonitoringEnabled) {
        const detectorStats = this.edgeCharacterDetector.getStats();
        this.recordCharacterDetection({
          charactersDetected: intersectingCharacters.length,
          detectionTime: processingTime,
          cacheHits: detectorStats.cacheHits,
          cacheMisses: detectorStats.cacheMisses
        });
      }
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Element entering with character detection:', {
          element: element.tagName,
          className: element.className || 'none',
          charactersDetected: intersectingCharacters.length,
          processingTime: processingTime.toFixed(2) + 'ms',
          cacheHitRate: this.edgeCharacterDetector.getStats().cacheHitRate
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error in character detection, falling back to traditional method:', error);
      this.extractTextNodesFromElement(element);
    }
  }

  /**
   * Handle element leaving rectangle with edge-level character detection (Task 2.2)
   * @param {Element} element - Element leaving the rectangle
   */
  handleElementLeavingWithCharacterDetection(element) {
    const startTime = performance.now();
    
    try {
      // Remove all characters from this element from selection
      this.removeElementCharactersFromSelection(element);
      
      // Update metrics
      const processingTime = performance.now() - startTime;
      this.metrics.edgeProcessingTime += processingTime;
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Element leaving with character detection:', {
          element: element.tagName,
          className: element.className || 'none',
          processingTime: processingTime.toFixed(2) + 'ms'
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error in character removal, falling back to traditional method:', error);
      this.removeTextNodesFromElement(element);
    }
  }

  /**
   * Add characters to the current selection (Task 2.2)
   * @param {Array<Object>} characters - Array of character objects to add
   */
  addCharactersToSelection(characters) {
    characters.forEach(character => {
      // Add the text node containing this character to the intersection set
      this.intersectingTextNodes.add(character.node);
    });
    
    // Update character detection metrics
    this.metrics.charactersDetected = (this.metrics.charactersDetected || 0) + characters.length;
  }

  /**
   * Remove all characters from an element from the selection (Task 2.2)
   * @param {Element} element - Element whose characters should be removed
   */
  removeElementCharactersFromSelection(element) {
    // Get all text nodes within this element
    const textNodes = this.edgeCharacterDetector.getTextNodes(element);
    
    // Remove these text nodes from the intersection set
    textNodes.forEach(textNode => {
      this.intersectingTextNodes.delete(textNode);
    });
  }

  /**
   * Handle dynamic content discovery with smart element filtering (Task 2.1)
   * @param {Array<Element>} newElements - New elements to potentially observe
   */
  discoverDynamicTextElements(newElements) {
    if (!this.textObserver || !newElements || newElements.length === 0) {
      return;
    }
    
    const startTime = performance.now();
    let elementsAdded = 0;
    
    if (this.smartTargetingEnabled) {
      // Use TextElementFilter to filter new elements
      const textElements = this.textElementFilter.filterTextElements(newElements);
      
      textElements.forEach(element => {
        if (!this.observedElements.has(element)) {
          this.textObserver.observe(element);
          this.observedElements.add(element);
          this.metrics.elementsObserved++;
          this.metrics.dynamicElementsAdded++;
          elementsAdded++;
        }
      });
      
      if (this.debugLogging && elementsAdded > 0) {
        console.log('[KeyPilot Debug] Dynamic content discovery with smart targeting:', {
          newElementsScanned: newElements.length,
          textElementsFound: textElements.length,
          elementsAdded,
          totalObserved: this.metrics.elementsObserved,
          processingTime: (performance.now() - startTime).toFixed(2) + 'ms'
        });
      }
    } else {
      // Fallback to traditional filtering
      newElements.forEach(element => {
        if (!this.observedElements.has(element) && this.elementContainsText(element)) {
          this.textObserver.observe(element);
          this.observedElements.add(element);
          this.metrics.elementsObserved++;
          this.metrics.dynamicElementsAdded++;
          elementsAdded++;
        }
      });
      
      if (this.debugLogging && elementsAdded > 0) {
        console.log('[KeyPilot Debug] Dynamic content discovery (traditional):', {
          newElementsScanned: newElements.length,
          elementsAdded,
          totalObserved: this.metrics.elementsObserved,
          processingTime: (performance.now() - startTime).toFixed(2) + 'ms'
        });
      }
    }
  }

  /**
   * Check if an element contains meaningful text content
   * @param {Element} element - Element to check
   * @returns {boolean} - True if element contains text
   */
  elementContainsText(element) {
    // Quick check for text content
    const textContent = element.textContent || '';
    if (!textContent.trim()) {
      return false;
    }
    
    // Exclude elements that are likely non-content
    if (this.isNonContentElement(element)) {
      return false;
    }
    
    // Check if element has visible text (not just whitespace)
    const hasVisibleText = textContent.replace(/\s+/g, ' ').trim().length > 0;
    
    return hasVisibleText;
  }

  /**
   * Create a text selection from the currently intersecting text nodes
   * @returns {Selection|null} - Browser selection object or null
   */
  createSelectionFromIntersection() {
    if (this.intersectingTextNodes.size === 0) {
      return null;
    }
    
    try {
      const textNodes = Array.from(this.intersectingTextNodes);
      
      // Sort text nodes by document order
      textNodes.sort((a, b) => {
        const comparison = a.compareDocumentPosition(b);
        if (comparison & Node.DOCUMENT_POSITION_FOLLOWING) {
          return -1;
        } else if (comparison & Node.DOCUMENT_POSITION_PRECEDING) {
          return 1;
        }
        return 0;
      });
      
      // Create selection from first to last text node
      const selection = window.getSelection();
      selection.removeAllRanges();
      
      const range = document.createRange();
      range.setStart(textNodes[0], 0);
      range.setEnd(textNodes[textNodes.length - 1], textNodes[textNodes.length - 1].textContent.length);
      
      selection.addRange(range);
      
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Selection created from intersection:', {
          textNodes: textNodes.length,
          selectedText: selection.toString().substring(0, 100) + '...'
        });
      }
      
      return selection;
    } catch (error) {
      console.warn('[KeyPilot] Error creating selection from intersection:', error);
      return null;
    }
  }

  /**
   * Monitor performance thresholds and trigger fallback if needed
   * @param {number} processingTime - Time taken for this processing cycle
   * @param {number} elementsProcessed - Number of elements processed
   */



/**
 * Evaluate recovery attempt and decide whether to stay recovered or return to fallback
 */
evaluateRecovery() {
  const averageTime = this.calculateAverageProcessingTime();
  const recoverySuccessful = averageTime < this.legacyPerformanceMonitor.thresholdMs && 
                             this.legacyPerformanceMonitor.consecutiveSlowOperations === 0;
  
  if (recoverySuccessful) {
    // Recovery successful - stay in edge-only mode
    this.fallbackHandler.active = false;
    this.fallbackHandler.reason = null;
    this.performanceState.fallbackTriggered = false;
    
    this.notifyRecoverySuccessful();
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Recovery successful - edge-only processing restored:', {
        averageTime: averageTime.toFixed(2) + 'ms',
        threshold: this.legacyPerformanceMonitor.thresholdMs + 'ms',
        recoveryAttempt: this.fallbackHandler.recoveryAttempts
      });
    }
  } else {
    // Recovery failed - return to fallback mode
    this.fallbackHandler.active = true;
    
    if (this.fallbackHandler.recoveryAttempts < this.fallbackHandler.maxRecoveryAttempts) {
      this.scheduleRecoveryAttempt();
    }
    
    if (this.debugLogging) {
      console.warn('[KeyPilot Debug] Recovery failed - returning to fallback mode:', {
        averageTime: averageTime.toFixed(2) + 'ms',
        threshold: this.legacyPerformanceMonitor.thresholdMs + 'ms',
        recoveryAttempt: this.fallbackHandler.recoveryAttempts,
        maxAttempts: this.fallbackHandler.maxRecoveryAttempts
      });
    }
  }
}
  
  /**
   * Get cache hit ratio as numeric value
   * @returns {number} - Cache hit ratio (0-1)
   */
  getCacheHitRatioNumeric() {
    const totalCacheAttempts = this.metrics.cacheHits + this.metrics.cacheMisses;
    if (totalCacheAttempts === 0) return 0;
    
    return this.metrics.cacheHits / totalCacheAttempts;
  }
  
  /**
   * Notify user about performance issues
   * @param {Object} alert - Performance alert object
   */
  notifyPerformanceIssue(alert) {
    if (!this.notificationSystem.enabled || 
        !this.notificationSystem.showPerformanceNotifications) {
      return;
    }
    
    const currentTime = Date.now();
    if (currentTime - this.notificationSystem.lastNotificationTime < 
        this.notificationSystem.notificationCooldownMs) {
      return; // Respect cooldown
    }
    
    let message = '';
    switch (alert.type) {
      case 'performance_regression':
        message = `KeyPilot: Performance regression detected (${alert.data.degradationFactor}x slower)`;
        break;
      case 'low_cache_efficiency':
        message = `KeyPilot: Low cache efficiency (${alert.data.cacheHitRatio})`;
        break;
      case 'high_processing_time':
        message = `KeyPilot: High processing time (${alert.data.processingTime})`;
        break;
      default:
        message = `KeyPilot: Performance issue detected (${alert.type})`;
    }
    
    this.showNotification(message, 'warning');
    this.notificationSystem.lastNotificationTime = currentTime;
  }
  
  /**
   * Notify user about fallback activation
   * @param {string} reason - Reason for fallback
   * @param {Object} data - Additional data
   */
  notifyFallbackActivated(reason, data) {
    if (!this.notificationSystem.enabled || 
        !this.notificationSystem.showPerformanceNotifications) {
      return;
    }
    
    const message = `KeyPilot: Switched to fallback selection method due to performance issues`;
    this.showNotification(message, 'info');
  }
  
  /**
   * Notify user about successful recovery
   */
  notifyRecoverySuccessful() {
    if (!this.notificationSystem.enabled || 
        !this.notificationSystem.showPerformanceNotifications) {
      return;
    }
    
    const message = `KeyPilot: Optimized selection method restored`;
    this.showNotification(message, 'success');
  }
  
  /**
   * Show notification to user
   * @param {string} message - Notification message
   * @param {string} type - Notification type (success, warning, error, info)
   */
  showNotification(message, type = 'info') {
    if (this.notificationSystem.notificationCallback) {
      this.notificationSystem.notificationCallback(message, type);
    } else {
      // Fallback to console logging if no notification callback is set
      const logMethod = type === 'error' ? 'error' : type === 'warning' ? 'warn' : 'log';
      console[logMethod](`[KeyPilot Notification] ${message}`);
    }
  }
  
  /**
   * Set notification callback for user notifications
   * @param {Function} callback - Callback function for notifications
   */
  setNotificationCallback(callback) {
    this.notificationSystem.notificationCallback = callback;
  }
  
  /**
   * Check if fallback mode is active
   * @returns {boolean} - True if in fallback mode
   */
  isFallbackActive() {
    return this.fallbackHandler.active;
  }
  
  /**
   * Get current performance status
   * @returns {Object} - Performance status information
   */
  getPerformanceStatus() {
    return {
      fallbackActive: this.fallbackHandler.active,
      fallbackReason: this.fallbackHandler.reason,
      fallbackStartTime: this.fallbackHandler.fallbackStartTime,
      recoveryAttempts: this.fallbackHandler.recoveryAttempts,
      maxRecoveryAttempts: this.fallbackHandler.maxRecoveryAttempts,
      performanceAlerts: this.performanceState.alerts,
      regressionDetected: this.performanceState.regressionDetected,
      baselinePerformance: this.performanceState.baselinePerformance,
      currentAveragePerformance: this.calculateAverageProcessingTime(),
      consecutiveSlowOperations: this.legacyPerformanceMonitor.consecutiveSlowOperations,
      thresholdMs: this.legacyPerformanceMonitor.thresholdMs
    };
  }

  /**
   * Clean up resources and stop timers
   */
  cleanup() {
    // Stop cache management timers
    if (this.memoryMonitoringTimer) {
      clearInterval(this.memoryMonitoringTimer);
      this.memoryMonitoringTimer = null;
    }
    
    if (this.cacheManagementTimer) {
      clearInterval(this.cacheManagementTimer);
      this.cacheManagementTimer = null;
    }
    
    // Disconnect observers
    if (this.textObserver) {
      this.textObserver.disconnect();
      this.textObserver = null;
    }
    
    // Clean up DOM elements
    if (this.rectangleRoot?.parentNode) {
      this.rectangleRoot.parentNode.removeChild(this.rectangleRoot);
      this.rectangleRoot = null;
    }
    
    // Clear cache and tracking data
    this.intersectingTextElements.clear();
    this.intersectingTextNodes.clear();
    this.elementStates.clear();
    // Note: WeakMap doesn't have a clear method, but references will be GC'd
    // Clear the LRU ordering structures
    this.lruRefs.clear();
    this.lruTimestamps.clear();
    this.cacheSize = 0;
    
    // Clear efficiency metrics
    this.cacheEfficiencyMetrics.hotElements.clear();
    this.cacheEfficiencyMetrics.coldElements.clear();
    this.cacheEfficiencyMetrics.evictionHistory = [];
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Rectangle intersection observer cleanup completed');
    }
  }

  /**
   * Get comprehensive performance metrics with enhanced edge-only processing data
   * @returns {Object} - Complete performance metrics object
   */
  getMetrics() {
    const avgTraversalTime = this.metrics.traversalCount > 0 
      ? (this.metrics.totalTraversalTime / this.metrics.traversalCount).toFixed(2)
      : 0;
    
    const avgEdgeProcessingTime = this.metrics.intersectionUpdates > 0
      ? (this.metrics.actualEdgeProcessingTime / this.metrics.intersectionUpdates).toFixed(2)
      : 0;
    
    const cacheEfficiency = this.calculateCacheEfficiency();
    
    return {
      // Basic metrics
      elementsObserved: this.metrics.elementsObserved,
      intersectionUpdates: this.metrics.intersectionUpdates,
      intersectingElements: this.intersectingTextElements.size,
      intersectingTextNodes: this.intersectingTextNodes.size,
      textNodesFound: this.metrics.textNodesFound,
      lastUpdateTime: this.metrics.lastUpdateTime,
      
      // Edge-only processing metrics
      edgeProcessingTime: this.metrics.edgeProcessingTime.toFixed(2) + 'ms',
      edgeElementsProcessed: this.metrics.edgeElementsProcessed,
      elementsEntering: this.metrics.elementsEntering,
      elementsLeaving: this.metrics.elementsLeaving,
      avgEdgeProcessingTime: avgEdgeProcessingTime + 'ms',
      
      // Text node traversal metrics
      totalTraversalTime: this.metrics.totalTraversalTime.toFixed(2) + 'ms',
      traversalCount: this.metrics.traversalCount,
      avgTraversalTime: avgTraversalTime + 'ms',
      
      // Cache performance metrics
      cacheHits: this.metrics.cacheHits,
      cacheMisses: this.metrics.cacheMisses,
      cacheHitRatio: cacheEfficiency.hitRatio,
      cacheEfficiency: cacheEfficiency,
      cacheEvictions: this.metrics.cacheEvictions || 0,
      cacheSize: this.cacheSize,
      maxCacheSize: this.maxCacheSize,
      
      // Advanced cache management metrics
      memoryUsage: this.metrics.memoryUsage ? {
        current: this.metrics.memoryUsage.current.toFixed(2) + 'MB',
        peak: this.metrics.memoryUsage.peak.toFixed(2) + 'MB',
        average: this.metrics.memoryUsage.average.toFixed(2) + 'MB',
        cleanupTriggers: this.metrics.memoryUsage.cleanupTriggers
      } : null,
      cacheUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%',
      cleanupThreshold: this.cleanupThreshold,
      cleanupBatchSize: this.cleanupBatchSize,
      
      // Performance comparison metrics
      estimatedFullTraversalTime: this.metrics.estimatedFullTraversalTime.toFixed(2) + 'ms',
      actualEdgeProcessingTime: this.metrics.actualEdgeProcessingTime.toFixed(2) + 'ms',
      efficiencyGainPercent: this.metrics.efficiencyGainPercent.toFixed(1) + '%',
      performanceGain: this.calculatePerformanceGain(),
      
      // State tracking
      elementStatesTracked: this.elementStates.size,
      debugLogging: this.debugLogging,
      
      // Performance monitoring and fallback status
      performanceMonitoring: {
        enabled: this.legacyPerformanceMonitor.enabled,
        fallbackActive: this.fallbackHandler.active,
        fallbackReason: this.fallbackHandler.reason,
        consecutiveSlowOperations: this.legacyPerformanceMonitor.consecutiveSlowOperations,
        thresholdMs: this.legacyPerformanceMonitor.thresholdMs,
        baselinePerformance: this.performanceState.baselinePerformance ? 
          this.performanceState.baselinePerformance.toFixed(2) + 'ms' : 'Not established',
        currentAveragePerformance: this.calculateAverageProcessingTime().toFixed(2) + 'ms',
        regressionDetected: this.performanceState.regressionDetected,
        recoveryAttempts: this.fallbackHandler.recoveryAttempts,
        maxRecoveryAttempts: this.fallbackHandler.maxRecoveryAttempts,
        recentAlerts: this.performanceState.alerts.slice(-3), // Last 3 alerts
        processingTimeWindow: this.legacyPerformanceMonitor.processingTimes.map(t => t.toFixed(2) + 'ms')
      }
    };
  }
  


  /**
   * Enhanced cache access tracking for LRU with detailed metrics
   * @param {Element} element - Element that was accessed
   */
  updateCacheAccess(element) {
    const startTime = performance.now();
    const elementId = this.getElementId(element);
    const currentTime = Date.now();

    // Get or create access record using WeakMap with element as key
    const existingRecord = this.cacheAccessOrder.get(element);
    const accessRecord = {
      timestamp: currentTime,
      accessCount: existingRecord ? existingRecord.accessCount + 1 : 1,
      elementId: elementId, // Store elementId for LRU ordering
      lastAccessTime: existingRecord ? existingRecord.timestamp : currentTime,
      accessFrequency: 0 // Will be calculated
    };

    // Calculate access frequency (accesses per minute)
    if (existingRecord) {
      const timeDiff = currentTime - existingRecord.timestamp;
      accessRecord.accessFrequency = timeDiff > 0 ? (accessRecord.accessCount / (timeDiff / 60000)) : 0;
    }

    this.cacheAccessOrder.set(element, accessRecord);

    // Update LRU ordering with WeakRef
    this.lruRefs.set(elementId, new WeakRef(element));
    this.lruTimestamps.set(elementId, currentTime);

    // Register element with FinalizationRegistry for automatic cleanup
    // Only register if not already registered
    if (!existingRecord) {
      this.cacheCleanupRegistry.register(element, elementId);
    }
    
    // Track hot and cold elements for optimization
    this.updateElementTemperature(elementId, accessRecord);
    
    // Update cache efficiency metrics
    const accessTime = performance.now() - startTime;
    this.cacheEfficiencyMetrics.totalCacheOperations++;
    this.cacheEfficiencyMetrics.totalAccessTime += accessTime;
    this.cacheEfficiencyMetrics.averageAccessTime = 
      this.cacheEfficiencyMetrics.totalAccessTime / this.cacheEfficiencyMetrics.totalCacheOperations;
    
    if (this.debugLogging && accessRecord.accessCount > 1) {
      console.log('[KeyPilot Debug] Enhanced cache access tracking:', {
        elementId: elementId.substring(0, 10) + '...',
        accessCount: accessRecord.accessCount,
        accessFrequency: accessRecord.accessFrequency.toFixed(2) + ' accesses/min',
        timeSinceLastAccess: timeDiff ? (timeDiff / 1000).toFixed(1) + 's' : 'first access',
        accessTime: accessTime.toFixed(3) + 'ms'
      });
    }
  }

  /**
   * Update element temperature classification for cache optimization
   * @param {string} elementId - Element identifier
   * @param {Object} accessRecord - Access record with frequency data
   */
  updateElementTemperature(elementId, accessRecord) {
    const { accessCount, accessFrequency } = accessRecord;
    
    // Define temperature thresholds
    const HOT_ACCESS_THRESHOLD = 5; // 5+ accesses makes an element "hot"
    const HOT_FREQUENCY_THRESHOLD = 2; // 2+ accesses per minute
    const COLD_ACCESS_THRESHOLD = 1; // Only 1 access makes an element "cold"
    const COLD_TIME_THRESHOLD = 300000; // 5 minutes without access
    
    // Remove from cold set if it's being accessed
    this.cacheEfficiencyMetrics.coldElements.delete(elementId);
    
    // Classify as hot element
    if (accessCount >= HOT_ACCESS_THRESHOLD || accessFrequency >= HOT_FREQUENCY_THRESHOLD) {
      this.cacheEfficiencyMetrics.hotElements.set(elementId, {
        accessCount,
        accessFrequency,
        lastAccess: accessRecord.timestamp,
        priority: accessCount * accessFrequency // Combined priority score
      });
    }
    
    // Clean up old hot elements that have cooled down
    const currentTime = Date.now();
    for (const [hotElementId, hotRecord] of this.cacheEfficiencyMetrics.hotElements.entries()) {
      if (currentTime - hotRecord.lastAccess > COLD_TIME_THRESHOLD) {
        this.cacheEfficiencyMetrics.hotElements.delete(hotElementId);
        this.cacheEfficiencyMetrics.coldElements.add(hotElementId);
      }
    }
  }

  /**
   * Enhanced cache text nodes with comprehensive size monitoring and LRU management
   * @param {Element} element - Element to cache text nodes for
   * @param {Array} textNodes - Text nodes to cache
   */
  cacheTextNodes(element, textNodes) {
    // Proactive cache size monitoring and cleanup
    this.monitorCacheSize();
    
    // Check if we need to evict entries before adding new one
    if (this.cacheSize >= this.cleanupThreshold) {
      this.evictLeastRecentlyUsed();
    }
    
    // Additional safety check for maximum cache size
    if (this.cacheSize >= this.maxCacheSize) {
      // Force more aggressive cleanup if we're at the absolute limit
      const aggressiveCleanupSize = Math.floor(this.maxCacheSize * 0.3); // Remove 30% of cache
      this.performTargetedEviction(aggressiveCleanupSize);
    }
    
    // Add to cache with enhanced tracking
    this.textNodeCache.set(element, textNodes);
    this.updateCacheAccess(element);
    this.cacheSize++;
    
    // Update cache efficiency metrics
    this.updateCacheEfficiencyMetrics(textNodes.length);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Cached text nodes for element:', {
        element: element.tagName,
        className: element.className || 'none',
        textNodesCount: textNodes.length,
        cacheSize: this.cacheSize,
        maxCacheSize: this.maxCacheSize,
        cacheUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%',
        hitRatio: this.getCacheHitRatio()
      });
    }
  }

  /**
   * Enhanced cache size monitoring with adaptive thresholds and memory pressure detection
   */
  monitorCacheSize() {
    const utilizationPercent = (this.cacheSize / this.maxCacheSize) * 100;
    const currentTime = Date.now();
    
    // Initialize or update cache monitoring metrics
    if (!this.metrics.maxCacheUtilization) {
      this.metrics.maxCacheUtilization = 0;
      this.metrics.avgCacheUtilization = 0;
      this.metrics.cacheMonitoringCount = 0;
      this.metrics.lastMonitoringTime = currentTime;
      this.metrics.utilizationHistory = [];
      this.metrics.memoryPressureScore = 0;
      this.metrics.cacheEvictions = 0;
    }
    
    // Update utilization statistics
    this.metrics.maxCacheUtilization = Math.max(this.metrics.maxCacheUtilization, utilizationPercent);
    this.metrics.cacheMonitoringCount++;
    this.metrics.avgCacheUtilization = 
      (this.metrics.avgCacheUtilization * (this.metrics.cacheMonitoringCount - 1) + utilizationPercent) / 
      this.metrics.cacheMonitoringCount;
    
    // Track utilization history for trend analysis
    this.metrics.utilizationHistory.push({
      timestamp: currentTime,
      utilization: utilizationPercent,
      cacheSize: this.cacheSize,
      accessOrderSize: this.lruRefs.size
    });
    
    // Keep only recent history (last 50 measurements)
    if (this.metrics.utilizationHistory.length > 50) {
      this.metrics.utilizationHistory.shift();
    }
    
    // Calculate memory pressure score based on recent trends
    this.calculateMemoryPressureScore();
    
    // Adaptive cleanup thresholds based on memory pressure and access patterns
    const adaptiveThresholds = this.calculateAdaptiveCleanupThresholds();
    
    // Enhanced warning system with memory pressure context
    if (utilizationPercent > 90) {
      this.cacheEfficiencyMetrics.memoryPressureEvents++;
      
      if (this.debugLogging) {
        console.warn('[KeyPilot Debug] High cache utilization detected:', {
          currentSize: this.cacheSize,
          maxSize: this.maxCacheSize,
          utilization: utilizationPercent.toFixed(1) + '%',
          accessOrderSize: this.lruRefs.size,
          memoryPressureScore: this.metrics.memoryPressureScore,
          adaptiveThresholds: adaptiveThresholds
        });
      }
    }
  }

  /**
   * Calculate memory pressure score based on cache utilization trends
   */
  calculateMemoryPressureScore() {
    if (!this.metrics.utilizationHistory || this.metrics.utilizationHistory.length < 3) {
      this.metrics.memoryPressureScore = 0;
      return;
    }
    
    const recent = this.metrics.utilizationHistory.slice(-5); // Last 5 measurements
    const avgUtilization = recent.reduce((sum, entry) => sum + entry.utilization, 0) / recent.length;
    const trend = recent[recent.length - 1].utilization - recent[0].utilization;
    
    // Calculate pressure score (0-100)
    let pressureScore = avgUtilization * 0.7; // Base score from utilization
    pressureScore += Math.max(0, trend) * 0.3; // Add trend component (only positive trends)
    
    this.metrics.memoryPressureScore = Math.min(100, Math.max(0, pressureScore));
  }

  /**
   * Calculate adaptive cleanup thresholds based on usage patterns
   */
  calculateAdaptiveCleanupThresholds() {
    const baseThreshold = this.cleanupThreshold;
    const pressureMultiplier = 1 - (this.metrics.memoryPressureScore / 200); // Reduce threshold under pressure
    const hotElementsRatio = this.cacheEfficiencyMetrics.hotElements.size / Math.max(1, this.cacheSize);
    
    return {
      cleanupThreshold: Math.max(
        baseThreshold * 0.5, // Never go below 50% of base threshold
        Math.floor(baseThreshold * pressureMultiplier * (1 - hotElementsRatio * 0.2))
      ),
      aggressiveThreshold: Math.floor(this.maxCacheSize * 0.95),
      emergencyThreshold: this.maxCacheSize
    };
  }

  /**
   * Evict least recently used cache entries with intelligent prioritization
   */
  evictLeastRecentlyUsed() {
    const targetEvictions = this.cleanupBatchSize;
    const evictionCandidates = [];

    // Build eviction candidates with priority scoring using LRU timestamps
    for (const [elementId, timestamp] of this.lruTimestamps.entries()) {
      const weakRef = this.lruRefs.get(elementId);
      const element = weakRef?.deref();

      // Skip if element was garbage collected
      if (!element) {
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);
        continue;
      }

      const accessRecord = this.cacheAccessOrder.get(element);

      // Skip if we couldn't find the access record
      if (!accessRecord) {
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);
        continue;
      }

      // Skip if element is no longer in cache (WeakMap cleanup)
      if (!this.textNodeCache.has(element)) {
        this.cacheAccessOrder.delete(element);
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);
        this.cacheSize = Math.max(0, this.cacheSize - 1);
        continue;
      }

      // Calculate eviction priority (lower = more likely to evict)
      const timeSinceAccess = Date.now() - accessRecord.timestamp;
      const accessFrequency = accessRecord.accessFrequency || 0;
      const isHot = this.cacheEfficiencyMetrics.hotElements.has(elementId);

      const priority = this.calculateEvictionPriority(accessRecord, timeSinceAccess, isHot);

      evictionCandidates.push({
        elementId,
        element,
        priority,
        accessRecord,
        timeSinceAccess
      });
    }
    
    // Sort by priority (lowest first = most likely to evict)
    evictionCandidates.sort((a, b) => a.priority - b.priority);
    
    // Evict the least valuable entries
    const evicted = [];
    for (let i = 0; i < Math.min(targetEvictions, evictionCandidates.length); i++) {
      const candidate = evictionCandidates[i];
      
      // Remove from cache
      this.textNodeCache.delete(candidate.element);
      this.cacheAccessOrder.delete(candidate.element);
      this.lruRefs.delete(candidate.elementId);
      this.lruTimestamps.delete(candidate.elementId);
      this.cacheSize = Math.max(0, this.cacheSize - 1);
      
      // Track eviction for analysis
      evicted.push({
        elementId: candidate.elementId,
        priority: candidate.priority,
        timeSinceAccess: candidate.timeSinceAccess,
        accessCount: candidate.accessRecord.accessCount
      });
    }
    
    // Update metrics
    this.metrics.cacheEvictions += evicted.length;
    this.cacheEfficiencyMetrics.evictionHistory.push(...evicted);
    this.cacheEfficiencyMetrics.adaptiveCleanupTriggers++;
    
    // Keep eviction history manageable
    if (this.cacheEfficiencyMetrics.evictionHistory.length > 100) {
      this.cacheEfficiencyMetrics.evictionHistory.splice(0, 50);
    }
    
    if (this.debugLogging && evicted.length > 0) {
      console.log('[KeyPilot Debug] LRU cache eviction completed:', {
        evictedCount: evicted.length,
        targetEvictions,
        newCacheSize: this.cacheSize,
        cacheUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%',
        avgEvictionPriority: (evicted.reduce((sum, e) => sum + e.priority, 0) / evicted.length).toFixed(2),
        oldestEvicted: Math.max(...evicted.map(e => e.timeSinceAccess)) / 1000 + 's ago'
      });
    }
  }

  /**
   * Calculate eviction priority for a cache entry
   * @param {Object} accessRecord - Access record for the element
   * @param {number} timeSinceAccess - Time since last access in ms
   * @param {boolean} isHot - Whether element is classified as hot
   * @returns {number} Priority score (lower = more likely to evict)
   */
  calculateEvictionPriority(accessRecord, timeSinceAccess, isHot) {
    const baseScore = 100;
    
    // Time component (older = higher eviction priority)
    const timeScore = Math.min(50, timeSinceAccess / 60000); // Max 50 points for 1+ minutes
    
    // Frequency component (less frequent = higher eviction priority)
    const frequencyScore = Math.max(0, 30 - (accessRecord.accessCount * 5)); // Max 30 points reduction
    
    // Hot element protection (hot elements get significant protection)
    const hotProtection = isHot ? -40 : 0;
    
    // Access frequency protection (frequently accessed elements get protection)
    const accessFrequencyProtection = Math.min(20, (accessRecord.accessFrequency || 0) * -10);
    
    return baseScore + timeScore + frequencyScore + hotProtection + accessFrequencyProtection;
  }

  /**
   * Perform targeted eviction to reach a specific cache size
   * @param {number} targetEvictions - Number of entries to evict
   */
  performTargetedEviction(targetEvictions) {
    if (targetEvictions <= 0) return;
    
    const cacheSizeBefore = this.cacheSize;
    const originalBatchSize = this.cleanupBatchSize;
    this.cleanupBatchSize = targetEvictions;
    
    this.evictLeastRecentlyUsed();
    
    this.cleanupBatchSize = originalBatchSize;
    
    if (this.debugLogging) {
      const actualEvictions = Math.max(0, cacheSizeBefore - this.cacheSize);
      console.log('[KeyPilot Debug] Targeted cache eviction completed:', {
        targetEvictions,
        actualEvictions,
        finalCacheSize: this.cacheSize,
        cacheUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%'
      });
    }
  }

  /**
   * Implement cache warming strategies for frequently accessed elements
   */
  implementCacheWarming() {
    if (!EDGE_ONLY_SELECTION.ENABLE_PREDICTIVE_CACHING) {
      return;
    }
    
    // Get viewport bounds for predictive caching
    const viewport = {
      left: window.scrollX,
      top: window.scrollY,
      right: window.scrollX + window.innerWidth,
      bottom: window.scrollY + window.innerHeight
    };
    
    // Expand viewport by predictive distance
    const expandedViewport = {
      left: viewport.left - EDGE_ONLY_SELECTION.PREDICTIVE_CACHE_DISTANCE,
      top: viewport.top - EDGE_ONLY_SELECTION.PREDICTIVE_CACHE_DISTANCE,
      right: viewport.right + EDGE_ONLY_SELECTION.PREDICTIVE_CACHE_DISTANCE,
      bottom: viewport.bottom + EDGE_ONLY_SELECTION.PREDICTIVE_CACHE_DISTANCE
    };
    
    // Find elements in expanded viewport that aren't cached yet
    const uncachedElements = [];
    const allElements = document.querySelectorAll('p, div, span, h1, h2, h3, h4, h5, h6, li, td, th');
    
    for (const element of allElements) {
      if (this.textNodeCache.has(element)) continue;
      if (!this.elementContainsText(element)) continue;
      
      const rect = element.getBoundingClientRect();
      const elementBounds = {
        left: rect.left + window.scrollX,
        top: rect.top + window.scrollY,
        right: rect.right + window.scrollX,
        bottom: rect.bottom + window.scrollY
      };
      
      // Check if element is in expanded viewport
      if (this.isElementInBounds(elementBounds, expandedViewport)) {
        uncachedElements.push(element);
      }
    }
    
    // Warm cache for a limited number of elements to avoid performance impact
    const maxWarmingElements = Math.min(10, uncachedElements.length);
    const startTime = performance.now();
    
    for (let i = 0; i < maxWarmingElements; i++) {
      const element = uncachedElements[i];
      
      // Extract and cache text nodes
      const textNodes = this.extractTextNodesForCaching(element);
      if (textNodes.length > 0) {
        this.cacheTextNodes(element, textNodes);
      }
      
      // Limit warming time to prevent performance impact
      if (performance.now() - startTime > 5) break;
    }
    
    if (this.debugLogging && maxWarmingElements > 0) {
      console.log('[KeyPilot Debug] Cache warming completed:', {
        elementsWarmed: maxWarmingElements,
        totalCandidates: uncachedElements.length,
        warmingTime: (performance.now() - startTime).toFixed(2) + 'ms',
        newCacheSize: this.cacheSize,
        cacheUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%'
      });
    }
  }

  /**
   * Check if element bounds intersect with target bounds
   * @param {Object} elementBounds - Element bounds
   * @param {Object} targetBounds - Target bounds to check against
   * @returns {boolean} True if bounds intersect
   */
  isElementInBounds(elementBounds, targetBounds) {
    return !(elementBounds.right < targetBounds.left ||
             elementBounds.left > targetBounds.right ||
             elementBounds.bottom < targetBounds.top ||
             elementBounds.top > targetBounds.bottom);
  }

  /**
   * Extract text nodes for caching purposes (without adding to intersection set)
   * @param {Element} element - Element to extract text nodes from
   * @returns {Array} Array of text nodes
   */
  extractTextNodesForCaching(element) {
    const textNodes = [];
    const walker = document.createTreeWalker(
      element,
      NodeFilter.SHOW_TEXT,
      {
        acceptNode: (node) => {
          if (!node.textContent || !node.textContent.trim()) {
            return NodeFilter.FILTER_REJECT;
          }
          
          const parent = node.parentElement;
          if (parent && this.isNonContentElement(parent)) {
            return NodeFilter.FILTER_REJECT;
          }
          
          return NodeFilter.FILTER_ACCEPT;
        }
      }
    );
    
    let node;
    while (node = walker.nextNode()) {
      textNodes.push(node);
    }
    
    return textNodes;
  }

  /**
   * Update cache efficiency metrics
   * @param {number} textNodesCount - Number of text nodes cached
   */
  updateCacheEfficiencyMetrics(textNodesCount) {
    this.cacheEfficiencyMetrics.totalCacheOperations++;
    
    // Update cache efficiency patterns
    const efficiency = this.getCacheEfficiencyMetrics();
    
    if (this.debugLogging && this.cacheEfficiencyMetrics.totalCacheOperations % 10 === 0) {
      console.log('[KeyPilot Debug] Cache efficiency update:', {
        totalOperations: this.cacheEfficiencyMetrics.totalCacheOperations,
        hitRatio: efficiency.hitRatio,
        hotElements: this.cacheEfficiencyMetrics.hotElements.size,
        coldElements: this.cacheEfficiencyMetrics.coldElements.size,
        longestHitStreak: this.cacheEfficiencyMetrics.longestHitStreak,
        longestMissStreak: this.cacheEfficiencyMetrics.longestMissStreak
      });
    }
  }

  /**
   * Get comprehensive cache efficiency metrics
   * @returns {Object} Cache efficiency metrics
   */
  getCacheEfficiencyMetrics() {
    const totalAttempts = this.metrics.cacheHits + this.metrics.cacheMisses;
    const hitRatio = totalAttempts > 0 ? ((this.metrics.cacheHits / totalAttempts) * 100).toFixed(1) + '%' : '0%';
    
    return {
      hitRatio,
      totalOperations: this.cacheEfficiencyMetrics.totalCacheOperations,
      currentHitStreak: this.cacheEfficiencyMetrics.cacheHitStreak,
      longestHitStreak: this.cacheEfficiencyMetrics.longestHitStreak,
      currentMissStreak: this.cacheEfficiencyMetrics.cacheMissStreak,
      longestMissStreak: this.cacheEfficiencyMetrics.longestMissStreak,
      averageAccessTime: this.cacheEfficiencyMetrics.averageAccessTime.toFixed(3) + 'ms',
      hotElements: this.cacheEfficiencyMetrics.hotElements.size,
      coldElements: this.cacheEfficiencyMetrics.coldElements.size,
      evictionHistory: this.cacheEfficiencyMetrics.evictionHistory.length,
      memoryPressureEvents: this.cacheEfficiencyMetrics.memoryPressureEvents,
      adaptiveCleanupTriggers: this.cacheEfficiencyMetrics.adaptiveCleanupTriggers,
      cacheSize: this.cacheSize,
      maxCacheSize: this.maxCacheSize,
      cacheUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%',
      memoryPressureScore: this.metrics.memoryPressureScore?.toFixed(1) || '0'
    };
  }

  /**
   * Monitor memory usage and trigger cleanup if necessary
   */
  monitorMemoryUsage() {
    if (!EDGE_ONLY_SELECTION.ENABLE_MEMORY_MONITORING) {
      return;
    }
    
    // Estimate memory usage based on cache size and content
    const estimatedMemoryUsage = this.estimateMemoryUsage();
    const memoryLimitMB = EDGE_ONLY_SELECTION.MAX_MEMORY_USAGE_MB;
    const memoryUsagePercent = (estimatedMemoryUsage / memoryLimitMB) * 100;
    
    // Initialize memory metrics if needed
    if (!this.metrics.memoryUsage) {
      this.metrics.memoryUsage = {
        current: 0,
        peak: 0,
        average: 0,
        measurements: 0,
        lastCleanup: Date.now(),
        cleanupTriggers: 0
      };
    }
    
    // Update memory metrics
    this.metrics.memoryUsage.current = estimatedMemoryUsage;
    this.metrics.memoryUsage.peak = Math.max(this.metrics.memoryUsage.peak, estimatedMemoryUsage);
    this.metrics.memoryUsage.measurements++;
    this.metrics.memoryUsage.average = 
      (this.metrics.memoryUsage.average * (this.metrics.memoryUsage.measurements - 1) + estimatedMemoryUsage) / 
      this.metrics.memoryUsage.measurements;
    
    // Trigger cleanup if memory usage is too high
    if (memoryUsagePercent > EDGE_ONLY_SELECTION.GARBAGE_COLLECTION_THRESHOLD * 100) {
      this.triggerMemoryCleanup();
    }
    
    if (this.debugLogging && this.metrics.memoryUsage.measurements % 20 === 0) {
      console.log('[KeyPilot Debug] Memory usage monitoring:', {
        currentUsage: estimatedMemoryUsage.toFixed(2) + 'MB',
        usagePercent: memoryUsagePercent.toFixed(1) + '%',
        peakUsage: this.metrics.memoryUsage.peak.toFixed(2) + 'MB',
        averageUsage: this.metrics.memoryUsage.average.toFixed(2) + 'MB',
        memoryLimit: memoryLimitMB + 'MB',
        cacheSize: this.cacheSize,
        cleanupTriggers: this.metrics.memoryUsage.cleanupTriggers
      });
    }
  }

  /**
   * Estimate memory usage of the cache system
   * @returns {number} Estimated memory usage in MB
   */
  estimateMemoryUsage() {
    // Rough estimation based on cache size and typical text node content
    const avgTextNodesPerElement = 5; // Conservative estimate
    const avgTextNodeSize = 100; // bytes per text node (conservative)
    const cacheOverhead = 200; // bytes per cache entry for metadata
    
    const textNodeMemory = this.cacheSize * avgTextNodesPerElement * avgTextNodeSize;
    const cacheMetadataMemory = this.cacheSize * cacheOverhead;
    const accessOrderMemory = this.lruRefs.size * 200; // bytes per LRU entry (WeakRef + timestamp)
    const metricsMemory = 50000; // rough estimate for all metrics objects
    
    const totalBytes = textNodeMemory + cacheMetadataMemory + accessOrderMemory + metricsMemory;
    return totalBytes / (1024 * 1024); // Convert to MB
  }

  /**
   * Trigger memory cleanup when usage is too high
   */
  triggerMemoryCleanup() {
    const startTime = performance.now();
    const initialCacheSize = this.cacheSize;
    
    // Aggressive cleanup - remove 40% of cache
    const targetEvictions = Math.floor(this.cacheSize * 0.4);
    this.performTargetedEviction(targetEvictions);
    
    // Clean up stale access records
    this.cleanupStaleAccessRecords();
    
    // Update memory cleanup metrics
    this.metrics.memoryUsage.cleanupTriggers++;
    this.metrics.memoryUsage.lastCleanup = Date.now();
    
    const cleanupTime = performance.now() - startTime;
    const evictedCount = initialCacheSize - this.cacheSize;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Memory cleanup triggered:', {
        reason: 'High memory usage',
        initialCacheSize,
        finalCacheSize: this.cacheSize,
        evictedEntries: evictedCount,
        cleanupTime: cleanupTime.toFixed(2) + 'ms',
        estimatedMemoryFreed: (evictedCount * 0.5).toFixed(2) + 'KB',
        newMemoryUsage: this.estimateMemoryUsage().toFixed(2) + 'MB'
      });
    }
  }

  /**
   * Clean up stale access records that no longer have corresponding cache entries
   */
  cleanupStaleAccessRecords() {
    const staleRecords = [];

    // Check LRU refs for elements that have been garbage collected
    for (const [elementId, weakRef] of this.lruRefs.entries()) {
      const element = weakRef.deref();

      // If element was GC'd but we still have a record, clean it up
      if (!element) {
        staleRecords.push(elementId);
        continue;
      }

      // Check if element is still in cache
      if (!this.textNodeCache.has(element)) {
        staleRecords.push(elementId);
      }
    }

    // Remove stale records
    staleRecords.forEach(elementId => {
      this.cacheAccessOrder.delete(this.lruRefs.get(elementId)?.deref());
      this.lruRefs.delete(elementId);
      this.lruTimestamps.delete(elementId);
      this.cacheEfficiencyMetrics.coldElements.delete(elementId);
      this.cacheEfficiencyMetrics.hotElements.delete(elementId);
      this.cacheSize = Math.max(0, this.cacheSize - 1);
    });

    if (this.debugLogging && staleRecords.length > 0) {
      console.log('[KeyPilot Debug] Cleaned up stale access records:', {
        staleRecords: staleRecords.length,
        remainingAccessRecords: this.lruRefs.size,
        cacheSize: this.cacheSize
      });
    }
  }

  /**
   * Implement cache efficiency optimization based on usage patterns
   */
  optimizeCacheEfficiency() {
    // Analyze cache usage patterns
    const efficiency = this.getCacheEfficiencyMetrics();
    const hitRatioNumeric = parseFloat(efficiency.hitRatio) / 100;
    
    // If hit ratio is low, implement optimization strategies
    if (hitRatioNumeric < EDGE_ONLY_SELECTION.CACHE_HIT_RATIO_THRESHOLD) {
      this.implementCacheOptimizations(hitRatioNumeric);
    }
    
    // Periodic cache warming if enabled
    if (EDGE_ONLY_SELECTION.ENABLE_PREDICTIVE_CACHING) {
      // Only warm cache if we have spare capacity
      const utilizationPercent = (this.cacheSize / this.maxCacheSize) * 100;
      if (utilizationPercent < 70) {
        this.implementCacheWarming();
      }
    }
    
    // Monitor and adjust cache parameters based on performance
    this.adjustCacheParameters();
  }

  /**
   * Implement cache optimizations when hit ratio is low
   * @param {number} currentHitRatio - Current cache hit ratio (0-1)
   */
  implementCacheOptimizations(currentHitRatio) {
    // Strategy 1: Increase cache size if memory allows
    const memoryUsage = this.estimateMemoryUsage();
    const memoryLimit = EDGE_ONLY_SELECTION.MAX_MEMORY_USAGE_MB;
    
    if (memoryUsage < memoryLimit * 0.7 && this.maxCacheSize < 2000) {
      const newMaxSize = Math.min(2000, Math.floor(this.maxCacheSize * 1.2));
      this.maxCacheSize = newMaxSize;
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Increased cache size for better hit ratio:', {
          oldMaxSize: Math.floor(newMaxSize / 1.2),
          newMaxSize,
          currentHitRatio: (currentHitRatio * 100).toFixed(1) + '%',
          memoryUsage: memoryUsage.toFixed(2) + 'MB'
        });
      }
    }
    
    // Strategy 2: Adjust cleanup threshold to retain more entries
    if (currentHitRatio < 0.5) {
      this.cleanupThreshold = Math.min(
        this.maxCacheSize * 0.9,
        this.cleanupThreshold * 1.1
      );
    }
    
    // Strategy 3: Protect hot elements more aggressively
    this.adjustHotElementProtection(currentHitRatio);
  }

  /**
   * Adjust hot element protection based on cache performance
   * @param {number} currentHitRatio - Current cache hit ratio (0-1)
   */
  adjustHotElementProtection(currentHitRatio) {
    // Lower hit ratios require more aggressive hot element protection
    const protectionMultiplier = Math.max(0.5, currentHitRatio);
    
    // Update hot element thresholds
    for (const [elementId, hotRecord] of this.cacheEfficiencyMetrics.hotElements.entries()) {
      hotRecord.priority *= (1 + (1 - protectionMultiplier));
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Adjusted hot element protection:', {
        currentHitRatio: (currentHitRatio * 100).toFixed(1) + '%',
        protectionMultiplier: protectionMultiplier.toFixed(2),
        hotElements: this.cacheEfficiencyMetrics.hotElements.size
      });
    }
  }

  /**
   * Adjust cache parameters based on performance metrics
   */
  adjustCacheParameters() {
    const metrics = this.getMetrics();
    const efficiency = this.getCacheEfficiencyMetrics();
    
    // Adjust batch size based on eviction frequency
    if (this.metrics.cacheEvictions > 0) {
      const evictionsPerUpdate = this.metrics.cacheEvictions / Math.max(1, this.metrics.intersectionUpdates);
      
      if (evictionsPerUpdate > 0.1) {
        // Too many evictions - increase batch size
        this.cleanupBatchSize = Math.min(500, Math.floor(this.cleanupBatchSize * 1.2));
      } else if (evictionsPerUpdate < 0.01) {
        // Too few evictions - decrease batch size for more granular control
        this.cleanupBatchSize = Math.max(50, Math.floor(this.cleanupBatchSize * 0.9));
      }
    }
    
    if (this.debugLogging && this.metrics.intersectionUpdates % 100 === 0) {
      console.log('[KeyPilot Debug] Cache parameter adjustment:', {
        cleanupBatchSize: this.cleanupBatchSize,
        cleanupThreshold: this.cleanupThreshold,
        maxCacheSize: this.maxCacheSize,
        cacheEvictions: this.metrics.cacheEvictions,
        intersectionUpdates: this.metrics.intersectionUpdates,
        hitRatio: efficiency.hitRatio
      });
    }
  }

  /**
   * Calculate memory pressure score based on utilization trends
   */
  calculateMemoryPressureScore() {
    if (this.metrics.utilizationHistory.length < 5) {
      this.metrics.memoryPressureScore = 0;
      return;
    }
    
    const recent = this.metrics.utilizationHistory.slice(-5);
    const trend = recent[recent.length - 1].utilization - recent[0].utilization;
    const avgUtilization = recent.reduce((sum, entry) => sum + entry.utilization, 0) / recent.length;
    const volatility = this.calculateUtilizationVolatility(recent);
    
    // Combine factors: high utilization + positive trend + high volatility = high pressure
    this.metrics.memoryPressureScore = (avgUtilization / 100) * 0.5 + 
                                      Math.max(0, trend / 100) * 0.3 + 
                                      volatility * 0.2;
  }

  /**
   * Calculate utilization volatility for memory pressure assessment
   * @param {Array} utilizationData - Recent utilization measurements
   * @returns {number} - Volatility score (0-1)
   */
  calculateUtilizationVolatility(utilizationData) {
    if (utilizationData.length < 2) return 0;
    
    const changes = [];
    for (let i = 1; i < utilizationData.length; i++) {
      changes.push(Math.abs(utilizationData[i].utilization - utilizationData[i-1].utilization));
    }
    
    const avgChange = changes.reduce((sum, change) => sum + change, 0) / changes.length;
    return Math.min(1, avgChange / 20); // Normalize to 0-1 scale
  }

  /**
   * Calculate adaptive cleanup thresholds based on system state
   * @returns {Object} - Threshold configuration
   */
  calculateAdaptiveCleanupThresholds() {
    const baseThresholds = { maintenance: 70, preventive: 85, emergency: 95 };
    const pressureAdjustment = this.metrics.memoryPressureScore * 10; // Up to 10% adjustment
    
    return {
      maintenance: Math.max(60, baseThresholds.maintenance - pressureAdjustment),
      preventive: Math.max(75, baseThresholds.preventive - pressureAdjustment),
      emergency: Math.max(85, baseThresholds.emergency - pressureAdjustment)
    };
  }

  /**
   * Determine if maintenance cleanup should be performed
   * @returns {boolean} - True if maintenance cleanup is needed
   */
  shouldPerformMaintenanceCleanup() {
    const timeSinceLastCleanup = Date.now() - (this.metrics.lastCleanupTime || 0);
    const hasStaleElements = this.cacheEfficiencyMetrics.coldElements.size > 10;
    const lowCacheEfficiency = this.getCacheHitRatio() < '80%';
    
    return timeSinceLastCleanup > 60000 && (hasStaleElements || lowCacheEfficiency);
  }

  /**
   * Perform targeted eviction of a specific number of cache entries
   * @param {number} targetEvictions - Number of entries to evict
   */
  performTargetedEviction(targetEvictions) {
    if (targetEvictions <= 0 || this.cacheSize === 0) {
      return;
    }
    
    const startTime = performance.now();
    const originalCleanupBatchSize = this.cleanupBatchSize;
    
    // Temporarily increase batch size for targeted eviction
    this.cleanupBatchSize = Math.min(targetEvictions, this.cacheSize);
    
    // Perform the eviction
    this.evictLeastRecentlyUsed();
    
    // Restore original batch size
    this.cleanupBatchSize = originalCleanupBatchSize;
    
    const evictionTime = performance.now() - startTime;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Targeted cache eviction completed:', {
        targetEvictions,
        evictionTime: evictionTime.toFixed(2) + 'ms',
        newCacheSize: this.cacheSize,
        newUtilization: ((this.cacheSize / this.maxCacheSize) * 100).toFixed(1) + '%'
      });
    }
  }

  /**
   * Update cache efficiency metrics when adding new entries
   * @param {number} textNodesCount - Number of text nodes being cached
   */
  updateCacheEfficiencyMetrics(textNodesCount) {
    if (!this.metrics.totalTextNodesCached) {
      this.metrics.totalTextNodesCached = 0;
      this.metrics.avgTextNodesPerElement = 0;
      this.metrics.cacheEntryCount = 0;
    }
    
    this.metrics.totalTextNodesCached += textNodesCount;
    this.metrics.cacheEntryCount++;
    this.metrics.avgTextNodesPerElement = this.metrics.totalTextNodesCached / this.metrics.cacheEntryCount;
  }

  /**
   * Intelligent cache eviction strategy that considers element temperature and access patterns
   * @param {string} mode - Eviction mode: 'maintenance', 'preventive', 'emergency'
   * @param {number} targetEvictions - Number of entries to evict
   */
  performIntelligentEviction(mode = 'preventive', targetEvictions = null) {
    if (!targetEvictions) {
      targetEvictions = Math.min(this.cleanupBatchSize, this.cacheSize);
    }
    
    const startTime = performance.now();
    let evictedCount = 0;
    const evictionStrategy = this.selectEvictionStrategy(mode);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Starting intelligent cache eviction:', {
        mode,
        targetEvictions,
        strategy: evictionStrategy,
        cacheSize: this.cacheSize,
        hotElements: this.cacheEfficiencyMetrics.hotElements.size,
        coldElements: this.cacheEfficiencyMetrics.coldElements.size
      });
    }
    
    // Execute eviction based on selected strategy
    switch (evictionStrategy) {
      case 'cold-first':
        evictedCount = this.evictColdElementsFirst(targetEvictions);
        break;
      case 'lru-standard':
        evictedCount = this.evictLeastRecentlyUsed(targetEvictions);
        break;
      case 'frequency-based':
        evictedCount = this.evictLowFrequencyElements(targetEvictions);
        break;
      case 'hybrid':
        evictedCount = this.evictHybridStrategy(targetEvictions);
        break;
      default:
        evictedCount = this.evictLeastRecentlyUsed(targetEvictions);
    }
    
    const evictionTime = performance.now() - startTime;
    this.metrics.lastCleanupTime = Date.now();
    
    // Record eviction in history for analysis
    this.cacheEfficiencyMetrics.evictionHistory.push({
      timestamp: Date.now(),
      mode,
      strategy: evictionStrategy,
      targetEvictions,
      actualEvictions: evictedCount,
      evictionTime,
      efficiency: targetEvictions > 0 ? (evictedCount / targetEvictions) * 100 : 0
    });
    
    // Keep eviction history manageable
    if (this.cacheEfficiencyMetrics.evictionHistory.length > 20) {
      this.cacheEfficiencyMetrics.evictionHistory.shift();
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Intelligent cache eviction completed:', {
        mode,
        strategy: evictionStrategy,
        targetEvictions,
        actualEvictions: evictedCount,
        evictionTime: evictionTime.toFixed(2) + 'ms',
        efficiency: ((evictedCount / targetEvictions) * 100).toFixed(1) + '%',
        newCacheSize: this.cacheSize,
        remainingHotElements: this.cacheEfficiencyMetrics.hotElements.size
      });
    }
    
    return evictedCount;
  }

  /**
   * Select optimal eviction strategy based on cache state and mode
   * @param {string} mode - Eviction mode
   * @returns {string} - Selected eviction strategy
   */
  selectEvictionStrategy(mode) {
    const coldElementsRatio = this.cacheEfficiencyMetrics.coldElements.size / this.cacheSize;
    const hotElementsRatio = this.cacheEfficiencyMetrics.hotElements.size / this.cacheSize;
    
    switch (mode) {
      case 'maintenance':
        return coldElementsRatio > 0.3 ? 'cold-first' : 'lru-standard';
      case 'preventive':
        return coldElementsRatio > 0.2 ? 'hybrid' : 'frequency-based';
      case 'emergency':
        return hotElementsRatio < 0.5 ? 'cold-first' : 'lru-standard';
      default:
        return 'lru-standard';
    }
  }

  /**
   * Evict cold elements first (elements with low access frequency)
   * @param {number} targetEvictions - Number of entries to evict
   * @returns {number} - Number of elements actually evicted
   */
  evictColdElementsFirst(targetEvictions) {
    let evictedCount = 0;
    const elementsToEvict = [];
    
    // First, evict known cold elements
    for (const elementId of this.cacheEfficiencyMetrics.coldElements) {
      if (evictedCount >= targetEvictions) break;

      const weakRef = this.lruRefs.get(elementId);
      const element = weakRef?.deref();
      const accessRecord = element ? this.cacheAccessOrder.get(element) : null;

      if (element && accessRecord) {
        elementsToEvict.push(element);
        this.cacheAccessOrder.delete(element);
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);
        this.cacheEfficiencyMetrics.coldElements.delete(elementId);
        evictedCount++;
      }
    }
    
    // If we need more evictions, fall back to LRU for remaining elements
    if (evictedCount < targetEvictions) {
      const remainingTarget = targetEvictions - evictedCount;
      evictedCount += this.evictLeastRecentlyUsed(remainingTarget);
    }
    
    // Actually remove from WeakMap cache
    elementsToEvict.forEach(element => {
      if (this.textNodeCache.has(element)) {
        this.textNodeCache.delete(element);
      }
    });
    
    this.cacheSize = Math.max(0, this.cacheSize - evictedCount);
    return evictedCount;
  }

  /**
   * Evict elements with low access frequency
   * @param {number} targetEvictions - Number of entries to evict
   * @returns {number} - Number of elements actually evicted
   */
  evictLowFrequencyElements(targetEvictions) {
    // Sort elements by access frequency (ascending - lowest first)
    const sortedByFrequency = Array.from(this.lruRefs.entries())
      .map(([elementId, weakRef]) => {
        const element = weakRef.deref();
        const accessRecord = element ? this.cacheAccessOrder.get(element) : null;
        return { elementId, element, accessRecord };
      })
      .filter(item => item.element && item.accessRecord)
      .sort((a, b) => (a.accessRecord.accessFrequency || 0) - (b.accessRecord.accessFrequency || 0));

    let evictedCount = 0;
    const elementsToEvict = [];

    for (let i = 0; i < Math.min(targetEvictions, sortedByFrequency.length); i++) {
      const { elementId, element, accessRecord } = sortedByFrequency[i];

      if (element) {
        elementsToEvict.push(element);
        this.cacheAccessOrder.delete(element);
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);

        // Remove from temperature tracking
        this.cacheEfficiencyMetrics.hotElements.delete(elementId);
        this.cacheEfficiencyMetrics.coldElements.delete(elementId);
        
        evictedCount++;
      }
    }
    
    // Actually remove from WeakMap cache
    elementsToEvict.forEach(element => {
      if (this.textNodeCache.has(element)) {
        this.textNodeCache.delete(element);
      }
    });
    
    this.cacheSize = Math.max(0, this.cacheSize - evictedCount);
    return evictedCount;
  }

  /**
   * Hybrid eviction strategy combining multiple factors
   * @param {number} targetEvictions - Number of entries to evict
   * @returns {number} - Number of elements actually evicted
   */
  evictHybridStrategy(targetEvictions) {
    // Calculate composite scores for each element
    const elementScores = [];
    const currentTime = Date.now();

    for (const [elementId, weakRef] of this.lruRefs.entries()) {
      const element = weakRef.deref();
      const accessRecord = element ? this.cacheAccessOrder.get(element) : null;

      if (!element || !accessRecord) continue;

      const timeSinceAccess = currentTime - accessRecord.timestamp;
      const accessFrequency = accessRecord.accessFrequency || 0;
      const accessCount = accessRecord.accessCount || 1;

      // Composite score: higher score = more likely to be evicted
      // Factors: time since access (40%), low frequency (30%), low count (30%)
      const timeScore = Math.min(1, timeSinceAccess / 300000); // Normalize to 5 minutes
      const frequencyScore = Math.max(0, 1 - (accessFrequency / 5)); // Normalize to 5 accesses/min
      const countScore = Math.max(0, 1 - (accessCount / 10)); // Normalize to 10 accesses

      const compositeScore = timeScore * 0.4 + frequencyScore * 0.3 + countScore * 0.3;

      elementScores.push({
        elementId,
        element,
        accessRecord,
        compositeScore
      });
    }
    
    // Sort by composite score (descending - highest scores evicted first)
    elementScores.sort((a, b) => b.compositeScore - a.compositeScore);
    
    let evictedCount = 0;
    const elementsToEvict = [];
    
    for (let i = 0; i < Math.min(targetEvictions, elementScores.length); i++) {
      const { elementId, element, accessRecord } = elementScores[i];

      if (element) {
        elementsToEvict.push(element);
        this.cacheAccessOrder.delete(element);
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);

        // Update temperature tracking
        this.cacheEfficiencyMetrics.hotElements.delete(elementId);
        this.cacheEfficiencyMetrics.coldElements.add(elementId);

        evictedCount++;
      }
    }
    
    // Actually remove from WeakMap cache
    elementsToEvict.forEach(element => {
      if (this.textNodeCache.has(element)) {
        this.textNodeCache.delete(element);
      }
    });
    
    this.cacheSize = Math.max(0, this.cacheSize - evictedCount);
    return evictedCount;
  }



  /**
   * Perform aggressive cache cleanup when standard LRU eviction is insufficient
   */
  performAggressiveCleanup() {
    const startTime = performance.now();
    const initialSize = this.lruTimestamps.size;
    
    // Remove stale entries from access order tracking
    const currentTime = Date.now();
    const staleThreshold = 300000; // 5 minutes
    let staleEntriesRemoved = 0;

    for (const [elementId, timestamp] of this.lruTimestamps.entries()) {
      if (currentTime - timestamp > staleThreshold) {
        const weakRef = this.lruRefs.get(elementId);
        const element = weakRef?.deref();

        if (element) {
          this.cacheAccessOrder.delete(element);
        }
        this.lruRefs.delete(elementId);
        this.lruTimestamps.delete(elementId);
        this.cacheEfficiencyMetrics.hotElements.delete(elementId);
        this.cacheEfficiencyMetrics.coldElements.delete(elementId);
        staleEntriesRemoved++;
      }
    }
    
    // Update cache size estimate
    this.cacheSize = Math.max(0, this.cacheSize - staleEntriesRemoved);
    
    const cleanupTime = performance.now() - startTime;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Aggressive cache cleanup completed:', {
        initialAccessOrderSize: initialSize,
        staleEntriesRemoved,
        finalAccessOrderSize: this.lruTimestamps.size,
        estimatedCacheSize: this.cacheSize,
        cleanupTime: cleanupTime.toFixed(2) + 'ms'
      });
    }
  }

  /**
   * Get comprehensive cache efficiency metrics including enhanced LRU performance data
   * @returns {Object} - Complete cache efficiency analysis
   */
  getCacheEfficiencyMetrics() {
    const totalAttempts = this.metrics.cacheHits + this.metrics.cacheMisses;
    const hitRatio = totalAttempts > 0 ? (this.metrics.cacheHits / totalAttempts) : 0;
    const evictions = this.metrics.cacheEvictions || 0;
    const avgEvictionTime = evictions > 0 ? (this.metrics.totalEvictionTime || 0) / evictions : 0;
    
    // Calculate advanced efficiency metrics
    const cacheUtilization = this.maxCacheSize > 0 ? (this.cacheSize / this.maxCacheSize) : 0;
    const accessOrderAccuracy = this.cacheSize > 0 ? (this.lruTimestamps.size / this.cacheSize) : 1;
    const hotElementRatio = this.cacheSize > 0 ? (this.cacheEfficiencyMetrics.hotElements.size / this.cacheSize) : 0;
    const coldElementRatio = this.cacheSize > 0 ? (this.cacheEfficiencyMetrics.coldElements.size / this.cacheSize) : 0;
    
    // Calculate eviction efficiency from history
    const recentEvictions = this.cacheEfficiencyMetrics.evictionHistory.slice(-5);
    const avgEvictionEfficiency = recentEvictions.length > 0 
      ? recentEvictions.reduce((sum, ev) => sum + ev.efficiency, 0) / recentEvictions.length 
      : 0;
    
    // Calculate cache performance trends
    const performanceTrend = this.calculateCachePerformanceTrend();
    
    return {
      // Basic metrics
      hitRatio: (hitRatio * 100).toFixed(1) + '%',
      hits: this.metrics.cacheHits,
      misses: this.metrics.cacheMisses,
      totalAttempts,
      
      // Size and utilization metrics
      cacheSize: this.cacheSize,
      maxCacheSize: this.maxCacheSize,
      utilization: (cacheUtilization * 100).toFixed(1) + '%',
      accessOrderSize: this.lruRefs.size,
      accessOrderAccuracy: (accessOrderAccuracy * 100).toFixed(1) + '%',
      
      // Eviction metrics
      evictions,
      avgEvictionTime: avgEvictionTime.toFixed(2) + 'ms',
      avgEvictionEfficiency: avgEvictionEfficiency.toFixed(1) + '%',
      adaptiveCleanupTriggers: this.cacheEfficiencyMetrics.adaptiveCleanupTriggers,
      
      // Temperature-based metrics
      hotElements: this.cacheEfficiencyMetrics.hotElements.size,
      coldElements: this.cacheEfficiencyMetrics.coldElements.size,
      hotElementRatio: (hotElementRatio * 100).toFixed(1) + '%',
      coldElementRatio: (coldElementRatio * 100).toFixed(1) + '%',
      
      // Performance metrics
      averageAccessTime: this.cacheEfficiencyMetrics.averageAccessTime.toFixed(3) + 'ms',
      totalCacheOperations: this.cacheEfficiencyMetrics.totalCacheOperations,
      memoryPressureEvents: this.cacheEfficiencyMetrics.memoryPressureEvents,
      memoryPressureScore: (this.metrics.memoryPressureScore || 0).toFixed(2),
      
      // Streak metrics
      longestHitStreak: this.cacheEfficiencyMetrics.longestHitStreak,
      longestMissStreak: this.cacheEfficiencyMetrics.longestMissStreak,
      currentHitStreak: this.cacheEfficiencyMetrics.cacheHitStreak,
      currentMissStreak: this.cacheEfficiencyMetrics.cacheMissStreak,
      
      // Advanced efficiency calculations
      memoryEfficiency: this.cacheSize > 0 ? (this.metrics.cacheHits / this.cacheSize).toFixed(2) : '0.00',
      accessEfficiency: totalAttempts > 0 ? (this.metrics.cacheHits / totalAttempts).toFixed(3) : '0.000',
      performanceTrend: performanceTrend,
      
      // Recent eviction history summary
      recentEvictions: recentEvictions.map(ev => ({
        mode: ev.mode,
        strategy: ev.strategy,
        efficiency: ev.efficiency.toFixed(1) + '%',
        evictionTime: ev.evictionTime.toFixed(2) + 'ms'
      }))
    };
  }

  /**
   * Calculate cache performance trend based on recent metrics
   * @returns {Object} - Performance trend analysis
   */
  calculateCachePerformanceTrend() {
    if (!this.metrics.utilizationHistory || this.metrics.utilizationHistory.length < 3) {
      return { trend: 'insufficient_data', direction: 'stable', confidence: 0 };
    }
    
    const recent = this.metrics.utilizationHistory.slice(-5);
    const utilizationTrend = recent[recent.length - 1].utilization - recent[0].utilization;
    const avgUtilization = recent.reduce((sum, entry) => sum + entry.utilization, 0) / recent.length;
    
    // Determine trend direction
    let direction = 'stable';
    if (utilizationTrend > 5) direction = 'increasing';
    else if (utilizationTrend < -5) direction = 'decreasing';
    
    // Calculate confidence based on consistency
    const changes = [];
    for (let i = 1; i < recent.length; i++) {
      changes.push(recent[i].utilization - recent[i-1].utilization);
    }
    
    const avgChange = changes.reduce((sum, change) => sum + change, 0) / changes.length;
    const consistency = changes.filter(change => 
      (avgChange > 0 && change > 0) || (avgChange < 0 && change < 0) || Math.abs(change) < 2
    ).length / changes.length;
    
    return {
      trend: direction,
      direction: direction,
      confidence: (consistency * 100).toFixed(0) + '%',
      avgUtilization: avgUtilization.toFixed(1) + '%',
      utilizationChange: utilizationTrend.toFixed(1) + '%',
      volatility: this.calculateUtilizationVolatility(recent).toFixed(2)
    };
  }

  /**
   * Generate comprehensive cache efficiency report for analysis and debugging
   * @returns {Object} - Detailed cache performance report
   */
  generateCacheEfficiencyReport() {
    const report = {
      timestamp: new Date().toISOString(),
      summary: this.getCacheEfficiencyMetrics(),
      
      // Detailed cache state analysis
      cacheState: {
        totalElements: this.cacheSize,
        trackedElements: this.lruTimestamps.size,
        hotElements: Array.from(this.cacheEfficiencyMetrics.hotElements.entries()).map(([id, data]) => ({
          elementId: id.substring(0, 10) + '...',
          accessCount: data.accessCount,
          accessFrequency: data.accessFrequency.toFixed(2) + ' accesses/min',
          priority: data.priority.toFixed(2),
          lastAccess: new Date(data.lastAccess).toLocaleTimeString()
        })).slice(0, 10), // Top 10 hot elements
        
        coldElementsCount: this.cacheEfficiencyMetrics.coldElements.size,
        memoryPressureScore: (this.metrics.memoryPressureScore || 0).toFixed(2)
      },
      
      // Performance analysis
      performance: {
        averageAccessTime: this.cacheEfficiencyMetrics.averageAccessTime.toFixed(3) + 'ms',
        totalOperations: this.cacheEfficiencyMetrics.totalCacheOperations,
        operationsPerSecond: this.calculateOperationsPerSecond(),
        efficiencyTrend: this.calculateCachePerformanceTrend(),
        memoryPressureEvents: this.cacheEfficiencyMetrics.memoryPressureEvents
      },
      
      // Eviction analysis
      evictionAnalysis: {
        totalEvictions: this.metrics.cacheEvictions || 0,
        adaptiveCleanups: this.cacheEfficiencyMetrics.adaptiveCleanupTriggers,
        recentEvictions: this.cacheEfficiencyMetrics.evictionHistory.slice(-10),
        evictionStrategies: this.analyzeEvictionStrategies()
      },
      
      // Recommendations
      recommendations: this.generateCacheOptimizationRecommendations()
    };
    
    return report;
  }

  /**
   * Calculate cache operations per second for performance analysis
   * @returns {number} - Operations per second
   */
  calculateOperationsPerSecond() {
    if (!this.metrics.lastUpdateTime || this.cacheEfficiencyMetrics.totalCacheOperations === 0) {
      return 0;
    }
    
    const timeElapsed = (Date.now() - this.metrics.lastUpdateTime) / 1000;
    return timeElapsed > 0 ? (this.cacheEfficiencyMetrics.totalCacheOperations / timeElapsed).toFixed(2) : 0;
  }

  /**
   * Analyze eviction strategies used and their effectiveness
   * @returns {Object} - Eviction strategy analysis
   */
  analyzeEvictionStrategies() {
    const strategies = {};
    
    this.cacheEfficiencyMetrics.evictionHistory.forEach(eviction => {
      if (!strategies[eviction.strategy]) {
        strategies[eviction.strategy] = {
          count: 0,
          totalEfficiency: 0,
          totalTime: 0,
          avgEfficiency: 0,
          avgTime: 0
        };
      }
      
      const strategy = strategies[eviction.strategy];
      strategy.count++;
      strategy.totalEfficiency += eviction.efficiency;
      strategy.totalTime += eviction.evictionTime;
      strategy.avgEfficiency = strategy.totalEfficiency / strategy.count;
      strategy.avgTime = strategy.totalTime / strategy.count;
    });
    
    // Format for readability
    Object.keys(strategies).forEach(key => {
      const strategy = strategies[key];
      strategy.avgEfficiency = strategy.avgEfficiency.toFixed(1) + '%';
      strategy.avgTime = strategy.avgTime.toFixed(2) + 'ms';
    });
    
    return strategies;
  }

  /**
   * Generate cache optimization recommendations based on current performance
   * @returns {Array} - List of optimization recommendations
   */
  generateCacheOptimizationRecommendations() {
    const recommendations = [];
    const metrics = this.getCacheEfficiencyMetrics();
    const hitRatio = parseFloat(metrics.hitRatio);
    const utilization = parseFloat(metrics.utilization);
    
    // Hit ratio recommendations
    if (hitRatio < 70) {
      recommendations.push({
        type: 'performance',
        priority: 'high',
        issue: 'Low cache hit ratio',
        recommendation: 'Consider increasing cache size or improving element access patterns',
        currentValue: metrics.hitRatio,
        targetValue: '80%+'
      });
    }
    
    // Utilization recommendations
    if (utilization > 90) {
      recommendations.push({
        type: 'memory',
        priority: 'medium',
        issue: 'High cache utilization',
        recommendation: 'Consider increasing max cache size or more aggressive cleanup',
        currentValue: metrics.utilization,
        targetValue: '70-85%'
      });
    }
    
    // Cold elements recommendations
    const coldRatio = parseFloat(metrics.coldElementRatio);
    if (coldRatio > 30) {
      recommendations.push({
        type: 'efficiency',
        priority: 'medium',
        issue: 'High ratio of cold elements',
        recommendation: 'Enable more frequent maintenance cleanup to remove unused elements',
        currentValue: metrics.coldElementRatio,
        targetValue: '<20%'
      });
    }
    
    // Memory pressure recommendations
    const memoryPressure = parseFloat(metrics.memoryPressureScore);
    if (memoryPressure > 0.7) {
      recommendations.push({
        type: 'memory',
        priority: 'high',
        issue: 'High memory pressure detected',
        recommendation: 'Enable adaptive processing and consider reducing cache size limits',
        currentValue: metrics.memoryPressureScore,
        targetValue: '<0.5'
      });
    }
    
    // Eviction efficiency recommendations
    const evictionEfficiency = parseFloat(metrics.avgEvictionEfficiency);
    if (evictionEfficiency < 80) {
      recommendations.push({
        type: 'performance',
        priority: 'low',
        issue: 'Low eviction efficiency',
        recommendation: 'Review element tracking accuracy and cleanup strategies',
        currentValue: metrics.avgEvictionEfficiency,
        targetValue: '90%+'
      });
    }
    
    return recommendations;
  }

  /**
   * Force cache cleanup - useful for memory management
   */
  forceCacheCleanup() {
    const oldSize = this.cacheSize;
    
    // Clear cache data structures
    // WeakMaps don't have `.clear()`, so we reset by reassignment.
    this.textNodeCache = new WeakMap();
    this.cacheAccessOrder = new WeakMap();

    // Clear access order tracking
    this.lruRefs.clear();
    this.lruTimestamps.clear();
    
    // Clear temperature tracking
    this.cacheEfficiencyMetrics.hotElements.clear();
    this.cacheEfficiencyMetrics.coldElements.clear();
    
    // Reset cache size (WeakMap will be cleaned up by garbage collection)
    this.cacheSize = 0;
    
    // Reset efficiency metrics
    this.cacheEfficiencyMetrics.cacheHitStreak = 0;
    this.cacheEfficiencyMetrics.cacheMissStreak = 0;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Enhanced forced cache cleanup:', {
        previousSize: oldSize,
        newSize: this.cacheSize,
        accessOrderCleared: true,
        temperatureTrackingCleared: true,
        streaksReset: true
      });
    }
  }

  /**
   * Cleanup the intersection observer and resources
   */
  cleanup() {
    if (this.textObserver) {
      this.textObserver.disconnect();
      this.textObserver = null;
    }
    
    if (this.rectangleRoot && this.rectangleRoot.parentNode) {
      this.rectangleRoot.parentNode.removeChild(this.rectangleRoot);
      this.rectangleRoot = null;
    }
    
    this.intersectingTextElements.clear();
    this.intersectingTextNodes.clear();
    this.elementStates.clear();
    this.textNodeCache = new WeakMap();
    
    // Clean up enhanced LRU cache management
    // WeakMap doesn't have `.clear()`, so reset by reassignment.
    this.cacheAccessOrder = new WeakMap();
    this.lruRefs.clear();
    this.lruTimestamps.clear();
    this.cacheSize = 0;
    
    // Clean up cache efficiency metrics
    this.cacheEfficiencyMetrics.hotElements.clear();
    this.cacheEfficiencyMetrics.coldElements.clear();
    this.cacheEfficiencyMetrics.evictionHistory = [];
    this.cacheEfficiencyMetrics.totalCacheOperations = 0;
    this.cacheEfficiencyMetrics.cacheHitStreak = 0;
    this.cacheEfficiencyMetrics.longestHitStreak = 0;
    this.cacheEfficiencyMetrics.cacheMissStreak = 0;
    this.cacheEfficiencyMetrics.longestMissStreak = 0;
    this.cacheEfficiencyMetrics.averageAccessTime = 0;
    this.cacheEfficiencyMetrics.totalAccessTime = 0;
    this.cacheEfficiencyMetrics.memoryPressureEvents = 0;
    this.cacheEfficiencyMetrics.adaptiveCleanupTriggers = 0;
    
    // Clean up adaptive processing timers (Task 2.1)
    if (this.adaptiveProcessor && this.adaptiveProcessor.enabled) {
      this.cleanupAdaptiveProcessing();
    }
    
    // Reset metrics
    Object.keys(this.metrics).forEach(key => {
      if (typeof this.metrics[key] === 'number') {
        this.metrics[key] = 0;
      }
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Enhanced rectangle intersection observer cleaned up');
    }
  }

  /**
   * Clean up adaptive processing resources (Task 2.1)
   */
  cleanupAdaptiveProcessing() {
    const batchProcessor = this.adaptiveProcessor.batchProcessor;
    
    // Clear batch timer
    if (batchProcessor.batchTimer) {
      clearTimeout(batchProcessor.batchTimer);
      batchProcessor.batchTimer = null;
    }
    
    // Clear pending batch
    batchProcessor.pendingBatch = [];
    
    // Clear frame monitoring data
    this.adaptiveProcessor.frameRateMonitor.frameTimes = [];
    
    // Clear quality history
    this.adaptiveProcessor.qualityController.qualityHistory = [];
    
    // Clean up predictive caching (Task 2.2)
    if (this.predictiveCaching && this.predictiveCaching.enabled) {
      this.cleanupPredictiveCaching();
    }
    
    // Clean up enhanced analytics (Task 2.3)
    if (this.enhancedAnalytics && this.enhancedAnalytics.enabled) {
      this.cleanupEnhancedAnalytics();
    }
    
    // Clean up performance monitoring (Task 2.3) - now integrated
    if (this.performanceMonitoringEnabled) {
      // Performance monitoring cleanup is handled by integrated system
      this.performanceState.isActive = false;
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Adaptive processing cleaned up');
    }
  }

  /**
   * Clean up predictive caching resources (Task 2.2)
   */
  cleanupPredictiveCaching() {
    const preloader = this.predictiveCaching.preloader;
    
    // Clear preload timer
    if (preloader.preloadTimer) {
      clearInterval(preloader.preloadTimer);
      preloader.preloadTimer = null;
    }
    
    // Clear preload queue
    preloader.preloadQueue = [];
    
    // Clear behavior analysis data
    this.predictiveCaching.behaviorAnalyzer.interactionHistory = [];
    this.predictiveCaching.behaviorAnalyzer.patterns.clear();
    
    // Clear scroll prediction data
    this.predictiveCaching.scrollPredictor.scrollHistory = [];
    
    // Clear viewport cache
    this.predictiveCaching.viewportCaching.sectorCache.clear();
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Predictive caching cleaned up');
    }
  }

  /**
   * Clean up enhanced analytics resources (Task 2.3)
   */
  cleanupEnhancedAnalytics() {
    const analytics = this.enhancedAnalytics;
    
    // Clear dashboard timer
    if (analytics.dashboard.updateTimer) {
      clearInterval(analytics.dashboard.updateTimer);
      analytics.dashboard.updateTimer = null;
    }
    
    // Clear trend analysis timer
    if (analytics.trendAnalyzer.analysisTimer) {
      clearInterval(analytics.trendAnalyzer.analysisTimer);
      analytics.trendAnalyzer.analysisTimer = null;
    }
    
    // Clear memory sampling timer
    if (analytics.memoryAnalyzer.samplingTimer) {
      clearInterval(analytics.memoryAnalyzer.samplingTimer);
      analytics.memoryAnalyzer.samplingTimer = null;
    }
    
    // Clear analytics data
    Object.keys(analytics.dashboard.currentMetrics).forEach(metric => {
      analytics.dashboard.currentMetrics[metric] = [];
    });
    
    analytics.trendAnalyzer.trends.clear();
    analytics.regressionDetector.regressions = [];
    analytics.memoryAnalyzer.memorySamples = [];
    analytics.comparisonAnalyzer.edgeOnlySamples = [];
    analytics.comparisonAnalyzer.spatialSamples = [];
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Enhanced analytics cleaned up');
    }
  }

  /**
   * Update analytics with new performance data (Task 2.3)
   * @param {Object} data - Current performance data
   */
  updateAnalyticsWithNewData(data) {
    // Update regression detection
    this.updateRegressionDetection(data);
    
    // Update comparison analytics
    this.updateComparisonAnalytics(data);
    
    // Record sample for trend analysis (handled by dashboard update)
  }

  /**
   * Update regression detection with new data (Task 2.3)
   * @param {Object} data - Current performance data
   */
  updateRegressionDetection(data) {
    const regressionDetector = this.enhancedAnalytics.regressionDetector;
    
    // Update baseline metrics
    regressionDetector.baselineMetrics.forEach((baseline, metric) => {
      if (data[metric] !== undefined) {
        baseline.samples.push(data[metric]);
        
        // Keep only recent samples
        if (baseline.samples.length > regressionDetector.detectionWindow) {
          baseline.samples.shift();
        }
        
        // Establish baseline after enough samples
        if (!baseline.established && baseline.samples.length >= 10) {
          baseline.baseline = baseline.samples.reduce((a, b) => a + b, 0) / baseline.samples.length;
          baseline.established = true;
        }
        
        // Check for regression
        if (baseline.established) {
          this.checkForRegression(metric, data[metric], baseline);
        }
      }
    });
  }

  /**
   * Check for performance regression (Task 2.3)
   * @param {string} metric - Metric name
   * @param {number} currentValue - Current metric value
   * @param {Object} baseline - Baseline data
   */
  checkForRegression(metric, currentValue, baseline) {
    const regressionDetector = this.enhancedAnalytics.regressionDetector;
    
    let isRegression = false;
    
    // Different regression logic for different metrics
    switch (metric) {
      case 'processingTime':
      case 'memoryUsage':
        // Higher values are worse
        isRegression = currentValue > baseline.baseline * regressionDetector.thresholdFactor;
        break;
      case 'cacheHitRatio':
      case 'performanceGain':
        // Lower values are worse
        isRegression = currentValue < baseline.baseline / regressionDetector.thresholdFactor;
        break;
    }
    
    if (isRegression) {
      regressionDetector.consecutivePoorSamples++;
      
      if (regressionDetector.consecutivePoorSamples >= regressionDetector.consecutiveThreshold) {
        this.alertPerformanceRegression(metric, currentValue, baseline.baseline);
        regressionDetector.consecutivePoorSamples = 0; // Reset to avoid spam
      }
    } else {
      // Check for recovery
      const isRecovery = this.isPerformanceRecovery(metric, currentValue, baseline.baseline);
      if (isRecovery && regressionDetector.consecutivePoorSamples > 0) {
        regressionDetector.consecutivePoorSamples = 0;
        
        if (this.debugLogging) {
          console.log('[KeyPilot Debug] Performance recovery detected:', {
            metric,
            currentValue,
            baseline: baseline.baseline
          });
        }
      }
    }
  }

  /**
   * Check if current performance indicates recovery (Task 2.3)
   * @param {string} metric - Metric name
   * @param {number} currentValue - Current metric value
   * @param {number} baselineValue - Baseline metric value
   * @returns {boolean} - True if performance has recovered
   */
  isPerformanceRecovery(metric, currentValue, baselineValue) {
    const recoveryThreshold = this.enhancedAnalytics.regressionDetector.recoveryThreshold;
    
    switch (metric) {
      case 'processingTime':
      case 'memoryUsage':
        return currentValue <= baselineValue * (1 + (1 - recoveryThreshold));
      case 'cacheHitRatio':
      case 'performanceGain':
        return currentValue >= baselineValue * recoveryThreshold;
      default:
        return false;
    }
  }

  /**
   * Update comparison analytics (Task 2.3)
   * @param {Object} data - Current performance data
   */
  updateComparisonAnalytics(data) {
    const comparisonAnalyzer = this.enhancedAnalytics.comparisonAnalyzer;
    
    // Add sample to edge-only data (current method)
    comparisonAnalyzer.edgeOnlySamples.push({
      processingTime: data.processingTime,
      memoryUsage: data.memoryUsage,
      cacheEfficiency: data.cacheHitRatio,
      timestamp: data.timestamp
    });
    
    // Keep only recent samples
    if (comparisonAnalyzer.edgeOnlySamples.length > comparisonAnalyzer.sampleSize) {
      comparisonAnalyzer.edgeOnlySamples.shift();
    }
    
    // Perform comparison if we have enough samples
    if (comparisonAnalyzer.edgeOnlySamples.length >= 20) {
      this.performStatisticalComparison();
    }
  }

  /**
   * Perform statistical comparison between methods (Task 2.3)
   */
  performStatisticalComparison() {
    const comparisonAnalyzer = this.enhancedAnalytics.comparisonAnalyzer;
    const now = Date.now();
    
    // Only perform comparison periodically
    if (now - comparisonAnalyzer.lastComparison < 30000) {
      return;
    }
    
    const edgeSamples = comparisonAnalyzer.edgeOnlySamples;
    
    // Calculate statistics for edge-only method
    const edgeProcessingTimes = edgeSamples.map(s => s.processingTime);
    const edgeMemoryUsage = edgeSamples.map(s => s.memoryUsage);
    
    const edgeStats = {
      processingTime: this.calculateStatistics(edgeProcessingTimes),
      memoryUsage: this.calculateStatistics(edgeMemoryUsage)
    };
    
    // Estimate spatial method performance (based on observed patterns)
    const estimatedSpatialStats = this.estimateSpatialMethodPerformance(edgeStats);
    
    // Update comparison results
    comparisonAnalyzer.comparisonResults.set('processingTime', {
      edgeOnlyMean: edgeStats.processingTime.mean,
      spatialMean: estimatedSpatialStats.processingTime.mean,
      edgeOnlyStdDev: edgeStats.processingTime.stdDev,
      spatialStdDev: estimatedSpatialStats.processingTime.stdDev,
      significantDifference: edgeStats.processingTime.mean < estimatedSpatialStats.processingTime.mean,
      effectSize: this.calculateEffectSize(edgeStats.processingTime, estimatedSpatialStats.processingTime),
      lastComparison: now
    });
    
    comparisonAnalyzer.lastComparison = now;
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Statistical comparison completed:', {
        edgeOnlyProcessingTime: edgeStats.processingTime.mean.toFixed(2) + 'ms',
        estimatedSpatialProcessingTime: estimatedSpatialStats.processingTime.mean.toFixed(2) + 'ms',
        performanceImprovement: ((estimatedSpatialStats.processingTime.mean - edgeStats.processingTime.mean) / estimatedSpatialStats.processingTime.mean * 100).toFixed(1) + '%'
      });
    }
  }

  /**
   * Calculate basic statistics for a dataset (Task 2.3)
   * @param {Array} data - Array of numbers
   * @returns {Object} - Statistics object
   */
  calculateStatistics(data) {
    if (data.length === 0) {
      return { mean: 0, stdDev: 0, min: 0, max: 0, median: 0 };
    }
    
    const sorted = [...data].sort((a, b) => a - b);
    const mean = data.reduce((a, b) => a + b, 0) / data.length;
    const variance = data.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / data.length;
    const stdDev = Math.sqrt(variance);
    const median = sorted.length % 2 === 0 
      ? (sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]) / 2
      : sorted[Math.floor(sorted.length / 2)];
    
    return {
      mean,
      stdDev,
      min: sorted[0],
      max: sorted[sorted.length - 1],
      median
    };
  }

  /**
   * Estimate spatial method performance based on edge-only data (Task 2.3)
   * @param {Object} edgeStats - Edge-only method statistics
   * @returns {Object} - Estimated spatial method statistics
   */
  estimateSpatialMethodPerformance(edgeStats) {
    // Based on observed patterns, spatial method is typically 10-50x slower
    const spatialMultiplier = 25; // Conservative estimate
    
    return {
      processingTime: {
        mean: edgeStats.processingTime.mean * spatialMultiplier,
        stdDev: edgeStats.processingTime.stdDev * spatialMultiplier
      },
      memoryUsage: {
        mean: edgeStats.memoryUsage.mean * 1.5, // Spatial uses more memory
        stdDev: edgeStats.memoryUsage.stdDev * 1.5
      }
    };
  }

  /**
   * Calculate effect size between two methods (Task 2.3)
   * @param {Object} method1Stats - Statistics for method 1
   * @param {Object} method2Stats - Statistics for method 2
   * @returns {number} - Cohen's d effect size
   */
  calculateEffectSize(method1Stats, method2Stats) {
    const pooledStdDev = Math.sqrt(
      (Math.pow(method1Stats.stdDev, 2) + Math.pow(method2Stats.stdDev, 2)) / 2
    );
    
    if (pooledStdDev === 0) return 0;
    
    return Math.abs(method1Stats.mean - method2Stats.mean) / pooledStdDev;
  }

  /**
   * Alert on performance trend (Task 2.3)
   * @param {string} metric - Metric name
   * @param {Object} trendData - Trend analysis data
   */
  alertPerformanceTrend(metric, trendData) {
    if (this.debugLogging) {
      console.warn('[KeyPilot Debug] Performance trend alert:', {
        metric,
        trend: trendData.trend,
        confidence: trendData.confidence.toFixed(2),
        slope: trendData.slope.toFixed(4)
      });
    }
    
    // Could trigger user notification or logging here
  }

  /**
   * Alert on performance regression (Task 2.3)
   * @param {string} metric - Metric name
   * @param {number} currentValue - Current metric value
   * @param {number} baselineValue - Baseline metric value
   */
  alertPerformanceRegression(metric, currentValue, baselineValue) {
    const degradationPercent = ((currentValue - baselineValue) / baselineValue * 100).toFixed(1);
    
    if (this.debugLogging) {
      console.warn('[KeyPilot Debug] Performance regression detected:', {
        metric,
        currentValue: currentValue.toFixed(2),
        baselineValue: baselineValue.toFixed(2),
        degradation: degradationPercent + '%'
      });
    }
    
    // Could trigger user notification or automatic fallback here
  }

  /**
   * Detect memory leaks (Task 2.3)
   */
  detectMemoryLeaks() {
    const memoryAnalyzer = this.enhancedAnalytics.memoryAnalyzer;
    const samples = memoryAnalyzer.memorySamples;
    
    if (samples.length < memoryAnalyzer.leakDetectionWindow) {
      return;
    }
    
    // Check for consistent memory growth
    const recentSamples = samples.slice(-memoryAnalyzer.leakDetectionWindow);
    const memoryValues = recentSamples.map(s => s.totalMemory);
    
    // Calculate trend
    const timestamps = recentSamples.map(s => s.timestamp);
    const regression = this.calculateLinearRegression(timestamps, memoryValues);
    
    // Detect leak if memory is consistently growing
    const avgMemory = memoryValues.reduce((a, b) => a + b, 0) / memoryValues.length;
    const growthRate = regression.slope / avgMemory;
    
    const wasLeakDetected = memoryAnalyzer.leakDetected;
    memoryAnalyzer.leakDetected = growthRate > memoryAnalyzer.growthThreshold && regression.rSquared > 0.7;
    
    if (memoryAnalyzer.leakDetected && !wasLeakDetected) {
      if (this.debugLogging) {
        console.warn('[KeyPilot Debug] Memory leak detected:', {
          growthRate: (growthRate * 100).toFixed(2) + '%',
          confidence: regression.rSquared.toFixed(2),
          avgMemory: avgMemory.toFixed(1) + 'MB'
        });
      }
    }
  }

  /**
   * Detect memory pressure (Task 2.3)
   * @param {Object} sample - Memory usage sample
   */
  detectMemoryPressure(sample) {
    const memoryAnalyzer = this.enhancedAnalytics.memoryAnalyzer;
    const pressureThreshold = memoryAnalyzer.pressureThreshold;
    
    // Calculate memory pressure based on heap usage
    const memoryPressure = sample.heapUsed / sample.heapTotal;
    
    const wasPressureDetected = memoryAnalyzer.pressureDetected;
    memoryAnalyzer.pressureDetected = memoryPressure > pressureThreshold;
    
    if (memoryAnalyzer.pressureDetected && !wasPressureDetected) {
      if (this.debugLogging) {
        console.warn('[KeyPilot Debug] Memory pressure detected:', {
          heapUsed: sample.heapUsed.toFixed(1) + 'MB',
          heapTotal: sample.heapTotal.toFixed(1) + 'MB',
          pressure: (memoryPressure * 100).toFixed(1) + '%',
          threshold: (pressureThreshold * 100).toFixed(1) + '%'
        });
      }
      
      // Trigger aggressive cache cleanup
      this.performAggressiveCleanup();
    }
  }

  /**
   * Get cache hit ratio as numeric value (Task 2.3)
   * @returns {number} - Cache hit ratio (0-1)
   */
  getCacheHitRatioNumeric() {
    const totalAttempts = this.metrics.cacheHits + this.metrics.cacheMisses;
    return totalAttempts > 0 ? this.metrics.cacheHits / totalAttempts : 0;
  }

  /**
   * Get performance gain as numeric value (Task 2.3)
   * @returns {number} - Performance gain (0-1)
   */
  getPerformanceGainNumeric() {
    if (this.metrics.estimatedFullTraversalTime === 0) {
      return 0;
    }
    
    const actualTime = this.metrics.actualEdgeProcessingTime;
    const estimatedTime = this.metrics.estimatedFullTraversalTime;
    
    return Math.max(0, (estimatedTime - actualTime) / estimatedTime);
  }

  /**
   * Get estimated memory usage (Task 2.3)
   * @returns {number} - Estimated memory usage in MB
   */
  getEstimatedMemoryUsage() {
    // Rough estimate based on cache size and other factors
    const cacheMemory = this.cacheSize * 0.001; // ~1KB per cached element
    const systemMemory = 2; // Base system memory usage
    
    return cacheMemory + systemMemory;
  }

  /**
   * Get detailed memory usage breakdown (Task 2.3)
   * @returns {Object} - Detailed memory usage information
   */
  getDetailedMemoryUsage() {
    const cacheMemory = this.cacheSize * 0.001;
    const systemMemory = 2;
    const total = cacheMemory + systemMemory;
    
    // Try to get actual heap usage if available
    let heapUsed = total;
    let heapTotal = total * 2;
    
    if (performance.memory) {
      heapUsed = performance.memory.usedJSHeapSize / (1024 * 1024);
      heapTotal = performance.memory.totalJSHeapSize / (1024 * 1024);
    }
    
    return {
      total,
      cache: cacheMemory,
      system: systemMemory,
      heapUsed,
      heapTotal
    };
  }

  /**
   * Find elements in a specific area (Task 2.2)
   * @param {Object} area - Area with left, top, right, bottom properties
   * @returns {Array} - Elements in the area
   */
  findElementsInArea(area) {
    const elements = [];
    const textContainerSelectors = [
      'p', 'div', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
      'li', 'td', 'th', 'blockquote', 'pre', 'code',
      'a', 'strong', 'em', 'b', 'i', 'u',
      'article', 'section', 'main', 'aside'
    ].join(', ');
    
    const allElements = document.querySelectorAll(textContainerSelectors);
    
    for (const element of allElements) {
      if (!this.elementContainsText(element)) {
        continue;
      }
      
      const rect = element.getBoundingClientRect();
      const elementArea = {
        left: rect.left + window.scrollX,
        top: rect.top + window.scrollY,
        right: rect.right + window.scrollX,
        bottom: rect.bottom + window.scrollY
      };
      
      // Check if element intersects with the area
      if (elementArea.right >= area.left &&
          elementArea.left <= area.right &&
          elementArea.bottom >= area.top &&
          elementArea.top <= area.bottom) {
        elements.push(element);
      }
    }
    
    return elements;
  }

  /**
   * Preload text nodes for an element (Task 2.2)
   * @param {Element} element - Element to preload
   * @param {string} reason - Reason for preloading
   */
  preloadElementTextNodes(element, reason) {
    const preloader = this.predictiveCaching.preloader;
    
    // Check if already preloaded or in cache
    if (this.textNodeCache.has(element) || preloader.preloadCache.has(element)) {
      return;
    }
    
    // Add to preload queue
    preloader.preloadQueue.push({
      element,
      reason,
      priority: this.calculatePreloadPriority(element, reason),
      timestamp: Date.now()
    });
    
    // Sort queue by priority
    preloader.preloadQueue.sort((a, b) => b.priority - a.priority);
    
    // Limit queue size
    if (preloader.preloadQueue.length > preloader.maxElements) {
      preloader.preloadQueue = preloader.preloadQueue.slice(0, preloader.maxElements);
    }
  }

  /**
   * Calculate preload priority for an element (Task 2.2)
   * @param {Element} element - Element to calculate priority for
   * @param {string} reason - Reason for preloading
   * @returns {number} - Priority score (0-1)
   */
  calculatePreloadPriority(element, reason) {
    let priority = 0.5; // Base priority
    
    // Adjust based on reason
    switch (reason) {
      case 'viewport':
        priority += 0.3;
        break;
      case 'scroll_prediction':
        priority += 0.4;
        break;
      case 'behavior_pattern':
        priority += 0.2;
        break;
    }
    
    // Adjust based on element characteristics
    const textLength = element.textContent?.length || 0;
    if (textLength > 100) {
      priority += 0.1; // Prefer elements with more text
    }
    
    // Adjust based on element visibility
    const rect = element.getBoundingClientRect();
    if (rect.width > 0 && rect.height > 0) {
      priority += 0.1; // Prefer visible elements
    }
    
    // Adjust based on element type
    const tagName = element.tagName.toLowerCase();
    if (['p', 'div', 'article', 'section'].includes(tagName)) {
      priority += 0.1; // Prefer content elements
    }
    
    return Math.min(1, Math.max(0, priority));
  }

  /**
   * Execute a preload request (Task 2.2)
   * @param {Object} request - Preload request
   */
  executePreloadRequest(request) {
    const { element, reason, priority } = request;
    const preloader = this.predictiveCaching.preloader;
    
    try {
      // Extract text nodes (similar to extractTextNodesFromElement but for preloading)
      const textNodes = [];
      const walker = document.createTreeWalker(
        element,
        NodeFilter.SHOW_TEXT,
        {
          acceptNode: (node) => {
            if (!node.textContent || !node.textContent.trim()) {
              return NodeFilter.FILTER_REJECT;
            }
            
            const parent = node.parentElement;
            if (parent && this.isNonContentElement(parent)) {
              return NodeFilter.FILTER_REJECT;
            }
            
            return NodeFilter.FILTER_ACCEPT;
          }
        }
      );
      
      let node;
      while (node = walker.nextNode()) {
        textNodes.push(node);
      }
      
      // Cache the preloaded text nodes
      this.textNodeCache.set(element, textNodes);
      this.cacheTextNodes(element, textNodes);
      
      // Mark as preloaded
      preloader.preloadCache.set(element, {
        reason,
        priority,
        timestamp: Date.now(),
        textNodeCount: textNodes.length
      });
      
      if (this.debugLogging) {
        console.log('[KeyPilot Debug] Element preloaded:', {
          element: element.tagName,
          reason,
          priority: priority.toFixed(2),
          textNodes: textNodes.length
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error preloading element:', error);
    }
  }

  /**
   * Preload elements for consistent rectangle sizes (Task 2.2)
   */
  preloadForConsistentSizes() {
    // Implementation for consistent size pattern
    const viewport = this.predictiveCaching.viewportCaching.currentViewport;
    const expandedArea = {
      left: viewport.x - 100,
      top: viewport.y - 100,
      right: viewport.x + viewport.width + 100,
      bottom: viewport.y + viewport.height + 100
    };
    
    const elements = this.findElementsInArea(expandedArea);
    elements.forEach(element => {
      this.preloadElementTextNodes(element, 'behavior_pattern');
    });
  }

  /**
   * Preload elements in repeated areas (Task 2.2)
   */
  preloadRepeatedAreas() {
    // Implementation for repeated areas pattern
    const behaviorAnalyzer = this.predictiveCaching.behaviorAnalyzer;
    const recentInteractions = behaviorAnalyzer.interactionHistory.slice(-5);
    
    recentInteractions.forEach(interaction => {
      if (interaction.type === 'rectangle_update') {
        const rect = interaction.data.rect;
        const area = {
          left: rect.left - 50,
          top: rect.top - 50,
          right: rect.left + rect.width + 50,
          bottom: rect.top + rect.height + 50
        };
        
        const elements = this.findElementsInArea(area);
        elements.forEach(element => {
          this.preloadElementTextNodes(element, 'behavior_pattern');
        });
      }
    });
  }

  /**
   * Preload elements for vertical scrolling (Task 2.2)
   */
  preloadVerticalScrollAreas() {
    const viewport = this.predictiveCaching.viewportCaching.currentViewport;
    const preloadArea = {
      left: viewport.x,
      top: viewport.y + viewport.height,
      right: viewport.x + viewport.width,
      bottom: viewport.y + viewport.height + 300
    };
    
    const elements = this.findElementsInArea(preloadArea);
    elements.forEach(element => {
      this.preloadElementTextNodes(element, 'behavior_pattern');
    });
  }

  /**
   * Preload elements for horizontal scrolling (Task 2.2)
   */
  preloadHorizontalScrollAreas() {
    const viewport = this.predictiveCaching.viewportCaching.currentViewport;
    const preloadArea = {
      left: viewport.x + viewport.width,
      top: viewport.y,
      right: viewport.x + viewport.width + 300,
      bottom: viewport.y + viewport.height
    };
    
    const elements = this.findElementsInArea(preloadArea);
    elements.forEach(element => {
      this.preloadElementTextNodes(element, 'behavior_pattern');
    });
  }

  /**
   * Initialize enhanced performance monitoring and analytics (Task 2.3)
   */
  initializeEnhancedAnalytics() {
    // Initialize real-time dashboard
    if (this.enhancedAnalytics.dashboard.enabled) {
      this.initializePerformanceDashboard();
    }
    
    // Initialize trend analysis
    if (this.enhancedAnalytics.trendAnalyzer.enabled) {
      this.initializeTrendAnalysis();
    }
    
    // Initialize regression detection
    if (this.enhancedAnalytics.regressionDetector.enabled) {
      this.initializeRegressionDetection();
    }
    
    // Initialize memory analytics
    if (this.enhancedAnalytics.memoryAnalyzer.enabled) {
      this.initializeMemoryAnalytics();
    }
    
    // Initialize comparison analytics
    if (this.enhancedAnalytics.comparisonAnalyzer.enabled) {
      this.initializeComparisonAnalytics();
    }
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Enhanced analytics initialized:', {
        dashboard: this.enhancedAnalytics.dashboard.enabled,
        trendAnalysis: this.enhancedAnalytics.trendAnalyzer.enabled,
        regressionDetection: this.enhancedAnalytics.regressionDetector.enabled,
        memoryAnalytics: this.enhancedAnalytics.memoryAnalyzer.enabled,
        comparisonAnalytics: this.enhancedAnalytics.comparisonAnalyzer.enabled
      });
    }
  }

  /**
   * Initialize real-time performance dashboard (Task 2.3)
   */
  initializePerformanceDashboard() {
    const dashboard = this.enhancedAnalytics.dashboard;
    
    // Start dashboard update timer
    dashboard.updateTimer = setInterval(() => {
      this.updatePerformanceDashboard();
    }, dashboard.updateInterval);
    
    // Initialize metrics arrays
    Object.keys(dashboard.currentMetrics).forEach(metric => {
      dashboard.currentMetrics[metric] = [];
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Performance dashboard initialized:', {
        updateInterval: dashboard.updateInterval + 'ms',
        historyWindow: dashboard.historyWindow,
        metricsBuffer: dashboard.metricsBuffer
      });
    }
  }

  /**
   * Update real-time performance dashboard (Task 2.3)
   */
  updatePerformanceDashboard() {
    const dashboard = this.enhancedAnalytics.dashboard;
    const now = Date.now();
    
    // Collect current metrics
    const currentData = {
      timestamp: now,
      processingTime: this.metrics.edgeProcessingTime || 0,
      cacheHitRatio: this.getCacheHitRatioNumeric(),
      memoryUsage: this.getEstimatedMemoryUsage(),
      elementsProcessed: this.metrics.edgeElementsProcessed || 0,
      performanceGain: this.getPerformanceGainNumeric(),
      frameRate: this.adaptiveProcessor?.frameRateMonitor?.currentFps || 60
    };
    
    // Add to metrics arrays
    Object.keys(currentData).forEach(metric => {
      if (metric !== 'timestamp') {
        dashboard.currentMetrics[metric].push({
          value: currentData[metric],
          timestamp: now
        });
        
        // Keep only recent data
        if (dashboard.currentMetrics[metric].length > dashboard.historyWindow) {
          dashboard.currentMetrics[metric].shift();
        }
      }
    });
    
    // Update analytics with new data
    this.updateAnalyticsWithNewData(currentData);
    
    dashboard.lastUpdate = now;
    
    if (this.debugLogging && now % 10000 < dashboard.updateInterval) {
      console.log('[KeyPilot Debug] Dashboard updated:', {
        processingTime: currentData.processingTime.toFixed(2) + 'ms',
        cacheHitRatio: (currentData.cacheHitRatio * 100).toFixed(1) + '%',
        memoryUsage: currentData.memoryUsage.toFixed(1) + 'MB',
        performanceGain: (currentData.performanceGain * 100).toFixed(1) + '%',
        frameRate: currentData.frameRate.toFixed(1) + 'fps'
      });
    }
  }

  /**
   * Initialize trend analysis system (Task 2.3)
   */
  initializeTrendAnalysis() {
    const trendAnalyzer = this.enhancedAnalytics.trendAnalyzer;
    
    // Start trend analysis timer
    trendAnalyzer.analysisTimer = setInterval(() => {
      this.analyzePerfomanceTrends();
    }, trendAnalyzer.analysisInterval);
    
    // Initialize trend tracking for key metrics
    const metricsToTrack = [
      'processingTime', 'cacheHitRatio', 'memoryUsage', 
      'performanceGain', 'frameRate'
    ];
    
    metricsToTrack.forEach(metric => {
      trendAnalyzer.trends.set(metric, {
        samples: [],
        trend: 'stable', // 'improving', 'degrading', 'stable'
        confidence: 0,
        slope: 0,
        lastAnalysis: 0
      });
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Trend analysis initialized:', {
        analysisInterval: trendAnalyzer.analysisInterval + 'ms',
        analysisWindow: trendAnalyzer.analysisWindow,
        metricsTracked: metricsToTrack.length
      });
    }
  }

  /**
   * Analyze performance trends (Task 2.3)
   */
  analyzePerfomanceTrends() {
    const trendAnalyzer = this.enhancedAnalytics.trendAnalyzer;
    const dashboard = this.enhancedAnalytics.dashboard;
    
    // Analyze trends for each metric
    trendAnalyzer.trends.forEach((trendData, metric) => {
      const metricData = dashboard.currentMetrics[metric];
      
      if (metricData.length < trendAnalyzer.analysisWindow) {
        return; // Not enough data for analysis
      }
      
      // Get recent samples
      const recentSamples = metricData.slice(-trendAnalyzer.analysisWindow);
      const values = recentSamples.map(sample => sample.value);
      const timestamps = recentSamples.map(sample => sample.timestamp);
      
      // Calculate linear regression
      const regression = this.calculateLinearRegression(timestamps, values);
      
      // Determine trend direction
      let trend = 'stable';
      const normalizedSlope = Math.abs(regression.slope) / (Math.max(...values) - Math.min(...values) + 0.001);
      
      if (normalizedSlope > trendAnalyzer.significanceThreshold) {
        if (regression.slope > 0) {
          trend = metric === 'processingTime' || metric === 'memoryUsage' ? 'degrading' : 'improving';
        } else {
          trend = metric === 'processingTime' || metric === 'memoryUsage' ? 'improving' : 'degrading';
        }
      }
      
      // Update trend data
      trendData.trend = trend;
      trendData.confidence = regression.rSquared;
      trendData.slope = regression.slope;
      trendData.lastAnalysis = Date.now();
      
      // Alert on significant trends
      if (trendData.confidence > trendAnalyzer.confidenceThreshold && trend === 'degrading') {
        this.alertPerformanceTrend(metric, trendData);
      }
    });
    
    trendAnalyzer.lastAnalysis = Date.now();
    
    if (this.debugLogging) {
      const trendSummary = {};
      trendAnalyzer.trends.forEach((data, metric) => {
        trendSummary[metric] = {
          trend: data.trend,
          confidence: data.confidence.toFixed(2)
        };
      });
      
      console.log('[KeyPilot Debug] Trend analysis completed:', trendSummary);
    }
  }

  /**
   * Calculate linear regression for trend analysis (Task 2.3)
   * @param {Array} x - X values (timestamps)
   * @param {Array} y - Y values (metric values)
   * @returns {Object} - Regression results
   */
  calculateLinearRegression(x, y) {
    const n = x.length;
    if (n === 0) return { slope: 0, intercept: 0, rSquared: 0 };
    
    // Normalize timestamps to prevent numerical issues
    const minX = Math.min(...x);
    const normalizedX = x.map(val => val - minX);
    
    const sumX = normalizedX.reduce((a, b) => a + b, 0);
    const sumY = y.reduce((a, b) => a + b, 0);
    const sumXY = normalizedX.reduce((sum, xi, i) => sum + xi * y[i], 0);
    const sumXX = normalizedX.reduce((sum, xi) => sum + xi * xi, 0);
    const sumYY = y.reduce((sum, yi) => sum + yi * yi, 0);
    
    const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
    const intercept = (sumY - slope * sumX) / n;
    
    // Calculate R-squared
    const yMean = sumY / n;
    const ssRes = y.reduce((sum, yi, i) => {
      const predicted = slope * normalizedX[i] + intercept;
      return sum + Math.pow(yi - predicted, 2);
    }, 0);
    const ssTot = y.reduce((sum, yi) => sum + Math.pow(yi - yMean, 2), 0);
    const rSquared = ssTot === 0 ? 1 : 1 - (ssRes / ssTot);
    
    return { slope, intercept, rSquared: Math.max(0, rSquared) };
  }

  /**
   * Initialize regression detection system (Task 2.3)
   */
  initializeRegressionDetection() {
    const regressionDetector = this.enhancedAnalytics.regressionDetector;
    
    // Initialize baseline metrics
    const metricsToMonitor = [
      'processingTime', 'cacheHitRatio', 'memoryUsage', 'performanceGain'
    ];
    
    metricsToMonitor.forEach(metric => {
      regressionDetector.baselineMetrics.set(metric, {
        baseline: null,
        samples: [],
        established: false
      });
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Regression detection initialized:', {
        detectionWindow: regressionDetector.detectionWindow,
        thresholdFactor: regressionDetector.thresholdFactor,
        consecutiveThreshold: regressionDetector.consecutiveThreshold
      });
    }
  }

  /**
   * Initialize memory analytics system (Task 2.3)
   */
  initializeMemoryAnalytics() {
    const memoryAnalyzer = this.enhancedAnalytics.memoryAnalyzer;
    
    // Start memory sampling
    memoryAnalyzer.samplingTimer = setInterval(() => {
      this.sampleMemoryUsage();
    }, memoryAnalyzer.samplingInterval);
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Memory analytics initialized:', {
        samplingInterval: memoryAnalyzer.samplingInterval + 'ms',
        leakDetectionWindow: memoryAnalyzer.leakDetectionWindow,
        growthThreshold: (memoryAnalyzer.growthThreshold * 100).toFixed(1) + '%'
      });
    }
  }

  /**
   * Sample memory usage for analytics (Task 2.3)
   */
  sampleMemoryUsage() {
    const memoryAnalyzer = this.enhancedAnalytics.memoryAnalyzer;
    
    const memoryUsage = this.getDetailedMemoryUsage();
    const sample = {
      timestamp: Date.now(),
      totalMemory: memoryUsage.total,
      cacheMemory: memoryUsage.cache,
      systemMemory: memoryUsage.system,
      heapUsed: memoryUsage.heapUsed,
      heapTotal: memoryUsage.heapTotal
    };
    
    memoryAnalyzer.memorySamples.push(sample);
    
    // Keep only recent samples
    if (memoryAnalyzer.memorySamples.length > memoryAnalyzer.leakDetectionWindow) {
      memoryAnalyzer.memorySamples.shift();
    }
    
    // Detect memory leaks
    this.detectMemoryLeaks();
    
    // Detect memory pressure
    this.detectMemoryPressure(sample);
  }

  /**
   * Initialize comparison analytics system (Task 2.3)
   */
  initializeComparisonAnalytics() {
    const comparisonAnalyzer = this.enhancedAnalytics.comparisonAnalyzer;
    
    // Initialize comparison results for key metrics
    const metricsToCompare = [
      'processingTime', 'accuracy', 'memoryUsage', 'cacheEfficiency'
    ];
    
    metricsToCompare.forEach(metric => {
      comparisonAnalyzer.comparisonResults.set(metric, {
        edgeOnlyMean: 0,
        spatialMean: 0,
        edgeOnlyStdDev: 0,
        spatialStdDev: 0,
        significantDifference: false,
        pValue: 1,
        effectSize: 0,
        lastComparison: 0
      });
    });
    
    if (this.debugLogging) {
      console.log('[KeyPilot Debug] Comparison analytics initialized:', {
        sampleSize: comparisonAnalyzer.sampleSize,
        confidenceInterval: comparisonAnalyzer.confidenceInterval,
        significanceThreshold: comparisonAnalyzer.significanceThreshold
      });
    }
  }

  // ===== INTEGRATED PERFORMANCE MONITORING METHODS =====

  /**
   * Initialize integrated performance monitoring system
   */
  initializePerformanceMonitoring() {
    if (!this.performanceMonitoringEnabled) {
      return;
    }

    const config = PERFORMANCE_MONITORING;
    
    // Core performance metrics
    this.performanceMetrics = {
      // Element processing metrics
      elementsObserved: 0,
      elementsProcessed: 0,
      elementsEntering: 0,
      elementsLeaving: 0,
      
      // Processing time metrics
      totalProcessingTime: 0,
      averageProcessingTime: 0,
      maxProcessingTime: 0,
      minProcessingTime: Infinity,
      
      // Character detection metrics
      charactersDetected: 0,
      characterDetectionTime: 0,
      characterCacheHits: 0,
      characterCacheMisses: 0,
      
      // Smart targeting metrics
      elementReductionPercentage: 0,
      smartTargetingTime: 0,
      dynamicElementsAdded: 0,
      
      // Cache efficiency metrics
      cacheHitRate: 0,
      cacheEfficiency: 0,
      memoryUsage: 0,
      
      // Performance comparison metrics
      estimatedTraditionalTime: 0,
      actualOptimizedTime: 0,
      performanceGainPercentage: 0,
      speedupFactor: 1
    };
    
    // Historical data for trend analysis
    this.performanceHistory = {
      processingTimes: [],
      elementCounts: [],
      cacheHitRates: [],
      performanceGains: [],
      timestamps: []
    };
    
    // Performance monitoring state
    this.performanceState = {
      startTime: 0,
      sessionStartTime: Date.now(),
      totalOperations: 0,
      lastUpdate: 0,
      metricsBufferSize: config.METRICS_BUFFER_SIZE || 100,
      performanceThreshold: config.PERFORMANCE_THRESHOLD_MS || 50,
      baselinePerformance: null,
      regressionDetected: false,
      performanceDegradationFactor: 2.0,
      fallbackTriggered: false,
      isActive: true,
      alerts: []
    };
    
    // Performance alerts
    this.performanceAlerts = {
      performanceDegradation: false,
      highProcessingTime: false,
      lowCacheEfficiency: false,
      memoryPressure: false
    };
  }

  /**
   * Add performance alert to integrated system
   */
  addPerformanceAlert(type, data) {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    const alert = {
      type,
      data,
      timestamp: Date.now(),
      id: `alert_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
    };
    
    this.performanceState.alerts.push(alert);
    
    // Keep only recent alerts (last 50)
    if (this.performanceState.alerts.length > 50) {
      this.performanceState.alerts = this.performanceState.alerts.slice(-50);
    }
  }

  /**
   * Start performance monitoring session
   */
  startPerformanceMonitoring() {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    this.performanceState.startTime = performance.now();
    this.performanceState.sessionStartTime = Date.now();
    this.resetPerformanceMetrics();
  }

  /**
   * Record element processing performance metrics
   * @param {Object} data - Processing data
   */
  recordElementProcessing(data) {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    const {
      elementsProcessed = 0,
      elementsEntering = 0,
      elementsLeaving = 0,
      processingTime = 0,
      elementsObserved = 0
    } = data;
    
    // Update core metrics
    this.performanceMetrics.elementsProcessed += elementsProcessed;
    this.performanceMetrics.elementsEntering += elementsEntering;
    this.performanceMetrics.elementsLeaving += elementsLeaving;
    this.performanceMetrics.elementsObserved = elementsObserved;
    this.performanceMetrics.totalProcessingTime += processingTime;
    
    // Update processing time statistics
    this.updateProcessingTimeStats(processingTime);
    
    // Add to historical data
    this.addToPerformanceHistory('processingTimes', processingTime);
    this.addToPerformanceHistory('elementCounts', elementsProcessed);
    this.addToPerformanceHistory('timestamps', Date.now());
    
    // Check for performance alerts
    this.checkPerformanceAlerts(processingTime);
    
    this.performanceState.totalOperations++;
    this.performanceState.lastUpdate = Date.now();
  }

  /**
   * Record character detection performance metrics
   * @param {Object} data - Character detection data
   */
  recordCharacterDetection(data) {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    const {
      charactersDetected = 0,
      detectionTime = 0,
      cacheHits = 0,
      cacheMisses = 0
    } = data;
    
    this.performanceMetrics.charactersDetected += charactersDetected;
    this.performanceMetrics.characterDetectionTime += detectionTime;
    this.performanceMetrics.characterCacheHits += cacheHits;
    this.performanceMetrics.characterCacheMisses += cacheMisses;
    
    // Calculate cache hit rate
    const totalCacheAccess = this.performanceMetrics.characterCacheHits + this.performanceMetrics.characterCacheMisses;
    this.performanceMetrics.cacheHitRate = totalCacheAccess > 0 
      ? (this.performanceMetrics.characterCacheHits / totalCacheAccess) * 100 
      : 0;
    
    this.addToPerformanceHistory('cacheHitRates', this.performanceMetrics.cacheHitRate);
  }

  /**
   * Record smart targeting performance metrics
   * @param {Object} data - Smart targeting data
   */
  recordSmartTargeting(data) {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    const {
      elementReductionPercentage = 0,
      smartTargetingTime = 0,
      dynamicElementsAdded = 0
    } = data;
    
    this.performanceMetrics.elementReductionPercentage = elementReductionPercentage;
    this.performanceMetrics.smartTargetingTime += smartTargetingTime;
    this.performanceMetrics.dynamicElementsAdded += dynamicElementsAdded;
  }

  /**
   * Record performance comparison data
   * @param {Object} data - Comparison data
   */
  recordPerformanceComparison(data) {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    const {
      estimatedTraditionalTime = 0,
      actualOptimizedTime = 0
    } = data;
    
    this.performanceMetrics.estimatedTraditionalTime += estimatedTraditionalTime;
    this.performanceMetrics.actualOptimizedTime += actualOptimizedTime;
    
    // Calculate performance gain
    if (this.performanceMetrics.estimatedTraditionalTime > 0) {
      this.performanceMetrics.performanceGainPercentage = 
        ((this.performanceMetrics.estimatedTraditionalTime - this.performanceMetrics.actualOptimizedTime) / 
         this.performanceMetrics.estimatedTraditionalTime) * 100;
      
      this.performanceMetrics.speedupFactor = 
        this.performanceMetrics.estimatedTraditionalTime / Math.max(this.performanceMetrics.actualOptimizedTime, 1);
    }
    
    this.addToPerformanceHistory('performanceGains', this.performanceMetrics.performanceGainPercentage);
  }

  /**
   * Update processing time statistics
   * @param {number} processingTime - Current processing time
   */
  updateProcessingTimeStats(processingTime) {
    if (processingTime > this.performanceMetrics.maxProcessingTime) {
      this.performanceMetrics.maxProcessingTime = processingTime;
    }
    
    if (processingTime < this.performanceMetrics.minProcessingTime) {
      this.performanceMetrics.minProcessingTime = processingTime;
    }
    
    // Calculate rolling average
    const totalOps = this.performanceState.totalOperations + 1;
    this.performanceMetrics.averageProcessingTime = 
      (this.performanceMetrics.averageProcessingTime * (totalOps - 1) + processingTime) / totalOps;
  }

  /**
   * Add data to performance history with buffer management
   * @param {string} metric - Metric name
   * @param {*} value - Value to add
   */
  addToPerformanceHistory(metric, value) {
    if (!this.performanceHistory[metric]) {
      this.performanceHistory[metric] = [];
    }
    
    this.performanceHistory[metric].push(value);
    
    // Keep buffer size within limits
    if (this.performanceHistory[metric].length > this.performanceState.metricsBufferSize) {
      this.performanceHistory[metric].shift();
    }
  }

  /**
   * Check for performance alerts
   * @param {number} processingTime - Current processing time
   */

  /**
   * Get current performance metrics
   * @returns {Object} Current metrics
   */
  getMetrics() {
    if (!this.performanceMonitoringEnabled) {
      return null;
    }
    
    return {
      ...this.performanceMetrics,
      alerts: { ...this.performanceAlerts },
      sessionDuration: Date.now() - this.performanceState.sessionStartTime,
      totalOperations: this.performanceState.totalOperations,
      lastUpdate: this.performanceState.lastUpdate
    };
  }

  /**
   * Get performance comparison statistics
   * @returns {Object} Comparison statistics
   */
  getComparisonStats() {
    if (!this.performanceMonitoringEnabled) {
      return null;
    }
    
    const optimizedSamples = this.performanceHistory.processingTimes;
    if (optimizedSamples.length === 0) {
      return null;
    }
    
    const optimizedAvg = optimizedSamples.reduce((sum, val) => sum + val, 0) / optimizedSamples.length;
    const traditionalAvg = this.performanceMetrics.estimatedTraditionalTime / Math.max(this.performanceState.totalOperations, 1);
    
    return {
      optimizedAverage: optimizedAvg,
      traditionalAverage: traditionalAvg,
      speedupFactor: traditionalAvg / Math.max(optimizedAvg, 1),
      performanceGain: traditionalAvg > 0 ? ((traditionalAvg - optimizedAvg) / traditionalAvg) * 100 : 0,
      sampleSize: optimizedSamples.length
    };
  }

  /**
   * Get trend analysis results
   * @returns {Object} Trend analysis
   */
  getTrendAnalysis() {
    if (!this.performanceMonitoringEnabled) {
      return null;
    }
    
    const processingTimes = this.performanceHistory.processingTimes;
    const cacheHitRates = this.performanceHistory.cacheHitRates;
    
    return {
      processingTimeTrend: this.calculateTrend(processingTimes),
      cacheEfficiencyTrend: this.calculateTrend(cacheHitRates),
      dataPoints: processingTimes.length,
      lastAnalysis: Date.now()
    };
  }

  /**
   * Calculate trend for a data series
   * @param {Array<number>} data - Data series
   * @returns {string} Trend direction
   */
  calculateTrend(data) {
    if (data.length < 3) {
      return 'insufficient_data';
    }
    
    const recent = data.slice(-Math.min(10, data.length));
    const firstHalf = recent.slice(0, Math.floor(recent.length / 2));
    const secondHalf = recent.slice(Math.floor(recent.length / 2));
    
    const firstAvg = firstHalf.reduce((sum, val) => sum + val, 0) / firstHalf.length;
    const secondAvg = secondHalf.reduce((sum, val) => sum + val, 0) / secondHalf.length;
    
    const threshold = firstAvg * 0.1; // 10% threshold
    
    if (secondAvg > firstAvg + threshold) {
      return 'increasing';
    } else if (secondAvg < firstAvg - threshold) {
      return 'decreasing';
    } else {
      return 'stable';
    }
  }

  /**
   * Reset performance metrics for new session
   */
  resetPerformanceMetrics() {
    if (!this.performanceMonitoringEnabled) {
      return;
    }
    
    this.performanceMetrics = {
      elementsObserved: 0,
      elementsProcessed: 0,
      elementsEntering: 0,
      elementsLeaving: 0,
      totalProcessingTime: 0,
      averageProcessingTime: 0,
      maxProcessingTime: 0,
      minProcessingTime: Infinity,
      charactersDetected: 0,
      characterDetectionTime: 0,
      characterCacheHits: 0,
      characterCacheMisses: 0,
      elementReductionPercentage: 0,
      smartTargetingTime: 0,
      dynamicElementsAdded: 0,
      cacheHitRate: 0,
      cacheEfficiency: 0,
      memoryUsage: 0,
      estimatedTraditionalTime: 0,
      actualOptimizedTime: 0,
      performanceGainPercentage: 0,
      speedupFactor: 1
    };
    
    this.performanceState.totalOperations = 0;
  }

  /**
   * Export complete performance data
   * @returns {Object} Complete performance data
   */
  exportData() {
    if (!this.performanceMonitoringEnabled) {
      return null;
    }
    
    return {
      metrics: this.getMetrics(),
      history: { ...this.performanceHistory },
      comparison: this.getComparisonStats(),
      trends: this.getTrendAnalysis(),
      configuration: {
        enabled: this.performanceMonitoringEnabled,
        metricsBufferSize: this.performanceState.metricsBufferSize,
        performanceThreshold: this.performanceState.performanceThreshold
      }
    };
  }
}


  // Module: src/ui/keybindings-ui-shared.js
/**
 * Shared pieces of the KeyPilot keybindings UI that need to stay consistent
 * across:
 * - bundled content UI (`src/ui/keybindings-ui.js`)
 * - early injection UI (`extension/early-inject.js`)
 *
 * NOTE: `early-inject.js` cannot import ESM at runtime (it must run at
 * `document_start` and is loaded directly by the manifest). Instead, the build
 * script stamps generated constants into `early-inject.js` from this module.
 */

const KEYBINDINGS_UI_STYLE_ATTR = 'data-kp-keybindings-ui-style';
const KEYBINDINGS_UI_ROOT_CLASS = 'kp-keybindings-ui';

// Used in generated CSS when runtime URLs are not available (e.g. build-time stamping into early-inject.js).
const KEYBINDINGS_UI_FONT_PLACEHOLDERS = {
  ROBOTECH: '__KP_FONT_ROBOTECH_URL__',
  TITILLIUM: '__KP_FONT_TITILLIUM_URL__',
  CUBELLAN: '__KP_FONT_CUBELLAN_URL__',
  EZARION: '__KP_FONT_EZARION_URL__',
  DOSIS: '__KP_FONT_DOSIS_URL__'
};

/**
 * Canonical keyboard layout used by both early-inject and the bundled UI.
 * Action keys reference IDs in `KEYBINDINGS`.
 */
const KEYBINDINGS_KEYBOARD_LAYOUT = getKeyboardUiLayoutForLayout(DEFAULT_KEYBOARD_LAYOUT_ID);

/**
 * Font Awesome Free solid-style SVG path data (viewBox 0 0 512 512).
 * Used as faded key background icons behind white foreground labels.
 * Paths are FA Free solid equivalents for offline/CSP-safe data-URI embedding.
 */
const FA_SOLID_PATHS = Object.freeze({
  // Navigation / history
  'arrow-left': 'M9.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 288 480 288c17.7 0 32-14.3 32-32s-14.3-32-32-32l-370.7 0 137.4-137.4c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-192 192z',
  'arrow-right': 'M502.6 278.6c12.5-12.5 12.5-32.8 0-45.3l-192-192c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L402.7 224 32 224c-17.7 0-32 14.3-32 32s14.3 32 32 32l370.7 0-137.4 137.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l192-192z',
  'arrow-up': 'M233.4 105.4c12.5-12.5 32.8-12.5 45.3 0l192 192c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L256 173.3 86.6 342.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3l192-192z',
  'arrow-down': 'M233.4 406.6c12.5 12.5 32.8 12.5 45.3 0l192-192c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L256 338.7 86.6 169.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l192 192z',
  'angles-up': 'M233.4 105.4c12.5-12.5 32.8-12.5 45.3 0l192 192c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L256 173.3 86.6 342.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3l192-192zm0 160c12.5-12.5 32.8-12.5 45.3 0l192 192c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L256 333.3 86.6 502.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3l192-192z',
  'angles-down': 'M233.4 406.6c12.5 12.5 32.8 12.5 45.3 0l192-192c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L256 338.7 86.6 169.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l192 192zm0-160c12.5 12.5 32.8 12.5 45.3 0l192-192c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L256 178.7 86.6 9.4C74.1-3.1 53.8-3.1 41.3 9.4s-12.5 32.8 0 45.3l192 192z',
  'chevron-left': 'M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z',
  'chevron-right': 'M470.6 278.6c12.5-12.5 12.5-32.8 0-45.3l-160-160c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L402.7 256 265.4 393.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l160-160z',

  // Clicks / tabs / windows (Font Awesome Free solid-style glyphs)
  'hand-pointer': 'M320 0c17.7 0 32 14.3 32 32V176h16c17.7 0 32 14.3 32 32s-14.3 32-32 32H352v16c0 17.7-14.3 32-32 32s-32-14.3-32-32V240H272v16c0 17.7-14.3 32-32 32s-32-14.3-32-32V240H192v80c0 53 43 96 96 96h32c53 0 96-43 96-96V224h32c17.7 0 32-14.3 32-32s-14.3-32-32-32H416V32c0-17.7-14.3-32-32-32H320zM192 96c0-17.7-14.3-32-32-32H128C57.3 64 0 121.3 0 192v96c0 53 43 96 96 96h32c17.7 0 32-14.3 32-32s-14.3-32-32-32H96c-17.7 0-32-14.3-32-32V192c0-35.3 28.7-64 64-64h32c17.7 0 32-14.3 32-32z',
  'arrow-up-right-from-square': 'M320 0c-17.7 0-32 14.3-32 32s14.3 32 32 32h82.7L201.4 265.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L448 109.3V192c0 17.7 14.3 32 32 32s32-14.3 32-32V32c0-17.7-14.3-32-32-32H320zM80 32C35.8 32 0 67.8 0 112V432c0 44.2 35.8 80 80 80H400c44.2 0 80-35.8 80-80V320c0-17.7-14.3-32-32-32s-32 14.3-32 32V432c0 8.8-7.2 16-16 16H80c-8.8 0-16-7.2-16-16V112c0-8.8 7.2-16 16-16H192c17.7 0 32-14.3 32-32s-14.3-32-32-32H80z',
  'clone': 'M64 464H288c8.8 0 16-7.2 16-16V384h48v64c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V224c0-35.3 28.7-64 64-64h64v48H64c-8.8 0-16 7.2-16 16V448c0 8.8 7.2 16 16 16zM224 0c-35.3 0-64 28.7-64 64V288c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V64c0-35.3-28.7-64-64-64H224zm0 48H448c8.8 0 16 7.2 16 16V288c0 8.8-7.2 16-16 16H224c-8.8 0-16-7.2-16-16V64c0-8.8 7.2-16 16-16z',
  'window-maximize': 'M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm32 96H416c17.7 0 32 14.3 32 32v32H64V160c0-17.7 14.3-32 32-32z',
  'eye': 'M256 96c-89.6 0-168.5 48.8-212.7 122.3c-7.3 12.1-7.3 27.3 0 39.4C87.5 331.2 166.4 380 256 380s168.5-48.8 212.7-122.3c7.3-12.1 7.3-27.3 0-39.4C424.5 144.8 345.6 96 256 96zm0 224a96 96 0 1 1 0-192 96 96 0 1 1 0 192zm0-144a48 48 0 1 0 0 96 48 48 0 1 0 0-96z',
  'plus': 'M256 80c0-17.7-14.3-32-32-32s-32 14.3-32 32V224H48c-17.7 0-32 14.3-32 32s14.3 32 32 32H192V432c0 17.7 14.3 32 32 32s32-14.3 32-32V288H400c17.7 0 32-14.3 32-32s-14.3-32-32-32H256V80z',
  'folder-plus': 'M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V160c0-35.3-28.7-64-64-64H298.5c-17 0-33.3-6.7-45.3-18.7L226.7 50.7C214.7 38.7 198.5 32 181.5 32H64zM232 248v-48c0-13.3 10.7-24 24-24s24 10.7 24 24v48h48c13.3 0 24 10.7 24 24s-10.7 24-24 24H280v48c0 13.3-10.7 24-24 24s-24-10.7-24-24V296H184c-13.3 0-24-10.7-24-24s10.7-24 24-24h48z',
  'xmark': 'M256 48a208 208 0 1 1 0 416 208 208 0 1 1 0-416zm0 464A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM175 175c9.4-9.4 24.6-9.4 33.9 0l47 47 47-47c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9l-47 47 47 47c9.4 9.4 9.4 24.6 0 33.9s-24.6 9.4-33.9 0l-47-47-47 47c-9.4 9.4-24.6 9.4-33.9 0s-9.4-24.6 0-33.9l47-47-47-47c-9.4-9.4-9.4-24.6 0-33.9z',
  'trash': 'M135.2 17.7L128 32H32C14.3 32 0 46.3 0 64S14.3 96 32 96H480c17.7 0 32-14.3 32-32s-14.3-32-32-32H384l-7.2-14.3C372.4 6.8 361.3 0 349.2 0H162.8c-12.1 0-23.2 6.8-28.6 17.7zM32 128V448c0 35.3 28.7 64 64 64H416c35.3 0 64-28.7 64-64V128H32zm112 64c8.8 0 16 7.2 16 16V400c0 8.8-7.2 16-16 16s-16-7.2-16-16V208c0-8.8 7.2-16 16-16zm96 0c8.8 0 16 7.2 16 16V400c0 8.8-7.2 16-16 16s-16-7.2-16-16V208c0-8.8 7.2-16 16-16zm96 0c8.8 0 16 7.2 16 16V400c0 8.8-7.2 16-16 16s-16-7.2-16-16V208c0-8.8 7.2-16 16-16z',
  'delete-left': 'M576 128c0-35.3-28.7-64-64-64H205.3c-17 0-33.3 6.7-45.3 18.7L9.4 233.4c-6 6-9.4 14.1-9.4 22.6s3.4 16.6 9.4 22.6L160 429.3c12 12 28.3 18.7 45.3 18.7H512c35.3 0 64-28.7 64-64V128zM271 175c9.4-9.4 24.6-9.4 33.9 0l47 47 47-47c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9l-47 47 47 47c9.4 9.4 9.4 24.6 0 33.9s-24.6 9.4-33.9 0l-47-47-47 47c-9.4 9.4-24.6 9.4-33.9 0s-9.4-24.6 0-33.9l47-47-47-47c-9.4-9.4-9.4-24.6 0-33.9z',

  // UI / utility
  'gear': 'M495.9 166.1c3.3 12.7 .9 26.3-7.1 36.1l-37.3 45.7c2.1 11.1 3.2 22.6 3.2 34.3s-1.1 23.2-3.2 34.3l37.3 45.7c8 9.8 10.4 23.4 7.1 36.1c-6.3 24.2-17.7 46.6-33.1 66.3c-8.1 10.3-21.2 14.9-33.9 12.1l-57.5-12.7c-17.9 15.3-38.4 27.3-60.7 35.4l-13.7 57.5c-2.9 12.1-12.9 21.1-25.4 22.4c-24.2 2.6-49.1 2.6-73.3 0c-12.5-1.3-22.5-10.3-25.4-22.4l-13.7-57.5c-22.3-8.1-42.8-20.1-60.7-35.4L71.6 436.6c-12.7 2.8-25.8-1.8-33.9-12.1C22.3 404.8 10.9 382.4 4.6 358.2c-3.3-12.7-.9-26.3 7.1-36.1l37.3-45.7C46.9 265.2 45.8 253.7 45.8 242s1.1-23.2 3.2-34.3L11.7 161.9c-8-9.8-10.4-23.4-7.1-36.1C10.9 101.6 22.3 79.2 37.7 59.5c8.1-10.3 21.2-14.9 33.9-12.1l57.5 12.7c17.9-15.3 38.4-27.3 60.7-35.4L203.5-32.8c2.9-12.1 12.9-21.1 25.4-22.4c24.2-2.6 49.1-2.6 73.3 0c12.5 1.3 22.5 10.3 25.4 22.4l13.7 57.5c22.3 8.1 42.8 20.1 60.7 35.4l57.5-12.7c12.7-2.8 25.8 1.8 33.9 12.1c15.4 19.7 26.8 42.1 33.1 66.3zM256 336a80 80 0 1 0 0-160 80 80 0 1 0 0 160z',
  'magnifying-glass': 'M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352a144 144 0 1 0 0-288 144 144 0 1 0 0 288z',
  'keyboard': 'M0 96C0 60.7 28.7 32 64 32H448c35.3 0 64 28.7 64 64V416c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V96zm128 64v32h32V160H128zm64 0v32h32V160H192zm64 0v32h32V160H256zm64 0v32h32V160H320zm64 0v32h32V160H384zM96 256v32h64V256H96zm96 0v32h32V256H192zm64 0v32h32V256H256zm64 0v32h32V256H320zm64 0v32h32V256H384zm64 0v32h32V256H448zM128 352v32H384V352H128z',
  'clock-rotate-left': 'M256 0C114.6 0 0 114.6 0 256S114.6 512 256 512c53 0 102-16.2 142.7-43.9c10.8-7.4 13.6-22.3 6.2-33.1s-22.3-13.6-33.1-6.2C340.8 449.1 299.6 464 256 464C141.1 464 48 370.9 48 256S141.1 48 256 48c60.7 0 115.5 26.1 153.4 67.7l-33.5 33.5c-9.4 9.4-2.7 25.5 10.5 25.5H456c13.3 0 24-10.7 24-24V56c0-13.2-16.1-19.9-25.5-10.5L418.7 81.3C368.5 31.4 315.1 0 256 0zM232 120c0-13.3-10.7-24-24-24s-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V120z',
  'rocket': 'M156.6 384.9L125.7 354c-8.5-8.5-11.5-20.8-7.7-32.2c3-8.9 7-20.5 11.8-33.8L24 288c-8.6 0-16.6-4.6-20.9-12.1s-4.2-16.7 .2-24.1l52.5-88.5c13-21.9 36.5-35.3 61.9-35.3h82.3c2.4-4 4.8-7.7 7.2-11.3C289.1-4.1 411.1-8.1 483.9 5.3c11.6 2.1 20.6 11.2 22.8 22.8c13.4 72.9 9.3 194.8-111.4 276.7c-3.5 2.4-7.3 4.8-11.3 7.2v82.3c0 25.4-13.4 49-35.3 61.9l-88.5 52.5c-7.4 4.4-16.6 4.5-24.1 .2s-12.1-12.2-12.1-20.9V384.9c-13.3 4.8-24.9 8.8-33.8 11.8c-11.4 3.7-23.7 .7-32.2-7.8zM215.3 237.3c28.3-28.3 73.1-31.3 105.4-8.5L200.5 348.5c-22.8-32.3-19.8-77.1 8.5-105.4l6.3-5.8z',
  'house': 'M575.8 255.5c0 18-15 32.1-32 32.1h-32l.7 160.2c.2 35.5-28.5 64.3-64 64.3H392c-22.1 0-40-17.9-40-40V448 384c0-17.7-14.3-32-32-32H256c-17.7 0-32 14.3-32 32v64 24c0 22.1-17.9 40-40 40H128.1c-35.3 0-64-28.7-64-64V287.6H32c-18 0-32-14-32-32.1c0-9 3-17 10-24L266.4 8c7-7 15-8 22-8s15 2 21 7L564.8 231.5c8 7 12 15 11 24z',
  'ban': 'M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM175 175c9.4-9.4 24.6-9.4 33.9 0L368 334.1c9.4 9.4 9.4 24.6 0 33.9s-24.6 9.4-33.9 0L175 208.9c-9.4-9.4-9.4-24.6 0-33.9z',

  // Special keys
  'arrow-right-to-bracket': 'M512 256c0 17.7-14.3 32-32 32H178.7l73.4 73.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0l-128-128c-12.5-12.5-12.5-32.8 0-45.3l128-128c12.5-12.5 32.8-12.5 45.3 0s12.5 32.8 0 45.3L178.7 224H480c17.7 0 32 14.3 32 32zM0 128C0 92.7 28.7 64 64 64H192c17.7 0 32 14.3 32 32s-14.3 32-32 32H64V384H192c17.7 0 32 14.3 32 32s-14.3 32-32 32H64c-35.3 0-64-28.7-64-64V128z',
  'turn-down': 'M54.6 310.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0L128 293.3V64c0-17.7 14.3-32 32-32H352c17.7 0 32 14.3 32 32s-14.3 32-32 32H192V293.3l28.1-28.1c12.5-12.5 32.8-12.5 45.3 0s12.5 32.8 0 45.3l-80 80c-12.5 12.5-32.8 12.5-45.3 0l-80-80z',
  'up-long': 'M278.6 9.4c-12.5-12.5-32.8-12.5-45.3 0l-128 128c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L224 109.3V480c0 17.7 14.3 32 32 32s32-14.3 32-32V109.3l73.4 73.4c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3l-128-128z',
  'arrow-up-from-line': 'M233.4 105.4c12.5-12.5 32.8-12.5 45.3 0l96 96c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L288 205.3V384c0 17.7-14.3 32-32 32s-32-14.3-32-32V205.3l-41.4 41.4c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3l96-96zM64 448c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H96c-17.7 0-32-14.3-32-32z',
  'arrow-down-to-line': 'M233.4 406.6c12.5 12.5 32.8 12.5 45.3 0l96-96c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 306.7V128c0-17.7-14.3-32-32-32s-32 14.3-32 32V306.7l-41.4-41.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l96 96zM64 64c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H96C78.3 96 64 81.7 64 64z',
  'circle': 'M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512z',

  // Text / rectangle selection
  // FA Free solid "font" (A glyph) — text select
  'font': 'M256 32c14.2 0 27.3 7.5 34.5 19.8l216 368c6.1 10.4 6.1 23.3 0 33.7s-17.4 16.5-29.9 16.5H35.4c-12.5 0-23.8-6.6-29.9-16.5s-6.1-23.3 0-33.7l216-368C228.7 39.5 241.8 32 256 32zm0 88.4L96.7 392h318.6L256 120.4z',
  // FA Free solid "i-cursor" — caret / character select
  'i-cursor': 'M128 64c0-17.7 14.3-32 32-32H352c17.7 0 32 14.3 32 32s-14.3 32-32 32H288v128h64c17.7 0 32 14.3 32 32s-14.3 32-32 32H288v128h64c17.7 0 32 14.3 32 32s-14.3 32-32 32H160c-17.7 0-32-14.3-32-32s14.3-32 32-32h64V288H160c-17.7 0-32-14.3-32-32s14.3-32 32-32h64V96H160c-17.7 0-32-14.3-32-32z',
  // FA Free solid "vector-square" — rectangle marquee corners
  'vector-square': 'M32 32C14.3 32 0 46.3 0 64v64c0 17.7 14.3 32 32 32s32-14.3 32-32V96h64c17.7 0 32-14.3 32-32s-14.3-32-32-32H32zM32 320c-17.7 0-32 14.3-32 32v64c0 17.7 14.3 32 32 32h64c17.7 0 32-14.3 32-32s-14.3-32-32-32H64V352c0-17.7-14.3-32-32-32zM320 64c0 17.7 14.3 32 32 32h64v64c0 17.7 14.3 32 32 32s32-14.3 32-32V64c0-17.7-14.3-32-32-32H352c-17.7 0-32 14.3-32 32zM480 320c-17.7 0-32 14.3-32 32v64H384c-17.7 0-32 14.3-32 32s14.3 32 32 32h64c17.7 0 32-14.3 32-32V352c0-17.7-14.3-32-32-32z',

  // FA Free solid "image" — copy hovered image
  'image': 'M0 96C0 60.7 28.7 32 64 32H448c35.3 0 64 28.7 64 64V416c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V96zM323.8 202.5c-4.5-6.6-11.9-10.5-19.8-10.5s-15.4 3.9-19.8 10.5l-87 127.6L170.7 297c-4.6-5.7-11.5-9-18.7-9s-14.2 3.3-18.7 9l-64 80c-5.8 7.2-6.9 17.1-2.9 25.4s12.4 13.6 21.6 13.6h96 32H424c8.9 0 17.1-4.9 21.2-12.8s3.6-17.4-1.4-24.7l-120-176zM112 192a48 48 0 1 0 0-96 48 48 0 1 0 0 96z',

  // FA Free solid "table-columns" — Cols Toggle multicol layout
  'table-columns': 'M0 96C0 60.7 28.7 32 64 32H448c35.3 0 64 28.7 64 64V416c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V96zm64 64V416H224V160H64zm320 0H288V416H448V160z'
});

/**
 * Map KeyPilot action IDs → FA icon keys (for CSS data-kp-icon / attribute selectors).
 * @type {Readonly<Record<string, string>>}
 */
const KEYBOARD_ACTION_ICON_IDS = Object.freeze({
  ACTIVATE: 'hand-pointer',
  ACTIVATE_NEW_TAB: 'arrow-up-right-from-square',
  ACTIVATE_NEW_TAB_BACKGROUND: 'clone',
  BACK: 'arrow-left',
  BACK2: 'arrow-left',
  FORWARD: 'arrow-right',
  DELETE: 'trash',
  COLS_TOGGLE: 'table-columns',
  TAB_LEFT: 'chevron-left',
  TAB_RIGHT: 'chevron-right',
  ROOT: 'house',
  LAUNCHER: 'rocket',
  CLOSE_TAB: 'xmark',
  CANCEL: 'ban',
  PAGE_UP_INSTANT: 'arrow-up',
  PAGE_DOWN_INSTANT: 'arrow-down',
  PAGE_TOP: 'arrow-up-from-line',
  PAGE_BOTTOM: 'arrow-down-to-line',
  NEW_TAB: 'folder-plus',
  OPEN_POPOVER: 'window-maximize',
  PREVIEW_LINK_POPOVER: 'eye',
  OPEN_SETTINGS_POPOVER: 'gear',
  OMNIBOX: 'magnifying-glass',
  TAB_HISTORY: 'clock-rotate-left',
  TOGGLE_KEYBOARD_HELP: 'keyboard',
  // Selection tools (recently re-enabled; were missing from the icon map)
  HIGHLIGHT: 'i-cursor',
  RECTANGLE_HIGHLIGHT: 'vector-square',
  COPY_HOVERED_IMAGE: 'image'
});

/**
 * @param {string} pathD
 * @param {string} [fill='black']
 * @returns {string} CSS url("data:image/svg+xml,...") for a solid glyph
 */
function faIconDataUri(pathD, fill = 'black') {
  const safeFill = String(fill || 'black').replace(/"/g, '');
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="${safeFill}"><path d="${pathD}"/></svg>`;
  return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`;
}

/**
 * Data-URI for a KeyPilot action's Font Awesome-style icon (for popovers / UI chrome).
 * @param {string} actionId
 * @param {{ fill?: string }} [opts]
 * @returns {string} empty string when no icon is mapped
 */
function getActionIconDataUri(actionId, opts = {}) {
  const iconName = KEYBOARD_ACTION_ICON_IDS[actionId];
  const pathD = iconName ? FA_SOLID_PATHS[iconName] : null;
  if (!pathD) return '';
  return faIconDataUri(pathD, opts.fill || 'white');
}

/**
 * Pro keycap material tokens for a color family.
 * Icons use a darker solid of the same family via --kp-key-icon.
 * @param {{ face: string, mid: string, deep: string, icon: string, glow?: string }} t
 * @returns {string}
 */
function keycapMaterial(t) {
  return `
  --kp-key-face: ${t.face};
  --kp-key-mid: ${t.mid};
  --kp-key-deep: ${t.deep};
  --kp-key-icon: ${t.icon};
  --kp-key-glow: ${t.glow || 'transparent'};
`;
}

/**
 * Build CSS rules that paint FA icons as darker monochrome key glyphs.
 * Uses mask-image + background-color so the icon inherits --kp-key-icon per key family.
 * @returns {string}
 */
function getKeyboardKeyIconCss() {
  const iconUris = {};
  for (const [name, pathD] of Object.entries(FA_SOLID_PATHS)) {
    iconUris[name] = faIconDataUri(pathD);
  }

  const lines = [];

  lines.push(`
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-bg-icon {
  position: absolute;
  inset: 7%;
  z-index: 0;
  pointer-events: none;
  /*
   * Transparent until an action rule applies both a mask and paint color.
   * mask-image:none + solid background-color otherwise paints a dark rectangle
   * (was visible on unmapped action keys like COPY_HOVERED_IMAGE before its icon).
   */
  background-color: transparent;
  background-image: none;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center center;
  mask-position: center center;
  -webkit-mask-size: 86% 86%;
  mask-size: 86% 86%;
  -webkit-mask-image: none;
  mask-image: none;
  opacity: 0.92;
}

/*
 * IMPORTANT: do NOT force position:relative on all non-icon children.
 * Action names (.key-main) and letter labels (.key-label) are absolutely
 * layered; a later relative rule would put letters back into flex flow.
 */

.${KEYBINDINGS_UI_ROOT_CLASS} .key:hover > .key-bg-icon {
  opacity: 1;
}

/* Keys without functions never paint an icon layer */
.${KEYBINDINGS_UI_ROOT_CLASS} .key:not([data-kp-action-id]) > .key-bg-icon {
  display: none;
}

/* Solid darken overlay for keydown feedback (more reliable than filter) */
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-press-overlay {
  position: absolute;
  inset: 0;
  z-index: 6;
  box-sizing: border-box;
  border-radius: inherit;
  background: rgba(0, 0, 0, 0.78);
  opacity: 0;
  pointer-events: none;
  transition: opacity 70ms ease;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.kp-key-pressed > .key-press-overlay,
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-press-overlay.is-on {
  opacity: 1;
}
`);

  // Action-specific icon masks only (keys with KeyPilot functions).
  // Color is applied here (not on the base rule) so unmapped actions stay transparent.
  for (const [actionId, iconName] of Object.entries(KEYBOARD_ACTION_ICON_IDS)) {
    const uri = iconUris[iconName];
    if (!uri) continue;
    lines.push(
      `.${KEYBINDINGS_UI_ROOT_CLASS} .key[data-kp-action-id="${actionId}"] > .key-bg-icon {` +
      ` -webkit-mask-image: ${uri}; mask-image: ${uri};` +
      ` background-color: var(--kp-key-icon, #0c1018); }`
    );
  }

  return lines.join('\n');
}

/**
 * Ensure a key element has the faded background-icon layer.
 * Safe to call repeatedly (idempotent).
 * @param {Document} doc
 * @param {HTMLElement} keyEl
 */
function ensureKeyBackgroundIcon(doc, keyEl) {
  if (!doc || !keyEl) return;
  try {
    if (keyEl.querySelector(':scope > .key-bg-icon')) return;
  } catch {
    if (keyEl.querySelector('.key-bg-icon')) return;
  }
  const icon = doc.createElement('span');
  icon.className = 'key-bg-icon';
  icon.setAttribute('aria-hidden', 'true');
  keyEl.insertBefore(icon, keyEl.firstChild);
}

/**
 * Ensure a key has a dedicated press-feedback overlay element.
 * Appended last so it paints above legends/icons.
 * Safe to call repeatedly (idempotent).
 * @param {Document} doc
 * @param {HTMLElement} keyEl
 * @returns {HTMLElement|null}
 */
function ensureKeyPressOverlay(doc, keyEl) {
  if (!doc || !keyEl) return null;
  let overlay = null;
  try {
    overlay = keyEl.querySelector(':scope > .key-press-overlay');
  } catch {
    overlay = keyEl.querySelector('.key-press-overlay');
  }
  if (overlay) return overlay;
  overlay = doc.createElement('span');
  overlay.className = 'key-press-overlay';
  overlay.setAttribute('aria-hidden', 'true');
  keyEl.appendChild(overlay);
  return overlay;
}

/**
 * Toggle overt press feedback via a dedicated dark overlay element.
 * Prefer this over filter/transform on the key itself.
 * @param {HTMLElement} keyEl
 * @param {boolean} pressed
 * @param {Document} [doc]
 */
function setKeyPressedState(keyEl, pressed, doc) {
  if (!keyEl) return;
  const d = doc || keyEl.ownerDocument || (typeof document !== 'undefined' ? document : null);
  if (pressed) {
    keyEl.classList.add('kp-key-pressed');
    const overlay = d ? ensureKeyPressOverlay(d, keyEl) : null;
    if (overlay) overlay.classList.add('is-on');
  } else {
    keyEl.classList.remove('kp-key-pressed');
    let overlay = null;
    try {
      overlay = keyEl.querySelector(':scope > .key-press-overlay');
    } catch {
      overlay = keyEl.querySelector('.key-press-overlay');
    }
    if (overlay) overlay.classList.remove('is-on');
  }
}

/**
 * Generate the injected CSS used by the keyboard UI (both early + bundled).
 *
 * @param {Object} params
 * @param {number} params.zKeybindingsPopover
 */
function getKeybindingsUiCss({ zKeybindingsPopover, fontUrls } = {}) {
  const z = Number.isFinite(zKeybindingsPopover) ? zKeybindingsPopover : Number(zKeybindingsPopover);
  // Default matches Z_INDEX.KEYBINDINGS_POPOVER in constants.js (stamped at build time when possible).
  const zIndex = Number.isFinite(z) ? z : 2147483046;

  // Font URLs are optional because `build.js` needs to stamp CSS into `early-inject.js`
  // without knowing the runtime extension ID. When omitted, we emit placeholders that
  // can be replaced at runtime (early-inject) or overwritten by the bundled UI.
  const urlRobotech = (fontUrls && fontUrls.robotech) || KEYBINDINGS_UI_FONT_PLACEHOLDERS.ROBOTECH;
  const urlTitillium = (fontUrls && fontUrls.titillium) || KEYBINDINGS_UI_FONT_PLACEHOLDERS.TITILLIUM;
  const urlCubellan = (fontUrls && fontUrls.cubellan) || KEYBINDINGS_UI_FONT_PLACEHOLDERS.CUBELLAN;
  const urlEzarion = (fontUrls && fontUrls.ezarion) || KEYBINDINGS_UI_FONT_PLACEHOLDERS.EZARION;
  const urlDosis = (fontUrls && fontUrls.dosis) || KEYBINDINGS_UI_FONT_PLACEHOLDERS.DOSIS;

  const keyIconCss = getKeyboardKeyIconCss();

  return `
/* KeyPilot Keybindings UI (injected) */
@font-face {
  font-family: "ROBOTECHGPRegular";
  src: url("${urlRobotech}") format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "TitilliumText";
  src: url("${urlTitillium}") format("opentype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Cubellan";
  src: url("${urlCubellan}") format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Ezarion";
  src: url("${urlEzarion}") format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Dosis";
  src: url("${urlDosis}") format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* Style isolation: all keyboard rules are scoped so host page CSS won't override them */
.${KEYBINDINGS_UI_ROOT_CLASS} {
  --kp-accent: #5be2f1;
}

/* ── Keyboard plate (pro app tray) ─────────────────────────────── */
.keyboard-visual.${KEYBINDINGS_UI_ROOT_CLASS} {
  --kp-kb-surface: #12151c;
  --kp-kb-well: #0a0c11;
  --kp-kb-rim: rgba(255, 255, 255, 0.08);
  box-sizing: border-box;
  width: 100%;
  padding: 5px;
  border-radius: 14px;
  border: 1px solid var(--kp-kb-rim);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.035) 0%, transparent 28%),
    radial-gradient(120% 80% at 50% 0%, rgba(91, 226, 241, 0.05) 0%, transparent 55%),
    linear-gradient(180deg, #161a22 0%, var(--kp-kb-surface) 45%, var(--kp-kb-well) 100%);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.06) inset,
    0 -1px 0 rgba(0, 0, 0, 0.45) inset,
    0 12px 28px rgba(0, 0, 0, 0.45),
    0 2px 0 rgba(0, 0, 0, 0.35);
  font-family: "Dosis", ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 10px;
  line-height: 1.1;
  user-select: none;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .keyboard-row {
  display: flex;
  justify-content: center;
  align-items: stretch;
  margin-bottom: 7px;
  gap: 5px;
  width: 100%;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .keyboard-row:last-child {
  margin-bottom: 0;
}

/* ── Base keycap: low-profile chiclet (flat + lightly realistic) ─ */
.${KEYBINDINGS_UI_ROOT_CLASS} .key {
  --kp-key-face: #3d4454;
  --kp-key-mid: #343a48;
  --kp-key-deep: #2c313e;
  --kp-key-icon: #1a1e28;
  --kp-key-glow: transparent;

  position: relative;
  box-sizing: border-box;
  margin: 0;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  cursor: default;
  appearance: none;
  -webkit-appearance: none;
  color: rgba(248, 250, 252, 0.94);
  text-align: center;
  overflow: hidden;

  /* Equal geometry for alphanumeric keys */
  flex: 1 1 0;
  min-width: 0;
  width: 0;
  height: 50px;
  min-height: 50px;
  max-height: 50px;
  /* Block layout: letter/name layers are absolutely positioned (not flex-flow) */
  display: block;
  padding: 0;
  border-radius: 7px;

  /*
   * Low-profile key: nearly flat face, thin rim, soft ground shadow.
   * Reads more like a real chiclet key than a heavy 3D bevel.
   */
  border: 1px solid rgba(0, 0, 0, 0.4);
  border-top-color: rgba(255, 255, 255, 0.1);
  border-bottom-color: rgba(0, 0, 0, 0.5);

  background:
    linear-gradient(180deg,
      rgba(255, 255, 255, 0.07) 0%,
      rgba(255, 255, 255, 0.02) 18%,
      transparent 42%),
    linear-gradient(180deg,
      var(--kp-key-face) 0%,
      var(--kp-key-mid) 70%,
      var(--kp-key-deep) 100%);

  box-shadow:
    0 1px 0 rgba(0, 0, 0, 0.45),
    0 2px 4px rgba(0, 0, 0, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    inset 0 -1px 0 rgba(0, 0, 0, 0.18);

  transition:
    transform 100ms ease,
    box-shadow 100ms ease,
    filter 100ms ease,
    border-color 100ms ease,
    background 100ms ease;
}

/* Minimal face sheen (not a tall sculpted plate) */
.${KEYBINDINGS_UI_ROOT_CLASS} .key::before {
  content: '';
  position: absolute;
  z-index: 0;
  top: 1px;
  left: 2px;
  right: 2px;
  height: 38%;
  border-radius: 5px 5px 40% 40%;
  background: linear-gradient(180deg,
    rgba(255, 255, 255, 0.08) 0%,
    rgba(255, 255, 255, 0.02) 60%,
    transparent 100%);
  pointer-events: none;
}

.${KEYBINDINGS_UI_ROOT_CLASS} [data-kp-action-id] {
  cursor: pointer;
}

.${KEYBINDINGS_UI_ROOT_CLASS} [data-kp-action-id]:hover {
  filter: brightness(1.07);
  border-top-color: rgba(255, 255, 255, 0.14);
  box-shadow:
    0 1px 0 rgba(0, 0, 0, 0.4),
    0 3px 8px rgba(0, 0, 0, 0.28),
    0 0 0 1px rgba(255, 255, 255, 0.04),
    0 0 10px var(--kp-key-glow),
    inset 0 1px 0 rgba(255, 255, 255, 0.12),
    inset 0 -1px 0 rgba(0, 0, 0, 0.16);
}

/*
 * Layered key legend (independent of each other):
 * - .key-main  = action name — upper band only
 * - .key-label = physical key letter — always pinned bottom-center
 * - .key-text  = special / unassigned glyph
 */
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-main {
  position: absolute;
  z-index: 1;
  /* Pin action name to the top of the key face */
  top: 3px;
  left: 2px;
  right: 2px;
  bottom: auto;
  /* Leave a fixed bottom band for the letter; never push it */
  height: auto;
  max-height: 30px;
  box-sizing: border-box;
  margin: 0;
  padding: 0 1px;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.02em;
  line-height: 1.1;
  opacity: 0.9;
  text-transform: uppercase;
  color: rgba(248, 250, 252, 0.94);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  word-break: break-word;
  hyphens: auto;
  text-align: center;
  pointer-events: none;
}

/* Letter / chrome labels: fixed bottom layer on every key */
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-label,
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-text {
  position: absolute;
  z-index: 2;
  left: 0;
  right: 0;
  bottom: 3px;
  top: auto;
  transform: none;
  box-sizing: border-box;
  margin: 0;
  padding: 0 2px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1;
  text-align: center;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
  pointer-events: none;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.03em;
  color: var(--kp-accent, #5be2f1);
}

/* Special chrome labels (Tab/Caps/…) */
.${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-text {
  font-size: 10px;
  font-weight: 650;
  letter-spacing: 0.02em;
  color: rgba(248, 250, 252, 0.92);
}

/* Unassigned letter-only keys: still bottom-centered (same letter layer) */
.${KEYBINDINGS_UI_ROOT_CLASS} .key:not([data-kp-action-id]):not(.key-tab):not(.key-caps):not(.key-enter):not(.key-shift):not(.key-backspace) > .key-text {
  bottom: 3px;
  top: auto;
  transform: none;
  font-size: 12px;
  font-weight: 700;
  color: var(--kp-accent, #5be2f1);
}

/* Special keys: wider, same height */
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-tab,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-caps,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-enter,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-shift,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-backspace {
  height: 50px;
  min-height: 50px;
  max-height: 50px;
  width: auto;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-tab { flex: 1.25 1 0; }
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-caps { flex: 1.35 1 0; }
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-enter { flex: 1.55 1 0; }
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-shift { flex: 1.65 1 0; }
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-backspace { flex: 1.55 1 0; }

/* ── Color families (muted pro tints + darker icon color) ─────── */
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-activate,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-activate-new,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-activate-new-over,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-tab-right,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-new-tab,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-open-popover,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-preview-popover,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-page-up,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-page-down,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-page-up-instant,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-page-down-instant,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-help,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-omnibox {
  ${keycapMaterial({
    face: '#2f8f5b',
    mid: '#247a4c',
    deep: '#17633a',
    icon: '#0d3a22',
    glow: 'rgba(34, 197, 94, 0.18)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-back,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-forward,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-scroll-top,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-scroll-bottom {
  ${keycapMaterial({
    face: '#2f7ea8',
    mid: '#256b92',
    deep: '#1a5475',
    icon: '#0d3044',
    glow: 'rgba(56, 189, 248, 0.16)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-delete,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-close-tab {
  ${keycapMaterial({
    face: '#b84a4a',
    mid: '#9e3b3b',
    deep: '#7a2b2b',
    icon: '#401616',
    glow: 'rgba(248, 113, 113, 0.16)'
  })}
}

/* Selection tools: indigo family (distinct from green activate / blue nav / amber unused) */
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-highlight,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-rect-highlight {
  ${keycapMaterial({
    face: '#5b6fd4',
    mid: '#4a5cbb',
    deep: '#3949a0',
    icon: '#1a2258',
    glow: 'rgba(99, 102, 241, 0.18)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-scroll {
  ${keycapMaterial({
    face: '#7a5638',
    mid: '#63452c',
    deep: '#4a3320',
    icon: '#26180f',
    glow: 'rgba(180, 120, 70, 0.12)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-purple,
.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-cols {
  ${keycapMaterial({
    face: '#7a4ab8',
    mid: '#663d9e',
    deep: '#4e2e7a',
    icon: '#281646',
    glow: 'rgba(167, 139, 250, 0.14)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-orange {
  ${keycapMaterial({
    face: '#c97a28',
    mid: '#a8641e',
    deep: '#834f16',
    icon: '#3f250a',
    glow: 'rgba(255, 165, 0, 0.14)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-launcher-orange {
  ${keycapMaterial({
    face: '#a06a3a',
    mid: '#85562e',
    deep: '#664122',
    icon: '#322010',
    glow: 'rgba(184, 115, 51, 0.12)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-settings-dark {
  ${keycapMaterial({
    face: '#3a4250',
    mid: '#2a313c',
    deep: '#1a1f28',
    icon: '#0c0f14',
    glow: 'rgba(148, 163, 184, 0.1)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-gray {
  ${keycapMaterial({
    face: '#5a6270',
    mid: '#484f5c',
    deep: '#343a45',
    icon: '#1a1e26',
    glow: 'rgba(148, 163, 184, 0.1)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-launcher {
  ${keycapMaterial({
    face: '#2a8fa3',
    mid: '#22788a',
    deep: '#185e6d',
    icon: '#0c343c',
    glow: 'rgba(34, 211, 238, 0.14)'
  })}
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-hatched {
  position: relative;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-hatched::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    repeating-linear-gradient(
      45deg,
      rgba(200, 200, 200, 0.4) 0px,
      rgba(200, 200, 200, 0.4) 1px,
      transparent 1px,
      transparent 4px
    );
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-radial-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at center,
    rgba(150, 150, 150, 0.3) 0%,
    rgba(120, 120, 120, 0.25) 30%,
    transparent 70%);
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-checkerboard-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    radial-gradient(circle at 25% 25%, rgba(160, 160, 160, 0.3) 2px, transparent 2px),
    radial-gradient(circle at 75% 75%, rgba(160, 160, 160, 0.3) 2px, transparent 2px);
  background-size: 8px 8px;
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-stripes-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(90deg, transparent 0%, transparent 40%, rgba(180, 180, 180, 0.25) 40%, rgba(180, 180, 180, 0.25) 60%, transparent 60%);
  background-size: 6px 100%;
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-crosshatch-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    repeating-linear-gradient(45deg, rgba(170, 170, 170, 0.25) 0px, rgba(170, 170, 170, 0.25) 1px, transparent 1px, transparent 4px),
    repeating-linear-gradient(-45deg, rgba(170, 170, 170, 0.25) 0px, rgba(170, 170, 170, 0.25) 1px, transparent 1px, transparent 4px);
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-noise-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    linear-gradient(45deg, rgba(80, 80, 80, 0.2) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(80, 80, 80, 0.2) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(80, 80, 80, 0.2) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(80, 80, 80, 0.2) 75%);
  background-size: 4px 4px;
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-conic-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: conic-gradient(
    from 45deg,
    rgba(100, 100, 100, 0.2) 0deg,
    rgba(120, 120, 120, 0.25) 90deg,
    rgba(140, 140, 140, 0.2) 180deg,
    rgba(100, 100, 100, 0.2) 360deg
  );
  pointer-events: none;
  border-radius: 4px;
}

.${KEYBINDINGS_UI_ROOT_CLASS} .key.key-dashed-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: inset 0 0 0 2px rgba(140, 140, 140, 0.4);
  pointer-events: none;
  border-radius: 4px;
}

/*
 * Keydown/keyup press feedback is a dedicated .key-press-overlay element
 * (see ensureKeyPressOverlay / setKeyPressedState). Avoid filter/transform
 * on the key itself — those were hard to see on colored keycaps.
 */
.${KEYBINDINGS_UI_ROOT_CLASS} .key.kp-key-pressed {
  outline: none;
}

/*
 * Text mode: all keys grayed out by default. Click Element (ACTIVATE / F)
 * lights up only while the hover-click countdown is armed on a clickable.
 */
.${KEYBINDINGS_UI_ROOT_CLASS}.kp-text-mode-filter .key.kp-key-text-mode-disabled {
  opacity: 0.34;
  filter: grayscale(0.85) brightness(0.78);
  pointer-events: none;
  cursor: default;
}
.${KEYBINDINGS_UI_ROOT_CLASS}.kp-text-mode-filter .key.kp-key-text-mode-active {
  opacity: 1;
  filter: none;
  z-index: 2;
  outline: none;
  pointer-events: auto;
  cursor: pointer;
  border-color: rgba(255, 140, 0, 0.75) !important;
  box-shadow:
    0 0 0 2px rgba(255, 140, 0, 0.35),
    0 0 12px 2px rgba(255, 140, 0, 0.28),
    inset 0 0 0 1px rgba(255, 255, 255, 0.14) !important;
}

/*
 * Link-hover hint: when the page pointer is over a link and the keyboard
 * reference is open, highlight the keys that activate / open that link.
 */
.${KEYBINDINGS_UI_ROOT_CLASS} .key.kp-key-link-hint {
  z-index: 2;
  outline: none;
  filter: brightness(1.12) saturate(1.15);
  border-color: rgba(91, 226, 241, 0.85) !important;
  border-top-color: rgba(180, 245, 255, 0.95) !important;
  border-bottom-color: rgba(40, 180, 200, 0.9) !important;
  box-shadow:
    0 0 0 2px rgba(91, 226, 241, 0.55),
    0 0 14px 3px rgba(91, 226, 241, 0.55),
    0 0 28px 6px rgba(56, 189, 248, 0.28),
    0 1px 0 rgba(0, 0, 0, 0.35),
    inset 0 0 0 1px rgba(255, 255, 255, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.18) !important;
  animation: kp-key-link-hint-pulse 1.35s ease-in-out infinite;
}

@keyframes kp-key-link-hint-pulse {
  0%, 100% {
    box-shadow:
      0 0 0 2px rgba(91, 226, 241, 0.5),
      0 0 12px 2px rgba(91, 226, 241, 0.45),
      0 0 22px 4px rgba(56, 189, 248, 0.22),
      0 1px 0 rgba(0, 0, 0, 0.35),
      inset 0 0 0 1px rgba(255, 255, 255, 0.18),
      inset 0 1px 0 rgba(255, 255, 255, 0.14);
  }
  50% {
    box-shadow:
      0 0 0 3px rgba(120, 240, 255, 0.75),
      0 0 18px 5px rgba(91, 226, 241, 0.7),
      0 0 34px 10px rgba(56, 189, 248, 0.38),
      0 1px 0 rgba(0, 0, 0, 0.35),
      inset 0 0 0 1px rgba(255, 255, 255, 0.28),
      inset 0 1px 0 rgba(255, 255, 255, 0.2);
  }
}

/* Popover (tooltip) — matches the hovered key material via CSS vars.
 * Uses the HTML Popover API (top layer) so it can escape the keyboard panel's
 * overflow:hidden and sit above/below keys outside the panel bounds.
 *
 * Do NOT set inset with !important: that locks left/top longhands and beats
 * JS style.left/top, pinning every tooltip at the viewport origin. Override UA
 * popover defaults with non-important longhands + margin:0 instead. */
.kp-keybindings-popover {
  --kp-key-face: #3d4454;
  --kp-key-mid: #343a48;
  --kp-key-deep: #2c313e;
  --kp-key-icon: #1a1e28;

  position: fixed !important;
  /* Kill UA popover centering (inset 0 / margin auto) without locking longhands. */
  margin: 0 !important;
  top: auto;
  right: auto;
  bottom: auto;
  left: auto;
  width: max-content;
  height: fit-content;
  overflow: visible;
  box-sizing: border-box;

  z-index: ${zIndex};
  max-width: min(300px, calc(100vw - 20px));
  min-width: 160px;
  color: rgba(248, 250, 252, 0.95);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.35;
  pointer-events: none; /* hover tooltips shouldn't steal pointer */

  /* Same low-profile key face treatment as .key */
  border: 1px solid rgba(0, 0, 0, 0.4);
  border-top-color: rgba(255, 255, 255, 0.12);
  border-bottom-color: rgba(0, 0, 0, 0.5);
  background:
    linear-gradient(180deg,
      rgba(255, 255, 255, 0.08) 0%,
      rgba(255, 255, 255, 0.02) 18%,
      transparent 42%),
    linear-gradient(180deg,
      var(--kp-key-face) 0%,
      var(--kp-key-mid) 70%,
      var(--kp-key-deep) 100%);
  box-shadow:
    0 1px 0 rgba(0, 0, 0, 0.45),
    0 10px 24px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    inset 0 -1px 0 rgba(0, 0, 0, 0.18);
}

/* Closed: both attribute + Popover API states */
.kp-keybindings-popover:not(:popover-open):not([data-kp-popover-open="true"]),
.kp-keybindings-popover[hidden] {
  display: none !important;
}

/* Open via Popover API or legacy fallback flag */
.kp-keybindings-popover:popover-open,
.kp-keybindings-popover[data-kp-popover-open="true"] {
  display: block;
  /* Re-assert after :popover-open (UA may reapply margin/inset). */
  margin: 0 !important;
  position: fixed !important;
}

.kp-keybindings-popover .kp-popover-head {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0 0 6px 0;
}

.kp-keybindings-popover .kp-popover-icon {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid rgba(0, 0, 0, 0.3);
  border-top-color: rgba(255, 255, 255, 0.1);
  /* Glyph uses same darker icon color as keys */
  background-color: var(--kp-key-icon);
  background-image: none;
  background-repeat: no-repeat;
  background-position: center;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center;
  mask-position: center;
  -webkit-mask-size: 62% 62%;
  mask-size: 62% 62%;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    inset 0 -1px 0 rgba(0, 0, 0, 0.2);
}

.kp-keybindings-popover .kp-popover-icon[hidden] {
  display: none;
}

.kp-keybindings-popover .kp-popover-title-wrap {
  min-width: 0;
  flex: 1 1 auto;
}

.kp-keybindings-popover .kp-popover-title {
  font-weight: 700;
  margin: 0 0 3px 0;
  color: rgba(248, 250, 252, 0.96);
  letter-spacing: 0.01em;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
}

.kp-keybindings-popover .kp-popover-keys {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  color: rgba(255, 255, 255, 0.72);
  margin: 0;
  font-size: 11px;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.kp-keybindings-popover .kp-popover-desc {
  margin: 0;
  color: rgba(248, 250, 252, 0.9);
  opacity: 0.95;
  font-size: 11.5px;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.kp-keybindings-popover::before {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  left: var(--kp-arrow-left, 18px);
  border: 9px solid transparent;
}

.kp-keybindings-popover[data-placement="top"]::before {
  top: 100%;
  border-top-color: rgba(0, 0, 0, 0.45);
}

.kp-keybindings-popover[data-placement="top"]::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  left: var(--kp-arrow-left, 18px);
  top: calc(100% - 1px);
  border: 8px solid transparent;
  border-top-color: var(--kp-key-deep);
}

.kp-keybindings-popover[data-placement="bottom"]::before {
  bottom: 100%;
  border-bottom-color: rgba(255, 255, 255, 0.12);
}

.kp-keybindings-popover[data-placement="bottom"]::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  left: var(--kp-arrow-left, 18px);
  bottom: calc(100% - 1px);
  border: 8px solid transparent;
  border-bottom-color: var(--kp-key-face);
}

/* Font Awesome-style faded key background icons (behind white labels) */
${keyIconCss}

/*
 * TEMP suspended: Floating Keyboard Reference flex-scale keys with panel resize.
 * Keys use the global fixed 50px keycap rules again. Re-enable together with
 * makePopoverResizable in floating-keyboard-help.js when resuming this work.
 *
 * .kp-floating-keyboard-help .kp-floating-keyboard-help__keyboard {
 *   box-sizing: border-box;
 *   flex: 1 1 auto;
 *   min-height: 0;
 *   width: 100%;
 *   height: 100%;
 *   display: flex;
 *   flex-direction: column;
 * }
 * .kp-floating-keyboard-help .keyboard-visual.${KEYBINDINGS_UI_ROOT_CLASS} {
 *   flex: 1 1 auto;
 *   min-height: 0;
 *   width: 100%;
 *   height: 100%;
 *   display: flex;
 *   flex-direction: column;
 *   gap: 7px;
 *   padding: clamp(4px, 1.2%, 10px);
 * }
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .keyboard-row {
 *   flex: 1 1 0;
 *   min-height: 0;
 *   margin-bottom: 0;
 *   gap: clamp(3px, 0.7%, 7px);
 *   align-items: stretch;
 * }
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key,
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key.key-tab,
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key.key-caps,
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key.key-enter,
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key.key-shift,
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key.key-backspace {
 *   height: 100%;
 *   min-height: 0;
 *   max-height: none;
 *   border-radius: clamp(5px, 12%, 10px);
 *   container-type: size;
 *   container-name: kp-key;
 * }
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-main {
 *   font-size: clamp(9px, 18cqh, 15px);
 *   max-height: 55%;
 *   top: clamp(2px, 8cqh, 8px);
 * }
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-label {
 *   font-size: clamp(10px, 22cqh, 18px);
 *   bottom: clamp(2px, 8cqh, 8px);
 * }
 * .kp-floating-keyboard-help .${KEYBINDINGS_UI_ROOT_CLASS} .key > .key-text {
 *   font-size: clamp(9px, 18cqh, 15px);
 *   bottom: clamp(2px, 8cqh, 8px);
 * }
 */
`;
}





  // Module: src/ui/keybindings-ui.js
/**
 * Reusable UI renderer for KeyPilot keybindings (keyboard visualization + legend table).
 *
 * Designed to be independent of any particular HTML file: you provide container elements.
 */


function getRuntimeFontUrls() {
  try {
    const getURL = (typeof chrome !== 'undefined' && chrome && chrome.runtime && chrome.runtime.getURL)
      ? chrome.runtime.getURL.bind(chrome.runtime)
      : null;
    if (!getURL) return null;
    return {
      robotech: getURL('fonts/ROBOTECHGPRegular.ttf'),
      titillium: getURL('fonts/TitilliumTextRegular.otf'),
      cubellan: getURL('fonts/CubellanRegular.ttf'),
      ezarion: getURL('fonts/EzarionRegular.ttf'),
      dosis: getURL('fonts/DosisBook.ttf')
    };
  } catch {
    return null;
  }
}

function getStyleCss() {
  return getKeybindingsUiCss({
    zKeybindingsPopover: Z_INDEX.KEYBINDINGS_POPOVER,
    fontUrls: getRuntimeFontUrls()
  });
}

function ensureStylesInjected(doc = document) {
  if (!doc || !doc.head) return;
  const css = getStyleCss();
  let style = null;
  try {
    const attr = typeof KEYBINDINGS_UI_STYLE_ATTR === 'string' && KEYBINDINGS_UI_STYLE_ATTR
      ? KEYBINDINGS_UI_STYLE_ATTR
      : 'data-kp-keybindings-ui-style';
    style = doc.head.querySelector(`style[${attr}]`);
  } catch {
    // If the selector is invalid for any reason, fall back to "first matching style tag" search.
    try {
      const all = doc.head.querySelectorAll('style');
      for (const s of all) {
        if (s && s.getAttribute && s.getAttribute('data-kp-keybindings-ui-style') === 'true') {
          style = s;
          break;
        }
      }
    } catch { /* ignore */ }
  }
  if (!style) {
    style = doc.createElement('style');
    try {
      style.setAttribute(
        (typeof KEYBINDINGS_UI_STYLE_ATTR === 'string' && KEYBINDINGS_UI_STYLE_ATTR)
          ? KEYBINDINGS_UI_STYLE_ATTR
          : 'data-kp-keybindings-ui-style',
        'true'
      );
    } catch {
      // ignore
    }
    style.textContent = css;
    doc.head.appendChild(style);
    return;
  }
  // Keep styles up-to-date (also replaces any build-time font URL placeholders).
  if (style.textContent !== css) {
    style.textContent = css;
  }
}

function clearElement(el) {
  while (el && el.firstChild) el.removeChild(el.firstChild);
}

function el(doc, tag, className, text) {
  const node = doc.createElement(tag);
  if (className) node.className = className;
  if (text !== undefined) node.textContent = text;
  return node;
}

function updateExistingKeyboardDOM({ container, keybindings }) {
  const doc = container.ownerDocument || document;

  // Strip leftover icon layers from keys that have no KeyPilot function.
  try {
    container.querySelectorAll('.key:not([data-kp-action-id]) > .key-bg-icon').forEach((el) => {
      try { el.remove(); } catch { /* ignore */ }
    });
  } catch { /* ignore */ }

  // Every key gets a press-overlay host (shown on keydown).
  try {
    container.querySelectorAll('.key').forEach((keyEl) => {
      ensureKeyPressOverlay(doc, keyEl);
    });
  } catch { /* ignore */ }

  const actionEls = container.querySelectorAll('[data-kp-action-id]');
  if (!actionEls || actionEls.length === 0) return false;

  for (const keyEl of actionEls) {
    const actionId = keyEl.dataset.kpActionId;
    const binding = keybindings && keybindings[actionId];
    const baseClass = keyEl.dataset.kpBaseClass || 'key';
    const keyboardClass = binding && binding.keyboardClass ? String(binding.keyboardClass) : '';
    keyEl.className = `${baseClass}${keyboardClass ? ' ' + keyboardClass : ''}`;
    // Only function-bearing keys get FA background icons.
    ensureKeyBackgroundIcon(doc, keyEl);
    ensureKeyPressOverlay(doc, keyEl);

    const title = (binding && (binding.description || binding.label)) || actionId;
    // Prefer aria-label over title so the browser native tooltip doesn't fight our hover popover.
    try { keyEl.removeAttribute('title'); } catch { /* ignore */ }
    keyEl.setAttribute('aria-label', title);

    const main = keyEl.querySelector('.key-main');
    if (main) main.textContent = (binding && binding.label) || actionId;

    const labelText = (binding && (binding.displayKey || binding.keyLabel)) || '';
    const existingLabel = keyEl.querySelector('.key-label');
    if (labelText) {
      if (existingLabel) existingLabel.textContent = labelText;
      else keyEl.appendChild(el(doc, 'div', 'key-label', labelText));
    } else if (existingLabel) {
      existingLabel.remove();
    }
  }

  return true;
}

/**
 * Render a keyboard visualization into a container.
 *
 * @param {Object} params
 * @param {HTMLElement} params.container
 * @param {Record<string, {label?: string, description?: string, displayKey?: string, keyboardClass?: string}>} params.keybindings
 * @param {any[]} [params.keyboardLayout]
 * @param {string} [params.layoutId]
 */
function renderKeybindingsKeyboard({ container, keybindings, keyboardLayout, layoutId } = {}) {
  if (!container) return;
  const doc = container.ownerDocument || document;
  ensureStylesInjected(doc);

  const layout = (keyboardLayout && Array.isArray(keyboardLayout)) ? keyboardLayout : KEYBINDINGS_KEYBOARD_LAYOUT;
  const layoutKey = typeof layoutId === 'string' ? layoutId : '';

  // If an early-inject (or previous render) already built the keyboard DOM,
  // just update the labels/classes to avoid flicker and layout jumps.
  let existingVisual = null;
  try {
    existingVisual = container.querySelector(':scope > .keyboard-visual');
  } catch {
    // Some environments (or odd documents) may not support :scope; fall back.
    try { existingVisual = container.querySelector('.keyboard-visual'); } catch { /* ignore */ }
  }
  if (existingVisual && existingVisual.dataset && existingVisual.dataset.kpKeyboardBuilt === 'true') {
    const existingLayoutKey = String(existingVisual.dataset.kpLayoutId || '');
    // If the caller provided a layoutId, only reuse DOM when it matches exactly.
    // This ensures switching layouts re-builds the keyboard positions (not just labels).
    const canReuse = !layoutKey ? true : (existingLayoutKey === layoutKey);
    if (canReuse) {
      if (updateExistingKeyboardDOM({ container, keybindings })) {
        attachKeyPopoverBehavior({ root: container, keybindings });
        return;
      }
    }
  }

  clearElement(container);

  const visual = el(doc, 'div', `keyboard-visual ${KEYBINDINGS_UI_ROOT_CLASS}`);
  visual.dataset.kpKeyboardBuilt = 'true';
  if (layoutKey) visual.dataset.kpLayoutId = layoutKey;
  container.appendChild(visual);

  // Layout is intentionally stable + reusable (not tied to popup.html).
  // Action keys are looked up by ID in `keybindings`.
  for (const row of layout) {
    const rowEl = el(doc, 'div', 'keyboard-row');
    visual.appendChild(rowEl);

    for (const item of row) {
      if (item.type === 'special') {
        // No KeyPilot function → no background icon.
        const keyEl = el(doc, 'div', item.className || 'key');
        keyEl.appendChild(el(doc, 'span', 'key-text', item.text));
        ensureKeyPressOverlay(doc, keyEl);
        rowEl.appendChild(keyEl);
        continue;
      }

      if (item.type === 'key') {
        // Unassigned alphanumeric key → no background icon.
        const keyEl = el(doc, 'div', item.className || 'key');
        keyEl.appendChild(el(doc, 'span', 'key-text', item.text));
        ensureKeyPressOverlay(doc, keyEl);
        rowEl.appendChild(keyEl);
        continue;
      }

      // action
      const binding = keybindings && keybindings[item.id];
      const baseClass = item.className || 'key';
      const className = `${baseClass}${binding && binding.keyboardClass ? ' ' + binding.keyboardClass : ''}`;
      const keyEl = el(doc, 'button', className);
      keyEl.dataset.kpActionId = item.id;
      keyEl.dataset.kpBaseClass = baseClass;
      keyEl.type = 'button'; // Prevent form submission if inside a form
      // Prefer aria-label over title so the browser native tooltip doesn't fight our hover popover.
      try { keyEl.removeAttribute('title'); } catch { /* ignore */ }
      keyEl.setAttribute(
        'aria-label',
        (binding && (binding.description || binding.label)) || item.fallbackText || item.id
      );
      // Only keys with functions get FA background icons.
      ensureKeyBackgroundIcon(doc, keyEl);

      const main = el(
        doc,
        'div',
        'key-main',
        (binding && binding.label) || item.fallbackText || item.id
      );
      keyEl.appendChild(main);

      const labelText = (binding && binding.displayKey) || (binding && binding.keyLabel) || '';
      if (labelText) {
        keyEl.appendChild(el(doc, 'div', 'key-label', labelText));
      }

      ensureKeyPressOverlay(doc, keyEl);
      rowEl.appendChild(keyEl);
    }
  }

  // Attach popover behavior directly to each key element (reusable, not tied to any page).
  attachKeyPopoverBehavior({
    root: container,
    keybindings
  });
}

/**
 * Whether the HTML Popover API is available on this element/document.
 * @param {HTMLElement|null} el
 * @returns {boolean}
 */
function supportsPopoverApi(el) {
  try {
    return !!(el && typeof el.showPopover === 'function' && typeof HTMLElement !== 'undefined'
      && 'popover' in HTMLElement.prototype);
  } catch {
    return false;
  }
}

/**
 * Ensure the shared key-info popover exists on document.body and is wired for
 * the Popover API (top layer — escapes keyboard panel overflow).
 * @param {Document} doc
 * @param {HTMLElement|null} [_container] ignored; kept for call-site compatibility
 * @returns {HTMLElement|null}
 */
function ensurePopover(doc, _container) {
  ensureStylesInjected(doc);
  if (!doc || !doc.body) return null;

  // Prefer a single shared popover on body (top-layer / fixed), not inside the panel.
  let pop = doc.body.querySelector('.kp-keybindings-popover[data-kp-key-info-popover="true"]')
    || doc.body.querySelector('.kp-keybindings-popover');

  // Migrate any legacy popover that still lives inside the floating keyboard panel.
  if (!pop) {
    try {
      const legacy = doc.querySelector('.kp-floating-keyboard-help .kp-keybindings-popover');
      if (legacy) {
        pop = legacy;
        doc.body.appendChild(pop);
      }
    } catch { /* ignore */ }
  }

  if (!pop) {
    pop = doc.createElement('div');
    pop.className = 'kp-keybindings-popover';
    pop.setAttribute('data-kp-key-info-popover', 'true');
    pop.setAttribute('data-placement', 'top');
    pop.setAttribute('role', 'tooltip');
    pop.innerHTML = `
      <div class="kp-popover-head">
        <div class="kp-popover-icon" aria-hidden="true"></div>
        <div class="kp-popover-title-wrap">
          <div class="kp-popover-title"></div>
          <div class="kp-popover-keys"></div>
        </div>
      </div>
      <p class="kp-popover-desc"></p>
    `;
    doc.body.appendChild(pop);
  } else {
    try { pop.setAttribute('data-kp-key-info-popover', 'true'); } catch { /* ignore */ }
    if (pop.parentElement !== doc.body) {
      try { doc.body.appendChild(pop); } catch { /* ignore */ }
    }
    if (!pop.querySelector('.kp-popover-icon')) {
      // Upgrade legacy popover markup (from older sessions / early inject).
      try {
        pop.setAttribute('role', 'tooltip');
        pop.innerHTML = `
          <div class="kp-popover-head">
            <div class="kp-popover-icon" aria-hidden="true"></div>
            <div class="kp-popover-title-wrap">
              <div class="kp-popover-title"></div>
              <div class="kp-popover-keys"></div>
            </div>
          </div>
          <p class="kp-popover-desc"></p>
        `;
      } catch { /* ignore */ }
    }
  }

  // HTML Popover API: manual mode so hover lifecycle owns show/hide (not light dismiss).
  try {
    if (supportsPopoverApi(pop)) {
      pop.popover = 'manual';
    }
  } catch {
    try { pop.setAttribute('popover', 'manual'); } catch { /* ignore */ }
  }

  // Start closed.
  try { pop.hidden = true; } catch { /* ignore */ }
  try { pop.removeAttribute('data-kp-popover-open'); } catch { /* ignore */ }

  return pop;
}

/**
 * @param {HTMLElement|null} pop
 */
function hidePopover(pop) {
  if (!pop) return;
  try {
    if (supportsPopoverApi(pop) && typeof pop.hidePopover === 'function') {
      try {
        if (pop.matches?.(':popover-open')) pop.hidePopover();
      } catch {
        try { pop.hidePopover(); } catch { /* ignore */ }
      }
    }
  } catch { /* ignore */ }
  try { pop.hidden = true; } catch { /* ignore */ }
  try { pop.removeAttribute('data-kp-popover-open'); } catch { /* ignore */ }
  try {
    const iconEl = pop.querySelector('.kp-popover-icon');
    if (iconEl) {
      iconEl.style.backgroundImage = '';
      iconEl.style.webkitMaskImage = '';
      iconEl.style.maskImage = '';
      iconEl.style.backgroundColor = '';
      iconEl.hidden = true;
    }
    // Clear key-theme overrides so the next hover starts clean.
    ['--kp-key-face', '--kp-key-mid', '--kp-key-deep', '--kp-key-icon'].forEach((prop) => {
      try { pop.style.removeProperty(prop); } catch { /* ignore */ }
    });
  } catch { /* ignore */ }
}

/**
 * Open the popover element (Popover API preferred; legacy display fallback).
 * @param {HTMLElement} pop
 */
function openPopoverElement(pop) {
  if (!pop) return;
  try { pop.hidden = false; } catch { /* ignore */ }
  try { pop.setAttribute('data-kp-popover-open', 'true'); } catch { /* ignore */ }
  if (supportsPopoverApi(pop) && typeof pop.showPopover === 'function') {
    try {
      if (!pop.matches?.(':popover-open')) pop.showPopover();
    } catch {
      try { pop.showPopover(); } catch { /* ignore */ }
    }
  }
}

function clamp(n, min, max) {
  return Math.max(min, Math.min(max, n));
}

/**
 * Copy keycap material CSS variables from the hovered key onto the popover
 * so the tooltip matches that key's color family.
 * @param {HTMLElement} pop
 * @param {HTMLElement} targetEl
 */
function applyKeyMaterialToPopover(pop, targetEl) {
  if (!pop || !targetEl) return;
  try {
    const cs = (targetEl.ownerDocument || document).defaultView?.getComputedStyle?.(targetEl);
    if (!cs) return;
    const face = (cs.getPropertyValue('--kp-key-face') || '').trim();
    const mid = (cs.getPropertyValue('--kp-key-mid') || '').trim();
    const deep = (cs.getPropertyValue('--kp-key-deep') || '').trim();
    const icon = (cs.getPropertyValue('--kp-key-icon') || '').trim();
    if (face) pop.style.setProperty('--kp-key-face', face);
    if (mid) pop.style.setProperty('--kp-key-mid', mid);
    if (deep) pop.style.setProperty('--kp-key-deep', deep);
    if (icon) pop.style.setProperty('--kp-key-icon', icon);
  } catch { /* ignore */ }
}

/**
 * Fill content and position the key-info popover against the *viewport*
 * (not the keyboard panel). Top-row keys can open above the panel; bottom-row
 * keys can open below it.
 *
 * @param {{
 *   doc: Document,
 *   pop: HTMLElement,
 *   targetEl: HTMLElement,
 *   binding: any,
 *   actionId: string,
 *   container?: HTMLElement|null
 * }} args
 */
function showPopoverForTarget({ doc, pop, targetEl, binding, actionId }) {
  if (!doc || !pop || !targetEl) return;

  const titleEl = pop.querySelector('.kp-popover-title');
  const keysEl = pop.querySelector('.kp-popover-keys');
  const descEl = pop.querySelector('.kp-popover-desc');
  const iconEl = pop.querySelector('.kp-popover-icon');

  const title = (binding && binding.label) || actionId;
  const keys = (binding && (binding.displayKey || binding.keyLabel)) || '';
  const desc = (binding && (binding.description || binding.label)) || '';
  // Black SVG for mask (same approach as key glyphs).
  const iconMaskUri = getActionIconDataUri(actionId, { fill: 'black' });

  // Match popover surface to this key's material tokens.
  applyKeyMaterialToPopover(pop, targetEl);

  if (titleEl) titleEl.textContent = title;
  if (keysEl) keysEl.textContent = keys ? `Key: ${keys}` : '';
  if (descEl) descEl.textContent = desc;
  if (iconEl) {
    if (iconMaskUri) {
      iconEl.hidden = false;
      iconEl.style.backgroundImage = 'none';
      iconEl.style.backgroundColor = 'var(--kp-key-icon)';
      iconEl.style.webkitMaskImage = iconMaskUri;
      iconEl.style.maskImage = iconMaskUri;
      iconEl.style.webkitMaskRepeat = 'no-repeat';
      iconEl.style.maskRepeat = 'no-repeat';
      iconEl.style.webkitMaskPosition = 'center';
      iconEl.style.maskPosition = 'center';
      iconEl.style.webkitMaskSize = '62% 62%';
      iconEl.style.maskSize = '62% 62%';
    } else {
      iconEl.hidden = true;
      iconEl.style.backgroundImage = '';
      iconEl.style.webkitMaskImage = '';
      iconEl.style.maskImage = '';
      iconEl.style.backgroundColor = '';
    }
  }

  // Capture key geometry *before* open (stable, independent of popover layer).
  const targetRect = targetEl.getBoundingClientRect();

  // Open first so layout/measurement works (top layer via Popover API).
  openPopoverElement(pop);

  /**
   * Write fixed coords so UA [popover] inset/margin cannot win the cascade.
   * Prefer setProperty with important for left/top (survives inset conflicts).
   *
   * Never call removeProperty('inset') *after* setting left/top: Chrome often
   * serializes left/top as the inset shorthand, so removing inset wipes them
   * and the tooltip jumps to the viewport origin.
   *
   * @param {number} leftPx
   * @param {number} topPx
   */
  const placeAt = (leftPx, topPx) => {
    try {
      // Drop any prior inset shorthand *first*, then set longhands.
      try { pop.style.removeProperty('inset'); } catch { /* ignore */ }
      pop.style.setProperty('position', 'fixed', 'important');
      pop.style.setProperty('margin', '0', 'important');
      pop.style.setProperty('right', 'auto', 'important');
      pop.style.setProperty('bottom', 'auto', 'important');
      pop.style.setProperty('left', `${Math.round(leftPx)}px`, 'important');
      pop.style.setProperty('top', `${Math.round(topPx)}px`, 'important');
    } catch {
      try { pop.style.removeProperty('inset'); } catch { /* ignore */ }
      pop.style.position = 'fixed';
      pop.style.margin = '0';
      pop.style.right = 'auto';
      pop.style.bottom = 'auto';
      pop.style.left = `${Math.round(leftPx)}px`;
      pop.style.top = `${Math.round(topPx)}px`;
    }
  };

  // Viewport-relative fixed positioning (escapes panel overflow).
  const margin = 10;
  const gap = 10; // distance between target and popover box (arrow included)

  // Measure while open; park off-screen first to avoid one-frame flicker at 0,0.
  placeAt(-9999, -9999);

  const popRect = pop.getBoundingClientRect();
  const popW = popRect.width || pop.offsetWidth || 160;
  const popH = popRect.height || pop.offsetHeight || 80;
  const vw = Math.max(
    doc.documentElement?.clientWidth || 0,
    (typeof window !== 'undefined' ? window.innerWidth : 0) || 0
  );
  const vh = Math.max(
    doc.documentElement?.clientHeight || 0,
    (typeof window !== 'undefined' ? window.innerHeight : 0) || 0
  );

  // Prefer above the key when there is room in the *viewport* (not the panel).
  // Top-row keys can therefore render above the keyboard reference window.
  const spaceAbove = targetRect.top;
  const spaceBelow = vh - targetRect.bottom;
  const needs = popH + gap + margin;
  const placeAbove = spaceAbove >= needs || (spaceAbove >= spaceBelow && spaceAbove >= gap + 24);
  const placement = placeAbove ? 'top' : 'bottom';
  pop.setAttribute('data-placement', placement);

  const targetCenterX = targetRect.left + targetRect.width / 2;

  let left = targetCenterX - popW / 2;
  const maxLeft = Math.max(margin, vw - margin - popW);
  left = clamp(left, margin, maxLeft);

  let top;
  if (placement === 'top') {
    top = targetRect.top - gap - popH;
    // Soft clamp: keep fully on-screen when possible, but never force bottom
    // placement just because the panel is short — we already chose above.
    if (top < margin) top = margin;
  } else {
    top = targetRect.bottom + gap;
    const maxTop = Math.max(margin, vh - margin - popH);
    if (top > maxTop) top = maxTop;
  }

  placeAt(left, top);

  // Arrow alignment: set CSS variable relative to popover box.
  const arrowLeft = clamp(targetCenterX - left - 9, 12, Math.max(12, popW - 24));
  pop.style.setProperty('--kp-arrow-left', `${Math.round(arrowLeft)}px`);
}

/**
 * Notify listeners that a Keyboard Reference key info popover was shown via hover/focus.
 * Uses the same keypilot:action channel as F-key activations so onboarding can complete
 * tasks like "hover a key to see what it does".
 * @param {{ actionId?: string, keyEl?: Element|null }} [detail]
 */
function emitKeyboardHelpKeyHover(detail = {}) {
  try {
    // Debounce rapid pointerenter chatter across adjacent keys so onboarding only needs one real hover.
    const now = Date.now();
    if (emitKeyboardHelpKeyHover._lastAt && now - emitKeyboardHelpKeyHover._lastAt < 120) return;
    emitKeyboardHelpKeyHover._lastAt = now;

    document.dispatchEvent(new CustomEvent('keypilot:action', {
      detail: {
        action: 'hover',
        isKeyboardHelpKey: true,
        actionId: detail.actionId ? String(detail.actionId) : null,
        timestamp: now
      }
    }));
  } catch {
    // ignore
  }
}

function attachKeyPopoverBehavior({ root, keybindings }) {
  if (!root) return;
  const doc = root.ownerDocument || document;

  // Popover lives on document.body + Popover API top layer (not clipped by panel).
  const pop = ensurePopover(doc, null);
  if (!pop) return;

  // Store handlers on the root to avoid duplicate attachments
  if (!root._kpKeyHandlers) {
    root._kpKeyHandlers = {
      enter: null,
      leave: null,
      focusin: null,
      focusout: null,
      docKeydown: null,
      resize: null,
      hideTimer: null
    };
  }

  const clearHideTimer = () => {
    if (root._kpKeyHandlers.hideTimer != null) {
      try { clearTimeout(root._kpKeyHandlers.hideTimer); } catch { /* ignore */ }
      root._kpKeyHandlers.hideTimer = null;
    }
  };

  const scheduleHide = () => {
    clearHideTimer();
    root._kpKeyHandlers.hideTimer = setTimeout(() => {
      hidePopover(pop);
      root._kpKeyHandlers.hideTimer = null;
    }, 60);
  };

  const showForKeyEl = (keyEl) => {
    if (!keyEl || !keyEl.dataset?.kpActionId) return;
    // Text mode: only Click Element stays interactive on the keyboard reference.
    try {
      if (keyEl.classList?.contains('kp-key-text-mode-disabled')) return;
    } catch { /* ignore */ }
    const actionId = keyEl.dataset.kpActionId;
    const binding = keybindings && keybindings[actionId];
    if (!binding) return;
    clearHideTimer();
    showPopoverForTarget({ doc, pop, targetEl: keyEl, binding, actionId });
    // Onboarding / other listeners: user hovered a Keyboard Reference key and saw its info.
    emitKeyboardHelpKeyHover({ actionId, keyEl });
  };

  // Remove existing handlers if re-attaching
  const keyElements = root.querySelectorAll('[data-kp-action-id]');
  if (root._kpKeyHandlers.enter) {
    keyElements.forEach((keyEl) => {
      try {
        if (root._kpKeyHandlers.enter) keyEl.removeEventListener('pointerenter', root._kpKeyHandlers.enter);
        if (root._kpKeyHandlers.leave) keyEl.removeEventListener('pointerleave', root._kpKeyHandlers.leave);
        if (root._kpKeyHandlers.focusin) keyEl.removeEventListener('focusin', root._kpKeyHandlers.focusin);
        if (root._kpKeyHandlers.focusout) keyEl.removeEventListener('focusout', root._kpKeyHandlers.focusout);
      } catch { /* ignore */ }
    });
  }

  function handleKeyEnter(e) {
    showForKeyEl(e.currentTarget);
  }

  function handleKeyLeave() {
    scheduleHide();
  }

  function handleKeyFocusIn(e) {
    showForKeyEl(e.currentTarget);
  }

  function handleKeyFocusOut() {
    scheduleHide();
  }

  // Store handlers for cleanup / re-attach
  root._kpKeyHandlers.enter = handleKeyEnter;
  root._kpKeyHandlers.leave = handleKeyLeave;
  root._kpKeyHandlers.focusin = handleKeyFocusIn;
  root._kpKeyHandlers.focusout = handleKeyFocusOut;

  if (keyElements.length === 0) {
    console.warn('[KeyPilot] No key elements found for popover behavior in:', root);
  }

  keyElements.forEach((keyEl) => {
    // Hover (pointer) — primary interaction
    keyEl.addEventListener('pointerenter', handleKeyEnter);
    keyEl.addEventListener('pointerleave', handleKeyLeave);
    // Keyboard focus for accessibility
    keyEl.addEventListener('focusin', handleKeyFocusIn);
    keyEl.addEventListener('focusout', handleKeyFocusOut);
  });

  // Escape / resize hide (only attach once per root)
  if (!root._kpKeyHandlers.docKeydown) {
    function handleDocKeydown(e) {
      if (e.key === 'Escape') hidePopover(pop);
    }
    
    function handleResize() {
      hidePopover(pop);
    }

    root._kpKeyHandlers.docKeydown = handleDocKeydown;
    root._kpKeyHandlers.resize = handleResize;

    doc.addEventListener('keydown', handleDocKeydown, true);
    window.addEventListener('resize', handleResize);
  }
}

function normalizeLegendRows(keybindings, extraRows) {
  const rows = [];
  for (const [id, b] of Object.entries(keybindings || {})) {
    rows.push({
      id,
      keys: b.displayKey || b.keyLabel || (Array.isArray(b.keys) ? b.keys.join(' / ') : ''),
      action: b.description || b.label || id,
      sortRow: typeof b.row === 'number' ? b.row : 99
    });
  }

  for (const extra of extraRows || []) {
    rows.push({
      id: extra.id || `extra_${rows.length}`,
      keys: extra.keys,
      action: extra.action,
      sortRow: 100
    });
  }

  // Stable sort: row then key label.
  rows.sort((a, b) => {
    if (a.sortRow !== b.sortRow) return a.sortRow - b.sortRow;
    return String(a.keys).localeCompare(String(b.keys));
  });

  return rows;
}

/**
 * Render the legend table body.
 *
 * @param {Object} params
 * @param {HTMLElement} params.tbody
 * @param {Record<string, any>} params.keybindings
 * @param {Array<{id?: string, keys: string, action: string}>} [params.extraRows]
 */
function renderKeybindingsLegendTable({ tbody, keybindings, extraRows = [] }) {
  if (!tbody) return;
  const doc = tbody.ownerDocument || document;
  clearElement(tbody);

  const rows = normalizeLegendRows(keybindings, extraRows);
  for (const r of rows) {
    const tr = el(doc, 'tr');
    tr.appendChild(el(doc, 'td', '', r.keys));
    tr.appendChild(el(doc, 'td', '', r.action));
    tbody.appendChild(tr);
  }
}

/**
 * Convenience wrapper for rendering both keyboard + legend.
 */
function renderKeybindingsUI({ keyboardContainer, legendTbody, keybindings, keyboardLayout, layoutId, extraRows = [] }) {
  renderKeybindingsKeyboard({ container: keyboardContainer, keybindings, keyboardLayout, layoutId });
  renderKeybindingsLegendTable({ tbody: legendTbody, keybindings, extraRows });
}





  // Module: src/ui/popup-theme-vars.js
/**
 * Popup theme CSS variables used across KeyPilot UI surfaces.
 *
 * Keep this in sync with `extension/styles/popup.css` `:root { ... }`.
 * We centralize the JS-side usage here so multiple UI surfaces (floating help,
 * early-inject shell) don't each hardcode their own copies.
 */

const POPUP_THEME_VARS = {
  '--fg': '#f8fafc',
  '--bg': '#0f172a',
  '--surface': '#1e293b',
  '--surface-light': '#334155',
  '--muted': '#94a3b8',
  '--ok': '#10b981',
  '--warn': '#f59e0b',
  '--err': '#44c7ef',
  '--brand': '#3b82f6',
  '--brand-dark': '#1e40af',
  '--border': '#334155',
  '--border2': '#4d617b'
};

function applyPopupThemeVars(targetEl) {
  if (!targetEl || !targetEl.style) return;
  try {
    for (const [k, v] of Object.entries(POPUP_THEME_VARS)) {
      targetEl.style.setProperty(k, v);
    }
  } catch {
    // ignore
  }
}





  // Module: src/ui/preview-open-actions.js
/**
 * Shared outline "Open" / "Open in New Tab" action buttons for preview chrome
 * (Link Preview titlebar, Launcher preview bar, etc.).
 */

/**
 * @param {Document} doc
 * @param {Array<{ tag?: string, attrs: Record<string, string> }>} paths
 * @returns {SVGElement}
 */
function createOutlineIcon(doc, paths) {
  const svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
  svg.setAttribute('viewBox', '0 0 24 24');
  svg.setAttribute('width', '14');
  svg.setAttribute('height', '14');
  svg.setAttribute('aria-hidden', 'true');
  svg.style.cssText = 'flex-shrink: 0; display: block;';
  const common = {
    fill: 'none',
    stroke: 'currentColor',
    'stroke-width': '1.75',
    'stroke-linecap': 'round',
    'stroke-linejoin': 'round'
  };
  for (const p of paths || []) {
    const el = doc.createElementNS('http://www.w3.org/2000/svg', p.tag || 'path');
    for (const [k, v] of Object.entries({ ...common, ...(p.attrs || {}) })) {
      el.setAttribute(k, v);
    }
    svg.appendChild(el);
  }
  return svg;
}

// Explicit chrome styles so host-page bare `button` rules cannot restyle layout.
// Slashdot (classic.ssl.css) ships `button { margin: 0 0 40px 0; margin-right: 10%; ... }`
// which otherwise inflates the Link Preview titlebar (~76px instead of ~34px).
const TITLEBAR_BTN_STYLE = `
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  box-sizing: border-box;
  background: transparent;
  border: 1px solid #4a4a4a;
  color: #e0e0e0;
  font-size: 11px;
  font-weight: 500;
  font-family: ${KP_UI_FONT};
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  text-shadow: none;
  box-shadow: none;
  padding: 4px 8px;
  border-radius: 5px;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
  min-width: 0;
  min-height: 0;
  height: auto;
  width: auto;
  position: relative;
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
`;

/**
 * @param {HTMLElement} btn
 */
function wireTitlebarBtnHover(btn) {
  btn.addEventListener('mouseenter', () => {
    btn.style.background = 'rgba(255,255,255,0.08)';
    btn.style.borderColor = '#6a6a6a';
    btn.style.color = '#fff';
  });
  btn.addEventListener('mouseleave', () => {
    btn.style.background = 'transparent';
    btn.style.borderColor = '#4a4a4a';
    btn.style.color = '#e0e0e0';
  });
}

/**
 * Build outline "Open" + "Open in New Tab" buttons.
 *
 * @param {object} opts
 * @param {Document} [opts.doc]
 * @param {() => (string|null|undefined)} opts.getUrl - resolve the URL at click time
 * @param {() => void} [opts.afterOpen] - e.g. close popover / launcher after navigating
 * @param {() => void} [opts.afterOpenNewTab]
 * @returns {{ openButton: HTMLButtonElement, openNewTabButton: HTMLButtonElement, actions: HTMLElement }}
 */
function createPreviewOpenActionButtons({
  doc = document,
  getUrl,
  afterOpen,
  afterOpenNewTab
} = {}) {
  const resolveUrl = typeof getUrl === 'function' ? getUrl : () => null;

  const actions = doc.createElement('div');
  actions.className = 'kp-preview-open-actions';
  actions.style.cssText = `
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
  `;

  // "Open" — navigate current tab
  const openButton = doc.createElement('button');
  openButton.type = 'button';
  openButton.className = 'kpv2-preview-open-btn';
  openButton.style.cssText = TITLEBAR_BTN_STYLE;
  openButton.appendChild(createOutlineIcon(doc, [
    { attrs: { d: 'M14 3h7v7' } },
    { attrs: { d: 'M10 14L21 3' } },
    { attrs: { d: 'M21 14v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5' } }
  ]));
  const openLabel = doc.createElement('span');
  openLabel.textContent = 'Open';
  openButton.appendChild(openLabel);
  openButton.title = 'Open in this tab';
  openButton.setAttribute('aria-label', 'Open in this tab');
  openButton.onclick = (e) => {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    const url = resolveUrl();
    if (!url) return;
    try {
      window.location.assign(url);
    } catch {
      try { window.location.href = url; } catch { /* ignore */ }
    }
    try { afterOpen?.(); } catch { /* ignore */ }
  };
  wireTitlebarBtnHover(openButton);

  // "Open in New Tab"
  const openNewTabButton = doc.createElement('button');
  openNewTabButton.type = 'button';
  openNewTabButton.className = 'kpv2-preview-open-new-tab-btn';
  openNewTabButton.style.cssText = TITLEBAR_BTN_STYLE;
  openNewTabButton.appendChild(createOutlineIcon(doc, [
    { tag: 'rect', attrs: { x: '8', y: '8', width: '13', height: '13', rx: '2' } },
    { tag: 'path', attrs: { d: 'M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3' } }
  ]));
  const openNewTabLabel = doc.createElement('span');
  openNewTabLabel.textContent = 'Open in New Tab';
  openNewTabButton.appendChild(openNewTabLabel);
  openNewTabButton.title = 'Open in new tab';
  openNewTabButton.setAttribute('aria-label', 'Open in new tab');
  openNewTabButton.onclick = (e) => {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    const url = resolveUrl();
    if (!url) return;
    try {
      window.open(url, '_blank', 'noopener,noreferrer');
    } catch {
      try { window.open(url, '_blank'); } catch { /* ignore */ }
    }
    try { afterOpenNewTab?.(); } catch { /* ignore */ }
  };
  wireTitlebarBtnHover(openNewTabButton);

  actions.appendChild(openButton);
  actions.appendChild(openNewTabButton);

  return { openButton, openNewTabButton, actions };
}



  // Module: src/ui/segmented-control.js
/**
 * Compact segmented control for popover chrome (titlebars, toolbars).
 *
 * Dark theme to match {@link createPopoverTitlebar} preview/modal variants.
 */

/**
 * @typedef {object} SegmentedControlOption
 * @property {string} value
 * @property {string} label
 * @property {string} [title]
 * @property {string} [ariaLabel]
 */

/**
 * @typedef {object} SegmentedControlConfig
 * @property {Document} [doc]
 * @property {SegmentedControlOption[]} options
 * @property {string} [value] - initially selected value
 * @property {(value: string, previous: string|null) => void} [onChange]
 * @property {string} [className]
 * @property {string} [ariaLabel]
 */

/**
 * @param {SegmentedControlConfig} [config]
 * @returns {{
 *   root: HTMLElement,
 *   buttons: HTMLButtonElement[],
 *   getValue: () => string|null,
 *   setValue: (value: string, opts?: { silent?: boolean }) => void,
 *   getInteractiveElements: () => HTMLElement[]
 * }}
 */
function createSegmentedControl(config = {}) {
  const doc = config.doc || document;
  const options = Array.isArray(config.options) ? config.options.filter(Boolean) : [];
  const className = typeof config.className === 'string' && config.className.trim()
    ? config.className.trim()
    : 'kp-segmented-control';

  /** @type {string|null} */
  let currentValue = null;
  if (config.value != null && options.some((o) => o.value === config.value)) {
    currentValue = String(config.value);
  } else if (options.length) {
    currentValue = String(options[0].value);
  }

  const root = doc.createElement('div');
  root.className = className;
  root.setAttribute('role', 'radiogroup');
  if (config.ariaLabel) {
    root.setAttribute('aria-label', config.ariaLabel);
  }
  root.style.cssText = `
    display: inline-flex;
    align-items: stretch;
    flex-shrink: 0;
    border: 1px solid #4a4a4a;
    border-radius: 5px;
    overflow: hidden;
    background: rgba(0, 0, 0, 0.25);
    font-family: ${KP_UI_FONT};
  `;

  /** @type {HTMLButtonElement[]} */
  const buttons = [];

  const applySelectionStyles = () => {
    for (const btn of buttons) {
      const selected = btn.dataset.value === currentValue;
      btn.setAttribute('aria-checked', selected ? 'true' : 'false');
      btn.dataset.selected = selected ? '1' : '0';
      btn.style.background = selected ? 'rgba(255,255,255,0.12)' : 'transparent';
      btn.style.color = selected ? '#fff' : '#b8b8b8';
      btn.style.fontWeight = selected ? '600' : '500';
    }
  };

  /**
   * @param {string} value
   * @param {{ silent?: boolean }} [opts]
   */
  const setValue = (value, opts = {}) => {
    const next = value != null ? String(value) : null;
    if (!options.some((o) => o.value === next)) return;
    const prev = currentValue;
    if (prev === next) {
      applySelectionStyles();
      return;
    }
    currentValue = next;
    applySelectionStyles();
    if (!opts.silent && typeof config.onChange === 'function') {
      try {
        config.onChange(currentValue, prev);
      } catch { /* ignore */ }
    }
  };

  options.forEach((opt, index) => {
    const btn = doc.createElement('button');
    btn.type = 'button';
    btn.className = 'kp-segmented-control-btn';
    btn.dataset.value = String(opt.value);
    btn.setAttribute('role', 'radio');
    btn.textContent = opt.label != null ? String(opt.label) : String(opt.value);
    if (opt.title) btn.title = opt.title;
    btn.setAttribute(
      'aria-label',
      opt.ariaLabel || opt.title || (opt.label != null ? String(opt.label) : String(opt.value))
    );
    btn.style.cssText = `
      display: inline-flex;
      align-items: center;
      justify-content: center;
      margin: 0;
      padding: 3px 8px;
      border: none;
      border-right: ${index < options.length - 1 ? '1px solid #4a4a4a' : 'none'};
      border-radius: 0;
      background: transparent;
      color: #b8b8b8;
      font-size: 11px;
      font-weight: 500;
      font-family: ${KP_UI_FONT};
      line-height: 1.2;
      letter-spacing: normal;
      text-transform: none;
      cursor: pointer;
      white-space: nowrap;
      transition: background 0.12s ease, color 0.12s ease;
    `;

    btn.addEventListener('mouseenter', () => {
      if (btn.dataset.selected === '1') return;
      btn.style.background = 'rgba(255,255,255,0.06)';
      btn.style.color = '#e0e0e0';
    });
    btn.addEventListener('mouseleave', () => {
      applySelectionStyles();
    });
    btn.addEventListener('click', (e) => {
      try {
        e.preventDefault();
        e.stopPropagation();
      } catch { /* ignore */ }
      setValue(String(opt.value));
    });

    buttons.push(btn);
    root.appendChild(btn);
  });

  applySelectionStyles();

  return {
    root,
    buttons,
    getValue: () => currentValue,
    setValue,
    getInteractiveElements: () => buttons.slice()
  };
}



  // Module: src/ui/popover-titlebar.js
/**
 * Standard KeyPilot popover titlebar chrome.
 *
 * Used by modal iframe popovers (Settings, Guide, Open Popover) and preview
 * popovers so close buttons, title layout, and optional action slots stay uniform.
 *
 * Layout (left → right):
 *   [ title  · optional hint ]     [ actions… ]  [ × close ]
 */

// Pin UI font (KP_UI_FONT) so host pages cannot leak typography into chrome.
// Preview popovers mount in the light DOM under body and inherit page fonts otherwise.

const VARIANT_STYLES = {
  modal: {
    titlebar: `
      padding: 10px 14px;
      background: linear-gradient(180deg, #232323 0%, #151515 100%);
      border-bottom: 1px solid #2b2b2b;
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 10px;
      flex-shrink: 0;
      min-height: 40px;
      box-sizing: border-box;
      font-family: ${KP_UI_FONT};
      font-size: 14px;
      font-weight: 400;
      font-style: normal;
      line-height: 1.3;
      letter-spacing: normal;
      text-transform: none;
      -webkit-font-smoothing: antialiased;
    `,
    title: `
      font-family: inherit;
      font-size: 14px;
      font-weight: 500;
      color: #e8e8e8;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      min-width: 0;
      flex: 0 1 auto;
    `,
    hint: `
      font-family: inherit;
      color: #999;
      font-weight: normal;
      font-size: 12px;
      margin-left: 10px;
      flex-shrink: 0;
    `,
    close: `
      /* Host pages often style bare \`button\` (e.g. Slashdot margin-bottom:40px). */
      margin: 0;
      appearance: none;
      -webkit-appearance: none;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      font-family: inherit;
      background: linear-gradient(180deg, rgba(255,255,255,0.08) 0%, rgba(255,255,255,0.02) 100%);
      border: 1px solid #3a3a3a;
      font-size: 18px;
      font-weight: 400;
      cursor: pointer;
      color: #e8e8e8;
      padding: 2px 8px;
      line-height: 1;
      border-radius: 4px;
      flex-shrink: 0;
      min-width: 0;
      min-height: 0;
      height: auto;
      width: auto;
      text-align: center;
      text-shadow: none;
      box-shadow: none;
      position: relative;
    `
  },
  preview: {
    titlebar: `
      padding: 6px 10px;
      background: linear-gradient(180deg, #232323 0%, #151515 100%);
      border-bottom: 1px solid #2b2b2b;
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 8px;
      flex-shrink: 0;
      min-height: 34px;
      box-sizing: border-box;
      user-select: none;
      -webkit-user-select: none;
      touch-action: none;
      font-family: ${KP_UI_FONT};
      font-size: 12px;
      font-weight: 400;
      font-style: normal;
      line-height: 1.3;
      letter-spacing: normal;
      text-transform: none;
      -webkit-font-smoothing: antialiased;
    `,
    title: `
      font-family: inherit;
      font-size: 12px;
      font-weight: 500;
      color: #e8e8e8;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      min-width: 0;
      flex: 0 1 auto;
    `,
    hint: `
      font-family: inherit;
      color: #999;
      font-weight: normal;
      font-size: 12px;
      margin-left: 8px;
      flex-shrink: 0;
    `,
    close: `
      /* Host pages often style bare \`button\` (e.g. Slashdot margin-bottom:40px). */
      margin: 0;
      appearance: none;
      -webkit-appearance: none;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      font-family: inherit;
      background: linear-gradient(180deg, rgba(255,255,255,0.08) 0%, rgba(255,255,255,0.02) 100%);
      border: 1px solid #3a3a3a;
      font-size: 16px;
      font-weight: 400;
      cursor: pointer;
      color: #e8e8e8;
      padding: 2px 6px;
      line-height: 1;
      border-radius: 4px;
      flex-shrink: 0;
      min-width: 0;
      min-height: 0;
      height: auto;
      width: auto;
      text-align: center;
      text-shadow: none;
      box-shadow: none;
      position: relative;
    `
  }
};

const KBD_STYLE = `
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 11px;
  padding: 1px 6px;
  border: 1px solid #3a3a3a;
  border-bottom-color: #2a2a2a;
  border-radius: 4px;
  background: linear-gradient(180deg, #2b2b2b 0%, #1a1a1a 100%);
  color: #f1f1f1;
`;

/**
 * Compact key chip for titlebar hints.
 * @param {Document} [doc]
 * @param {string} label
 * @returns {HTMLElement}
 */
function createTitlebarKbd(doc = document, label = '') {
  const kbd = doc.createElement('kbd');
  kbd.style.cssText = KBD_STYLE;
  kbd.textContent = String(label || '');
  return kbd;
}

/**
 * Build a fragment: "Press" + kbd keys (joined by " / ") + optional trailing text.
 *
 * @param {object} opts
 * @param {Document} [opts.doc]
 * @param {string[]} [opts.keys]
 * @param {string} [opts.prefix='Press']
 * @param {string} [opts.suffix]
 * @param {boolean} [opts.useKbdChips=true]
 * @returns {DocumentFragment}
 */
function createTitlebarCloseHint({
  doc = document,
  keys = ['Esc'],
  prefix = 'Press',
  suffix = '',
  useKbdChips = true
} = {}) {
  const frag = doc.createDocumentFragment();
  if (prefix) {
    frag.appendChild(doc.createTextNode(`${prefix} `));
  }
  const list = Array.isArray(keys) ? keys.filter((k) => k != null && String(k).length) : [];
  list.forEach((key, i) => {
    if (i > 0) frag.appendChild(doc.createTextNode(' / '));
    if (useKbdChips) {
      frag.appendChild(createTitlebarKbd(doc, String(key)));
    } else {
      frag.appendChild(doc.createTextNode(String(key)));
    }
  });
  if (suffix) {
    const needsSpace = !/^\s/.test(suffix);
    frag.appendChild(doc.createTextNode(needsSpace ? ` ${suffix}` : suffix));
  }
  return frag;
}

/**
 * @typedef {object} PopoverTitlebarConfig
 * @property {Document} [doc]
 * @property {string} [title]
 * @property {string|Node|null} [hint] - plain text, or a Node/DocumentFragment for rich hints
 * @property {boolean} [showClose=true]
 * @property {() => void} [onClose]
 * @property {string} [closeTitle='Close (Esc)']
 * @property {string} [closeLabel='×']
 * @property {HTMLElement|HTMLElement[]|null} [actions] - nodes placed before the close button
 * @property {'modal'|'preview'} [variant='modal']
 * @property {boolean} [draggable=false]
 * @property {string} [className='kpv2-popover-titlebar']
 * @property {string} [ariaLabel]
 * @property {string} [titleAttr] - HTML title attribute on the bar (e.g. "Drag to move")
 */

/**
 * Create a standard popover titlebar.
 *
 * @param {PopoverTitlebarConfig} [config]
 * @returns {{
 *   titlebar: HTMLElement,
 *   closeButton: HTMLButtonElement|null,
 *   titleEl: HTMLElement,
 *   hintEl: HTMLElement,
 *   titleContainer: HTMLElement,
 *   actionsSlot: HTMLElement,
 *   setTitle: (text: string) => void,
 *   setHint: (hint: string|Node|null|undefined) => void,
 *   getInteractiveElements: () => HTMLElement[]
 * }}
 */
function createPopoverTitlebar(config = {}) {
  const doc = config.doc || document;
  const variant = config.variant === 'preview' ? 'preview' : 'modal';
  const styles = VARIANT_STYLES[variant];
  const showClose = config.showClose !== false;
  const className = typeof config.className === 'string' && config.className.trim()
    ? config.className.trim()
    : 'kpv2-popover-titlebar';

  const titlebar = doc.createElement('div');
  titlebar.className = className;
  titlebar.setAttribute('data-kp-popover-titlebar', 'true');
  titlebar.setAttribute('data-kp-titlebar-variant', variant);
  titlebar.style.cssText = styles.titlebar;

  if (config.draggable) {
    titlebar.style.cursor = 'grab';
  }
  if (config.titleAttr) {
    titlebar.title = config.titleAttr;
  }
  if (config.ariaLabel) {
    titlebar.setAttribute('aria-label', config.ariaLabel);
  }

  const titleContainer = doc.createElement('div');
  titleContainer.className = 'kpv2-popover-titlebar-title-wrap';
  titleContainer.style.cssText = `
    display: flex;
    align-items: baseline;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
    margin-right: 4px;
    ${config.draggable ? 'pointer-events: none;' : ''}
  `;

  const titleEl = doc.createElement('span');
  titleEl.className = 'kpv2-popover-titlebar-title';
  titleEl.style.cssText = styles.title;
  titleEl.textContent = typeof config.title === 'string' ? config.title : '';

  const hintEl = doc.createElement('span');
  hintEl.className = 'kpv2-popover-titlebar-hint';
  hintEl.style.cssText = styles.hint;

  titleContainer.appendChild(titleEl);
  titleContainer.appendChild(hintEl);
  titlebar.appendChild(titleContainer);

  const actionsSlot = doc.createElement('div');
  actionsSlot.className = 'kpv2-popover-titlebar-actions';
  actionsSlot.style.cssText = `
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
  `;

  const actionNodes = [];
  if (Array.isArray(config.actions)) {
    for (const node of config.actions) {
      if (node) {
        actionsSlot.appendChild(node);
        actionNodes.push(node);
      }
    }
  } else if (config.actions) {
    actionsSlot.appendChild(config.actions);
    actionNodes.push(config.actions);
  }

  if (actionsSlot.childNodes.length > 0) {
    titlebar.appendChild(actionsSlot);
  }

  /** @type {HTMLButtonElement|null} */
  let closeButton = null;
  if (showClose) {
    closeButton = doc.createElement('button');
    closeButton.type = 'button';
    closeButton.className = 'kpv2-popover-titlebar-close';
    closeButton.style.cssText = styles.close;
    closeButton.textContent = config.closeLabel != null ? String(config.closeLabel) : '×';
    closeButton.title = config.closeTitle || 'Close (Esc)';
    closeButton.setAttribute('aria-label', config.closeTitle || 'Close (Esc)');
    closeButton.addEventListener('click', (e) => {
      try {
        e.preventDefault();
        e.stopPropagation();
      } catch { /* ignore */ }
      try {
        config.onClose?.();
      } catch { /* ignore */ }
    });
    titlebar.appendChild(closeButton);
  }

  const setHint = (hint) => {
    while (hintEl.firstChild) hintEl.removeChild(hintEl.firstChild);
    if (hint == null || hint === '') {
      hintEl.style.display = 'none';
      return;
    }
    hintEl.style.display = '';
    if (typeof hint === 'string') {
      hintEl.textContent = hint;
    } else if (hint instanceof Node) {
      hintEl.appendChild(hint);
    } else {
      hintEl.textContent = String(hint);
    }
  };

  const setTitle = (text) => {
    titleEl.textContent = text != null ? String(text) : '';
  };

  setHint(config.hint);

  const getInteractiveElements = () => {
    const els = [];
    if (closeButton) els.push(closeButton);
    for (const node of actionNodes) {
      if (!node) continue;
      if (node.matches?.('button, a, input, select, textarea, [role="button"]')) {
        els.push(node);
      } else if (typeof node.querySelectorAll === 'function') {
        node.querySelectorAll('button, a, input, select, textarea, [role="button"]').forEach((el) => {
          els.push(el);
        });
      }
    }
    return els;
  };

  return {
    titlebar,
    closeButton,
    titleEl,
    hintEl,
    titleContainer,
    actionsSlot,
    setTitle,
    setHint,
    getInteractiveElements
  };
}



  // Module: src/ui/floating-keyboard-help.js
/**
 * Floating keyboard reference panel (content-script friendly).
 *
 * Shows KeyPilot's keyboard visualization in a small, fixed-position panel.
 *
 * Note: This is intentionally implemented in light DOM (no shadow root) because
 * `renderKeybindingsKeyboard()` injects its CSS into `document.head`.
 */







/** Match legacy keyboard dock inset (left/bottom 16px) while still using shared snap/clamp. */
const KEYBOARD_POSITION_MARGIN_PX = Math.max(PANEL_POSITION_MARGIN_PX, 16);
/** Keep max size in sync with margin on every edge (was 24 → asymmetric right/bottom gaps). */
const KEYBOARD_MAX_VIEWPORT_INSET_PX = KEYBOARD_POSITION_MARGIN_PX * 2;

class FloatingKeyboardHelp {
  /**
   * @param {Object} params
   * @param {Record<string, any>} params.keybindings
   * @param {any[]} [params.keyboardLayout]
   * @param {string} [params.layoutId]
   * @param {import('../modules/settings-manager.js').PanelPositionSettings|null} [params.panelPosition]
   *   Optional known dock/free position (from KeyPilot settings). When provided,
   *   the first show paints at this location instead of flashing the default corner.
   */
  constructor({ keybindings, keyboardLayout, layoutId, panelPosition } = {}) {
    this.keybindings = keybindings || {};
    this.keyboardLayout = keyboardLayout || null;
    this.layoutId = typeof layoutId === 'string' ? layoutId : '';
    this.root = null;
    this.keyboardContainer = null;
    this.closeBtn = null;
    this.hintEl = null;
    /** @type {HTMLElement|null} */
    this._titlebar = null;
    this._onCloseClick = this._onCloseClick.bind(this);

    // Keydown/keyup visual feedback
    this._pressedLabels = new Set();
    this._keyElsByLabel = new Map();
    /** @type {Map<string, HTMLElement[]>} */
    this._keyElsByActionId = new Map();
    this._keydownBound = false;
    this._onDocKeyDown = this._onDocKeyDown.bind(this);
    this._onDocKeyUp = this._onDocKeyUp.bind(this);
    this._onWinBlur = this._onWinBlur.bind(this);

    // When hovering a page link, highlight action keys that can activate it.
    this._linkHoverHintActive = false;
    /** @type {Set<string>} */
    this._linkHoverHintActionIds = new Set();

    // Text-focus mode: all keys grayed out; ACTIVATE only lights up while the
    // hover-click countdown is armed on a clickable under the cursor.
    this._textModeFilterActive = false;
    this._textModeActivateArmed = false;

    this._keyFeedbackEnabled = true;
    this._settingsBound = false;
    this._onStorageChanged = this._onStorageChanged.bind(this);

    // Titlebar drag + edge/corner resize (via shared panel-position system)
    this._windowChromeBound = false;
    /** @type {(() => void)|null} */
    this._resizeDispose = null;
    /** @type {(() => void)|null} */
    this._dragDispose = null;

    /** @type {import('../modules/settings-manager.js').PanelPositionSettings|null} */
    this._panelPosition = {
      ...DEFAULT_SETTINGS.panelPositions.keyboardReference
    };
    /** True once position has been seeded from settings/DOM or loaded from storage. */
    this._positionHydrated = false;
    /** Monotonic token so delayed first-show reveals don't race with hide/cleanup. */
    this._showGeneration = 0;
    this._positionApplyScheduled = false;
    this._onWinResizePosition = this._onWinResizePosition.bind(this);
    this._suppressPositionPersist = false;

    if (panelPosition && typeof panelPosition === 'object') {
      this._seedPanelPosition(panelPosition, { hydrated: true });
    }
  }

  /**
   * Update in-memory dock/free position (does not paint unless root exists).
   * @param {import('../modules/settings-manager.js').PanelPositionSettings|null|undefined} next
   * @param {{ hydrated?: boolean }} [opts]
   */
  _seedPanelPosition(next, opts = {}) {
    const normalized = normalizePanelPositionState(
      next,
      DEFAULT_SETTINGS.panelPositions.keyboardReference
    ) || { ...DEFAULT_SETTINGS.panelPositions.keyboardReference };
    this._panelPosition = {
      left: normalized.left,
      top: normalized.top,
      anchor: normalized.anchor === undefined ? null : normalized.anchor
    };
    if (opts.hydrated) this._positionHydrated = true;
  }

  /**
   * Public: seed position from already-loaded KeyPilot settings before show().
   * @param {import('../modules/settings-manager.js').PanelPositionSettings|null|undefined} next
   */
  setPanelPositionFromSettings(next) {
    if (!next || typeof next !== 'object') return;
    this._seedPanelPosition(next, { hydrated: true });
    if (this.root) this._applyPanelPositionNow();
  }

  /**
   * Read left/top (and optional anchor attr) already painted on a shell element.
   * @param {HTMLElement|null} el
   * @returns {import('../modules/settings-manager.js').PanelPositionSettings|null}
   */
  _readPositionFromDom(el) {
    if (!el || !el.style) return null;
    try {
      const left = parseFloat(el.style.left);
      const top = parseFloat(el.style.top);
      if (!Number.isFinite(left) || !Number.isFinite(top)) return null;
      let anchor = null;
      try {
        const attr = el.getAttribute('data-kp-panel-anchor');
        if (attr) anchor = attr;
      } catch { /* ignore */ }
      // Free positions use left/top with no anchor (or explicit null).
      return { left, top, anchor: anchor || null };
    } catch {
      return null;
    }
  }

  setKeybindings(keybindings) {
    this.keybindings = keybindings || {};
    if (this.root && !this.root.hidden) {
      this._render();
    }
  }

  setKeyboardLayout({ keyboardLayout, layoutId } = {}) {
    this.keyboardLayout = keyboardLayout || null;
    this.layoutId = typeof layoutId === 'string' ? layoutId : '';
    if (this.root && !this.root.hidden) {
      this._render();
    }
  }

  isVisible() {
    if (!this.root || !this.root.isConnected) return false;
    if (this.root.hidden) return false;
    // Inline display:flex (panel chrome) can override [hidden] on some host pages;
    // treat explicit none as hidden as well.
    try {
      if (this.root.style && this.root.style.display === 'none') return false;
    } catch { /* ignore */ }
    return true;
  }

  /**
   * Show/hide must set both the `hidden` attribute and inline display.
   * Our panel chrome uses display:flex; without clearing it, hide() can fail on
   * pages that weaken or override [hidden]{display:none}.
   * @param {boolean} visible
   */
  _setRootVisible(visible) {
    if (!this.root) return;
    if (visible) {
      try { this.root.hidden = false; } catch { /* ignore */ }
      try { this.root.style.display = 'flex'; } catch { /* ignore */ }
      try { this.root.setAttribute('aria-hidden', 'false'); } catch { /* ignore */ }
    } else {
      try { this.root.hidden = true; } catch { /* ignore */ }
      try { this.root.style.display = 'none'; } catch { /* ignore */ }
      try { this.root.setAttribute('aria-hidden', 'true'); } catch { /* ignore */ }
    }
  }

  show() {
    // Never show inside iframes (avoids duplicating the panel in popover iframes).
    if (window !== window.top) return;
    const gen = ++this._showGeneration;
    this._ensure();
    this._bindSettingsSync();
    this._refreshKeyFeedbackSetting(); // async; best-effort

    // Always paint the best-known position before the panel becomes visible so a
    // saved free/dock location never flashes at the default bottom-left corner.
    this._applyPanelPositionNow();

    const reveal = () => {
      if (gen !== this._showGeneration) return;
      if (!this.root || !this.root.isConnected) return;
      this._applyPanelPositionNow();
      this._setRootVisible(true);
      this._render();
      // Reclamp after keyboard rows size (free tops can shift once height is known).
      this._schedulePanelPositionAfterLayout();
      this._bindKeydownFeedback();
      try {
        const mode = window.__KeyPilotInstance?.state?.getState?.()?.mode;
        const inText = String(mode || '') === 'text_focus';
        this.setTextModeFilter(inText || this._textModeFilterActive);
      } catch {
        if (this._textModeFilterActive) this._applyTextModeFilterClasses(true);
      }
    };

    if (this._positionHydrated) {
      reveal();
      // Background refresh keeps multi-tab moves in sync without a default-corner flash.
      void this._refreshPanelPosition();
      return;
    }

    // First show without a seeded position: stay hidden until storage returns.
    // (Root remains hidden from _ensure; do not call _setRootVisible(true) yet.)
    void this._refreshPanelPosition().finally(() => {
      if (gen !== this._showGeneration) return;
      this._positionHydrated = true;
      reveal();
    });
  }

  hide() {
    // Invalidate any in-flight first-show reveal so a late storage resolve cannot re-open.
    this._showGeneration += 1;
    this._setRootVisible(false);
    this.setLinkHoverHints(false);
    this.setTextModeFilter(false);
    this._unbindKeydownFeedback();
    this._unbindSettingsSync();
  }

  toggle() {
    if (this.isVisible()) this.hide();
    else this.show();
  }

  cleanup() {
    this._showGeneration += 1;
    try {
      if (this.closeBtn) this.closeBtn.removeEventListener('click', this._onCloseClick);
    } catch { /* ignore */ }
    this._unbindWindowChrome();
    this._unbindKeydownFeedback();
    this._unbindSettingsSync();
    try { this._posResizeObserver?.disconnect?.(); } catch { /* ignore */ }
    this._posResizeObserver = null;
    try {
      if (this.root && this.root.parentNode) this.root.parentNode.removeChild(this.root);
    } catch { /* ignore */ }
    this.root = null;
    this.keyboardContainer = null;
    this.closeBtn = null;
    this._titlebar = null;
  }

  /**
   * Panel shell chrome shared by create + early-inject adopt paths.
   * Position is applied separately via the shared panel-position system
   * (default anchor: bottom-left).
   * @param {HTMLElement} root
   */
  _applyProPanelChrome(root) {
    if (!root || !root.style) return;
    // Preserve intentional hide (display:none) — chrome must not force flex on a
    // hidden panel (that broke K-toggle / close after we added flex layout).
    let display = 'flex';
    try {
      if (root.hidden || root.style.display === 'none') display = 'none';
    } catch { /* ignore */ }
    Object.assign(root.style, {
      position: 'fixed',
      width: '760px',
      // Symmetric inset on all sides (matches KEYBOARD_POSITION_MARGIN_PX).
      maxWidth: `calc(100vw - ${KEYBOARD_MAX_VIEWPORT_INSET_PX}px)`,
      maxHeight: `calc(100vh - ${KEYBOARD_MAX_VIEWPORT_INSET_PX}px)`,
      // Flex column when visible (titlebar + body).
      display,
      flexDirection: 'column',
      overflow: 'hidden',
      boxSizing: 'border-box',
      zIndex: String(Z_INDEX.FLOATING_KEYBOARD_HELP),
      background: 'rgba(10, 11, 14, 0.98)',
      color: 'rgba(248, 250, 252, 0.95)',
      border: '1px solid rgba(255, 255, 255, 0.08)',
      borderRadius: '4px',
      boxShadow: '0 16px 40px rgba(0,0,0,0.55), 0 2px 8px rgba(0,0,0,0.35)',
      fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif',
      pointerEvents: 'auto'
    });
    applyPopupThemeVars(root);
  }

  /**
   * Apply current in-memory panel position to the root element.
   * Re-reads live size so free positions cannot stay below the fold after keyboard paint.
   */
  _applyPanelPositionNow() {
    if (!this.root) return;
    try {
      const resolved = applyPanelPosition(this.root, this._panelPosition, {
        margin: KEYBOARD_POSITION_MARGIN_PX,
        defaultAnchor: 'bottom-left',
        // Used when the panel has not laid out yet (height 0) so free tops
        // cannot pin to vh−margin and then grow off the bottom of the screen.
        fallbackWidth: 760,
        fallbackHeight: 200
      });
      // Keep in-memory free coords in sync with what was actually painted (clamped).
      if (resolved && !resolved.anchor) {
        this._panelPosition = {
          left: resolved.left,
          top: resolved.top,
          anchor: null
        };
      } else if (resolved?.anchor) {
        this._panelPosition = {
          left: resolved.left,
          top: resolved.top,
          anchor: resolved.anchor
        };
      }
    } catch { /* ignore */ }
  }

  /**
   * Re-apply after layout (keyboard rows often size one frame after show).
   */
  _schedulePanelPositionAfterLayout() {
    this._applyPanelPositionNow();
    try {
      requestAnimationFrame(() => {
        this._applyPanelPositionNow();
        requestAnimationFrame(() => this._applyPanelPositionNow());
      });
    } catch {
      this._applyPanelPositionNow();
    }
    // One-shot ResizeObserver: reclamp when content height appears / changes.
    try {
      if (typeof ResizeObserver === 'undefined' || !this.root) return;
      try { this._posResizeObserver?.disconnect?.(); } catch { /* ignore */ }
      let fires = 0;
      this._posResizeObserver = new ResizeObserver(() => {
        this._applyPanelPositionNow();
        if (++fires >= 4) {
          try { this._posResizeObserver?.disconnect?.(); } catch { /* ignore */ }
          this._posResizeObserver = null;
        }
      });
      this._posResizeObserver.observe(this.root);
    } catch { /* ignore */ }
  }

  /**
   * @param {import('../modules/settings-manager.js').PanelPositionSettings|null|undefined} next
   * @param {{ persist?: boolean }} [opts]
   */
  _setPanelPosition(next, opts = {}) {
    this._seedPanelPosition(next, { hydrated: true });
    this._applyPanelPositionNow();
    if (opts.persist && !this._suppressPositionPersist) {
      this._persistPanelPosition(this._panelPosition);
    }
  }

  async _refreshPanelPosition() {
    try {
      const settings = await getSettings();
      const stored = settings?.panelPositions?.keyboardReference;
      this._suppressPositionPersist = true;
      this._setPanelPosition(stored, { persist: false });
      this._suppressPositionPersist = false;
      this._positionHydrated = true;
      // Reclamp once size is known (no-op while hidden; safe after reveal).
      if (this.isVisible()) {
        this._schedulePanelPositionAfterLayout();
      } else {
        this._applyPanelPositionNow();
      }
      // If a free position was saved while height was 0, rewrite the clamped coords.
      try {
        const pos = this._panelPosition;
        if (pos && pos.anchor == null && Number.isFinite(pos.left) && Number.isFinite(pos.top)) {
          const before = stored && typeof stored === 'object' ? stored : null;
          const moved =
            !before ||
            Math.round(Number(before.top)) !== Math.round(pos.top) ||
            Math.round(Number(before.left)) !== Math.round(pos.left);
          // Only rewrite storage when the panel is actually visible + laid out;
          // otherwise a pre-show clamp with estimated height can corrupt the saved top.
          if (moved && this.isVisible()) {
            void this._persistPanelPosition(pos);
          }
        }
      } catch { /* ignore */ }
    } catch {
      this._suppressPositionPersist = false;
      // Fall back to whatever we already have (default or seeded).
      this._positionHydrated = true;
    }
  }

  /**
   * @param {import('../modules/settings-manager.js').PanelPositionSettings} position
   */
  async _persistPanelPosition(position) {
    try {
      await setSettings({
        panelPositions: {
          keyboardReference: {
            left: position.left,
            top: position.top,
            anchor: position.anchor === undefined ? null : position.anchor
          }
        }
      });
    } catch { /* ignore */ }
  }

  _onWinResizePosition() {
    if (!this.root || !this.isVisible()) return;
    if (this._positionApplyScheduled) return;
    this._positionApplyScheduled = true;
    try {
      requestAnimationFrame(() => {
        this._positionApplyScheduled = false;
        this._applyPanelPositionNow();
      });
    } catch {
      this._positionApplyScheduled = false;
      this._applyPanelPositionNow();
    }
  }

  /**
   * Compact, dark window-style titlebar (drag handle).
   * @param {HTMLElement|null} header
   * @param {{ titleEl?: HTMLElement|null, hintEl?: HTMLElement|null, closeBtn?: HTMLElement|null }} [parts]
   */
  _applyCompactTitlebar(header, parts = {}) {
    if (!header || !header.style) return;
    Object.assign(header.style, {
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
      gap: '8px',
      height: '28px',
      minHeight: '28px',
      maxHeight: '28px',
      boxSizing: 'border-box',
      padding: '0 6px 0 10px',
      margin: '0',
      borderBottom: '1px solid rgba(0,0,0,0.55)',
      background: 'linear-gradient(180deg, #1a1b1f 0%, #121316 100%)',
      flex: '0 0 auto',
      cursor: 'grab',
      userSelect: 'none',
      WebkitUserSelect: 'none',
      touchAction: 'none'
    });
    try {
      header.title = header.title || 'Drag to move';
    } catch { /* ignore */ }

    const titleEl = parts.titleEl || header.querySelector('[data-kp-floating-keyboard-title="true"]') || header.firstElementChild;
    if (titleEl && titleEl.style) {
      Object.assign(titleEl.style, {
        fontSize: '11px',
        fontWeight: '600',
        letterSpacing: '0.01em',
        textTransform: 'none',
        color: 'rgba(220, 220, 225, 0.9)',
        lineHeight: '28px',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        margin: '0',
        padding: '0'
      });
    }

    const hintEl = parts.hintEl || header.querySelector('[data-kp-floating-keyboard-hint="true"]');
    if (hintEl && hintEl.style) {
      Object.assign(hintEl.style, {
        display: 'inline-flex',
        alignItems: 'center',
        gap: '4px',
        marginLeft: 'auto',
        fontSize: '10px',
        fontWeight: '500',
        letterSpacing: '0',
        color: 'rgba(140, 145, 155, 0.95)',
        padding: '0 4px',
        borderRadius: '0',
        border: 'none',
        background: 'transparent',
        lineHeight: '28px',
        whiteSpace: 'nowrap'
      });
    }

    const closeBtn = parts.closeBtn
      || header.querySelector('button[data-kp-floating-keyboard-close="true"]')
      || header.querySelector('button[aria-label="Close keyboard reference"]');
    if (closeBtn && closeBtn.style) {
      Object.assign(closeBtn.style, {
        width: '22px',
        height: '22px',
        minWidth: '22px',
        minHeight: '22px',
        borderRadius: '4px',
        border: 'none',
        background: 'transparent',
        color: 'rgba(200, 200, 205, 0.9)',
        cursor: 'pointer',
        fontSize: '15px',
        lineHeight: '20px',
        padding: '0',
        margin: '0',
        flex: '0 0 auto',
        boxShadow: 'none'
      });
    }
  }

  /**
   * Body that wraps `.kp-floating-keyboard-help__keyboard` — no chrome padding.
   * @param {HTMLElement|null} body
   */
  _applyKeyboardBodyChrome(body) {
    if (!body || !body.style) return;
    Object.assign(body.style, {
      padding: '0',
      margin: '0',
      border: 'none',
      background: 'transparent',
      flex: '1 1 auto',
      minHeight: '0',
      // Fixed key sizes for now (resize/flex-scale temporarily suspended).
      overflow: 'auto'
    });
  }

  /**
   * Keyboard host chrome.
   * TEMP: plain block layout with fixed key sizes (flex-fill suspended with resize).
   * @param {HTMLElement|null} keyboardContainer
   */
  _applyKeyboardHostChrome(keyboardContainer) {
    if (!keyboardContainer || !keyboardContainer.style) return;
    Object.assign(keyboardContainer.style, {
      width: '100%',
      boxSizing: 'border-box'
    });
  }

  /**
   * Titlebar drag (shared panel-position: margin clamp + corner/edge snap)
   * + shared edge/corner resize with SE grip (resize currently suspended).
   */
  _bindWindowChrome() {
    if (this._windowChromeBound || !this.root) return;
    const panel = this.root;
    const header = this._titlebar
      || panel.querySelector('[data-kp-floating-keyboard-titlebar="true"]')
      || panel.firstElementChild;
    if (!header) return;

    this._titlebar = header;
    this._windowChromeBound = true;

    try {
      const api = makePanelDraggable(panel, header, {
        margin: KEYBOARD_POSITION_MARGIN_PX,
        excludeSelector:
          'button[data-kp-floating-keyboard-close="true"], button[aria-label="Close keyboard reference"], .kpv2-popover-resize-handle',
        onMoveEnd: (state) => {
          if (!state?.moved) return;
          this._setPanelPosition(
            {
              left: state.left,
              top: state.top,
              anchor: state.anchor
            },
            { persist: true }
          );
        }
      });
      this._dragDispose = api?.dispose || null;
    } catch (err) {
      console.warn('[KeyPilot] Failed to make keyboard reference draggable:', err?.message || err);
      this._dragDispose = null;
    }

    try {
      window.addEventListener('resize', this._onWinResizePosition, true);
    } catch { /* ignore */ }

    // TEMP suspended: resize + aspect lock (return when key flex-scaling is ready).
    // See keybindings-ui-shared.js floating-keyboard flex rules (also suspended).
    // try {
    //   this._resizeDispose?.();
    // } catch { /* ignore */ }
    // try {
    //   const api = makePopoverResizable(panel, {
    //     minWidth: 360,
    //     minHeight: 160,
    //     margin: PANEL_POSITION_MARGIN_PX,
    //     aspectRatio: true,
    //     onResizeStart: () => {
    //       pinPanelGeometry(panel);
    //     }
    //   });
    //   this._resizeDispose = api?.dispose || null;
    // } catch (err) {
    //   console.warn('[KeyPilot] Failed to make keyboard reference resizable:', err?.message || err);
    //   this._resizeDispose = null;
    // }
    this._resizeDispose = null;

    // Apply saved / default dock once chrome is ready.
    this._applyPanelPositionNow();
  }

  _unbindWindowChrome() {
    try { this._dragDispose?.(); } catch { /* ignore */ }
    this._dragDispose = null;
    try { this._resizeDispose?.(); } catch { /* ignore */ }
    this._resizeDispose = null;
    try { window.removeEventListener('resize', this._onWinResizePosition, true); } catch { /* ignore */ }
    this._windowChromeBound = false;
  }

  _ensure() {
    if (this.root && this.root.isConnected) {
      // Re-bind chrome if the root survived but listeners were torn down.
      this._bindWindowChrome();
      return;
    }

    // If early-inject created the shell at document_start, adopt it to avoid flicker.
    try {
      const existing = document.querySelector('.kp-floating-keyboard-help[data-kp-early-floating-keyboard="true"]');
      if (existing && existing.isConnected) {
        const keyboardContainer = existing.querySelector('.kp-floating-keyboard-help__keyboard');
        const closeBtn =
          existing.querySelector('button[data-kp-floating-keyboard-close="true"]') ||
          existing.querySelector('button[aria-label="Close keyboard reference"]');
        const header =
          existing.querySelector('[data-kp-floating-keyboard-titlebar="true"]') ||
          existing.firstElementChild;
        const body = keyboardContainer?.parentElement || null;
        const hintEl = existing.querySelector('[data-kp-floating-keyboard-hint="true"]');
        const titleEl = existing.querySelector('[data-kp-floating-keyboard-title="true"]')
          || (header ? header.querySelector('div:not([data-kp-floating-keyboard-hint])') : null);

        // Prefer early shell's already-applied position when we were not seeded.
        if (!this._positionHydrated) {
          const fromDom = this._readPositionFromDom(existing);
          if (fromDom) this._seedPanelPosition(fromDom, { hydrated: true });
        }

        try {
          this._applyProPanelChrome(existing);
          this._applyCompactTitlebar(header, { titleEl, hintEl, closeBtn });
          this._applyKeyboardBodyChrome(body);
          this._applyKeyboardHostChrome(keyboardContainer);
        } catch { /* ignore */ }

        if (keyboardContainer) {
          this.root = existing;
          this.keyboardContainer = keyboardContainer;
          this.closeBtn = closeBtn || null;
          this.hintEl = hintEl || null;
          this._titlebar = header || null;
          if (this.closeBtn) {
            try {
              this.closeBtn.removeEventListener('click', this._onCloseClick);
            } catch { /* ignore */ }
            this.closeBtn.addEventListener('click', this._onCloseClick);
          }
          this._bindWindowChrome();
          return;
        }
      }
    } catch { /* ignore */ }

    const root = document.createElement('div');
    root.className = 'kp-floating-keyboard-help';
    root.hidden = true;
    root.setAttribute('role', 'dialog');
    root.setAttribute('aria-label', 'KeyPilot keyboard reference');

    this._applyProPanelChrome(root);

    const header = document.createElement('div');
    header.setAttribute('data-kp-floating-keyboard-titlebar', 'true');

    const title = document.createElement('div');
    title.textContent = 'Keyboard Reference';
    title.setAttribute('data-kp-floating-keyboard-title', 'true');

    const hint = document.createElement('div');
    hint.setAttribute('data-kp-floating-keyboard-hint', 'true');
    this._setToggleHint(hint, 'K');

    const closeBtn = document.createElement('button');
    closeBtn.type = 'button';
    closeBtn.textContent = '×';
    closeBtn.setAttribute('aria-label', 'Close keyboard reference');
    closeBtn.setAttribute('data-kp-floating-keyboard-close', 'true');
    closeBtn.addEventListener('click', this._onCloseClick);

    header.appendChild(title);
    header.appendChild(hint);
    header.appendChild(closeBtn);
    this._applyCompactTitlebar(header, { titleEl: title, hintEl: hint, closeBtn });

    const body = document.createElement('div');
    body.setAttribute('data-kp-floating-keyboard-body', 'true');
    this._applyKeyboardBodyChrome(body);

    const keyboardContainer = document.createElement('div');
    keyboardContainer.className = 'kp-floating-keyboard-help__keyboard';
    this._applyKeyboardHostChrome(keyboardContainer);
    body.appendChild(keyboardContainer);

    root.appendChild(header);
    root.appendChild(body);

    // Attach to DOM.
    (document.body || document.documentElement).appendChild(root);

    this.root = root;
    this.keyboardContainer = keyboardContainer;
    this.closeBtn = closeBtn;
    this.hintEl = hint;
    this._titlebar = header;
    this._bindWindowChrome();
  }

  /**
   * Titlebar hint: "Press <kbd>K</kbd> to toggle" (key label is layout-aware).
   * @param {HTMLElement|null} hintEl
   * @param {string} keyLabel
   */
  _setToggleHint(hintEl, keyLabel) {
    if (!hintEl) return;
    const key = String(keyLabel || 'K').trim() || 'K';
    // Rebuild so we don't leave stale key labels after layout switches.
    while (hintEl.firstChild) hintEl.removeChild(hintEl.firstChild);

    hintEl.appendChild(document.createTextNode('Press '));

    const kbd = document.createElement('kbd');
    kbd.setAttribute('data-kp-floating-keyboard-hint-key', 'true');
    kbd.textContent = key;
    Object.assign(kbd.style, {
      display: 'inline-block',
      fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
      fontSize: '10px',
      fontWeight: '600',
      lineHeight: '1.2',
      padding: '1px 5px',
      border: '1px solid rgba(255, 255, 255, 0.16)',
      borderBottomColor: 'rgba(0, 0, 0, 0.55)',
      borderRadius: '4px',
      background: 'linear-gradient(180deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.04) 100%)',
      color: 'rgba(230, 232, 238, 0.95)',
      boxShadow: '0 1px 0 rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.06)',
      verticalAlign: 'middle'
    });
    hintEl.appendChild(kbd);

    hintEl.appendChild(document.createTextNode(' to toggle'));
    try {
      hintEl.setAttribute('aria-label', `Press ${key} to toggle`);
    } catch { /* ignore */ }
  }

  _render() {
    if (!this.keyboardContainer) return;
    try {
      const b = this.keybindings && this.keybindings.TOGGLE_KEYBOARD_HELP;
      const key = (b && (b.displayKey || b.keyLabel)) ? String(b.displayKey || b.keyLabel) : 'K';
      this._setToggleHint(this.hintEl, key);
    } catch { /* ignore */ }
    try {
      renderKeybindingsKeyboard({
        container: this.keyboardContainer,
        keybindings: this.keybindings,
        keyboardLayout: this.keyboardLayout || undefined,
        layoutId: this.layoutId || undefined
      });
      this._rebuildKeyIndex();
    } catch (e) {
      // In case a page CSP / DOM edge case breaks rendering, fail gracefully.
      this.keyboardContainer.textContent = 'Unable to render keyboard reference on this page.';
      console.warn('[KeyPilot] Failed to render floating keyboard reference:', e);
    }
  }

  /**
   * Public entry points used by KeyPilot's capture-phase handler.
   * KeyPilot calls stopImmediatePropagation() on claimed shortcuts, which would
   * otherwise prevent document-level listeners registered later from seeing keydown.
   * @param {KeyboardEvent} e
   */
  reflectKeyDown(e) {
    this._onDocKeyDown(e);
  }

  /**
   * @param {KeyboardEvent} e
   */
  reflectKeyUp(e) {
    this._onDocKeyUp(e);
  }

  _bindKeydownFeedback() {
    if (this._keydownBound) return;
    try {
      // Capture listeners cover keys KeyPilot does not claim. Claimed shortcuts are
      // reflected via reflectKeyDown() from KeyPilot before stopImmediatePropagation.
      document.addEventListener('keydown', this._onDocKeyDown, true);
      document.addEventListener('keyup', this._onDocKeyUp, true);
      window.addEventListener('blur', this._onWinBlur, true);
      this._keydownBound = true;
    } catch { /* ignore */ }
  }

  _unbindKeydownFeedback() {
    if (!this._keydownBound) return;
    try { document.removeEventListener('keydown', this._onDocKeyDown, true); } catch { /* ignore */ }
    try { document.removeEventListener('keyup', this._onDocKeyUp, true); } catch { /* ignore */ }
    try { window.removeEventListener('blur', this._onWinBlur, true); } catch { /* ignore */ }
    this._keydownBound = false;
    this._clearPressed();
  }

  _bindSettingsSync() {
    if (this._settingsBound) return;
    try {
      if (chrome?.storage?.onChanged?.addListener) {
        chrome.storage.onChanged.addListener(this._onStorageChanged);
        this._settingsBound = true;
      }
    } catch { /* ignore */ }
  }

  _unbindSettingsSync() {
    if (!this._settingsBound) return;
    try { chrome?.storage?.onChanged?.removeListener?.(this._onStorageChanged); } catch { /* ignore */ }
    this._settingsBound = false;
  }

  async _refreshKeyFeedbackSetting() {
    try {
      const settings = await getSettings();
      this._setKeyFeedbackEnabled(!!settings.keyboardReferenceKeyFeedback);
    } catch {
      // ignore (keep default)
    }
  }

  _onStorageChanged(changes, area) {
    try {
      if (area !== 'sync' && area !== 'local') return;
      const entry = changes && changes[SETTINGS_STORAGE_KEY];
      if (!entry || !entry.newValue) return;
      this._setKeyFeedbackEnabled(!!entry.newValue.keyboardReferenceKeyFeedback);
      // Cross-tab / cross-page position sync (and re-apply after navigation restore).
      const nextPos = entry.newValue.panelPositions?.keyboardReference;
      if (nextPos && typeof nextPos === 'object') {
        this._suppressPositionPersist = true;
        this._setPanelPosition(nextPos, { persist: false });
        this._suppressPositionPersist = false;
      }
    } catch { /* ignore */ }
  }

  _setKeyFeedbackEnabled(enabled) {
    const next = !!enabled;
    if (this._keyFeedbackEnabled === next) return;
    this._keyFeedbackEnabled = next;
    if (!next) this._clearPressed();
  }

  _onWinBlur() {
    // If the page loses focus while keys are held, keyup may never arrive.
    this._clearPressed();
  }

  _normalizeLabel(s) {
    return String(s || '').trim().toUpperCase();
  }

  /**
   * Normalize an event/binding key token into the label(s) used on the keyboard UI.
   * @param {string} token
   * @returns {string[]}
   */
  _labelsFromToken(token) {
    const raw = String(token || '').trim();
    if (!raw || raw === ' ') return [];

    const upper = this._normalizeLabel(raw);
    if (!upper) return [];

    // Match the UI's special key text / common KeyboardEvent.key values.
    if (upper === 'CAPSLOCK' || upper === 'CAPS') return ['CAPS'];
    if (upper === 'ESCAPE' || upper === 'ESC') return ['ESC']; // may be absent from mini layout
    if (upper === 'CONTROL' || upper === 'CTRL' || upper === 'ALT' || upper === 'META' || upper === 'OS') {
      return []; // not shown on the mini keyboard
    }
    if (upper === 'SHIFT') return ['SHIFT'];
    if (upper === 'ENTER' || upper === 'RETURN') return ['ENTER'];
    if (upper === 'TAB') return ['TAB'];
    if (upper === 'BACKSPACE') return ['BACKSPACE'];
    if (upper === 'SEMICOLON') return [';'];
    if (upper === 'QUOTE') return ["'"];
    if (upper === 'BACKQUOTE' || upper === 'BACKTICK') return ['`'];
    if (upper === 'BRACKETLEFT') return ['['];
    if (upper === 'BRACKETRIGHT') return [']'];
    if (upper === 'COMMA') return [','];
    if (upper === 'PERIOD') return ['.'];
    if (upper === 'SLASH') return ['/'];
    if (upper === 'MINUS') return ['-'];
    if (upper === 'EQUAL') return ['='];
    if (upper === 'BACKSLASH') return ['\\'];

    // Punctuation: map shifted glyphs back to the base key label shown on the keyboard UI.
    if (upper === ':') return [';'];
    if (upper === '?') return ['/'];
    if (upper === '>') return ['.'];
    if (upper === '<') return [','];
    if (upper === '"') return ["'"];
    if (upper === '~') return ['`'];
    if (upper === '{') return ['['];
    if (upper === '}') return [']'];
    if (upper === '_') return ['-'];
    if (upper === '+') return ['='];
    if (upper === '|') return ['\\'];
    if (upper === '!') return ['1'];
    if (upper === '@') return ['2'];
    if (upper === '#') return ['3'];
    if (upper === '$') return ['4'];
    if (upper === '%') return ['5'];
    if (upper === '^') return ['6'];
    if (upper === '&') return ['7'];
    if (upper === '*') return ['8'];
    if (upper === '(') return ['9'];
    if (upper === ')') return ['0'];

    return [upper];
  }

  _labelsFromKeyboardEvent(e) {
    // Prefer semantic key names so this works across keyboard layouts; also use
    // KeyboardEvent.code so physical keys still light when key is a shifted glyph.
    const out = [];
    const seen = new Set();
    const pushAll = (tokens) => {
      for (const t of tokens || []) {
        if (!t || seen.has(t)) continue;
        seen.add(t);
        out.push(t);
      }
    };

    const key = e && typeof e.key === 'string' ? e.key : '';
    if (key) pushAll(this._labelsFromToken(key));

    const code = e && typeof e.code === 'string' ? e.code : '';
    if (code) {
      // KeyA → A, Digit1 → 1
      if (/^Key[A-Z]$/i.test(code)) pushAll([code.slice(3).toUpperCase()]);
      else if (/^Digit[0-9]$/.test(code)) pushAll([code.slice(5)]);
      else pushAll(this._labelsFromToken(code));
    }

    return out;
  }

  /**
   * @param {Map<string, HTMLElement[]>} map
   * @param {string} label
   * @param {HTMLElement} keyEl
   */
  _indexLabel(map, label, keyEl) {
    const tokens = [];
    const norm = this._normalizeLabel(label);
    if (norm) tokens.push(norm);

    // Composite display keys (e.g. "A/`", "F / G") → also index each part.
    if (norm && /[/|,]/.test(norm)) {
      for (const part of norm.split(/[/|,]+/)) {
        const p = this._normalizeLabel(part);
        if (p) tokens.push(p);
      }
    }

    // Expand each token through the same alias map used for events.
    const expanded = new Set();
    for (const t of tokens) {
      for (const alias of this._labelsFromToken(t)) {
        if (alias) expanded.add(alias);
      }
      if (t) expanded.add(t);
    }

    for (const token of expanded) {
      const arr = map.get(token) || [];
      if (!arr.includes(keyEl)) arr.push(keyEl);
      map.set(token, arr);
    }
  }

  _rebuildKeyIndex() {
    if (!this.keyboardContainer) return;
    const map = new Map();
    const byAction = new Map();
    const bindings = this.keybindings || {};

    // Index by the visible "key label":
    // - action keys use `.key-label` (e.g. Q/W/E...)
    // - plain keys and specials use `.key-text` (e.g. Y, Tab, Caps, Shift)
    // - also index binding.keys so event.key / event.code always resolve
    const keyEls = this.keyboardContainer.querySelectorAll('.key');
    for (const keyEl of keyEls) {
      const labelEl = keyEl.querySelector?.('.key-label');
      const textEl = keyEl.querySelector?.('.key-text');
      if (labelEl && labelEl.textContent) {
        this._indexLabel(map, labelEl.textContent, keyEl);
      }
      if (textEl && textEl.textContent) {
        this._indexLabel(map, textEl.textContent, keyEl);
      }
      if (!labelEl && !textEl) {
        this._indexLabel(map, keyEl.textContent, keyEl);
      }

      // Also index by action id for link-hover hints (ACTIVATE, OPEN_POPOVER, …).
      const actionId = keyEl.dataset?.kpActionId ? String(keyEl.dataset.kpActionId) : '';
      if (actionId) {
        const arr = byAction.get(actionId) || [];
        if (!arr.includes(keyEl)) arr.push(keyEl);
        byAction.set(actionId, arr);

        const binding = bindings[actionId];
        const keys = binding && Array.isArray(binding.keys) ? binding.keys : [];
        for (const k of keys) {
          this._indexLabel(map, k, keyEl);
        }
        if (binding?.displayKey) this._indexLabel(map, binding.displayKey, keyEl);
        if (binding?.keyLabel) this._indexLabel(map, binding.keyLabel, keyEl);
      }
    }

    this._keyElsByLabel = map;
    this._keyElsByActionId = byAction;

    // If we re-rendered while keys were held, re-apply pressed overlay.
    for (const label of this._pressedLabels) {
      const els = this._keyElsByLabel.get(label);
      if (!els) continue;
      for (const el of els) setKeyPressedState(el, true);
    }

    // Re-apply link-hover hints after re-render.
    if (this._linkHoverHintActive) {
      this._applyLinkHoverHintClasses(true);
    }

    // Re-apply text-mode filter after re-render.
    if (this._textModeFilterActive) {
      this._applyTextModeFilterClasses(true);
    }
  }

  /**
   * While a text field has focus, gray out every key.
   * Click Element (ACTIVATE) is re-enabled only while the hover countdown is armed
   * via setTextModeActivateArmed(true).
   * @param {boolean} active
   */
  setTextModeFilter(active) {
    const next = Boolean(active);
    if (!next) {
      this._textModeActivateArmed = false;
    }
    if (this._textModeFilterActive === next) {
      // Still re-apply if DOM was rebuilt while state was already true.
      if (next && this.isVisible()) this._applyTextModeFilterClasses(true);
      return;
    }

    this._applyTextModeFilterClasses(false);
    this._textModeFilterActive = next;
    if (!next) this._textModeActivateArmed = false;

    try {
      const kbRoot = this.root?.querySelector?.('.kp-keybindings-ui') || this.keyboardContainer;
      if (kbRoot) {
        if (next) kbRoot.classList.add('kp-text-mode-filter');
        else kbRoot.classList.remove('kp-text-mode-filter');
      }
    } catch { /* ignore */ }

    if (next && this.isVisible()) {
      this._applyTextModeFilterClasses(true);
    }
  }

  /**
   * During text mode, light up Click Element only while a clickable is under the
   * cursor and the hover-click countdown is running.
   * @param {boolean} armed
   */
  setTextModeActivateArmed(armed) {
    const next = Boolean(armed);
    if (!this._textModeFilterActive) {
      this._textModeActivateArmed = false;
      return;
    }
    if (this._textModeActivateArmed === next) {
      if (next && this.isVisible()) this._applyTextModeFilterClasses(true);
      return;
    }
    this._textModeActivateArmed = next;
    if (this.isVisible()) this._applyTextModeFilterClasses(true);
  }

  /**
   * @param {boolean} on
   */
  _applyTextModeFilterClasses(on) {
    try {
      const root = this.root;
      if (!root) return;
      const activateArmed = !!(on && this._textModeActivateArmed);
      const keys = root.querySelectorAll('.key');
      for (const el of keys) {
        if (!el) continue;
        el.classList.remove('kp-key-text-mode-disabled');
        el.classList.remove('kp-key-text-mode-active');
        if (!on) continue;
        const actionId = el.getAttribute('data-kp-action-id') || '';
        if (actionId === 'ACTIVATE' && activateArmed) {
          // Hover countdown armed: Click Element is the only live key.
          el.classList.add('kp-key-text-mode-active');
        } else {
          // Default text-mode look: everything grayed out, including ACTIVATE.
          el.classList.add('kp-key-text-mode-disabled');
        }
      }

      const kbRoot = root.querySelector?.('.kp-keybindings-ui') || this.keyboardContainer;
      if (kbRoot) {
        if (on) kbRoot.classList.add('kp-text-mode-filter');
        else kbRoot.classList.remove('kp-text-mode-filter');
      }
    } catch { /* ignore */ }
  }

  /**
   * Highlight keyboard keys that activate/open a hovered page link.
   * @param {boolean} active
   * @param {string[]} [actionIds] defaults to click + popover link actions
   */
  setLinkHoverHints(active, actionIds) {
    const next = Boolean(active);
    const ids = Array.isArray(actionIds) && actionIds.length
      ? actionIds.map(String)
      : ['ACTIVATE', 'OPEN_POPOVER', 'PREVIEW_LINK_POPOVER', 'ACTIVATE_NEW_TAB', 'ACTIVATE_NEW_TAB_BACKGROUND'];

    // Clear previous classes first.
    this._applyLinkHoverHintClasses(false);

    this._linkHoverHintActive = next;
    this._linkHoverHintActionIds = new Set(next ? ids : []);

    if (next && this.isVisible()) {
      this._applyLinkHoverHintClasses(true);
    }
  }

  /**
   * @param {boolean} on
   */
  _applyLinkHoverHintClasses(on) {
    try {
      const ids = this._linkHoverHintActionIds;
      if (!ids || ids.size === 0) return;
      for (const actionId of ids) {
        const els = this._keyElsByActionId.get(actionId);
        if (!els) continue;
        for (const el of els) {
          if (on) el.classList.add('kp-key-link-hint');
          else el.classList.remove('kp-key-link-hint');
        }
      }
    } catch { /* ignore */ }
  }

  _setPressed(label, pressed) {
    const norm = this._normalizeLabel(label);
    if (!norm) return;
    const els = this._keyElsByLabel.get(norm);
    if (!els) return;
    for (const el of els) {
      setKeyPressedState(el, pressed);
    }
  }

  _clearPressed() {
    for (const label of this._pressedLabels) {
      this._setPressed(label, false);
    }
    this._pressedLabels.clear();
  }

  _onDocKeyDown(e) {
    try {
      if (!this.isVisible()) return;
      if (!this._keyFeedbackEnabled) return;
      const labels = this._labelsFromKeyboardEvent(e);
      if (!labels || labels.length === 0) return;
      for (const label of labels) {
        if (this._pressedLabels.has(label)) continue;
        this._pressedLabels.add(label);
        this._setPressed(label, true);
      }
    } catch { /* ignore */ }
  }

  _onDocKeyUp(e) {
    try {
      if (!this.isVisible()) return;
      if (!this._keyFeedbackEnabled) return;
      const labels = this._labelsFromKeyboardEvent(e);
      if (!labels || labels.length === 0) return;
      for (const label of labels) {
        this._pressedLabels.delete(label);
        this._setPressed(label, false);
      }
    } catch { /* ignore */ }
  }

  _onCloseClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    // Closing the panel should behave like pressing "K":
    // it must update KeyPilot's persisted visibility state, not only hide the DOM.
    try {
      const kp = window?.__KeyPilotInstance;
      if (kp && typeof kp.applyKeyboardHelpVisibility === 'function') {
        kp.applyKeyboardHelpVisibility(false, { persist: true });
        return;
      }
    } catch { /* ignore */ }

    // Fallback: still hide if KeyPilot isn't available for some reason.
    this.hide();
  }
}

// Debug: Make sure class is available globally for bundled version
if (typeof window !== 'undefined') {
  window.FloatingKeyboardHelp = FloatingKeyboardHelp;
}





  // Module: src/ui/onboarding-shared.js
/**
 * Shared onboarding walkthrough primitives.
 *
 * Zero imports so this file can be:
 * - imported by ESM modules (panel / manager)
 * - stamped into early-inject.js by build.js (export keywords stripped)
 *
 * Keep DOM construction and progress shape here so early-inject and the
 * bundled content script cannot drift.
 */

// ── Storage / progress ──────────────────────────────────────────────────────

const ONBOARDING_STORAGE_KEYS = {
  ACTIVE: 'keypilot_onboarding_active',
  PROGRESS: 'keypilot_onboarding_progress'
};

const ONBOARDING_FIRST_SLIDE_ID = 'basic_navigation';
const ONBOARDING_PANEL_CLASS = 'kp-onboarding-panel';
const ONBOARDING_DEFAULT_TITLE = 'Welcome to KeyPilot';
const ONBOARDING_REOPEN_TIP = 'Tip: Press Alt + / to re-open this walkthrough later.';

/** Default z-index fallback if caller does not pass Z_INDEX.ONBOARDING_PANEL. */
const ONBOARDING_PANEL_Z_FALLBACK = 2147483026;

/** Layout: control strip stays at top; walkthrough sits just below it. */
const ONBOARDING_DEFAULT_LEFT_PX = 16;
const ONBOARDING_DEFAULT_TOP_PX = 16;
/** Prefixed names avoid clashing with early-inject locals when this file is stamped. */
const ONBOARDING_STRIP_TOP_PX = 16;
const ONBOARDING_STRIP_HEIGHT_PX = 28;
const ONBOARDING_BELOW_STRIP_GAP_PX = 8;

/**
 * Preferred top offset for the walkthrough so the control strip stays visible above it.
 * @returns {number}
 */
function getOnboardingTopBelowControlStrip() {
  try {
    const strip = document.querySelector('.kp-control-strip');
    if (strip && strip.isConnected) {
      const cs = typeof window.getComputedStyle === 'function' ? window.getComputedStyle(strip) : null;
      const hidden =
        strip.hidden === true ||
        strip.getAttribute('hidden') !== null ||
        (strip.style && strip.style.display === 'none') ||
        (cs && (cs.display === 'none' || cs.visibility === 'hidden'));
      if (!hidden) {
        const rect = strip.getBoundingClientRect();
        if (rect && rect.height > 0) {
          return Math.max(
            ONBOARDING_DEFAULT_TOP_PX,
            Math.round(rect.bottom + ONBOARDING_BELOW_STRIP_GAP_PX)
          );
        }
      }
    }
  } catch { /* ignore */ }
  // Fallback assuming strip is at its default top-left perch.
  return ONBOARDING_STRIP_TOP_PX + ONBOARDING_STRIP_HEIGHT_PX + ONBOARDING_BELOW_STRIP_GAP_PX;
}

/**
 * @param {HTMLElement|null} root
 */
function positionOnboardingBelowControlStrip(root) {
  if (!root) return;
  try {
    root.style.top = `${getOnboardingTopBelowControlStrip()}px`;
  } catch { /* ignore */ }
}

/**
 * @typedef {Object} OnboardingProgress
 * @property {string|null} slideId
 * @property {string[]} completedTaskIds
 * @property {string[]} onEnterDoneSlideIds
 * @property {boolean} completed
 * @property {number} timestamp
 */

/**
 * @param {string|null} [slideId]
 * @returns {OnboardingProgress}
 */
function createEmptyProgress(slideId = null) {
  return {
    slideId: slideId || null,
    completedTaskIds: [],
    onEnterDoneSlideIds: [],
    completed: false,
    timestamp: Date.now()
  };
}

/**
 * @param {any} progress
 * @returns {OnboardingProgress}
 */
function cloneProgress(progress) {
  const p = progress && typeof progress === 'object' ? progress : {};
  return {
    slideId: typeof p.slideId === 'string' ? p.slideId : null,
    completedTaskIds: Array.isArray(p.completedTaskIds) ? p.completedTaskIds.map(String) : [],
    onEnterDoneSlideIds: Array.isArray(p.onEnterDoneSlideIds) ? p.onEnterDoneSlideIds.map(String) : [],
    completed: !!p.completed,
    timestamp: typeof p.timestamp === 'number' ? p.timestamp : Date.now()
  };
}

/**
 * @param {string[]} a
 * @param {string[]} b
 */
function arraysEqualString(a, b) {
  const aa = Array.isArray(a) ? a : [];
  const bb = Array.isArray(b) ? b : [];
  if (aa.length !== bb.length) return false;
  for (let i = 0; i < aa.length; i++) {
    if (String(aa[i]) !== String(bb[i])) return false;
  }
  return true;
}

/**
 * @param {OnboardingProgress|any} a
 * @param {OnboardingProgress|any} b
 */
function progressEqual(a, b) {
  const aa = cloneProgress(a);
  const bb = cloneProgress(b);
  return (
    aa.slideId === bb.slideId &&
    aa.completed === bb.completed &&
    aa.timestamp === bb.timestamp &&
    arraysEqualString(aa.completedTaskIds, bb.completedTaskIds) &&
    arraysEqualString(aa.onEnterDoneSlideIds, bb.onEnterDoneSlideIds)
  );
}

/**
 * @param {{tasks?: Array<{id?: string}>}|null|undefined} slide
 * @param {Set<string>|string[]} completedTaskIds
 */
function isSlideComplete(slide, completedTaskIds) {
  const tasks = slide?.tasks || [];
  if (!tasks.length) return true;
  const set = completedTaskIds instanceof Set
    ? completedTaskIds
    : new Set(Array.isArray(completedTaskIds) ? completedTaskIds.map(String) : []);
  for (const t of tasks) {
    if (!t?.id) continue;
    if (!set.has(String(t.id))) return false;
  }
  return true;
}

// ── Text helpers ────────────────────────────────────────────────────────────

/**
 * Convert backtick-wrapped segments into <kbd> nodes (no innerHTML).
 * Safer on hostile pages than parsing HTML strings.
 * @param {ParentNode} el
 * @param {string} text
 */
function renderKeyboardKeysInto(el, text) {
  if (!el) return;
  try { el.textContent = ''; } catch { /* ignore */ }
  const parts = String(text || '').split(/`/);
  for (let i = 0; i < parts.length; i++) {
    const part = parts[i];
    if (!part) continue;
    if (i % 2 === 1) {
      const k = document.createElement('kbd');
      k.textContent = part;
      el.appendChild(k);
    } else {
      el.appendChild(document.createTextNode(part));
    }
  }
}

/**
 * @param {string} text
 * @returns {string} HTML with <kbd> (for callers that intentionally use innerHTML)
 */
function formatKeyboardKeysHtml(text) {
  return String(text || '').replace(/`([^`]+)`/g, '<kbd>$1</kbd>');
}

// ── CSS ─────────────────────────────────────────────────────────────────────

/**
 * @param {{includeViewTransitions?: boolean}} [opts]
 * @returns {string}
 */
function getOnboardingPanelCss(opts = {}) {
  const includeVt = opts.includeViewTransitions !== false;
  let css =
    `.${ONBOARDING_PANEL_CLASS} kbd {
        font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
        font-size: 11px;
        padding: 1px 6px;
        border: 1px solid #3a3a3a;
        border-bottom-color: #2a2a2a;
        border-radius: 4px;
        background: linear-gradient(180deg, #2b2b2b 0%, #1a1a1a 100%);
        color: #f1f1f1;
      }`;
  if (includeVt) {
    css += `
      @keyframes kpOnboardingSlideOut {
        to { transform: translateX(calc(var(--kp-onboarding-slide-dir, 1) * -100%)); opacity: 0.0; }
      }
      @keyframes kpOnboardingSlideIn {
        from { transform: translateX(calc(var(--kp-onboarding-slide-dir, 1) * 100%)); opacity: 0.0; }
        to { transform: translateX(0%); opacity: 1.0; }
      }
      ::view-transition-old(kp-onboarding-slide-surface) {
        animation: kpOnboardingSlideOut 220ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
      }
      ::view-transition-new(kp-onboarding-slide-surface) {
        animation: kpOnboardingSlideIn 220ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
      }`;
  }
  return css;
}

// ── Shell DOM ───────────────────────────────────────────────────────────────

function assignStyle(el, styles) {
  if (!el || !styles) return;
  try { Object.assign(el.style, styles); } catch { /* ignore */ }
}

function mkIconBtn(doc, label, dataAttr, aria) {
  const b = doc.createElement('button');
  b.type = 'button';
  b.textContent = label;
  b.setAttribute(dataAttr, 'true');
  if (aria) b.setAttribute('aria-label', aria);
  assignStyle(b, {
    width: '28px',
    height: '28px',
    borderRadius: '10px',
    border: '1px solid rgba(255,255,255,0.18)',
    background: 'rgba(255,255,255,0.06)',
    color: 'rgba(255,255,255,0.95)',
    cursor: 'pointer',
    fontSize: '14px',
    lineHeight: '26px',
    padding: '0',
    display: 'inline-flex',
    alignItems: 'center',
    justifyContent: 'center'
  });
  return b;
}

/**
 * Build the floating walkthrough panel shell (header / body / footer).
 * Does not attach event listeners — callers wire close/prev/next/reset.
 *
 * @param {Document} doc
 * @param {Object} [opts]
 * @param {number|string} [opts.zIndex]
 * @param {boolean} [opts.early] Mark as early-inject shell for adoption
 * @param {boolean} [opts.initiallyHidden]
 * @param {boolean} [opts.includeViewTransitions]
 * @param {(el: HTMLElement) => void} [opts.applyTheme]
 * @param {string} [opts.title]
 * @param {string} [opts.stepText]
 * @param {boolean} [opts.navDisabled] Disable prev/next (early shell until wired)
 * @returns {{
 *   root: HTMLElement,
 *   body: HTMLElement,
 *   slideSurface: HTMLElement,
 *   titleEl: HTMLElement,
 *   stepEl: HTMLElement,
 *   prevBtn: HTMLButtonElement,
 *   nextBtn: HTMLButtonElement,
 *   resetBtn: HTMLButtonElement,
 *   closeBtn: HTMLButtonElement
 * }}
 */
function createOnboardingShell(doc, opts = {}) {
  const zIndex = opts.zIndex != null ? opts.zIndex : ONBOARDING_PANEL_Z_FALLBACK;
  const initiallyHidden = opts.initiallyHidden !== false;
  const includeVt = opts.includeViewTransitions !== false;

  const root = doc.createElement('div');
  root.className = ONBOARDING_PANEL_CLASS;
  root.hidden = initiallyHidden;
  if (opts.early) root.dataset.kpEarlyOnboarding = 'true';
  root.setAttribute('role', 'dialog');
  root.setAttribute('aria-label', 'KeyPilot onboarding walkthrough');

  // When initially hidden, use display:none + pointer-events:none.
  // Some pages override [hidden]; never put display:flex on a hidden shell.
  assignStyle(root, {
    position: 'fixed',
    left: `${ONBOARDING_DEFAULT_LEFT_PX}px`,
    // Prefer sitting below the control strip so the strip is not pushed down.
    top: `${getOnboardingTopBelowControlStrip()}px`,
    width: '360px',
    maxWidth: 'calc(100vw - 24px)',
    maxHeight: 'calc(100vh - 24px)',
    display: initiallyHidden ? 'none' : 'flex',
    flexDirection: 'column',
    overflow: 'hidden',
    zIndex: String(zIndex),
    background: 'rgba(18, 18, 18, 0.94)',
    color: 'rgba(255,255,255,0.95)',
    border: '1px solid rgba(255,255,255,0.12)',
    borderRadius: '14px',
    boxShadow: '0 12px 34px rgba(0,0,0,0.45)',
    fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif',
    pointerEvents: initiallyHidden ? 'none' : 'auto'
  });

  try { opts.applyTheme?.(root); } catch { /* ignore */ }

  const style = doc.createElement('style');
  style.textContent = getOnboardingPanelCss({ includeViewTransitions: includeVt });
  root.appendChild(style);

  const header = doc.createElement('div');
  assignStyle(header, {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'space-between',
    gap: '10px',
    padding: '10px 12px',
    borderBottom: '1px solid rgba(255,255,255,0.10)'
  });

  const titleWrap = doc.createElement('div');
  assignStyle(titleWrap, {
    display: 'flex',
    flexDirection: 'column',
    gap: '2px',
    minWidth: '0'
  });

  const titleEl = doc.createElement('div');
  titleEl.textContent = String(opts.title || ONBOARDING_DEFAULT_TITLE);
  titleEl.setAttribute('data-kp-onboarding-title', 'true');
  assignStyle(titleEl, {
    fontSize: '13px',
    fontWeight: '800',
    letterSpacing: '0.2px',
    whiteSpace: 'nowrap',
    overflow: 'hidden',
    textOverflow: 'ellipsis'
  });

  const stepEl = doc.createElement('div');
  stepEl.textContent = String(opts.stepText || '1 / 1');
  stepEl.setAttribute('data-kp-onboarding-step', 'true');
  assignStyle(stepEl, {
    fontSize: '12px',
    fontWeight: '600',
    opacity: '0.75'
  });

  // Title wrap only gets the title; step lives in the footer stepWrap
  // (appendChild moves nodes — keep a single step element).
  titleWrap.appendChild(titleEl);

  const navWrap = doc.createElement('div');
  assignStyle(navWrap, {
    display: 'inline-flex',
    alignItems: 'center',
    gap: '8px',
    flex: '0 0 auto'
  });

  const stepWrap = doc.createElement('div');
  assignStyle(stepWrap, {
    display: 'inline-flex',
    alignItems: 'center',
    gap: '6px'
  });

  const prevBtn = mkIconBtn(doc, '←', 'data-kp-onboarding-prev', 'Previous slide');
  const nextBtn = mkIconBtn(doc, '→', 'data-kp-onboarding-next', 'Next slide');
  if (opts.navDisabled) {
    prevBtn.disabled = true;
    nextBtn.disabled = true;
  }

  stepWrap.appendChild(prevBtn);
  stepWrap.appendChild(stepEl);
  stepWrap.appendChild(nextBtn);

  const resetBtn = doc.createElement('button');
  resetBtn.type = 'button';
  resetBtn.textContent = 'Reset';
  resetBtn.setAttribute('data-kp-onboarding-reset', 'true');
  assignStyle(resetBtn, {
    height: '28px',
    borderRadius: '999px',
    border: '1px solid rgba(255,255,255,0.18)',
    background: 'rgba(255,255,255,0.06)',
    color: 'rgba(255,255,255,0.92)',
    cursor: 'pointer',
    fontSize: '12px',
    fontWeight: '700',
    padding: '0 10px',
    lineHeight: '26px'
  });

  const closeBtn = doc.createElement('button');
  closeBtn.type = 'button';
  closeBtn.textContent = '×';
  closeBtn.setAttribute('aria-label', 'Close onboarding walkthrough');
  closeBtn.setAttribute('data-kp-onboarding-close', 'true');
  assignStyle(closeBtn, {
    width: '30px',
    height: '30px',
    borderRadius: '10px',
    border: '1px solid rgba(255,255,255,0.18)',
    background: 'rgba(255,255,255,0.06)',
    color: 'rgba(255,255,255,0.95)',
    cursor: 'pointer',
    fontSize: '18px',
    lineHeight: '28px',
    padding: '0',
    flex: '0 0 auto'
  });

  navWrap.appendChild(closeBtn);
  header.appendChild(titleWrap);
  header.appendChild(navWrap);

  const body = doc.createElement('div');
  body.setAttribute('data-kp-onboarding-body', 'true');
  assignStyle(body, {
    flex: '1',
    overflowY: 'auto',
    minHeight: '0',
    position: 'relative'
  });

  const slideSurface = doc.createElement('div');
  slideSurface.setAttribute('data-kp-onboarding-slide-surface', 'true');
  assignStyle(slideSurface, { padding: '12px' });
  body.appendChild(slideSurface);

  const footer = doc.createElement('div');
  assignStyle(footer, {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'space-between',
    gap: '10px',
    padding: '10px 12px',
    borderTop: '1px solid rgba(255,255,255,0.10)'
  });
  footer.appendChild(stepWrap);
  footer.appendChild(resetBtn);

  root.appendChild(header);
  root.appendChild(body);
  root.appendChild(footer);

  return {
    root,
    body,
    slideSurface,
    titleEl,
    stepEl,
    prevBtn,
    nextBtn,
    resetBtn,
    closeBtn
  };
}

/**
 * Query shell refs from an existing panel root (early-inject adoption).
 * @param {Element|null} root
 */
function queryOnboardingShellRefs(root) {
  if (!root) return null;
  const body =
    root.querySelector('[data-kp-onboarding-body="true"]') ||
    root.querySelector(':scope > div[data-kp-onboarding-body]');
  if (!body) return null;
  let slideSurface = body.querySelector('[data-kp-onboarding-slide-surface="true"]');
  if (!slideSurface) {
    slideSurface = document.createElement('div');
    slideSurface.setAttribute('data-kp-onboarding-slide-surface', 'true');
    assignStyle(slideSurface, { padding: '12px' });
    try { body.appendChild(slideSurface); } catch { /* ignore */ }
  }
  return {
    root,
    body,
    slideSurface,
    titleEl: root.querySelector('[data-kp-onboarding-title="true"]'),
    stepEl: root.querySelector('[data-kp-onboarding-step="true"]'),
    prevBtn: root.querySelector('button[data-kp-onboarding-prev="true"]'),
    nextBtn: root.querySelector('button[data-kp-onboarding-next="true"]'),
    resetBtn: root.querySelector('button[data-kp-onboarding-reset="true"]'),
    closeBtn: root.querySelector('button[data-kp-onboarding-close="true"]')
  };
}

/**
 * Show/hide panel in a way that survives hostile [hidden] CSS overrides.
 * @param {HTMLElement|null} root
 * @param {boolean} visible
 */
function setOnboardingPanelVisible(root, visible) {
  if (!root) return;
  const show = !!visible;
  try {
    root.hidden = !show;
    root.style.display = show ? 'flex' : 'none';
    root.style.pointerEvents = show ? 'auto' : 'none';
    if (show) positionOnboardingBelowControlStrip(root);
  } catch { /* ignore */ }
}

// ── Slide content ───────────────────────────────────────────────────────────

function applyTaskRowInteractive(row, { uncheckable, onTaskRowClick }) {
  if (!row) return;
  try {
    if (uncheckable) {
      row.setAttribute('data-kp-onboarding-uncheckable', 'true');
      row.style.cursor = 'pointer';
      row.title = 'Click to uncheck (undo last completed step)';
      row.setAttribute('role', 'button');
      row.tabIndex = 0;
    } else {
      row.removeAttribute('data-kp-onboarding-uncheckable');
      row.style.cursor = '';
      row.removeAttribute('title');
      row.removeAttribute('role');
      row.removeAttribute('tabindex');
    }
  } catch { /* ignore */ }

  try {
    if (row._kpOnboardingTaskClick) {
      row.removeEventListener('click', row._kpOnboardingTaskClick);
      row._kpOnboardingTaskClick = null;
    }
  } catch { /* ignore */ }
  if (uncheckable && typeof onTaskRowClick === 'function') {
    row._kpOnboardingTaskClick = onTaskRowClick;
    try { row.addEventListener('click', onTaskRowClick); } catch { /* ignore */ }
  }
}

function applyTaskRowVisual(row, task, done, opts = {}) {
  if (!row) return;
  assignStyle(row, {
    background: done ? 'rgba(46, 204, 113, 0.10)' : 'rgba(255,255,255,0.04)'
  });

  const box =
    row.querySelector(':scope > div[aria-hidden="true"]') ||
    row.firstElementChild;
  const textEl =
    (box && box.nextElementSibling) ||
    row.querySelector(':scope > div:last-child');

  if (box) {
    assignStyle(box, {
      border: done ? '1px solid rgba(46, 204, 113, 0.9)' : '1px solid rgba(255,255,255,0.22)',
      background: done ? 'rgba(46, 204, 113, 0.85)' : 'transparent',
      boxShadow: done ? '0 0 0 2px rgba(46, 204, 113, 0.18)' : 'none'
    });
    try {
      const existingCheck = box.querySelector(':scope > div');
      if (done) {
        if (!existingCheck) {
          const check = document.createElement('div');
          check.textContent = '✓';
          assignStyle(check, {
            position: 'absolute',
            inset: '0',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '13px',
            fontWeight: '800',
            color: '#0b1410'
          });
          box.appendChild(check);
        }
      } else if (existingCheck) {
        box.removeChild(existingCheck);
      }
    } catch { /* ignore */ }
  }

  if (textEl) {
    try {
      renderKeyboardKeysInto(textEl, task.label || task.id);
      assignStyle(textEl, {
        color: done ? 'rgba(255,255,255,0.92)' : 'rgba(255,255,255,0.88)',
        opacity: done ? '0.95' : '1'
      });
    } catch { /* ignore */ }
  }

  applyTaskRowInteractive(row, {
    uncheckable: !!(done && opts.uncheckable),
    onTaskRowClick: opts.onTaskRowClick
  });
}

function createTaskRow(doc, task, done, opts = {}) {
  const row = doc.createElement('div');
  row.setAttribute('data-kp-onboarding-task-id', task.id);
  assignStyle(row, {
    display: 'flex',
    alignItems: 'flex-start',
    gap: '10px',
    padding: '8px 10px',
    borderRadius: '10px',
    border: '1px solid rgba(255,255,255,0.10)',
    background: done ? 'rgba(46, 204, 113, 0.10)' : 'rgba(255,255,255,0.04)'
  });

  const box = doc.createElement('div');
  box.setAttribute('aria-hidden', 'true');
  assignStyle(box, {
    width: '18px',
    height: '18px',
    borderRadius: '6px',
    border: done ? '1px solid rgba(46, 204, 113, 0.9)' : '1px solid rgba(255,255,255,0.22)',
    background: done ? 'rgba(46, 204, 113, 0.85)' : 'transparent',
    boxShadow: done ? '0 0 0 2px rgba(46, 204, 113, 0.18)' : 'none',
    flex: '0 0 auto',
    marginTop: '1px',
    position: 'relative'
  });

  if (done) {
    const check = doc.createElement('div');
    check.textContent = '✓';
    assignStyle(check, {
      position: 'absolute',
      inset: '0',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
      fontSize: '13px',
      fontWeight: '800',
      color: '#0b1410'
    });
    box.appendChild(check);
  }

  const text = doc.createElement('div');
  renderKeyboardKeysInto(text, task.label || task.id);
  assignStyle(text, {
    fontSize: '13px',
    lineHeight: '1.35',
    color: done ? 'rgba(255,255,255,0.92)' : 'rgba(255,255,255,0.88)',
    opacity: done ? '0.95' : '1'
  });

  row.appendChild(box);
  row.appendChild(text);
  applyTaskRowInteractive(row, {
    uncheckable: !!(done && opts.uncheckable),
    onTaskRowClick: opts.onTaskRowClick
  });
  return row;
}

/**
 * Render or update slide checklist into the slide surface.
 *
 * @param {HTMLElement} surface
 * @param {Object} params
 * @param {Array<{id:string, label?:string}>} [params.tasks]
 * @param {Set<string>|string[]} [params.completedTaskIds]
 * @param {string|null} [params.lastCompletedTaskId]
 * @param {(e:Event)=>void} [params.onTaskRowClick]
 * @param {string} [params.bodyText]
 * @param {boolean} [params.forceRebuild]
 * @param {boolean} [params.showTip]
 * @param {Document} [params.doc]
 */
function renderOnboardingSlideSurface(surface, params = {}) {
  if (!surface) return;
  const doc = params.doc || surface.ownerDocument || document;
  const tasks = (params.tasks || []).filter((t) => t && t.id);
  const completedSet = params.completedTaskIds instanceof Set
    ? params.completedTaskIds
    : new Set(Array.isArray(params.completedTaskIds) ? params.completedTaskIds.map(String) : []);
  const lastCompletedTaskId = params.lastCompletedTaskId != null ? String(params.lastCompletedTaskId) : '';
  const onTaskRowClick = typeof params.onTaskRowClick === 'function' ? params.onTaskRowClick : null;
  const bodyTextStr = String(params.bodyText || '').trim();
  const forceRebuild = !!params.forceRebuild;
  const showTip = params.showTip !== false;

  const existingRows = surface.querySelectorAll('[data-kp-onboarding-task-id]');
  const existingBody = surface.querySelector('[data-kp-onboarding-body-text="true"]');

  const canUpdateInPlace =
    !forceRebuild &&
    existingRows.length === tasks.length &&
    tasks.every((t, i) => existingRows[i]?.getAttribute('data-kp-onboarding-task-id') === t.id);

  // Body text (independent of task update mode when possible)
  try {
    if (bodyTextStr) {
      if (existingBody && canUpdateInPlace) {
        existingBody.textContent = '';
        // Preserve line breaks without full HTML parse
        const lines = bodyTextStr.split('\n');
        lines.forEach((line, i) => {
          if (i > 0) existingBody.appendChild(doc.createElement('br'));
          renderKeyboardKeysInto(
            (() => { const s = doc.createElement('span'); existingBody.appendChild(s); return s; })(),
            line
          );
        });
        existingBody.style.display = 'block';
        existingBody.style.marginBottom = tasks.length ? '12px' : '0px';
      }
    } else if (existingBody && canUpdateInPlace) {
      existingBody.style.display = 'none';
      existingBody.textContent = '';
    }
  } catch { /* ignore */ }

  if (canUpdateInPlace) {
    tasks.forEach((task, i) => {
      const done = completedSet.has(task.id);
      applyTaskRowVisual(existingRows[i], task, done, {
        uncheckable: done && task.id === lastCompletedTaskId,
        onTaskRowClick
      });
    });
    return;
  }

  // Full rebuild
  while (surface.firstChild) surface.removeChild(surface.firstChild);

  if (bodyTextStr) {
    const body = doc.createElement('div');
    body.setAttribute('data-kp-onboarding-body-text', 'true');
    const lines = bodyTextStr.split('\n');
    lines.forEach((line, i) => {
      if (i > 0) body.appendChild(doc.createElement('br'));
      const span = doc.createElement('span');
      renderKeyboardKeysInto(span, line);
      body.appendChild(span);
    });
    assignStyle(body, {
      fontSize: '13px',
      lineHeight: '1.45',
      color: 'rgba(255,255,255,0.90)',
      marginBottom: tasks.length ? '12px' : '0px',
      opacity: '0.95'
    });
    surface.appendChild(body);
  }

  const list = doc.createElement('div');
  assignStyle(list, {
    display: 'flex',
    flexDirection: 'column',
    gap: '8px'
  });

  for (const task of tasks) {
    const done = completedSet.has(task.id);
    list.appendChild(createTaskRow(doc, task, done, {
      uncheckable: done && task.id === lastCompletedTaskId,
      onTaskRowClick
    }));
  }
  surface.appendChild(list);

  if (showTip) {
    const tip = doc.createElement('div');
    tip.setAttribute('data-kp-onboarding-tip', 'true');
    tip.textContent = ONBOARDING_REOPEN_TIP;
    assignStyle(tip, {
      marginTop: '10px',
      fontSize: '12px',
      opacity: '0.78',
      lineHeight: '1.35',
      color: 'rgba(255,255,255,0.85)'
    });
    surface.appendChild(tip);
  }
}

/**
 * Update chrome (title, step counter, nav disabled state).
 * @param {Object} refs
 * @param {Object} params
 */
function updateOnboardingChrome(refs, params = {}) {
  if (!refs) return;
  const idx = Number(params.slideIndex) || 0;
  const total = Math.max(1, Number(params.slideCount) || 1);
  try {
    if (refs.titleEl) refs.titleEl.textContent = String(params.title || ONBOARDING_DEFAULT_TITLE);
  } catch { /* ignore */ }
  try {
    if (refs.stepEl) refs.stepEl.textContent = `${idx + 1} / ${total}`;
  } catch { /* ignore */ }
  try {
    if (refs.root && params.slideId != null) {
      refs.root.dataset.kpOnboardingSlideId = String(params.slideId || '');
    }
  } catch { /* ignore */ }
  try {
    if (refs.prevBtn) refs.prevBtn.disabled = idx <= 0;
  } catch { /* ignore */ }
  try {
    if (refs.nextBtn) refs.nextBtn.disabled = idx >= total - 1;
  } catch { /* ignore */ }
}

// ── Overlay ─────────────────────────────────────────────────────────────────

/**
 * Ensure modal overlay exists inside the scrollable body host.
 * @param {HTMLElement} bodyHost
 * @param {Document} [doc]
 * @returns {{
 *   overlayEl: HTMLElement,
 *   titleEl: HTMLElement,
 *   msgEl: HTMLElement,
 *   primaryBtn: HTMLButtonElement,
 *   secondaryBtn: HTMLButtonElement
 * }|null}
 */
function ensureOnboardingOverlay(bodyHost, doc) {
  if (!bodyHost) return null;
  const d = doc || bodyHost.ownerDocument || document;

  const existing = bodyHost.querySelector('[data-kp-onboarding-overlay="true"]');
  if (existing) {
    return {
      overlayEl: existing,
      titleEl: existing.querySelector('[data-kp-onboarding-overlay-title="true"]'),
      msgEl: existing.querySelector('[data-kp-onboarding-overlay-message="true"]'),
      primaryBtn: existing.querySelector('button[data-kp-onboarding-overlay-primary="true"]'),
      secondaryBtn: existing.querySelector('button[data-kp-onboarding-overlay-secondary="true"]')
    };
  }

  const overlay = d.createElement('div');
  overlay.setAttribute('data-kp-onboarding-overlay', 'true');
  overlay.hidden = true;
  assignStyle(overlay, {
    position: 'absolute',
    inset: '0',
    padding: '14px',
    display: 'none',
    alignItems: 'center',
    justifyContent: 'center',
    background: 'rgba(0,0,0,0.42)',
    backdropFilter: 'blur(10px)',
    WebkitBackdropFilter: 'blur(10px)',
    zIndex: '5',
    pointerEvents: 'none'
  });

  const card = d.createElement('div');
  assignStyle(card, {
    width: '100%',
    maxWidth: '320px',
    borderRadius: '14px',
    border: '1px solid rgba(255,255,255,0.16)',
    background: 'rgba(18, 18, 18, 0.78)',
    boxShadow: '0 16px 44px rgba(0,0,0,0.55)',
    padding: '14px 14px 12px 14px'
  });

  const titleEl = d.createElement('div');
  titleEl.setAttribute('data-kp-onboarding-overlay-title', 'true');
  titleEl.textContent = 'Nice!';
  assignStyle(titleEl, {
    fontSize: '14px',
    fontWeight: '900',
    letterSpacing: '0.2px',
    marginBottom: '8px'
  });

  const msgEl = d.createElement('div');
  msgEl.setAttribute('data-kp-onboarding-overlay-message', 'true');
  msgEl.textContent = '';
  assignStyle(msgEl, {
    fontSize: '13px',
    lineHeight: '1.35',
    color: 'rgba(255,255,255,0.90)',
    whiteSpace: 'pre-wrap'
  });

  const btnRow = d.createElement('div');
  assignStyle(btnRow, {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'flex-end',
    gap: '8px',
    marginTop: '12px'
  });

  const mkBtn = (variant) => {
    const b = d.createElement('button');
    b.type = 'button';
    assignStyle(b, {
      height: '30px',
      borderRadius: '999px',
      border: variant === 'primary' ? '1px solid rgba(46, 204, 113, 0.55)' : '1px solid rgba(255,255,255,0.20)',
      background: variant === 'primary' ? 'rgba(46, 204, 113, 0.18)' : 'rgba(255,255,255,0.06)',
      color: 'rgba(255,255,255,0.92)',
      cursor: 'pointer',
      fontSize: '12px',
      fontWeight: '800',
      padding: '0 12px',
      lineHeight: '28px'
    });
    return b;
  };

  const secondaryBtn = mkBtn('secondary');
  secondaryBtn.hidden = true;
  secondaryBtn.textContent = '';
  secondaryBtn.setAttribute('data-kp-onboarding-overlay-secondary', 'true');

  const primaryBtn = mkBtn('primary');
  primaryBtn.textContent = 'OK';
  primaryBtn.setAttribute('data-kp-onboarding-overlay-primary', 'true');

  btnRow.appendChild(secondaryBtn);
  btnRow.appendChild(primaryBtn);
  card.appendChild(titleEl);
  card.appendChild(msgEl);
  card.appendChild(btnRow);
  overlay.appendChild(card);
  bodyHost.appendChild(overlay);

  return { overlayEl: overlay, titleEl, msgEl, primaryBtn, secondaryBtn };
}

/**
 * @param {HTMLElement|null} overlayEl
 * @param {boolean} open
 * @param {HTMLElement|null} [root]
 */
function setOnboardingOverlayOpen(overlayEl, open, root = null) {
  if (!overlayEl) return;
  try {
    overlayEl.hidden = !open;
    overlayEl.style.display = open ? 'flex' : 'none';
    overlayEl.style.pointerEvents = open ? 'auto' : 'none';
    if (root) {
      if (open) root.dataset.kpOnboardingOverlayOpen = 'true';
      else delete root.dataset.kpOnboardingOverlayOpen;
    }
  } catch { /* ignore */ }
}



  // Module: src/ui/control-strip.js
/**
 * macOS 7–style Control Strip (content-script friendly).
 *
 * Compact fixed strip (default upper-left). Dark GUI Pro chrome matches the
 * floating keyboard reference panel. Segments: On/Off, move (expanded),
 * Keyboard, Settings, collapse, and close.
 *
 * Position uses the shared panel-position system (snap + margin + persist).
 * Light DOM + inline styles so the strip survives hostile page CSS.
 */






const ROOT_CLASS = 'kp-control-strip';
const DEFAULT_TOP_PX = 16;
const DEFAULT_LEFT_PX = 16;
const STRIP_HEIGHT_PX = 28;
/** Prefer a slightly larger dock margin for the strip (matches legacy 16px perch). */
const STRIP_POSITION_MARGIN_PX = Math.max(PANEL_POSITION_MARGIN_PX, 16);

/**
 * @typedef {{
 *   onToggleEnabled?: () => void,
 *   onToggleKeyboard?: () => void,
 *   onOpenSettings?: () => void,
 *   onCollapseChange?: (collapsed: boolean) => void,
 *   onClose?: () => void
 * }} ControlStripHandlers
 */

class ControlStrip {
  /**
   * @param {ControlStripHandlers} [handlers]
   */
  constructor(handlers = {}) {
    this.root = null;
    this._modulesEl = null;
    this._statusBtn = null;
    this._statusDot = null;
    this._statusLabel = null;
    this._moveBtn = null;
    this._keyboardBtn = null;
    this._settingsBtn = null;
    this._collapseBtn = null;
    this._closeBtn = null;

    this._enabled = true;
    this._textMode = false;
    this._collapsed = false;
    this._keyboardActive = false;
    this._desiredVisible = true;

    /** @type {import('../modules/settings-manager.js').PanelPositionSettings} */
    this._panelPosition = { ...DEFAULT_SETTINGS.panelPositions.controlStrip };
    this._suppressPositionPersist = false;
    /** @type {(() => void)|null} */
    this._dragDispose = null;
    this._dragBound = false;
    this._settingsBound = false;
    this._onStorageChanged = this._onStorageChanged.bind(this);

    /** @type {ControlStripHandlers} */
    this._handlers = {
      onToggleEnabled: typeof handlers.onToggleEnabled === 'function' ? handlers.onToggleEnabled : null,
      onToggleKeyboard: typeof handlers.onToggleKeyboard === 'function' ? handlers.onToggleKeyboard : null,
      onOpenSettings: typeof handlers.onOpenSettings === 'function' ? handlers.onOpenSettings : null,
      onCollapseChange: typeof handlers.onCollapseChange === 'function' ? handlers.onCollapseChange : null,
      onClose: typeof handlers.onClose === 'function' ? handlers.onClose : null
    };

    this._onStatusClick = this._onStatusClick.bind(this);
    this._onKeyboardClick = this._onKeyboardClick.bind(this);
    this._onSettingsClick = this._onSettingsClick.bind(this);
    this._onCollapseClick = this._onCollapseClick.bind(this);
    this._onCloseClick = this._onCloseClick.bind(this);
    this._onDocMutate = this._onDocMutate.bind(this);
    this._onWinResize = this._onWinResize.bind(this);

    this._observer = null;
    this._observeBound = false;
  }

  /**
   * @param {Partial<ControlStripHandlers>} handlers
   */
  setHandlers(handlers = {}) {
    if (typeof handlers.onToggleEnabled === 'function') this._handlers.onToggleEnabled = handlers.onToggleEnabled;
    if (typeof handlers.onToggleKeyboard === 'function') this._handlers.onToggleKeyboard = handlers.onToggleKeyboard;
    if (typeof handlers.onOpenSettings === 'function') this._handlers.onOpenSettings = handlers.onOpenSettings;
    if (typeof handlers.onCollapseChange === 'function') this._handlers.onCollapseChange = handlers.onCollapseChange;
    if (typeof handlers.onClose === 'function') this._handlers.onClose = handlers.onClose;
  }

  isVisible() {
    return !!(this.root && this.root.isConnected && this.root.hidden === false);
  }

  isCollapsed() {
    return !!this._collapsed;
  }

  show() {
    if (window !== window.top) return;
    this._desiredVisible = true;
    this._ensure();
    this.root.hidden = false;
    this.root.style.display = 'flex';
    this.root.style.pointerEvents = 'auto';
    this._applyCollapsedLayout();
    this._bindDrag();
    this._bindSettingsSync();
    this._refreshPanelPosition(); // async
    this._applyPanelPositionNow();
    this._syncOnboardingOffset();
    this._bindOnboardingWatch();
  }

  hide() {
    this._desiredVisible = false;
    if (this.root) {
      this.root.hidden = true;
      this.root.style.display = 'none';
      this.root.style.pointerEvents = 'none';
    }
    this._unbindOnboardingWatch();
  }

  /**
   * Apply desired visibility without changing the persisted "desired" flag when already set.
   * @param {boolean} visible
   */
  setVisible(visible) {
    if (visible) this.show();
    else this.hide();
  }

  /**
   * @param {boolean} collapsed
   * @param {{ notify?: boolean }} [opts]
   */
  setCollapsed(collapsed, opts = {}) {
    const next = !!collapsed;
    if (this._collapsed === next) {
      this._applyCollapsedLayout();
      this._bindDrag();
      return;
    }
    this._collapsed = next;
    this._applyCollapsedLayout();
    this._bindDrag();
    // Width changes when expanding/collapsing — reclamp so margin stays valid.
    this._applyPanelPositionNow();
    this._syncOnboardingOffset();
    if (opts.notify !== false && this._handlers.onCollapseChange) {
      try { this._handlers.onCollapseChange(this._collapsed); } catch { /* ignore */ }
    }
  }

  /**
   * @param {boolean} enabled
   */
  setEnabledState(enabled) {
    this._enabled = !!enabled;
    // Text mode only applies while KeyPilot is on.
    if (!this._enabled) this._textMode = false;
    this._renderStatus();
  }

  /**
   * Reflect text-field focus (text mode) on the On/Off status segment.
   * When active, the green ON indicator turns orange.
   * @param {boolean} active
   */
  setTextModeActive(active) {
    const next = !!active && !!this._enabled;
    if (this._textMode === next) {
      this._renderStatus();
      return;
    }
    this._textMode = next;
    this._renderStatus();
  }

  /**
   * @param {boolean} active
   */
  setKeyboardHelpActive(active) {
    this._keyboardActive = !!active;
    this._renderKeyboard();
  }

  cleanup() {
    this._unbindOnboardingWatch();
    this._unbindDrag();
    this._unbindSettingsSync();
    try {
      if (this._statusBtn) this._statusBtn.removeEventListener('click', this._onStatusClick);
      if (this._keyboardBtn) this._keyboardBtn.removeEventListener('click', this._onKeyboardClick);
      if (this._settingsBtn) this._settingsBtn.removeEventListener('click', this._onSettingsClick);
      if (this._collapseBtn) this._collapseBtn.removeEventListener('click', this._onCollapseClick);
      if (this._closeBtn) this._closeBtn.removeEventListener('click', this._onCloseClick);
    } catch { /* ignore */ }
    try {
      if (this.root && this.root.parentNode) this.root.parentNode.removeChild(this.root);
    } catch { /* ignore */ }
    this.root = null;
    this._modulesEl = null;
    this._statusBtn = null;
    this._statusDot = null;
    this._statusLabel = null;
    this._moveBtn = null;
    this._keyboardBtn = null;
    this._settingsBtn = null;
    this._collapseBtn = null;
    this._closeBtn = null;
  }

  _ensure() {
    if (this.root && this.root.isConnected) {
      this._ensureMoveHandle();
      this._bindDrag();
      return;
    }

    // If early-inject created the shell at document_start, adopt it to avoid flicker.
    try {
      const existing = document.querySelector('.kp-control-strip[data-kp-early-control-strip="true"]');
      if (existing && existing.isConnected) {
        const statusBtn = existing.querySelector('[data-kp-control-strip-status="true"]');
        const statusDot = existing.querySelector('[data-kp-control-strip-status-dot="true"]');
        const statusLabel = existing.querySelector('[data-kp-control-strip-status-label="true"]');
        const modules = existing.querySelector('[data-kp-control-strip-modules="true"]');
        const keyboardBtn = existing.querySelector('[data-kp-control-strip-keyboard="true"]');
        const settingsBtn = existing.querySelector('[data-kp-control-strip-settings="true"]');
        const collapseBtn = existing.querySelector('[data-kp-control-strip-collapse="true"]');
        const closeBtn = existing.querySelector('[data-kp-control-strip-close="true"]');

        if (statusBtn && modules && keyboardBtn && settingsBtn && collapseBtn && closeBtn) {
          // Enrich early shell with icons if it only has text labels.
          try { this._ensureSegmentIcon(keyboardBtn, 'TOGGLE_KEYBOARD_HELP'); } catch { /* ignore */ }
          try { this._ensureSegmentIcon(settingsBtn, 'OPEN_SETTINGS_POPOVER'); } catch { /* ignore */ }

          this.root = existing;
          this._modulesEl = modules;
          this._statusBtn = statusBtn;
          this._statusDot = statusDot;
          this._statusLabel = statusLabel;
          this._keyboardBtn = keyboardBtn;
          this._settingsBtn = settingsBtn;
          this._collapseBtn = collapseBtn;
          this._closeBtn = closeBtn;

          this._ensureMoveHandle();
          this._bindButtonHandlers();
          this._renderStatus();
          this._renderKeyboard();
          this._applyCollapsedLayout();
          this._bindDrag();
          this._applyPanelPositionNow();
          return;
        }
      }
    } catch { /* ignore */ }

    const root = document.createElement('div');
    root.className = ROOT_CLASS;
    root.hidden = true;
    root.setAttribute('role', 'toolbar');
    root.setAttribute('aria-label', 'KeyPilot control strip');
    root.setAttribute('data-kp-control-strip', 'true');

    Object.assign(root.style, {
      position: 'fixed',
      left: `${DEFAULT_LEFT_PX}px`,
      top: `${DEFAULT_TOP_PX}px`,
      height: `${STRIP_HEIGHT_PX}px`,
      minHeight: `${STRIP_HEIGHT_PX}px`,
      maxHeight: `${STRIP_HEIGHT_PX}px`,
      display: 'none',
      flexDirection: 'row',
      alignItems: 'stretch',
      zIndex: String(Z_INDEX.CONTROL_STRIP || 2147483025),
      background: 'rgba(10, 11, 14, 0.98)',
      color: 'rgba(248, 250, 252, 0.95)',
      border: '1px solid rgba(255, 255, 255, 0.08)',
      borderRadius: '4px',
      boxShadow: '0 8px 24px rgba(0,0,0,0.5), 0 1px 4px rgba(0,0,0,0.35)',
      fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif',
      pointerEvents: 'auto',
      overflow: 'hidden',
      boxSizing: 'border-box',
      userSelect: 'none',
      WebkitUserSelect: 'none'
    });
    applyPopupThemeVars(root);

    // Status (On/Off) — always visible
    const statusBtn = this._createSegmentButton({
      ariaLabel: 'Toggle KeyPilot on or off',
      title: 'Toggle KeyPilot (Alt+K)',
      primary: true
    });
    statusBtn.setAttribute('data-kp-control-strip-status', 'true');

    const statusInner = document.createElement('span');
    Object.assign(statusInner.style, {
      display: 'inline-flex',
      alignItems: 'center',
      gap: '6px',
      pointerEvents: 'none'
    });

    const statusDot = document.createElement('span');
    Object.assign(statusDot.style, {
      width: '7px',
      height: '7px',
      borderRadius: '50%',
      flex: '0 0 auto',
      background: 'rgba(16, 185, 129, 0.95)',
      boxShadow: '0 0 0 2px rgba(16, 185, 129, 0.2)'
    });
    statusDot.setAttribute('aria-hidden', 'true');
    statusDot.setAttribute('data-kp-control-strip-status-dot', 'true');

    const statusLabel = document.createElement('span');
    statusLabel.textContent = 'ON';
    statusLabel.setAttribute('data-kp-control-strip-status-label', 'true');
    Object.assign(statusLabel.style, {
      fontSize: '11px',
      fontWeight: '700',
      letterSpacing: '0.06em',
      lineHeight: '1'
    });

    statusInner.appendChild(statusDot);
    statusInner.appendChild(statusLabel);
    statusBtn.appendChild(statusInner);

    // Expandable modules (hidden when collapsed)
    const modules = document.createElement('div');
    modules.setAttribute('data-kp-control-strip-modules', 'true');
    Object.assign(modules.style, {
      display: 'flex',
      flexDirection: 'row',
      alignItems: 'stretch',
      flex: '0 0 auto'
    });

    const moveBtn = this._createMoveHandleButton();

    const keyboardBtn = this._createSegmentButton({
      ariaLabel: 'Toggle keyboard reference',
      title: 'Keyboard reference',
      text: 'KB',
      iconActionId: 'TOGGLE_KEYBOARD_HELP'
    });
    keyboardBtn.setAttribute('data-kp-control-strip-keyboard', 'true');

    const settingsBtn = this._createSegmentButton({
      ariaLabel: 'Open KeyPilot settings',
      title: 'Settings',
      text: 'Settings',
      iconActionId: 'OPEN_SETTINGS_POPOVER'
    });
    settingsBtn.setAttribute('data-kp-control-strip-settings', 'true');

    modules.appendChild(moveBtn);
    modules.appendChild(keyboardBtn);
    modules.appendChild(settingsBtn);

    // Collapse / expand
    const collapseBtn = this._createSegmentButton({
      ariaLabel: 'Collapse control strip',
      title: 'Collapse',
      text: '◀',
      compact: true
    });
    collapseBtn.setAttribute('data-kp-control-strip-collapse', 'true');

    // Close
    const closeBtn = this._createSegmentButton({
      ariaLabel: 'Close control strip',
      title: 'Close (Alt+J to show again)',
      text: '×',
      compact: true,
      last: true
    });
    closeBtn.setAttribute('data-kp-control-strip-close', 'true');

    root.appendChild(statusBtn);
    root.appendChild(modules);
    root.appendChild(collapseBtn);
    root.appendChild(closeBtn);

    (document.body || document.documentElement).appendChild(root);

    this.root = root;
    this._modulesEl = modules;
    this._statusBtn = statusBtn;
    this._statusDot = statusDot;
    this._statusLabel = statusLabel;
    this._moveBtn = moveBtn;
    this._keyboardBtn = keyboardBtn;
    this._settingsBtn = settingsBtn;
    this._collapseBtn = collapseBtn;
    this._closeBtn = closeBtn;

    this._bindButtonHandlers();
    this._renderStatus();
    this._renderKeyboard();
    this._applyCollapsedLayout();
    this._bindDrag();
    this._applyPanelPositionNow();
  }

  /**
   * Drag grip shown only when the strip is expanded.
   * @returns {HTMLElement}
   */
  _createMoveHandleButton() {
    const btn = this._createSegmentButton({
      ariaLabel: 'Move control strip',
      title: 'Drag to move',
      text: '⠿',
      compact: true
    });
    btn.setAttribute('data-kp-control-strip-move', 'true');
    try {
      btn.style.cursor = 'grab';
      btn.style.touchAction = 'none';
      btn.style.letterSpacing = '0';
      btn.style.fontSize = '12px';
      btn.style.opacity = '0.85';
    } catch { /* ignore */ }
    // Prevent accidental click activation — drag handle only.
    btn.addEventListener('click', (e) => {
      try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    });
    return btn;
  }

  /**
   * Ensure early-inject shells gain a move handle without rebuilding the strip.
   */
  _ensureMoveHandle() {
    if (!this.root || !this._modulesEl) return;
    let moveBtn = this.root.querySelector('[data-kp-control-strip-move="true"]');
    if (!moveBtn) {
      moveBtn = this._createMoveHandleButton();
      try {
        this._modulesEl.insertBefore(moveBtn, this._modulesEl.firstChild);
      } catch {
        try { this._modulesEl.appendChild(moveBtn); } catch { /* ignore */ }
      }
    }
    this._moveBtn = moveBtn;
  }

  _applyPanelPositionNow() {
    if (!this.root) return;
    try {
      applyPanelPosition(this.root, this._panelPosition, {
        margin: STRIP_POSITION_MARGIN_PX,
        defaultAnchor: 'top-left'
      });
    } catch { /* ignore */ }
  }

  /**
   * @param {import('../modules/settings-manager.js').PanelPositionSettings|null|undefined} next
   * @param {{ persist?: boolean }} [opts]
   */
  _setPanelPosition(next, opts = {}) {
    const normalized = normalizePanelPositionState(
      next,
      DEFAULT_SETTINGS.panelPositions.controlStrip
    ) || { ...DEFAULT_SETTINGS.panelPositions.controlStrip };
    this._panelPosition = {
      left: normalized.left,
      top: normalized.top,
      anchor: normalized.anchor === undefined ? null : normalized.anchor
    };
    this._applyPanelPositionNow();
    this._syncOnboardingOffset();
    if (opts.persist && !this._suppressPositionPersist) {
      this._persistPanelPosition(this._panelPosition);
    }
  }

  async _refreshPanelPosition() {
    try {
      const settings = await getSettings();
      const stored = settings?.panelPositions?.controlStrip;
      this._suppressPositionPersist = true;
      this._setPanelPosition(stored, { persist: false });
      this._suppressPositionPersist = false;
    } catch {
      this._suppressPositionPersist = false;
    }
  }

  /**
   * @param {import('../modules/settings-manager.js').PanelPositionSettings} position
   */
  async _persistPanelPosition(position) {
    try {
      await setSettings({
        panelPositions: {
          controlStrip: {
            left: position.left,
            top: position.top,
            anchor: position.anchor === undefined ? null : position.anchor
          }
        }
      });
    } catch { /* ignore */ }
  }

  _bindDrag() {
    if (!this.root || !this._moveBtn) return;
    // Only draggable while expanded (move icon visible).
    if (this._collapsed) {
      this._unbindDrag();
      return;
    }
    if (this._dragBound) return;
    try {
      const api = makePanelDraggable(this.root, this._moveBtn, {
        margin: STRIP_POSITION_MARGIN_PX,
        // Keep width/height content-driven so expand/collapse still works.
        pinWidth: false,
        pinHeight: false,
        onMove: () => {
          this._syncOnboardingOffset();
        },
        onMoveEnd: (state) => {
          if (!state?.moved) return;
          this._setPanelPosition(
            {
              left: state.left,
              top: state.top,
              anchor: state.anchor
            },
            { persist: true }
          );
        }
      });
      this._dragDispose = api?.dispose || null;
      this._dragBound = true;
    } catch (err) {
      console.warn('[KeyPilot] Failed to make control strip draggable:', err?.message || err);
      this._dragDispose = null;
      this._dragBound = false;
    }
  }

  _unbindDrag() {
    try { this._dragDispose?.(); } catch { /* ignore */ }
    this._dragDispose = null;
    this._dragBound = false;
  }

  _bindSettingsSync() {
    if (this._settingsBound) return;
    try {
      if (chrome?.storage?.onChanged?.addListener) {
        chrome.storage.onChanged.addListener(this._onStorageChanged);
        this._settingsBound = true;
      }
    } catch { /* ignore */ }
  }

  _unbindSettingsSync() {
    if (!this._settingsBound) return;
    try { chrome?.storage?.onChanged?.removeListener?.(this._onStorageChanged); } catch { /* ignore */ }
    this._settingsBound = false;
  }

  _onStorageChanged(changes, area) {
    try {
      if (area !== 'sync' && area !== 'local') return;
      const entry = changes && changes[SETTINGS_STORAGE_KEY];
      if (!entry || !entry.newValue) return;
      const nextPos = entry.newValue.panelPositions?.controlStrip;
      if (nextPos && typeof nextPos === 'object') {
        this._suppressPositionPersist = true;
        this._setPanelPosition(nextPos, { persist: false });
        this._suppressPositionPersist = false;
      }
    } catch { /* ignore */ }
  }

  /**
   * Attach click handlers (safe to call after early-inject adoption).
   */
  _bindButtonHandlers() {
    try {
      if (this._statusBtn) {
        this._statusBtn.removeEventListener('click', this._onStatusClick);
        this._statusBtn.addEventListener('click', this._onStatusClick);
      }
      if (this._keyboardBtn) {
        this._keyboardBtn.removeEventListener('click', this._onKeyboardClick);
        this._keyboardBtn.addEventListener('click', this._onKeyboardClick);
      }
      if (this._settingsBtn) {
        this._settingsBtn.removeEventListener('click', this._onSettingsClick);
        this._settingsBtn.addEventListener('click', this._onSettingsClick);
      }
      if (this._collapseBtn) {
        this._collapseBtn.removeEventListener('click', this._onCollapseClick);
        this._collapseBtn.addEventListener('click', this._onCollapseClick);
      }
      if (this._closeBtn) {
        this._closeBtn.removeEventListener('click', this._onCloseClick);
        this._closeBtn.addEventListener('click', this._onCloseClick);
      }
    } catch { /* ignore */ }
  }

  /**
   * Prepend action icon into a segment button if missing (early shell is text-only).
   * @param {HTMLElement|null} btn
   * @param {string} actionId
   */
  _ensureSegmentIcon(btn, actionId) {
    if (!btn || !actionId) return;
    try {
      if (btn.querySelector('img[data-kp-control-strip-icon="true"]')) return;
      const icon = this._createActionIcon(actionId);
      if (!icon) return;
      icon.setAttribute('data-kp-control-strip-icon', 'true');
      btn.insertBefore(icon, btn.firstChild);
    } catch { /* ignore */ }
  }

  /**
   * Inline monochrome icon for a KeyPilot action (keyboard, gear, etc.).
   * @param {string} actionId
   * @param {string} [color]
   * @returns {HTMLElement|null}
   */
  _createActionIcon(actionId, color = 'rgba(220, 220, 225, 0.92)') {
    try {
      const cssUri = getActionIconDataUri(actionId, { fill: color });
      if (!cssUri) return null;
      // cssUri is CSS url("data:...") — extract the raw data URL for <img src>.
      const match = String(cssUri).match(/^url\("(.+)"\)$/);
      const src = match ? match[1] : null;
      if (!src) return null;
      const img = document.createElement('img');
      img.src = src;
      img.alt = '';
      img.setAttribute('aria-hidden', 'true');
      Object.assign(img.style, {
        width: '14px',
        height: '14px',
        display: 'block',
        pointerEvents: 'none',
        flex: '0 0 auto',
        opacity: '0.95'
      });
      return img;
    } catch {
      return null;
    }
  }

  /**
   * @param {{
   *   ariaLabel: string,
   *   title?: string,
   *   text?: string,
   *   iconActionId?: string,
   *   primary?: boolean,
   *   compact?: boolean,
   *   last?: boolean
   * }} opts
   */
  _createSegmentButton(opts) {
    const btn = document.createElement('button');
    btn.type = 'button';
    btn.setAttribute('aria-label', opts.ariaLabel);
    if (opts.title) btn.title = opts.title;

    if (opts.iconActionId) {
      const icon = this._createActionIcon(opts.iconActionId);
      if (icon) {
        try { icon.setAttribute('data-kp-control-strip-icon', 'true'); } catch { /* ignore */ }
        btn.appendChild(icon);
      }
    }
    if (opts.text) {
      const label = document.createElement('span');
      label.textContent = opts.text;
      Object.assign(label.style, {
        pointerEvents: 'none',
        lineHeight: '1'
      });
      btn.appendChild(label);
    }

    Object.assign(btn.style, {
      display: 'inline-flex',
      alignItems: 'center',
      justifyContent: 'center',
      gap: '4px',
      height: '100%',
      minHeight: `${STRIP_HEIGHT_PX - 2}px`,
      margin: '0',
      padding: opts.compact ? '0 8px' : '0 10px',
      border: 'none',
      borderRight: opts.last ? 'none' : '1px solid rgba(255,255,255,0.08)',
      borderRadius: '0',
      background: opts.primary
        ? 'linear-gradient(180deg, #1a1b1f 0%, #121316 100%)'
        : 'transparent',
      color: 'rgba(220, 220, 225, 0.92)',
      fontSize: '11px',
      fontWeight: '600',
      letterSpacing: '0.01em',
      lineHeight: '1',
      cursor: 'pointer',
      flex: '0 0 auto',
      boxShadow: 'none',
      outline: 'none',
      whiteSpace: 'nowrap'
    });

    btn.addEventListener('mouseenter', () => {
      try {
        btn.style.background = opts.primary
          ? 'linear-gradient(180deg, #22232a 0%, #18191e 100%)'
          : 'rgba(255,255,255,0.06)';
      } catch { /* ignore */ }
    });
    btn.addEventListener('mouseleave', () => {
      try {
        if (btn === this._keyboardBtn && this._keyboardActive) {
          btn.style.background = 'rgba(59, 130, 246, 0.18)';
        } else {
          btn.style.background = opts.primary
            ? 'linear-gradient(180deg, #1a1b1f 0%, #121316 100%)'
            : 'transparent';
        }
      } catch { /* ignore */ }
    });
    btn.addEventListener('focus', () => {
      try { btn.style.boxShadow = 'inset 0 0 0 1px rgba(59, 130, 246, 0.55)'; } catch { /* ignore */ }
    });
    btn.addEventListener('blur', () => {
      try { btn.style.boxShadow = 'none'; } catch { /* ignore */ }
    });

    return btn;
  }

  _applyCollapsedLayout() {
    if (!this.root) return;
    const collapsed = !!this._collapsed;

    if (this._modulesEl) {
      this._modulesEl.style.display = collapsed ? 'none' : 'flex';
    }
    if (this._moveBtn) {
      this._moveBtn.style.display = collapsed ? 'none' : 'inline-flex';
    }
    if (this._closeBtn) {
      this._closeBtn.style.display = collapsed ? 'none' : 'inline-flex';
    }
    if (this._collapseBtn) {
      this._collapseBtn.textContent = collapsed ? '▶' : '◀';
      this._collapseBtn.setAttribute(
        'aria-label',
        collapsed ? 'Expand control strip' : 'Collapse control strip'
      );
      this._collapseBtn.title = collapsed ? 'Expand' : 'Collapse';
      // When collapsed, collapse control is last visible segment.
      this._collapseBtn.style.borderRight = 'none';
      if (!collapsed && this._closeBtn) {
        this._collapseBtn.style.borderRight = '1px solid rgba(255,255,255,0.08)';
      }
    }

    try {
      this.root.setAttribute('data-kp-collapsed', collapsed ? 'true' : 'false');
    } catch { /* ignore */ }
  }

  _renderStatus() {
    if (!this._statusLabel || !this._statusDot) return;
    const on = !!this._enabled;
    const textMode = on && !!this._textMode;
    this._statusLabel.textContent = on ? 'ON' : 'OFF';

    // Green when on; orange while a text field has focus (text mode); gray when off.
    let dotBg;
    let dotShadow;
    let labelColor;
    if (!on) {
      dotBg = 'rgba(148, 163, 184, 0.85)';
      dotShadow = '0 0 0 2px rgba(148, 163, 184, 0.15)';
      labelColor = 'rgba(148, 163, 184, 0.95)';
    } else if (textMode) {
      // Matches KeyPilot text-mode orange (#ff8c00 / COLORS.ORANGE).
      dotBg = 'rgba(255, 140, 0, 0.95)';
      dotShadow = '0 0 0 2px rgba(255, 140, 0, 0.25)';
      labelColor = 'rgba(253, 186, 116, 0.98)';
    } else {
      dotBg = 'rgba(16, 185, 129, 0.95)';
      dotShadow = '0 0 0 2px rgba(16, 185, 129, 0.2)';
      labelColor = 'rgba(167, 243, 208, 0.95)';
    }

    this._statusDot.style.background = dotBg;
    this._statusDot.style.boxShadow = dotShadow;
    this._statusLabel.style.color = labelColor;
    if (this._statusBtn) {
      this._statusBtn.setAttribute('aria-pressed', on ? 'true' : 'false');
      try {
        this._statusBtn.setAttribute('data-kp-text-mode', textMode ? 'true' : 'false');
      } catch { /* ignore */ }
      this._statusBtn.title = !on
        ? 'KeyPilot is off — click to turn on (Alt+K)'
        : textMode
          ? 'KeyPilot is on (text mode) — click to turn off (Alt+K)'
          : 'KeyPilot is on — click to turn off (Alt+K)';
    }
  }

  _renderKeyboard() {
    if (!this._keyboardBtn) return;
    const active = !!this._keyboardActive;
    this._keyboardBtn.setAttribute('aria-pressed', active ? 'true' : 'false');
    this._keyboardBtn.style.background = active
      ? 'rgba(59, 130, 246, 0.18)'
      : 'transparent';
    this._keyboardBtn.style.color = active
      ? 'rgba(191, 219, 254, 0.98)'
      : 'rgba(220, 220, 225, 0.92)';
    this._keyboardBtn.title = active
      ? 'Hide keyboard reference'
      : 'Show keyboard reference';
  }

  _onStatusClick(e) {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    if (this._handlers.onToggleEnabled) {
      try { this._handlers.onToggleEnabled(); } catch { /* ignore */ }
    }
  }

  _onKeyboardClick(e) {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    if (this._handlers.onToggleKeyboard) {
      try { this._handlers.onToggleKeyboard(); } catch { /* ignore */ }
    }
  }

  _onSettingsClick(e) {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    if (this._handlers.onOpenSettings) {
      try { this._handlers.onOpenSettings(); } catch { /* ignore */ }
    }
  }

  _onCollapseClick(e) {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    this.setCollapsed(!this._collapsed, { notify: true });
  }

  _onCloseClick(e) {
    try { e.preventDefault(); e.stopPropagation(); } catch { /* ignore */ }
    this.hide();
    if (this._handlers.onClose) {
      try { this._handlers.onClose(); } catch { /* ignore */ }
    }
  }

  /**
   * Reposition the walkthrough relative to the strip.
   * Does not force the strip back to the default top-left — user position is preserved.
   */
  _syncOnboardingOffset() {
    if (!this.root) return;
    try {
      const panel = document.querySelector('.kp-onboarding-panel');
      if (panel && panel.isConnected) {
        const hidden = panel.hidden === true
          || panel.getAttribute('hidden') !== null
          || panel.style.display === 'none';
        if (!hidden) positionOnboardingBelowControlStrip(panel);
      }
    } catch { /* ignore */ }
  }

  _bindOnboardingWatch() {
    if (this._observeBound) return;
    try {
      if (typeof MutationObserver !== 'undefined') {
        this._observer = new MutationObserver(() => {
          this._syncOnboardingOffset();
        });
        this._observer.observe(document.documentElement || document.body, {
          childList: true,
          subtree: true,
          attributes: true,
          attributeFilter: ['hidden', 'style', 'class']
        });
      }
    } catch { /* ignore */ }
    try {
      window.addEventListener('resize', this._onWinResize, true);
    } catch { /* ignore */ }
    this._observeBound = true;
  }

  _unbindOnboardingWatch() {
    if (!this._observeBound) return;
    try {
      if (this._observer) {
        this._observer.disconnect();
        this._observer = null;
      }
    } catch { /* ignore */ }
    try {
      window.removeEventListener('resize', this._onWinResize, true);
    } catch { /* ignore */ }
    this._observeBound = false;
  }

  _onDocMutate() {
    this._syncOnboardingOffset();
  }

  _onWinResize() {
    // Re-resolve anchors / reclamp free positions when the viewport changes.
    this._applyPanelPositionNow();
    this._syncOnboardingOffset();
  }
}



  // Module: src/ui/onboarding-panel.js
/**
 * Floating KeyPilot onboarding walkthrough panel (content-script friendly).
 * Shell + checklist DOM live in onboarding-shared.js (shared with early-inject).
 */



function stripListeners(btn) {
  try {
    if (!btn || !btn.parentNode) return btn;
    const clone = btn.cloneNode(true);
    btn.parentNode.replaceChild(clone, btn);
    return clone;
  } catch {
    return btn;
  }
}

class OnboardingPanel {
  /**
   * @param {Object} params
   * @param {() => void} params.onRequestClose
   * @param {() => void} [params.onRequestPrev]
   * @param {() => void} [params.onRequestNext]
   * @param {() => void} [params.onRequestReset]
   * @param {(taskId: string) => void} [params.onRequestUncheckTask]
   */
  constructor({ onRequestClose, onRequestPrev, onRequestNext, onRequestReset, onRequestUncheckTask } = {}) {
    this.root = null;
    this.body = null;
    this.slideSurface = null;
    this.titleEl = null;
    this.stepEl = null;
    this.closeBtn = null;
    this.prevBtn = null;
    this.nextBtn = null;
    this.resetBtn = null;
    this._overlayEl = null;
    this._overlayTitleEl = null;
    this._overlayMsgEl = null;
    this._overlayPrimaryBtn = null;
    this._overlaySecondaryBtn = null;
    this._overlayOnPrimary = null;
    this._overlayOnSecondary = null;
    this._lastRenderedSlideId = null;
    this._lastRenderedSlideIndex = null;
    this._onRequestClose = typeof onRequestClose === 'function' ? onRequestClose : null;
    this._onRequestPrev = typeof onRequestPrev === 'function' ? onRequestPrev : null;
    this._onRequestNext = typeof onRequestNext === 'function' ? onRequestNext : null;
    this._onRequestReset = typeof onRequestReset === 'function' ? onRequestReset : null;
    this._onRequestUncheckTask = typeof onRequestUncheckTask === 'function' ? onRequestUncheckTask : null;
    this._onCloseClick = this._onCloseClick.bind(this);
    this._onPrevClick = this._onPrevClick.bind(this);
    this._onNextClick = this._onNextClick.bind(this);
    this._onResetClick = this._onResetClick.bind(this);
    this._onOverlayPrimary = this._onOverlayPrimary.bind(this);
    this._onOverlaySecondary = this._onOverlaySecondary.bind(this);
    this._onTaskRowClick = this._onTaskRowClick.bind(this);

    // Tip bubble under the control-strip On/Off segment (shown while KP is off
    // after the walkthrough "turn off" step).
    this._reEnableTipEl = null;
    this._reEnableTipArrow = null;
    this._reEnableTipMsg = null;
    this._onReEnableTipOutside = this._onReEnableTipOutside.bind(this);
    this._onReEnableTipResize = this._onReEnableTipResize.bind(this);
    this._reEnableTipAnchor = null;
  }

  isVisible() {
    return !!(this.root && this.root.isConnected && this.root.hidden === false);
  }

  show() {
    if (window !== window.top) return;
    this._ensure();
    setOnboardingPanelVisible(this.root, true);
  }

  hide() {
    setOnboardingPanelVisible(this.root, false);
  }

  /**
   * Small speech-bubble under the control strip status control.
   * Click outside dismisses it (interrupted); status click is ignored as outside.
   *
   * @param {Object} [opts]
   * @param {Element|null} [opts.anchorEl]
   * @param {string} [opts.message]
   */
  showReEnableTip({
    anchorEl = null,
    message = 'Click the control strip again to turn KeyPilot back on.'
  } = {}) {
    if (window !== window.top) return;
    this.hideReEnableTip();

    const anchor =
      anchorEl ||
      document.querySelector('.kp-control-strip [data-kp-control-strip-status="true"]') ||
      document.querySelector('.kp-control-strip');
    if (!anchor || !anchor.isConnected) return;

    const tip = document.createElement('div');
    tip.setAttribute('data-kp-onboarding-reenable-tip', 'true');
    tip.setAttribute('role', 'status');
    Object.assign(tip.style, {
      position: 'fixed',
      zIndex: String((Z_INDEX.ONBOARDING_PANEL || ONBOARDING_PANEL_Z_FALLBACK) + 2),
      maxWidth: '260px',
      padding: '10px 12px',
      borderRadius: '12px',
      border: '1px solid rgba(255,255,255,0.16)',
      background: 'rgba(18, 18, 18, 0.96)',
      color: 'rgba(255,255,255,0.95)',
      boxShadow: '0 10px 28px rgba(0,0,0,0.45)',
      fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif',
      fontSize: '13px',
      fontWeight: '600',
      lineHeight: '1.35',
      pointerEvents: 'auto'
    });

    const arrow = document.createElement('div');
    arrow.setAttribute('data-kp-onboarding-reenable-tip-arrow', 'true');
    Object.assign(arrow.style, {
      position: 'absolute',
      width: '0',
      height: '0',
      borderLeft: '8px solid transparent',
      borderRight: '8px solid transparent',
      borderBottom: '8px solid rgba(18, 18, 18, 0.96)',
      top: '-8px',
      left: '20px',
      filter: 'drop-shadow(0 -1px 0 rgba(255,255,255,0.16))'
    });

    const msg = document.createElement('div');
    msg.textContent = String(message || '');
    tip.appendChild(arrow);
    tip.appendChild(msg);

    try { applyPopupThemeVars(tip); } catch { /* ignore */ }

    (document.body || document.documentElement).appendChild(tip);
    this._reEnableTipEl = tip;
    this._reEnableTipArrow = arrow;
    this._reEnableTipMsg = msg;
    this._reEnableTipAnchor = anchor;

    this._positionReEnableTip();

    // Outside click: capture phase so we see it before other handlers; skip tip + status btn.
    try {
      document.addEventListener('pointerdown', this._onReEnableTipOutside, true);
      window.addEventListener('resize', this._onReEnableTipResize, true);
      window.addEventListener('scroll', this._onReEnableTipResize, true);
    } catch { /* ignore */ }
  }

  hideReEnableTip() {
    try {
      document.removeEventListener('pointerdown', this._onReEnableTipOutside, true);
      window.removeEventListener('resize', this._onReEnableTipResize, true);
      window.removeEventListener('scroll', this._onReEnableTipResize, true);
    } catch { /* ignore */ }
    try {
      if (this._reEnableTipEl && this._reEnableTipEl.parentNode) {
        this._reEnableTipEl.parentNode.removeChild(this._reEnableTipEl);
      }
    } catch { /* ignore */ }
    this._reEnableTipEl = null;
    this._reEnableTipArrow = null;
    this._reEnableTipMsg = null;
    this._reEnableTipAnchor = null;
  }

  isReEnableTipVisible() {
    return !!(this._reEnableTipEl && this._reEnableTipEl.isConnected);
  }

  _positionReEnableTip() {
    const tip = this._reEnableTipEl;
    const anchor = this._reEnableTipAnchor;
    if (!tip || !anchor || !anchor.isConnected) return;
    try {
      const rect = anchor.getBoundingClientRect();
      const tipRect = tip.getBoundingClientRect();
      const gap = 10;
      let left = Math.round(rect.left + rect.width / 2 - Math.min(tipRect.width, 260) / 2);
      left = Math.max(8, Math.min(left, window.innerWidth - tipRect.width - 8));
      let top = Math.round(rect.bottom + gap);
      // Prefer below the strip; if off-screen, flip above.
      if (top + tipRect.height > window.innerHeight - 8) {
        top = Math.max(8, Math.round(rect.top - tipRect.height - gap));
        if (this._reEnableTipArrow) {
          Object.assign(this._reEnableTipArrow.style, {
            top: 'auto',
            bottom: '-8px',
            borderBottom: 'none',
            borderTop: '8px solid rgba(18, 18, 18, 0.96)'
          });
        }
      } else if (this._reEnableTipArrow) {
        Object.assign(this._reEnableTipArrow.style, {
          top: '-8px',
          bottom: 'auto',
          borderTop: 'none',
          borderBottom: '8px solid rgba(18, 18, 18, 0.96)'
        });
      }
      tip.style.left = `${left}px`;
      tip.style.top = `${top}px`;

      // Point the arrow toward the anchor center.
      if (this._reEnableTipArrow) {
        const arrowLeft = Math.round(rect.left + rect.width / 2 - left - 8);
        this._reEnableTipArrow.style.left = `${Math.max(12, Math.min(arrowLeft, tipRect.width - 24))}px`;
      }
    } catch { /* ignore */ }
  }

  _onReEnableTipOutside(e) {
    try {
      const t = e?.target;
      if (!t || !this._reEnableTipEl) return;
      if (this._reEnableTipEl.contains(t)) return;
      // Clicks on the status control are intentional (toggle back on) — keep tip until ON.
      if (typeof t.closest === 'function') {
        if (t.closest('[data-kp-control-strip-status="true"]')) return;
        if (t.closest('[data-kp-onboarding-reenable-tip="true"]')) return;
      }
      this.hideReEnableTip();
    } catch { /* ignore */ }
  }

  _onReEnableTipResize() {
    this._positionReEnableTip();
  }

  /**
   * @param {Object} params
   * @param {string} params.title
   * @param {string} [params.bodyText]
   * @param {string} params.slideId
   * @param {number} params.slideIndex
   * @param {number} params.slideCount
   * @param {Array} params.tasks
   * @param {Set<string>} params.completedTaskIds
   * @param {{type:'slide', dir:1|-1}|null} [params.transition]
   * @param {boolean} [params.forceRebuild]
   * @returns {Promise<void>}
   */
  async render({
    title,
    bodyText = '',
    slideId,
    slideIndex,
    slideCount,
    tasks,
    completedTaskIds,
    lastCompletedTaskId = null,
    showTip = true,
    transition = null,
    forceRebuild = false
  }) {
    if (!this.root || this.root.hidden) return;

    try {
      const targetSurface = this.slideSurface || this.body;
      const refs = {
        root: this.root,
        titleEl: this.titleEl,
        stepEl: this.stepEl,
        prevBtn: this.prevBtn,
        nextBtn: this.nextBtn
      };

      const updateDom = () => {
        updateOnboardingChrome(refs, {
          title: title || ONBOARDING_DEFAULT_TITLE,
          slideId,
          slideIndex,
          slideCount
        });
        renderOnboardingSlideSurface(targetSurface, {
          tasks,
          completedTaskIds,
          lastCompletedTaskId,
          bodyText,
          forceRebuild,
          showTip: showTip !== false,
          onTaskRowClick: this._onTaskRowClick
        });
      };

      const doSlide = !!(transition && transition.type === 'slide');
      const dir = doSlide && transition?.dir === -1 ? -1 : 1;

      if (!doSlide) {
        updateDom();
        this._lastRenderedSlideId = String(slideId || '');
        this._lastRenderedSlideIndex = Number(slideIndex) || 0;
        return;
      }

      // Prefer View Transitions API.
      try {
        if (document && typeof document.startViewTransition === 'function' && targetSurface) {
          try { this.root?.style?.setProperty?.('--kp-onboarding-slide-dir', String(dir)); } catch { /* ignore */ }
          try { targetSurface.style.viewTransitionName = 'kp-onboarding-slide-surface'; } catch { /* ignore */ }

          const vt = document.startViewTransition(() => {
            updateDom();
          });
          await Promise.resolve(vt?.finished).catch(() => {});

          try { targetSurface.style.viewTransitionName = ''; } catch { /* ignore */ }
          this._lastRenderedSlideId = String(slideId || '');
          this._lastRenderedSlideIndex = Number(slideIndex) || 0;
          return;
        }
      } catch {
        // fall back
      }

      try {
        targetSurface.style.transform = `translateX(${dir > 0 ? '100%' : '-100%'})`;
        targetSurface.style.opacity = '0.7';
      } catch { /* ignore */ }

      updateDom();

      try {
        const anim = targetSurface.animate([
          { transform: `translateX(${dir > 0 ? '100%' : '-100%'})`, opacity: 0.7 },
          { transform: 'translateX(0%)', opacity: 1 }
        ], { duration: 220, easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)', fill: 'both' });
        await Promise.resolve(anim?.finished).catch(() => {});
      } catch { /* ignore */ }

      try {
        targetSurface.style.transform = '';
        targetSurface.style.opacity = '';
      } catch { /* ignore */ }

      this._lastRenderedSlideId = String(slideId || '');
      this._lastRenderedSlideIndex = Number(slideIndex) || 0;
    } catch (e) {
      try {
        const surface = this.slideSurface || this.body;
        while (surface && surface.firstChild) surface.removeChild(surface.firstChild);
        const msg = document.createElement('div');
        msg.textContent = 'Unable to render onboarding panel on this page.';
        surface?.appendChild(msg);
      } catch {
        // ignore
      }
      console.warn('[KeyPilot] Failed to render onboarding panel:', e);
    }
  }

  showOverlay({ title, message, primaryText = 'OK', secondaryText = '', onPrimary = null, onSecondary = null } = {}) {
    this._ensure();
    if (!this.root || !this._overlayEl) return;

    this._overlayOnPrimary = typeof onPrimary === 'function' ? onPrimary : null;
    this._overlayOnSecondary = typeof onSecondary === 'function' ? onSecondary : null;

    try { this._overlayTitleEl.textContent = String(title || 'Nice!'); } catch { /* ignore */ }
    try { this._overlayMsgEl.textContent = String(message || ''); } catch { /* ignore */ }
    try {
      this._overlayPrimaryBtn.textContent = String(primaryText || 'OK');
      this._overlayPrimaryBtn.hidden = false;
    } catch { /* ignore */ }
    try {
      const showSecondary = !!(secondaryText && String(secondaryText).trim());
      this._overlaySecondaryBtn.textContent = String(secondaryText || '');
      this._overlaySecondaryBtn.hidden = !showSecondary;
    } catch { /* ignore */ }

    setOnboardingOverlayOpen(this._overlayEl, true, this.root);
  }

  hideOverlay() {
    if (!this._overlayEl) return;
    setOnboardingOverlayOpen(this._overlayEl, false, this.root);
    this._overlayOnPrimary = null;
    this._overlayOnSecondary = null;
  }

  _ensure() {
    if (this.root && this.root.isConnected) return;

    // Adopt early-inject shell when present (avoids flicker).
    try {
      const existing = document.querySelector(`.${ONBOARDING_PANEL_CLASS}[data-kp-early-onboarding="true"]`);
      if (existing && existing.isConnected) {
        const refs = queryOnboardingShellRefs(existing);
        if (refs) {
          try {
            existing.style.zIndex = String(Z_INDEX.ONBOARDING_PANEL || ONBOARDING_PANEL_Z_FALLBACK);
          } catch { /* ignore */ }
          applyPopupThemeVars(existing);

          let { closeBtn, prevBtn, nextBtn, resetBtn } = refs;
          closeBtn = stripListeners(closeBtn);
          prevBtn = stripListeners(prevBtn);
          nextBtn = stripListeners(nextBtn);
          resetBtn = stripListeners(resetBtn);

          if (closeBtn) closeBtn.addEventListener('click', this._onCloseClick);
          if (prevBtn) prevBtn.addEventListener('click', this._onPrevClick);
          if (nextBtn) nextBtn.addEventListener('click', this._onNextClick);
          if (resetBtn) resetBtn.addEventListener('click', this._onResetClick);

          this._bindShell({
            root: existing,
            body: refs.body,
            slideSurface: refs.slideSurface,
            titleEl: refs.titleEl,
            stepEl: refs.stepEl,
            closeBtn,
            prevBtn,
            nextBtn,
            resetBtn
          });
          this._ensureOverlay(existing);
          return;
        }
      }
    } catch { /* ignore */ }

    const shell = createOnboardingShell(document, {
      zIndex: Z_INDEX.ONBOARDING_PANEL || ONBOARDING_PANEL_Z_FALLBACK,
      early: false,
      initiallyHidden: true,
      includeViewTransitions: true,
      applyTheme: applyPopupThemeVars,
      navDisabled: false
    });

    shell.prevBtn.addEventListener('click', this._onPrevClick);
    shell.nextBtn.addEventListener('click', this._onNextClick);
    shell.resetBtn.addEventListener('click', this._onResetClick);
    shell.closeBtn.addEventListener('click', this._onCloseClick);

    (document.body || document.documentElement).appendChild(shell.root);
    this._bindShell(shell);
    this._ensureOverlay(shell.root);
  }

  _bindShell(shell) {
    this.root = shell.root;
    this.body = shell.body;
    this.slideSurface = shell.slideSurface;
    this.titleEl = shell.titleEl || null;
    this.stepEl = shell.stepEl || null;
    this.closeBtn = shell.closeBtn || null;
    this.prevBtn = shell.prevBtn || null;
    this.nextBtn = shell.nextBtn || null;
    this.resetBtn = shell.resetBtn || null;
  }

  _ensureOverlay(rootEl) {
    try {
      if (this._overlayEl && this._overlayEl.isConnected) return;

      const bodyHost =
        rootEl?.querySelector?.('[data-kp-onboarding-body="true"]') ||
        rootEl?.querySelector?.(':scope > div[data-kp-onboarding-body]') ||
        null;
      const host = bodyHost || rootEl;
      const overlayRefs = ensureOnboardingOverlay(host);
      if (!overlayRefs) return;

      this._overlayEl = overlayRefs.overlayEl;
      this._overlayTitleEl = overlayRefs.titleEl;
      this._overlayMsgEl = overlayRefs.msgEl;
      this._overlayPrimaryBtn = overlayRefs.primaryBtn;
      this._overlaySecondaryBtn = overlayRefs.secondaryBtn;

      try { this._overlayPrimaryBtn?.removeEventListener?.('click', this._onOverlayPrimary); } catch { /* ignore */ }
      try { this._overlaySecondaryBtn?.removeEventListener?.('click', this._onOverlaySecondary); } catch { /* ignore */ }
      try { this._overlayPrimaryBtn?.addEventListener?.('click', this._onOverlayPrimary); } catch { /* ignore */ }
      try { this._overlaySecondaryBtn?.addEventListener?.('click', this._onOverlaySecondary); } catch { /* ignore */ }

      // Normalize visibility if overlay already existed (hostile pages may override [hidden]).
      try {
        const isHidden = this._overlayEl.hidden === true || this._overlayEl.hasAttribute('hidden');
        setOnboardingOverlayOpen(this._overlayEl, !isHidden, this.root);
      } catch { /* ignore */ }
    } catch {
      // ignore
    }
  }

  _onOverlayPrimary(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    const cb = this._overlayOnPrimary;
    this.hideOverlay();
    try { cb?.(); } catch { /* ignore */ }
  }

  _onOverlaySecondary(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    const cb = this._overlayOnSecondary;
    this.hideOverlay();
    try { cb?.(); } catch { /* ignore */ }
  }

  _onCloseClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch {
      // ignore
    }
    try {
      if (this._onRequestClose) this._onRequestClose();
    } catch (err) {
      console.warn('[KeyPilot Onboarding] Error in onRequestClose:', err);
    }
  }

  _onPrevClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    try {
      if (this._onRequestPrev) this._onRequestPrev();
    } catch { /* ignore */ }
  }

  _onNextClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    try {
      if (this._onRequestNext) this._onRequestNext();
    } catch { /* ignore */ }
  }

  _onResetClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    try {
      const result = this._onRequestReset ? this._onRequestReset() : null;
      if (result && typeof result.then === 'function') {
        result.catch((err) => {
          console.warn('[KeyPilot Onboarding] reset failed:', err);
        });
      }
    } catch (err) {
      console.warn('[KeyPilot Onboarding] reset handler error:', err);
    }
  }

  _onTaskRowClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    try {
      const row = e?.currentTarget || e?.target?.closest?.('[data-kp-onboarding-task-id]');
      if (!row) return;
      if (row.getAttribute('data-kp-onboarding-uncheckable') !== 'true') return;
      const taskId = row.getAttribute('data-kp-onboarding-task-id');
      if (!taskId || !this._onRequestUncheckTask) return;
      this._onRequestUncheckTask(String(taskId));
    } catch { /* ignore */ }
  }

  /**
   * Play the F-key click border effect around the walkthrough panel (marquee/flash/etc).
   * Uses the same CSS classes as OverlayManager so it matches user settings visuals.
   * @param {'marquee'|'flash'|'dash'|'scale'} [effect]
   */
  playBorderEffect(effect = 'marquee') {
    try {
      this._ensure();
      const host = this.root;
      if (!host || !host.isConnected) return;
      const rect = host.getBoundingClientRect();
      if (!rect || rect.width < 2 || rect.height < 2) return;

      const map = {
        marquee: { className: CSS_CLASSES.FOCUS_MARQUEE || 'kpv2-focus-marquee', ms: 1200 },
        flash: { className: CSS_CLASSES.FOCUS_FLASH || 'kpv2-focus-flash', ms: 500 },
        scale: { className: CSS_CLASSES.FOCUS_PULSE || 'kpv2-focus-pulse', ms: 800 },
        dash: { className: CSS_CLASSES.FOCUS_DASH || 'kpv2-focus-dash', ms: 1100 }
      };
      const kind = map[String(effect || 'marquee')] || map.marquee;

      const pulse = document.createElement('div');
      pulse.className = kind.className;
      pulse.setAttribute('aria-hidden', 'true');
      pulse.setAttribute('data-kp-onboarding-border-effect', String(effect || 'marquee'));
      Object.assign(pulse.style, {
        left: `${rect.left}px`,
        top: `${rect.top}px`,
        width: `${rect.width}px`,
        height: `${rect.height}px`,
        borderRadius: '14px',
        zIndex: String((Z_INDEX.ONBOARDING_PANEL || ONBOARDING_PANEL_Z_FALLBACK) + 5),
        pointerEvents: 'none'
      });
      (document.body || document.documentElement).appendChild(pulse);
      window.setTimeout(() => {
        try {
          if (pulse.parentNode) pulse.parentNode.removeChild(pulse);
        } catch { /* ignore */ }
      }, kind.ms + 80);
    } catch {
      // ignore
    }
  }
}



  // Module: src/ui/practice-popover-panel.js
/**
 * PracticePopoverPanel
 * Floating "practice" panel used by onboarding to let users practice text box mode
 * without navigating away from the current page.
 *
 * IMPORTANT: This panel intentionally renders real <input>/<textarea> elements in the
 * top document so focusing them triggers KeyPilot's real text focus mode (so onboarding
 * can detect enter/exit via KeyPilot state).
 */


function clearElement(el) {
  while (el && el.firstChild) el.removeChild(el.firstChild);
}

function clamp(n, min, max) {
  return Math.max(min, Math.min(max, n));
}

class PracticePopoverPanel {
  /**
   * @param {Object} params
   * @param {() => void} [params.onRequestClose]
   */
  constructor({ onRequestClose } = {}) {
    this.root = null;
    this.body = null;
    this._onRequestClose = typeof onRequestClose === 'function' ? onRequestClose : null;
    this._onCloseClick = this._onCloseClick.bind(this);
  }

  isVisible() {
    return !!(this.root && this.root.isConnected && this.root.hidden === false);
  }

  show() {
    if (window !== window.top) return;
    this._ensure();
    this.root.hidden = false;
  }

  hide() {
    if (this.root) this.root.hidden = true;
  }

  /**
   * Position the practice panel directly to the right of the onboarding panel when possible.
   * Falls back to "below" when the viewport is too narrow.
   * @param {HTMLElement|null} onboardingRoot
   */
  positionNextToOnboarding(onboardingRoot) {
    try {
      if (!this.root || !this.root.isConnected) return;
      if (!onboardingRoot || !onboardingRoot.getBoundingClientRect) return;

      const gap = 12;
      const margin = 12;
      const ob = onboardingRoot.getBoundingClientRect();
      const w = window.innerWidth || document.documentElement?.clientWidth || 0;
      const h = window.innerHeight || document.documentElement?.clientHeight || 0;

      const desiredLeft = Math.round(ob.right + gap);
      const desiredTop = Math.round(ob.top);

      // Default size is set in CSS below; read computed width/height for clamping.
      const rect = this.root.getBoundingClientRect();
      const panelW = rect.width || 420;
      const panelH = rect.height || 280;

      const canFitRight = desiredLeft + panelW <= w - margin;

      let left = desiredLeft;
      let top = desiredTop;

      if (!canFitRight) {
        // Place below onboarding instead.
        left = Math.round(ob.left);
        top = Math.round(ob.bottom + gap);
      }

      left = clamp(left, margin, Math.max(margin, w - panelW - margin));
      top = clamp(top, margin, Math.max(margin, h - panelH - margin));

      this.root.style.left = `${left}px`;
      this.root.style.top = `${top}px`;
    } catch {
      // ignore
    }
  }

  render() {
    if (!this.root || this.root.hidden) return;
    if (!this.body) return;

    // IMPORTANT: Do NOT clear/rebuild the DOM on every onboarding render.
    // Onboarding re-renders when KeyPilot enters text focus mode; if we rebuilt here we'd
    // remove the currently focused input, causing an immediate blur → exit text mode,
    // which incorrectly completes the "Press Escape to exit text mode" task.
    try {
      const existing = this.body.querySelector('[data-kp-practice-built="true"]');
      if (existing) return;
    } catch {
      // ignore
    }

    clearElement(this.body);

    const wrap = document.createElement('div');
    wrap.dataset.kpPracticeBuilt = 'true';
    Object.assign(wrap.style, {
      display: 'flex',
      flexDirection: 'column',
      gap: '12px'
    });

    const card = (titleText) => {
      const c = document.createElement('div');
      Object.assign(c.style, {
        padding: '12px',
        borderRadius: '14px',
        border: '1px solid rgba(255,255,255,0.12)',
        background: 'rgba(10, 12, 18, 0.58)',
        boxShadow: '0 10px 26px rgba(0, 0, 0, 0.35)'
      });
      if (titleText) {
        const t = document.createElement('div');
        t.textContent = titleText;
        Object.assign(t.style, { fontSize: '13px', fontWeight: '800', marginBottom: '8px' });
        c.appendChild(t);
      }
      return c;
    };

    const p = (text) => {
      const el = document.createElement('div');
      el.textContent = text;
      Object.assign(el.style, {
        fontSize: '13px',
        lineHeight: '1.5',
        color: 'rgba(255,255,255,0.88)'
      });
      return el;
    };

    const kbd = (text) => {
      const el = document.createElement('span');
      el.textContent = text;
      Object.assign(el.style, {
        fontFamily:
          'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
        fontSize: '12px',
        padding: '2px 6px',
        border: '1px solid rgba(255, 255, 255, 0.18)',
        borderBottomColor: 'rgba(0, 0, 0, 0.6)',
        borderRadius: '6px',
        background: 'rgba(255, 255, 255, 0.08)',
        color: 'rgba(255, 255, 255, 0.95)'
      });
      return el;
    };

    const intro = card('Text Boxes'); // card('What you’re practicing');
    intro.appendChild(
      p('Click a text box with F to enter text box mode (typing works normally). Press `Esc` to exit text box mode and return to normal browsing.')
    );
    const tip = document.createElement('div');
    Object.assign(tip.style, { marginTop: '8px', fontSize: '12px', opacity: '0.82', lineHeight: '1.45' });
    tip.appendChild(document.createTextNode('Tip: while in text mode, you can still hover something and press '));
    tip.appendChild(kbd('F'));
    tip.appendChild(document.createTextNode(' during a short countdown window.'));
    intro.appendChild(tip);
//    wrap.appendChild(intro);

    const fields = card('Text Boxes');
    const grid = document.createElement('div');
    Object.assign(grid.style, {
      display: 'grid',
      gridTemplateColumns: '1fr 1fr',
      gap: '10px'
    });

    const mkLabel = (forId, text) => {
      const l = document.createElement('label');
      l.setAttribute('for', forId);
      l.textContent = text;
      Object.assign(l.style, { display: 'block', fontWeight: '800', fontSize: '12px', margin: '0 0 6px 0' });
      return l;
    };

    const mkInput = (id, placeholder) => {
      const i = document.createElement('input');
      i.id = id;
      i.type = 'text';
      i.placeholder = placeholder;
      Object.assign(i.style, {
        width: '100%',
        boxSizing: 'border-box',
        borderRadius: '12px',
        border: '1px solid rgba(255,255,255,0.16)',
        background: 'rgba(255,255,255,0.06)',
        color: 'rgba(255,255,255,0.95)',
        padding: '10px 12px',
        fontSize: '14px',
        outline: 'none'
      });
      i.addEventListener('focus', () => {
        try {
          i.style.borderColor = 'rgba(255, 140, 0, 0.55)';
          i.style.boxShadow = '0 0 0 3px rgba(255, 140, 0, 0.18)';
        } catch {}
      });
      i.addEventListener('blur', () => {
        try {
          i.style.borderColor = 'rgba(255,255,255,0.16)';
          i.style.boxShadow = 'none';
        } catch {}
      });
      return i;
    };

    const mkTextarea = (id, placeholder) => {
      const t = document.createElement('textarea');
      t.id = id;
      t.placeholder = placeholder;
      Object.assign(t.style, {
        width: '100%',
        boxSizing: 'border-box',
        borderRadius: '12px',
        border: '1px solid rgba(255,255,255,0.16)',
        background: 'rgba(255,255,255,0.06)',
        color: 'rgba(255,255,255,0.95)',
        padding: '10px 12px',
        fontSize: '14px',
        outline: 'none',
        minHeight: '120px',
        resize: 'vertical'
      });
      t.addEventListener('focus', () => {
        try {
          t.style.borderColor = 'rgba(255, 140, 0, 0.55)';
          t.style.boxShadow = '0 0 0 3px rgba(255, 140, 0, 0.18)';
        } catch {}
      });
      t.addEventListener('blur', () => {
        try {
          t.style.borderColor = 'rgba(255,255,255,0.16)';
          t.style.boxShadow = 'none';
        } catch {}
      });
      return t;
    };

    const col1 = document.createElement('div');
    col1.appendChild(mkLabel('kp-practice-input-1', 'Practice input'));
    col1.appendChild(mkInput('kp-practice-input-1', 'Click here with F, then type…'));
    const col2 = document.createElement('div');
    col2.appendChild(mkLabel('kp-practice-input-2', 'Second input (optional)'));
    col2.appendChild(mkInput('kp-practice-input-2', 'Try switching between fields…'));
    grid.appendChild(col1);
    grid.appendChild(col2);
    fields.appendChild(grid);

    const taWrap = document.createElement('div');
    Object.assign(taWrap.style, { marginTop: '10px' });
    taWrap.appendChild(mkLabel('kp-practice-textarea', 'Practice textarea'));
    taWrap.appendChild(mkTextarea('kp-practice-textarea', 'Type a few lines here…'));
    fields.appendChild(taWrap);

    const note = document.createElement('div');
    Object.assign(note.style, { marginTop: '10px', fontSize: '12px', opacity: '0.84', lineHeight: '1.45' });
    note.appendChild(document.createTextNode('When you’re done typing, press '));
    note.appendChild(kbd('Esc'));
    note.appendChild(document.createTextNode(' to exit text box mode.'));
    fields.appendChild(note);

    wrap.appendChild(fields);

    const clickWin = card('Step 2: Practice the “hover + F click” window');
    const descr = document.createElement('div');
    Object.assign(descr.style, { fontSize: '13px', lineHeight: '1.5', color: 'rgba(255,255,255,0.88)' });
    descr.appendChild(document.createTextNode('While still in text mode, hover this link or button and press '));
    descr.appendChild(kbd('F'));
    descr.appendChild(document.createTextNode(' during the short countdown window:'));
    clickWin.appendChild(descr);

    const link = document.createElement('a');
    link.href = '#kp-practice-anchor';
    link.textContent = 'Practice link (jumps in this panel)';
    link.id = 'kp-practice-link';
    Object.assign(link.style, { color: 'rgba(91, 226, 241, 0.95)', textDecoration: 'underline', display: 'inline-block', marginTop: '10px' });
    clickWin.appendChild(link);

    const btn = document.createElement('button');
    btn.type = 'button';
    btn.textContent = 'Practice button (no navigation)';
    Object.assign(btn.style, {
      display: 'inline-flex',
      alignItems: 'center',
      justifyContent: 'center',
      padding: '10px 12px',
      borderRadius: '12px',
      border: '1px solid rgba(255, 255, 255, 0.16)',
      background: 'rgba(255, 255, 255, 0.08)',
      color: 'rgba(255, 255, 255, 0.92)',
      cursor: 'pointer',
      fontWeight: '800',
      marginTop: '10px'
    });
    btn.addEventListener('click', () => {
      try {
        btn.textContent = 'Clicked!';
        setTimeout(() => {
          btn.textContent = 'Practice button (no navigation)';
        }, 900);
      } catch { /* ignore */ }
    });
    clickWin.appendChild(btn);

    const anchor = document.createElement('div');
    anchor.id = 'kp-practice-anchor';
    anchor.textContent = 'Anchor reached. Press Esc to exit text mode if needed.';
    Object.assign(anchor.style, { marginTop: '10px', fontSize: '12px', opacity: '0.84', lineHeight: '1.45' });
    clickWin.appendChild(anchor);

    //wrap.appendChild(clickWin);

    this.body.appendChild(wrap);
  }

  _ensure() {
    if (this.root && this.root.isConnected) return;

    const root = document.createElement('div');
    root.className = 'kp-practice-popover';
    root.dataset.kpPracticePopover = 'true';
    root.hidden = true;
    root.setAttribute('role', 'dialog');
    root.setAttribute('aria-label', 'KeyPilot practice popover');

    Object.assign(root.style, {
      position: 'fixed',
      left: '392px', // will be repositioned next to onboarding
      top: '16px',
      width: '420px',
      maxWidth: 'calc(100vw - 24px)',
      // Fit content — do not force a tall frame around the Text Boxes card.
      height: 'auto',
      maxHeight: 'calc(100vh - 24px)',
      overflow: 'auto',
      zIndex: String((Z_INDEX.ONBOARDING_PANEL || 2147483045) - 1),
      background: 'rgba(18, 18, 18, 0.94)',
      color: 'rgba(255,255,255,0.95)',
      border: '1px solid rgba(255,255,255,0.12)',
      borderRadius: '14px',
      boxShadow: '0 12px 34px rgba(0,0,0,0.45)',
      fontFamily: 'system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif',
      pointerEvents: 'auto'
    });

    applyPopupThemeVars(root);

    const header = document.createElement('div');
    Object.assign(header.style, {
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
      gap: '10px',
      padding: '10px 12px',
      borderBottom: '1px solid rgba(255,255,255,0.10)'
    });

    const title = document.createElement('div');
    title.textContent = 'Entering Text';
    Object.assign(title.style, {
      fontSize: '13px',
      fontWeight: '900',
      letterSpacing: '0.2px',
      whiteSpace: 'nowrap',
      overflow: 'hidden',
      textOverflow: 'ellipsis'
    });

    const closeBtn = document.createElement('button');
    closeBtn.type = 'button';
    closeBtn.textContent = '×';
    closeBtn.setAttribute('aria-label', 'Hide practice popover');
    Object.assign(closeBtn.style, {
      width: '30px',
      height: '30px',
      borderRadius: '10px',
      border: '1px solid rgba(255,255,255,0.18)',
      background: 'rgba(255,255,255,0.06)',
      color: 'rgba(255,255,255,0.95)',
      cursor: 'pointer',
      fontSize: '18px',
      lineHeight: '28px',
      padding: '0',
      flex: '0 0 auto'
    });
    closeBtn.addEventListener('click', this._onCloseClick);

    header.appendChild(title);
    header.appendChild(closeBtn);

    const body = document.createElement('div');
    Object.assign(body.style, { padding: '12px' });

    root.appendChild(header);
    root.appendChild(body);

    (document.body || document.documentElement).appendChild(root);

    this.root = root;
    this.body = body;
  }

  _onCloseClick(e) {
    try {
      e.preventDefault();
      e.stopPropagation();
    } catch { /* ignore */ }
    try {
      this.hide();
      if (this._onRequestClose) this._onRequestClose();
    } catch { /* ignore */ }
  }
}





  // Module: src/modules/onboarding-manager.js
/**
 * OnboardingManager
 * - Loads onboarding slides/tasks from XML
 * - Persists progress globally (profile-wide) via chrome.storage
 * - Renders a floating panel (top-left) and auto-checks tasks based on KeyPilot actions/mode changes
 */





// NOTE: Do not `import { X as Y }` — build.js strips imports and aliases are lost.
// Use ONBOARDING_STORAGE_KEYS by name (defined in onboarding-shared.js).

const TRANSIENT_KEYS = {
  LAST_ACTION: 'keypilot_transient_action'
};

function safeBool(v) {
  return typeof v === 'boolean' ? v : null;
}

/**
 * Read onboarding keys from sync + local.
 * For PROGRESS, prefer the newer timestamp when both areas have a value so a
 * successful local-only reset cannot be masked by stale sync data (and vice versa).
 * @param {string[]} keys
 */
async function storageGet(keys) {
  const list = Array.isArray(keys) ? keys.filter((k) => typeof k === 'string' && k) : [];
  if (!list.length) return {};

  /** @type {Record<string, any>} */
  let sync = {};
  /** @type {Record<string, any>} */
  let local = {};

  try {
    if (chrome?.storage?.sync?.get) {
      sync = (await chrome.storage.sync.get(list)) || {};
    }
  } catch {
    sync = {};
  }
  try {
    if (chrome?.storage?.local?.get) {
      local = (await chrome.storage.local.get(list)) || {};
    }
  } catch {
    local = {};
  }

  /** @type {Record<string, any>} */
  const out = {};
  for (const key of list) {
    const hasSync = Object.prototype.hasOwnProperty.call(sync, key) && sync[key] !== undefined;
    const hasLocal = Object.prototype.hasOwnProperty.call(local, key) && local[key] !== undefined;

    if (key === ONBOARDING_STORAGE_KEYS.PROGRESS && hasSync && hasLocal) {
      const s = sync[key];
      const l = local[key];
      const sTs = s && typeof s === 'object' && typeof s.timestamp === 'number' ? s.timestamp : 0;
      const lTs = l && typeof l === 'object' && typeof l.timestamp === 'number' ? l.timestamp : 0;
      out[key] = lTs > sTs ? l : s;
      continue;
    }

    if (hasSync) out[key] = sync[key];
    else if (hasLocal) out[key] = local[key];
  }
  return out;
}

async function storageGetTransient() {
  // Prefer session (ephemeral + fast), fall back to local.
  try {
    if (chrome?.storage?.session?.get) {
      const r = await chrome.storage.session.get([TRANSIENT_KEYS.LAST_ACTION]);
      return r || {};
    }
  } catch {
    // ignore
  }
  try {
    const r = await chrome.storage.local.get([TRANSIENT_KEYS.LAST_ACTION]);
    return r || {};
  } catch {
    return {};
  }
}

async function storageRemoveTransient() {
  try {
    if (chrome?.storage?.session?.remove) {
      await chrome.storage.session.remove([TRANSIENT_KEYS.LAST_ACTION]);
      return;
    }
  } catch {
    // ignore
  }
  try {
    await chrome.storage.local.remove([TRANSIENT_KEYS.LAST_ACTION]);
  } catch {
    // ignore
  }
}

/**
 * Write onboarding keys to both sync and local.
 * Preferring sync-only caused reset/progress races: a failed or partial sync write left
 * local with a newer snapshot that later reads could miss (or vice versa).
 * @param {Record<string, any>} obj
 */
async function storageSet(obj) {
  if (!obj || typeof obj !== 'object') return false;
  let ok = false;
  try {
    if (chrome?.storage?.sync?.set) {
      await chrome.storage.sync.set(obj);
      ok = true;
    }
  } catch {
    // continue; still try local so both areas can converge
  }
  try {
    if (chrome?.storage?.local?.set) {
      await chrome.storage.local.set(obj);
      ok = true;
    }
  } catch {
    // ignore
  }
  // Fallback to shared helper if neither area accepted the direct write.
  if (!ok) {
    ok = await storageSetObject(obj);
  }
  return ok;
}

function withViewTransition(updateDomFn) {
  try {
    if (document && typeof document.startViewTransition === 'function') {
      document.startViewTransition(() => {
        updateDomFn();
      });
      return;
    }
  } catch {
    // ignore, fall back
  }
  updateDomFn();
}

function parseOnboardingXml(xmlText) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(String(xmlText || ''), 'application/xml');
  const parseError = doc.querySelector('parsererror');
  if (parseError) {
    throw new Error('Failed to parse onboarding.xml');
  }

  const slides = [];
  const slideEls = doc.querySelectorAll('onboarding > slide');
  for (const slideEl of slideEls) {
    const id = slideEl.getAttribute('id') || '';
    const title = slideEl.getAttribute('title') || '';
    if (!id) continue;

    const bodyEl = slideEl.querySelector(':scope > body');
    const bodyText = bodyEl ? String(bodyEl.textContent || '').trim() : '';

    const onEnter = [];
    const onEnterEls = slideEl.querySelectorAll(':scope > onEnter');
    for (const oe of onEnterEls) {
      const type = (oe.getAttribute('type') || '').trim();
      if (!type) continue;
      const entry = { type };
      for (const attr of oe.attributes || []) {
        if (!attr || !attr.name) continue;
        if (attr.name === 'type') continue;
        entry[attr.name] = attr.value;
      }
      onEnter.push(entry);
    }

    const tasks = [];
    const taskEls = slideEl.querySelectorAll(':scope > task');
    for (const taskEl of taskEls) {
      const taskId = taskEl.getAttribute('id') || '';
      const label = taskEl.getAttribute('label') || '';
      if (!taskId) continue;

      const whenEl = taskEl.querySelector(':scope > when');
      const when = whenEl
        ? {
            type: (whenEl.getAttribute('type') || '').trim(),
            action: (whenEl.getAttribute('action') || '').trim(),
            target: (whenEl.getAttribute('target') || '').trim(),
            mode: (whenEl.getAttribute('mode') || '').trim(),
            change: (whenEl.getAttribute('change') || '').trim()
          }
        : { type: '' };

      tasks.push({ id: taskId, label, when });
    }

    slides.push({ id, title, tasks, onEnter, bodyText });
  }

  return { slides };
}

class OnboardingManager {
  /**
   * @param {Object} [params]
   * @param {(type:string, detail:Object) => void} [params.onEvent] Optional callback hook for onboarding lifecycle events.
   */
  constructor({ onEvent } = {}) {
    this.panel = new OnboardingPanel({
      onRequestClose: () => this.setActive(false),
      onRequestPrev: () => this.goPrevSlide(),
      onRequestNext: () => this.goNextSlide(),
      onRequestReset: () => this.resetTutorial(),
      onRequestUncheckTask: (taskId) => this.uncheckLastTask(taskId)
    });

    this.practicePanel = new PracticePopoverPanel({
      onRequestClose: () => {
        // Practice popover is auxiliary; hiding it should not disable onboarding.
        this._practiceDismissed = true;
      }
    });

    this.model = { slides: [] };
    this.progress = createEmptyProgress(null);

    this.active = false;
    this._bound = false;
    this._keyPilotReady = false;
    this._unsubscribeState = null;
    this._prevMode = null;
    this._practiceDismissed = false;
    this._practiceLastSlideId = null;

    this._onActionEvent = this._onActionEvent.bind(this);
    this._onDocKeydownCapture = this._onDocKeydownCapture.bind(this);
    this._onPageShow = this._onPageShow.bind(this);
    this._pageShowBound = false;

    // Event hooks
    this._onEvent = typeof onEvent === 'function' ? onEvent : null;
    this._listeners = new Map(); // eventType -> Set<fn>

    // Track transitions
    this._lastRenderedSlideId = null;
    this._lastRenderedSlideIndex = null;
    this._lastAction = null;
    this._isTransitioning = false;

    // Coalesced persistence: rapid task updates + Reset must not race.
    // Fire-and-forget _persist() used to capture a live progress object that a later
    // resetTutorial() replaced — the late write then restored stale completed tasks.
    this._persistDirty = false;
    this._persistChain = null;
    this._persistEpoch = 0;
    this._applyingRemoteProgress = false;

    // Cross-tab progress sync + transient recovery when tab becomes visible.
    this._storageListenerBound = false;
    this._storageChangeTimer = null;
    this._onStorageChanged = this._onStorageChanged.bind(this);
    this._onVisibilityChange = this._onVisibilityChange.bind(this);
    this._visibilityBound = false;

    // Cached "enabled" state to avoid startup races.
    // IMPORTANT: treat unknown (null) as disabled so we never show onboarding when KeyPilot is OFF.
    this._enabledCache = null; // boolean|null
    this._enabledCacheTs = 0;
  }

  /**
   * Subscribe to onboarding events.
   * Events are also dispatched globally as `document` CustomEvents: `keypilot:onboarding`.
   *
   * @param {string} type
   * @param {(detail:Object) => void} handler
   * @returns {() => void} unsubscribe
   */
  on(type, handler) {
    if (!type || typeof handler !== 'function') return () => {};
    const key = String(type);
    const set = this._listeners.get(key) || new Set();
    set.add(handler);
    this._listeners.set(key, set);
    return () => {
      try { set.delete(handler); } catch { /* ignore */ }
    };
  }

  _emit(type, detail = {}) {
    const t = String(type || '');
    if (!t) return;

    const payload = { type: t, ...(detail && typeof detail === 'object' ? detail : {}) };

    // 1) Optional direct callback
    try { this._onEvent?.(t, payload); } catch { /* ignore */ }

    // 2) Instance listeners
    try {
      const set = this._listeners.get(t);
      if (set && set.size) {
        for (const fn of set) {
          try { fn(payload); } catch { /* ignore */ }
        }
      }
    } catch { /* ignore */ }

    // 3) Global DOM event hook
    try {
      document.dispatchEvent(new CustomEvent('keypilot:onboarding', { detail: payload }));
    } catch { /* ignore */ }
  }

  /**
   * Clear all walkthrough progress and return to the first slide.
   * Safe to call while other task-completion persists are in flight.
   */
  /**
   * Undo only the most recently completed task (user clicked that checklist row).
   * @param {string} taskId
   */
  uncheckLastTask(taskId) {
    if (!this._isKeyPilotEnabled() || !this.active || this.progress.completed) return;
    const id = String(taskId || '');
    if (!id) return;
    const ids = Array.isArray(this.progress.completedTaskIds)
      ? this.progress.completedTaskIds.map(String)
      : [];
    if (!ids.length || ids[ids.length - 1] !== id) return;
    this.progress.completedTaskIds = ids.slice(0, -1);
    // Allow onEnter effects (overlay/marquee) to re-fire if undoing back into a prior slide context
    // is not needed; only uncheck the task.
    this._persist();
    this._render({ reason: 'uncheck', forceRebuild: true });
  }

  async resetTutorial() {
    if (!this._isKeyPilotEnabled()) return;

    const firstSlideId = this.model.slides[0]?.id || null;
    // Bump epoch first so any in-flight remote apply / stale persist is ignored.
    this._persistEpoch += 1;
    this.progress = createEmptyProgress(firstSlideId);
    this.active = true;
    this._practiceDismissed = false;
    this._practiceLastSlideId = null;
    this._lastRenderedSlideId = null;
    this._lastRenderedSlideIndex = null;
    this._isTransitioning = false;
    this._lastAction = null;

    try { this.panel.hideOverlay(); } catch { /* ignore */ }
    try { this.practicePanel.hide(); } catch { /* ignore */ }

    await this._persist();
    this._isTransitioning = true;
    // forceRebuild: always rebuild checklist rows so completed checkmarks cannot stick
    // via in-place DOM update when remaining on the same slide.
    this._render({
      transition: { type: 'slide', dir: -1 },
      reason: 'reset',
      forceRebuild: true
    });
    this._emit('tutorialReset', {
      slideId: firstSlideId,
      timestamp: this.progress.timestamp
    });
  }

  async goPrevSlide() {
    if (!this._isKeyPilotEnabled() || this._isTransitioning) return;
    const idx = this._currentSlideIndex();
    if (idx <= 0) return;
    const prev = this.model.slides[idx - 1];
    if (!prev) return;
    this.progress.slideId = prev.id;
    await this._persist();
    this._isTransitioning = true;
    this._render({ transition: { type: 'slide', dir: -1 }, reason: 'manualPrev' });
  }

  async goNextSlide() {
    if (!this._isKeyPilotEnabled() || this._isTransitioning) return;
    const idx = this._currentSlideIndex();
    const next = this.model.slides[idx + 1];
    if (!next) return;
    this.progress.slideId = next.id;
    await this._persist();
    this._isTransitioning = true;
    this._render({ transition: { type: 'slide', dir: 1 }, reason: 'manualNext' });
  }

  _readEnabledFromGlobals() {
    // Prefer the toggle handler's authoritative state.
    try {
      const th = window.__KeyPilotToggleHandler;
      if (th && typeof th.enabled === 'boolean') return th.enabled === true;
    } catch { /* ignore */ }

    // Fall back to KeyPilot instance state ONLY once initialization is complete.
    // During startup `kp.enabled` briefly defaults to true before KP_GET_STATE resolves.
    try {
      const kp = window.__KeyPilotInstance;
      if (kp && kp.initializationComplete === true && typeof kp.enabled === 'boolean') {
        return kp.enabled === true;
      }
    } catch { /* ignore */ }

    return null;
  }

  async _syncEnabledFromServiceWorker() {
    // The service worker is the source of truth for global enable/disable.
    try {
      const resp = await chrome.runtime.sendMessage({ type: 'KP_GET_STATE' });
      if (resp && typeof resp.enabled === 'boolean') {
        this._enabledCache = resp.enabled === true;
        this._enabledCacheTs = Date.now();
        return this._enabledCache;
      }
    } catch {
      // ignore and fall back
    }

    const g = this._readEnabledFromGlobals();
    if (typeof g === 'boolean') {
      this._enabledCache = g === true;
      this._enabledCacheTs = Date.now();
      return this._enabledCache;
    }

    // Conservative default: if we can't confirm enabled, treat it as disabled.
    this._enabledCache = false;
    this._enabledCacheTs = Date.now();
    return this._enabledCache;
  }

  _isKeyPilotEnabled() {
    // When KeyPilot is toggled off, the content script may still exist but should not
    // keep extra hotkey listeners alive. Alt+K is handled separately.
    // Prefer the toggle handler's authoritative state even if our cache is stale.
    // This prevents hotkeys from being captured when the extension has been toggled OFF.
    const g = this._readEnabledFromGlobals();
    if (typeof g === 'boolean') return g === true;

    if (typeof this._enabledCache === 'boolean') return this._enabledCache === true;
    return false;
  }

  _setAltSlashListenerEnabled(enabled) {
    const next = !!enabled;
    try {
      if (next) {
        if (this._altSlashListenerInstalled) return;
        document.addEventListener('keydown', this._onDocKeydownCapture, true);
        this._altSlashListenerInstalled = true;
      } else {
        if (!this._altSlashListenerInstalled) return;
        document.removeEventListener('keydown', this._onDocKeydownCapture, true);
        this._altSlashListenerInstalled = false;
      }
    } catch {
      // ignore
    }
  }

  async init() {
    // Never run inside iframes (keeps onboarding at the top level only).
    if (window !== window.top) return;
    if (this._bound) return;
    this._bound = true;

    await this._loadModel();
    await this._loadProgress();

    // Ensure we have a valid initial slide.
    if (!this.progress.slideId) {
      this.progress.slideId = this.model.slides[0]?.id || null;
    }

    // If a previous page navigated away too quickly to persist onboarding progress,
    // recover "transient" actions recorded by the service worker (most importantly: back).
    await this._applyTransientActionHeuristicIfNeeded();

    // If this page load came from a back/forward navigation, mark the "back" task as done.
    // This covers cases where the user used the browser back button OR navigation happened too fast
    // for an in-page storage write to complete.
    await this._applyBackForwardHeuristicIfNeeded();

    // BFCache restores do not rerun the content script; apply transient recovery on pageshow.
    if (!this._pageShowBound) {
      this._pageShowBound = true;
      try {
        window.addEventListener('pageshow', this._onPageShow, true);
      } catch {
        // ignore
      }
    }

    // Keep onboarding state consistent across tabs/windows.
    this._bindStorageSync();

    // When the document becomes visible (user tab-switches), apply transient recovery.
    if (!this._visibilityBound) {
      this._visibilityBound = true;
      try {
        document.addEventListener('visibilitychange', this._onVisibilityChange, true);
      } catch {
        // ignore
      }
    }

    // Wire event listeners first (so actions right after load count).
    try {
      document.addEventListener('keypilot:action', this._onActionEvent, true);
    } catch {
      // ignore
    }

    // Prime enabled state from the service worker before we decide whether to show anything.
    await this._syncEnabledFromServiceWorker();

    // Alt + / re-opens onboarding, but ONLY while KeyPilot is enabled.
    this._setAltSlashListenerEnabled(this._isKeyPilotEnabled());

    // Show/hide based on persisted active flag.
    this._render();

    // Bind to KeyPilot state (for mode enter/exit tasks).
    this._attachToKeyPilotStateSoon();
  }

  _onPageShow(ev) {
    try {
      if (!ev || ev.persisted !== true) return;
      if (!this.active || this.progress.completed) return;
      this._applyTransientActionHeuristicIfNeeded()
        .then(() => {
          withViewTransition(() => this._render());
        })
        .catch(() => {});
    } catch {
      // ignore
    }
  }

  async _applyTransientActionHeuristicIfNeeded() {
    try {
      if (!this.active || this.progress.completed) return;

      const data = await storageGetTransient();
      const rec = data && data[TRANSIENT_KEYS.LAST_ACTION];
      if (!rec || typeof rec !== 'object') return;

      const action = typeof rec.action === 'string' ? rec.action : '';
      const ts = typeof rec.timestamp === 'number' ? rec.timestamp : 0;
      if (!action) return;

      // Expire old records to avoid false positives.
      const now = Date.now();
      if (!ts || now - ts > 15000) {
        await storageRemoveTransient();
        return;
      }

      // Only handle actions onboarding cares about currently.
      // These are the actions that commonly happen concurrently with navigation/tab switching.
      const handled = new Set(['back', 'newTab', 'tabLeft', 'tabRight']);
      if (!handled.has(action)) {
        await storageRemoveTransient();
        return;
      }

      const slide = this._getCurrentSlide();
      if (!slide) return;

      const completed = new Set(this.progress.completedTaskIds);
      let changed = false;

      for (const task of slide.tasks || []) {
        if (!task || !task.id || completed.has(task.id)) continue;
        if (this._taskMatches(task, { type: 'action', action, detail: {} })) {
          completed.add(task.id);
          changed = true;
        }
      }

      // Consume it regardless; we only want it to apply once.
      await storageRemoveTransient();

      if (!changed) return;
      this.progress.completedTaskIds = Array.from(completed);
      await this._persist();

      // IMPORTANT: If a slide becomes complete via transient recovery (common for tab switches),
      // we still need to auto-advance just like we do in the live `keypilot:action` handler.
      const slideComplete = this._isSlideComplete(slide, completed);
      if (slideComplete) {
        this._handleSlideCompleted(slide, { cause: `transient:${action}`, completedTaskIds: Array.from(completed) });
        await this._advanceSlide({ cause: `transient:${action}` });
      }
    } catch {
      // ignore
    }
  }

  _bindStorageSync() {
    if (this._storageListenerBound) return;
    this._storageListenerBound = true;
    try {
      chrome.storage.onChanged.addListener(this._onStorageChanged);
    } catch {
      // ignore
    }
  }

  _onStorageChanged(changes, areaName) {
    try {
      if (!changes || typeof changes !== 'object') return;
      if (!areaName || (areaName !== 'sync' && areaName !== 'local' && areaName !== 'session')) return;
      // Ignore echo from our own coalesced persist loop.
      if (this._applyingRemoteProgress) return;
      if (this._persistDirty || this._persistChain) return;

      const relevant =
        Object.prototype.hasOwnProperty.call(changes, ONBOARDING_STORAGE_KEYS.ACTIVE) ||
        Object.prototype.hasOwnProperty.call(changes, ONBOARDING_STORAGE_KEYS.PROGRESS);
      if (!relevant) return;

      // Debounce: multiple writes can come in quick bursts (sync + local dual-write).
      try { if (this._storageChangeTimer) clearTimeout(this._storageChangeTimer); } catch { /* ignore */ }
      const epochAtSchedule = this._persistEpoch;
      this._storageChangeTimer = setTimeout(() => {
        // A local reset/navigation happened after this event was scheduled.
        if (epochAtSchedule !== this._persistEpoch) return;
        if (this._persistDirty || this._persistChain) return;
        this._loadProgress({ preferRemote: true })
          .then((changed) => {
            if (epochAtSchedule !== this._persistEpoch) return;
            if (changed === false) return;
            withViewTransition(() => this._render({ reason: 'storageChanged' }));
          })
          .catch(() => {});
      }, 50);
    } catch {
      // ignore
    }
  }

  _onVisibilityChange() {
    try {
      if (document.visibilityState !== 'visible') return;
      if (!this.active || this.progress.completed) return;
      this._applyTransientActionHeuristicIfNeeded()
        .then(() => {
          withViewTransition(() => this._render({ reason: 'visibility' }));
        })
        .catch(() => {});
    } catch {
      // ignore
    }
  }

  async _applyBackForwardHeuristicIfNeeded() {
    try {
      // Only relevant for onboarding being active/incomplete.
      if (!this.active || this.progress.completed) return;

      const navEntries = (typeof performance !== 'undefined' && performance.getEntriesByType)
        ? performance.getEntriesByType('navigation')
        : [];
      const nav = navEntries && navEntries[0];
      const navType = nav && typeof nav.type === 'string' ? nav.type : '';
      if (navType !== 'back_forward') return;

      const slide = this._getCurrentSlide();
      if (!slide) return;

      const completed = new Set(this.progress.completedTaskIds);
      let changed = false;

      for (const task of slide.tasks || []) {
        if (!task || !task.id || completed.has(task.id)) continue;
        if (this._taskMatches(task, { type: 'action', action: 'back', detail: {} })) {
          completed.add(task.id);
          changed = true;
        }
      }

      if (!changed) return;
      this.progress.completedTaskIds = Array.from(completed);
      await this._persist();

      // Mirror auto-advance behavior for BFCache/back-forward recovery.
      const slideComplete = this._isSlideComplete(slide, completed);
      if (slideComplete) {
        this._handleSlideCompleted(slide, { cause: 'back_forward', completedTaskIds: Array.from(completed) });
        await this._advanceSlide({ cause: 'back_forward' });
      }
    } catch {
      // ignore
    }
  }

  async _loadModel() {
    const url = chrome.runtime.getURL('pages/onboarding.xml');
    const res = await fetch(url);
    const text = await res.text();
    this.model = parseOnboardingXml(text);
  }

  /**
   * Load active flag + progress from storage.
   * @param {{preferRemote?: boolean}} [opts]
   * @returns {Promise<boolean>} true when in-memory state was updated
   */
  async _loadProgress(opts = {}) {
    const preferRemote = opts.preferRemote === true;
    const data = await storageGet([ONBOARDING_STORAGE_KEYS.ACTIVE, ONBOARDING_STORAGE_KEYS.PROGRESS]);

    const active = safeBool(data[ONBOARDING_STORAGE_KEYS.ACTIVE]);
    let changed = false;

    if (active !== null && active !== this.active) {
      this.active = active;
      changed = true;
    } else if (active === null && !preferRemote) {
      // First load: default inactive when unset.
      if (this.active !== false) {
        this.active = false;
        changed = true;
      }
    }

    const p = data[ONBOARDING_STORAGE_KEYS.PROGRESS];
    if (p && typeof p === 'object') {
      const remote = cloneProgress(p);
      const localTs = typeof this.progress?.timestamp === 'number' ? this.progress.timestamp : 0;
      const remoteTs = remote.timestamp || 0;

      // Drop strictly older snapshots (stale dual-area / late writes).
      // Equal timestamps still apply so a cold load can hydrate.
      if (preferRemote && remoteTs && localTs && remoteTs < localTs) {
        return changed;
      }

      if (!progressEqual(this.progress, remote)) {
        this.progress = remote;
        changed = true;
      }
    }
    return changed;
  }

  /**
   * Persist active + progress. Coalesces concurrent callers so the last
   * in-memory state always wins (critical for Reset vs task-complete races).
   * @returns {Promise<void>}
   */
  async _persist() {
    this._persistDirty = true;
    if (!this._persistChain) {
      this._persistChain = this._drainPersistQueue().finally(() => {
        this._persistChain = null;
      });
    }
    await this._persistChain;
  }

  async _drainPersistQueue() {
    while (this._persistDirty) {
      this._persistDirty = false;
      // Snapshot at write time (not at schedule time) so superseded states are skipped.
      const payload = {
        [ONBOARDING_STORAGE_KEYS.ACTIVE]: this.active,
        [ONBOARDING_STORAGE_KEYS.PROGRESS]: cloneProgress({
          ...this.progress,
          timestamp: Date.now()
        })
      };
      this.progress.timestamp = payload[ONBOARDING_STORAGE_KEYS.PROGRESS].timestamp;
      this._applyingRemoteProgress = true;
      try {
        await storageSet(payload);
      } finally {
        // Allow storage echo handlers after a tick so dual-area onChanged can settle.
        try {
          setTimeout(() => {
            this._applyingRemoteProgress = false;
          }, 0);
        } catch {
          this._applyingRemoteProgress = false;
        }
      }
    }
  }

  async setActive(active) {
    const next = !!active;
    // Don't allow Alt+/ (or other triggers) to reopen onboarding while KeyPilot is disabled.
    if (next && !this._isKeyPilotEnabled()) {
      this.panel.hide();
      this.practicePanel.hide();
      this._isTransitioning = false;
      return;
    }
    if (this.active === next) {
      // Even if state hasn't changed, ensure UI is in sync (e.g., if panel visibility got out of sync)
      if (next) this._render();
      else {
        this.hideReEnableTip();
        this.panel.hide();
        this.practicePanel.hide();
        await this._persist();
      }
      return;
    }
    this.active = next;
    this._isTransitioning = false;
    if (!next) this.hideReEnableTip();
    await this._persist();
    this._render();
  }

  _currentSlideIndex() {
    const idx = this.model.slides.findIndex((s) => s.id === this.progress.slideId);
    return idx >= 0 ? idx : 0;
  }

  _getCurrentSlide() {
    const idx = this._currentSlideIndex();
    return this.model.slides[idx] || null;
  }

  /**
   * @param {Object} [opts]
   * @param {{type:'slide', dir:1|-1}|null} [opts.transition]
   * @param {string} [opts.reason]
   * @param {boolean} [opts.forceRebuild] Force checklist DOM rebuild (e.g. after Reset)
   */
  _render(opts = {}) {
    const slide = this._getCurrentSlide();
    const total = this.model.slides.length;
    const index = this._currentSlideIndex();

    // Never show onboarding UI while KeyPilot is disabled.
    if (!this._isKeyPilotEnabled()) {
      this.panel.hide();
      this.practicePanel.hide();
      return;
    }

    if (!this.active || this.progress.completed || !slide) {
      this.panel.hide();
      this.practicePanel.hide();
      return;
    }

    // If the off-step is still open while KeyPilot is already disabled, complete it
    // so the user can focus on turning KeyPilot back on.
    try { this._autoCompleteToggleOffIfAlreadyDisabled(); } catch { /* ignore */ }

    this.panel.show();
    // Determine whether this render is a slide transition (for animation + hooks).
    const isSlideChange =
      this._lastRenderedSlideId !== null &&
      String(this._lastRenderedSlideId) !== String(slide.id);

    /** @type {{type:'slide', dir:1|-1}|null} */
    const transition =
      opts && opts.transition
        ? opts.transition
        : (isSlideChange ? { type: 'slide', dir: index > (this._lastRenderedSlideIndex || 0) ? 1 : -1 } : null);

    const reason = opts && opts.reason ? String(opts.reason) : 'render';
    const forceRebuild = !!(opts && opts.forceRebuild) || reason === 'reset';

    const fromSlideId = this._lastRenderedSlideId;

    // If an overlay was shown on the prior slide (e.g. via onEnter), ensure it doesn't
    // persist into the next slide when the user manually navigates or resets.
    if (
      reason === 'reset' ||
      (transition && transition.type === 'slide' && fromSlideId !== null && String(fromSlideId) !== String(slide.id))
    ) {
      try { this.panel.hideOverlay(); } catch { /* ignore */ }
    }

    if (transition && transition.type === 'slide') {
      this._emit('slideTransitionStart', {
        fromSlideId,
        toSlideId: slide.id,
        dir: transition.dir,
        reason
      });
    }

    const tasksForUi = (slide.tasks || []).map((t) => ({
      ...t,
      label: this._resolveTaskLabel(t)
    }));

    const completedList = Array.isArray(this.progress.completedTaskIds)
      ? this.progress.completedTaskIds.map(String)
      : [];
    // Only the most recently completed task may be unchecked by clicking it.
    const lastCompletedTaskId = completedList.length ? completedList[completedList.length - 1] : null;

    const renderPromise = this.panel.render({
      title: slide.title || 'Welcome to KeyPilot',
      bodyText: slide.bodyText || '',
      slideId: slide.id,
      slideIndex: index,
      slideCount: total,
      tasks: tasksForUi,
      completedTaskIds: new Set(completedList),
      lastCompletedTaskId,
      // Hide the Alt+/ tip on practice, scrolling, tabs, and completion slides.
      showTip: slide.id !== 'text_box_mode' &&
        slide.id !== 'scrolling' &&
        slide.id !== 'tabs' &&
        slide.id !== 'completion',
      transition,
      forceRebuild
    });

    // Transition end hook (best-effort).
    if (transition && transition.type === 'slide') {
      Promise.resolve(renderPromise)
        .then(() => {
          this._isTransitioning = false;
          this._emit('slideTransitionEnd', {
            fromSlideId,
            toSlideId: slide.id,
            dir: transition.dir,
            reason
          });
        })
        .catch(() => {
          this._isTransitioning = false;
        });
    } else {
      // No transition, so we're not transitioning
      this._isTransitioning = false;
    }

    this._lastRenderedSlideId = slide.id;
    this._lastRenderedSlideIndex = index;

    // Reset practice dismissal when entering a new slide.
    if (this._practiceLastSlideId !== slide.id) {
      this._practiceLastSlideId = slide.id;
      this._practiceDismissed = false;
    }

    // Practice popover: show only on the text box mode slide, and hide otherwise.
    const shouldShowPractice = slide.id === 'text_box_mode' && !this._practiceDismissed;
    if (shouldShowPractice) {
      const wasVisible = this.practicePanel.isVisible();

      this.practicePanel.show();
      this.practicePanel.render();
      this.practicePanel.positionNextToOnboarding(this.panel.root);

      // IMPORTANT: Only do the "start fresh" text-mode exit when the practice popover
      // FIRST appears. If we do this on every render, it will immediately cancel text mode
      // the moment the user focuses an input inside the practice panel.
      if (!wasVisible) {
        try {
          const kp = window.__KeyPilotInstance;
          const st = kp?.state?.getState?.();
          if (st?.mode === MODES.TEXT_FOCUS && typeof kp.handleEscapeFromTextFocus === 'function') {
            kp.handleEscapeFromTextFocus(st);
          }
        } catch { /* ignore */ }
      }
    } else {
      this.practicePanel.hide();
    }

    // Fire onEnter actions once per slide.
    if (!this.progress.onEnterDoneSlideIds.includes(slide.id)) {
      this.progress.onEnterDoneSlideIds.push(slide.id);
      this._persist(); // best-effort; don't block UI
      this._runOnEnter(slide);
    }

  }

  _runOnEnter(slide) {
    const entries = Array.isArray(slide?.onEnter) ? slide.onEnter : [];
    for (const entry of entries) {
      if (!entry || !entry.type) continue;

      // overlay: show a modal overlay on top of the onboarding panel, blurring the slide behind it.
      if (entry.type === 'overlay') {
        const title = String(entry.title || 'Nice!').trim();
        const message = String(entry.message || entry.text || '').trim();
        const primaryText = String(entry.primaryText || entry.primary || 'Got it').trim();
        const secondaryText = String(entry.secondaryText || entry.secondary || '').trim();
        const effect = String(entry.effect || '').trim().toLowerCase();
        try {
          this.panel.showOverlay({
            title,
            message,
            primaryText,
            secondaryText
          });
          this._emit('overlayShown', { slideId: slide?.id || null, title, message });
          // Celebration: marquee/flash around the walkthrough chrome (e.g. "Nice — KeyPilot is back on").
          if (effect === 'marquee' || effect === 'flash' || effect === 'dash' || effect === 'scale') {
            try { this.panel.playBorderEffect?.(effect); } catch { /* ignore */ }
          }
        } catch {
          // ignore
        }
        continue;
      }

      // openTab: open an extension page in a new foreground tab so users can practice.
      if (entry.type === 'openTab') {
        const relUrl = String(entry.url || '').trim();
        if (!relUrl) continue;
        const url = chrome.runtime.getURL(relUrl);

        // Prefer opening via background so it behaves like KeyPilot's other new-tab actions.
        try {
          chrome.runtime.sendMessage({ type: 'KP_OPEN_URL_FOREGROUND', url }).catch(() => {});
          continue;
        } catch {
          // fall back
        }

        try {
          window.open(url, '_blank', 'noopener,noreferrer');
        } catch {
          // ignore
        }
        continue;
      }

      // Legacy: openPopover (kept for future slides).
      if (entry.type === 'openPopover') {
        const relUrl = String(entry.url || '').trim();
        if (!relUrl) continue;

        const title = String(entry.title || 'KeyPilot Tutorial');

        try {
          const kp = window.__KeyPilotInstance;
          if (!kp || !kp.overlayManager || typeof kp.overlayManager.showPopover !== 'function') continue;
          const url = chrome.runtime.getURL(relUrl);
          kp.overlayManager.showPopover(url, { title });
          // Popover is modal; keep KeyPilot state consistent.
          try {
            kp.state?.setPopoverOpen?.(true, url);
          } catch {
            // ignore
          }
        } catch {
          // ignore
        }
      }
    }
  }

  _onDocKeydownCapture(e) {
    try {
      if (!this._isKeyPilotEnabled()) return;

      // Don't open onboarding popover when in text mode (similar to how ' doesn't open settings)
      try {
        const kp = window.__KeyPilotInstance;
        const st = kp?.state?.getState?.();
        if (st?.mode === MODES.TEXT_FOCUS) return;
      } catch { /* ignore */ }

      // Alt + / : open/close onboarding.
      //
      // Notes:
      // - Prefer `e.code === 'Slash'` because `e.key` varies by layout.
      // - Support AltGr layouts where the browser may report Ctrl+Alt, and/or AltGraph state.
      const isAltOrAltGraph =
        !!e &&
        (
          e.altKey === true ||
          (typeof e.getModifierState === 'function' && e.getModifierState('AltGraph') === true)
        );

      const isSlashKey =
        !!e &&
        (
          e.code === 'Slash' ||
          e.key === '/' ||
          e.key === '?'
        );

      const isAltSlash = isAltOrAltGraph && isSlashKey;

      if (!isAltSlash) return;

      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();

      // Toggle onboarding panel (does not reset progress).
      // If it's open, close it; if closed, open it.
      const shouldOpen = !(this.active === true && this.panel?.isVisible?.());
      this.setActive(shouldOpen);
    } catch {
      // ignore
    }
  }

  _onActionEvent(ev) {
    if (!this.active || this.progress.completed) return;

    const detail = ev?.detail || {};
    const action = typeof detail.action === 'string' ? detail.action : '';
    if (!action) return;
    this._lastAction = action;

    // Keep enabled cache in sync for toggle actions (before task matching / UI hide).
    if (action === 'toggleExtension' && typeof detail.enabled === 'boolean') {
      this._enabledCache = detail.enabled === true;
      this._enabledCacheTs = Date.now();
      this._setAltSlashListenerEnabled(detail.enabled);
    }

    const slide = this._getCurrentSlide();
    if (!slide) {
      if (action === 'toggleExtension' && detail.enabled === false) {
        this.panel.hide();
        this.practicePanel.hide();
      }
      if (action === 'toggleExtension' && detail.enabled === true) {
        this.hideReEnableTip();
        this._render({ reason: 'toggleOn' });
      }
      return;
    }

    const completedBefore = new Set(this.progress.completedTaskIds);
    const offTaskWasOpen = (slide.tasks || []).some((t) => {
      if (!t?.id || completedBefore.has(t.id)) return false;
      const when = t.when || {};
      return (
        String(when.type || '') === 'action' &&
        String(when.action || '') === 'toggleExtension' &&
        String(when.change || '') === 'off'
      );
    });

    let changed = false;
    const completed = new Set(this.progress.completedTaskIds);

    // Coming back from disabled: any pending "turn off" step is already satisfied.
    if (action === 'toggleExtension' && detail.enabled === true) {
      for (const task of slide.tasks || []) {
        if (!task?.id || completed.has(task.id)) continue;
        const when = task.when || {};
        if (
          String(when.type || '') === 'action' &&
          String(when.action || '') === 'toggleExtension' &&
          String(when.change || '') === 'off'
        ) {
          completed.add(task.id);
          changed = true;
        }
      }
    }

    for (const task of slide.tasks || []) {
      if (!task || !task.id || completed.has(task.id)) continue;
      if (this._taskMatches(task, { type: 'action', action, detail })) {
        completed.add(task.id);
        changed = true;
      }
    }

    if (changed) {
      this.progress.completedTaskIds = Array.from(completed);
      this._persist(); // best-effort
    }

    // Turned off: hide walkthrough, keep active, optional tip on the control strip.
    if (action === 'toggleExtension' && detail.enabled === false) {
      this.panel.hide();
      this.practicePanel.hide();
      if (offTaskWasOpen) {
        this.showReEnableTip();
      }
      return;
    }

    // Turned back on: drop tip and restore walkthrough (active was preserved).
    if (action === 'toggleExtension' && detail.enabled === true) {
      this.hideReEnableTip();
      this._render({ reason: 'toggleOn' });
      const slideComplete = this._isSlideComplete(slide, new Set(this.progress.completedTaskIds));
      if (slideComplete) {
        this._handleSlideCompleted(slide, {
          cause: action,
          completedTaskIds: Array.from(this.progress.completedTaskIds)
        });
        this._advanceSlide({ cause: action });
      }
      return;
    }

    if (changed) {
      this._render({ reason: 'taskUpdate' });
      const slideComplete = this._isSlideComplete(slide, completed);
      if (slideComplete) {
        this._handleSlideCompleted(slide, { cause: action, completedTaskIds: Array.from(completed) });
        this._advanceSlide({ cause: action });
      }
    }
  }

  showReEnableTip() {
    try {
      const anchor =
        document.querySelector('.kp-control-strip [data-kp-control-strip-status="true"]') ||
        document.querySelector('.kp-control-strip');
      this.panel?.showReEnableTip?.({
        anchorEl: anchor,
        message: 'Click it again to turn KeyPilot back on.'
      });
    } catch { /* ignore */ }
  }

  hideReEnableTip() {
    try { this.panel?.hideReEnableTip?.(); } catch { /* ignore */ }
  }

  _isSlideComplete(slide, completedTaskIdsSet) {
    return isSlideComplete(slide, completedTaskIdsSet);
  }

  _handleSlideCompleted(slide, { cause = '', completedTaskIds = null } = {}) {
    try {
      const idx = this._currentSlideIndex();
      const next = this.model.slides[idx + 1] || null;
      const isLast = !next;
      this._emit('slideCompleted', {
        slideId: slide?.id || null,
        slideIndex: idx,
        slideCount: this.model.slides.length,
        nextSlideId: next?.id || null,
        cause: String(cause || ''),
        completedTaskIds: Array.isArray(completedTaskIds) ? completedTaskIds : Array.from(new Set(this.progress.completedTaskIds))
      });
      if (isLast) {
        this._emit('lastSlideCompleted', {
          slideId: slide?.id || null,
          slideIndex: idx,
          slideCount: this.model.slides.length,
          cause: String(cause || '')
        });
      }
    } catch {
      // ignore
    }
  }

  async _advanceSlide({ cause = '' } = {}) {
    if (this._isTransitioning) return;

    const idx = this._currentSlideIndex();
    const next = this.model.slides[idx + 1];

    if (!next) {
      // Completed all slides.
      this.progress.completed = true;
      this.active = false;
      await this._persist();
      this._emit('onboardingCompleted', {
        slideId: this.progress.slideId,
        slideIndex: idx,
        slideCount: this.model.slides.length,
        cause: String(cause || '')
      });
      this._render({ reason: 'completed' });
      return;
    }

    this.progress.slideId = next.id;
    await this._persist();
    this._isTransitioning = true;
    this._render({ transition: { type: 'slide', dir: 1 }, reason: 'autoAdvance' });
  }

  _taskMatches(task, ctx) {
    const when = task?.when || {};
    const type = String(when.type || '').trim();
    if (!type) return false;

    if (type === 'action' && ctx.type === 'action') {
      if (when.action && when.action !== ctx.action) return false;

      // Optional polarity for toggles: change="off" | "on"
      const change = String(when.change || '').trim();
      if (change === 'off') {
        if (ctx.detail?.enabled !== false) return false;
      } else if (change === 'on') {
        if (ctx.detail?.enabled !== true) return false;
      }

      const target = String(when.target || '').trim();
      if (!target) return true;

      if (target === 'link') return !!ctx.detail?.isLink;
      if (target === 'keyboardHelpKey') return !!ctx.detail?.isKeyboardHelpKey;
      return false;
    }

    if (type === 'mode' && ctx.type === 'mode') {
      const mode = String(when.mode || '').trim();
      const change = String(when.change || '').trim();
      if (!mode || !change) return false;

      if (change === 'enter') return ctx.nextMode === mode && ctx.prevMode !== mode;
      if (change === 'exit') return ctx.prevMode === mode && ctx.nextMode !== mode;
      return false;
    }

    return false;
  }

  /**
   * Labels that depend on whether KeyPilot is currently ON or OFF.
   * @param {{id?: string, label?: string}} task
   * @returns {string}
   */
  _resolveTaskLabel(task) {
    const base = String(task?.label || task?.id || '');
    const id = String(task?.id || '');
    const enabled = this._isKeyPilotEnabled();

    if (id === 'toggle_extension_off') {
      if (!enabled) {
        // Panel is usually hidden while off; tip bubble carries the "click again" copy.
        return 'KeyPilot is already off (control strip shows `OFF`). Click the strip to turn it back on — a tip points at the control if you need it.';
      }
      return base || 'Notice the control strip above that says `ON`. Click it to turn KeyPilot completely off.';
    }
    return base;
  }

  /**
   * If the "turn off" step is still open and KeyPilot is already disabled, complete it.
   * @returns {boolean} whether progress changed
   */
  _autoCompleteToggleOffIfAlreadyDisabled() {
    if (!this.active || this.progress.completed) return false;
    if (this._isKeyPilotEnabled()) return false;

    const slide = this._getCurrentSlide();
    if (!slide) return false;

    const completed = new Set(this.progress.completedTaskIds);
    let changed = false;
    for (const task of slide.tasks || []) {
      if (!task?.id || completed.has(task.id)) continue;
      const when = task.when || {};
      if (
        String(when.type || '') === 'action' &&
        String(when.action || '') === 'toggleExtension' &&
        String(when.change || '') === 'off'
      ) {
        completed.add(task.id);
        changed = true;
      }
    }
    if (!changed) return false;
    this.progress.completedTaskIds = Array.from(completed);
    this._persist();
    return true;
  }

  _attachToKeyPilotStateSoon() {
    if (this._keyPilotReady) return;

    const tryAttach = () => {
      try {
        const kp = window.__KeyPilotInstance;
        if (!kp || !kp.state || typeof kp.state.subscribe !== 'function') return false;

        this._keyPilotReady = true;
        // Track initial mode.
        try {
          const s = kp.state.getState?.();
          this._prevMode = s?.mode || null;
        } catch {
          this._prevMode = null;
        }

        this._unsubscribeState = kp.state.subscribe((nextState, prevState) => {
          if (!this.active || this.progress.completed) return;
          const slide = this._getCurrentSlide();
          if (!slide) return;

          const prevMode = prevState?.mode || this._prevMode || null;
          const nextMode = nextState?.mode || null;
          this._prevMode = nextMode;

          let changed = false;
          const completed = new Set(this.progress.completedTaskIds);

          for (const task of slide.tasks || []) {
            if (!task || !task.id || completed.has(task.id)) continue;
            if (this._taskMatches(task, { type: 'mode', prevMode, nextMode })) {
              completed.add(task.id);
              changed = true;
            }
          }

          if (!changed) return;
          this.progress.completedTaskIds = Array.from(completed);
          this._persist(); // best-effort
          this._render({ reason: 'modeTaskUpdate' });

          const slideComplete = this._isSlideComplete(slide, completed);
          if (slideComplete) {
            this._handleSlideCompleted(slide, { cause: 'modeChange', completedTaskIds: Array.from(completed) });
            this._advanceSlide({ cause: 'modeChange' });
          }
        });

        return true;
      } catch {
        return false;
      }
    };

    // Poll for KeyPilot instance for a short window; it initializes async.
    let attempts = 0;
    const timer = setInterval(() => {
      attempts += 1;
      if (tryAttach() || attempts > 80) {
        try { clearInterval(timer); } catch { /* ignore */ }
      }
    }, 100);
  }
}





  // Module: src/keypilot.js
/**
 * KeyPilot main application class
 */




























class KeyPilot extends EventManager {
  constructor() {
    super();

    // Prevent multiple instances
    if (window.__KeyPilotV22) {
      console.warn('[KeyPilot] Already loaded.');
      return;
    }
    window.__KeyPilotV22 = true;
    window.__KeyPilotInstance = this; // Store instance for popover access

    // Extension enabled state - default to true, will be updated from service worker
    this.enabled = true;
    this.initializationComplete = false;

    this.state = new StateManager();
    this.cursor = new CursorManager();
    this.detector = new ElementDetector();
    this.activator = new ActivationHandler(this.detector);
    this.mouseCoordinateManager = new MouseCoordinateManager();
    this.focusDetector = new FocusDetector(this.state, this.mouseCoordinateManager);
    this.overlayManager = new OverlayManager();
    this.styleManager = new StyleManager();
    this.columnLayoutManager = new ColumnLayoutManager();
    this.inspector = new InspectorModeController({
      state: this.state,
      deepElementFromPoint: (x, y) => this.detector.deepElementFromPoint(x, y),
      onBeforeEnter: () => {
        // Cancel competing select modes when entering any inspector kind
        try {
          if (this.state.isHighlightMode()) this.cancelHighlightMode();
        } catch { /* ignore */ }
      }
    });
    this.shadowDOMManager = new ShadowDOMManager(this.styleManager);
    this.floatingKeyboardHelp = null;
    this.controlStrip = null;
    this.keybindings = buildKeybindingsForLayout(DEFAULT_KEYBOARD_LAYOUT_ID);
    this._keyboardLayoutId = DEFAULT_KEYBOARD_LAYOUT_ID;
    this._keyboardUiLayout = getKeyboardUiLayoutForLayout(DEFAULT_KEYBOARD_LAYOUT_ID);
    this.omniboxManager = new OmniboxManager({
      onClose: () => {
        try {
          // Only the top frame should manage omnibox state.
          if (window === window.top) this.state.setMode(MODES.NONE);
        } catch { /* ignore */ }
      }
    });
    this.tabHistoryPopover = new TabHistoryPopover({
      popupManager: this.overlayManager?.popupManager,
      onStateChange: (open) => {
        this.state.setPopoverOpen(open, open ? 'tab-history' : null);
      }
    });
    this.launcherPopover = new LauncherPopover(this);
    this.KEYBOARD_HELP_STORAGE_KEY = 'keypilot_keyboard_help_visible';
    this._keyboardHelpVisible = false;
    this._keyboardHelpStorageListener = null;

    // Link-hover → keyboard key glow (debounced; video thumbs thrash focusEl).
    this._LINK_HOVER_HINT_DEBOUNCE_MS = 90;
    this._linkHoverHintTimer = null;
    this._linkHoverHintPendingState = null;
    /** @type {boolean|null} */
    this._linkHoverHintLastApplied = null;
    
    // Intersection Observer optimizations
    this.intersectionManager = new IntersectionObserverManager(this.detector);
    this.scrollManager = new OptimizedScrollManager(this.overlayManager, this.state, {
      // Live-refresh text/rectangle selection overlays every scroll frame (~60fps).
      onScrollFrame: () => this.refreshHighlightDuringScroll()
    });

    // Permanent hover targeting: browser-native DOM hover listeners drive focusEl
    // during normal browsing. RBush spatial indexing is retired (see architecture audit).
    this._domHoverListenersEnabled = true;
    
    // Panel tracking for negative regions
    this._panelTrackingInterval = null;
    this._lastPanelCheck = 0;
    
    // Edge-only rectangle intersection observer for performance optimization
    this.rectangleIntersectionObserver = null;
    this.edgeOnlyProcessingEnabled = FEATURE_FLAGS.ENABLE_EDGE_ONLY_PROCESSING;

    // Mouse movement optimization: only query every 2+ pixels (increased threshold)
    this.lastQueryPosition = { x: -1, y: -1 };
    this.MOUSE_QUERY_THRESHOLD = 1;
    // Highlight selection updates: last cursor pos we applied (avoid thrashing on micro-moves)
    this._lastHighlightUpdatePos = { x: -1, y: -1 };
    this._HIGHLIGHT_UPDATE_THRESHOLD = 2;
    // Prevent re-entrant completeSelection / updates while finishing a session
    this._completingHighlight = false;
    
    // Mousemove hot-path optimization: coalesce hover work to once-per-frame.
    // We still update cursor position immediately, but defer expensive hit-testing work.
    this._mouseMoveRAF = 0;
    this._pendingMouse = { x: -1, y: -1, underHint: null };

    // Post-click refresh: after UI-mutating clicks, re-query under cursor once the DOM settles.
    this._postClickRefreshToken = 0;
    this._postClickRefreshTimer = null;
    this._postClickRefreshInnerTimer = null;

    // Text focus mode: "mouse-move arms click" countdown (only F clicks; ESC exits only).
    this._textModeClickArmed = false;
    this._textModeClickArmedTarget = null;
    this._textModeClickInterval = null;

    // Settings cache (kp_settings_v1).
    this._settings = null;
    this._settingsStorageListener = null;
    
    
    // Performance monitoring
    this.performanceMetrics = {
      mouseQueries: 0,
      cacheHits: 0,
      lastMetricsLog: Date.now()
    };

    // Bound custom event handlers (so cleanup can remove them)
    this._boundScrollEndHandler = null;

    this.init();
  }

  _handleDomHoverChange(el) {
    try {
      if (!this.enabled) return;
      const currentState = this.state.getState();
      // Only drive hover selection in normal browsing mode.
      if (currentState.mode !== MODES.NONE) return;

      let next = (el && el.nodeType === 1) ? el : null;
      // HTML/BODY are too coarse — treat as clear so hover chrome does not stick.
      try {
        if (next && (next.tagName === 'HTML' || next.tagName === 'BODY')) next = null;
      } catch { /* ignore */ }

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] DOM hover change:', {
          element: next?.tagName,
          href: next?.href,
          currentFocusEl: currentState.focusEl?.tagName
        });
      }

      if (next !== currentState.focusEl) {
        this.state.setFocusElement(next);
      }
    } catch { /* ignore */ }
  }
  
  _getUnderElementHintFromMouseEvent(e) {
    try {
      let under = null;
      const path = (e && typeof e.composedPath === 'function') ? e.composedPath() : null;
      if (Array.isArray(path)) {
        for (const n of path) {
          if (n && n.nodeType === 1) { under = n; break; }
        }
      }
      if (!under && e && e.target && e.target.nodeType === 1) under = e.target;
      if (!under || under.nodeType !== 1) return null;
      // HTML/BODY are too coarse to be useful as an occlusion gate.
      try {
        if (under.tagName === 'HTML' || under.tagName === 'BODY') return null;
      } catch { /* ignore */ }
      return under;
    } catch {
      return null;
    }
  }

  _isKeyPilotUiElement(el) {
    try {
      let n = el;
      let guard = 0;
      while (n && n.nodeType === 1 && guard++ < 12) {
        const id = typeof n.id === 'string' ? n.id : '';
        if (id && id.startsWith('kpv2-')) return true;
        const cl = n.classList;
        if (cl && cl.length) {
          for (const c of cl) {
            if (typeof c === 'string' && c.startsWith('kpv2-')) return true;
          }
        }
        n = n.parentElement;
      }
    } catch { /* ignore */ }
    return false;
  }

  /**
   * Check if an element is inside a valid popover container (iframe or popupManager modal)
   * @param {Element} el
   * @returns {boolean}
   */
  _isElementInPopover(el) {
    if (!el || !(el instanceof Element)) return false;

    // Check iframe popover container
    const iframeContainer = this.overlayManager?.popoverContainer;
    if (iframeContainer instanceof Element && iframeContainer.contains(el)) {
      return true;
    }

    // Check popupManager modal
    const modalPanel = this.overlayManager?.popupManager?.top()?.panel;
    if (modalPanel instanceof Element && modalPanel.contains(el)) {
      return true;
    }

    return false;
  }

  /**
   * When DOM-hover listener mode is enabled, `state.focusEl` is driven by delegated hover events.
   * However, if the DOM changes under a stationary pointer (menus, dialogs, overlays), hover events
   * may not fire and `focusEl` can become stale. This helper validates focusEl against the actual
   * element under the cursor at activation time.
   */
  _getValidatedActivationTarget(currentState) {
    const st = currentState || this.state.getState();
    const x = st?.lastMouse?.x;
    const y = st?.lastMouse?.y;
    const focus = st?.focusEl;

    let under = null;
    try {
      if (typeof x === 'number' && typeof y === 'number') {
        under = this.detector.deepElementFromPoint(x, y);
      }
    } catch { /* ignore */ }

    try {
      if (under && this._isKeyPilotUiElement(under)) {
        // Allow activation on KeyPilot UI elements that represent clickable content (e.g. history rows with data-kp-url)
        if (!(under instanceof Element) ||
            !(under.getAttribute('role') === 'link' && under.dataset?.kpUrl)) {
          under = null;
        }
      }
    } catch { /* ignore */ }

    let target = focus || under;

    // In DOM-hover mode, prefer focusEl when it's consistent with what's actually under the pointer.
    // If not, fall back to a fresh under-cursor pick to avoid clicking stale targets.
    try {
      if (this._domHoverListenersEnabled && focus && under && focus instanceof Element && under instanceof Element) {
        const consistent = focus.contains(under) || under.contains(focus);
        if (!consistent) {
          // Map under->clickable ancestor for better semantics (e.g. hovering a child inside a button).
          const clickableUnder = this.detector.findClickable(under) || under;
          target = clickableUnder;
        }
      }
    } catch { /* ignore */ }

    return target;
  }

  /**
   * Emit a semantic KeyPilot action event for consumers like onboarding.
   * Consumers should prefer this over raw keydown because it reflects what KeyPilot actually did.
   */
  emitAction(action, detail = {}) {
    try {
      const payload = {
        action: String(action || ''),
        timestamp: Date.now(),
        ...detail
      };
      if (!payload.action) return;
      document.dispatchEvent(new CustomEvent('keypilot:action', { detail: payload }));
    } catch {
      // ignore
    }
  }

  _buildActivationDetail(target) {
    const detail = {
      isLink: false,
      href: null,
      isKeyboardHelpKey: false,
      category: CLICKABLE_CATEGORY.NONE
    };

    try {
      const el = target instanceof Element ? target : null;

      // Category is the source of truth for "is this a link?" — sliders/media/buttons
      // must not be reported as links even when nested near anchors in a player bar.
      if (el && this.detector?.getClickableCategory) {
        detail.category = this.detector.getClickableCategory(el);
      }

      if (detail.category === CLICKABLE_CATEGORY.LINK) {
        detail.isLink = true;
        // Resolve href from the element or a link-like host.
        if (el && el.tagName === 'A' && /** @type {any} */ (el).href) {
          detail.href = /** @type {any} */ (el).href;
        } else if (el && typeof el.closest === 'function') {
          const a = el.closest('a[href]');
          if (a && /** @type {any} */ (a).href) detail.href = /** @type {any} */ (a).href;
          if (!detail.href) {
            const linkish = /** @type {HTMLElement|null} */ (el.closest('[role="link"]'));
            const kpUrl = linkish?.dataset?.kpUrl ? String(linkish.dataset.kpUrl) : '';
            if (kpUrl) detail.href = kpUrl;
          }
        }
      }
    } catch {
      // ignore
    }

    try {
      // Keys in the floating keyboard reference have `data-kp-action-id`.
      // If the user F-clicks one, it triggers the tooltip popover (key details).
      const el = target instanceof Element ? target : null;
      const keyEl =
        el && typeof el.closest === 'function'
          ? el.closest('[data-kp-action-id]')
          : null;
      const inKeyboardHelp =
        !!(keyEl && keyEl.closest && keyEl.closest('.kp-floating-keyboard-help'));
      if (keyEl && inKeyboardHelp) detail.isKeyboardHelpKey = true;
    } catch {
      // ignore
    }

    try {
      const el = target instanceof Element ? target : null;
      if (el && typeof el.closest === 'function' && el.closest('.kp-control-strip')) {
        detail.isControlStrip = true;
      }
    } catch {
      // ignore
    }

    return detail;
  }

  _clearTextModeClickTimers() {
    try {
      if (this._textModeClickInterval) {
        clearInterval(this._textModeClickInterval);
      }
    } catch { /* ignore */ }
    this._textModeClickInterval = null;
  }

  _disarmTextModeClick() {
    this._clearTextModeClickTimers();
    this._textModeClickArmed = false;
    this._textModeClickArmedTarget = null;
    try { this.overlayManager?.setHoverClickLabelText?.('F clicks'); } catch { /* ignore */ }
    // Keyboard reference: gray Click Element again until the next armed hover.
    try { this.floatingKeyboardHelp?.setTextModeActivateArmed?.(false); } catch { /* ignore */ }
  }

  _armTextModeClick(target) {
    this._clearTextModeClickTimers();
    this._textModeClickArmed = true;
    this._textModeClickArmedTarget = target;

    let remaining = 3;
    try { this.overlayManager?.setHoverClickLabelText?.(`F clicks ${remaining}`); } catch { /* ignore */ }
    // Keyboard reference: enable Click Element for the countdown window only.
    try { this.floatingKeyboardHelp?.setTextModeActivateArmed?.(true); } catch { /* ignore */ }

    this._textModeClickInterval = setInterval(() => {
      remaining -= 1;

      if (remaining > 0) {
        try { this.overlayManager?.setHoverClickLabelText?.(`F clicks ${remaining}`); } catch { /* ignore */ }
        return;
      }

      // Countdown complete: remove hover focus rect + return cursor to orange until the user moves the mouse again.
      this._disarmTextModeClick();
      try { this.state.setFocusElement(null); } catch { /* ignore */ }
    }, 1000);
  }

  _handleActivateFromTextFocus(currentState) {
    const target = currentState?.focusEl;
    if (!target) return;

    // Try semantic activation first
    if (this.activator.handleSmartActivate(target, currentState.lastMouse.x, currentState.lastMouse.y)) {
      this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
      this.overlayManager.flashFocusOverlay(target);
      this.postClickRefresh(target, currentState.lastMouse.x, currentState.lastMouse.y);
    } else {
      // Fallback to click
      this.activator.smartClick(target, currentState.lastMouse.x, currentState.lastMouse.y);
      this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
      this.overlayManager.flashFocusOverlay(target);
      this.postClickRefresh(target, currentState.lastMouse.x, currentState.lastMouse.y);
    }

    // After clicking, hide hover UI until the user moves the mouse again.
    this._disarmTextModeClick();
    this.state.setFocusElement(null);
  }

  shouldImmediateRefreshAfterClick(target) {
    try {
      if (!target || !(target instanceof Element)) return false;
      const a = target.tagName === 'A' ? target : (typeof target.closest === 'function' ? target.closest('a') : null);
      if (!a || a.tagName !== 'A') return false;
      const rawHref = a.getAttribute('href'); // preserve '#' anchors (a.href becomes absolute)
      if (!rawHref) return true; // <a> with no href is typically UI-only
      return rawHref.trim().startsWith('#');
    } catch {
      return false;
    }
  }

  postClickRefresh(target, x, y) {
    if (!this.enabled) return;
    if (typeof x !== 'number' || typeof y !== 'number') return;
    if (x < 0 || y < 0) return;

    // UI-only anchors should refresh immediately (no 250ms delay).
    if (this.shouldImmediateRefreshAfterClick(target)) {
      const doc = document;
      const token = ++this._postClickRefreshToken;

      // Clear any prior scheduled refresh so rapid clicks don't pile up work.
      try { if (this._postClickRefreshTimer) clearTimeout(this._postClickRefreshTimer); } catch { /* ignore */ }
      try { if (this._postClickRefreshInnerTimer) clearTimeout(this._postClickRefreshInnerTimer); } catch { /* ignore */ }

      // Make the rect disappear immediately (UI interaction likely mutated DOM).
      try { this.overlayManager?.hideFocusOverlay?.(); } catch { /* ignore */ }

      // Then re-query after a short settle period (accordion/menu animation, etc.).
      this._postClickRefreshTimer = window.setTimeout(() => {
        if (!this.enabled) return;
        if (token !== this._postClickRefreshToken) return;
        if (document !== doc) return; // navigated/reloaded
        if (!document?.body) return;

        this.updateElementsUnderCursor(x, y);
        this.state.setState({ _overlayUpdateTrigger: Date.now() });
      }, 250);

      return;
    }

    this.schedulePostClickRefresh(x, y);
  }

  schedulePostClickRefresh(x, y) {
    if (!this.enabled) return;
    if (typeof x !== 'number' || typeof y !== 'number') return;
    if (x < 0 || y < 0) return;

    const doc = document;
    const token = ++this._postClickRefreshToken;

    // Clear any prior scheduled refresh so rapid clicks don't pile up work.
    try { if (this._postClickRefreshTimer) clearTimeout(this._postClickRefreshTimer); } catch { /* ignore */ }
    try { if (this._postClickRefreshInnerTimer) clearTimeout(this._postClickRefreshInnerTimer); } catch { /* ignore */ }

    this._postClickRefreshTimer = window.setTimeout(() => {
      if (!this.enabled) return;
      if (token !== this._postClickRefreshToken) return;
      if (document !== doc) return; // navigated/reloaded
      if (!document?.body) return;

      // Fade out the current rect so we don't leave a stale highlight when the UI mutated.
      this.overlayManager?.fadeOutFocusOverlay?.(120);

      // After the fade starts, force an unthrottled re-query under the cursor and refresh overlays,
      // even if the "focused" element reference didn't change.
      this._postClickRefreshInnerTimer = window.setTimeout(() => {
        if (!this.enabled) return;
        if (token !== this._postClickRefreshToken) return;
        if (document !== doc) return;
        if (!document?.body) return;

        // Bypass threshold logic: the mouse may not have moved, but the DOM might have.
        this.updateElementsUnderCursor(x, y);

        // Force overlay refresh even when focusEl stays the same reference.
        this.state.setState({ _overlayUpdateTrigger: Date.now() });
      }, 130);
    }, 250);
  }

  async init() {
    // Always set up styles and shadow DOM support
    this.setupStyles();
    this.setupShadowDOMSupport();

    // Query service worker for current enabled state
    await this.queryInitialState();

    // Load persisted settings and keep them synced across tabs.
    await this.refreshSettingsFromStorage();
    this.installSettingsSync();

    // Apply cursor-mode behavior as early as we can (after settings are loaded).
    try {
      this.styleManager?.setCursorOverridesEnabled?.(this._isCustomCursorModeEnabled());
    } catch { /* ignore */ }
    
    // Only initialize functionality if enabled
    if (this.enabled) {
      if (this._isCustomCursorModeEnabled()) {
        // Only apply the custom cursor once we know KeyPilot is enabled.
        // This prevents a brief "cursor flash" during page load when the extension is disabled.
        this.cursor.ensure();
        // Apply the cursor immediately using the already-loaded settings, without waiting for the
        // first hover/mousemove-driven state update. This also preserves the early-inject cursor
        // (if present) by avoiding a default overwrite in CursorManager.ensure().
        try {
          const st = this.state?.getState?.() || {};
          const mode = this._cursorModeForState(st);
          this.cursor.setMode(mode, this._buildCursorOptionsForState({ ...st, mode: st.mode }));
        } catch { /* ignore */ }
      } else {
        // Ensure we do not leave any cursor overrides behind.
        try { this.cursor.cleanup(); } catch { /* ignore */ }
      }
      await this.initializeEnabledState();
    } else {
      this.initializeDisabledState();
    }

    // Always set up communication and state management
    this.state.subscribe((newState, prevState) => {
      this.handleStateChange(newState, prevState);
    });



    this.setupPopupCommunication();
    this.setupOptimizedEventListeners();
   this.setupContinuousCursorSync();

    // Keep floating keyboard reference visibility synced across tabs.
    await this.setupKeyboardHelpSync();

    // Control strip (upper-left): survives disable so On/Off remains available.
    this.setupControlStrip();
    this.applyControlStripFromSettings();

    // Initialize cursor position using stored coordinates or fallback
    await this.initializeCursorPosition();

    // Note: occlusion is handled automatically via DOM hit-testing (no custom negative regions).

    this.initializationComplete = true;
    this.state.setState({ isInitialized: true });
  }

  async refreshSettingsFromStorage() {
    const prevCursorMode = this._settings?.cursorMode;
    try {
      this._settings = await getSettings();
    } catch {
      this._settings = { ...DEFAULT_SETTINGS };
    }

    // Apply keyboard layout immediately (used by key handling + keyboard reference UI).
    try {
      this._applyKeyboardLayoutFromSettings();
    } catch { /* ignore */ }

    // Apply cursor-mode behavior immediately.
    const cursorEnabled = this._isCustomCursorModeEnabled();
    try {
      this.styleManager?.setCursorOverridesEnabled?.(cursorEnabled);
    } catch { /* ignore */ }

    // Apply to runtime immediately.
    try {
      this.overlayManager?.setModeSettings?.(this._settings);
    } catch { /* ignore */ }

    // Active text input frame stroke thickness + left-edge bar width (CSS-driven).
    try {
      const px = Number(this._settings?.textMode?.strokeThickness);
      if (Number.isFinite(px)) {
        document.documentElement.style.setProperty('--kpv2-text-stroke-width', `${px}px`);
      } else {
        document.documentElement.style.removeProperty('--kpv2-text-stroke-width');
      }
    } catch { /* ignore */ }

    try {
      const edgePx = Number(this._settings?.textMode?.leftEdgeWidth);
      if (Number.isFinite(edgePx)) {
        document.documentElement.style.setProperty('--kpv2-text-left-edge-width', `${edgePx}px`);
      } else {
        document.documentElement.style.removeProperty('--kpv2-text-left-edge-width');
      }
    } catch { /* ignore */ }

    // Cursor behavior:
    // - CUSTOM_CURSORS: ensure + refresh cursor
    // - NO_CUSTOM_CURSORS: remove cursor overrides and skip cursor updates entirely
    if (cursorEnabled) {
      try {
        this.cursor.ensure();
      } catch { /* ignore */ }
      try {
        this.cursor.currentMode = null;
        this.cursor.currentModeKey = null;
        const st = this.state?.getState?.();
        if (st && st.mode) {
          this.cursor.setMode(this._cursorModeForState(st), this._buildCursorOptionsForState(st));
        }
      } catch { /* ignore */ }
    } else {
      try {
        // If we just switched away from CUSTOM_CURSORS, aggressively clean up.
        if (prevCursorMode === CURSOR_MODE.CUSTOM_CURSORS) {
          this.cursor.cleanup();
        } else {
          // Still ensure we aren't leaving any cursor variable set.
          this.cursor.cleanup();
        }
      } catch { /* ignore */ }
    }

    // Force overlay refresh.
    try {
      this.state?.setState?.({ _overlayUpdateTrigger: Date.now() });
    } catch { /* ignore */ }

    // Control strip visibility / collapsed state.
    try {
      this.applyControlStripFromSettings();
    } catch { /* ignore */ }

    // Keyboard link-hover glow may have been toggled in Settings → Click Mode.
    try {
      this._linkHoverHintLastApplied = null;
      this._flushKeyboardLinkHoverHints();
    } catch { /* ignore */ }
  }

  /**
   * Create the control strip host (top frame only). Safe to call multiple times.
   * Kept alive while KeyPilot is disabled so On/Off can re-enable the extension.
   */
  setupControlStrip() {
    if (window !== window.top) return;
    if (this.controlStrip) {
      this._wireControlStripHandlers();
      this._syncControlStripEnabledFromRuntime();
      return;
    }
    try {
      const ControlStripClass = ControlStrip || window.ControlStrip;
      if (!ControlStripClass) return;
      this.controlStrip = new ControlStripClass();
      this._wireControlStripHandlers();
      this.controlStrip.setKeyboardHelpActive(!!this._keyboardHelpVisible);
      this._syncControlStripEnabledFromRuntime();
    } catch (e) {
      console.warn('[KeyPilot] Failed to create control strip:', e);
      this.controlStrip = null;
    }
  }

  /**
   * Prefer toggle-handler state (global source of truth), then KeyPilot.enabled.
   */
  _syncControlStripTextModeFromState() {
    if (!this.controlStrip || typeof this.controlStrip.setTextModeActive !== 'function') return;
    try {
      const mode = this.state?.getState?.()?.mode;
      this.controlStrip.setTextModeActive(mode === MODES.TEXT_FOCUS);
    } catch { /* ignore */ }
  }

  _syncControlStripEnabledFromRuntime() {
    if (!this.controlStrip) return;
    let on = !!this.enabled;
    try {
      const th = window.__KeyPilotToggleHandler;
      if (th && typeof th.isEnabled === 'function') {
        on = !!th.isEnabled();
      } else if (th && typeof th.enabled === 'boolean') {
        on = !!th.enabled;
      }
    } catch { /* ignore */ }
    try {
      this.controlStrip.setEnabledState(on);
    } catch { /* ignore */ }
    this._syncControlStripTextModeFromState();
  }

  _wireControlStripHandlers() {
    if (!this.controlStrip || typeof this.controlStrip.setHandlers !== 'function') return;
    this.controlStrip.setHandlers({
      onToggleEnabled: () => {
        this._sendRuntimeMessage({ type: MSG.TOGGLE_STATE }, { silent: true });
      },
      onToggleKeyboard: () => {
        if (!this.enabled) return;
        const next = !this._keyboardHelpVisible;
        this.applyKeyboardHelpVisibility(next, { persist: true });
      },
      onOpenSettings: () => {
        if (!this.enabled) return;
        try {
          this.handleOpenSettingsPopover();
        } catch (e) {
          console.warn('[KeyPilot] Control strip open settings failed:', e);
        }
      },
      onCollapseChange: (collapsed) => {
        void setSettings({ controlStrip: { collapsed: !!collapsed } });
      },
      onClose: () => {
        void setSettings({ controlStrip: { visible: false } });
      }
    });
  }

  /**
   * Apply control strip visibility + collapsed from settings (or defaults).
   */
  applyControlStripFromSettings() {
    if (window !== window.top) return;
    this.setupControlStrip();
    if (!this.controlStrip) return;

    const cs = this._settings?.controlStrip || DEFAULT_SETTINGS.controlStrip || {};
    const visible = cs.visible !== false;
    const collapsed = !!cs.collapsed;

    this._syncControlStripEnabledFromRuntime();
    this.controlStrip.setKeyboardHelpActive(!!this._keyboardHelpVisible);
    this._syncControlStripTextModeFromState();
    // Avoid re-notifying storage when applying remote settings.
    this.controlStrip.setCollapsed(collapsed, { notify: false });
    this.controlStrip.setVisible(visible);
  }

  /**
   * Toggle the control strip (Alt+J).
   * When showing: persists visible=true and keeps the existing collapsed state.
   * When hiding: persists visible=false (same as the strip close button).
   */
  async toggleControlStripFromHotkey() {
    if (window !== window.top) return;
    this.setupControlStrip();

    // Prefer live DOM visibility; fall back to settings when the strip is not mounted yet.
    let currentlyVisible = false;
    try {
      if (this.controlStrip && typeof this.controlStrip.isVisible === 'function') {
        currentlyVisible = !!this.controlStrip.isVisible();
      } else {
        currentlyVisible = this._settings?.controlStrip?.visible !== false;
      }
    } catch {
      currentlyVisible = this._settings?.controlStrip?.visible !== false;
    }

    // Only flip visibility — preserve collapsed/expanded as last set by the user.
    const patch = { visible: !currentlyVisible };

    try {
      await setSettings({ controlStrip: patch });
    } catch { /* ignore */ }
    try {
      if (this._settings) {
        this._settings.controlStrip = {
          ...(this._settings.controlStrip || DEFAULT_SETTINGS.controlStrip),
          ...patch
        };
      }
    } catch { /* ignore */ }
    this.applyControlStripFromSettings();
  }

  /** @deprecated Use toggleControlStripFromHotkey — kept for older call sites. */
  async showControlStripFromHotkey() {
    return this.toggleControlStripFromHotkey();
  }

  _applyKeyboardLayoutFromSettings() {
    const layoutId = normalizeKeyboardLayoutId(this._settings?.keyboardLayoutId);
    this._keyboardLayoutId = layoutId;
    this.keybindings = buildKeybindingsForLayout(layoutId);
    this._keyboardUiLayout = getKeyboardUiLayoutForLayout(layoutId);

    // Keep text-input SVG background hints in sync with layout-bound keys (F vs J, Esc).
    try {
      this._applyTextInputHintLabels();
    } catch { /* ignore */ }

    // If the floating keyboard reference is active, keep it in sync (no flicker if layoutId matches).
    try {
      if (this.floatingKeyboardHelp) {
        this.floatingKeyboardHelp.setKeybindings(this.keybindings);
        if (typeof this.floatingKeyboardHelp.setKeyboardLayout === 'function') {
          this.floatingKeyboardHelp.setKeyboardLayout({
            keyboardLayout: this._keyboardUiLayout,
            layoutId: this._keyboardLayoutId
          });
        }
      }
    } catch { /* ignore */ }
  }

  /**
   * Paint layout-aware "Press F to select text field" / "press Esc to exit" SVG
   * background-images onto hovered/focused text inputs (via StyleManager CSS vars).
   */
  _applyTextInputHintLabels() {
    const KB = this.keybindings || {};
    const activate =
      KB.ACTIVATE?.keyLabel ||
      KB.ACTIVATE?.displayKey ||
      (Array.isArray(KB.ACTIVATE?.keys) ? KB.ACTIVATE.keys[0] : null) ||
      'F';
    const cancel =
      KB.CANCEL?.keyLabel ||
      KB.CANCEL?.displayKey ||
      'Esc';
    this.styleManager?.setTextInputHintLabels?.({
      hover: `Press ${activate} to select text field`,
      focus: `press ${cancel} to exit`
    });
  }

  installSettingsSync() {
    if (this._settingsStorageListener) return;
    try {
      this._settingsStorageListener = (changes, area) => {
        // Prefer sync; also accept local (storage helper falls back when sync fails).
        if (area !== 'sync' && area !== 'local') return;
        if (!changes || !changes[SETTINGS_STORAGE_KEY]) return;
        this.refreshSettingsFromStorage();
      };
      chrome.storage.onChanged.addListener(this._settingsStorageListener);
    } catch {
      // ignore
    }
  }

  /**
   * Resolve cursor glyph mode from state.
   * Inspector uses kind-specific cursors (delete / cols / …).
   * @param {any} state
   * @returns {string}
   */
  _cursorModeForState(state) {
    const mode = state?.mode;
    if (mode === MODES.INSPECTOR) {
      return getInspectorCursorMode(state?.inspectorKind);
    }
    // Legacy status/mode strings still map to the same glyphs
    if (mode === MODES.DELETE || mode === 'delete') return 'delete';
    if (mode === MODES.COLS || mode === 'cols') return 'cols';
    return mode || MODES.NONE;
  }

  _buildCursorOptionsForState(state) {
    const s = this._settings || DEFAULT_SETTINGS;
    const mode = state?.mode;
    const hasClickableElement = !!state?.focusEl;

    // Text mode cursor selection.
    if (mode === MODES.TEXT_FOCUS) {
      const type = s?.textMode?.cursorType || DEFAULT_SETTINGS.textMode.cursorType;
      if (type === 'crosshair') {
        return { hasClickableElement };
      }
      return { cursorType: 't_square', hasClickableElement };
    }

    // Click mode cursor selection (normal + popover).
    if (mode === MODES.NONE || mode === MODES.POPOVER) {
      const type = s?.clickMode?.cursor?.type || DEFAULT_SETTINGS.clickMode.cursor.type;
      if (type === 'native_arrow') return { cursorType: 'native_arrow' };
      if (type === 'native_pointer') return { cursorType: 'native_pointer' };

      const lineWidth = Number(s?.clickMode?.cursor?.lineWidth);
      const sizePixels = Number(s?.clickMode?.cursor?.sizePixels);
      const gap = Number(s?.clickMode?.cursor?.gap);
      return {
        strokeWidth: Number.isFinite(lineWidth) ? lineWidth : DEFAULT_SETTINGS.clickMode.cursor.lineWidth,
        crossHairQuadrantWidth: Number.isFinite(sizePixels) ? sizePixels : DEFAULT_SETTINGS.clickMode.cursor.sizePixels,
        gap: Number.isFinite(gap) ? gap : DEFAULT_SETTINGS.clickMode.cursor.gap
      };
    }

    // Leave other modes (delete/highlight/etc.) at their defaults.
    return mode === MODES.HIGHLIGHT ? { strokeWidth: 4, hasClickableElement: false } : {};
  }

  async setupKeyboardHelpSync() {
    if (this._keyboardHelpStorageListener) return;
    if (!chrome?.storage) return;

    this._keyboardHelpStorageListener = (changes, areaName) => {
      if (!changes || (areaName !== 'sync' && areaName !== 'local')) return;
      if (!Object.prototype.hasOwnProperty.call(changes, this.KEYBOARD_HELP_STORAGE_KEY)) return;

      const next = Boolean(changes[this.KEYBOARD_HELP_STORAGE_KEY]?.newValue);
      // Apply without persisting again (prevents ping-pong; storage event is already the source of truth).
      this.applyKeyboardHelpVisibility(next, { persist: false });
    };

    try {
      chrome.storage.onChanged.addListener(this._keyboardHelpStorageListener);
    } catch {
      // Ignore if storage events aren't available.
    }

    const initial = await this.getKeyboardHelpVisibleFromStorage();
    this.applyKeyboardHelpVisibility(initial, { persist: false });
  }

  async refreshKeyboardHelpVisibilityFromStorage() {
    const visible = await this.getKeyboardHelpVisibleFromStorage();
    this.applyKeyboardHelpVisibility(visible, { persist: false });
  }

  async getKeyboardHelpVisibleFromStorage() {
    const value = await storageGetValue(this.KEYBOARD_HELP_STORAGE_KEY, false);
    return typeof value === 'boolean' ? value : false;
  }

  async setKeyboardHelpVisibleInStorage(visible) {
    if (!chrome?.storage) return;
    await storageSetValue(this.KEYBOARD_HELP_STORAGE_KEY, Boolean(visible), {
      includeTimestamp: true
    });
  }

  /**
   * Push a keydown/keyup into the floating keyboard reference so pressed keys
   * light up even when KeyPilot stops the event with stopImmediatePropagation.
   * @param {KeyboardEvent} e
   * @param {'down'|'up'} phase
   */
  _reflectKeyOnKeyboardHelp(e, phase = 'down') {
    try {
      const help = this.floatingKeyboardHelp;
      if (!help || !this._keyboardHelpVisible) return;
      if (typeof help.isVisible === 'function' && !help.isVisible()) return;
      if (phase === 'up') {
        if (typeof help.reflectKeyUp === 'function') help.reflectKeyUp(e);
        return;
      }
      if (typeof help.reflectKeyDown === 'function') help.reflectKeyDown(e);
    } catch { /* ignore */ }
  }

  applyKeyboardHelpVisibility(visible, { persist = false } = {}) {
    const next = Boolean(visible);
    this._keyboardHelpVisible = next;

    // If KeyPilot is disabled, ensure the panel is not visible but remember the desired state.
    if (!this.enabled) {
      if (this.floatingKeyboardHelp) {
        try {
          this.floatingKeyboardHelp.cleanup();
        } catch { /* ignore */ }
        this.floatingKeyboardHelp = null;
      }
      if (persist) this.setKeyboardHelpVisibleInStorage(next);
      return;
    }

    try {
      const kb = this.keybindings || buildKeybindingsForLayout(DEFAULT_KEYBOARD_LAYOUT_ID);
      // Only seed when settings are loaded. If _settings is null, leave position
      // unhydrated so show() waits for storage instead of painting bottom-left first.
      const settingsReady = !!this._settings;
      const knownPosition = settingsReady
        ? (this._settings.panelPositions?.keyboardReference
          || DEFAULT_SETTINGS.panelPositions?.keyboardReference
          || null)
        : null;

      if (!this.floatingKeyboardHelp) {
        const FloatingKeyboardHelpClass = FloatingKeyboardHelp || window.FloatingKeyboardHelp;
        this.floatingKeyboardHelp = new FloatingKeyboardHelpClass({
          keybindings: kb,
          keyboardLayout: this._keyboardUiLayout,
          layoutId: this._keyboardLayoutId,
          // Seed saved dock/free coords so the first paint is not bottom-left then jump.
          panelPosition: knownPosition || undefined
        });
      } else {
        // Keep bindings current (in case they were updated).
        this.floatingKeyboardHelp.setKeybindings(kb);
        if (typeof this.floatingKeyboardHelp.setKeyboardLayout === 'function') {
          this.floatingKeyboardHelp.setKeyboardLayout({
            keyboardLayout: this._keyboardUiLayout,
            layoutId: this._keyboardLayoutId
          });
        }
        // Re-seed from settings before show (handles multi-tab move while hidden).
        if (settingsReady && typeof this.floatingKeyboardHelp.setPanelPositionFromSettings === 'function') {
          this.floatingKeyboardHelp.setPanelPositionFromSettings(knownPosition);
        }
      }

      if (next) {
        this.floatingKeyboardHelp.show();
        // Apply link-hover key hints for the current pointer target immediately.
        this._syncKeyboardLinkHoverHints(undefined, { immediate: true });
      } else {
        this.floatingKeyboardHelp.hide();
        this._syncKeyboardLinkHoverHints(undefined, { immediate: true });
      }

      try {
        this.controlStrip?.setKeyboardHelpActive?.(!!next);
      } catch { /* ignore */ }

      if (persist) this.setKeyboardHelpVisibleInStorage(next);
    } catch (e) {
      console.warn('[KeyPilot] Failed to apply keyboard reference visibility:', e);
    }
  }

  /**
   * Query service worker for initial enabled state
   */
  async queryInitialState() {
    try {
      if (!isExtensionContextValid()) {
        this.enabled = true;
        console.log('[KeyPilot] Extension context invalid; defaulting to enabled until reload');
        return;
      }
      const response = await chrome.runtime.sendMessage({ type: MSG.GET_STATE });
      if (response && typeof response.enabled === 'boolean') {
        this.enabled = response.enabled;
        console.log('[KeyPilot] Initial state from service worker:', this.enabled ? 'enabled' : 'disabled');
      } else {
        // Default to enabled if no response or invalid response
        this.enabled = true;
        console.log('[KeyPilot] No valid state from service worker, defaulting to enabled');
      }
    } catch (error) {
      if (noteExtensionContextError(error)) {
        this.enabled = true;
        return;
      }
      // Service worker might not be available, default to enabled
      this.enabled = true;
      console.log('[KeyPilot] Service worker not available, defaulting to enabled:', error.message);
    }
  }

  /**
   * Initialize KeyPilot in enabled state
   */
  async initializeEnabledState() {
    // Set rendering mode for focus overlays (can be 'dom', 'canvas', 'css-custom-props')
    this.overlayManager.setRenderingMode('canvas');

    // Initialize debug panel if enabled
    this.overlayManager.initDebugPanel();

    // Sync with early injection cursor state
    if (window.KEYPILOT_EARLY) {
      window.KEYPILOT_EARLY.setEnabled(true);
    }

    this.focusDetector.start();

    // Enable DOM-hover listeners BEFORE init (permanent targeting path).
    try {
      if (this.intersectionManager &&
          typeof this.intersectionManager.setDomHoverListenersEnabled === 'function') {
        this.intersectionManager.setDomHoverListenersEnabled(
          true,
          (el) => this._handleDomHoverChange(el)
        );
      }
    } catch { /* ignore */ }

    await this.intersectionManager.init();

    // Visual indicator: blue focus rectangles for DOM-hover targeting mode.
    try {
      if (this.overlayManager && typeof this.overlayManager.setDomHoverFocusColorsEnabled === 'function') {
        this.overlayManager.setDomHoverFocusColorsEnabled(true);
      }
    } catch { /* ignore */ }

    this.scrollManager.init();
    // Edge-only rectangle selection is lazy-initialized on first rectangle
    // highlight session — avoid ~startup DOM discovery when only browsing.
    this.start();
    this.cursor.show();
  }

  /**
   * Initialize KeyPilot in disabled state
   */
  initializeDisabledState() {
    // Sync with early injection cursor state
    if (window.KEYPILOT_EARLY) {
      window.KEYPILOT_EARLY.setEnabled(false);
    }
    
    // Don't start event listeners or focus detector
    // Hide cursor
    this.cursor.hide();
    
    // Ensure overlays are hidden
    this.overlayManager.hideFocusOverlay();
    this.overlayManager.hideDeleteOverlay();
  }

  setupOptimizedEventListeners() {
    // Listen for scroll end events from optimized scroll manager
    if (!this._boundScrollEndHandler) {
      this._boundScrollEndHandler = (event) => {
        const { mouseX, mouseY } = event.detail || {};
        if (typeof mouseX !== 'number' || typeof mouseY !== 'number') return;
        // After scrolling, the viewport changes: re-seed incremental interactive discovery
        // so locality-first discovery stays warm without a full-document scan.
        try { this.intersectionManager?.resetDiscoveryAndSchedule?.(); } catch { /* ignore */ }
        // Prefer a cached event-derived "under element" hint to avoid a DOM hit-test on scroll-end.
        this.updateElementsUnderCursor(mouseX, mouseY, false, this._pendingMouse?.underHint || null);
      };
    }
    document.addEventListener('keypilot:scroll-end', this._boundScrollEndHandler);
    
    // Periodic performance metrics logging
    // Enable by setting window.KEYPILOT_DEBUG = true in console
    setInterval(() => {
      if (window.KEYPILOT_DEBUG) {
        this.logPerformanceMetrics();
      }
    }, 10000); // Log every 10 seconds when debug enabled

    // Performance monitoring removed
  }

  /**
   * Initialize edge-only processing for rectangle selection optimization.
   * Lazy: call only when entering rectangle highlight mode (not on enable).
   * @param {{ force?: boolean }} [opts]
   */
  initializeEdgeOnlyProcessing(opts = {}) {
    // Already ready
    if (this.rectangleIntersectionObserver) {
      return true;
    }

    // Check all feature flags for edge-only processing
    const edgeOnlyEnabled = FEATURE_FLAGS.USE_EDGE_ONLY_SELECTION && 
                           FEATURE_FLAGS.ENABLE_EDGE_ONLY_PROCESSING &&
                           this.edgeOnlyProcessingEnabled;

    if (!edgeOnlyEnabled) {
      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Edge-only processing disabled:', {
          USE_EDGE_ONLY_SELECTION: FEATURE_FLAGS.USE_EDGE_ONLY_SELECTION,
          ENABLE_EDGE_ONLY_PROCESSING: FEATURE_FLAGS.ENABLE_EDGE_ONLY_PROCESSING,
          edgeOnlyProcessingEnabled: this.edgeOnlyProcessingEnabled
        });
      }
      return false;
    }

    try {
      // Initialize rectangle intersection observer
      if (window.KEYPILOT_DEBUG || opts.force) {
        console.log('[KeyPilot] Initializing RectangleIntersectionObserver (lazy)...');
      }
      this.rectangleIntersectionObserver = new RectangleIntersectionObserver();
      
      if (this.rectangleIntersectionObserver) {
        console.log('[KeyPilot] RectangleIntersectionObserver initialized successfully');
      }
      
      // Initialize with callback for intersection changes
      this.rectangleIntersectionObserver.initialize((intersectionData) => {
        this.handleEdgeOnlyIntersectionChange(intersectionData);
      });
      
      // Performance monitoring is now integrated into RectangleIntersectionObserver
      // No separate initialization needed

      // Initialize highlight manager with edge-only observer and notification callback
      if (this.overlayManager && this.overlayManager.highlightManager) {
        const notificationCallback = (message, type) => {
          // Map notification types to colors
          const colorMap = {
            'success': COLORS.NOTIFICATION_SUCCESS,
            'warning': COLORS.NOTIFICATION_WARNING || COLORS.NOTIFICATION_INFO,
            'error': COLORS.NOTIFICATION_ERROR,
            'info': COLORS.NOTIFICATION_INFO
          };
          
          const backgroundColor = colorMap[type] || COLORS.NOTIFICATION_INFO;
          this.showFlashNotification(message, backgroundColor);
        };
        
        this.overlayManager.highlightManager.initializeEdgeOnlyProcessing(
          this.rectangleIntersectionObserver, 
          notificationCallback
        );
      }

      if (window.KEYPILOT_DEBUG) {
        console.log('[KeyPilot Debug] Edge-only rectangle intersection observer initialized:', {
          caching: FEATURE_FLAGS.ENABLE_SELECTION_CACHING,
          batchProcessing: FEATURE_FLAGS.ENABLE_EDGE_BATCH_PROCESSING,
          predictiveCaching: FEATURE_FLAGS.ENABLE_PREDICTIVE_CACHING
        });
      }
      return true;
    } catch (error) {
      console.warn('[KeyPilot] Failed to initialize edge-only processing:', error);
      this.edgeOnlyProcessingEnabled = false;
      this.rectangleIntersectionObserver = null;
      return false;
    }
  }

  /**
   * Ensure rectangle edge-only stack exists (first rectangle session only).
   */
  ensureEdgeOnlyProcessingForRectangle() {
    if (this.rectangleIntersectionObserver) return true;
    return !!this.initializeEdgeOnlyProcessing();
  }

  /**
   * Handle intersection changes from edge-only processing
   * @param {Object} intersectionData - Data from edge-only intersection observer
   */
  handleEdgeOnlyIntersectionChange(intersectionData) {
    if (!intersectionData || !this.state.isHighlightMode()) {
      return;
    }

    try {
      // Update selection based on edge-only intersection results
      const selection = this.rectangleIntersectionObserver.createSelectionFromIntersection();
      
      if (selection) {
        this.state.setCurrentSelection(selection);
        
        // Update visual overlays
        if (this.overlayManager && this.overlayManager.highlightManager) {
          this.overlayManager.highlightManager.updateHighlightSelectionOverlays(selection);
        }
      }

      // Log performance metrics if debugging enabled
      if (window.KEYPILOT_DEBUG && FEATURE_FLAGS.DEBUG_EDGE_ONLY_PROCESSING) {
        console.log('[KeyPilot Debug] Edge-only intersection update:', {
          intersectingElements: intersectionData.intersectingElements?.length || 0,
          intersectingTextNodes: intersectionData.intersectingTextNodes?.length || 0,
          edgeProcessing: intersectionData.edgeProcessing,
          metrics: intersectionData.metrics
        });
      }
    } catch (error) {
      console.warn('[KeyPilot] Error handling edge-only intersection change:', error);
    }
  }



  /**
   * Trigger fallback from edge-only processing to spatial method
   * @param {string} reason - Reason for fallback
   */
  triggerEdgeOnlyFallback(reason) {
    if (!this.edgeOnlyProcessingEnabled) {
      return; // Already using fallback
    }

    console.warn('[KeyPilot] Triggering edge-only processing fallback:', reason);

    // Disable edge-only processing for this session
    this.edgeOnlyProcessingEnabled = false;

    // Show user notification if enabled
    if (FEATURE_FLAGS.SHOW_SELECTION_METHOD_IN_UI) {
      this.showFlashNotification(
        'Selection method switched to spatial for better performance',
        COLORS.NOTIFICATION_INFO
      );
    }

    if (window.KEYPILOT_DEBUG) {
      console.log('[KeyPilot Debug] Edge-only processing disabled, using spatial fallback');
    }
  }

  setupContinuousCursorSync() {
    // Fallback cursor sync for problematic sites
    let lastSyncTime = 0;
    const syncCursor = () => {
      const now = Date.now();
      
      // Only sync every 16ms (60fps) to avoid performance issues
      if (now - lastSyncTime > 16) {
        const currentState = this.state.getState();
        if (currentState.lastMouse.x !== -1 && currentState.lastMouse.y !== -1) {
          // Force cursor position update
          this.cursor.updatePosition(currentState.lastMouse.x, currentState.lastMouse.y);
        }
        lastSyncTime = now;
      }
      
      // Continue syncing
      requestAnimationFrame(syncCursor);
    };
    
    // Start the sync loop
    requestAnimationFrame(syncCursor);
  }

  setupStyles() {
    this.styleManager.injectSharedStyles();
  }

  setupShadowDOMSupport() {
    this.shadowDOMManager.setup();
    // Connect shadow DOM manager to intersection observer for shadow element discovery
    if (this.intersectionManager?.setShadowDOMManager) {
      this.intersectionManager.setShadowDOMManager(this.shadowDOMManager);
    }
  }

  setupPopupCommunication() {
    chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
      if (msg.type === MSG.GET_STATUS) {
        sendResponse({ mode: this.state.getState().mode });
      } else if (msg.type === MSG.TOGGLE_STATE || msg.type === MSG.UPDATE_STATE) {
        // Handle state change message from service worker.
        // Prefer the toggle handler (single path for enable/disable + control strip sync).
        if (typeof msg.enabled === 'boolean') {
          try {
            const th = window.__KeyPilotToggleHandler;
            if (th && typeof th.setEnabled === 'function') {
              void th.setEnabled(msg.enabled, false);
              return;
            }
          } catch { /* fall through */ }
          if (msg.enabled) {
            this.enable();
          } else {
            this.disable();
          }
        }
      } else if (msg.type === MSG.OPEN_SETTINGS_POPOVER) {
        try {
          // Tab-local open (top frame only)
          if (window !== window.top) return;
          this.handleOpenSettingsPopover();
        } catch (e) {
          console.warn('[KeyPilot] Failed to open settings popover via message:', e);
        }
      } else if (msg.type === MSG.OPEN_GUIDE_POPOVER) {
        try {
          // Tab-local open (top frame only)
          if (window !== window.top) return;
          this.handleOpenGuidePopover();
        } catch (e) {
          console.warn('[KeyPilot] Failed to open guide popover via message:', e);
        }
      } else if (msg.type === MSG.OPEN_ONBOARDING) {
        try {
          // Tab-local open (top frame only)
          if (window !== window.top) return;
          // Respect disabled state: when KeyPilot is off, only Alt+K should do anything.
          try {
            const th = window.__KeyPilotToggleHandler;
            if (th && typeof th.enabled === 'boolean' && th.enabled === false) return;
          } catch { /* ignore */ }
          const ob = window.__KeyPilotOnboarding;
          if (ob && typeof ob.setActive === 'function') {
            ob.setActive(true);
          }
        } catch (e) {
          console.warn('[KeyPilot] Failed to open onboarding via message:', e);
        }
      } else if (msg.type === MSG.LAUNCH_WALKTHROUGH) {
        try {
          // Tab-local: close guide/settings popover, reset + open walkthrough.
          if (window !== window.top) return;
          try {
            const th = window.__KeyPilotToggleHandler;
            if (th && typeof th.enabled === 'boolean' && th.enabled === false) return;
          } catch { /* ignore */ }
          try {
            const st = this.state?.getState?.();
            if (st?.mode === MODES.POPOVER) {
              this.handleClosePopover();
            }
          } catch { /* ignore */ }
          const ob = window.__KeyPilotOnboarding;
          if (ob && typeof ob.resetTutorial === 'function') {
            void ob.resetTutorial();
          } else if (ob && typeof ob.setActive === 'function') {
            ob.setActive(true);
          }
        } catch (e) {
          console.warn('[KeyPilot] Failed to launch walkthrough via message:', e);
        }
      }
    });
  }

  handleStateChange(newState, prevState) {
    // If we leave text focus mode, cancel any pending hover-click countdown.
    if (prevState.mode === MODES.TEXT_FOCUS && newState.mode !== MODES.TEXT_FOCUS) {
      this._disarmTextModeClick();
    }

    // Control strip: green ON → orange while a text field has focus.
    // Keyboard reference: only Click Element stays active; other keys gray out.
    if (newState.mode !== prevState.mode) {
      const inText = newState.mode === MODES.TEXT_FOCUS;
      try {
        this.controlStrip?.setTextModeActive?.(inText);
      } catch { /* ignore */ }
      try {
        this.floatingKeyboardHelp?.setTextModeFilter?.(inText);
      } catch { /* ignore */ }
    }

    // Update cursor mode (inspector kinds switch glyph without leaving INSPECTOR mode)
    if (newState.mode !== prevState.mode ||
        newState.inspectorKind !== prevState.inspectorKind ||
        (newState.mode === MODES.TEXT_FOCUS && newState.focusEl !== prevState.focusEl) ||
        (newState.mode === MODES.NONE && newState.focusEl !== prevState.focusEl)) {
      if (this._isCustomCursorModeEnabled()) {
        const options = this._buildCursorOptionsForState(newState);
        this.cursor.setMode(this._cursorModeForState(newState), options);
      }
      this.updatePopupStatus(newState.mode, newState.inspectorKind);
    }

    // Update overlays when focus/inspector targets change, mode changes (e.g. enter
    // highlight → companion "Press H again…" instruction), or an explicit trigger fires.
    if (newState.focusedTextElement !== prevState.focusedTextElement ||
        newState._overlayUpdateTrigger !== prevState._overlayUpdateTrigger ||
        newState.focusEl !== prevState.focusEl ||
        newState.inspectorEl !== prevState.inspectorEl ||
        newState.inspectorKind !== prevState.inspectorKind ||
        newState.mode !== prevState.mode) {
      this.updateOverlays(newState.focusEl, newState.inspectorEl, newState.inspectorKind);
    }

    // Keyboard reference: glow keys that activate the currently hovered link.
    // Debounced — focusEl can thrash on video thumbs / hover carousels.
    if (newState.focusEl !== prevState.focusEl || newState.mode !== prevState.mode) {
      this._syncKeyboardLinkHoverHints(newState);
    }
  }

  /**
   * Schedule (or immediately apply) link-hover keyboard key hints.
   * Debounces rapid focusEl churn (e.g. YouTube preview thumbnails).
   * @param {any} [state]
   * @param {{ immediate?: boolean }} [opts]
   */
  _syncKeyboardLinkHoverHints(state, opts = {}) {
    this._linkHoverHintPendingState = state;
    if (opts.immediate) {
      this._clearKeyboardLinkHoverHintTimer();
      this._flushKeyboardLinkHoverHints();
      return;
    }
    if (this._linkHoverHintTimer != null) {
      try { clearTimeout(this._linkHoverHintTimer); } catch { /* ignore */ }
    }
    const delay = Number.isFinite(this._LINK_HOVER_HINT_DEBOUNCE_MS)
      ? this._LINK_HOVER_HINT_DEBOUNCE_MS
      : 90;
    this._linkHoverHintTimer = setTimeout(() => {
      this._linkHoverHintTimer = null;
      this._flushKeyboardLinkHoverHints();
    }, delay);
  }

  _clearKeyboardLinkHoverHintTimer() {
    if (this._linkHoverHintTimer == null) return;
    try { clearTimeout(this._linkHoverHintTimer); } catch { /* ignore */ }
    this._linkHoverHintTimer = null;
  }

  /**
   * When the keyboard reference is open and the pointer is over a link,
   * highlight ACTIVATE / OPEN_POPOVER (and related) keys with a strong outline/glow.
   */
  _flushKeyboardLinkHoverHints() {
    try {
      const help = this.floatingKeyboardHelp;
      if (!help || typeof help.setLinkHoverHints !== 'function') return;

      // Settings → Click Mode → "Glow keys when hovering a link" (default off).
      const hintsEnabled =
        this._settings?.clickMode?.keyboardLinkHoverHints === true ||
        (this._settings == null && DEFAULT_SETTINGS.clickMode.keyboardLinkHoverHints === true);

      let isLink = false;
      if (hintsEnabled && this._keyboardHelpVisible && this.enabled && help.isVisible?.()) {
        const st = this._linkHoverHintPendingState || this.state?.getState?.();
        // Don't suggest page link actions while modal modes own the pointer.
        if (!(st?.mode === MODES.POPOVER || st?.mode === MODES.INSPECTOR ||
              st?.mode === MODES.DELETE || st?.mode === MODES.COLS ||
              st?.mode === MODES.HIGHLIGHT || st?.mode === MODES.OMNIBOX)) {
          const focusEl = st?.focusEl;
          // Category-based: only true navigational links (not sliders / media chrome).
          isLink = this.detector?.getClickableCategory?.(focusEl) === CLICKABLE_CATEGORY.LINK;
        }
      }

      // Skip redundant DOM class toggles when the boolean hasn't changed.
      if (this._linkHoverHintLastApplied === isLink) return;
      this._linkHoverHintLastApplied = isLink;
      help.setLinkHoverHints(isLink);
    } catch { /* ignore */ }
  }

  _isCustomCursorModeEnabled() {
    const mode = this._settings?.cursorMode || DEFAULT_SETTINGS.cursorMode;
    return mode === CURSOR_MODE.CUSTOM_CURSORS;
  }

  /**
   * @param {string} mode
   * @param {string|null} [inspectorKind]
   */
  updatePopupStatus(mode, inspectorKind = null) {
    try {
      // Only the top frame should push status updates to the extension popup.
      // If we run KeyPilot inside a popover iframe, sending KP_STATUS from that frame
      // would overwrite the popup UI with the iframe's mode.
      if (window !== window.top) return;
      // Map shared inspector mode → kind-specific status labels (DELETE / COLS).
      const statusMode = mode === MODES.INSPECTOR
        ? getInspectorStatusMode(inspectorKind || this.state.getState()?.inspectorKind)
        : mode;
      this._sendRuntimeMessage({ type: MSG.STATUS, mode: statusMode }, { silent: true });
    } catch (error) {
      // Popup might not be open / extension context may be invalidated
    }
  }

  /**
   * Send a message to the service worker, handling "Extension context invalidated"
   * (content script orphaned after extension reload/update).
   * @param {object} message
   * @param {{ silent?: boolean, onResponse?: (response: unknown) => void }} [opts]
   * @returns {boolean}
   */
  _sendRuntimeMessage(message, opts = {}) {
    return safeRuntimeSendMessage(message, {
      onInvalidated: () => this._handleExtensionContextInvalidated(),
      onError: (error) => {
        if (opts.silent) return;
        if (window.KEYPILOT_DEBUG) {
          console.warn('[KeyPilot] runtime.sendMessage failed:', error);
        }
      },
      onResponse: typeof opts.onResponse === 'function' ? opts.onResponse : undefined
    });
  }

  /**
   * One-time UX + quiet shutdown after the extension was reloaded/updated.
   */
  _handleExtensionContextInvalidated() {
    if (this._extensionContextInvalidatedHandled) return;
    this._extensionContextInvalidatedHandled = true;

    if (window.KEYPILOT_DEBUG) {
      console.warn('[KeyPilot] Extension context invalidated — reload the page to restore KeyPilot');
    }

    try {
      this.showFlashNotification(
        'KeyPilot updated — reload this page to restore shortcuts',
        COLORS.NOTIFICATION_WARNING
      );
    } catch { /* ignore */ }

    // Stop listening so orphaned content scripts don't keep throwing.
    try {
      if (typeof this.disable === 'function') {
        this.disable();
      }
    } catch { /* ignore */ }
  }

  handleKeyDown(e) {
    // Handle Alt+K toggle FIRST, regardless of extension state
    // Check for Alt key (covers both left and right Alt) and K key (case insensitive)
    if ((e.altKey || e.code === 'AltRight') && (e.key === 'k' || e.key === 'K' || e.code === 'KeyK')) {
      // Mark the event so any other KeyPilot-installed listeners don't double-toggle.
      try { e.__kpToggleHandled = true; } catch { /* ignore */ }
      e.preventDefault();
      e.stopPropagation();
      // Important: stop other document-level keydown capture listeners from also firing,
      // which could otherwise trigger a second toggle and effectively do nothing.
      e.stopImmediatePropagation();
      // Send toggle message to background script
      this._sendRuntimeMessage({ type: MSG.TOGGLE_STATE }, { silent: true });
      return;
    }

    // Alt+J: toggle control strip (works even when closed; persists visibility).
    // Also handled by KeyPilotToggleHandler's always-on listener when disabled.
    if ((e.altKey || e.code === 'AltRight') && (e.key === 'j' || e.key === 'J' || e.code === 'KeyJ')) {
      if (e.__kpControlStripHandled) return;
      try { e.__kpControlStripHandled = true; } catch { /* ignore */ }
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      void this.toggleControlStripFromHotkey();
      return;
    }

    // Alt+L: open omnibox (top frame only)
    if ((e.altKey || e.code === 'AltRight') && (e.key === 'l' || e.key === 'L' || e.code === 'KeyL')) {
      // Only operate in the top frame to avoid duplicates.
      if (window !== window.top) return;
      // Respect enabled state (do nothing when disabled).
      if (!this.enabled) return;
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();

      const currentState = this.state.getState();
      if (currentState.mode === MODES.OMNIBOX) {
        this.handleCloseOmnibox();
      } else {
        this.handleOpenOmnibox();
      }
      return;
    }

    // Alt+;: open launcher with search focused (Alt+A for left-handed)
    if ((e.altKey || e.code === 'AltRight') && (e.key === ';' || e.key === ':' || e.code === 'Semicolon' || e.key === 'a' || e.key === 'A' || e.code === 'KeyA')) {
      // Only operate in the top frame to avoid duplicates.
      if (window !== window.top) return;
      // Respect enabled state (do nothing when disabled).
      if (!this.enabled) return;
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();

      if (this.launcherPopover.isOpen()) {
        this.launcherPopover.hide();
      } else {
        this.launcherPopover.showWithSearchFocus();
      }
      return;
    }

    // Don't handle keys if extension is disabled
    if (!this.enabled) {
      return;
    }

    // Live key feedback on the floating keyboard reference.
    // Must run before any stopImmediatePropagation() below — claimed shortcuts
    // would otherwise never reach FloatingKeyboardHelp's document listener
    // (EventManager registers its capture handler first).
    this._reflectKeyOnKeyboardHelp(e, 'down');

    // Debug key presses
    console.log('[KeyPilot] Key pressed:', e.key, 'Code:', e.code);

    // Don't interfere with modifier key combinations (Cmd+C, Ctrl+V, etc.)
    if (this.hasModifierKeys(e)) {
      return;
    }

    // Inside a KeyPilot popover iframe: parent auto-focuses us so F/hover work, but
    // close keys (Esc / E / P) must still request parent close. In-frame KeyPilot can
    // register after the bridge and win capture order, so we own these here too.
    try {
      if (window !== window.top && window.__KP_POPOVER_IFRAME) {
        const closeKeys = Array.isArray(window.__KP_POPOVER_CLOSE_KEYS) && window.__KP_POPOVER_CLOSE_KEYS.length
          ? window.__KP_POPOVER_CLOSE_KEYS
          : ['Escape', 'e', 'E', 'p', 'P'];
        const k = e.key;
        const isEsc = k === 'Escape' || k === 'Esc';
        if (isEsc || closeKeys.includes(k)) {
          // Never trap letter close keys while typing in the iframe page.
          if (!isEsc && this._isUnsafeToRunActionKey?.(e)) {
            /* fall through to normal typing */
          } else {
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation();
            try {
              window.parent.postMessage({ type: 'KP_POPOVER_REQUEST_CLOSE', key: k }, '*');
            } catch { /* ignore */ }
            return;
          }
        }
      }
    } catch { /* ignore */ }

    const currentState = this.state.getState();
    const KB = this.keybindings || {};

    // Global default: Escape closes the frontmost "popover-like" UI.
    // Priority: omnibox → launcher → PopupManager (topmost modal panel).
    // Match both e.key and e.code so Escape is reliable across layouts.
    const isCancelKey = KB.CANCEL?.keys?.includes?.(e.key)
      || e.key === 'Escape'
      || e.code === 'Escape';
    if (isCancelKey) {
      // Omnibox
      try {
        if (currentState.mode === MODES.OMNIBOX || this.omniboxManager?.isOpen?.()) {
          e.preventDefault();
          e.stopPropagation();
          e.stopImmediatePropagation();
          this.handleCloseOmnibox();
          return;
        }
      } catch { /* ignore */ }

      // Launcher (before generic PopupManager so we always clear launcher state,
      // even if the modal stack entry was lost or show() is still in flight).
      try {
        if (this.launcherPopover?.isOpen?.()) {
          e.preventDefault();
          e.stopPropagation();
          e.stopImmediatePropagation();
          this.launcherPopover.hide();
          return;
        }
      } catch { /* ignore */ }

      // Popup stack (iframe popovers, settings/guide, etc.)
      try {
        const pm = this.overlayManager?.popupManager;
        if (pm?.isOpen?.()) {
          e.preventDefault();
          e.stopPropagation();
          e.stopImmediatePropagation();
          pm.requestCloseTop?.();
          return;
        }
      } catch { /* ignore */ }
    }

    // If launcher is open, delegate keyboard handling to it
    if (this.launcherPopover?.isOpen?.()) {
      const handled = this.launcherPopover.handleKeyDown(e);
      if (handled) {
        return;
      }
    }

    // If omnibox is open, let its input handler do the work.
    if (currentState.mode === MODES.OMNIBOX) {
      // Escape should always close omnibox.
      if (KB.CANCEL?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleCloseOmnibox();
      }
      return;
    }

    // Check if popover is open
    if (currentState.mode === MODES.POPOVER) {
      console.log('[KeyPilot] Popover mode active, key pressed:', e.key);
      
      // Escape should close the popover (always, regardless of where it's pressed)
      if (KB.CANCEL?.keys?.includes?.(e.key)) {
        console.log('[KeyPilot] Escape key pressed while popover open');
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleClosePopover();
        return;
      }

      // Scroll shortcuts should scroll the popover iframe (not the parent page)
      // This path covers cases where focus is still in the parent document.
      if (KB.PAGE_UP?.keys?.includes?.(e.key)) {
        e.preventDefault();
        this.overlayManager?.scrollPopoverBy?.(-this._getPageScrollPx(), this._getScrollBehavior());
        return;
      }
      if (KB.PAGE_DOWN?.keys?.includes?.(e.key)) {
        e.preventDefault();
        this.overlayManager?.scrollPopoverBy?.(this._getPageScrollPx(), this._getScrollBehavior());
        return;
      }
      if (KB.PAGE_UP_INSTANT?.keys?.includes?.(e.key)) {
        e.preventDefault();
        this.overlayManager?.scrollPopoverBy?.(-this._getHalfPageScrollPx(), this._getScrollBehavior());
        return;
      }
      if (KB.PAGE_DOWN_INSTANT?.keys?.includes?.(e.key)) {
        e.preventDefault();
        this.overlayManager?.scrollPopoverBy?.(this._getHalfPageScrollPx(), this._getScrollBehavior());
        return;
      }
      if (KB.PAGE_TOP?.keys?.includes?.(e.key)) {
        e.preventDefault();
        this.overlayManager?.scrollPopoverToTop?.(this._getScrollBehavior());
        return;
      }
      if (KB.PAGE_BOTTOM?.keys?.includes?.(e.key)) {
        e.preventDefault();
        this.overlayManager?.scrollPopoverToBottom?.(this._getScrollBehavior());
        return;
      }

      // Allow click keys to interact with popover UI (e.g. the × close button).
      // Popovers are normal z-index layers (not the browser top-layer), so the
      // green rectangle + F-to-click should work again.
      if (KB.ACTIVATE?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleActivateKey();
        return;
      }
      if (KB.ACTIVATE_NEW_TAB?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleActivateNewTabKey();
        return;
      }
      if (KB.ACTIVATE_NEW_TAB_BACKGROUND?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleActivateNewTabBackgroundKey();
        return;
      }

      // Allow preview link popover toggle (E/W) to work even when popover is open
      if (KB.PREVIEW_LINK_POPOVER?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handlePreviewLinkPopover();
        return;
      }

      // Toggle popovers must work on a second press while mode is POPOVER.
      // Tab History / Open Popover set MODES.POPOVER, which used to swallow their
      // own keys and made J (and P) feel stuck open.
      if (KB.TAB_HISTORY?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleToggleTabHistoryPopover();
        return;
      }
      if (KB.OPEN_POPOVER?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleOpenPopover(e);
        return;
      }
      if (KB.LAUNCHER?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleLauncherKey(e);
        return;
      }
      if (KB.TOGGLE_KEYBOARD_HELP?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleToggleKeyboardHelp();
        return;
      }
      if (KB.OPEN_SETTINGS_POPOVER?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleToggleSettingsPopover();
        return;
      }

      // History navigation must work even while a popover is open (parent focus).
      // Without this, D/S/R are silently swallowed and feel like they need a second press
      // after the popover is closed.
      if (KB.BACK?.keys?.includes?.(e.key) || KB.BACK2?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleBackKey();
        return;
      }
      if (KB.FORWARD?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.handleForwardKey();
        return;
      }

      // For all other keys when popover is open, let them pass through to the iframe
      // Don't prevent default so the iframe can handle navigation keys
      console.log('[KeyPilot] Letting key pass through to popover/iframe');
      return;
    }

    // -------------------------------------------------------------------------
    // FAIL-CLOSED typing gate (after modal UI, before action-key dispatch).
    // Google.com autofocuses <textarea name=q> before our content script loads;
    // we must suppress Close Tab (A), etc. even when text_focus mode lagged.
    // -------------------------------------------------------------------------
    const typingGate = this._evaluateTypingGate(e, currentState, KB);
    if (typingGate.blockActions) {
      if (typingGate.handled) return;
      // Swallow KeyPilot shortcuts; let the character type into the field.
      return;
    }

    // Special handling for highlight mode — complete with H/Y (layout-bound), cancel with Esc
    // or any other key (which then falls through to its normal action).
    if (currentState.mode === MODES.HIGHLIGHT) {
      if (KB.CANCEL?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        this.cancelHighlightMode();
        return;
      }
      if (KB.HIGHLIGHT?.keys?.includes?.(e.key) || KB.RECTANGLE_HIGHLIGHT?.keys?.includes?.(e.key)) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        // Fire-and-forget; completeSelection always exits highlight mode.
        void this.completeSelection().catch((err) => {
          console.error('[KeyPilot] completeSelection failed:', err);
          try { this.cancelHighlightMode(); } catch { /* ignore */ }
        });
        return;
      }
      // Any other key — cancel highlight mode and let the key run normally
      this.cancelHighlightMode();
    }

    // Handle our keyboard shortcuts (table-driven via KEYBINDINGS.*.handler)
    for (const keybinding of Object.values(KB)) {
      if (!keybinding?.handler || !Array.isArray(keybinding.keys)) continue;

      const matchOn = Array.isArray(keybinding.matchOn) ? keybinding.matchOn : ['key'];
      const isMatch = matchOn.some((field) => keybinding.keys.includes(e[field]));
      if (!isMatch) continue;

      // Final fail-closed guard: never dispatch action keys while typing.
      if (this._isUnsafeToRunActionKey(e)) {
        console.warn(
          '[KeyPilot] Suppressed action while typing:',
          keybinding.handler,
          'key=',
          e.key,
          'active=',
          document.activeElement && document.activeElement.tagName,
          document.activeElement && document.activeElement.id
        );
        return;
      }

      e.preventDefault();
      // Critical: when KeyPilot claims a key, we must fully consume it so page-level
      // shortcuts (e.g. Internet Archive BookReader 'f' fullscreen) don't also fire.
      // We use stopImmediatePropagation() to prevent other listeners on the same target
      // from running (including other capture listeners).
      e.stopPropagation();
      e.stopImmediatePropagation();

      const handlerFn = this[keybinding.handler];
      if (typeof handlerFn === 'function') {
        // Pass the event so handlers can re-check typing with the real event target.
        handlerFn.call(this, e);
      } else {
        console.warn('[KeyPilot] Missing keybinding handler:', keybinding.handler, keybinding);
      }
      return;
    }
  }

  /**
   * Dependency-free: is this element a place where letter keys should type?
   * Intentionally does NOT call isTypingContext/resolveTypingTarget (avoids any
   * bundle name-shadowing or swallowed-throw fail-open paths).
   * @param {any} el
   * @returns {boolean}
   */
  _isTextEntryElement(el) {
    if (!el) return false;
    try {
      // Text node inside contenteditable
      if (el.nodeType === 3) el = el.parentElement;
    } catch { /* ignore */ }
    if (!el || el.nodeType !== 1) return false;

    try {
      // Never treat KeyPilot chrome as page typing (omnibox has its own mode).
      if (el.classList?.contains?.(CSS_CLASSES.OMNIBOX_INPUT)) return false;
      if (el.closest?.(`.${CSS_CLASSES.OMNIBOX_BACKDROP}`) ||
          el.closest?.(`.${CSS_CLASSES.OMNIBOX_PANEL}`)) {
        return false;
      }
    } catch { /* ignore */ }

    try {
      if (el.isContentEditable) return true;
    } catch { /* ignore */ }

    let tag = '';
    try { tag = String(el.tagName || '').toUpperCase(); } catch { tag = ''; }

    if (tag === 'TEXTAREA') {
      try { return !el.disabled; } catch { return true; }
    }

    if (tag === 'INPUT') {
      try {
        if (el.disabled || el.readOnly) return false;
      } catch { /* ignore */ }
      let type = 'text';
      try { type = String(el.type || 'text').toLowerCase(); } catch { /* ignore */ }
      // Everything that accepts keyboard text entry; exclude pure click controls.
      if (
        type === 'button' || type === 'submit' || type === 'reset' ||
        type === 'checkbox' || type === 'radio' || type === 'file' ||
        type === 'image' || type === 'range' || type === 'color' ||
        type === 'hidden'
      ) {
        return false;
      }
      return true;
    }

    // ARIA text fields (custom widgets)
    try {
      const role = el.getAttribute?.('role');
      if (role === 'textbox' || role === 'searchbox') return true;
      // Google uses role=combobox on a real <textarea> (already handled). For
      // custom comboboxes that accept typing, require contentEditable or aria-multiline.
      if (role === 'combobox') {
        if (el.isContentEditable) return true;
        if (el.getAttribute?.('aria-multiline') === 'true') return true;
        // If it has a value property and isn't a native select, treat as typing.
        if ('value' in el && tag !== 'SELECT') return true;
      }
    } catch { /* ignore */ }

    // Walk up for contenteditable hosts (event target may be a child span).
    try {
      let p = el.parentElement;
      let depth = 0;
      while (p && depth++ < 6) {
        if (p.isContentEditable) return true;
        p = p.parentElement;
      }
    } catch { /* ignore */ }

    return false;
  }

  /**
   * Find the live typing element from event + focus, without helper indirection.
   * @param {KeyboardEvent|null|undefined} e
   * @returns {Element|null}
   */
  _findLiveTypingElement(e) {
    const candidates = [];

    // 1) composedPath (shadow-aware real target)
    try {
      const path = e && typeof e.composedPath === 'function' ? e.composedPath() : null;
      if (path) {
        for (const n of path) {
          if (n && n.nodeType === 1) {
            candidates.push(n);
            break;
          }
        }
      }
    } catch { /* ignore */ }

    // 2) event.target
    try {
      if (e?.target) candidates.push(e.target);
    } catch { /* ignore */ }

    // 3) deep activeElement (open shadow)
    try {
      let active = document.activeElement;
      let guard = 0;
      while (active && active.shadowRoot && active.shadowRoot.activeElement && guard++ < 10) {
        active = active.shadowRoot.activeElement;
      }
      if (active) candidates.push(active);
    } catch { /* ignore */ }

    // 4) FocusDetector memory
    try {
      const cur = this.focusDetector?.currentFocusedElement;
      if (cur) candidates.push(cur);
    } catch { /* ignore */ }

    // 5) state.focusedTextElement
    try {
      const st = this.state?.getState?.();
      if (st?.focusedTextElement) candidates.push(st.focusedTextElement);
    } catch { /* ignore */ }

    for (const c of candidates) {
      if (this._isTextEntryElement(c)) {
        // Prefer the actual element (not a text node parent already resolved)
        return /** @type {Element} */ (c.nodeType === 1 ? c : c.parentElement);
      }
    }
    return null;
  }

  /**
   * True when KeyPilot action keys (close tab, new tab, etc.) must not run.
   * Fail-closed for any detected text entry surface.
   * @param {KeyboardEvent|null|undefined} [e]
   * @returns {boolean}
   */
  _isUnsafeToRunActionKey(e) {
    try {
      const st = this.state?.getState?.();
      if (st?.mode === MODES.TEXT_FOCUS) return true;
      if (st?.focusedTextElement) return true;
    } catch { /* ignore */ }

    try {
      if (this.focusDetector?.isInTextFocus?.()) return true;
    } catch { /* ignore */ }

    if (this._findLiveTypingElement(e)) return true;

    // Last resort: call shared helpers (may throw after bundle issues — ignore).
    try {
      if (typeof this.resolveTypingTarget === 'function' && this.resolveTypingTarget(e)) {
        return true;
      }
    } catch { /* ignore */ }
    try {
      if (this.isTypingContext?.(e?.target)) return true;
    } catch { /* ignore */ }

    return false;
  }

  /**
   * Call at the top of every tab/nav action handler.
   * @param {string} actionName
   * @param {KeyboardEvent|null|undefined} [e]
   * @returns {boolean} true if the action is allowed to proceed
   */
  _allowActionKey(actionName, e) {
    if (this._isUnsafeToRunActionKey(e)) {
      console.warn(
        `[KeyPilot] ${actionName} blocked — typing context`,
        'active=',
        document.activeElement && document.activeElement.tagName,
        document.activeElement && document.activeElement.id
      );
      return false;
    }
    return true;
  }

  /**
   * Early typing gate for handleKeyDown.
   * @param {KeyboardEvent} e
   * @param {any} currentState
   * @param {Record<string, any>} KB
   * @returns {{ blockActions: boolean, handled: boolean }}
   */
  _evaluateTypingGate(e, currentState, KB) {
    const inTextFocus =
      currentState?.mode === MODES.TEXT_FOCUS || !!currentState?.focusedTextElement;

    const typingTarget = this._findLiveTypingElement(e);

    if (!inTextFocus && !typingTarget) {
      return { blockActions: false, handled: false };
    }

    // Sync text-focus mode if we only discovered typing via the DOM probe.
    if (typingTarget && !inTextFocus) {
      try {
        if (this.focusDetector?.isTextInput?.(typingTarget)) {
          this.focusDetector.setTextFocus(typingTarget);
        } else if (this.focusDetector?.setTextFocus) {
          // isTextInput may be stricter; still force mode when our probe matched.
          try { this.focusDetector.setTextFocus(typingTarget); } catch { /* ignore */ }
        }
      } catch { /* ignore */ }
    }

    // ESC exits text focus.
    if (KB.CANCEL?.keys?.includes?.(e.key) || e.key === 'Escape' || e.code === 'Escape') {
      console.debug('Escape key detected in text focus / typing context');
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      try {
        this.handleEscapeFromTextFocus(currentState || this.state.getState());
      } catch {
        try { this.focusDetector?.clearTextFocus?.(); } catch { /* ignore */ }
      }
      return { blockActions: true, handled: true };
    }

    // Text mode isolation (product rule): keyboard-ref actions do NOT run while typing.
    // Only exceptions:
    //   1) Esc — exit text mode
    //   2) Armed F — optional hover-click after a mouse-move countdown (explicit UX)
    // S/D/R, A, T, and every other binding must type into the field, not navigate.
    if (
      (inTextFocus || !!typingTarget) &&
      KB.ACTIVATE?.keys?.includes?.(e.key) &&
      this._textModeClickArmed &&
      currentState?.focusEl
    ) {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      this._handleActivateFromTextFocus(currentState);
      return { blockActions: true, handled: true };
    }

    // Swallow all KeyPilot shortcuts; let the character reach the text field.
    return { blockActions: true, handled: false };
  }

  handleMouseMove(e) {
    // Don't handle mouse events if extension is disabled
    if (!this.enabled) {
      return;
    }

    // Store mouse position immediately to prevent sync issues
    const x = e.clientX;
    const y = e.clientY;
    
    this.state.setMousePosition(x, y);
    this.cursor.updatePosition(x, y);

    // Update current mouse position in coordinate manager for beforeunload storage
    this.mouseCoordinateManager.updateCurrentMousePosition(x, y);

    // Coalesce hover detection to once-per-frame to avoid doing hit-testing at high mouse Hz.
    // Also extract an "under element" hint from the event path so we can skip elementFromPoint.
    this._pendingMouse.x = x;
    this._pendingMouse.y = y;
    this._pendingMouse.underHint = this._getUnderElementHintFromMouseEvent(e);

    // DOM-hover only: avoid explicit hover hit-testing in normal mode.
    // The browser resolves occlusion/clipping; focusEl comes from hover listeners.
    try {
      const st = this.state.getState();
      if (this._domHoverListenersEnabled && st.mode === MODES.NONE) {
        return;
      }
    } catch { /* ignore */ }

    // Skip hit-testing during scrolling - overlay is already hidden at scroll start
    try {
      if (this.scrollManager && this.scrollManager.isScrolling) {
        return;
      }
    } catch { /* ignore */ }

    if (this._mouseMoveRAF) return;
    this._mouseMoveRAF = window.requestAnimationFrame(() => {
      this._mouseMoveRAF = 0;
      if (!this.enabled) return;
      // Use optimized element detection with threshold (tagged as real mouse-move)
      this.updateElementsUnderCursorWithThreshold(
        this._pendingMouse.x,
        this._pendingMouse.y,
        true,
        this._pendingMouse.underHint
      );
    });
  }

  handleScroll(e) {
    // Don't handle scroll events if extension is disabled
    if (!this.enabled) {
      return;
    }

    // Delegate scroll handling to optimized scroll manager
    // The scroll manager handles all the optimization logic
    return; // OptimizedScrollManager handles scroll events directly
  }

  updateElementsUnderCursorWithThreshold(x, y, fromMouseMove = false, underHint = null) {
    // Check if mouse has moved enough to warrant a new query
    const deltaX = Math.abs(x - this.lastQueryPosition.x);
    const deltaY = Math.abs(y - this.lastQueryPosition.y);

    if (deltaX < this.MOUSE_QUERY_THRESHOLD && deltaY < this.MOUSE_QUERY_THRESHOLD) {
      // Mouse hasn't moved enough, skip the query entirely
      // No need to check cursor consistency when mouse is idle - cursor should remain stable
      return;
    }

    // Update last query position
    this.lastQueryPosition.x = x;
    this.lastQueryPosition.y = y;

    // Perform the actual element query
    this.updateElementsUnderCursor(x, y, fromMouseMove, underHint);
  }


  updateElementsUnderCursor(x, y, fromMouseMove = false, underHint = null) {
    const currentState = this.state.getState();

    this.performanceMetrics.mouseQueries++;

    // DOM-hover only for normal browsing; this path is for delete / popover / text-focus
    // and other modes that still need explicit under-cursor resolution via elementFromPoint.
    let under = underHint || null;
    if (!under) under = this.detector.deepElementFromPoint(x, y);

    let clickable = null;
    try {
      clickable = this.detector.findClickable(under);
    } catch {
      clickable = null;
    }

    // Popover mode is modal: only track elements inside the popover UI.
    // This prevents the green rectangle from following the background page.
    if (currentState.mode === MODES.POPOVER) {
      const isInsidePopover = (el) => this._isElementInPopover(el);

      if (!isInsidePopover(under)) clickable = null;
      else if (clickable && !isInsidePopover(clickable)) clickable = null;

      // When the pointer is over the popover iframe, the top document can only "see" the <iframe>,
      // not the elements inside it. Highlighting the iframe border is distracting and misleading
      // (actual interaction happens inside the iframe document), so suppress hover focus on iframes.
      try {
        if (under && under.tagName === 'IFRAME') clickable = null;
        if (clickable && clickable.tagName === 'IFRAME') clickable = null;
      } catch { /* ignore */ }
    }
    
    // In text focus mode, exclude the currently focused text element from being considered clickable
    if (currentState.mode === MODES.TEXT_FOCUS && currentState.focusedTextElement && clickable === currentState.focusedTextElement) {
      clickable = null;
    }

    // In text focus mode, only arm hover-click UI on real mouse movement.
    // Any other refresh paths (scroll-end, post-click refresh, etc.) should not arm clicks.
    if (currentState.mode === MODES.TEXT_FOCUS && !fromMouseMove) {
      // Ensure we don't stay armed without user intention.
      if (this._textModeClickArmed || currentState.focusEl) {
        this._disarmTextModeClick();
        if (currentState.focusEl) this.state.setFocusElement(null);
      } else {
        try { this.overlayManager?.setHoverClickLabelText?.('F clicks'); } catch { /* ignore */ }
      }

      // Clear inspector hover target when not in inspector mode
      if (currentState.inspectorEl) this.state.setInspectorElement(null);
      return;
    }
    
    // Track with intersection manager for performance metrics and caching.
    // Pass through computed values to avoid redundant deepElementFromPoint() work.
    this.intersectionManager.trackElementAtPoint(x, y, under, clickable);

    // Debug logging when debug mode is enabled
    if (window.KEYPILOT_DEBUG && clickable) {
      console.log('[KeyPilot Debug] Found clickable element:', {
        tagName: clickable.tagName,
        href: clickable.href,
        className: clickable.className,
        text: clickable.textContent?.substring(0, 50),
        mode: currentState.mode
      });
    }

    // Reduce overlay churn: only update state if focus element actually changed.
    if (clickable !== currentState.focusEl) {
      this.state.setFocusElement(clickable);
    }

    // In text focus mode, (re)arm countdown on mouse-move hover changes.
    if (currentState.mode === MODES.TEXT_FOCUS) {
      if (clickable) {
        if (!this._textModeClickArmed || this._textModeClickArmedTarget !== clickable) {
          this._armTextModeClick(clickable);
        }
      } else {
        this._disarmTextModeClick();
      }
    }

    // Shared inspector pick mode: track any element under cursor (not just clickables).
    this.inspector.updateHover(under);

    // Update text selection in highlight mode
    if (this.state.isHighlightMode()) {
      this.updateSelection();
    }
  }

  /**
   * C / V scroll distance (px), from Settings with SCROLL fallback.
   * @returns {number}
   */
  _getHalfPageScrollPx() {
    const n = Number(this._settings?.scroll?.halfPagePx);
    if (Number.isFinite(n) && n > 0) return n;
    return SCROLL.HALF_PAGE_PX;
  }

  /**
   * Z / X scroll distance (px). Currently uses the constant; not yet a setting.
   * @returns {number}
   */
  _getPageScrollPx() {
    return SCROLL.PAGE_PX;
  }

  /**
   * CSS scroll-behavior derived from Settings scroll.speed.
   * @returns {'smooth'|'auto'}
   */
  _getScrollBehavior() {
    return scrollBehaviorFromSpeed(this._settings?.scroll?.speed ?? DEFAULT_SETTINGS.scroll.speed);
  }

  handlePageUp(e) {
    if (!this._allowActionKey('handlePageUp', e)) return;
    window.scrollBy({
      top: -this._getPageScrollPx(),
      behavior: this._getScrollBehavior()
    });
    this.emitAction('scrollUp');
  }

  handlePageDown(e) {
    if (!this._allowActionKey('handlePageDown', e)) return;
    window.scrollBy({
      top: this._getPageScrollPx(),
      behavior: this._getScrollBehavior()
    });
    this.emitAction('scrollDown');
  }

  handleInstantPageUp(e) {
    if (!this._allowActionKey('handleInstantPageUp', e)) return;
    this._scrollHalfPageAtCursor(-1);
    this.emitAction('scrollUp');
  }

  handleInstantPageDown(e) {
    if (!this._allowActionKey('handleInstantPageDown', e)) return;
    this._scrollHalfPageAtCursor(1);
    this.emitAction('scrollDown');
  }

  /**
   * C / V: scroll the overflow container under the cursor first (vertical, or
   * horizontal when that container only/can scroll on X). Falls back to the
   * page when nothing nested can move. Forwards into iframes via the light
   * frame-click-agent (FRAME_SCROLL).
   *
   * @param {number} sign  -1 = C (up/left), +1 = V (down/right)
   */
  _scrollHalfPageAtCursor(sign) {
    const delta = this._getHalfPageScrollPx();
    const behavior = this._getScrollBehavior();
    const s = sign < 0 ? -1 : 1;
    const st = this.state.getState();
    let x = Number(st?.lastMouse?.x);
    let y = Number(st?.lastMouse?.y);
    if (!Number.isFinite(x) || !Number.isFinite(y) || x < 0 || y < 0) {
      x = Math.floor(window.innerWidth / 2);
      y = Math.floor(window.innerHeight / 2);
    }

    // Iframe under cursor: top hit-testing only sees the shell.
    if (this._tryScrollIframeUnderCursor(x, y, s, delta, behavior)) {
      return;
    }

    scrollAtPoint(x, y, s, delta, behavior);
  }

  /**
   * When the pointer is over an iframe, scroll inside that frame at local
   * coordinates (same-origin directly; cross-origin via FRAME_SCROLL agent).
   *
   * @param {number} clientX
   * @param {number} clientY
   * @param {number} sign
   * @param {number} deltaPx
   * @param {ScrollBehavior} behavior
   * @returns {boolean} true if an iframe under the cursor was targeted
   */
  _tryScrollIframeUnderCursor(clientX, clientY, sign, deltaPx, behavior) {
    if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return false;

    let under = null;
    try {
      under = this.detector?.deepElementFromPoint?.(clientX, clientY)
        || elementFromPointDeep(clientX, clientY);
    } catch {
      under = null;
    }
    if (!under || (under.tagName !== 'IFRAME' && under.tagName !== 'FRAME')) {
      return false;
    }

    try {
      if (this._isKeyPilotUiElement?.(under)) return false;
    } catch { /* ignore */ }

    const iframe = /** @type {HTMLIFrameElement} */ (under);
    let rect;
    try {
      rect = iframe.getBoundingClientRect();
    } catch {
      return false;
    }
    if (!rect || rect.width <= 0 || rect.height <= 0) return false;

    const localX = clientX - rect.left;
    const localY = clientY - rect.top;
    if (localX < 0 || localY < 0 || localX > rect.width || localY > rect.height) {
      return false;
    }

    const payload = {
      type: MSG.FRAME_SCROLL,
      clientX: localX,
      clientY: localY,
      sign: sign < 0 ? -1 : 1,
      deltaPx: Math.abs(Number(deltaPx)) || 0,
      behavior: behavior === 'auto' || behavior === 'instant' ? 'auto' : 'smooth',
      frameName: typeof iframe.name === 'string' ? iframe.name : ''
    };

    // Same-origin: scroll nested overflow inside the child document directly.
    try {
      const doc = iframe.contentDocument;
      const view = iframe.contentWindow;
      if (doc && view) {
        // Nested iframe: re-forward with coordinates local to the nested frame.
        let el = null;
        try {
          el = elementFromPointDeep(localX, localY, doc);
        } catch { el = null; }
        if (el && (el.tagName === 'IFRAME' || el.tagName === 'FRAME')) {
          try {
            const nested = /** @type {HTMLIFrameElement} */ (el);
            const nr = nested.getBoundingClientRect();
            nested.contentWindow?.postMessage({
              ...payload,
              clientX: localX - nr.left,
              clientY: localY - nr.top,
              frameName: typeof nested.name === 'string' ? nested.name : ''
            }, '*');
            return true;
          } catch { /* fall through to scroll this frame */ }
        }
        scrollAtPoint(localX, localY, payload.sign, payload.deltaPx, payload.behavior, {
          doc,
          win: view
        });
        return true;
      }
    } catch {
      // Cross-origin — postMessage / runtime relay.
    }

    let posted = false;
    try {
      const win = iframe.contentWindow;
      if (win) {
        win.postMessage(payload, '*');
        posted = true;
      }
    } catch { /* ignore */ }

    try {
      this._sendRuntimeMessage(payload, { silent: true });
      posted = true;
    } catch { /* ignore */ }

    return posted;
  }

  handlePageTop(e) {
    if (!this._allowActionKey('handlePageTop', e)) return;
    // Scroll to top of page (Home key equivalent)
    window.scrollTo({
      top: 0,
      behavior: this._getScrollBehavior()
    });
    this.emitAction('scrollTop');
  }

  handlePageBottom(e) {
    if (!this._allowActionKey('handlePageBottom', e)) return;
    // Scroll to bottom of page (End key equivalent)
    window.scrollTo({
      top: document.documentElement.scrollHeight,
      behavior: this._getScrollBehavior()
    });
    this.emitAction('scrollBottom');
  }

  /**
   * True when the event matches browser history back/forward keybindings.
   * @param {Record<string, any>} KB
   * @param {KeyboardEvent} e
   */
  _isHistoryNavigationKey(KB, e) {
    if (!KB || !e) return false;
    return !!(
      KB.BACK?.keys?.includes?.(e.key) ||
      KB.BACK2?.keys?.includes?.(e.key) ||
      KB.FORWARD?.keys?.includes?.(e.key)
    );
  }

  /**
   * Run the history handler that matches this key (back vs forward).
   * @param {Record<string, any>} KB
   * @param {KeyboardEvent} e
   */
  _runHistoryNavigationKey(KB, e) {
    if (KB.FORWARD?.keys?.includes?.(e.key)) {
      this.handleForwardKey();
      return;
    }
    // BACK and BACK2 share the same handler.
    this.handleBackKey();
  }

  handleBackKey(e) {
    // Never run history shortcuts while typing (D/S are real letters in text mode).
    if (!this._allowActionKey('handleBackKey', e)) return;
    // Emit BEFORE navigating away so onboarding (and other listeners) can observe/persist it.
    // This is synchronous so listeners run before any navigation is requested.
    this.emitAction('back');
    this._navigateHistory(-1, { recordTransientAction: 'back' });
  }

  handleForwardKey(e) {
    if (!this._allowActionKey('handleForwardKey', e)) return;
    this._navigateHistory(1);
  }

  /**
   * Navigate browser session history (back/forward).
   *
   * Uses `history.back/forward` as the primary action (immediate, same stack as the page).
   * Transient onboarding actions are recorded separately via the service worker.
   *
   * Silent hops (common SPA / in-page patterns that feel like "D did nothing"):
   * - Only cleared a `#hash` fragment
   * - URL is unchanged after a same-document `pushState` entry
   * - Only the query string changed on the same path (e.g. openrouter.ai/activity
   *   pushes `?from=&to=&date_preset=` after load — first back only strips filters)
   * In those cases, take one automatic extra step so one keypress matches user intent.
   *
   * @param {-1|1} direction
   * @param {{ recordTransientAction?: string }} [opts]
   */
  _navigateHistory(direction, opts = {}) {
    const dir = direction < 0 ? -1 : 1;
    const recordAction = typeof opts.recordTransientAction === 'string' ? opts.recordTransientAction : '';

    // Onboarding recovery: record before unload, fire-and-forget (do not gate navigation on it).
    if (recordAction) {
      this._sendRuntimeMessage({
        type: MSG.TRANSIENT_ACTION,
        action: recordAction,
        timestamp: Date.now()
      }, { silent: true });
    }

    const step = () => {
      try {
        if (dir < 0) history.back();
        else history.forward();
      } catch { /* ignore */ }
    };

    // Popover iframe: only this frame's history.
    if (window !== window.top) {
      step();
      return;
    }

    // Only auto-skip "invisible" hops when going back.
    if (dir >= 0) {
      step();
      return;
    }

    const beforeHref = location.href;
    const beforeOrigin = location.origin;
    const beforePathname = location.pathname;
    const beforeSearch = location.search || '';
    const beforePath = `${beforePathname}${beforeSearch}`;
    const beforeHash = location.hash || '';

    // Cancel any previous skip watcher (rapid D presses).
    try {
      if (this._historySkipCleanup) this._historySkipCleanup();
    } catch { /* ignore */ }
    this._historySkipCleanup = null;

    let extraHopsUsed = 0;
    const maxExtraHops = 1;

    const cleanup = () => {
      if (popListener) {
        try { window.removeEventListener('popstate', popListener); } catch { /* ignore */ }
        popListener = null;
      }
      if (timer) {
        try { clearTimeout(timer); } catch { /* ignore */ }
        timer = 0;
      }
      if (this._historySkipCleanup === cleanup) this._historySkipCleanup = null;
    };

    let popListener = () => {
      try {
        if (extraHopsUsed >= maxExtraHops) {
          cleanup();
          return;
        }
        const path = `${location.pathname}${location.search}`;
        const hash = location.hash || '';
        const href = location.href;
        const search = location.search || '';

        // Invisible hop patterns:
        // 1) Full URL unchanged after back (same-document pushState with no URL change)
        // 2) Only a hash fragment was cleared (page#section → page)
        // 3) Same origin+pathname, only query string changed (SPA filters / date presets)
        const urlUnchanged = href === beforeHref;
        const onlyClearedHash = !!beforeHash && !hash && path === beforePath;
        const samePath =
          location.origin === beforeOrigin &&
          location.pathname === beforePathname;
        const onlyQueryChanged = samePath && search !== beforeSearch;

        if (urlUnchanged || onlyClearedHash || onlyQueryChanged) {
          extraHopsUsed += 1;
          step();
          // Keep listening briefly for the extra hop's popstate; timeout cleans up.
          return;
        }
      } catch { /* ignore */ }
      cleanup();
    };

    let timer = 0;
    try {
      window.addEventListener('popstate', popListener);
      // SPA query-param hops can settle a bit after popstate; keep watcher briefly.
      timer = window.setTimeout(cleanup, 700);
      this._historySkipCleanup = cleanup;
    } catch {
      popListener = null;
    }

    step();
  }

  handleTabLeftKey(e) {
    if (!this._allowActionKey('handleTabLeftKey', e)) return;
    // Switch to the tab to the left
    // Emit + record transient action BEFORE switching tabs so onboarding can persist/recover reliably.
    this.emitAction('tabLeft');
    this._sendRuntimeMessage({ type: MSG.TRANSIENT_ACTION, action: 'tabLeft', timestamp: Date.now() }, { silent: true });
    if (!this._sendRuntimeMessage({ type: MSG.TAB_LEFT }, {
      onResponse: (response) => this._notifyTabSwitchFailure(response)
    })) {
      // Context invalidated — user already notified once via _handleExtensionContextInvalidated
    }
  }

  handleTabRightKey(e) {
    if (!this._allowActionKey('handleTabRightKey', e)) return;
    // Switch to the tab to the right
    // Emit + record transient action BEFORE switching tabs so onboarding can persist/recover reliably.
    this.emitAction('tabRight');
    this._sendRuntimeMessage({ type: MSG.TRANSIENT_ACTION, action: 'tabRight', timestamp: Date.now() }, { silent: true });
    if (!this._sendRuntimeMessage({ type: MSG.TAB_RIGHT }, {
      onResponse: (response) => this._notifyTabSwitchFailure(response)
    })) {
      // Context invalidated — user already notified once via _handleExtensionContextInvalidated
    }
  }

  /**
   * Show the top-center flash alert when left/right tab switch has nowhere to go
   * (or otherwise fails in the background).
   * @param {unknown} response
   */
  _notifyTabSwitchFailure(response) {
    if (!response || typeof response !== 'object') return;
    if (/** @type {{ type?: string }} */ (response).type !== 'KP_ERROR') return;

    const error = String(/** @type {{ error?: unknown }} */ (response).error || '');
    const message = /no valid tabs to switch to/i.test(error)
      ? 'No other tabs to switch to'
      : (error ? error.replace(/^Failed to switch tab:\s*/i, '') : 'Failed to switch tab');

    this.showFlashNotification(message, COLORS.NOTIFICATION_INFO);
  }

  /**
   * Shared path: enter inspector kind, or confirm if already that kind.
   * @param {string} kind INSPECTOR_KIND
   * @param {(el: Element|null) => void} onConfirm
   * @param {{ ignoreTextFocus?: boolean, ignoreModal?: boolean }} [opts]
   * @returns {boolean} true if enter/confirm was handled (false if blocked)
   */
  _toggleInspectorKind(kind, onConfirm, opts = {}) {
    const currentState = this.state.getState();

    if (!opts.ignoreTextFocus && currentState.mode === MODES.TEXT_FOCUS) {
      return false;
    }
    if (!opts.ignoreModal &&
        (currentState.mode === MODES.POPOVER || currentState.mode === MODES.OMNIBOX)) {
      return false;
    }

    if (this.inspector.isKind(kind)) {
      const target = this.inspector.confirmAndExit();
      try {
        onConfirm(target);
      } catch (err) {
        console.warn('[KeyPilot] Inspector confirm failed:', kind, err);
      }
      return true;
    }

    console.log('[KeyPilot] Entering inspector mode:', kind);
    this._prepareInspectorModeIndicator(kind);
    this.inspector.enter(kind);
    return true;
  }

  /**
   * Layout-aware confirm key label for inspector instruction chip.
   * @param {string} kind
   * @returns {string}
   */
  _confirmKeyLabelForInspectorKind(kind) {
    const def = getInspectorDef(kind);
    const actionId = def?.actionId;
    const binding = actionId ? this.keybindings?.[actionId] : null;
    if (binding?.displayKey) return String(binding.displayKey);
    if (Array.isArray(binding?.keys) && binding.keys.length) {
      // Prefer a single printable character over code names when possible.
      const ch = binding.keys.find((k) => typeof k === 'string' && k.length === 1);
      if (ch) return ch === ' ' ? 'Space' : ch;
      const first = String(binding.keys[0] || '');
      if (first === 'Backspace') return 'Backspace';
      if (first === 'Escape') return 'Esc';
      return first || '?';
    }
    if (kind === INSPECTOR_KIND.COLS) return '.';
    if (kind === INSPECTOR_KIND.DELETE) return 'Backspace';
    return '?';
  }

  /**
   * Seed top-right instruction chip (Text Select style) before/while pick is active.
   * @param {string} kind
   */
  _prepareInspectorModeIndicator(kind) {
    try {
      const confirmKey = this._confirmKeyLabelForInspectorKind(kind);
      this.overlayManager?.setInspectorModeIndicatorOpts?.({ kind, confirmKey });
      this.overlayManager?.showInspectorModeIndicator?.({ kind, confirmKey });
    } catch { /* ignore */ }
  }

  handleDeleteKey(e) {
    if (!this._allowActionKey('handleDeleteKey', e)) return;

    this._toggleInspectorKind(INSPECTOR_KIND.DELETE, (victim) => {
      console.log('[KeyPilot] Deleting element:', victim);
      this.deleteElement(victim);
    }, { ignoreTextFocus: true, ignoreModal: true });
  }

  /**
   * Cols Toggle (period): shared inspector pick → apply multicol,
   * or clear sticky columns if already applied. Esc exits pick only.
   */
  handleColsToggleKey(e) {
    if (!this._allowActionKey('handleColsToggleKey', e)) return;
    const currentState = this.state.getState();

    if (currentState.mode === MODES.TEXT_FOCUS) {
      console.log('[KeyPilot] Cols toggle ignored — text focus mode');
      return;
    }
    if (currentState.mode === MODES.POPOVER || currentState.mode === MODES.OMNIBOX) {
      console.log('[KeyPilot] Cols toggle ignored — modal mode');
      return;
    }

    // Sticky columns already on → period toggles them off (any target / any mode).
    if (this.columnLayoutManager?.isActive?.()) {
      console.log('[KeyPilot] Clearing column layout');
      try { this.columnLayoutManager.clear(); } catch (err) {
        console.warn('[KeyPilot] Failed to clear columns:', err);
      }
      if (this.inspector.isKind(INSPECTOR_KIND.COLS)) {
        this.inspector.exit();
      }
      try {
        this.showFlashNotification('Columns off', COLORS.NOTIFICATION_INFO);
      } catch { /* ignore */ }
      return;
    }

    this._toggleInspectorKind(INSPECTOR_KIND.COLS, (target) => {
      if (!target) {
        try {
          this.showFlashNotification('Nothing to columnize', COLORS.NOTIFICATION_INFO);
        } catch { /* ignore */ }
        return;
      }
      console.log('[KeyPilot] Applying columns to:', target?.tagName, target?.id || target?.className);
      const ok = this.columnLayoutManager.apply(target);
      if (ok) {
        try {
          this.showFlashNotification(
            this.columnLayoutManager.isPageMode?.() ? 'Page columns on' : 'Columns on',
            COLORS.NOTIFICATION_SUCCESS
          );
        } catch { /* ignore */ }
      } else {
        try {
          this.showFlashNotification('Could not columnize element', COLORS.NOTIFICATION_ERROR);
        } catch { /* ignore */ }
      }
    });
  }

  handleHighlightKey(e) {
    if (!this._allowActionKey('handleHighlightKey', e)) return;
    const currentState = this.state.getState();

    // Prevent highlight mode activation in text focus mode
    if (currentState.mode === MODES.TEXT_FOCUS) {
      console.log('[KeyPilot] H key ignored - currently in text focus mode');
      return;
    }

    if (!this.state.isHighlightMode()) {
      console.log('[KeyPilot] Entering highlight mode');
      
      // Cancel shared inspector pick if active
      if (this.state.isInspectorMode()) {
        console.log('[KeyPilot] Canceling inspector mode to enter highlight mode');
        this.inspector.exit();
      }
      
      // Enter highlight mode and start highlighting at current cursor position
      this.state.setMode(MODES.HIGHLIGHT);
      this._lastHighlightUpdatePos = { x: -1, y: -1 };
      
      // Set default selection mode to character-level
      this.overlayManager.setSelectionMode('character');
      
      this.startHighlighting();
    } else {
      console.log('[KeyPilot] Completing highlight selection');
      void this.completeSelection().catch((err) => {
        console.error('[KeyPilot] completeSelection failed:', err);
        try { this.cancelHighlightMode(); } catch { /* ignore */ }
      });
    }
  }

  handleRectangleHighlightKey(e) {
    if (!this._allowActionKey('handleRectangleHighlightKey', e)) return;
    const currentState = this.state.getState();

    // Prevent highlight mode activation in text focus mode
    if (currentState.mode === MODES.TEXT_FOCUS) {
      console.log('[KeyPilot] Rectangle highlight ignored - currently in text focus mode');
      return;
    }

    if (!this.state.isHighlightMode()) {
      console.log('[KeyPilot] Entering rectangle highlight mode');
      
      // Cancel shared inspector pick if active
      if (this.state.isInspectorMode()) {
        console.log('[KeyPilot] Canceling inspector mode to enter rectangle highlight mode');
        this.inspector.exit();
      }

      // Lazy-init edge-only stack only when explicitly enabled (off by default).
      if (FEATURE_FLAGS.USE_EDGE_ONLY_SELECTION && FEATURE_FLAGS.ENABLE_EDGE_ONLY_PROCESSING) {
        try { this.ensureEdgeOnlyProcessingForRectangle(); } catch { /* ignore */ }
      }
      
      // Enter highlight mode and start rectangle highlighting at current cursor position
      this.state.setMode(MODES.HIGHLIGHT);
      this._lastHighlightUpdatePos = { x: -1, y: -1 };
      
      // Set selection mode to rectangle
      this.overlayManager.setSelectionMode('rectangle');
      
      this.startHighlighting();
    } else {
      // Second press completes (and always exits) current highlight session.
      console.log('[KeyPilot] Completing rectangle highlight selection');
      void this.completeSelection().catch((err) => {
        console.error('[KeyPilot] completeSelection failed:', err);
        try { this.cancelHighlightMode(); } catch { /* ignore */ }
      });
    }
  }

  startHighlighting() {
    const currentState = this.state.getState();
    const selectionMode = this.overlayManager.getSelectionMode();

    // Companion instruction: "Press H again to finish selection" (layout-aware key).
    try {
      const KB = this.keybindings || {};
      const binding = selectionMode === 'rectangle' ? KB.RECTANGLE_HIGHLIGHT : KB.HIGHLIGHT;
      const finishKey = Array.isArray(binding?.keys) && binding.keys.length
        ? binding.keys.find((k) => k && k.length === 1) || binding.keys[0]
        : (selectionMode === 'rectangle' ? 'Y' : 'H');
      this.overlayManager.highlightManager?.showHighlightModeIndicator?.({ finishKey });
    } catch {
      try {
        this.overlayManager.highlightManager?.showHighlightModeIndicator?.();
      } catch { /* ignore */ }
    }
    
    // Convert viewport coordinates to document coordinates for scroll-independent selection
    const scrollX = window.pageXOffset || document.documentElement.scrollLeft;
    const scrollY = window.pageYOffset || document.documentElement.scrollTop;
    
    const startPosition = {
      x: currentState.lastMouse.x + scrollX, // Document coordinate
      y: currentState.lastMouse.y + scrollY, // Document coordinate
      viewportX: currentState.lastMouse.x,   // Keep viewport coordinate for reference
      viewportY: currentState.lastMouse.y,   // Keep viewport coordinate for reference
      element: this.detector.deepElementFromPoint(currentState.lastMouse.x, currentState.lastMouse.y)
    };

    console.log('[KeyPilot] Starting text selection at:', startPosition, 'Mode:', selectionMode);
    this.state.setHighlightStartPosition(startPosition);

    // Selection geometry lives entirely in HighlightManager (caret APIs).
    if (selectionMode === 'character') {
      // Character mode stays character mode — never silently switch to rectangle.
      const success = this.overlayManager.startCharacterSelection(
        { x: currentState.lastMouse.x, y: currentState.lastMouse.y }
      );

      if (success) {
        console.log('[KeyPilot] Character selection started successfully');
      } else {
        console.log('[KeyPilot] No text at start point; character selection will seed when cursor hits text');
      }
      return;
    }

    // Rectangle selection mode — seed caret at origin if possible (updates use dual-caret).
    try {
      const viewportStart = {
        x: currentState.lastMouse.x,
        y: currentState.lastMouse.y
      };
      const seeded = this.overlayManager.updateRectangleSelectionFromCarets(
        viewportStart,
        viewportStart
      );
      if (seeded) {
        this.state.setCurrentSelection(seeded);
        console.log('[KeyPilot] Rectangle selection seeded at cursor');
      } else {
        console.log('[KeyPilot] No text at rectangle origin; selection will start when drag hits text');
      }
    } catch (error) {
      console.warn('[KeyPilot] Error seeding rectangle selection:', error);
    }
  }

  /**
   * Update live text/rectangle selection.
   * @param {{ force?: boolean }} [opts] - force=true bypasses mouse-move threshold (use on scroll)
   */
  updateSelection(opts = {}) {
    if (this._completingHighlight) return;
    const force = !!opts.force;
    const currentState = this.state.getState();
    const startPos = currentState.highlightStartPosition;
    const selectionMode = this.overlayManager.getSelectionMode();
    
    if (!startPos) {
      return;
    }

    const currentPos = {
      x: currentState.lastMouse.x,
      y: currentState.lastMouse.y
    };

    // Skip micro-moves relative to last applied highlight update (not origin).
    // Scroll-driven refresh must always run even if the mouse viewport point is unchanged.
    if (!force) {
      const last = this._lastHighlightUpdatePos || { x: -1, y: -1 };
      const thr = this._HIGHLIGHT_UPDATE_THRESHOLD ?? 2;
      if (Math.abs(currentPos.x - last.x) < thr && Math.abs(currentPos.y - last.y) < thr) {
        return;
      }
    }
    this._lastHighlightUpdatePos = { x: currentPos.x, y: currentPos.y };

    // Origin in document space (set at highlight start) → current viewport each frame.
    // Never reuse frozen startPos.viewportX/Y after the page has scrolled.
    const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
    const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
    const startPosForOverlay = {
      x: (typeof startPos.x === 'number' ? startPos.x : (startPos.viewportX ?? 0) + scrollX) - scrollX,
      y: (typeof startPos.y === 'number' ? startPos.y : (startPos.viewportY ?? 0) + scrollY) - scrollY
    };
    // Prefer highlight-manager's document anchor when already seeded
    const hmOrigin = this.overlayManager?.highlightManager?.getOriginViewportPoint?.();
    const originVp = hmOrigin || startPosForOverlay;

    // All caret resolution + selection geometry is HighlightManager-owned.
    if (selectionMode === 'character') {
      try {
        this.overlayManager.updateCharacterSelection(currentPos, originVp);
      } catch (error) {
        console.warn('[KeyPilot] Error updating character selection:', error);
      }
      return;
    }

    // Rectangle: caret-based only on the mousemove/scroll path (no TreeWalker).
    try {
      const caretSelection = this.overlayManager.updateRectangleSelectionFromCarets(
        originVp,
        currentPos
      );
      if (caretSelection) {
        this.state.setCurrentSelection(caretSelection);
      } else {
        try {
          this.overlayManager.updateHighlightRectangleOverlay(originVp, currentPos);
        } catch (overlayError) {
          console.warn('[KeyPilot] Error updating highlight rectangle overlay:', overlayError);
        }
      }
    } catch (error) {
      console.error('[KeyPilot] Unexpected error updating selection:', error);
      // Stay in highlight mode; user can move cursor or press the key again to exit.
    }
  }

  /**
   * Called every scroll frame while in highlight mode so the dashed rectangle
   * stays glued to page content (not only on scroll-end).
   */
  refreshHighlightDuringScroll() {
    if (!this.enabled || this._completingHighlight || !this.state?.isHighlightMode?.()) return;
    try {
      this.updateSelection({ force: true });
    } catch (e) {
      console.warn('[KeyPilot] refreshHighlightDuringScroll failed:', e);
    }
  }

  async completeSelection() {
    // Ignore re-entrant completes (double H / concurrent awaits) — those left mode
    // stuck mid-copy and thrashing updateSelection, which froze pages on later uses.
    if (this._completingHighlight) {
      return;
    }
    this._completingHighlight = true;

    const currentState = this.state.getState();
    const selectionMode = this.overlayManager.getSelectionMode();
    
    console.log('[KeyPilot] Completing text selection, mode:', selectionMode);

    const exitHighlight = () => {
      try {
        this.cancelHighlightMode();
      } catch (e) {
        console.warn('[KeyPilot] Error exiting highlight mode:', e);
        try { this.state.setMode(MODES.NONE); } catch { /* ignore */ }
      }
    };
    
    try {
      let selectedText = '';
      let contentToClipboard = null;

      // Capture text FIRST while the browser Selection is still intact.
      // Do not reset session state until after we have the string.
      if (selectionMode === 'character') {
        selectedText = this.overlayManager.peekCharacterSelectedText() || '';
      } else {
        try {
          const selection = this.getCurrentSelectionWithShadowSupport();
          if (selection && typeof selection.toString === 'function' && selection.rangeCount > 0) {
            if (FEATURE_FLAGS.ENABLE_RICH_TEXT_CLIPBOARD) {
              try {
                contentToClipboard = this.extractSelectionContent(selection);
                selectedText = contentToClipboard.plainText || '';
              } catch (extractError) {
                console.warn('[KeyPilot] Error extracting rich text content:', extractError);
                selectedText = selection.toString() || '';
              }
            } else {
              selectedText = selection.toString() || '';
            }
          }
        } catch (selectionError) {
          console.warn('[KeyPilot] Error getting current selection:', selectionError);
        }

        if (!selectedText || !selectedText.trim()) {
          try {
            const stateSelection = currentState.currentSelection;
            if (stateSelection && typeof stateSelection.toString === 'function') {
              selectedText = stateSelection.toString() || '';
            }
          } catch { /* ignore */ }
        }
      }

      // Exit highlight mode immediately so mousemove/scroll stop updating selection
      // before any async clipboard work. This was a major freeze source.
      exitHighlight();
      
      if (!selectedText || !String(selectedText).trim()) {
        console.log('[KeyPilot] Empty selection — exited highlight mode');
        this.showFlashNotification('No text selected', COLORS.NOTIFICATION_INFO);
        return;
      }

      selectedText = String(selectedText);
      if (!contentToClipboard) {
        contentToClipboard = selectedText;
      }

      let copySuccess = false;
      let clipboardError = null;
      
      try {
        copySuccess = await this.copyToClipboard(contentToClipboard);
      } catch (error) {
        clipboardError = error;
        console.error('[KeyPilot] Clipboard operation threw error:', error);
        copySuccess = false;
      }

      if (copySuccess) {
        const copyType = (contentToClipboard && contentToClipboard.hasRichContent) ? 'rich text' : 'plain text';
        const textPreview = (typeof contentToClipboard === 'string')
          ? contentToClipboard.substring(0, 50)
          : (contentToClipboard.plainText || '').substring(0, 50);
        console.log(`[KeyPilot] Content copied (${copyType}):`, textPreview);

        const notificationCopyType = (contentToClipboard && contentToClipboard.hasRichContent) ? 'Rich text' : 'Text';
        this.showFlashNotification(`${notificationCopyType} copied to clipboard`, COLORS.NOTIFICATION_SUCCESS);
        
        try {
          this.overlayManager.flashFocusOverlay();
        } catch (flashError) {
          console.warn('[KeyPilot] Error flashing focus overlay:', flashError);
        }
      } else {
        console.warn('[KeyPilot] Failed to copy text to clipboard');
        let errorMessage = 'Failed to copy text';
        if (clipboardError) {
          if (clipboardError.name === 'NotAllowedError' || clipboardError.message?.includes('permission')) {
            errorMessage = 'Clipboard access denied - check browser permissions';
          } else if (clipboardError.message?.includes('not supported')) {
            errorMessage = 'Clipboard not supported in this context';
          } else if (clipboardError.message?.includes('secure context')) {
            errorMessage = 'Clipboard requires secure connection (HTTPS)';
          }
        }
        this.showFlashNotification(errorMessage, COLORS.NOTIFICATION_ERROR);
      }
    } catch (error) {
      console.error('[KeyPilot] Unexpected error completing selection:', error);
      try { exitHighlight(); } catch { /* ignore */ }
      let errorMessage = 'Error copying text';
      if (error?.message?.includes('Selection API')) {
        errorMessage = 'Text selection not supported on this page';
      } else if (error?.message?.includes('shadow')) {
        errorMessage = 'Cannot copy text from this element';
      }
      this.showFlashNotification(errorMessage, COLORS.NOTIFICATION_ERROR);
    } finally {
      this._completingHighlight = false;
    }
  }

  /**
   * Get current selection with comprehensive shadow DOM support and error handling
   * @returns {Selection|null} - Current selection or null
   */
  getCurrentSelectionWithShadowSupport() {
    try {
      // First try the main document selection with validation
      let mainSelection = null;
      try {
        if (window && typeof window.getSelection === 'function') {
          mainSelection = window.getSelection();
          
          // Validate selection object
          if (mainSelection && 
              typeof mainSelection.rangeCount === 'number' && 
              typeof mainSelection.toString === 'function') {
            
            if (mainSelection.rangeCount > 0) {
              const selectionText = mainSelection.toString();
              if (selectionText && selectionText.trim()) {
                console.log('[KeyPilot] Found valid main document selection');
                return mainSelection;
              }
            }
          }
        }
      } catch (mainSelectionError) {
        console.warn('[KeyPilot] Error accessing main document selection:', mainSelectionError);
      }
      
      // If no main selection, check stored selection from state with validation
      try {
        const currentState = this.state.getState();
        if (currentState && currentState.currentSelection) {
          const stateSelection = currentState.currentSelection;
          
          // Validate stored selection
          if (stateSelection && 
              typeof stateSelection.toString === 'function' &&
              typeof stateSelection.rangeCount === 'number') {
            
            const stateSelectionText = stateSelection.toString();
            if (stateSelectionText && stateSelectionText.trim()) {
              console.log('[KeyPilot] Found valid stored selection from state');
              return stateSelection;
            }
          }
        }
      } catch (stateSelectionError) {
        console.warn('[KeyPilot] Error accessing stored selection from state:', stateSelectionError);
      }
      
      // Try to find selection in shadow DOM contexts with comprehensive error handling
      try {
        const shadowSelection = this.findSelectionInShadowDOM();
        if (shadowSelection) {
          console.log('[KeyPilot] Found valid shadow DOM selection');
          return shadowSelection;
        }
      } catch (shadowSelectionError) {
        console.warn('[KeyPilot] Error finding selection in shadow DOM:', shadowSelectionError);
      }
      
      console.log('[KeyPilot] No valid selection found in any context');
      return null;
    } catch (error) {
      console.error('[KeyPilot] Unexpected error getting current selection:', error);
      return null;
    }
  }

  /**
   * Find selection in shadow DOM contexts with comprehensive error handling
   * @returns {Selection|null} - Selection found in shadow DOM or null
   */
  findSelectionInShadowDOM() {
    try {
      // Validate shadow DOM manager availability
      if (!this.shadowDOMManager) {
        console.log('[KeyPilot] Shadow DOM manager not available');
        return null;
      }
      
      // Validate shadow roots collection
      const shadowRoots = this.shadowDOMManager.shadowRoots;
      if (!shadowRoots ||
          !(shadowRoots instanceof Set) ||
          shadowRoots.size === 0) {
        console.log('[KeyPilot] No shadow roots available for selection search');
        return null;
      }
      
      // Iterate through shadow roots with comprehensive error handling
      let i = 0;
      for (const shadowRoot of shadowRoots) {
        
        try {
          // Validate shadow root
          if (!shadowRoot) {
            console.warn(`[KeyPilot] Shadow root at index ${i} is null or undefined`);
            i++;
            continue;
          }
          
          // Check if shadow root has selection capability
          if (typeof shadowRoot.getSelection !== 'function') {
            // Most shadow roots don't have their own getSelection method
            // This is normal and not an error
            i++;
            continue;
          }
          
          // Try to get selection from shadow root
          let shadowSelection = null;
          try {
            shadowSelection = shadowRoot.getSelection();
          } catch (getSelectionError) {
            console.warn(`[KeyPilot] Error calling getSelection on shadow root ${i}:`, getSelectionError);
            i++;
            continue;
          }
          
          // Validate shadow selection
          if (!shadowSelection) {
            i++;
            continue;
          }
          
          // Check if selection has required methods and properties
          if (typeof shadowSelection.rangeCount !== 'number' ||
              typeof shadowSelection.toString !== 'function') {
            console.warn(`[KeyPilot] Shadow selection at index ${i} missing required methods`);
            i++;
            continue;
          }
          
          // Check if selection has content
          if (shadowSelection.rangeCount > 0) {
            let selectionText = '';
            try {
              selectionText = shadowSelection.toString();
            } catch (toStringError) {
              console.warn(`[KeyPilot] Error getting text from shadow selection ${i}:`, toStringError);
              i++;
              continue;
            }
            
            if (selectionText && selectionText.trim()) {
              console.log(`[KeyPilot] Found valid selection in shadow root ${i}`);
              return shadowSelection;
            }
          }
        } catch (shadowRootError) {
          console.warn(`[KeyPilot] Error processing shadow root ${i}:`, shadowRootError);
          // Continue to next shadow root
          i++;
          continue;
        }

        i++;
      }
      
      console.log('[KeyPilot] No valid selection found in any shadow DOM context');
      return null;
    } catch (error) {
      console.error('[KeyPilot] Unexpected error finding selection in shadow DOM:', error);
      return null;
    }
  }

  /**
   * Extract both plain text and HTML content from a selection
   * @param {Selection} selection - The selection object to extract content from
   * @returns {Object} - Object containing both plainText and htmlContent
   */
  extractSelectionContent(selection) {
    try {
      if (!selection || typeof selection.rangeCount !== 'number' || selection.rangeCount === 0) {
        throw new Error('Invalid or empty selection');
      }

      // Get plain text
      const plainText = selection.toString();
      
      // Get HTML content
      let htmlContent = '';
      
      if (FEATURE_FLAGS.ENABLE_RICH_TEXT_CLIPBOARD) {
        try {
          // Create a temporary container to hold the selection content
          const container = document.createElement('div');
          
          // Clone all ranges from the selection
          for (let i = 0; i < selection.rangeCount; i++) {
            const range = selection.getRangeAt(i);
            const clonedContent = range.cloneContents();
            container.appendChild(clonedContent);
          }
          
          // Get the HTML content
          htmlContent = container.innerHTML;
          
          // Clean up the HTML - remove script tags and other potentially harmful content
          htmlContent = this.sanitizeHtmlContent(htmlContent);
          
        } catch (htmlError) {
          console.warn('[KeyPilot] Failed to extract HTML content:', htmlError);
          htmlContent = ''; // Fall back to plain text only
        }
      }

      return {
        plainText: plainText,
        htmlContent: htmlContent,
        hasRichContent: htmlContent.length > 0 && htmlContent !== plainText
      };
      
    } catch (error) {
      console.error('[KeyPilot] Error extracting selection content:', error);
      return {
        plainText: '',
        htmlContent: '',
        hasRichContent: false
      };
    }
  }

  /**
   * Sanitize HTML content to remove potentially harmful elements
   * @param {string} html - Raw HTML content
   * @returns {string} - Sanitized HTML content
   */
  sanitizeHtmlContent(html) {
    if (!html || typeof html !== 'string') {
      return '';
    }

    try {
      // Create a temporary element to parse and clean the HTML
      const temp = document.createElement('div');
      temp.innerHTML = html;

      // Remove script tags and other potentially harmful elements
      const dangerousElements = temp.querySelectorAll('script, object, embed, iframe, form, input, button');
      dangerousElements.forEach(el => el.remove());

      // Remove event handlers and javascript: links
      const allElements = temp.querySelectorAll('*');
      allElements.forEach(el => {
        // Remove event handler attributes
        const attributes = Array.from(el.attributes);
        attributes.forEach(attr => {
          if (attr.name.startsWith('on') || attr.value.includes('javascript:')) {
            el.removeAttribute(attr.name);
          }
        });
      });

      return temp.innerHTML;
    } catch (error) {
      console.warn('[KeyPilot] Error sanitizing HTML content:', error);
      return html; // Return original if sanitization fails
    }
  }

  /**
   * Copy text to clipboard using modern Clipboard API with comprehensive fallback methods and error handling
   * @param {string|Object} content - Text to copy, or object with plainText and htmlContent
   * @returns {Promise<boolean>} - True if copy was successful, false otherwise
   */
  async copyToClipboard(content) {
    // Handle both string (legacy) and object (rich text) input
    let plainText, htmlContent, hasRichContent;
    
    if (typeof content === 'string') {
      // Legacy string input - plain text only
      plainText = content;
      htmlContent = '';
      hasRichContent = false;
    } else if (content && typeof content === 'object') {
      // Rich text object input
      plainText = content.plainText || '';
      htmlContent = content.htmlContent || '';
      hasRichContent = content.hasRichContent || false;
    } else {
      console.warn('[KeyPilot] Invalid content provided to copyToClipboard:', typeof content);
      return false;
    }

    // Comprehensive input validation
    if (!plainText) {
      console.warn('[KeyPilot] No plain text content provided to copyToClipboard');
      return false;
    }
    
    if (plainText.length === 0) {
      console.warn('[KeyPilot] Empty plain text content provided to copyToClipboard');
      return false;
    }
    
    // Validate text content (check for null characters or other issues)
    try {
      // Test if text can be properly encoded
      const encoded = encodeURIComponent(plainText);
      if (!encoded) {
        throw new Error('Text contains invalid characters');
      }
    } catch (encodingError) {
      console.warn('[KeyPilot] Text encoding validation failed:', encodingError);
      return false;
    }

    // Try modern Clipboard API first with comprehensive error handling
    if (navigator.clipboard) {
      try {
        // Check if we're in a secure context
        if (!window.isSecureContext) {
          console.warn('[KeyPilot] Not in secure context, modern Clipboard API may fail');
        }
        
        let clipboardPromise;
        
        // Use rich text API if we have HTML content and the browser supports it
        if (hasRichContent && htmlContent && navigator.clipboard.write) {
          try {
            const clipboardItems = [];
            const clipboardItem = new ClipboardItem({
              'text/plain': new Blob([plainText], { type: 'text/plain' }),
              'text/html': new Blob([htmlContent], { type: 'text/html' })
            });
            clipboardItems.push(clipboardItem);
            
            clipboardPromise = navigator.clipboard.write(clipboardItems);
            console.log('[KeyPilot] Attempting to copy rich text (HTML + plain text)');
          } catch (richTextError) {
            console.warn('[KeyPilot] Rich text clipboard failed, falling back to plain text:', richTextError);
            // Fall back to plain text
            if (typeof navigator.clipboard.writeText === 'function') {
              clipboardPromise = navigator.clipboard.writeText(plainText);
            } else {
              throw new Error('Neither rich text nor plain text clipboard API available');
            }
          }
        } else {
          // Use plain text API
          if (typeof navigator.clipboard.writeText === 'function') {
            clipboardPromise = navigator.clipboard.writeText(plainText);
            console.log('[KeyPilot] Attempting to copy plain text');
          } else {
            throw new Error('Plain text clipboard API not available');
          }
        }
        
        // Attempt to write to clipboard with timeout
        const timeoutPromise = new Promise((_, reject) => {
          setTimeout(() => reject(new Error('Clipboard operation timed out')), 5000);
        });
        
        await Promise.race([clipboardPromise, timeoutPromise]);
        
        const copyType = hasRichContent && htmlContent ? 'rich text (HTML + plain text)' : 'plain text';
        console.log(`[KeyPilot] Content copied using modern Clipboard API (${copyType})`);
        return true;
      } catch (error) {
        console.warn('[KeyPilot] Modern Clipboard API failed:', error.message);
        
        // Categorize the error for better user feedback
        if (error.name === 'NotAllowedError') {
          console.warn('[KeyPilot] Clipboard permission denied');
        } else if (error.name === 'NotSupportedError') {
          console.warn('[KeyPilot] Clipboard API not supported');
        } else if (error.message.includes('secure context')) {
          console.warn('[KeyPilot] Clipboard requires secure context (HTTPS)');
        } else if (error.message.includes('timed out')) {
          console.warn('[KeyPilot] Clipboard operation timed out');
        } else if (error.message.includes('permission')) {
          console.warn('[KeyPilot] Clipboard permission issue');
        }
        
        // Fall through to fallback method
      }
    } else {
      console.log('[KeyPilot] Modern Clipboard API not available, using fallback method');
    }

    // Fallback method using execCommand with comprehensive error handling
    let textarea = null;
    try {
      // Validate document state
      if (!document || !document.body) {
        throw new Error('Document or document.body not available');
      }
      
      // Check if execCommand is available
      if (typeof document.execCommand !== 'function') {
        throw new Error('execCommand not available');
      }
      
      // Create appropriate element for rich text or plain text
      let tempElement;
      
      if (hasRichContent && htmlContent && FEATURE_FLAGS.ENABLE_RICH_TEXT_CLIPBOARD) {
        // Use a div for rich text content
        tempElement = document.createElement('div');
        if (!tempElement) {
          throw new Error('Failed to create div element for rich text');
        }
        
        // Set div properties for rich text
        try {
          tempElement.innerHTML = htmlContent;
          tempElement.style.position = 'fixed';
          tempElement.style.left = '-9999px';
          tempElement.style.top = '-9999px';
          tempElement.style.width = '1px';
          tempElement.style.height = '1px';
          tempElement.style.opacity = '0';
          tempElement.style.pointerEvents = 'none';
          tempElement.style.zIndex = '-1';
          tempElement.setAttribute('tabindex', '-1');
          tempElement.setAttribute('aria-hidden', 'true');
          console.log('[KeyPilot] Using div element for rich text fallback');
        } catch (styleError) {
          throw new Error(`Failed to set div properties: ${styleError.message}`);
        }
      } else {
        // Use textarea for plain text (traditional method)
        tempElement = document.createElement('textarea');
        if (!tempElement) {
          throw new Error('Failed to create textarea element');
        }
        
        // Set textarea properties with error handling
        try {
          tempElement.value = plainText;
          tempElement.style.position = 'fixed';
          tempElement.style.left = '-9999px';
          tempElement.style.top = '-9999px';
          tempElement.style.width = '1px';
          tempElement.style.height = '1px';
          tempElement.style.opacity = '0';
          tempElement.style.pointerEvents = 'none';
          tempElement.style.zIndex = '-1';
          tempElement.setAttribute('readonly', '');
          tempElement.setAttribute('tabindex', '-1');
          tempElement.setAttribute('aria-hidden', 'true');
          console.log('[KeyPilot] Using textarea element for plain text fallback');
        } catch (styleError) {
          throw new Error(`Failed to set textarea properties: ${styleError.message}`);
        }
      }
      
      // Store reference for cleanup
      textarea = tempElement;
      
      // Add to DOM with error handling
      try {
        document.body.appendChild(tempElement);
      } catch (appendError) {
        throw new Error(`Failed to append element to DOM: ${appendError.message}`);
      }
      
      // Focus and select content with error handling
      try {
        tempElement.focus();
        
        if (hasRichContent && htmlContent && tempElement.tagName === 'DIV') {
          // For div elements with rich content, select all content
          const range = document.createRange();
          range.selectNodeContents(tempElement);
          const selection = window.getSelection();
          selection.removeAllRanges();
          selection.addRange(range);
          console.log('[KeyPilot] Selected rich content in div element');
        } else {
          // For textarea elements, use traditional selection
          tempElement.select();
          
          // Ensure selection is set properly
          if (typeof tempElement.setSelectionRange === 'function') {
            tempElement.setSelectionRange(0, plainText.length);
          }
          
          // Verify selection was successful
          if (tempElement.selectionStart !== 0 || tempElement.selectionEnd !== plainText.length) {
            console.warn('[KeyPilot] Text selection in textarea may be incomplete');
          }
        }
      } catch (selectionError) {
        throw new Error(`Failed to select content: ${selectionError.message}`);
      }
      
      // Execute copy command with error handling
      let success = false;
      try {
        success = document.execCommand('copy');
      } catch (execError) {
        throw new Error(`execCommand failed: ${execError.message}`);
      }
      
      // Clean up temporary element
      try {
        if (tempElement && tempElement.parentNode) {
          document.body.removeChild(tempElement);
        }
      } catch (cleanupError) {
        console.warn('[KeyPilot] Error cleaning up temporary element:', cleanupError);
        // Don't fail the operation due to cleanup issues
      }
      
      if (success) {
        const copyType = hasRichContent && htmlContent ? 'rich text' : 'plain text';
        console.log(`[KeyPilot] Content copied using fallback execCommand method (${copyType})`);
        return true;
      } else {
        throw new Error('execCommand returned false');
      }
    } catch (error) {
      console.error('[KeyPilot] Fallback clipboard method failed:', error);
      
      // Ensure cleanup even on error
      try {
        if (tempElement && tempElement.parentNode) {
          document.body.removeChild(tempElement);
        }
      } catch (cleanupError) {
        console.warn('[KeyPilot] Error cleaning up temporary element after failure:', cleanupError);
      }
      
      return false;
    }
  }

  /**
   * Copy image under the cursor to the clipboard (I on right-handed layout; E on left).
   * Uses getHoveredImage utility — discovery is not clipboard-tied.
   */
  async handleCopyHoveredImageKey() {
    const currentState = this.state.getState();
    const x = Number(currentState?.lastMouse?.x);
    const y = Number(currentState?.lastMouse?.y);

    if (!Number.isFinite(x) || !Number.isFinite(y)) {
      this.showFlashNotification('No image under cursor', COLORS.NOTIFICATION_INFO);
      return;
    }

    let result = null;
    try {
      result = await getHoveredImage(x, y);
    } catch (error) {
      console.warn('[KeyPilot] getHoveredImage failed:', error);
      this.showFlashNotification('Could not copy image', COLORS.NOTIFICATION_ERROR);
      return;
    }

    if (!result?.blob) {
      this.showFlashNotification('No image under cursor', COLORS.NOTIFICATION_INFO);
      return;
    }

    try {
      const ok = await this.copyImageToClipboard(result.blob, result.mimeType);
      if (ok) {
        // Distinct scale animation (shutter → pop → shrink), not the green F-click pulse.
        try {
          this.overlayManager?.flashImageCopyPulse?.(result.element);
        } catch { /* ignore visual feedback failures */ }

        const label =
          result.kind === 'background' ? 'Background image'
            : result.kind === 'svg' ? 'SVG'
              : 'Image';
        this.showFlashNotification(`${label} copied to clipboard`, COLORS.NOTIFICATION_SUCCESS);
        this.emitAction('copy_hovered_image', {
          kind: result.kind,
          url: result.url ? String(result.url).slice(0, 200) : ''
        });
      } else {
        this.showFlashNotification('Could not copy image', COLORS.NOTIFICATION_ERROR);
      }
    } catch (error) {
      console.warn('[KeyPilot] copyImageToClipboard failed:', error);
      let message = 'Could not copy image';
      if (error?.name === 'NotAllowedError' || /permission/i.test(error?.message || '')) {
        message = 'Clipboard permission denied';
      } else if (/secure context/i.test(error?.message || '')) {
        message = 'Clipboard requires HTTPS';
      }
      this.showFlashNotification(message, COLORS.NOTIFICATION_ERROR);
    }
  }

  /**
   * Write an image Blob to the system clipboard (PNG preferred).
   * Separate from text copyToClipboard to keep that API type-safe.
   *
   * @param {Blob} blob
   * @param {string} [mimeType='image/png']
   * @returns {Promise<boolean>}
   */
  async copyImageToClipboard(blob, mimeType = 'image/png') {
    if (!blob || !(blob instanceof Blob) || blob.size === 0) {
      console.warn('[KeyPilot] Invalid blob provided to copyImageToClipboard');
      return false;
    }

    const type = (mimeType && String(mimeType).startsWith('image/'))
      ? String(mimeType)
      : (blob.type && blob.type.startsWith('image/') ? blob.type : 'image/png');

    if (!navigator.clipboard || typeof navigator.clipboard.write !== 'function') {
      console.warn('[KeyPilot] Clipboard image write not available');
      return false;
    }

    try {
      // ClipboardItem often wants a Promise<Blob> for image types.
      const item = new ClipboardItem({
        [type]: Promise.resolve(blob)
      });
      const writePromise = navigator.clipboard.write([item]);
      const timeoutPromise = new Promise((_, reject) => {
        setTimeout(() => reject(new Error('Clipboard operation timed out')), 5000);
      });
      await Promise.race([writePromise, timeoutPromise]);
      console.log('[KeyPilot] Image copied to clipboard:', type, blob.size);
      return true;
    } catch (error) {
      // Some browsers require the exact type image/png only — retry as png wrapper.
      if (type !== 'image/png') {
        try {
          const item = new ClipboardItem({
            'image/png': Promise.resolve(blob)
          });
          await navigator.clipboard.write([item]);
          console.log('[KeyPilot] Image copied to clipboard as image/png fallback');
          return true;
        } catch (retryErr) {
          console.warn('[KeyPilot] Image clipboard write failed:', retryErr);
          throw retryErr;
        }
      }
      console.warn('[KeyPilot] Image clipboard write failed:', error);
      throw error;
    }
  }

  /**
   * Cancel highlight mode and return to normal mode
   * Clears selection, visual indicators, and state with shadow DOM support
   */
  cancelHighlightMode() {
    console.log('[KeyPilot] Canceling highlight mode');

    // Always fully clear both character + rectangle session state (previous path
    // only cleared one branch and could leave characterSelectionActive stuck true).
    try {
      this.overlayManager.clearCharacterSelection();
    } catch (error) {
      console.warn('[KeyPilot] Error clearing character selection:', error);
    }

    try {
      this.overlayManager.removeHighlightRectangleOverlay();
    } catch (error) {
      console.warn('[KeyPilot] Error clearing highlight rectangle overlay:', error);
    }

    try {
      this.overlayManager.clearHighlightSelectionOverlays();
    } catch (error) {
      console.warn('[KeyPilot] Error clearing highlight overlays:', error);
    }

    // Hide companion instruction modal
    try {
      this.overlayManager.highlightManager?.hideHighlightModeIndicator?.();
    } catch { /* ignore */ }

    // Sweep any orphaned selection / instruction overlays left in the DOM
    try {
      document.querySelectorAll(
        '.kpv2-highlight-selection-overlay, .kpv2-highlight-selection, .kpv2-highlight-mode-indicator'
      ).forEach((el) => {
        try { el.remove(); } catch { /* ignore */ }
      });
      try {
        if (this.overlayManager?.highlightManager) {
          this.overlayManager.highlightManager.highlightModeIndicator = null;
        }
      } catch { /* ignore */ }
    } catch { /* ignore */ }

    // Optional edge-only stack cleanup
    if (this.edgeOnlyProcessingEnabled &&
        this.rectangleIntersectionObserver &&
        FEATURE_FLAGS.ENABLE_EDGE_ONLY_PROCESSING) {
      try {
        this.rectangleIntersectionObserver.updateRectangle({
          left: 0, top: 0, width: 0, height: 0
        });
      } catch (error) {
        console.warn('[KeyPilot] Error cleaning up edge-only processing:', error);
      }
    }
    
    // Clear any active text selection immediately with shadow DOM support
    this.clearAllSelectionsWithShadowSupport();
    
    // Clear all highlight-related state
    this.state.setHighlightStartPosition(null);
    this.state.setCurrentSelection(null);
    this.state.setHighlightElement(null);
    this._lastHighlightUpdatePos = { x: -1, y: -1 };
    
    // Return to normal mode
    this.state.setMode(MODES.NONE);
    
    console.log('[KeyPilot] Highlight mode canceled, returned to normal mode');
  }

  /**
   * Clear all selections including shadow DOM contexts with comprehensive error handling
   */
  clearAllSelectionsWithShadowSupport() {
    // Clear main document selection with validation
    try {
      if (window && typeof window.getSelection === 'function') {
        const mainSelection = window.getSelection();
        if (mainSelection && 
            typeof mainSelection.rangeCount === 'number' &&
            typeof mainSelection.removeAllRanges === 'function') {
          
          if (mainSelection.rangeCount > 0) {
            mainSelection.removeAllRanges();
            console.log('[KeyPilot] Cleared main document selection');
          }
        }
      }
    } catch (error) {
      console.warn('[KeyPilot] Error clearing main document selection:', error);
    }
    
    // Clear selections in shadow DOM contexts with comprehensive validation
    try {
      if (!this.shadowDOMManager) {
        console.log('[KeyPilot] Shadow DOM manager not available for selection clearing');
        return;
      }
      
      const shadowRoots = this.shadowDOMManager.shadowRoots;
      if (!shadowRoots ||
          !(shadowRoots instanceof Set) ||
          shadowRoots.size === 0) {
        console.log('[KeyPilot] No shadow roots available for selection clearing');
        return;
      }
      
      let i = 0;
      for (const shadowRoot of shadowRoots) {
        
        try {
          if (!shadowRoot) {
            console.warn(`[KeyPilot] Shadow root at index ${i} is null`);
            i++;
            continue;
          }
          
          // Check if shadow root has selection capability
          if (typeof shadowRoot.getSelection !== 'function') {
            // Most shadow roots don't have getSelection - this is normal
            i++;
            continue;
          }
          
          // Try to get and clear shadow selection
          let shadowSelection = null;
          try {
            shadowSelection = shadowRoot.getSelection();
          } catch (getSelectionError) {
            console.warn(`[KeyPilot] Error getting shadow selection ${i}:`, getSelectionError);
            i++;
            continue;
          }
          
          if (shadowSelection &&
              typeof shadowSelection.rangeCount === 'number' &&
              typeof shadowSelection.removeAllRanges === 'function') {
            
            if (shadowSelection.rangeCount > 0) {
              try {
                shadowSelection.removeAllRanges();
                console.log(`[KeyPilot] Cleared shadow DOM selection ${i}`);
              } catch (removeRangesError) {
                console.warn(`[KeyPilot] Error removing ranges from shadow selection ${i}:`, removeRangesError);
              }
            }
          }
        } catch (shadowRootError) {
          console.warn(`[KeyPilot] Error processing shadow root ${i} for selection clearing:`, shadowRootError);
          i++;
          continue;
        }

        i++;
      }
    } catch (error) {
      console.error('[KeyPilot] Unexpected error clearing shadow DOM selections:', error);
    }
  }

  handleRootKey(e) {
    if (!this._allowActionKey('handleRootKey', e)) return;
    console.log('[KeyPilot] Root key pressed!');
    console.log('[KeyPilot] Current URL:', window.location.href);
    console.log('[KeyPilot] Origin:', window.location.origin);

    // Navigate to the site root (origin)
    const rootUrl = window.location.origin;
    if (rootUrl && rootUrl !== window.location.href) {
      console.log('[KeyPilot] Navigating to root:', rootUrl);
      this.showFlashNotification('Navigating to Site Root...', COLORS.NOTIFICATION_INFO);
      window.location.href = rootUrl;
    } else {
      console.log('[KeyPilot] Already at root, no navigation needed');
    }
  }

  handleLauncherKey(e) {
    if (!this._allowActionKey('handleLauncherKey', e)) return;
    console.log('[KeyPilot] Launcher key pressed!');

    if (this.launcherPopover.isOpen()) {
      this.launcherPopover.hide();
    } else {
      this.launcherPopover.show();
    }
  }

  handleCloseTabKey(e) {
    if (!this._allowActionKey('handleCloseTabKey', e)) return;
    console.log('[KeyPilot] Close tab key pressed!');
    
    try {
      // Send message to background script to close the current tab
      if (!this._sendRuntimeMessage({ type: MSG.CLOSE_TAB })) {
        if (!this._extensionContextInvalidatedHandled) {
          this.showFlashNotification('Failed to Close Tab', COLORS.NOTIFICATION_ERROR);
        }
        return;
      }
      this.showFlashNotification('Closing Tab...', COLORS.NOTIFICATION_INFO);
    } catch (error) {
      if (noteExtensionContextError(error)) {
        this._handleExtensionContextInvalidated();
        return;
      }
      console.error('[KeyPilot] Failed to close tab:', error);
      this.showFlashNotification('Failed to Close Tab', COLORS.NOTIFICATION_ERROR);
    }
  }

  handleNewTabKey(e) {
    if (!this._allowActionKey('handleNewTabKey', e)) return;
    console.log('[KeyPilot] New tab key pressed!');
    
    try {
      // Emit + record transient action BEFORE opening the new tab so onboarding can persist/recover reliably.
      this.emitAction('newTab');
      this._sendRuntimeMessage({ type: MSG.TRANSIENT_ACTION, action: 'newTab', timestamp: Date.now() }, { silent: true });

      // Send message to background script to open a new tab
      if (!this._sendRuntimeMessage({ type: MSG.NEW_TAB })) {
        // Invalidated context already shows a reload hint once.
        if (!this._extensionContextInvalidatedHandled) {
          this.showFlashNotification('Failed to Open New Tab', COLORS.NOTIFICATION_ERROR);
        }
        return;
      }
      this.showFlashNotification('Opening New Tab...', COLORS.NOTIFICATION_INFO);
    } catch (error) {
      if (noteExtensionContextError(error)) {
        this._handleExtensionContextInvalidated();
        return;
      }
      console.error('[KeyPilot] Failed to open new tab:', error);
      this.showFlashNotification('Failed to Open New Tab', COLORS.NOTIFICATION_ERROR);
    }
  }

  /**
   * When the pointer is over a cross-origin (or any) iframe, top-frame hit-testing
   * only sees the <iframe> shell. Forward activate to the child frame-click-agent
   * with coordinates local to the iframe viewport.
   *
   * @param {number} clientX
   * @param {number} clientY
   * @param {{ openInNewTab?: boolean, background?: boolean }} [opts]
   * @returns {boolean} true if a message was posted to an iframe under the cursor
   */
  _tryActivateIframeUnderCursor(clientX, clientY, opts = {}) {
    if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return false;

    let under = null;
    try {
      under = this.detector.deepElementFromPoint(clientX, clientY);
    } catch {
      under = null;
    }
    if (!under || under.tagName !== 'IFRAME') return false;

    // Popover mode is modal: only interact with iframes inside the popover UI.
    try {
      const mode = this.state.getState()?.mode;
      if (mode === MODES.POPOVER && !this._isElementInPopover(under)) {
        return false;
      }
    } catch { /* ignore */ }

    // Ignore KeyPilot UI chrome (not page iframes).
    try {
      if (this._isKeyPilotUiElement?.(under)) return false;
    } catch { /* ignore */ }

    const iframe = /** @type {HTMLIFrameElement} */ (under);
    let rect;
    try {
      rect = iframe.getBoundingClientRect();
    } catch {
      return false;
    }
    if (!rect || rect.width <= 0 || rect.height <= 0) return false;

    const localX = clientX - rect.left;
    const localY = clientY - rect.top;
    if (localX < 0 || localY < 0 || localX > rect.width || localY > rect.height) {
      return false;
    }

    const payload = {
      type: MSG.FRAME_ACTIVATE,
      clientX: localX,
      clientY: localY,
      openInNewTab: !!opts.openInNewTab,
      background: !!opts.background,
      // Child agent filters on window.name when set (e.g. Google name="account").
      frameName: typeof iframe.name === 'string' ? iframe.name : ''
    };

    // Same-origin fast path: click directly inside the child document (no agent needed).
    try {
      const doc = iframe.contentDocument;
      const view = iframe.contentWindow;
      if (doc && view) {
        let el = doc.elementFromPoint(localX, localY);
        let guard = 0;
        while (el && el.shadowRoot && guard++ < 10) {
          const nested = el.shadowRoot.elementFromPoint(localX, localY);
          if (!nested || nested === el) break;
          el = nested;
        }
        if (el) {
          // Nested iframe inside a same-origin frame: recurse via postMessage to that child.
          if (el.tagName === 'IFRAME') {
            try {
              const nested = /** @type {HTMLIFrameElement} */ (el);
              const nr = nested.getBoundingClientRect();
              nested.contentWindow?.postMessage({
                ...payload,
                clientX: localX - nr.left,
                clientY: localY - nr.top,
                frameName: typeof nested.name === 'string' ? nested.name : ''
              }, '*');
            } catch { /* ignore */ }
          } else {
            const common = {
              bubbles: true,
              cancelable: true,
              composed: true,
              view,
              clientX: localX,
              clientY: localY,
              button: 0,
              buttons: 0
            };
            try { el.dispatchEvent(new view.MouseEvent('mousedown', { ...common, buttons: 1 })); } catch { /* ignore */ }
            try { el.dispatchEvent(new view.MouseEvent('mouseup', common)); } catch { /* ignore */ }
            try { el.dispatchEvent(new view.MouseEvent('click', common)); } catch { /* ignore */ }
            try {
              const clickable = typeof el.closest === 'function'
                ? el.closest('a[href], button, [role="button"], [role="link"], [role="menuitem"]')
                : null;
              if (clickable && clickable !== el) {
                try { /** @type {HTMLElement} */ (clickable).click(); } catch { /* ignore */ }
              } else if (typeof /** @type {any} */ (el).click === 'function') {
                try { /** @type {any} */ (el).click(); } catch { /* ignore */ }
              }
            } catch { /* ignore */ }
          }
          return true;
        }
      }
    } catch {
      // Cross-origin — fall through to postMessage / runtime relay.
    }

    let posted = false;
    try {
      const win = iframe.contentWindow;
      if (win) {
        win.postMessage(payload, '*');
        posted = true;
      }
    } catch { /* ignore */ }

    // Backup: SW fans out to subframes; agent matches frameName / activates at local coords.
    // Fire-and-forget so we never block the key handler.
    try {
      this._sendRuntimeMessage(payload, { silent: true });
      posted = true;
    } catch { /* ignore */ }

    return posted;
  }

  handleActivateKey() {
    const currentState = this.state.getState();
    const x = currentState.lastMouse.x;
    const y = currentState.lastMouse.y;

    // Cross-origin iframes (Google account switcher, etc.): forward into the frame.
    if (this._tryActivateIframeUnderCursor(x, y, {})) {
      this.showRipple(x, y);
      this.emitAction('activate', { viaIframe: true });
      return;
    }

    const target = this._getValidatedActivationTarget(currentState);

    if (!target || target === document.documentElement || target === document.body) {
      return;
    }

    // Popover mode is modal: don't allow activation on background page elements.
    if (currentState.mode === MODES.POPOVER) {
      if (!this._isElementInPopover(target)) {
        return;
      }
    }

    console.log('[KeyPilot] Activating element:', {
      tagName: target.tagName,
      className: target.className,
      id: target.id,
      hasClickHandler: !!(target.onclick || target.getAttribute('onclick'))
    });

    const activationDetail = this._buildActivationDetail(target);

    // Store coordinates if this is a link click
    if (target.tagName === 'A' && target.href) {
      this.mouseCoordinateManager.handleLinkClick(currentState.lastMouse.x, currentState.lastMouse.y, target);
    }

    // Try semantic activation first
    if (this.activator.handleSmartActivate(target, currentState.lastMouse.x, currentState.lastMouse.y)) {
      this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
      this.overlayManager.flashFocusOverlay(target);
      this.postClickRefresh(target, currentState.lastMouse.x, currentState.lastMouse.y);
      this.emitAction('activate', activationDetail);
      return;
    }

    // Always try to click the element, regardless of whether it's "detected" as interactive
    // This ensures videos, custom elements, and other non-standard interactive elements work
    this.activator.smartClick(target, currentState.lastMouse.x, currentState.lastMouse.y);
    this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
    this.overlayManager.flashFocusOverlay(target);
    this.postClickRefresh(target, currentState.lastMouse.x, currentState.lastMouse.y);
    this.emitAction('activate', activationDetail);
  }

  handleActivateNewTabKey() {
    const currentState = this.state.getState();
    const x = currentState.lastMouse.x;
    const y = currentState.lastMouse.y;

    if (this._tryActivateIframeUnderCursor(x, y, { openInNewTab: true })) {
      this.showRipple(x, y);
      this.emitAction('activateNewTab', { viaIframe: true });
      return;
    }

    const target = this._getValidatedActivationTarget(currentState);

    if (!target || target === document.documentElement || target === document.body) {
      return;
    }

    // Popover mode is modal: don't allow activation on background page elements.
    if (currentState.mode === MODES.POPOVER) {
      if (!this._isElementInPopover(target)) {
        return;
      }
    }

    console.log('[KeyPilot] Activating element in new tab:', {
      tagName: target.tagName,
      className: target.className,
      id: target.id,
      hasClickHandler: !!(target.onclick || target.getAttribute('onclick'))
    });

    // If we're on/inside a link, open via background script so we can reliably focus the new tab.
    // Support both:
    // - traditional <a href="...">
    // - KeyPilot URL listing rows rendered as `[role="link"][data-kp-url="..."]` (e.g. extension New Tab page)
    let link = target;
    let url = null;
    try {
      if (link && link.tagName !== 'A' && typeof link.closest === 'function') {
        link = link.closest('a[href]');
      }
    } catch { }

    if (link && link.tagName === 'A' && link.href) {
      url = link.href;
    } else {
      // `renderUrlListing()` uses role="link" rows with `data-kp-url` instead of <a>.
      try {
        let roleLink = target;
        if (roleLink instanceof Element) {
          if (roleLink.getAttribute('role') !== 'link') {
            roleLink = roleLink.closest?.('[role="link"][data-kp-url]') || null;
          }
          if (roleLink && roleLink.getAttribute('role') === 'link' && roleLink.dataset?.kpUrl) {
            url = String(roleLink.dataset.kpUrl || '').trim() || null;
            link = roleLink;
          }
        }
      } catch { /* ignore */ }
    }

    if (url) {
      this.mouseCoordinateManager.handleLinkClick(currentState.lastMouse.x, currentState.lastMouse.y, link);

      try {
        if (this._sendRuntimeMessage({ type: MSG.OPEN_URL_FOREGROUND, url })) {
          this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
          this.overlayManager.flashFocusOverlay(link);
          this.postClickRefresh(link, currentState.lastMouse.x, currentState.lastMouse.y);
          this.emitAction('activateNewTab', { isLink: true, href: url });
          return;
        }
        // Context invalidated — fall through to legacy window.open path below.
      } catch (error) {
        if (noteExtensionContextError(error)) {
          this._handleExtensionContextInvalidated();
        } else {
          console.error('[KeyPilot] Failed to open link in foreground tab:', error);
        }
        // Fall through to legacy behavior below.
      }
    }

    // Try semantic activation first (but force new tab for links)
    if (this.activator.handleSmartActivate(target, currentState.lastMouse.x, currentState.lastMouse.y, true)) {
      this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
      this.overlayManager.flashFocusOverlay(target);
      this.postClickRefresh(target, currentState.lastMouse.x, currentState.lastMouse.y);
      this.emitAction('activateNewTab', this._buildActivationDetail(target));
      return;
    }

    // Always try to click the element in new tab mode
    this.activator.smartClick(target, currentState.lastMouse.x, currentState.lastMouse.y, true);
    this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
    this.overlayManager.flashFocusOverlay(target);
    this.postClickRefresh(target, currentState.lastMouse.x, currentState.lastMouse.y);
    this.emitAction('activateNewTab', this._buildActivationDetail(target));
  }

  handleActivateNewTabBackgroundKey() {
    const currentState = this.state.getState();
    const x = currentState.lastMouse.x;
    const y = currentState.lastMouse.y;

    if (this._tryActivateIframeUnderCursor(x, y, { background: true, openInNewTab: true })) {
      this.showRipple(x, y);
      this.emitAction('activateNewTabBackground', { viaIframe: true });
      return;
    }

    const target = this._getValidatedActivationTarget(currentState);

    if (!target || target === document.documentElement || target === document.body) {
      return;
    }

    // Popover mode is modal: don't allow activation on background page elements.
    if (currentState.mode === MODES.POPOVER) {
      if (!this._isElementInPopover(target)) {
        return;
      }
    }

    // Find a URL to open:
    // - traditional <a href="...">
    // - KeyPilot URL listing rows rendered as `[role="link"][data-kp-url="..."]`
    let link = target;
    let url = null;
    try {
      if (link && link.tagName !== 'A') {
        link = link.closest?.('a[href]') || link;
      }
    } catch { /* ignore */ }

    if (link && link.tagName === 'A' && link.href) {
      url = link.href;
    } else {
      try {
        let roleLink = target;
        if (roleLink instanceof Element) {
          if (roleLink.getAttribute('role') !== 'link') {
            roleLink = roleLink.closest?.('[role="link"][data-kp-url]') || null;
          }
          if (roleLink && roleLink.getAttribute('role') === 'link' && roleLink.dataset?.kpUrl) {
            url = String(roleLink.dataset.kpUrl || '').trim() || null;
            link = roleLink;
          }
        }
      } catch { /* ignore */ }
    }

    // Only work if we have a URL
    if (!url) {
      console.log('[KeyPilot] Activate New Tab Background: not hovering over a hyperlink');
      return;
    }

    console.log('[KeyPilot] Opening link in new tab (background):', url);

    // Store coordinates for link click
    this.mouseCoordinateManager.handleLinkClick(currentState.lastMouse.x, currentState.lastMouse.y, link);

    // Open link in a background tab (middle-click style: do NOT switch/focus the new tab).
    try {
      if (this._sendRuntimeMessage({ type: MSG.OPEN_URL_BACKGROUND, url })) {
        this.showRipple(currentState.lastMouse.x, currentState.lastMouse.y);
        this.overlayManager.flashFocusOverlay(link);
        this.postClickRefresh(link, currentState.lastMouse.x, currentState.lastMouse.y);
        this.emitAction('activateNewTabBackground', { isLink: true, href: url });
        return;
      }
      // Context invalidated — fall through to window.open fallback.
      try { window.open(url, '_blank', 'noopener,noreferrer'); } catch { /* ignore */ }
    } catch (error) {
      if (noteExtensionContextError(error)) {
        this._handleExtensionContextInvalidated();
      } else {
        console.error('[KeyPilot] Failed to open link in background tab:', error);
      }
      // Fallback (may focus the new tab depending on browser/user settings).
      try { window.open(url, '_blank', 'noopener,noreferrer'); } catch { /* ignore */ }
    }
  }

  handleOpenPopover(e) {
    if (!this._allowActionKey('handleOpenPopover', e)) return;
    // Check if popover is already open - if so, close it (toggle behavior)
    if (this.overlayManager.isPopoverOpen()) {
      this.handleClosePopover();
      return;
    }

    const currentState = this.state.getState();
    const { lastMouse } = currentState;

    // Prefer DOM-hover focusEl; fall back to elementFromPoint when nothing is hovered.
    let target = currentState.focusEl;
    if (!target) {
      const under = this.detector.deepElementFromPoint(lastMouse.x, lastMouse.y);
      target = this.detector.findClickable(under);
    }

    // Resolve to the closest anchor (including within shadow DOM).
    // deepElementFromPoint() often returns a child <div> inside a link (e.g. archive.org
    // `div#collection-image-title` inside a shadow-root <a href=...>), so requiring
    // target.tagName === 'A' is too strict.
    if (!target || !(target instanceof Element)) {
      console.log('[KeyPilot] Open popover: not hovering over a link');
      return;
    }

    let probe = target;
    let link = probe;

    // First try to find a traditional <a> element
    if (link.tagName !== 'A') {
      link = link.closest('a[href]');
    }

    // If no <a> element found, look for role="link" elements with data-kp-url
    let url = null;
    if (link && link.tagName === 'A' && link.href) {
      url = link.href;
    } else {
      // Look for role="link" elements (used by renderUrlListing)
      let roleLink = probe;
      if (roleLink.getAttribute('role') !== 'link') {
        roleLink = roleLink.closest('[role="link"]');
      }

      if (roleLink && roleLink.getAttribute('role') === 'link' && roleLink.dataset.kpUrl) {
        url = roleLink.dataset.kpUrl;
        link = roleLink; // Use the role="link" element as the link
      }
    }

    // If we're inside a shadow root and closest() didn't find it, walk up to the host and retry.
    // This allows resolving links that span across shadow boundaries (common with web-components).
    let guard = 0;
    while ((!url) && guard++ < 10) {
      const root = probe.getRootNode?.();
      if (!(root instanceof ShadowRoot) || !(root.host instanceof Element)) break;
      probe = root.host;

      // Retry finding links in the host element
      let hostLink = probe;
      if (hostLink.tagName !== 'A') {
        hostLink = hostLink.closest('a[href]');
      }
      if (hostLink && hostLink.tagName === 'A' && hostLink.href) {
        url = hostLink.href;
        link = hostLink;
        break;
      }

      // Also check for role="link" in host
      let hostRoleLink = probe;
      if (hostRoleLink.getAttribute('role') !== 'link') {
        hostRoleLink = hostRoleLink.closest('[role="link"]');
      }
      if (hostRoleLink && hostRoleLink.getAttribute('role') === 'link' && hostRoleLink.dataset.kpUrl) {
        url = hostRoleLink.dataset.kpUrl;
        link = hostRoleLink;
        break;
      }
    }

    if (!url) {
      console.log('[KeyPilot] Open popover: not hovering over a link');
      return;
    }
    console.log('[KeyPilot] Opening popover for link:', url);

    // Show popover
    this.overlayManager.showPopover(url);
    this.state.setPopoverOpen(true, url);
  }

  handlePreviewLinkPopover(e) {
    if (!this._allowActionKey('handlePreviewLinkPopover', e)) return;
    // Check if popover is already open - if so, close it (toggle behavior)
    if (this.overlayManager.isPopoverOpen()) {
      this.handleClosePopover();
      return;
    }

    const currentState = this.state.getState();
    const { lastMouse } = currentState;

    // Prefer DOM-hover focusEl; fall back to elementFromPoint when nothing is hovered.
    // Keep the raw under-cursor node so collapsed history groups (not always "clickable")
    // can still resolve a root-domain preview URL.
    let under = null;
    try {
      under = this.detector.deepElementFromPoint(lastMouse.x, lastMouse.y);
    } catch { /* ignore */ }

    let target = currentState.focusEl;
    if (!target) {
      target = this.detector.findClickable(under) || under;
    }

    // Resolve to the closest anchor (including within shadow DOM)
    if (!target || !(target instanceof Element)) {
      console.log('[KeyPilot] Preview popover: not hovering over a link');
      return;
    }

    let probe = target;
    let link = probe;

    // First try to find a traditional <a> element
    if (link.tagName !== 'A') {
      link = link.closest('a[href]');
    }

    // If no <a> element found, look for role="link" elements with data-kp-url
    let url = null;
    if (link && link.tagName === 'A' && link.href) {
      url = link.href;
    } else {
      // Look for role="link" elements (used by renderUrlListing)
      let roleLink = probe;
      if (roleLink.getAttribute('role') !== 'link') {
        roleLink = roleLink.closest('[role="link"]');
      }

      if (roleLink && roleLink.getAttribute('role') === 'link' && roleLink.dataset.kpUrl) {
        url = roleLink.dataset.kpUrl;
        link = roleLink;
      }
    }

    // If we're inside a shadow root and closest() didn't find it, walk up to the host and retry
    let guard = 0;
    while ((!url) && guard++ < 10) {
      const root = probe.getRootNode?.();
      if (!(root instanceof ShadowRoot) || !(root.host instanceof Element)) break;
      probe = root.host;

      // Retry finding links in the host element
      let hostLink = probe;
      if (hostLink.tagName !== 'A') {
        hostLink = hostLink.closest('a[href]');
      }
      if (hostLink && hostLink.tagName === 'A' && hostLink.href) {
        url = hostLink.href;
        link = hostLink;
        break;
      }

      // Also check for role="link" in host
      let hostRoleLink = probe;
      if (hostRoleLink.getAttribute('role') !== 'link') {
        hostRoleLink = hostRoleLink.closest('[role="link"]');
      }
      if (hostRoleLink && hostRoleLink.getAttribute('role') === 'link' && hostRoleLink.dataset.kpUrl) {
        url = hostRoleLink.dataset.kpUrl;
        link = hostRoleLink;
        break;
      }
    }

    // Collapsed Recent History group boxes (New Tab): no child row is hit-testable,
    // so preview the root domain shown as the group label.
    if (!url) {
      try {
        const start =
          (under instanceof Element ? under : null) ||
          (target instanceof Element ? target : null);
        const outline = start?.closest?.('details.history-outline') || null;
        if (outline && !outline.open) {
          const rootUrl = String(outline.dataset?.kpRootUrl || '').trim();
          if (rootUrl) {
            url = rootUrl;
            link = outline;
          }
        }
      } catch { /* ignore */ }
    }

    if (!url) {
      console.log('[KeyPilot] Preview popover: not hovering over a link');
      return;
    }
    console.log('[KeyPilot] Opening preview popover for link:', url);

    // Show preview popover near cursor
    this.overlayManager.showPreviewPopover(url, {
      title: 'Link Preview',
      mouseX: lastMouse.x,
      mouseY: lastMouse.y
    });
    this.state.setPopoverOpen(true, url);
  }

  getSettingsPopoverUrl() {
    try {
      return chrome.runtime.getURL('pages/settings.html');
    } catch {
      return null;
    }
  }

  getGuidePopoverUrl() {
    try {
      return chrome.runtime.getURL('pages/guide.html');
    } catch {
      return null;
    }
  }

  handleOpenSettingsPopover() {
    const url = this.getSettingsPopoverUrl();
    if (!url) return;

    // Close any existing popover first (e.g., if opened from Guide)
    const currentState = this.state.getState();
    if (currentState.mode === MODES.POPOVER) {
      this.handleClosePopover();
    }

    // Master–detail settings layout: left nav (~168px) + detail pane.
    // Prefer a wider frame than the old single-column cards so tabs stay readable.
    const settingsContainerWidth = Math.min(980, window.innerWidth - 36) + 20;
    const settingsContainerHeight = Math.min(window.innerHeight * 0.82, window.innerHeight - 80) + 20;

    this.overlayManager.showPopover(url, {
      title: 'KeyPilot Settings',
      hintKeyLabel: "'",
      closeKeys: ['Escape', "'", '"'],
      width: `${settingsContainerWidth}px`,
      height: `${settingsContainerHeight}px`
    });
    this.state.setPopoverOpen(true, url);
  }

  handleToggleSettingsPopover() {
    // Only operate in the top frame to avoid duplicates.
    if (window !== window.top) return;
    const currentState = this.state.getState();
    const settingsUrl = this.getSettingsPopoverUrl();
    if (!settingsUrl) return;

    if (currentState.mode === MODES.POPOVER && currentState.popoverUrl === settingsUrl) {
      this.handleClosePopover();
      return;
    }

    this.handleOpenSettingsPopover();
  }

  handleOpenGuidePopover() {
    const url = this.getGuidePopoverUrl();
    if (!url) return;

    // Close any existing popover first (e.g., if opened from Settings)
    const currentState = this.state.getState();
    if (currentState.mode === MODES.POPOVER) {
      this.handleClosePopover();
    }

    // Calculate guide container dimensions + 10pt padding
    // The guide container has max-width: 920px and padding: 18px on each side (same as settings)
    const guideContainerWidth = Math.min(920, window.innerWidth - 36) + 20; // 920px max + 10pt padding each side
    const guideContainerHeight = Math.min(window.innerHeight * 0.8, window.innerHeight - 100) + 20; // Use 80vh max + 10pt padding each side

    this.overlayManager.showPopover(url, {
      title: 'KeyPilot Guide',
      hintKeyLabel: 'Esc',
      closeKeys: ['Escape', "'", '"', 'p', 'P'],
      width: `${guideContainerWidth}px`,
      height: `${guideContainerHeight}px`
    });
    this.state.setPopoverOpen(true, url);
  }

  handleClosePopover() {
    console.log('[KeyPilot] Closing popover');
    this.overlayManager.hidePopover();
    this.state.setPopoverOpen(false, null);
  }

  handleToggleTabHistoryPopover() {
    // Only operate in the top frame to avoid duplicates.
    if (window !== window.top) return;
    try {
      this.tabHistoryPopover?.toggle?.();
    } catch (e) {
      console.warn('[KeyPilot] Failed to toggle tab history popover:', e);
    }
  }

  handleToggleKeyboardHelp() {
    try {
      const next = !this._keyboardHelpVisible;
      this.applyKeyboardHelpVisibility(next, { persist: true });
      this.emitAction('toggleKeyboardHelp', { visible: next });
    } catch (e) {
      console.warn('[KeyPilot] Failed to toggle keyboard reference:', e);
    }
  }

  deleteElement(element) {
    if (!element || element === document.documentElement || element === document.body) {
      return;
    }

    try {
      element.remove();
    } catch (error) {
      // Fallback: hide element
      try {
        element.classList.add('kpv2-hidden');
        element.setAttribute('aria-hidden', 'true');
      } catch { }
    }
  }

  handleEscapeFromTextFocus(currentState) {
    console.debug('Escape pressed in text focus mode (exit only)');

    // Cancel any hover-click countdown/armed UI.
    this._disarmTextModeClick();
    try { this.state.setFocusElement(null); } catch { /* ignore */ }

    // Use the simple, proven approach that works in DevTools
    // Blur the active element and set focus to the body
    if (document.activeElement) {
      document.activeElement.blur();
    }
    document.body.focus();

    // Clear the text focus state
    this.focusDetector.clearTextFocus();

    console.debug('Text focus escape completed');
  }

  /**
   * Cancel all active modes and return to normal mode
   * Handles mode-specific cleanup logic
   */
  cancelModes() {
    const currentState = this.state.getState();
    
    console.log('[KeyPilot] Canceling modes, current mode:', currentState.mode);

    // Launcher is not a state.mode but must still close on Escape/cancel.
    try {
      if (this.launcherPopover?.isOpen?.()) {
        this.launcherPopover.hide();
        return;
      }
    } catch { /* ignore */ }
    
    // Handle highlight mode cancellation specifically
    if (currentState.mode === MODES.HIGHLIGHT) {
      this.cancelHighlightMode();
      return;
    }
    
    // Handle popover mode cancellation
    if (currentState.mode === MODES.POPOVER) {
      this.handleClosePopover();
      return;
    }

    // Handle omnibox mode cancellation
    if (currentState.mode === MODES.OMNIBOX) {
      this.handleCloseOmnibox();
      return;
    }

    // Shared inspector pick: Esc exits pick mode only — leave sticky effects
    // (e.g. applied columns + slip bar) intact.
    if (currentState.mode === MODES.INSPECTOR ||
        currentState.mode === MODES.DELETE ||
        currentState.mode === MODES.COLS) {
      this.inspector.exit();
      try { this.overlayManager?.hideInspectorModeIndicator?.(); } catch { /* ignore */ }
      return;
    }
    
    // Don't reset if we're in text focus mode - that should only be cleared by ESC or blur
    if (currentState.mode !== MODES.TEXT_FOCUS) {
      this.state.reset();
    }
  }

  handleOpenOmnibox() {
    try {
      if (window !== window.top) return;
      // Omnibox should not be affected by text focus mode.
      // If the user was in text mode, clear it before opening omnibox.
      try { this._disarmTextModeClick?.(); } catch { /* ignore */ }
      try { this.focusDetector?.clearTextFocus?.(); } catch { /* ignore */ }
      this.state.setMode(MODES.OMNIBOX);
      this.omniboxManager?.show?.('');
    } catch (e) {
      console.warn('[KeyPilot] Failed to open omnibox:', e);
    }
  }

  handleCloseOmnibox() {
    try {
      if (window !== window.top) return;
      this.omniboxManager?.hide?.();
      this.state.setMode(MODES.NONE);
    } catch (e) {
      console.warn('[KeyPilot] Failed to close omnibox:', e);
    }
  }

  showRipple(x, y) {
    const ripple = document.createElement('div');
    ripple.className = CSS_CLASSES.RIPPLE;
    ripple.style.left = `${x}px`;
    ripple.style.top = `${y}px`;
    document.body.appendChild(ripple);

    ripple.addEventListener('animationend', () => ripple.remove(), { once: true });
  }

  showFlashNotification(message, backgroundColor = COLORS.NOTIFICATION_SUCCESS) {
    try {
      // Validate input parameters
      if (!message || typeof message !== 'string') {
        console.warn('[KeyPilot] Invalid message provided to showFlashNotification:', message);
        return;
      }
      
      if (!backgroundColor || typeof backgroundColor !== 'string') {
        console.warn('[KeyPilot] Invalid backgroundColor provided, using default');
        backgroundColor = COLORS.NOTIFICATION_SUCCESS;
      }
      
      // Validate document availability
      if (!document || !document.body) {
        console.warn('[KeyPilot] Document or document.body not available for notification');
        return;
      }
      
      // Create notification overlay with error handling
      let notification = null;
      try {
        notification = document.createElement('div');
        if (!notification) {
          throw new Error('Failed to create notification element');
        }
      } catch (createError) {
        console.error('[KeyPilot] Error creating notification element:', createError);
        return;
      }
      
      notification.className = 'kpv2-flash-notification';
      notification.textContent = message;
      
      // Style the notification with error handling
      try {
        Object.assign(notification.style, {
          position: 'fixed',
          top: '20px',
          left: '50%',
          transform: 'translateX(-50%)',
          backgroundColor: backgroundColor,
          color: 'white',
          padding: '12px 24px',
          borderRadius: '6px',
          fontSize: '14px',
          fontWeight: '500',
          fontFamily: 'system-ui, -apple-system, sans-serif',
          zIndex: String(Z_INDEX.NOTIFICATION),
          boxShadow: '0 4px 12px rgba(0, 0, 0, 0.15)',
          opacity: '0',
          transition: 'opacity 0.3s ease-in-out',
          pointerEvents: 'none',
          maxWidth: '400px',
          wordWrap: 'break-word',
          textAlign: 'center'
        });
      } catch (styleError) {
        console.error('[KeyPilot] Error styling notification:', styleError);
        // Continue with basic styling
        notification.style.position = 'fixed';
        notification.style.top = '20px';
        notification.style.left = '50%';
        notification.style.backgroundColor = backgroundColor;
        notification.style.color = 'white';
        notification.style.padding = '12px 24px';
        notification.style.zIndex = String(Z_INDEX.NOTIFICATION);
      }

      // Add to document with error handling
      try {
        document.body.appendChild(notification);
      } catch (appendError) {
        console.error('[KeyPilot] Error appending notification to document:', appendError);
        return;
      }

      // Show notification immediately (animation removed)
      notification.style.opacity = '1';

      // Fade out and remove after appropriate duration based on message type
      const duration = backgroundColor === COLORS.NOTIFICATION_ERROR ? 4000 : 2000; // Show errors longer
      
      setTimeout(() => {
        try {
          if (notification && notification.parentNode) {
            notification.parentNode.removeChild(notification);
          }
        } catch (removeError) {
          console.warn('[KeyPilot] Error removing notification:', removeError);
        }
      }, duration);
      
    } catch (error) {
      console.error('[KeyPilot] Unexpected error showing flash notification:', error);
      
      // Fallback: try to show a basic alert if available
      try {
        if (window.alert && typeof window.alert === 'function') {
          window.alert(`KeyPilot: ${message}`);
        }
      } catch (alertError) {
        console.error('[KeyPilot] Even alert fallback failed:', alertError);
      }
    }
  }

  updateOverlays(focusEl, inspectorEl = null, inspectorKind = null) {
    // Don't update overlays if extension is disabled
    if (!this.enabled) {
      return;
    }

    const currentState = this.state.getState();
    const inspEl = inspectorEl !== undefined && inspectorEl !== null
      ? inspectorEl
      : currentState.inspectorEl;
    const kind = inspectorKind != null
      ? inspectorKind
      : currentState.inspectorKind;

    // Display-only coalescing: for same-destination link clusters, draw a single unioned rect
    // to reduce flicker while preserving correct element selection for clicks.
    let focusRectOverride = null;
    try {
      if (focusEl &&
          this.intersectionManager &&
          typeof this.intersectionManager.getDisplayRectForElement === 'function') {
        focusRectOverride = this.intersectionManager.getDisplayRectForElement(focusEl, { tolerancePx: 6 });
      }
    } catch { /* ignore */ }

    this.overlayManager.updateOverlays(
      focusEl,
      inspEl,
      currentState.mode,
      currentState.focusedTextElement,
      focusRectOverride,
      kind
    );
  }

  logPerformanceMetrics() {
    const now = Date.now();
    const timeSinceLastLog = now - this.performanceMetrics.lastMetricsLog;
    
    if (timeSinceLastLog < 10000) return; // Only log every 10 seconds
    
    const intersectionMetrics = this.intersectionManager.getMetrics();
    const scrollMetrics = this.scrollManager.getScrollMetrics();
    
    console.group('[KeyPilot] Performance Metrics');
    console.log('Mouse Queries:', this.performanceMetrics.mouseQueries);
    console.log('Intersection Observer Cache Hit Rate:', intersectionMetrics.cacheHitRate);
    console.log('Visible Interactive Elements:', intersectionMetrics.visibleElements);
    console.log('Scroll Throttle Ratio:', scrollMetrics.throttleRatio);
    console.log('Average Scroll Duration:', `${scrollMetrics.averageScrollDuration.toFixed(1)}ms`);
    console.groupEnd();
    
    this.performanceMetrics.lastMetricsLog = now;
  }

  /**
   * Enable KeyPilot functionality
   */
  async enable() {
    if (this.enabled) return;
    
    this.enabled = true;
    
    // Only initialize if initialization is complete
    if (this.initializationComplete) {
      // Restart event listeners
      this.start();
      
      // Show cursor
      if (this.cursor) {
        this.cursor.show();
      }
      
      // Restart focus detector
      if (this.focusDetector) {
        this.focusDetector.start();
      }
      
      // Restart intersection manager
      if (this.intersectionManager) {
        // Re-apply permanent DOM-hover targeting BEFORE re-init.
        try {
          if (typeof this.intersectionManager.setDomHoverListenersEnabled === 'function') {
            this.intersectionManager.setDomHoverListenersEnabled(
              true,
              (el) => this._handleDomHoverChange(el)
            );
          }
        } catch { /* ignore */ }
        await this.intersectionManager.init();
      }

      // Re-apply visual indicator on enable.
      try {
        if (this.overlayManager && typeof this.overlayManager.setDomHoverFocusColorsEnabled === 'function') {
          this.overlayManager.setDomHoverFocusColorsEnabled(true);
        }
      } catch { /* ignore */ }
      
      // Restart scroll manager
      if (this.scrollManager) {
        this.scrollManager.init();
      }
      
      // Reset state to normal mode
      this.state.reset();

      // Force a hover/overlay refresh immediately on re-enable.
      // Without this, focus overlays can remain hidden until the mouse moves past the
      // threshold gate in `updateElementsUnderCursorWithThreshold()`, even though
      // keyboard actions still work.
      try {
        const { lastMouse } = this.state.getState();
        const x = Number(lastMouse?.x);
        const y = Number(lastMouse?.y);
        if (Number.isFinite(x) && Number.isFinite(y) && (x !== 0 || y !== 0)) {
          // Bypass threshold gating: the DOM may have changed while disabled.
          this.updateElementsUnderCursor(x, y, false, null);
        } else {
          // If we don't have a valid mouse position yet, attempt to seed it.
          await this.initializeCursorPosition();
          const s2 = this.state.getState();
          const x2 = Number(s2.lastMouse?.x);
          const y2 = Number(s2.lastMouse?.y);
          if (Number.isFinite(x2) && Number.isFinite(y2) && (x2 !== 0 || y2 !== 0)) {
            this.updateElementsUnderCursor(x2, y2, false, null);
          }
        }
      } catch { /* ignore */ }
    }

    // Restore keyboard reference UI based on persisted state.
    // Fire-and-forget: we don't want to block enable() on storage.
    try {
      this.refreshKeyboardHelpVisibilityFromStorage();
    } catch { /* ignore */ }

    try {
      this.controlStrip?.setEnabledState?.(true);
      this.controlStrip?.setKeyboardHelpActive?.(!!this._keyboardHelpVisible);
      this._syncControlStripTextModeFromState();
    } catch { /* ignore */ }
    
    console.log('[KeyPilot] Extension enabled');
  }

  /**
   * Tear down every transient KeyPilot UI surface (popovers, launcher, omnibox,
   * keyboard help, highlight mode, onboarding panels, flash toasts, etc.).
   * Safe to call while already disabled / partially initialized.
   */
  dismissActiveUI() {
    // Modes / highlight selection first so follow-up closes don't fight mode state.
    try {
      if (this.state?.getState?.()?.mode === MODES.HIGHLIGHT) {
        this.cancelHighlightMode?.();
      }
    } catch { /* ignore */ }

    // Launcher (;)
    try { this.launcherPopover?.hide?.(); } catch { /* ignore */ }

    // Omnibox (Alt+L)
    try { this.handleCloseOmnibox?.(); } catch { /* ignore */ }
    try { this.omniboxManager?.hide?.(); } catch { /* ignore */ }

    // Tab history popover
    try { this.tabHistoryPopover?.hide?.(); } catch { /* ignore */ }

    // P-key / settings / guide iframe popovers + preview popovers
    try { this.handleClosePopover?.(); } catch { /* ignore */ }
    try { this.overlayManager?.hidePopover?.(); } catch { /* ignore */ }

    // Any remaining PopupManager modals (backdrop + panels)
    try { this.overlayManager?.popupManager?.closeAll?.(); } catch { /* ignore */ }

    // Floating keyboard reference panel
    if (this.floatingKeyboardHelp) {
      try { this.floatingKeyboardHelp.cleanup(); } catch { /* ignore */ }
      this.floatingKeyboardHelp = null;
    }

    // Note: Control strip is intentionally NOT torn down here so users can
    // re-enable KeyPilot from the On/Off segment while the extension is off.

    // Focus / hover chrome
    try {
      this.overlayManager?.hideFocusOverlay?.();
      this.overlayManager?.hideDeleteOverlay?.();
      this.overlayManager?.hideEscExitLabel?.();
      this.overlayManager?.hideActiveTextInputFrame?.();
      this.overlayManager?.hideFocusedTextOverlay?.();
    } catch { /* ignore */ }

    // Onboarding / practice panels: hide only — do NOT setActive(false).
    // Toggle-off during an incomplete walkthrough must keep progress so re-enable
    // can restore the panel. Do not dismiss the re-enable tip here — onboarding
    // shows it right before disable runs (same toggleExtension event).
    try {
      const ob = window.__KeyPilotOnboarding;
      try { ob?.panel?.hide?.(); } catch { /* ignore */ }
      try { ob?.practicePanel?.hide?.(); } catch { /* ignore */ }
    } catch { /* ignore */ }

    // Ephemeral toasts left on the page
    try {
      document.querySelectorAll(
        '.kpv2-flash-notification, .kpv2-toggle-notification'
      ).forEach((el) => {
        try { el.remove(); } catch { /* ignore */ }
      });
    } catch { /* ignore */ }

    // Reset mode/state so a re-enable starts clean
    try { this.state?.reset?.(); } catch { /* ignore */ }
  }

  /**
   * Disable KeyPilot functionality
   */
  disable() {
    if (!this.enabled) return;
    
    this.enabled = false;

    // Always dismiss popovers/launcher/omnibox even if init is incomplete.
    this.dismissActiveUI();

    // Clear sticky column layout + slip bar so the page is not left altered.
    try { this.columnLayoutManager?.clear?.(); } catch { /* ignore */ }

    try {
      this.controlStrip?.setEnabledState?.(false);
      this.controlStrip?.setKeyboardHelpActive?.(false);
    } catch { /* ignore */ }
    
    // Only cleanup if initialization is complete
    if (this.initializationComplete) {
      // Stop event listeners
      this.stop();
      
      // Hide cursor
      if (this.cursor) {
        this.cursor.hide();
      }
      
      // Stop focus detector
      if (this.focusDetector) {
        this.focusDetector.stop();
      }
      
      // Clean up intersection manager
      if (this.intersectionManager) {
        // Ensure delegated DOM-hover listeners are detached promptly on disable.
        try { this.intersectionManager.setDomHoverListenersEnabled(false, null); } catch { /* ignore */ }
        this.intersectionManager.cleanup();
      }
      
      // Clean up scroll manager
      if (this.scrollManager) {
        this.scrollManager.cleanup();
      }
      
      // Clean up edge-only processing
      if (this.rectangleIntersectionObserver) {
        this.rectangleIntersectionObserver.cleanup();
        this.rectangleIntersectionObserver = null;
      }
      
      // Clear any active state (again after manager cleanup)
      this.state.reset();
    }
    
    console.log('[KeyPilot] Extension disabled');
  }

  /**
   * Initialize cursor position using stored coordinates or fallback
   */
  async initializeCursorPosition() {
    const currentState = this.state.getState();
    
    // If mouse position hasn't been set yet (still at 0,0), initialize with stored coordinates or fallback
    if (currentState.lastMouse.x === 0 && currentState.lastMouse.y === 0) {
      // Get initial position from mouse coordinate manager
      const initialPosition = this.mouseCoordinateManager.getInitialCursorPosition();
      
      console.log('[KeyPilot] Initializing cursor position:', initialPosition);
      
      this.state.setMousePosition(initialPosition.x, initialPosition.y);
      this.cursor.updatePosition(initialPosition.x, initialPosition.y);
      
      // Start inactive mouse monitoring if enabled
      this.mouseCoordinateManager.startInactiveMouseMonitoring();
    }
  }

  /**
   * Check if KeyPilot is currently enabled
   */
  isEnabled() {
    return this.enabled;
  }

  cleanup() {
    try { this.dismissActiveUI(); } catch { /* ignore */ }
    try { this.columnLayoutManager?.clear?.(); } catch { /* ignore */ }
    try {
      if (this.controlStrip) {
        this.controlStrip.cleanup();
        this.controlStrip = null;
      }
    } catch { /* ignore */ }
    this.stop();
    // Clean up intersection observer optimizations
    if (this.intersectionManager) {
      this.intersectionManager.cleanup();
    }
    
    if (this.scrollManager) {
      this.scrollManager.cleanup();
    }

    if (this.focusDetector) {
      this.focusDetector.stop();
    }

    if (this.mouseCoordinateManager) {
      this.mouseCoordinateManager.cleanup();
    }

    if (this.cursor) {
      this.cursor.cleanup();
    }

    if (this.overlayManager) {
      this.overlayManager.cleanup();
    }

    if (this.styleManager) {
      this.styleManager.cleanup();
    }

    if (this.shadowDOMManager) {
      this.shadowDOMManager.cleanup();
    }
    
    // Remove custom event listeners
    if (this._boundScrollEndHandler) {
      document.removeEventListener('keypilot:scroll-end', this._boundScrollEndHandler);
      this._boundScrollEndHandler = null;
    }
  }
}


  // Module: src/content-script.js
/**
 * Content script entry point
 */





/**
 * When running inside an iframe, we normally avoid initializing full KeyPilot.
 * For KeyPilot popover iframes, we use a bridge handshake from the parent to:
 * - keep Esc/P close working via postMessage
 * - optionally initialize full KeyPilot inside the iframe for the full cursor/overlay experience
 *
 * Separately, a thin frame-click-agent always runs in child frames so top-frame
 * F-activate can click through cross-origin iframes (e.g. Google account switcher).
 */
function setupIframeAgents() {
  try {
    // Only install in iframes
    if (window === window.top) return;

    // Thin idle agent: postMessage activate + F when this frame has focus.
    installFrameClickAgent();

    installPopoverIframeBridge({
      // Frame-click-agent owns pre-KP activate keys; avoid double-clicking links.
      enableFClickBeforeKeyPilot: false,
      onBridgeInit: () => {
        // Marker for debugging / future conditional behavior.
        try { window.__KP_POPOVER_IFRAME = true; } catch { /* ignore */ }
        try {
          // Fire-and-forget; toggle handler will sync enabled state.
          initializeKeyPilot();
        } catch {
          // ignore
        }
      },
      onError: (error) => {
        console.warn('[KeyPilot] Failed to install popover iframe bridge:', error);
      }
    });
  } catch (error) {
    console.warn('[KeyPilot] Failed to install iframe agents:', error);
  }
}

// Initialize KeyPilot with toggle functionality
async function initializeKeyPilot() {
  try {
    // Create KeyPilot instance
    const keyPilot = new KeyPilot();

    // Store reference globally for debugging/metrics panels (used by OverlayManager debug panel)
    // Note: this is within the content-script isolated world; it is intended for KeyPilot internals.
    window.keyPilot = keyPilot;
    
    // Create toggle handler and wrap KeyPilot instance
    const toggleHandler = new KeyPilotToggleHandler(keyPilot);
    
    // Initialize toggle handler (queries service worker for state)
    await toggleHandler.initialize();
    
    // Store reference globally for debugging
    window.__KeyPilotToggleHandler = toggleHandler;
    
  } catch (error) {
    console.error('[KeyPilot] Failed to initialize with toggle functionality:', error);
    
    // Fallback: initialize KeyPilot without toggle functionality
    try {
      const keyPilot = new KeyPilot();
      window.keyPilot = keyPilot;
      console.warn('[KeyPilot] Initialized without toggle functionality as fallback');
    } catch (fallbackError) {
      console.error('[KeyPilot] Complete initialization failure:', fallbackError);
    }
  }
}

// If inside an iframe, install thin agents (no full KeyPilot unless popover INIT).
setupIframeAgents();

// Initialize KeyPilot only in the top frame.
if (window === window.top) {
  (async () => {
    // Ensure we query the service worker toggle state before onboarding decides whether it can show.
    // This prevents the onboarding walkthrough from briefly appearing on new tabs when KeyPilot is OFF.
    await initializeKeyPilot();

    // Initialize onboarding walkthrough (top-level only).
    try {
      const onboarding = new OnboardingManager();
      await onboarding.init();
      window.__KeyPilotOnboarding = onboarding;
    } catch (e) {
      console.warn('[KeyPilot] Failed to initialize onboarding:', e);
    }
  })();
}



})();
