sailfork / t3code-github / apps/ desktop/ src/ preview/ PickPreload.ts

// @effect-diagnostics globalDate:off globalTimers:off - This isolated Electron preload does not run inside an Effect runtime.
import { ipcRenderer } from "electron";
import { getElementContext } from "react-grab/primitives";
import { matchesKeybindingShortcut } from "@t3tools/shared/keybindingMatching";
import type {
  DesktopPreviewAnnotationTheme,
  DesktopPreviewAnnotationVoiceConfig,
  DesktopPreviewAnnotationVoiceEvent,
  DesktopPreviewAnnotationVoiceState,
  PickedElementPayload,
  PickedElementStackFrame,
  PreviewAnnotationPayload,
  PreviewAnnotationPoint,
  PreviewAnnotationRect,
  PreviewAnnotationRegionTarget,
  PreviewAnnotationStrokeTarget,
  PreviewAnnotationStyleChange,
  PreviewAnnotationSubmission,
} from "@t3tools/contracts";

import { createAnnotationIdSource } from "./AnnotationIds.ts";
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts";
import { installRecordingCursor } from "./RecordingCursor.ts";
import { DEFAULT_RECORDING_INPUT_OPTIONS } from "./RecordingInput.ts";
import {
  ANNOTATION_CAPTURED_CHANNEL,
  ANNOTATION_SEND_ENABLED_CHANNEL,
  ANNOTATION_THEME_CHANNEL,
  ANNOTATION_VOICE_EVENT_CHANNEL,
  ANNOTATION_VOICE_STATE_CHANNEL,
  CANCEL_PICK_CHANNEL,
  ELEMENT_PICKED_CHANNEL,
  HUMAN_INPUT_CHANNEL,
  MOUSE_NAVIGATE_CHANNEL,
  RECORDING_CURSOR_CHANNEL,
  RECORDING_POINTER_CHANNEL,
  RECORDING_KEY_CHANNEL,
  RECORDING_INPUT_CHANNEL,
  RECORDING_CONTROLLER_CHANNEL,
  START_PICK_CHANNEL,
} from "./GuestProtocol.ts";
const OVERLAY_ATTRIBUTE = "data-t3code-annotation-ui";
const INPUT_LAYER_ATTRIBUTE = "data-t3code-annotation-input";
const Z_INDEX_OVERLAY = 2147483646;
const PRIMARY = "var(--t3-primary)";
const PRIMARY_FILL = "color-mix(in srgb, var(--t3-primary) 10%, transparent)";
const MAX_MARQUEE_ELEMENTS = 20;
const ELEMENT_CONTEXT_TIMEOUT_MS = 5_000;
const CONTENT_LAYER_Z_INDEX = 1;
const CHROME_LAYER_Z_INDEX = 10;

let recordingCursor: ReturnType<typeof installRecordingCursor> | null = null;
ipcRenderer.on(
  RECORDING_CURSOR_CHANNEL,
  (_event, active: unknown, inputOptions: unknown, controller: unknown) => {
    if (active === true) {
      const options =
        typeof inputOptions === "object" && inputOptions !== null
          ? {
              showKeyPresses:
                "showKeyPresses" in inputOptions && inputOptions.showKeyPresses === true,
              showMousePresses:
                "showMousePresses" in inputOptions && inputOptions.showMousePresses === true,
            }
          : DEFAULT_RECORDING_INPUT_OPTIONS;
      recordingCursor ??= installRecordingCursor(document, window, options, (input) =>
        ipcRenderer.send(RECORDING_INPUT_CHANNEL, input),
      );
      recordingCursor.setTheme(annotationTheme);
      if (controller === "agent" || controller === "human" || controller === "none")
        recordingCursor.setController(controller);
    } else {
      recordingCursor?.dispose();
      recordingCursor = null;
    }
  },
);
ipcRenderer.on(RECORDING_CONTROLLER_CHANNEL, (_event, controller: unknown, point: unknown) => {
  const humanPoint =
    typeof point === "object" &&
    point !== null &&
    "x" in point &&
    typeof point.x === "number" &&
    Number.isFinite(point.x) &&
    "y" in point &&
    typeof point.y === "number" &&
    Number.isFinite(point.y)
      ? { x: point.x, y: point.y }
      : undefined;
  if (controller === "agent" || controller === "human" || controller === "none")
    recordingCursor?.setController(controller, humanPoint);
});
ipcRenderer.on(RECORDING_KEY_CHANNEL, (_event, input: unknown) => {
  if (
    typeof input !== "object" ||
    input === null ||
    !("key" in input) ||
    typeof input.key !== "string"
  )
    return;
  recordingCursor?.keyPress({
    key: input.key,
    metaKey: "metaKey" in input && input.metaKey === true,
    ctrlKey: "ctrlKey" in input && input.ctrlKey === true,
    altKey: "altKey" in input && input.altKey === true,
    shiftKey: "shiftKey" in input && input.shiftKey === true,
  });
});
ipcRenderer.on(RECORDING_POINTER_CHANNEL, (_event, point: unknown) => {
  if (
    typeof point === "object" &&
    point !== null &&
    "x" in point &&
    typeof point.x === "number" &&
    Number.isFinite(point.x) &&
    "y" in point &&
    typeof point.y === "number" &&
    Number.isFinite(point.y)
  )
    recordingCursor?.move(
      { x: point.x, y: point.y },
      "phase" in point && point.phase === "click" ? "click" : "move",
    );
});

type AnnotationTool = "select" | "marquee" | "draw" | "erase";

interface SelectedElement {
  id: string;
  element: Element;
  outline: HTMLDivElement;
  label: HTMLDivElement;
  baselineStyles: Map<string, string>;
}

interface AnnotationSession {
  teardown: (notifyMain: boolean) => void;
  applyTheme: (theme: DesktopPreviewAnnotationTheme) => void;
  setSendEnabled: (enabled: boolean) => void;
}

let activeSession: AnnotationSession | null = null;
let annotationTheme: DesktopPreviewAnnotationTheme | null = null;

const applyAnnotationTheme = (
  host: HTMLElement,
  theme: DesktopPreviewAnnotationTheme | null,
): void => {
  if (!theme) return;
  host.style.colorScheme = theme.colorScheme;
  const variables = {
    "--t3-radius": theme.radius,
    "--t3-background": theme.background,
    "--t3-foreground": theme.foreground,
    "--t3-popover": theme.popover,
    "--t3-popover-foreground": theme.popoverForeground,
    "--t3-primary": theme.primary,
    "--t3-primary-foreground": theme.primaryForeground,
    "--t3-muted": theme.muted,
    "--t3-muted-foreground": theme.mutedForeground,
    "--t3-accent": theme.accent,
    "--t3-accent-foreground": theme.accentForeground,
    "--t3-border": theme.border,
    "--t3-input": theme.input,
    "--t3-ring": theme.ring,
    "--t3-font-sans": theme.fontSans,
    "--t3-font-mono": theme.fontMono,
  };
  for (const [name, value] of Object.entries(variables)) {
    host.style.setProperty(name, value);
  }
};

const reportHumanPointerInput = (event: PointerEvent): void => {
  if (!event.isTrusted) return;
  ipcRenderer.send(HUMAN_INPUT_CHANNEL, {
    kind: "pointer",
    x: event.clientX,
    y: event.clientY,
    button: event.button,
  });
};

const reportHumanKeyInput = (event: KeyboardEvent): void => {
  if (!event.isTrusted) return;
  ipcRenderer.send(HUMAN_INPUT_CHANNEL, {
    kind: "key",
    key: event.key,
    code: event.code,
  });
};

window.addEventListener("pointerdown", reportHumanPointerInput, true);
window.addEventListener("keydown", reportHumanKeyInput, true);

const MOUSE_BUTTON_BACK = 3;
const MOUSE_BUTTON_FORWARD = 4;

const navigationDirectionForButton = (button: number): "back" | "forward" | null => {
  if (button === MOUSE_BUTTON_BACK) return "back";
  if (button === MOUSE_BUTTON_FORWARD) return "forward";
  return null;
};

// Chromium routes thumb-button navigation to the focused WebContents; navigate this hovered guest explicitly.
const suppressNavigationButton = (event: MouseEvent): void => {
  if (!event.isTrusted || navigationDirectionForButton(event.button) === null) return;
  event.preventDefault();
  event.stopImmediatePropagation();
};

const requestNavigationForButton = (event: MouseEvent): void => {
  if (!event.isTrusted) return;
  const direction = navigationDirectionForButton(event.button);
  if (direction === null) return;
  event.preventDefault();
  event.stopImmediatePropagation();
  ipcRenderer.send(MOUSE_NAVIGATE_CHANNEL, { direction });
};

window.addEventListener("mousedown", suppressNavigationButton, true);
window.addEventListener("mouseup", requestNavigationForButton, true);
window.addEventListener("auxclick", suppressNavigationButton, true);

const nextId = createAnnotationIdSource();

const rectFromDomRect = (rect: DOMRect): PreviewAnnotationRect => ({
  x: rect.left,
  y: rect.top,
  width: rect.width,
  height: rect.height,
});

const normalizeRect = (
  startX: number,
  startY: number,
  endX: number,
  endY: number,
): PreviewAnnotationRect => ({
  x: Math.min(startX, endX),
  y: Math.min(startY, endY),
  width: Math.abs(endX - startX),
  height: Math.abs(endY - startY),
});

const isUsableRect = (rect: PreviewAnnotationRect): boolean => rect.width >= 3 && rect.height >= 3;

function unionRects(
  rects: ReadonlyArray<PreviewAnnotationRect>,
  padding = 20,
): PreviewAnnotationRect | null {
  if (rects.length === 0) return null;
  const left = Math.min(...rects.map((rect) => rect.x));
  const top = Math.min(...rects.map((rect) => rect.y));
  const right = Math.max(...rects.map((rect) => rect.x + rect.width));
  const bottom = Math.max(...rects.map((rect) => rect.y + rect.height));
  // Main clips to the captured viewport; clipping here would lose offscreen bounds.
  return {
    x: left - padding,
    y: top - padding,
    width: right - left + padding * 2,
    height: bottom - top + padding * 2,
  };
}

function isAnnotationNode(element: Element): boolean {
  return element instanceof Element && element.closest(`[${OVERLAY_ATTRIBUTE}]`) !== null;
}

function pickFromPoint(clientX: number, clientY: number): Element | null {
  for (const candidate of document.elementsFromPoint(clientX, clientY)) {
    if (!(candidate instanceof Element)) continue;
    if (isAnnotationNode(candidate) || candidate.hasAttribute(INPUT_LAYER_ATTRIBUTE)) continue;
    if (candidate === document.documentElement || candidate === document.body) continue;
    return candidate;
  }
  return null;
}

function describeRawElement(element: Element): string {
  const tag = element.tagName.toLowerCase();
  const id = element.id ? `#${element.id}` : "";
  const classes =
    element instanceof HTMLElement && typeof element.className === "string"
      ? element.className
          .trim()
          .split(/\s+/)
          .filter(Boolean)
          .slice(0, 2)
          .map((name) => `.${name}`)
          .join("")
      : "";
  return `${tag}${id}${classes}`;
}

function createBox(color: string, fill: string): HTMLDivElement {
  const node = document.createElement("div");
  node.setAttribute(OVERLAY_ATTRIBUTE, "");
  node.style.cssText = [
    "position:fixed",
    "pointer-events:none",
    `border:2px solid ${color}`,
    `background:${fill}`,
    "border-radius:3px",
    "box-sizing:border-box",
    "display:none",
    `z-index:${CONTENT_LAYER_Z_INDEX}`,
  ].join(";");
  return node;
}

function positionBox(node: HTMLElement, rect: PreviewAnnotationRect): void {
  if (rect.width <= 0 || rect.height <= 0) {
    node.style.display = "none";
    return;
  }
  node.style.display = "block";
  node.style.transform = `translate(${rect.x}px, ${rect.y}px)`;
  node.style.width = `${rect.width}px`;
  node.style.height = `${rect.height}px`;
}

function visibleElementRect(element: Element): PreviewAnnotationRect {
  const rect = element.getBoundingClientRect();
  let left = Math.max(0, rect.left);
  let top = Math.max(0, rect.top);
  let right = Math.min(document.documentElement.clientWidth, rect.right);
  let bottom = Math.min(document.documentElement.clientHeight, rect.bottom);
  if (getComputedStyle(element).position !== "fixed") {
    for (let ancestor = element.parentElement; ancestor; ancestor = ancestor.parentElement) {
      if (ancestor === document.body || ancestor === document.documentElement) break;
      const style = getComputedStyle(ancestor);
      const bounds = ancestor.getBoundingClientRect();
      const clipLeft = bounds.left + ancestor.clientLeft;
      const clipTop = bounds.top + ancestor.clientTop;
      if (style.overflowX !== "visible") {
        left = Math.max(left, clipLeft);
        right = Math.min(right, clipLeft + ancestor.clientWidth);
      }
      if (style.overflowY !== "visible") {
        top = Math.max(top, clipTop);
        bottom = Math.min(bottom, clipTop + ancestor.clientHeight);
      }
    }
  }
  return { x: left, y: top, width: Math.max(0, right - left), height: Math.max(0, bottom - top) };
}

function createLabel(): HTMLDivElement {
  const label = document.createElement("div");
  label.setAttribute(OVERLAY_ATTRIBUTE, "");
  label.className =
    "fixed z-1 max-w-70 overflow-hidden rounded-md bg-primary px-2 py-1 font-sans text-xs font-semibold text-primary-foreground shadow-md";
  label.style.cssText = [
    "position:fixed",
    "pointer-events:none",
    "white-space:nowrap",
    "text-overflow:ellipsis",
    `z-index:${CONTENT_LAYER_Z_INDEX}`,
  ].join(";");
  return label;
}

function updateSelectedVisual(target: SelectedElement): void {
  if (!target.element.isConnected) {
    target.outline.style.display = "none";
    target.label.style.display = "none";
    return;
  }
  const rect = visibleElementRect(target.element);
  positionBox(target.outline, rect);
  if (rect.width === 0 || rect.height === 0) {
    target.label.style.display = "none";
    return;
  }
  target.label.textContent = describeRawElement(target.element);
  target.label.style.display = "block";
  target.label.style.transform = `translate(${Math.max(4, rect.x)}px, ${Math.max(4, rect.y - 22)}px)`;
}

function toStackFrame(frame: {
  functionName?: string;
  fileName?: string;
  lineNumber?: number;
  columnNumber?: number;
}): PickedElementStackFrame {
  return {
    functionName: frame.functionName ?? null,
    fileName: frame.fileName ?? null,
    lineNumber: frame.lineNumber ?? null,
    columnNumber: frame.columnNumber ?? null,
  };
}

/** React-internals lookups can remain pending forever; bound them so submit cannot stall the overlay. */
function withCaptureTimeout<A>(promise: Promise<A>, millis: number): Promise<A | null> {
  let timer: ReturnType<typeof setTimeout> | undefined;
  return Promise.race([
    promise,
    new Promise<null>((resolve) => {
      timer = setTimeout(() => resolve(null), millis);
    }),
  ]).finally(() => clearTimeout(timer));
}

const HTML_PREVIEW_MAX_CHARS = 500;

/** Preserve the tag, HTML, and crop rect when React context lookup fails or stalls. */
async function captureElement(element: Element): Promise<PickedElementPayload> {
  const base = {
    pageUrl: location.href,
    pageTitle: document.title?.trim() || null,
    tagName: element.tagName.toLowerCase(),
    pickedAt: new Date().toISOString(),
  };
  try {
    const context = await withCaptureTimeout(
      Promise.resolve(getElementContext(element)),
      ELEMENT_CONTEXT_TIMEOUT_MS,
    );
    if (context) {
      const stack = (context.stack ?? []).map(toStackFrame);
      return {
        ...base,
        selector: context.selector,
        htmlPreview: context.htmlPreview ?? "",
        componentName: context.componentName,
        source: stack[0] ?? null,
        stack,
        styles: context.styles ?? "",
      };
    }
  } catch {}
  return {
    ...base,
    selector: null,
    htmlPreview: element.outerHTML.slice(0, HTML_PREVIEW_MAX_CHARS),
    componentName: null,
    source: null,
    stack: [],
    styles: "",
  };
}

function createButton(label: string, title: string): HTMLButtonElement {
  const button = document.createElement("button");
  button.type = "button";
  button.textContent = label;
  button.title = title;
  button.className =
    "inline-flex h-7 cursor-pointer items-center justify-center rounded-md border border-transparent px-2 font-sans text-xs font-medium text-foreground outline-none hover:bg-accent disabled:pointer-events-none disabled:opacity-60";
  return button;
}

function styleControl(input: HTMLInputElement | HTMLSelectElement): void {
  input.setAttribute("aria-label", input.getAttribute("aria-label") ?? "Style value");
  input.className =
    "h-7 min-w-0 w-full appearance-none rounded-md border border-input bg-background px-2 font-mono text-xs text-foreground shadow-xs outline-none";
}

function createUnitControl(input: HTMLInputElement): HTMLElement {
  const wrapper = document.createElement("div");
  wrapper.style.cssText = "position:relative;min-width:0";
  const unit = document.createElement("span");
  unit.textContent = input.dataset.unit ?? "";
  unit.className =
    "pointer-events-none absolute top-1/2 right-2 -translate-y-1/2 font-mono text-xs text-muted-foreground";
  wrapper.append(input, unit);
  return wrapper;
}

function createField(
  labelText: string,
  input: HTMLInputElement | HTMLSelectElement,
): HTMLLabelElement {
  const label = document.createElement("label");
  label.className =
    "grid min-h-7 grid-cols-[82px_minmax(0,1fr)] items-center gap-2 font-sans text-xs font-medium text-muted-foreground";
  const text = document.createElement("span");
  text.textContent = labelText;
  styleControl(input);
  label.append(
    text,
    input instanceof HTMLInputElement && input.dataset.unit ? createUnitControl(input) : input,
  );
  return label;
}

function createStyleSection(): HTMLElement {
  const section = document.createElement("section");
  section.className = "grid gap-1 border-t border-border py-2";
  return section;
}

function createUnitInput(unit: string, placeholder = "0"): HTMLInputElement {
  const input = document.createElement("input");
  input.type = "number";
  input.placeholder = placeholder;
  input.style.paddingRight = "30px";
  input.dataset.unit = unit;
  return input;
}

function pathFromPoints(points: ReadonlyArray<PreviewAnnotationPoint>): string {
  if (points.length === 0) return "";
  if (points.length === 1) return `M ${points[0]!.x} ${points[0]!.y} l 0.01 0.01`;
  let path = `M ${points[0]!.x} ${points[0]!.y}`;
  for (let index = 1; index < points.length - 1; index += 1) {
    const current = points[index]!;
    const next = points[index + 1]!;
    path += ` Q ${current.x} ${current.y} ${(current.x + next.x) / 2} ${(current.y + next.y) / 2}`;
  }
  const last = points[points.length - 1]!;
  path += ` L ${last.x} ${last.y}`;
  return path;
}

function strokeBounds(
  points: ReadonlyArray<PreviewAnnotationPoint>,
  width: number,
): PreviewAnnotationRect {
  const xs = points.map((point) => point.x);
  const ys = points.map((point) => point.y);
  const padding = width + 3;
  const left = Math.min(...xs) - padding;
  const top = Math.min(...ys) - padding;
  const right = Math.max(...xs) + padding;
  const bottom = Math.max(...ys) + padding;
  return { x: left, y: top, width: right - left, height: bottom - top };
}

function startAnnotation(
  sendEnabled: boolean,
  voice: DesktopPreviewAnnotationVoiceConfig | undefined,
): void {
  activeSession?.teardown(false);
  let finished = false;
  const host = document.createElement("div");
  host.setAttribute(OVERLAY_ATTRIBUTE, "");
  host.setAttribute("dir", "ltr");
  host.style.cssText = `position:fixed;inset:0;z-index:${Z_INDEX_OVERLAY};pointer-events:none`;
  applyAnnotationTheme(host, annotationTheme);
  const shadowRoot = host.attachShadow({ mode: "closed" });
  const themeStyle = document.createElement("style");
  themeStyle.textContent = previewAnnotationStyles;
  shadowRoot.appendChild(themeStyle);

  const root = document.createElement("div");
  root.setAttribute(OVERLAY_ATTRIBUTE, "");
  root.setAttribute("dir", "ltr");
  root.className = "fixed inset-0 font-sans text-foreground";
  root.style.cssText = "pointer-events:none";
  const cursorStyle = document.createElement("style");
  cursorStyle.setAttribute(OVERLAY_ATTRIBUTE, "");
  cursorStyle.textContent = `html[data-t3code-annotation-tool] body, html[data-t3code-annotation-tool] body * { cursor: crosshair !important; } [${OVERLAY_ATTRIBUTE}], [${OVERLAY_ATTRIBUTE}] * { cursor: default !important; } [${OVERLAY_ATTRIBUTE}] input[type=number]::-webkit-inner-spin-button, [${OVERLAY_ATTRIBUTE}] input[type=number]::-webkit-outer-spin-button { appearance:none; margin:0; }`;
  document.documentElement.appendChild(cursorStyle);
  shadowRoot.appendChild(root);

  const hoverOutline = createBox(PRIMARY, PRIMARY_FILL);
  const marqueeBox = createBox(PRIMARY, PRIMARY_FILL);
  root.append(hoverOutline, marqueeBox);

  const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  svg.setAttribute(OVERLAY_ATTRIBUTE, "");
  svg.setAttribute("width", "100%");
  svg.setAttribute("height", "100%");
  svg.setAttribute("viewBox", `0 0 ${window.innerWidth} ${window.innerHeight}`);
  svg.style.cssText = "position:fixed;inset:0;overflow:visible;pointer-events:none";
  svg.style.zIndex = String(CONTENT_LAYER_Z_INDEX);
  root.appendChild(svg);

  const toolbar = document.createElement("div");
  toolbar.setAttribute(OVERLAY_ATTRIBUTE, "");
  toolbar.className =
    "pointer-events-auto fixed top-2.5 left-1/2 flex -translate-x-1/2 gap-0.5 rounded-lg border border-border bg-popover/95 p-1 text-popover-foreground shadow-lg backdrop-blur-xl";
  toolbar.style.zIndex = String(CHROME_LAYER_Z_INDEX);
  root.appendChild(toolbar);

  const editor = document.createElement("div");
  editor.setAttribute(OVERLAY_ATTRIBUTE, "");
  editor.className =
    "pointer-events-auto fixed hidden max-h-[calc(100vh-16px)] w-[min(360px,calc(100vw-16px))] flex-col overflow-hidden rounded-xl border border-border bg-popover/96 text-popover-foreground shadow-2xl backdrop-blur-xl";
  editor.style.zIndex = String(CHROME_LAYER_Z_INDEX);
  root.appendChild(editor);

  const composerRow = document.createElement("div");
  composerRow.className = "flex items-start gap-2 p-2";

  const adjust = createButton("", "Expand annotation editor");
  adjust.setAttribute("aria-label", "Expand annotation editor");
  adjust.setAttribute("aria-expanded", "false");
  adjust.className +=
    " h-8 w-8 shrink-0 bg-muted p-0 text-muted-foreground hover:bg-accent hover:text-accent-foreground";
  adjust.innerHTML =
    '<svg viewBox="0 0 20 20" width="15" height="15" aria-hidden="true"><path d="M4 5h12M4 10h12M4 15h12M7 3v4M13 8v4M9 13v4" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg>';
  composerRow.appendChild(adjust);

  const comment = document.createElement("textarea");
  comment.placeholder = "Describe the change…";
  comment.setAttribute("dir", "auto");
  comment.rows = 1;
  comment.className =
    "min-h-8 max-h-24 min-w-0 flex-1 resize-none overflow-y-hidden border-0 border-b border-b-transparent bg-transparent px-0 py-1.5 font-sans text-sm leading-5 text-foreground outline-none ring-0 placeholder:text-muted-foreground focus:border-b-primary focus:outline-none focus:ring-0";
  composerRow.appendChild(comment);

  // Recording happens in the application renderer, which owns the microphone consent.
  // This guest only draws the controls, forwards trusted gestures, and mirrors state.
  let voicePhase: DesktopPreviewAnnotationVoiceState["phase"] = "idle";
  let voiceSettings = false;
  let voiceBusy = false;
  let voiceAvailable = false;
  let voiceKeyCode: string | null = null;
  let pendingVoiceSubmission: PreviewAnnotationSubmission | null = null;
  let voiceSubmissionReady = false;
  const voiceButton = createButton("", "Start voice input");
  voiceButton.innerHTML =
    '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 19v3"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><rect x="9" y="2" width="6" height="13" rx="3"/></svg>';
  voiceButton.className += " h-8 w-8 shrink-0 p-0 text-muted-foreground";
  voiceButton.hidden = true;
  voiceButton.setAttribute("aria-label", "Start voice input");
  composerRow.appendChild(voiceButton);
  const voiceRow = document.createElement("div");
  voiceRow.className = "flex min-w-0 items-center justify-end gap-1 px-2 pb-2";
  voiceRow.hidden = true;
  const voicePill = document.createElement("div");
  const voiceCancel = createButton("", "Cancel voice input");
  voiceCancel.innerHTML =
    '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="m18 6-12 12M6 6l12 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>';
  voiceCancel.className += " h-7 w-7 shrink-0 rounded-full p-0";
  const voiceFinish = createButton("", "Finish voice input");
  voiceFinish.className +=
    " h-7 w-7 shrink-0 rounded-full border-primary bg-primary p-0 text-primary-foreground hover:bg-primary/90";
  const voiceSkip = createButton("Skip", "Skip transcript cleanup");
  voiceSkip.className += " h-7 shrink-0 rounded-full px-2.5";
  // createButton's rounded-md sorts after rounded-full in the generated CSS, so it would win.
  for (const button of [voiceCancel, voiceFinish, voiceSkip]) button.classList.remove("rounded-md");
  const voiceStatus = document.createElement("div");
  voiceStatus.className = "flex h-7 min-w-0 flex-1 items-center gap-2";
  voiceStatus.setAttribute("role", "status");
  const voiceStatusText = document.createElement("span");
  voiceStatusText.className = "min-w-0 flex-1 truncate text-right text-sm text-muted-foreground";
  const waveform = document.createElement("div");
  waveform.className =
    "flex h-5 min-w-0 flex-1 items-center justify-between gap-px overflow-hidden";
  waveform.setAttribute("aria-hidden", "true");
  const waveformLevels = Array<number>(24).fill(0);
  const waveformBars = waveformLevels.map(() => {
    const bar = document.createElement("span");
    bar.className = "h-full w-0.5 shrink-0 origin-center rounded-full bg-primary";
    bar.style.transform = "scaleY(0.08)";
    bar.style.opacity = "0.25";
    waveform.appendChild(bar);
    return bar;
  });
  const voiceElapsed = document.createElement("span");
  voiceElapsed.className = "shrink-0 text-xs tabular-nums text-muted-foreground";
  let recordingStartedAt = 0;
  let elapsedTimer: ReturnType<typeof setInterval> | null = null;
  const updateElapsed = () => {
    const seconds = Math.floor((Date.now() - recordingStartedAt) / 1_000);
    voiceElapsed.textContent = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
  };
  voiceStatus.append(voiceStatusText, waveform, voiceElapsed);
  voicePill.append(voiceCancel, voiceStatus, voiceSkip, voiceFinish);
  voiceRow.appendChild(voicePill);
  const voicePreview = document.createElement("p");
  voicePreview.className = "px-2 pb-2 text-sm text-muted-foreground";
  voicePreview.setAttribute("role", "status");
  voicePreview.hidden = true;
  const sendVoice = (
    action: DesktopPreviewAnnotationVoiceEvent["action"],
    keyboard?: KeyboardEvent,
  ) => {
    if (!voice || finished) return;
    const event: DesktopPreviewAnnotationVoiceEvent = {
      sessionId: voice.sessionId,
      text: comment.value,
      cursor: comment.selectionStart,
      selectionEnd: comment.selectionEnd,
      action,
      ...(keyboard
        ? {
            keyboard: {
              type: keyboard.type === "keyup" ? ("keyup" as const) : ("keydown" as const),
              key: keyboard.key,
              code: keyboard.code,
              metaKey: keyboard.metaKey,
              ctrlKey: keyboard.ctrlKey,
              shiftKey: keyboard.shiftKey,
              altKey: keyboard.altKey,
              repeat: keyboard.repeat,
            },
          }
        : {}),
    };
    ipcRenderer.send(ANNOTATION_VOICE_EVENT_CHANNEL, event);
  };
  const isDictationChord = (event: KeyboardEvent) =>
    voice !== undefined &&
    voice.shortcuts.some((shortcut) =>
      matchesKeybindingShortcut(event, shortcut, navigator.platform),
    );
  for (const button of [voiceButton, voiceFinish, voiceCancel, voiceSkip]) {
    button.addEventListener("pointerdown", (event) => event.preventDefault());
  }
  // Page scripts cannot reach the closed shadow root, and a synthetic click never counts.
  voiceButton.addEventListener("click", (event) => {
    if (!event.isTrusted) return;
    if (voiceSettings) {
      sendVoice("settings");
      return;
    }
    if (!voiceAvailable || (voiceBusy && voicePhase !== "recording")) return;
    sendVoice(voicePhase === "recording" ? "stop" : "start");
  });
  voiceFinish.addEventListener("click", (event) => {
    if (event.isTrusted) sendVoice("stop");
  });
  voiceCancel.addEventListener("click", (event) => {
    if (!event.isTrusted) return;
    pendingVoiceSubmission = null;
    sendVoice("cancel");
  });
  voiceSkip.addEventListener("click", (event) => {
    if (event.isTrusted) sendVoice("skip");
  });
  const onVoiceState = (
    _event: Electron.IpcRendererEvent,
    state: DesktopPreviewAnnotationVoiceState,
  ) => {
    if (finished || state.sessionId !== voice?.sessionId) return;
    const phaseChanged = voicePhase !== state.phase;
    const submissionChanged = voiceBusy !== state.blocksSubmission;
    const previewChanged = voicePreview.textContent !== (state.preview ?? "");
    voicePhase = state.phase;
    voiceSettings = state.errorAction === "settings";
    voiceBusy = state.blocksSubmission;
    voiceAvailable = state.available;
    comment.readOnly = state.freezesEditor;
    if (state.draft) {
      voiceSubmissionReady = pendingVoiceSubmission !== null;
      comment.value = state.draft.text;
      comment.setSelectionRange(state.draft.cursor, state.draft.cursor);
      resizeComment();
    }
    const recording = state.phase === "recording";
    const active = state.phase !== "idle" && state.phase !== "error";
    const wasActive = voicePill.parentElement === composerRow;
    voiceButton.hidden = !state.available || active;
    voiceButton.disabled = voiceBusy;
    const label = voiceSettings ? "Open voice settings" : "Start voice input";
    voiceButton.title =
      !voiceSettings && state.shortcutLabel ? `${label} (${state.shortcutLabel})` : label;
    voiceButton.setAttribute("aria-label", label);
    // An active dictation takes the textarea's place; it takes focus once (sr-only) so Enter still attaches.
    voiceRow.hidden = state.phase === "idle" || active;
    comment.classList.toggle("sr-only", active);
    if (active && !wasActive) comment.focus({ preventScroll: true });
    submit.hidden = active;
    // Re-inserting on every level update would blur a focused Finish or Cancel.
    if (active && voicePill.parentElement !== composerRow) comment.after(voicePill);
    else if (!active && voicePill.parentElement !== voiceRow) voiceRow.appendChild(voicePill);
    voicePill.className = active
      ? "flex h-8 min-w-0 flex-1 items-center gap-2"
      : "flex min-w-0 items-center gap-1";
    // A draft that arrived while the textarea was sr-only was measured at 1px wide.
    if (wasActive && !active) resizeComment();
    else if (phaseChanged) queueEditorLayout();
    voiceCancel.title =
      state.phase === "error" ? "Dismiss voice input error" : "Cancel voice input";
    voiceCancel.setAttribute("aria-label", voiceCancel.title);
    voiceStatus.setAttribute("aria-label", state.status ?? "Voice input");
    voiceStatus.setAttribute("aria-live", recording ? "off" : "polite");
    voiceStatusText.textContent = state.status;
    voiceStatusText.style.color = state.phase === "error" ? "var(--t3-destructive)" : "";
    voiceStatusText.hidden = recording;
    waveform.hidden = !recording;
    voiceElapsed.hidden = !recording;
    if (recording && elapsedTimer === null) {
      recordingStartedAt = Date.now();
      waveformLevels.fill(0);
      updateElapsed();
      elapsedTimer = setInterval(updateElapsed, 250);
    } else if (!recording && elapsedTimer !== null) {
      clearInterval(elapsedTimer);
      elapsedTimer = null;
    }
    if (recording) {
      waveformLevels.copyWithin(0, 1);
      waveformLevels[waveformLevels.length - 1] = state.level;
      const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
      waveformBars.forEach((bar, index) => {
        const level = waveformLevels[index] ?? 0;
        bar.style.opacity = String(0.25 + level * 0.75);
        bar.style.transform = `scaleY(${reducedMotion ? 0.35 : Math.max(0.08, level)})`;
      });
    }
    voiceSkip.hidden = state.phase !== "post-processing";
    voiceFinish.hidden = !active || state.phase === "post-processing";
    voiceFinish.disabled = !recording;
    voiceFinish.title = recording ? "Finish voice input" : (state.status ?? "Voice input is busy");
    if (recording && state.shortcutLabel) voiceFinish.title += ` (${state.shortcutLabel})`;
    voiceFinish.setAttribute("aria-label", voiceFinish.title);
    if (phaseChanged)
      voiceFinish.innerHTML = recording
        ? '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="m5 12 4 4L19 6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>'
        : '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="M12 2a10 10 0 1 0 10 10" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>';
    if (!active) voicePill.append(voiceStatus, voiceCancel);
    else if (phaseChanged) voicePill.append(voiceCancel, voiceStatus, voiceSkip, voiceFinish);
    voicePreview.textContent = state.preview;
    voicePreview.hidden = !state.preview;
    if (phaseChanged || submissionChanged || previewChanged || state.draft) updateStatus();
    if (pendingVoiceSubmission !== null) {
      if (state.phase === "recording") sendVoice("stop");
      if (state.phase === "error") pendingVoiceSubmission = null;
      if (state.phase === "idle") {
        const submission = pendingVoiceSubmission;
        pendingVoiceSubmission = null;
        if (voiceSubmissionReady && submission !== null) submitAnnotation(submission);
        voiceSubmissionReady = false;
      }
    }
  };

  const dragHandle = document.createElement("button");
  dragHandle.type = "button";
  dragHandle.textContent = "⠿";
  dragHandle.title = "Drag annotation editor";
  dragHandle.className =
    "hidden h-8 w-6 shrink-0 cursor-grab select-none border-0 bg-transparent p-0 font-sans text-lg font-bold leading-5 text-muted-foreground";
  composerRow.appendChild(dragHandle);

  const submit = createButton("Attach", "Attach annotation and screenshot (Enter)");
  const updateSendHint = () => {
    submit.title = sendEnabled
      ? "Attach annotation and screenshot (Enter). Send with Cmd/Ctrl+Enter."
      : "Attach annotation and screenshot (Enter)";
  };
  updateSendHint();
  submit.className +=
    " h-8 shrink-0 border-primary bg-primary px-3 text-primary-foreground shadow-sm hover:bg-primary/90";
  composerRow.appendChild(submit);
  editor.append(composerRow, voicePreview, voiceRow);

  const stylePanel = document.createElement("div");
  stylePanel.className =
    "hidden max-h-[min(176px,calc(100vh-180px))] overflow-auto border-t border-border bg-muted/40 px-3";
  editor.appendChild(stylePanel);

  const selected = new Map<Element, SelectedElement>();
  const regions: PreviewAnnotationRegionTarget[] = [];
  const strokes: PreviewAnnotationStrokeTarget[] = [];
  const styleChanges = new Map<string, PreviewAnnotationStyleChange>();
  const toolButtons = new Map<AnnotationTool, HTMLButtonElement>();
  // Chromium draws a PDF in its own viewer frame, so pointer and key input over it
  // never reaches this document. A transparent layer above the viewer catches the
  // pointer and takes focus for Escape and the tool keys. A PDF has no elements to
  // select, so it starts on Region.
  const isPdf = document.contentType === "application/pdf";
  const inputLayer = isPdf ? document.createElement("div") : null;
  if (inputLayer) {
    inputLayer.setAttribute(INPUT_LAYER_ATTRIBUTE, "");
    inputLayer.tabIndex = -1;
    inputLayer.style.cssText = `position:fixed;inset:0;z-index:${Z_INDEX_OVERLAY - 1};cursor:crosshair;outline:none`;
  }
  let tool: AnnotationTool = isPdf ? "marquee" : "select";
  let dragStart: PreviewAnnotationPoint | null = null;
  let activeStroke: { target: PreviewAnnotationStrokeTarget; path: SVGPathElement } | null = null;
  let pendingCapture = false;
  let editorExpanded = false;
  let editorWasShown = false;
  let editorPosition: { left: number; top: number } | null = null;
  let editorDrag: { pointerId: number; offsetX: number; offsetY: number } | null = null;
  let editorLayoutFrame: number | null = null;

  const resizeComment = (): void => {
    const maxHeight = 96;
    comment.style.height = "auto";
    const nextHeight = Math.min(comment.scrollHeight, maxHeight);
    comment.style.height = `${nextHeight}px`;
    comment.style.overflowY = comment.scrollHeight > maxHeight ? "auto" : "hidden";
    queueEditorLayout();
  };
  comment.addEventListener("input", () => {
    resizeComment();
    sendVoice("sync");
  });

  const updateStatus = (): void => {
    const hasTargets = selected.size > 0 || regions.length > 0 || strokes.length > 0;
    if (!hasTargets && voiceBusy) {
      pendingVoiceSubmission = null;
      sendVoice("cancel");
    }
    editor.style.display = hasTargets ? "flex" : "none";
    submit.disabled = !hasTargets || voicePhase === "preparing" || pendingCapture;
    submit.style.opacity = hasTargets ? "1" : "0.45";
    adjust.disabled = !hasTargets;
    stylePanel.style.display = editorExpanded && selected.size > 0 ? "grid" : "none";
    queueEditorLayout();
    if (hasTargets && !editorWasShown) {
      editorWasShown = true;
      window.setTimeout(() => comment.focus({ preventScroll: true }), 0);
    }
  };

  const refreshToolButtons = (): void => {
    for (const [candidate, button] of toolButtons) {
      const active = candidate === tool;
      button.classList.toggle("bg-primary/10", active);
      button.classList.toggle("text-primary", active);
      button.classList.toggle("text-foreground", !active);
    }
    if (tool !== "select") hoverOutline.style.display = "none";
    if (tool !== "marquee") marqueeBox.style.display = "none";
    document.documentElement.setAttribute("data-t3code-annotation-tool", tool);
  };

  const removeSelected = (target: SelectedElement): void => {
    if (target.element instanceof HTMLElement || target.element instanceof SVGElement) {
      for (const [property, baseline] of target.baselineStyles) {
        if (baseline) target.element.style.setProperty(property, baseline);
        else target.element.style.removeProperty(property);
      }
    }
    selected.delete(target.element);
    target.outline.remove();
    target.label.remove();
    for (const [key, change] of styleChanges) {
      if (change.targetId === target.id) styleChanges.delete(key);
    }
  };

  const addSelected = (element: Element): void => {
    if (selected.has(element)) return;
    const target: SelectedElement = {
      id: nextId("element"),
      element,
      outline: createBox(PRIMARY, PRIMARY_FILL),
      label: createLabel(),
      baselineStyles: new Map(),
    };
    selected.set(element, target);
    root.append(target.outline, target.label);
    updateSelectedVisual(target);
    if (editorExpanded) {
      stylePanel.style.display = "grid";
      syncStyleControls();
    }
  };

  const toggleSelected = (element: Element, additive: boolean): void => {
    const existing = selected.get(element);
    if (existing) {
      removeSelected(existing);
    } else {
      if (!additive) {
        for (const target of Array.from(selected.values())) removeSelected(target);
      }
      addSelected(element);
    }
    updateStatus();
  };

  const setStyleForSelected = (property: string, value: string): void => {
    for (const target of selected.values()) {
      if (!(target.element instanceof HTMLElement || target.element instanceof SVGElement))
        continue;
      if (!target.baselineStyles.has(property)) {
        target.baselineStyles.set(property, target.element.style.getPropertyValue(property));
      }
      const key = `${target.id}:${property}`;
      const previousValue =
        styleChanges.get(key)?.previousValue ??
        getComputedStyle(target.element).getPropertyValue(property).trim();
      target.element.style.setProperty(property, value, "important");
      styleChanges.set(key, {
        targetId: target.id,
        selector: null,
        property,
        previousValue,
        value,
      });
      updateSelectedVisual(target);
    }
  };

  const textSection = createStyleSection();
  const colorsSection = createStyleSection();
  const bordersSection = createStyleSection();
  const sizingSection = createStyleSection();
  stylePanel.append(textSection, colorsSection, bordersSection, sizingSection);

  const fontFamily = document.createElement("select");
  for (const value of ["inherit", "system-ui", "sans-serif", "serif", "monospace"]) {
    const option = document.createElement("option");
    option.value = value;
    option.textContent = value;
    fontFamily.appendChild(option);
  }
  fontFamily.addEventListener("change", () => setStyleForSelected("font-family", fontFamily.value));
  textSection.appendChild(createField("Font", fontFamily));

  const fontSize = createUnitInput("px", "16");
  fontSize.min = "1";
  fontSize.max = "300";
  fontSize.addEventListener("input", () => {
    if (fontSize.value) setStyleForSelected("font-size", `${fontSize.value}px`);
  });
  textSection.appendChild(createField("Font size", fontSize));

  const fontWeight = document.createElement("select");
  for (const value of ["300", "400", "500", "600", "700", "800", "900"]) {
    const option = document.createElement("option");
    option.value = value;
    option.textContent = value;
    fontWeight.appendChild(option);
  }
  fontWeight.addEventListener("change", () => setStyleForSelected("font-weight", fontWeight.value));
  textSection.appendChild(createField("Font weight", fontWeight));

  const lineHeight = document.createElement("input");
  lineHeight.type = "text";
  lineHeight.placeholder = "normal / 1.4";
  lineHeight.addEventListener("change", () => {
    if (lineHeight.value.trim()) setStyleForSelected("line-height", lineHeight.value.trim());
  });
  textSection.appendChild(createField("Line height", lineHeight));

  const createColorRow = (
    labelText: string,
    property: string,
    section: HTMLElement,
  ): { row: HTMLLabelElement; color: HTMLInputElement; text: HTMLInputElement } => {
    const row = document.createElement("label");
    row.className =
      "grid min-h-7 grid-cols-[82px_minmax(0,1fr)] items-center gap-2 font-sans text-xs font-medium text-muted-foreground";
    const label = document.createElement("span");
    label.textContent = labelText;
    const control = document.createElement("div");
    control.className =
      "grid h-7 grid-cols-[22px_minmax(0,1fr)] items-center gap-1 rounded-md border border-input bg-background px-1 shadow-xs";
    const color = document.createElement("input");
    color.type = "color";
    color.setAttribute("aria-label", labelText);
    color.style.cssText =
      "width:20px;height:20px;padding:0;border:0;border-radius:5px;overflow:hidden;background:transparent;cursor:pointer";
    const text = document.createElement("input");
    text.type = "text";
    text.setAttribute("aria-label", `${labelText} value`);
    text.className =
      "min-w-0 w-full border-0 bg-transparent font-mono text-xs text-foreground outline-none";
    color.addEventListener("input", () => {
      text.value = color.value;
      setStyleForSelected(property, color.value);
    });
    text.addEventListener("change", () => {
      const value = text.value.trim();
      if (!value) return;
      setStyleForSelected(property, value);
      if (/^#[0-9a-f]{6}$/i.test(value)) color.value = value;
    });
    control.append(color, text);
    row.append(label, control);
    section.appendChild(row);
    return { row, color, text };
  };

  const textColor = createColorRow("Text color", "color", colorsSection);
  const backgroundColor = createColorRow("Background", "background-color", colorsSection);

  const opacity = document.createElement("input");
  opacity.type = "range";
  opacity.min = "0";
  opacity.max = "1";
  opacity.step = "0.05";
  opacity.value = "1";
  opacity.style.accentColor = PRIMARY;
  opacity.addEventListener("input", () => setStyleForSelected("opacity", opacity.value));
  colorsSection.appendChild(createField("Opacity", opacity));

  const radius = createUnitInput("px", "0");
  radius.min = "0";
  radius.max = "300";
  radius.addEventListener("input", () => {
    if (radius.value) setStyleForSelected("border-radius", `${radius.value}px`);
  });
  bordersSection.appendChild(createField("Radius", radius));

  const borderColor = createColorRow("Border color", "border-color", bordersSection);

  const borderWidth = createUnitInput("px", "0");
  borderWidth.min = "0";
  borderWidth.max = "100";
  borderWidth.addEventListener("input", () => {
    if (borderWidth.value) {
      setStyleForSelected("border-style", "solid");
      setStyleForSelected("border-width", `${borderWidth.value}px`);
    }
  });
  bordersSection.appendChild(createField("Border width", borderWidth));

  const dimensions = document.createElement("div");
  dimensions.style.cssText =
    "display:grid;grid-template-columns:82px minmax(0,1fr);gap:8px;align-items:center";
  const dimensionLabel = document.createElement("div");
  dimensionLabel.className = "grid gap-2 font-sans text-xs font-medium text-muted-foreground";
  dimensionLabel.innerHTML = "<span>Width</span><span>Height</span>";
  const dimensionControls = document.createElement("div");
  dimensionControls.style.cssText = "position:relative;display:grid;gap:3px;padding-left:22px";
  const widthInput = createUnitInput("px", "auto");
  const heightInput = createUnitInput("px", "auto");
  styleControl(widthInput);
  styleControl(heightInput);
  const aspectLock = createButton("", "Lock aspect ratio");
  aspectLock.setAttribute("aria-pressed", "true");
  aspectLock.style.cssText +=
    ";position:absolute;left:0;top:50%;transform:translateY(-50%);width:18px;height:38px;padding:0";
  aspectLock.className += " bg-primary/10 text-primary";
  dimensionControls.append(
    createUnitControl(widthInput),
    createUnitControl(heightInput),
    aspectLock,
  );
  dimensions.append(dimensionLabel, dimensionControls);
  sizingSection.appendChild(dimensions);

  let aspectLocked = true;
  let aspectRatio = 1;
  const refreshAspectButton = (): void => {
    aspectLock.innerHTML = aspectLocked
      ? '<svg viewBox="0 0 20 20" width="14" height="14" aria-hidden="true"><path d="M8 6.5 9.5 5A3.5 3.5 0 0 1 14.5 10l-1.5 1.5M12 13.5 10.5 15A3.5 3.5 0 0 1 5.5 10L7 8.5M7.5 12.5l5-5" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>'
      : '<svg viewBox="0 0 20 20" width="14" height="14" aria-hidden="true"><path d="m6 6 8 8M8 6.5 9.5 5A3.5 3.5 0 0 1 14 9M12 13.5 10.5 15A3.5 3.5 0 0 1 6 11" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>';
    aspectLock.setAttribute("aria-pressed", String(aspectLocked));
    aspectLock.classList.toggle("bg-primary/10", aspectLocked);
    aspectLock.classList.toggle("text-primary", aspectLocked);
    aspectLock.classList.toggle("bg-muted", !aspectLocked);
    aspectLock.classList.toggle("text-muted-foreground", !aspectLocked);
  };
  aspectLock.addEventListener("click", () => {
    aspectLocked = !aspectLocked;
    refreshAspectButton();
  });
  widthInput.addEventListener("input", () => {
    const width = Number(widthInput.value);
    if (!Number.isFinite(width) || width <= 0) return;
    setStyleForSelected("width", `${width}px`);
    if (aspectLocked && aspectRatio > 0) {
      const height = Math.max(1, Math.round(width / aspectRatio));
      heightInput.value = String(height);
      setStyleForSelected("height", `${height}px`);
    }
  });
  heightInput.addEventListener("input", () => {
    const height = Number(heightInput.value);
    if (!Number.isFinite(height) || height <= 0) return;
    setStyleForSelected("height", `${height}px`);
    if (aspectLocked && aspectRatio > 0) {
      const width = Math.max(1, Math.round(height * aspectRatio));
      widthInput.value = String(width);
      setStyleForSelected("width", `${width}px`);
    }
  });
  refreshAspectButton();

  const addSpacingField = (
    label: string,
    property: string,
    placeholder: string,
  ): HTMLInputElement => {
    const input = document.createElement("input");
    input.type = "text";
    input.placeholder = placeholder;
    input.addEventListener("change", () => {
      if (input.value.trim()) setStyleForSelected(property, input.value.trim());
    });
    sizingSection.appendChild(createField(label, input));
    return input;
  };
  const padding = addSpacingField("Padding", "padding", "0 0 0 0");
  const margin = addSpacingField("Margin", "margin", "0 0 0 0");
  const gap = addSpacingField("Gap", "gap", "0px");

  const syncStyleControls = (): void => {
    const first = selected.values().next().value as SelectedElement | undefined;
    if (!first) return;
    const computed = getComputedStyle(first.element);
    const rect = first.element.getBoundingClientRect();
    aspectRatio = rect.height > 0 ? rect.width / rect.height : 1;
    widthInput.value = String(Math.round(rect.width));
    heightInput.value = String(Math.round(rect.height));
    fontSize.value = String(Math.round(Number.parseFloat(computed.fontSize) || 16));
    fontWeight.value = computed.fontWeight.match(/^[0-9]+$/) ? computed.fontWeight : "400";
    lineHeight.value = computed.lineHeight;
    fontFamily.value = Array.from(fontFamily.options).some(
      (option) => option.value === computed.fontFamily,
    )
      ? computed.fontFamily
      : "inherit";
    textColor.text.value = computed.color;
    backgroundColor.text.value = computed.backgroundColor;
    borderColor.text.value = computed.borderColor;
    opacity.value = computed.opacity;
    radius.value = String(Math.round(Number.parseFloat(computed.borderRadius) || 0));
    borderWidth.value = String(Math.round(Number.parseFloat(computed.borderWidth) || 0));
    padding.value = computed.padding;
    margin.value = computed.margin;
    gap.value = computed.gap === "normal" ? "0px" : computed.gap;
  };

  const tools: ReadonlyArray<[AnnotationTool, string, string]> = [
    ["select", "Select", "Select elements (V)"],
    ["marquee", "Region", "Draw a region or marquee-select elements (R)"],
    ["draw", "Draw", "Draw freehand (D)"],
    ["erase", "Erase", "Remove an annotation target (E)"],
  ];
  for (const [candidate, label, title] of tools) {
    const button = createButton(label, title);
    button.className += " h-8 px-2.5 text-sm";
    button.addEventListener("click", () => {
      tool = candidate;
      refreshToolButtons();
    });
    toolButtons.set(candidate, button);
    toolbar.appendChild(button);
  }

  const clampEditorPosition = (left: number, top: number): { left: number; top: number } => {
    const margin = 8;
    const rect = editor.getBoundingClientRect();
    return {
      left: Math.min(
        Math.max(margin, left),
        Math.max(margin, window.innerWidth - rect.width - margin),
      ),
      top: Math.min(
        Math.max(margin, top),
        Math.max(margin, window.innerHeight - rect.height - margin),
      ),
    };
  };

  const applyEditorPosition = (position: { left: number; top: number }): void => {
    const clamped = clampEditorPosition(position.left, position.top);
    editor.style.left = `${clamped.left}px`;
    editor.style.top = `${clamped.top}px`;
    editor.style.right = "auto";
    editor.style.bottom = "auto";
    if (editorExpanded) editorPosition = clamped;
  };

  const getAnnotationBounds = (): PreviewAnnotationRect | null =>
    unionRects(
      [
        ...Array.from(selected.values(), (target) => visibleElementRect(target.element)).filter(
          (rect) => rect.width > 0 && rect.height > 0,
        ),
        ...regions.map((region) => region.rect),
        ...strokes.map((stroke) => stroke.bounds),
      ],
      0,
    );

  const positionCompactEditor = (): void => {
    const bounds = getAnnotationBounds();
    if (!bounds) return;
    const editorRect = editor.getBoundingClientRect();
    const gap = 8;
    const candidates = [
      { left: bounds.x + bounds.width + gap, top: bounds.y },
      { left: bounds.x - editorRect.width - gap, top: bounds.y },
      {
        left: bounds.x + bounds.width - editorRect.width,
        top: bounds.y + bounds.height + gap,
      },
      {
        left: bounds.x + bounds.width - editorRect.width,
        top: bounds.y - editorRect.height - gap,
      },
    ];
    const overflow = (position: { left: number; top: number }): number =>
      Math.max(0, -position.left) +
      Math.max(0, -position.top) +
      Math.max(0, position.left + editorRect.width - window.innerWidth) +
      Math.max(0, position.top + editorRect.height - window.innerHeight);
    const best = candidates.reduce((current, candidate) =>
      overflow(candidate) < overflow(current) ? candidate : current,
    );
    applyEditorPosition(best);
  };

  function queueEditorLayout(): void {
    if (editorLayoutFrame !== null) window.cancelAnimationFrame(editorLayoutFrame);
    editorLayoutFrame = window.requestAnimationFrame(() => {
      editorLayoutFrame = null;
      if (editor.style.display === "none") return;
      if (editorExpanded && editorPosition) applyEditorPosition(editorPosition);
      else positionCompactEditor();
    });
  }

  adjust.addEventListener("click", () => {
    if (selected.size === 0) return;
    if (!editorExpanded) {
      const rect = editor.getBoundingClientRect();
      editorExpanded = true;
      editorPosition = { left: rect.left, top: rect.top };
      stylePanel.style.display = selected.size > 0 ? "grid" : "none";
      dragHandle.style.display = "block";
      adjust.setAttribute("aria-expanded", "true");
      adjust.title = "Collapse annotation editor";
      adjust.setAttribute("aria-label", "Collapse annotation editor");
      if (selected.size > 0) syncStyleControls();
    } else {
      editorExpanded = false;
      editorPosition = null;
      stylePanel.style.display = "none";
      dragHandle.style.display = "none";
      adjust.setAttribute("aria-expanded", "false");
      adjust.title = "Expand annotation editor";
      adjust.setAttribute("aria-label", "Expand annotation editor");
    }
    queueEditorLayout();
  });

  const onEditorPointerDown = (event: PointerEvent): void => {
    if (event.button !== 0 || !editorExpanded) return;
    const rect = editor.getBoundingClientRect();
    editorDrag = {
      pointerId: event.pointerId,
      offsetX: event.clientX - rect.left,
      offsetY: event.clientY - rect.top,
    };
    dragHandle.setPointerCapture(event.pointerId);
    dragHandle.style.cursor = "grabbing";
    event.preventDefault();
    event.stopPropagation();
  };

  const onEditorPointerMove = (event: PointerEvent): void => {
    if (!editorDrag || editorDrag.pointerId !== event.pointerId) return;
    applyEditorPosition({
      left: event.clientX - editorDrag.offsetX,
      top: event.clientY - editorDrag.offsetY,
    });
    event.preventDefault();
    event.stopPropagation();
  };

  const onEditorPointerUp = (event: PointerEvent): void => {
    if (!editorDrag || editorDrag.pointerId !== event.pointerId) return;
    editorDrag = null;
    dragHandle.style.cursor = "grab";
    if (dragHandle.hasPointerCapture(event.pointerId))
      dragHandle.releasePointerCapture(event.pointerId);
    event.preventDefault();
    event.stopPropagation();
  };
  dragHandle.addEventListener("pointerdown", onEditorPointerDown);
  dragHandle.addEventListener("pointermove", onEditorPointerMove);
  dragHandle.addEventListener("pointerup", onEditorPointerUp);
  dragHandle.addEventListener("pointercancel", onEditorPointerUp);

  const repaint = (): void => {
    for (const target of selected.values()) updateSelectedVisual(target);
    queueEditorLayout();
  };
  // A scrollbar appearing narrows the viewport without a window resize.
  const rootResizeObserver = new ResizeObserver(repaint);

  const removeTargetAtPoint = (x: number, y: number): boolean => {
    for (const target of Array.from(selected.values()).toReversed()) {
      const rect = target.element.getBoundingClientRect();
      if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
        removeSelected(target);
        updateStatus();
        return true;
      }
    }
    const regionIndex = regions.findIndex(
      (region) =>
        x >= region.rect.x &&
        x <= region.rect.x + region.rect.width &&
        y >= region.rect.y &&
        y <= region.rect.y + region.rect.height,
    );
    if (regionIndex >= 0) {
      const [removed] = regions.splice(regionIndex, 1);
      root.querySelector(`[data-region-id="${removed?.id}"]`)?.remove();
      updateStatus();
      return true;
    }
    const strokeIndex = strokes.findIndex(
      (stroke) =>
        x >= stroke.bounds.x &&
        x <= stroke.bounds.x + stroke.bounds.width &&
        y >= stroke.bounds.y &&
        y <= stroke.bounds.y + stroke.bounds.height,
    );
    if (strokeIndex >= 0) {
      const [removed] = strokes.splice(strokeIndex, 1);
      svg.querySelector(`[data-stroke-id="${removed?.id}"]`)?.remove();
      updateStatus();
      return true;
    }
    return false;
  };

  const selectElementsInRect = (rect: PreviewAnnotationRect): number => {
    const candidates = Array.from(document.querySelectorAll("body *"))
      .filter((element) => !isAnnotationNode(element))
      .map((element) => ({ element, rect: element.getBoundingClientRect() }))
      .filter(({ rect: candidate }) => {
        if (candidate.width < 2 || candidate.height < 2) return false;
        return !(
          candidate.right < rect.x ||
          candidate.left > rect.x + rect.width ||
          candidate.bottom < rect.y ||
          candidate.top > rect.y + rect.height
        );
      })
      .filter(({ element, rect: candidate }) => {
        const centerX = candidate.left + candidate.width / 2;
        const centerY = candidate.top + candidate.height / 2;
        return (
          centerX >= rect.x &&
          centerX <= rect.x + rect.width &&
          centerY >= rect.y &&
          centerY <= rect.y + rect.height &&
          (element.children.length === 0 ||
            element instanceof HTMLButtonElement ||
            element instanceof HTMLAnchorElement ||
            element.getAttribute("role") === "button")
        );
      })
      .sort(
        (left, right) => left.rect.width * left.rect.height - right.rect.width * right.rect.height,
      )
      .slice(0, MAX_MARQUEE_ELEMENTS);
    for (const candidate of candidates) addSelected(candidate.element);
    return candidates.length;
  };

  const clearHoverOutline = (): void => {
    hoverOutline.style.display = "none";
  };

  const onPointerMove = (event: PointerEvent): void => {
    if (isAnnotationNode(event.target as Element)) {
      clearHoverOutline();
      return;
    }
    if (tool === "select" && dragStart === null) {
      const target = pickFromPoint(event.clientX, event.clientY);
      if (target) positionBox(hoverOutline, visibleElementRect(target));
      else clearHoverOutline();
      return;
    }
    clearHoverOutline();
    if (tool === "marquee" && dragStart) {
      positionBox(
        marqueeBox,
        normalizeRect(dragStart.x, dragStart.y, event.clientX, event.clientY),
      );
      return;
    }
    if (tool === "draw" && activeStroke) {
      activeStroke.target.points = [
        ...activeStroke.target.points,
        { x: event.clientX, y: event.clientY },
      ];
      activeStroke.target.bounds = strokeBounds(
        activeStroke.target.points,
        activeStroke.target.width,
      );
      activeStroke.path.setAttribute("d", pathFromPoints(activeStroke.target.points));
    }
  };

  const onPointerDown = (event: PointerEvent): void => {
    if (event.button !== 0 || isAnnotationNode(event.target as Element)) return;
    event.preventDefault();
    event.stopPropagation();
    if (tool === "select") {
      const target = pickFromPoint(event.clientX, event.clientY);
      if (target) toggleSelected(target, event.shiftKey);
      return;
    }
    if (tool === "erase") {
      removeTargetAtPoint(event.clientX, event.clientY);
      return;
    }
    dragStart = { x: event.clientX, y: event.clientY };
    if (tool === "draw") {
      const stroke: PreviewAnnotationStrokeTarget = {
        id: nextId("stroke"),
        color: annotationTheme?.primary ?? "#2563eb",
        width: 4,
        points: [dragStart],
        bounds: { x: dragStart.x, y: dragStart.y, width: 1, height: 1 },
      };
      const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
      path.setAttribute(OVERLAY_ATTRIBUTE, "");
      path.setAttribute("data-stroke-id", stroke.id);
      path.setAttribute("fill", "none");
      path.setAttribute("stroke", stroke.color);
      path.setAttribute("stroke-width", String(stroke.width));
      path.setAttribute("stroke-linecap", "round");
      path.setAttribute("stroke-linejoin", "round");
      svg.appendChild(path);
      activeStroke = { target: stroke, path };
    }
  };

  const onPointerUp = (event: PointerEvent): void => {
    if (!dragStart) return;
    event.preventDefault();
    event.stopPropagation();
    if (tool === "marquee") {
      const rect = normalizeRect(dragStart.x, dragStart.y, event.clientX, event.clientY);
      marqueeBox.style.display = "none";
      if (isUsableRect(rect)) {
        const found = selectElementsInRect(rect);
        if (found === 0) {
          const region: PreviewAnnotationRegionTarget = { id: nextId("region"), rect };
          regions.push(region);
          const regionBox = createBox(
            PRIMARY,
            "color-mix(in srgb, var(--t3-primary) 6%, transparent)",
          );
          regionBox.setAttribute("data-region-id", region.id);
          positionBox(regionBox, rect);
          root.appendChild(regionBox);
        }
      }
    } else if (tool === "draw" && activeStroke) {
      if (activeStroke.target.points.length > 1) strokes.push(activeStroke.target);
      else activeStroke.path.remove();
      activeStroke = null;
    }
    dragStart = null;
    updateStatus();
  };

  const onClick = (event: MouseEvent): void => {
    if (isAnnotationNode(event.target as Element)) return;
    event.preventDefault();
    event.stopPropagation();
  };

  const onPointerOut = (event: PointerEvent): void => {
    if (event.relatedTarget === null) clearHoverOutline();
  };

  const onWindowFocus = (): void => {
    if (editor.style.display !== "none") sendVoice("focus");
  };
  const onWindowBlur = (): void => {
    sendVoice("blur");
    clearHoverOutline();
  };

  const restoreStyles = (): void => {
    for (const target of selected.values()) {
      if (!(target.element instanceof HTMLElement || target.element instanceof SVGElement))
        continue;
      for (const [property, baseline] of target.baselineStyles) {
        if (baseline) target.element.style.setProperty(property, baseline);
        else target.element.style.removeProperty(property);
      }
    }
  };

  const teardown = (notifyMain: boolean): void => {
    if (finished) return;
    finished = true;
    rootResizeObserver.disconnect();
    restoreStyles();
    window.removeEventListener("pointermove", onPointerMove, true);
    window.removeEventListener("pointerdown", onPointerDown, true);
    window.removeEventListener("pointerup", onPointerUp, true);
    window.removeEventListener("pointerout", onPointerOut, true);
    window.removeEventListener("click", onClick, true);
    window.removeEventListener("blur", onWindowBlur);
    window.removeEventListener("focus", onWindowFocus);
    window.removeEventListener("keydown", onKeyDown, true);
    window.removeEventListener("keyup", onVoiceKeyUp, true);
    window.removeEventListener("scroll", repaint, true);
    window.removeEventListener("resize", repaint);
    dragHandle.removeEventListener("pointerdown", onEditorPointerDown);
    dragHandle.removeEventListener("pointermove", onEditorPointerMove);
    dragHandle.removeEventListener("pointerup", onEditorPointerUp);
    dragHandle.removeEventListener("pointercancel", onEditorPointerUp);
    if (editorLayoutFrame !== null) window.cancelAnimationFrame(editorLayoutFrame);
    ipcRenderer.off(CANCEL_PICK_CHANNEL, onCancel);
    ipcRenderer.off(ANNOTATION_CAPTURED_CHANNEL, onCaptured);
    ipcRenderer.off(ANNOTATION_VOICE_STATE_CHANNEL, onVoiceState);
    if (elapsedTimer !== null) clearInterval(elapsedTimer);
    document.documentElement.removeAttribute("data-t3code-annotation-tool");
    cursorStyle.remove();
    inputLayer?.remove();
    host.remove();
    activeSession = null;
    if (notifyMain) ipcRenderer.send(ELEMENT_PICKED_CHANNEL, null);
  };

  const onCancel = (): void => teardown(false);
  const onCaptured = (): void => teardown(false);
  const onVoiceKeyUp = (event: KeyboardEvent): void => {
    if (!event.isTrusted || (event.code || event.key) !== voiceKeyCode) return;
    voiceKeyCode = null;
    event.preventDefault();
    event.stopImmediatePropagation();
    sendVoice("key", event);
  };
  const onKeyDown = (event: KeyboardEvent): void => {
    // Only the keyboard drives dictation; a page cannot synthesize the chord or Escape.
    if (voice && event.isTrusted && !event.isComposing && event.keyCode !== 229) {
      if (event.key === "Escape" && voiceBusy) {
        event.preventDefault();
        event.stopImmediatePropagation();
        voiceKeyCode = null;
        pendingVoiceSubmission = null;
        sendVoice("key", event);
        return;
      }
      if (voiceAvailable && editor.style.display !== "none" && isDictationChord(event)) {