Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

192 KB
Raw Blame
/**
 * Note Detail Page JavaScript
 * Handles note viewing, editing, and research triggering.
 */

// Global state
let note = null;
let collections = [];
let noteCollections = [];
let noteResearch = [];
let hasUnsavedChanges = false;
let isEditMode = false;
let _editorFeaturesInitialized = false;
// Holds the IntersectionObserver that powers TOC scroll-spy. setupScrollSpy
// is called every time the note re-renders (saves, restores), so we must
// disconnect the previous observer before creating a new one — otherwise
// observers stack up and every scroll fires N callbacks after N saves.
let _tocObserver = null;
// Snapshot of note.tags taken on enterEditMode and used by exitEditMode
// (cancel) to revert mid-edit add/remove. Without this, tags mutated
// in place during an edit session would silently persist on next save.
let _tagsBeforeEdit = null;
// Guard against re-entry into saveNote while a save is in flight. The
// save button is also disabled via setButtonLoading, but a keyboard
// shortcut or programmatic call could otherwise still dispatch a
// second PUT, racing the first.
let _saveInProgress = false;
// A save requested while another was in flight. The in-flight PUT's body
// was serialized before the new mutation (pin flip, suggested tag,
// keystrokes), so dropping the request would silently lose that change —
// the completed save chains one follow-up that re-reads current state.
let _savePending = false;
// Sticky within one save→chain cycle: does the pending follow-up need to read
// the editor (an explicit edit-mode Save was queued, or editor drift occurred)
// rather than the `note` singleton? Reset when the chain is dispatched.
let _savePendingFromEditor = false;
// Last state the server acknowledged ({title, content, tagsJson, pinned}).
// saveNote diffs against it to send only changed fields — a pin toggle on
// a large note must not re-upload the whole body, and the route only
// schedules the auto-index worker when content/title are present in the
// payload. Set on every server sync (renderNote, save success).
let _lastSaved = null;
// Guard against a double-submit of a collection add/remove. The triggering
// control (modal Add button / badge × button) is also disabled while in
// flight, but a rapid second activation (double-click or keyboard) could
// otherwise fire a duplicate POST/DELETE — surfacing a success plus a
// spurious error toast (the second request 404s / conflicts).
let _collectionMutationInProgress = false;

// DOM Elements
let noteLoading;
let noteContentWrapper;
let noteNotFound;
let titleInput;
let contentEditor;
let pinBtn;
let researchModal;
let collectionModal;
let readMode;
let editMode;
let fabContainer;

/**
 * Initialize the note detail page
 *
 * Skip auto-init under Vitest (happy-dom): the tests import this module to
 * exercise individual functions in isolation and build their own DOM
 * scaffold, so the full page bootstrap (loadNote/fetch, Bootstrap modal
 * construction, global listeners) must not run. `__VITEST_TEST__` is set by
 * the test harness before import; production never sets it, so this branch
 * is a no-op in the browser.
 */
if (typeof window === 'undefined' || !window.__VITEST_TEST__) {
document.addEventListener('DOMContentLoaded', function() {
    noteLoading = document.getElementById('note-loading');
    noteContentWrapper = document.getElementById('note-content-wrapper');
    noteNotFound = document.getElementById('note-not-found');
    titleInput = document.getElementById('ldr-note-title');
    contentEditor = document.getElementById('note-content');
    pinBtn = document.getElementById('pin-btn');
    readMode = document.getElementById('read-mode');
    editMode = document.getElementById('edit-mode');
    fabContainer = document.getElementById('fab-container');

    // Initialize Bootstrap modals
    researchModal = new bootstrap.Modal(document.getElementById('researchModal'));
    collectionModal = new bootstrap.Modal(document.getElementById('collectionModal'));

    // Load the note and collections in parallel
    if (window.noteId) {
        Promise.all([
            loadNote(window.noteId),
            loadAllCollections()
        ]).catch(error => {
            SafeLogger.error('Error during initial load:', error);
        });
    } else {
        showNotFound();
    }

    // Setup change tracking
    setupChangeTracking();

    // Bind tag-input keypress handler directly (replaces inline onkeypress).
    const addTagInput = document.getElementById('add-tag-input');
    if (addTagInput) {
        addTagInput.addEventListener('keypress', handleTagKeypress);
    }

    // Delegated click dispatcher. Replaces inline `onclick="foo()"` in
    // note_detail.html and the inline onclicks in JS-rendered innerHTML
    // strings further down (TOC items, version-history rows, sidebar
    // backlinks, AI result cards). Match: `data-action="kebab-name"`.
    // Single-arg handlers read their argument from a sibling `data-*`
    // attribute; the `'navigate'` action wraps URLValidator.safeAssign
    // for the safe-href links that previously used inline JS.
    const ACTIONS = {
        // Trivial (no args)
        'toggle-pin': togglePin,
        'enter-edit-mode': enterEditMode,
        'exit-edit-mode': exitEditMode,
        'save-note': saveNote,
        'delete-note': deleteNote,
        'show-add-collection-modal': showAddCollectionModal,
        'show-index-to-collection-modal': showIndexToCollectionModal,
        'add-to-collection': addToCollection,
        'index-note-from-modal': indexNoteFromModal,
        'toggle-more-menu': toggleMoreMenu,
        'toggle-fab-menu': toggleFabMenu,
        'toggle-related-notes': toggleRelatedNotes,
        'summarize-note': summarizeNote,
        'extract-research-questions': extractResearchQuestions,
        'suggest-tags': suggestTags,
        'extract-key-concepts': extractKeyConcepts,
        'find-similar-notes': findSimilarNotes,
        'similar-passages': findSimilarPassages,
        'suggest-links': suggestLinks,
        'unlinked-mentions': findUnlinkedMentions,
        'verify-note': verifyNote,
        'trigger-research': triggerResearch,
        'start-research': startResearchFromNote,
        'close-ai-results': closeAIResults,
        // Retry the initial note load after a transient/server failure
        // surfaced the retryable #note-load-error panel.
        'retry-load-note': () => loadNote(window.noteId),
        // Args read from dataset
        // preventDefault: the TOC entries are <a href="#..."> — without it
        // the default hash navigation jumps instantly (defeating the smooth
        // scroll) and pushes a history entry per click.
        'scroll-to-heading': (el, e) => {
            if (e) e.preventDefault();
            scrollToHeading(el.dataset.headingId);
        },
        'view-version': (el) => viewVersion(el.dataset.versionId),
        'compare-version': (el) => compareVersion(el.dataset.versionId),
        'restore-version': (el) => restoreVersion(el.dataset.versionId),
        // Versions pager — append the next (older) page. Wrapped so the
        // dispatcher's (el, e) args don't leak into the `append` param.
        'load-more-versions': () => loadVersionHistory(true),
        // Generic safe-navigation: replaces inline `onclick="URLValidator.safeAssign(...)"`
        // on sidebar-backlink / ai-result-card divs.
        'navigate': (el) => URLValidator.safeAssign(window.location, 'href', el.dataset.href),
    };
    document.addEventListener('click', (e) => {
        const el = e.target.closest('[data-action]');
        if (!el) return;
        const fn = ACTIONS[el.dataset.action];
        if (fn) fn(el, e);
        // Close the FAB menu when an action originating inside it
        // fires. Native menu pattern is to dismiss on selection — the
        // previous behavior kept the menu open over the FAB and over
        // the AI results panel that the action opens, hiding the new
        // busy indicator on the FAB and conflicting visually with the
        // results.
        if (el.closest && el.closest('#fab-menu')) {
            const fabMenu = document.getElementById('fab-menu');
            const fabButton = document.getElementById('fab-button');
            if (fabMenu) fabMenu.classList.remove('ldr-open');
            if (fabButton) {
                fabButton.classList.remove('ldr-open');
                fabButton.setAttribute('aria-expanded', 'false');
            }
        }
    });

    // Mirror the click dispatcher for keyboard activation. Real
    // <button>/<a> elements already handle Enter/Space natively, but
    // some data-action elements are non-button (e.g. divs left over
    // for layout reasons) — without this, keyboard users can't fire
    // those actions even when they're tabbable.
    document.addEventListener('keydown', (e) => {
        if (e.key !== 'Enter' && e.key !== ' ') return;
        const el = e.target.closest('[data-action]');
        if (!el) return;
        // Native <button>/<a> already fire click on Enter (and Space
        // for <button>); skip them so we don't double-dispatch.
        const tag = el.tagName;
        if (tag === 'BUTTON' || (tag === 'A' && el.hasAttribute('href'))) {
            return;
        }
        const fn = ACTIONS[el.dataset.action];
        if (fn) {
            e.preventDefault();
            fn(el, e);
        }
    });

    // Setup click outside handler for menus
    document.addEventListener('click', function(e) {
        // Close more menu if clicking outside
        const moreBtn = document.getElementById('more-btn');
        const moreMenu = document.getElementById('more-menu');
        if (moreMenu && moreBtn && !moreBtn.contains(e.target) && !moreMenu.contains(e.target)) {
            moreMenu.classList.remove('ldr-open');
            moreBtn.setAttribute('aria-expanded', 'false');
        }

        // Close FAB menu if clicking outside
        const fabButton = document.getElementById('fab-button');
        const fabMenu = document.getElementById('fab-menu');
        if (fabMenu && fabButton && !fabButton.contains(e.target) && !fabMenu.contains(e.target)) {
            fabMenu.classList.remove('ldr-open');
            fabButton.classList.remove('ldr-open');
            fabButton.setAttribute('aria-expanded', 'false');
        }
    });

    // Escape closes any open menu and restores focus to its trigger.
    // The wiki-link autocomplete dropdown has its own Escape handler on
    // the textarea (line ~2486-ish) which fires first via the event's
    // path through the focused element; we additionally early-return if
    // the dropdown is open so we never steal Escape from the combobox.
    document.addEventListener('keydown', function(e) {
        if (e.key !== 'Escape') return;
        if (wikiLinkDropdown && wikiLinkDropdown.style.display !== 'none') {
            return;
        }
        const moreMenu = document.getElementById('more-menu');
        const moreBtn = document.getElementById('more-btn');
        if (moreMenu && moreMenu.classList.contains('ldr-open')) {
            moreMenu.classList.remove('ldr-open');
            if (moreBtn) {
                moreBtn.setAttribute('aria-expanded', 'false');
                moreBtn.focus();
            }
            return;
        }
        const fabMenu = document.getElementById('fab-menu');
        const fabButton = document.getElementById('fab-button');
        if (fabMenu && fabMenu.classList.contains('ldr-open')) {
            fabMenu.classList.remove('ldr-open');
            if (fabButton) {
                fabButton.classList.remove('ldr-open');
                fabButton.setAttribute('aria-expanded', 'false');
                fabButton.focus();
            }
        }
    });

    // Warn before leaving with unsaved changes
    window.addEventListener('beforeunload', function(e) {
        if (hasUnsavedChanges) {
            e.preventDefault();
            e.returnValue = '';
        }
    });
});
} // end auto-init guard (skipped under Vitest)

/**
 * Setup change tracking
 */
function setupChangeTracking() {
    titleInput.addEventListener('input', () => { hasUnsavedChanges = true; });
    contentEditor.addEventListener('input', () => { hasUnsavedChanges = true; });
}

/**
 * Load note from API
 */
async function loadNote(noteId) {
    // Reset to the loading state so a retry from the error panel shows the
    // spinner again and clears any prior not-found / load-error panel.
    setNoteViewState('loading');

    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${noteId}`, {
            credentials: 'same-origin'
        });

        // safeFetch does NOT throw on non-2xx. Reserve the permanent
        // "Note not found" state for a genuine 404 (or a 200 whose body
        // reports success:false below); any other non-OK status is a
        // transient server failure where the note likely still exists, so
        // show a retryable error instead of falsely claiming it's gone.
        if (!response.ok) {
            if (response.status === 404) {
                showNotFound();
            } else {
                showLoadError();
            }
            return;
        }

        const data = await response.json();

        if (data.success && data.note) {
            note = data.note;
            renderNote();
            // Load all note-dependent data in parallel
            await Promise.all([
                loadNoteCollections(),
                loadNoteResearch(),
                loadBacklinks(),
                loadOutgoingLinks(),
                loadVersionHistory()
            ]);
        } else {
            showNotFound();
        }
    } catch (error) {
        // A network blip or a non-JSON error body (response.json() throwing)
        // is transient, not a genuine not-found — offer a retry rather than
        // telling the user their persisted note no longer exists.
        SafeLogger.error('Error loading note:', error);
        showLoadError();
    }
}

/**
 * Update the read-mode meta display (title, updated time, word count) from the
 * current module-level `note`. Shared between renderNote() and saveNote()'s
 * success branch so the markup lives in one place. The created-date row is
 * NOT touched here: it never changes after load, so only renderNote() sets it.
 */
function updateReadModeMeta() {
    document.getElementById('note-title-display').textContent = note.title;
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: formatDateTime returns plain text, no user HTML
    document.getElementById('note-updated').innerHTML =
        `<i class="fas fa-clock"></i> Updated: ${formatDateTime(note.updated_at)}`;
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: countWords returns number, no user HTML
    document.getElementById('note-word-count').innerHTML =
        `<i class="fas fa-align-left"></i> ${countWords(note.content)} words`;
}

/**
 * Render note data
 */
function renderNote() {
    setNoteViewState('content');

    // Title and content for edit mode
    titleInput.value = note.title;
    contentEditor.value = note.content;

    // Title / updated time / word count for read mode (shared with saveNote)
    updateReadModeMeta();

    // Created date (read mode) — set once; unchanged after save
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: formatDateTime returns plain text, no user HTML
    document.getElementById('note-created').innerHTML =
        `<i class="fas fa-calendar-plus"></i> Created: ${formatDateTime(note.created_at)}`;

    // Indexed status
    refreshIndexedBadge();

    // Pin button state
    updatePinButton();

    // Tags (both read and edit mode)
    renderTags();
    renderReadModeTags();

    // Render markdown content for read mode
    renderReadModeContent();

    // Generate Table of Contents
    generateTableOfContents();

    // Show FAB in read mode
    if (fabContainer) {
        fabContainer.style.display = isEditMode ? 'none' : 'block';
    }

    // Update page title
    document.title = `${note.title} - Deep Research System`;

    hasUnsavedChanges = false;
    // Fresh server state is the new diff baseline for partial saves…
    _lastSaved = {
        title: note.title || '',
        content: note.content || '',
        tagsJson: JSON.stringify(note.tags || []),
        pinned: !!note.pinned
    };
    // …and the new Cancel-revert baseline: after a restore (or any other
    // full reload) mid-edit-session, Cancel must revert tags to what the
    // server now holds, not to a pre-reload snapshot.
    _tagsBeforeEdit = isEditMode ? (note.tags || []).slice() : null;

    // Initialize markdown toolbar and wiki-link autocomplete once.
    // renderNote may be called multiple times — re-attaching listeners would leak.
    if (!_editorFeaturesInitialized) {
        initMarkdownToolbar();
        initWikiLinkAutocomplete();
        _editorFeaturesInitialized = true;
    }
}

/**
 * Toggle the meta-bar "Indexed" badge (#note-indexed) BOTH directions from the
 * current note.is_indexed state. renderNote sets the badge once at load; this
 * keeps it in sync after index/un-index/save so the prominent status can't go
 * stale (e.g. still reading "not indexed" after a successful index toast).
 */
function refreshIndexedBadge() {
    const indexedEl = document.getElementById('note-indexed');
    if (!indexedEl) return;
    indexedEl.style.display = (note && note.is_indexed) ? 'inline-flex' : 'none';
}

/**
 * Count words in text
 */
function countWords(text) {
    if (!text) return 0;
    return text.trim().split(/\s+/).filter(word => word.length > 0).length;
}

/**
 * Render markdown `text` into element `el`. Single home for the
 * render-and-sanitize decision shared by read-mode content and the editor's
 * preview tab — keep the sanitization choice (and its audit comments) here
 * only, so it can never drift between the two call sites.
 */
function renderNoteContentInto(el, text) {
    // Use renderMarkdown from ui.js if available, otherwise basic render
    if (typeof renderMarkdown === 'function') {
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: renderMarkdown sanitizes via DOMPurify
        el.innerHTML = renderMarkdown(text);
    } else {
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: renderNoteMarkdown escapes user input first
        el.innerHTML = renderNoteMarkdown(text);
    }
}

/**
 * Render content in read mode
 */
function renderReadModeContent() {
    const contentEl = document.getElementById('note-content-rendered');
    if (!contentEl) return;

    renderNoteContentInto(contentEl, note.content || '');

    // Process wiki-style links [[Note Title]]
    processWikiLinks(contentEl);
}

/**
 * Process wiki-style [[links]] in content
 */
function processWikiLinks(container) {
    // Build a fast lookup of {link_text_lowercase: target_id} from the
    // note's resolved outgoing links so [[wiki-links]] navigate via the
    // stored NoteLink FK rather than re-resolving by title. This makes
    // navigation robust to renames: even after a target note is
    // renamed, the existing [[OldTitle]] anchor still navigates to the
    // correct document via target_id. Falls back to title-search if a
    // link_text isn't in the map (legacy NoteLinks, unresolved text,
    // or the source note hasn't been re-saved since the link was
    // typed).
    const linkMap = new Map();
    const outgoing = (note && note.outgoing_links) || [];
    for (const link of outgoing) {
        if (link && link.link_text && link.target_id) {
            linkMap.set(String(link.link_text).toLowerCase(), link.target_id);
        }
    }

    // Build the replacement for one [[…]] occurrence. Shared between
    // the in-DOM walker below and the legacy string-replace fallback.
    // Supports the Obsidian/Roam `[[Target|Display]]` alias syntax —
    // resolution uses the target (everything before the first `|`)
    // while rendering shows the display half. Without the split,
    // `[[Target|Display]]` would search for a literal note titled
    // "Target|Display" (which never exists) and the link would
    // silently fall back to title search every render.
    const renderLink = (rawLinkText) => {
        const linkText = String(rawLinkText);
        const pipeIdx = linkText.indexOf('|');
        const target = (pipeIdx >= 0 ? linkText.slice(0, pipeIdx) : linkText).trim();
        const displayRaw = pipeIdx >= 0 ? linkText.slice(pipeIdx + 1).trim() : '';
        const display = displayRaw || target;
        // Not a link (whitespace-only target, e.g. the alias form "[[|x]]").
        // Render as literal text — but escapeHtml the raw capture: this
        // string is assigned to innerHTML below, and processWikiLinks runs a
        // SECOND time over already-sanitized DOM text, so an unescaped capture
        // like "|<img src=x onerror=1>" would re-animate into a live element
        // (stored XSS). Every other renderLink branch already escapes.
        if (!target) return `[[${escapeHtml(rawLinkText)}]]`;
        const encodedTitle = encodeURIComponent(target);
        const resolvedId = linkMap.get(target.toLowerCase()) || '';
        // data-target-id is the resolved Document id from the NoteLink
        // FK; data-note-title is the original link target used as the
        // fallback search query. The click handler prefers id when set.
        return `<a href="javascript:void(0)" class="ldr-wiki-link" data-note-title="${escapeHtml(encodedTitle)}" data-target-id="${escapeHtml(resolvedId)}" title="Go to: ${escapeHtml(target)}">${escapeHtml(display)}</a>`;
    };

    // Walk text nodes only, skipping anything inside <code> or <pre>
    // (and their descendants) so ``[[X]]`` in inline code stays literal.
    // Using a TreeWalker keeps us from re-parsing innerHTML and avoids
    // matching inside attribute values.
    const walker = document.createTreeWalker(
        container,
        NodeFilter.SHOW_TEXT,
        {
            acceptNode: (node) => {
                let p = node.parentNode;
                while (p && p !== container) {
                    const tag = p.nodeName;
                    if (tag === 'CODE' || tag === 'PRE') {
                        return NodeFilter.FILTER_REJECT;
                    }
                    p = p.parentNode;
                }
                return NodeFilter.FILTER_ACCEPT;
            },
        }
    );

    // Canonical wiki-link target pattern (excludes newline) — defined once
    // in formatting.js as WIKILINK_TARGET so parse/render agree.
    const wikiRegex = new RegExp('\\[\\[(' + window.formatting.WIKILINK_TARGET + ')\\]\\]', 'g');
    const targets = [];
    let current = walker.nextNode();
    while (current) {
        if (wikiRegex.test(current.nodeValue)) {
            targets.push(current);
        }
        wikiRegex.lastIndex = 0;
        current = walker.nextNode();
    }

    for (const textNode of targets) {
        const text = textNode.nodeValue;
        const frag = document.createDocumentFragment();
        let lastIndex = 0;
        text.replace(wikiRegex, (match, noteTitle, offset) => {
            if (offset > lastIndex) {
                frag.appendChild(
                    document.createTextNode(text.slice(lastIndex, offset))
                );
            }
            const html = renderLink(noteTitle);
            const tpl = document.createElement('template');
            // eslint-disable-next-line no-unsanitized/property -- audited 2026-05-20: renderLink output uses escapeHtml on every interpolation
            tpl.innerHTML = html;
            frag.appendChild(tpl.content);
            lastIndex = offset + match.length;
            return match;
        });
        if (lastIndex < text.length) {
            frag.appendChild(document.createTextNode(text.slice(lastIndex)));
        }
        textNode.parentNode.replaceChild(frag, textNode);
    }

    // Attach click handlers to wiki links (avoids inline JS escaping issues)
    container.querySelectorAll('.ldr-wiki-link[data-note-title]').forEach(link => {
        link.addEventListener('click', (event) => {
            event.preventDefault();
            const targetId = link.getAttribute('data-target-id') || '';
            if (targetId) {
                // Stable FK-based navigation — survives target renames.
                URLValidator.safeAssign(window.location, 'href', `/notes/${targetId}`);
                return;
            }
            // Fallback: no NoteLink row resolved this text (legacy or
            // not-yet-saved content). Use the existing title search.
            const encoded = link.getAttribute('data-note-title') || '';
            try {
                const title = decodeURIComponent(encoded);
                navigateToNoteByTitle(title);
            } catch (e) {
                SafeLogger.error('Failed to decode wiki link title:', e);
            }
        });
    });
}

/**
 * Navigate to a note by its title
 */
async function navigateToNoteByTitle(title) {
    try {
        // Search for the note by title. The endpoint is full-text and
        // also matches content, so when the source note's body contains
        // [[<title>]] (the very link the user is clicking) the source
        // itself shows up in the results — and at limit=1 it could
        // shadow the actual target. Pull a wider window so the exact-
        // title match below has something to find.
        const response = await safeFetchWithAuth(`/notes/api/notes?search=${encodeURIComponent(title)}&limit=20`, {
            credentials: 'same-origin'
        });
        const data = await response.json();

        if (data.success && data.notes && data.notes.length > 0) {
            const foundNote = data.notes.find(n => n.title.toLowerCase() === title.toLowerCase());
            if (foundNote) {
                URLValidator.safeAssign(window.location, 'href', `/notes/${foundNote.id}`);
            } else {
                showNoteError(`Note "${title}" not found`);
            }
        } else {
            showNoteError(`Note "${title}" not found`);
        }
    } catch (error) {
        SafeLogger.error('Error navigating to note:', error);
        showNoteError('Failed to find the linked note');
    }
}

/**
 * Render tags in read mode
 */
function renderReadModeTags() {
    const container = document.getElementById('note-tags-display');
    if (!container) return;

    const tags = note.tags || [];
    if (tags.length === 0) {
        container.style.display = 'none';
        return;
    }

    container.style.display = 'flex';

    // Remove existing tags (keep the label)
    const existingTags = container.querySelectorAll('.ldr-tag');
    existingTags.forEach(tag => tag.remove());

    // Add tags
    tags.forEach(tag => {
        const tagEl = document.createElement('span');
        tagEl.className = 'ldr-tag';
        tagEl.textContent = `#${tag}`;
        container.appendChild(tagEl);
    });
}

/**
 * Generate Table of Contents from headings
 */
function generateTableOfContents() {
    const contentEl = document.getElementById('note-content-rendered');
    const tocList = document.getElementById('toc-list');
    if (!contentEl || !tocList) return;

    const headings = contentEl.querySelectorAll('h1, h2, h3');

    if (headings.length === 0) {
        tocList.innerHTML = '<li class="ldr-toc-empty">No headings found</li>';
        return;
    }

    tocList.innerHTML = '';

    headings.forEach((heading, index) => {
        // Add ID to heading for linking
        const id = `heading-${index}`;
        heading.id = id;

        // Determine level
        const level = parseInt(heading.tagName[1], 10);

        // Create TOC item
        const li = document.createElement('li');
        li.className = `ldr-toc-item ldr-level-${level}`;
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: id is numeric index, heading.textContent escaped
        li.innerHTML = `<a href="#${id}" data-action="scroll-to-heading" data-heading-id="${id}">${escapeHtml(heading.textContent)}</a>`;
        tocList.appendChild(li);
    });

    // Setup scroll spy for TOC highlighting
    setupScrollSpy();
}

/**
 * Update pin button appearance
 */
function updatePinButton() {
    if (!pinBtn) return;
    if (note.pinned) {
        pinBtn.classList.add('ldr-pinned');
        pinBtn.title = 'Unpin note';
    } else {
        pinBtn.classList.remove('ldr-pinned');
        pinBtn.title = 'Pin note';
    }
}

/**
 * Render tags
 */
function renderTags() {
    const container = document.getElementById('ldr-note-tags');
    const addInput = document.getElementById('add-tag-input');

    // Remove existing tags (keep the input)
    const existingTags = container.querySelectorAll('.ldr-note-tag');
    existingTags.forEach(tag => tag.remove());

    // Add tags
    (note.tags || []).forEach(tag => {
        const tagEl = document.createElement('span');
        tagEl.className = 'ldr-note-tag';
        // Real <button> instead of <span> so keyboard users can remove
        // tags via Enter/Space — bare spans are not focusable or
        // activatable by AT.
        tagEl.innerHTML = `
            ${escapeHtml(tag)}
            <button type="button" class="ldr-remove-tag" data-tag="${escapeHtml(tag)}" aria-label="Remove tag ${escapeHtml(tag)}">&times;</button>
        `;
        // Attach click handler (avoids inline JS escaping issues)
        const removeBtn = tagEl.querySelector('.ldr-remove-tag');
        if (removeBtn) {
            removeBtn.addEventListener('click', () => {
                removeTag(tag);
            });
        }
        container.insertBefore(tagEl, addInput);
    });
}

/**
 * Handle tag input keypress
 */
// Cap tag length to a reasonable value. Mirrors the backend's
// MAX_TAG_LENGTH in note_service.py (_validate_tags rejects longer tags
// with a 400); enforcing it here too gives inline feedback instead of a
// failed save.
const MAX_TAG_LENGTH = 50;

function handleTagKeypress(event) {
    if (event.key === 'Enter') {
        event.preventDefault();
        const input = document.getElementById('add-tag-input');
        const tag = input.value.trim();

        if (tag.length > MAX_TAG_LENGTH) {
            showNoteError(`Tag must be ${MAX_TAG_LENGTH} characters or less.`);
            return;
        }

        // Initialize before .includes() — older notes can load with
        // tags === null / undefined, in which case calling .includes
        // first would TypeError.
        note.tags ||= [];
        if (tag && !note.tags.includes(tag)) {
            note.tags.push(tag);
            renderTags();
            hasUnsavedChanges = true;
        }

        input.value = '';
    }
}

/**
 * Remove a tag
 */
function removeTag(tag) {
    note.tags = (note.tags || []).filter(t => t !== tag);
    renderTags();
    hasUnsavedChanges = true;
}

/**
 * Toggle pin status
 */
async function togglePin() {
    // Optimistically flip the pin state, then revert ONLY on a 'failed'
    // outcome (a real PUT/network/server failure — saveNote swallows its own
    // exceptions, so it never throws; we must check the returned outcome).
    // A 'queued' outcome needs no revert: the in-flight save chains a
    // follow-up that re-reads note.pinned, so a rapid double-toggle's final
    // state IS persisted (pre-queue, the second toggle was silently dropped
    // and the UI diverged from the server until the next reload). Title and
    // content are always valid in read mode (where the pin button lives), so
    // the validation 'failed' paths don't apply.
    const prevPinned = note.pinned;
    note.pinned = !note.pinned;
    updatePinButton();
    // saveNote reads `note` directly in read mode, so no editor re-sync
    // is needed here — note.content already reflects any accepted [[link]].
    const outcome = await saveNote();
    if (outcome === 'failed') {
        // saveNote already surfaced the error toast; just undo the flip.
        // eslint-disable-next-line require-atomic-updates -- intentional revert of the optimistic flip on a real save failure
        note.pinned = prevPinned;
        updatePinButton();
    }
}

/**
 * Save note.
 *
 * Returns a tri-state outcome so callers can tell a real failure apart
 * from the benign queue case:
 *   'saved'   — the PUT succeeded
 *   'queued'  — another save is running; a follow-up save is chained onto
 *               it and will re-read current state (so the change that
 *               prompted this call WILL be persisted)
 *   'failed'  — validation, network, or server failure (the error toast
 *               has already been shown)
 *
 * @param {Object} [options]
 * @param {boolean} [options.chained] Internal: this call is the follow-up
 *     chained onto a completed save (see _savePending). Chained saves are
 *     system-initiated — they persist drift but never exit edit mode,
 *     since the user didn't click Save for them and may still be typing.
 */
async function saveNote(options = {}) {
    const chained = options.chained === true;
    // Mirror of restoreVersion's _saveInProgress guard: a restore POST is
    // in flight and its loadNote() will rewrite `note` and the editor. A
    // save dispatched now (Save click, pin, suggested tag) would race it
    // with the stale pre-restore state and whichever write resolved last
    // would win — refuse rather than lie. Queueing (_savePending) would be
    // just as wrong: the queued body would still be pre-restore.
    if (_restoreInProgress) {
        if (window.ui) window.ui.showMessage('Restore in progress — please wait.', 'info');
        return 'failed';
    }
    if (_saveInProgress) {
        // Don't drop the request: the in-flight PUT's body was serialized
        // before whatever mutation prompted this call (pin flip, suggested
        // tag, keystrokes), so a plain skip would leave that change
        // in-memory only — UI showing state the server never received.
        _savePending = true;
        // Remember if THIS queued caller is an explicit edit-mode save: the
        // eventual chain must then read the editor, even if the in-flight
        // (root) save was read-mode. Without this, an edit-mode Save queued
        // behind a read-mode pin/tag save would be executed note-sourced and
        // the user's typed content silently dropped under a "saved" toast.
        // A queued call is always user-initiated (chained calls run only when
        // _saveInProgress is already false), so isEditMode is the intent.
        if (isEditMode) _savePendingFromEditor = true;
        return 'queued';
    }

    // Source of truth: in EDIT mode the editor holds the user's live
    // input; in READ mode the `note` singleton is authoritative — read-mode
    // features (accept-suggested-link, add-suggested-tag, pin) mutate `note`
    // directly. Reading from `note` in read mode removes the need for every
    // read-mode caller to first re-sync the hidden editor fields (which was
    // duplicated and easy to forget — silently stripping a just-added
    // [[link]]). We read into locals and only write `note` on success below,
    // so a validation failure never corrupts the singleton.
    // A chained follow-up inherits its PARENT save's data source instead of
    // re-reading the live isEditMode: a chain rooted in an edit-mode Save
    // exists to persist the user's trailing keystrokes (editor drift), but a
    // chain rooted in a read-mode save (pin flip, suggested tag) fired while
    // the user has since entered edit mode must NOT read the editor — it
    // holds half-typed WIP that only their explicit Save may publish.
    const readFromEditor = chained
        ? (options.fromEditMode === true && isEditMode)
        : isEditMode;
    const title = (readFromEditor ? titleInput.value : note.title || '').trim();
    const content = (readFromEditor ? contentEditor.value : note.content || '').trim();

    if (!title) {
        showNoteError('Title is required');
        return 'failed';
    }

    if (!content) {
        showNoteError('Content is required');
        return 'failed';
    }

    let outcome = 'failed';
    // Snapshot the mode at save start: only a save initiated FROM edit mode
    // should exit edit mode on success. Read-mode saves (togglePin,
    // addSuggestedTag) must not force-exit an edit session the user opened
    // while their PUT was still in flight.
    const wasEditMode = isEditMode;
    // Snapshot what this save sends (tags copied — note.tags mutates in
    // place) so the success path can detect state that changed while the
    // PUT and follow-up fetches were in flight, instead of pretending the
    // drifted state was saved.
    const payload = {
        title,
        content,
        tags: (note.tags || []).slice(),
        pinned: note.pinned
    };
    // Send only the fields that changed since the last server-acked state
    // (the route treats absent keys as no-change): a pin toggle or tag
    // edit on a large note must not re-upload the whole body, and a
    // content/title key in the payload is what schedules the server's
    // auto-index worker — sending them unchanged queued a guaranteed
    // no-op background job per pin flip. Without a baseline (first save
    // after a failed load) fall back to sending everything.
    const payloadTagsJson = JSON.stringify(payload.tags);
    const body = {};
    if (!_lastSaved || title !== _lastSaved.title) body.title = title;
    if (!_lastSaved || content !== _lastSaved.content) body.content = content;
    if (!_lastSaved || payloadTagsJson !== _lastSaved.tagsJson) {
        body.tags = payload.tags;
    }
    if (!_lastSaved || payload.pinned !== _lastSaved.pinned) {
        body.pinned = payload.pinned;
    }
    // Nothing differs from the server's state (e.g. Save clicked with no
    // edits, or a chained follow-up whose drift resolved itself): skip
    // the PUT — the server would reject an empty body — but still run
    // the success path so an edit-mode save exits edit mode.
    const skipPut = Object.keys(body).length === 0;
    _saveInProgress = true;
    setButtonLoading('save-btn', true);
    try {
        let data = { success: true };
        if (!skipPut) {
            const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': getCSRFToken()
                },
                body: JSON.stringify(body),
                credentials: 'same-origin'
            });

            data = await response.json();
        }

        if (data.success) {
            outcome = 'saved';
            showSuccess('Note saved');

            // Update note object. Eslint flags this as a possible race
            // condition because `note` is module-level and was read before
            // `await response.json()`; in this app save never overlaps with
            // loadNote (only call site that reassigns `note`), so the
            // mutation is safe.
            // eslint-disable-next-line require-atomic-updates
            note.title = title;
            // eslint-disable-next-line require-atomic-updates
            note.content = content;

            if (!skipPut) {
                // eslint-disable-next-line require-atomic-updates
                note.updated_at = new Date().toISOString();

                // The server accepted exactly this state — it is the new
                // diff baseline for the next partial save.
                // eslint-disable-next-line require-atomic-updates
                _lastSaved = {
                    title,
                    content,
                    tagsJson: payloadTagsJson,
                    pinned: payload.pinned
                };
                // Persisted tags are also the new Cancel-revert baseline:
                // reverting past a successful save would make the next
                // save silently delete tags the server already holds.
                if (_tagsBeforeEdit !== null) {
                    // eslint-disable-next-line require-atomic-updates
                    _tagsBeforeEdit = payload.tags.slice();
                }

                // Update read mode display (note.title/content/updated_at
                // were just set above, so this renders the saved values)
                updateReadModeMeta();

                // Refresh outgoing links from server before re-rendering,
                // so any new [[wiki-link]] added in this edit resolves with
                // its current target_document_id instead of falling through
                // to the title-only path (which silently breaks if the
                // target was renamed). Failure here is non-fatal.
                try {
                    await loadOutgoingLinks();
                } catch (_) {
                    // already logged inside loadOutgoingLinks
                }

                // Refresh the version-history panel: a content/title/tags
                // changing save just snapshotted a new NoteVersion
                // server-side, and the sidebar would otherwise show it only
                // after a full page reload. Failure is non-fatal (the panel
                // renders its own retry state).
                try {
                    await loadVersionHistory();
                } catch (_) {
                    // already logged inside loadVersionHistory
                }

                // Re-render read mode content
                renderReadModeContent();
                generateTableOfContents();
                renderReadModeTags();

                // Update page title (title is a local computed before the
                // PUT; no concurrent writer)
                document.title = `${title} - Deep Research System`;
            }

            // Drift detection — computed HERE, after the awaited follow-up
            // fetches, so it also catches changes made during them:
            // anything the user changed since the PUT body was serialized
            // is NOT in the state the server just accepted. Any drift
            // keeps hasUnsavedChanges armed (truthful beforeunload guard)
            // and chains a follow-up save; editor drift additionally keeps
            // edit mode open below — force-exiting would render the
            // pre-drift content and re-entering would repopulate the
            // textarea from note.content, silently discarding those
            // keystrokes.
            // Editor drift only means something for a save that READ the
            // editor: a chained/read-mode save sourced from `note` will
            // trivially differ from the user's in-progress editor buffer,
            // and treating that as drift would chain saves forever while
            // they type.
            const editorDrifted = readFromEditor && (
                titleInput.value.trim() !== title ||
                contentEditor.value.trim() !== content
            );
            const noteDrifted =
                note.pinned !== payload.pinned ||
                JSON.stringify(note.tags || []) !== payloadTagsJson;
            const drifted = editorDrifted || noteDrifted;
            // A save that never consulted the editor (read-mode-initiated,
            // or a chained follow-up) completing while the user is in edit
            // mode must not disarm the unsaved-changes guard their
            // keystrokes armed.
            const editorNotConsulted = isEditMode && !readFromEditor;
            // eslint-disable-next-line require-atomic-updates
            hasUnsavedChanges =
                drifted || (editorNotConsulted && hasUnsavedChanges);
            if (drifted) {
                _savePending = true;
                // Editor drift (trailing keystrokes past the PUT body) means
                // the chain must read the editor to persist them; note-only
                // drift (pin/tag) stays note-sourced.
                if (editorDrifted) _savePendingFromEditor = true;
            }

            // Exit edit mode and return to read mode — but only when the
            // user explicitly saved from edit mode (see wasEditMode above;
            // chained follow-ups are system-initiated) AND the editor
            // still matches what was saved. On editor drift, stay in edit
            // mode so the trailing keystrokes remain visible; the chained
            // follow-up save persists them.
            if (wasEditMode && !editorDrifted && !chained) {
                // eslint-disable-next-line require-atomic-updates -- mode flips are serialized by _saveInProgress
                isEditMode = false;
                if (readMode) readMode.style.display = 'block';
                if (editMode) editMode.style.display = 'none';
                if (fabContainer) fabContainer.style.display = 'block';

                // Show sidebar again on mobile
                const sidebar = document.querySelector('.ldr-note-sidebar');
                if (sidebar) sidebar.style.display = '';
            }
        } else {
            showNoteError(data.error || 'Failed to save note');
        }
    } catch (error) {
        SafeLogger.error('Error saving note:', error);
        showNoteError('Failed to save note');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard at function entry prevents re-entry; only this finally clears it
        _saveInProgress = false;
        setButtonLoading('save-btn', false);
    }
    if (_savePending) {
        // A change arrived while this save was in flight (queued caller or
        // drift detected above). The guard flag is already false, so this
        // re-entry runs the PUT with freshly-read state; it cannot loop unless
        // state keeps changing, which is exactly when another save is wanted.
        _savePending = false;
        // Read the editor in the chain iff SOMETHING pending needs it — an
        // explicit edit-mode Save queued behind this save, or editor drift
        // detected above — regardless of whether this (root) save was
        // editor- or note-sourced. Reset the flag for the next cycle.
        const fromEditMode = _savePendingFromEditor;
        _savePendingFromEditor = false;
        // AWAIT the chained save so callers that care about full persistence
        // still settle correctly (e.g. edit-mode drift: hasUnsavedChanges
        // clears only once the trailing keystrokes land) — but DISCARD its
        // outcome and return THIS call's own result. The chained save's
        // outcome belongs to whoever queued the change (they already received
        // 'queued' and moved on, trusting the chain), NOT to this call's
        // caller. Returning the chained promise misattributed a later,
        // different mutation's result: togglePin's own PUT persists the pin,
        // but a failed chained save then reported 'failed', so togglePin
        // reverted note.pinned and showed a spurious error even though its own
        // save succeeded (local state then diverged from the server). saveNote
        // never rejects (it catches its own errors and surfaces them via its
        // own toast), so awaiting it here is safe and still reports failures.
        await saveNote({ chained: true, fromEditMode });
    }
    return outcome;
}

/**
 * Delete note
 */
let _deleteInProgress = false;

async function deleteNote() {
    // Re-entrancy guard, matching every other mutating handler here: the
    // triggering control stays enabled, so a double-activation would fire
    // two DELETEs (the second 404s into a spurious error toast).
    if (_deleteInProgress) return;
    if (!confirm('Are you sure you want to delete this note? This action cannot be undone.')) {
        return;
    }

    _deleteInProgress = true;
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}`, {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': getCSRFToken()
            },
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            // eslint-disable-next-line require-atomic-updates -- navigation follows immediately
            hasUnsavedChanges = false;
            URLValidator.safeAssign(window.location, 'href', '/notes/');
        } else {
            showNoteError(data.error || 'Failed to delete note');
        }
    } catch (error) {
        SafeLogger.error('Error deleting note:', error);
        showNoteError('Failed to delete note');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _deleteInProgress = false;
    }
}

/**
 * Load note collections
 */
async function loadNoteCollections() {
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/collections`, {
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            noteCollections = data.collections || [];
            renderNoteCollections();
            // Derive the note's indexed status from its collection membership
            // (a note is indexed iff at least one of its collections has it
            // indexed) and keep note.is_indexed + the meta badge in sync. This
            // runs after both index and un-index (both call loadNoteCollections),
            // so the badge toggles both directions instead of going stale.
            if (note) {
                note.is_indexed = noteCollections.some(c => c.indexed);
                refreshIndexedBadge();
            }
        }
    } catch (error) {
        SafeLogger.error('Error loading note collections:', error);
    }
}

/**
 * Render note collections
 */
function renderNoteCollections() {
    const container = document.getElementById('ldr-note-collections');
    const addBtn = container.querySelector('.ldr-add-collection-btn');

    // Remove existing badges
    const existingBadges = container.querySelectorAll('.ldr-collection-badge');
    existingBadges.forEach(badge => badge.remove());

    // Add collection badges
    noteCollections.forEach(coll => {
        const badge = document.createElement('span');
        badge.className = 'ldr-collection-badge';
        // The system Notes collection is every note's persistent home —
        // the deletion services rely on that membership to protect notes
        // from the orphan-delete cascade, and remove_from_collection
        // refuses to unlink it server-side. Don't render a remove
        // control for it.
        const removable = coll.collection_type !== 'notes';
        // Real <button> for the same A11y reason as the tag-remove spans:
        // bare spans are unfocusable and not activatable by keyboard.
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: coll.name escaped, coll.id is a UUID string
        badge.innerHTML = `
            <i aria-hidden="true" class="fas fa-folder"></i>
            ${escapeHtml(coll.name)}
            ${coll.indexed ? '<i aria-hidden="true" class="fas fa-database" title="Indexed"></i>' : ''}
            ${removable ? `<button type="button" class="ldr-remove-tag" data-collection-id="${escapeHtml(coll.id)}" aria-label="Remove from collection ${escapeHtml(coll.name)}">&times;</button>` : ''}
        `;
        if (removable) {
            const removeBtn = badge.querySelector('.ldr-remove-tag[data-collection-id]');
            removeBtn.addEventListener('click', () => {
                removeFromCollection(coll.id, removeBtn);
            });
        }
        container.insertBefore(badge, addBtn);
    });
}

/**
 * Load all collections for the dropdown
 */
let _collectionsLoadFailed = false;

async function loadAllCollections() {
    try {
        const response = await safeFetchWithAuth('/library/api/collections', {
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            collections = data.collections || [];
            _collectionsLoadFailed = false;
        } else {
            _collectionsLoadFailed = true;
        }
    } catch (error) {
        SafeLogger.error('Error loading collections:', error);
        // Remember the failure: with `collections` left empty, the Add to
        // Collection modal would otherwise claim "already in all
        // collections" — a misleading dead end instead of an error.
        // eslint-disable-next-line require-atomic-updates -- single writer; flag consumed by showAddCollectionModal
        _collectionsLoadFailed = true;
    }
}

/**
 * Show add collection modal
 */
function showAddCollectionModal() {
    const select = document.getElementById('collection-select');

    if (_collectionsLoadFailed) {
        showNoteError("Couldn't load collections — please try again.");
        // Retry in the background so the next open can succeed.
        loadAllCollections();
        return;
    }

    // Filter out collections the note is already in
    const noteCollIds = noteCollections.map(c => c.id);
    const availableColls = collections.filter(c => !noteCollIds.includes(c.id));

    if (availableColls.length === 0) {
        showNoteError('This note is already in all collections');
        return;
    }

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: c.id is UUID, c.name escaped
    select.innerHTML = availableColls.map(c =>
        `<option value="${c.id}">${escapeHtml(c.name)}</option>`
    ).join('');

    collectionModal.show();
}

/**
 * Add note to collection
 */
async function addToCollection(btnEl) {
    const collectionId = document.getElementById('collection-select').value;

    if (!collectionId) {
        showNoteError('Please select a collection');
        return;
    }

    // Re-entrancy guard: block a rapid second activation while the POST is in
    // flight so it can't fire a duplicate request.
    if (_collectionMutationInProgress) return;
    _collectionMutationInProgress = true;
    if (btnEl) btnEl.disabled = true;

    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/collections`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            body: JSON.stringify({ collection_id: collectionId }),
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            collectionModal.hide();
            loadNoteCollections();
            showSuccess('Added to collection');
        } else {
            showNoteError(data.error || 'Failed to add to collection');
        }
    } catch (error) {
        SafeLogger.error('Error adding to collection:', error);
        showNoteError('Failed to add to collection');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _collectionMutationInProgress = false;
        if (btnEl) btnEl.disabled = false;
    }
}

/**
 * Remove note from collection
 */
async function removeFromCollection(collectionId, btnEl) {
    // Re-entrancy guard: block a rapid second click on the badge × while the
    // DELETE is in flight so it can't fire a duplicate request.
    if (_collectionMutationInProgress) return;
    _collectionMutationInProgress = true;
    if (btnEl) btnEl.disabled = true;

    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/collections/${collectionId}`, {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': getCSRFToken()
            },
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            loadNoteCollections();
            showSuccess('Removed from collection');
        } else {
            showNoteError(data.error || 'Failed to remove from collection');
        }
    } catch (error) {
        SafeLogger.error('Error removing from collection:', error);
        showNoteError('Failed to remove from collection');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _collectionMutationInProgress = false;
        if (btnEl) btnEl.disabled = false;
    }
}

/**
 * Render a distinct error+retry state into a sidebar panel so a transient
 * fetch failure is distinguishable from a genuinely-empty panel (mirrors the
 * loadRelatedNotesForSidebar failure notice and the retryable loadNote
 * pattern). `retryFn` re-runs the originating loader on click.
 */
function renderSidebarLoadError(containerId, message, retryFn) {
    const container = document.getElementById(containerId);
    if (!container) return;
    container.innerHTML =
        `<div class="ldr-toc-empty">${escapeHtml(message)} <button type="button" class="ldr-research-empty-link ldr-sidebar-retry">Retry</button></div>`;
    const retryBtn = container.querySelector('.ldr-sidebar-retry');
    if (retryBtn && typeof retryFn === 'function') {
        retryBtn.addEventListener('click', retryFn);
    }
}

/**
 * Load note research history
 */
async function loadNoteResearch() {
    const orderIntent = _latestResearchOrderIntent;
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/research`, {
            credentials: 'same-origin'
        });

        const data = await response.json();

        // A newer drop may occur while a failure-recovery GET is pending.
        // Neither that response nor its error owns the newer visible order.
        if (orderIntent !== _latestResearchOrderIntent) return;

        if (data.success) {
            noteResearch = data.research || [];
            renderNoteResearch();
        } else {
            renderSidebarLoadError('research-list', "Couldn't load research", loadNoteResearch);
        }
    } catch (error) {
        if (orderIntent !== _latestResearchOrderIntent) return;
        SafeLogger.error('Error loading note research:', error);
        renderSidebarLoadError('research-list', "Couldn't load research", loadNoteResearch);
    }
}

/**
 * Render note research history
 */
function renderNoteResearch() {
    const container = document.getElementById('research-list');
    // Defensive: if the Research panel isn't on the page, bail rather than
    // throwing "Cannot set innerHTML of null" inside the parallel loadNote
    // fetch (which previously aborted the whole load).
    if (!container) return;

    const countEl = document.getElementById('sidebar-research-count');
    if (countEl) countEl.textContent = noteResearch.length;

    if (noteResearch.length === 0) {
        // The button reuses the same `trigger-research` action as the FAB
        // (opens the research modal pre-filled with this note), via the
        // delegated [data-action] dispatcher.
        container.innerHTML = `
            <div class="ldr-research-empty">
                <p>No research runs yet.
                    <button type="button" class="ldr-research-empty-link" data-action="trigger-research">Research this note</button>
                    to start from its content.</p>
            </div>
        `;
        return;
    }

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-04-15: all interpolations use escapeHtml/formatDateTime/hardcoded
    container.innerHTML = noteResearch.map(r => `
        <div class="ldr-research-item${r.is_collapsed ? ' ldr-collapsed' : ''}"
             data-research-id="${escapeHtml(r.research_id)}"
             draggable="true">
            <div class="ldr-research-item-header">
                <span class="ldr-research-item-drag-handle" title="Drag to reorder">
                    <i class="fas fa-grip-vertical"></i>
                </span>
                <button class="ldr-research-item-collapse-btn" title="Collapse/expand" type="button" aria-expanded="${r.is_collapsed ? 'false' : 'true'}" aria-label="Collapse or expand research details">
                    <i aria-hidden="true" class="fas fa-chevron-${r.is_collapsed ? 'right' : 'down'}"></i>
                </button>
                <span class="ldr-research-item-query">${escapeHtml(r.query_used || 'Research')}</span>
                <a href="/results/${encodeURIComponent(r.research_id)}" class="btn btn-sm btn-outline-primary">
                    View Results
                </a>
            </div>
            <div class="ldr-research-item-body">
                <div class="ldr-research-item-meta">
                    <span>${escapeHtml(r.research_mode || 'quick')} mode</span>
                    ${r.search_engine ? `<span> | ${escapeHtml(r.search_engine)}</span>` : ''}
                    <span> | ${formatDateTime(r.created_at)}</span>
                </div>
            </div>
        </div>
    `).join('');

    // Wire up collapse toggles
    container.querySelectorAll('.ldr-research-item').forEach(item => {
        const collapseBtn = item.querySelector('.ldr-research-item-collapse-btn');
        if (collapseBtn) {
            collapseBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                toggleResearchCollapsed(item);
            });
        }
        attachResearchDragHandlers(item);
    });
}

/**
 * Toggle collapsed state on a research item, persist via PATCH.
 */
async function toggleResearchCollapsed(itemEl) {
    const researchId = itemEl.getAttribute('data-research-id');
    if (!researchId) return;

    // Per-item re-entrancy guard (mirrors _saveInProgress etc.): without it,
    // two rapid clicks fire two independent PATCHes with no ordering
    // guarantee, so the server can persist the OPPOSITE of the user's final
    // choice — which then reappears (wrongly) on the next note reload while
    // the UI shows the right state. Refuse the second click until the first
    // PATCH settles.
    if (itemEl.dataset.collapsePending === '1') return;
    itemEl.dataset.collapsePending = '1';

    const willCollapse = !itemEl.classList.contains('ldr-collapsed');
    // Optimistic UI
    itemEl.classList.toggle('ldr-collapsed', willCollapse);
    const collapseBtn = itemEl.querySelector('.ldr-research-item-collapse-btn');
    if (collapseBtn) {
        collapseBtn.setAttribute('aria-expanded', willCollapse ? 'false' : 'true');
    }
    const chevron = collapseBtn && collapseBtn.querySelector('i');
    if (chevron) {
        chevron.classList.toggle('fa-chevron-down', !willCollapse);
        chevron.classList.toggle('fa-chevron-right', willCollapse);
    }

    try {
        const response = await safeFetchWithAuth(
            `/notes/api/notes/${note.id}/research/${encodeURIComponent(researchId)}`,
            {
                method: 'PATCH',
                credentials: 'same-origin',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': getCSRFToken(),
                },
                body: JSON.stringify({ is_collapsed: willCollapse }),
            }
        );
        const data = await response.json();
        if (!data.success) throw new Error(data.error || 'Failed to persist');

        // Update local cache so re-renders don't reset the state
        const cached = noteResearch.find(r => r.research_id === researchId);
        if (cached) cached.is_collapsed = willCollapse;
    } catch (error) {
        SafeLogger.error('Error toggling collapse:', error);
        // Revert optimistic update
        itemEl.classList.toggle('ldr-collapsed', !willCollapse);
        if (collapseBtn) {
            collapseBtn.setAttribute('aria-expanded', willCollapse ? 'true' : 'false');
        }
        if (chevron) {
            chevron.classList.toggle('fa-chevron-down', willCollapse);
            chevron.classList.toggle('fa-chevron-right', !willCollapse);
        }
        showNoteError('Failed to update collapse state');
    } finally {
        delete itemEl.dataset.collapsePending;
    }
}

/**
 * HTML5 drag-and-drop handlers for reordering research items.
 */
let _dragSourceEl = null;
let _researchOrderWriteQueue = Promise.resolve();
let _latestResearchOrderIntent = 0;

function attachResearchDragHandlers(itemEl) {
    itemEl.addEventListener('dragstart', (e) => {
        _dragSourceEl = itemEl;
        itemEl.classList.add('ldr-dragging');
        if (e.dataTransfer) {
            e.dataTransfer.effectAllowed = 'move';
            // Required by Firefox for dragstart to fire
            e.dataTransfer.setData('text/plain', itemEl.dataset.researchId || '');
        }
    });

    itemEl.addEventListener('dragend', () => {
        itemEl.classList.remove('ldr-dragging');
        document.querySelectorAll('.ldr-research-item.ldr-drop-target')
            .forEach(el => el.classList.remove('ldr-drop-target'));
        _dragSourceEl = null;
    });

    itemEl.addEventListener('dragover', (e) => {
        if (!_dragSourceEl || _dragSourceEl === itemEl) return;
        e.preventDefault();
        if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
        itemEl.classList.add('ldr-drop-target');
    });

    itemEl.addEventListener('dragleave', () => {
        itemEl.classList.remove('ldr-drop-target');
    });

    itemEl.addEventListener('drop', (e) => {
        e.preventDefault();
        itemEl.classList.remove('ldr-drop-target');
        if (!_dragSourceEl || _dragSourceEl === itemEl) return;

        const container = itemEl.parentElement;
        const items = Array.from(container.querySelectorAll('.ldr-research-item'));
        const fromIdx = items.indexOf(_dragSourceEl);
        const toIdx = items.indexOf(itemEl);
        if (fromIdx < 0 || toIdx < 0) return;

        // Insert source before or after target depending on direction
        if (fromIdx < toIdx) {
            itemEl.after(_dragSourceEl);
        } else {
            itemEl.before(_dragSourceEl);
        }

        persistResearchOrder();
    });
}

async function persistResearchOrder() {
    const container = document.getElementById('research-list');
    if (!container) return Promise.resolve();
    const orderedIds = Array.from(
        container.querySelectorAll('.ldr-research-item')
    ).map(el => el.dataset.researchId).filter(Boolean);
    if (orderedIds.length === 0) return Promise.resolve();

    // Each drop captures its own order, then joins a serialized write queue.
    // Fast consecutive drops must reach the server in user-intent order;
    // otherwise an older slow request can commit after the newer request and
    // resurrect the earlier arrangement on reload.
    const intentId = ++_latestResearchOrderIntent;
    const write = async () => {
        try {
            const response = await safeFetchWithAuth(
                `/notes/api/notes/${note.id}/research/reorder`,
                {
                    method: 'POST',
                    credentials: 'same-origin',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': getCSRFToken(),
                    },
                    body: JSON.stringify({ research_ids: orderedIds }),
                }
            );
            const data = await response.json();
            if (!data.success) throw new Error(data.error || 'Failed');

            // Update local cache order. The DOM already reflects the drop.
            noteResearch.sort(
                (a, b) => orderedIds.indexOf(a.research_id) - orderedIds.indexOf(b.research_id)
            );
        } catch (error) {
            SafeLogger.error('Error persisting research order:', error);
            // An older failed intent must not roll back a newer queued drop.
            // Only the latest intent owns the visible error and server reload.
            if (intentId !== _latestResearchOrderIntent) return;
            showNoteError('Failed to save new order');
            await loadNoteResearch();
        }
    };

    const queued = _researchOrderWriteQueue.then(write, write);
    _researchOrderWriteQueue = queued;
    return queued;
}

/**
 * Trigger research from note
 */
function triggerResearch() {
    // Pre-fill with note title
    document.getElementById('research-query').value = note.title;
    researchModal.show();
}

/**
 * Start research
 */
// Guard against re-entry while the start POST is in flight (mirrors
// _saveInProgress / _verifyInProgress). The modal's Start button stays
// clickable during the request, so a double-click would otherwise launch
// two full research runs and link both to the note.
let _startResearchInProgress = false;

async function startResearchFromNote() {
    if (_startResearchInProgress) return;

    const query = document.getElementById('research-query').value.trim()
        || (note && typeof note.title === 'string' ? note.title.trim() : '');
    const mode = document.getElementById('research-mode').value;
    if (!query) {
        // Without this guard an empty query is silently rejected server-side
        // (400 "Query is required") and the button appears to do nothing.
        showNoteError('Enter a research query first.');
        return;
    }

    _startResearchInProgress = true;
    setButtonLoading('start-research-btn', true);
    try {
        // Start research via the existing API
        const response = await safeFetchWithAuth('/api/start_research', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            body: JSON.stringify({
                query,
                mode,
                metadata: {
                    source_type: 'note',
                    note_id: note.id,
                    note_preview: note.content.substring(0, 500)
                }
            }),
            credentials: 'same-origin'
        });

        const data = await response.json();

        // A queued start (user at app.max_concurrent_researches) is a
        // success: the route returns {status: 'queued', research_id, ...}
        // and the queue processor WILL run it. The results page renders the
        // queued state, so link + navigate exactly like a direct start.
        // Mirrors the established contract in subscriptions.js.
        const started = (data.status === 'success'
            || data.status === window.RESEARCH_STATUS.QUEUED)
            && data.research_id;
        if (started) {
            // The research started; link it to the note. linkResearchToNote
            // returns false (and logs) on failure — capture it and tell the
            // user the run isn't attached to this note, instead of navigating
            // away as if it were (silently orphaned note↔research link).
            const linked = await linkResearchToNote(data.research_id);
            if (!linked) {
                showNoteError('Research started, but it could not be linked to this note.');
            }

            researchModal.hide();

            // Navigate to research result page
            URLValidator.safeAssign(window.location, 'href', `/results/${data.research_id}`);
        } else {
            // /api/start_research reports failures as {status, message};
            // data.error is kept as a fallback for other shapes.
            showNoteError(data.message || data.error || 'Failed to start research');
        }
    } catch (error) {
        SafeLogger.error('Error starting research:', error);
        showNoteError('Failed to start research');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _startResearchInProgress = false;
        setButtonLoading('start-research-btn', false);
    }
}

/**
 * Link research to note
 */
async function linkResearchToNote(researchId) {
    try {
        const resp = await safeFetchWithAuth(`/notes/api/notes/${note.id}/research`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            body: JSON.stringify({
                research_id: researchId
            }),
            credentials: 'same-origin'
        });
        // Don't treat a 4xx/5xx as success — that silently orphaned the
        // research↔note link. Surface it in the log at least.
        if (!resp.ok) {
            let detail = '';
            try { detail = (await resp.json()).error || ''; } catch (_e) { /* ignore */ }
            SafeLogger.error(`Failed to link research ${researchId} to note (HTTP ${resp.status}): ${detail}`);
            return false;
        }
        return true;
    } catch (error) {
        SafeLogger.error('Error linking research to note:', error);
        return false;
    }
}

/**
 * Toggle the note view-state panels. Hides every panel, then shows the single
 * one for `state` ∈ {'loading','content','not-found','load-error'}.
 * Centralizes the panel toggling shared by loadNote(), renderNote(),
 * showNotFound() and showLoadError() so they can't drift.
 */
function setNoteViewState(state) {
    const loadErrorEl = document.getElementById('note-load-error');
    noteLoading.style.display = 'none';
    noteContentWrapper.style.display = 'none';
    noteNotFound.style.display = 'none';
    if (loadErrorEl) loadErrorEl.style.display = 'none';

    if (state === 'loading') {
        noteLoading.style.display = 'block';
    } else if (state === 'content') {
        noteContentWrapper.style.display = 'block';
    } else if (state === 'not-found') {
        noteNotFound.style.display = 'block';
    } else if (state === 'load-error') {
        if (loadErrorEl) loadErrorEl.style.display = 'block';
    }
}

/**
 * Show not found state
 */
function showNotFound() {
    setNoteViewState('not-found');
}

/**
 * Show a retryable load-error state. Unlike showNotFound (a genuine 404 /
 * not-found), this is for transient network/server failures where the note
 * likely still exists, so it offers a Retry instead of implying the note is
 * gone. The Retry button dispatches the 'retry-load-note' action.
 */
function showLoadError() {
    setNoteViewState('load-error');
}

/**
 * Get CSRF token — delegates to global api service
 */
function getCSRFToken() {
    // Delegates to the shared helper (meta-tag fallback when window.api
    // hasn't loaded — the previous inline version sent an empty token then).
    return window.NotesShared.csrfToken();
}

// escapeHtml is provided globally by xss-protection.js (window.escapeHtml)

/**
 * Format datetime for display
 */
function formatDateTime(dateStr) {
    if (!dateStr) return '--';
    try {
        const date = new Date(dateStr);
        return date.toLocaleString();
    } catch (_e) {
        return '--';
    }
}

/**
 * Show error message
 */
function showNoteError(message) {
    if (window.ui && typeof window.ui.showMessage === 'function') {
        window.ui.showMessage(message, 'error');
    } else {
        // Never swallow an error silently — a missing ui helper otherwise
        // makes failed actions look like "the button did nothing".
        window.alert(message);
    }
}

/**
 * Show success message
 */
function showSuccess(message) { if (window.ui) window.ui.showMessage(message, 'success'); }

/**
 * Index note to a collection for RAG search
 */
async function indexNoteToCollection(collectionId) {
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/index`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            body: JSON.stringify({
                collection_id: collectionId,
                force_reindex: false
            }),
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            showSuccess(`Note indexed successfully (${data.result.chunk_count} chunks)`);
            // Refresh collections to show indexed status
            loadNoteCollections();
        } else {
            showNoteError(data.error || 'Failed to index note');
        }
    } catch (error) {
        SafeLogger.error('Error indexing note:', error);
        showNoteError('Failed to index note');
    }
}

/**
 * Show index to collection modal
 */
function showIndexToCollectionModal() {
    const select = document.getElementById('index-collection-select');

    // Use collections the note is already in
    if (noteCollections.length === 0) {
        showNoteError('Note must be in a collection before it can be indexed');
        return;
    }

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: c.id is UUID, c.name escaped
    select.innerHTML = noteCollections.map(c =>
        `<option value="${c.id}">${escapeHtml(c.name)}${c.indexed ? ' (already indexed)' : ''}</option>`
    ).join('');

    // Use getOrCreateInstance so repeated open/close cycles reuse the
    // same Modal instance instead of accumulating orphaned
    // modal-backdrop divs in the DOM (Bootstrap 5 does not dispose
    // prior instances on re-instantiation). Matches the pattern used
    // by showDiffModal.
    const indexModal = bootstrap.Modal.getOrCreateInstance(
        document.getElementById('indexModal'),
    );
    indexModal.show();
}

/**
 * Index note from modal
 */
async function indexNoteFromModal() {
    const collectionId = document.getElementById('index-collection-select').value;

    if (!collectionId) {
        showNoteError('Please select a collection');
        return;
    }

    const indexModal = bootstrap.Modal.getInstance(document.getElementById('indexModal'));
    indexModal.hide();

    await indexNoteToCollection(collectionId);
}

// ============================================================================
// Read/Edit Mode Toggle
// ============================================================================

/**
 * Enter edit mode
 */
function enterEditMode() {
    isEditMode = true;

    // Snapshot tags so Cancel can revert add/remove. handleTagKeypress
    // and removeTag mutate note.tags in place; without this snapshot a
    // user who adds "foo", clicks Cancel, then later legitimately saves
    // would silently persist "foo".
    _tagsBeforeEdit = (note.tags || []).slice();

    // Populate edit fields with current values
    titleInput.value = note.title;
    contentEditor.value = note.content;

    // Reopen on the Write tab: a previous session may have ended on
    // Preview, which leaves the textarea hidden — the editor would open
    // showing a stale read-only preview with nothing to type into.
    const writeTab = document.querySelector('.ldr-editor-tab[data-mode="write"]');
    if (writeTab && !writeTab.classList.contains('active')) {
        writeTab.click();
    }

    // Show edit mode, hide read mode
    if (readMode) readMode.style.display = 'none';
    if (editMode) editMode.style.display = 'block';

    // Hide FAB in edit mode
    if (fabContainer) fabContainer.style.display = 'none';

    // Hide sidebar on mobile when editing (optional - could also keep it)
    const sidebar = document.querySelector('.ldr-note-sidebar');
    if (sidebar && window.innerWidth < 1024) {
        sidebar.style.display = 'none';
    }

    // Focus on title
    setTimeout(() => titleInput.focus(), 100);
}

/**
 * Exit edit mode (cancel)
 */
function exitEditMode() {
    // A save initiated from edit mode is in flight: its PUT has already
    // been dispatched with the edited title/content and WILL commit
    // server-side, and its success path re-applies those values and exits
    // edit mode itself. Cancelling here can't undo that (it would only
    // briefly show a "discarded" state that the resolving save silently
    // reinstates), so refuse rather than lie.
    if (_saveInProgress) {
        if (window.ui) window.ui.showMessage('Still saving — please wait.', 'info');
        return;
    }

    if (hasUnsavedChanges) {
        if (!confirm('You have unsaved changes. Are you sure you want to discard them?')) {
            return;
        }
    }

    isEditMode = false;
    hasUnsavedChanges = false;

    // Revert tag mutations from this edit session. handleTagKeypress and
    // removeTag mutate note.tags in place during edit; here we restore
    // the snapshot taken at enterEditMode so Cancel actually cancels.
    if (_tagsBeforeEdit !== null) {
        note.tags = _tagsBeforeEdit;
        _tagsBeforeEdit = null;
    }

    // Show read mode, hide edit mode
    if (readMode) readMode.style.display = 'block';
    if (editMode) editMode.style.display = 'none';

    // Show FAB
    if (fabContainer) fabContainer.style.display = 'block';

    // Show sidebar again on mobile
    const sidebar = document.querySelector('.ldr-note-sidebar');
    if (sidebar) sidebar.style.display = '';

    // Re-render read mode content (in case we need to restore)
    titleInput.value = note.title;
    contentEditor.value = note.content;
    renderTags();
    renderReadModeTags();
}

/**
 * Scroll to heading by ID
 */
function scrollToHeading(id) {
    const el = document.getElementById(id);
    if (el) {
        el.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
}

/**
 * Setup scroll spy for TOC highlighting
 */
function setupScrollSpy() {
    const tocItems = document.querySelectorAll('.ldr-toc-item');
    const headings = document.querySelectorAll('#note-content-rendered h1, #note-content-rendered h2, #note-content-rendered h3');

    // Tear down any previous observer before creating a fresh one. Without
    // this, every save / restore stacks a new observer on top of the
    // previous ones — they all fire on every scroll forever.
    if (_tocObserver) {
        _tocObserver.disconnect();
        _tocObserver = null;
    }

    if (headings.length === 0) return;

    const observerOptions = {
        root: null,
        rootMargin: '-20% 0px -70% 0px',
        threshold: 0
    };

    _tocObserver = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const id = entry.target.id;
                tocItems.forEach(item => {
                    item.classList.remove('active');
                    if (item.querySelector(`a[href="#${id}"]`)) {
                        item.classList.add('active');
                    }
                });
            }
        });
    }, observerOptions);

    headings.forEach(heading => _tocObserver.observe(heading));
}

/**
 * Toggle more menu
 *
 * Mirrors toggleFabMenu: class-toggle + aria-expanded kept in sync so
 * screen readers announce the open/closed state. Pre-fix this used
 * style.display alone, leaving aria-expanded permanently "false".
 */
function toggleMoreMenu() {
    const menu = document.getElementById('more-menu');
    const button = document.getElementById('more-btn');
    if (menu && button) {
        const isOpen = menu.classList.toggle('ldr-open');
        button.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
    }
}

/**
 * Toggle FAB menu
 */
function toggleFabMenu() {
    const menu = document.getElementById('fab-menu');
    const button = document.getElementById('fab-button');
    if (menu && button) {
        const isOpen = menu.classList.toggle('ldr-open');
        button.classList.toggle('ldr-open', isOpen);
        // Keep aria-expanded in sync with the visible state so screen
        // readers announce the menu state on toggle.
        button.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
    }
}

/**
 * Toggle related notes section in sidebar
 */
// Load lifecycle for the lazy related-notes section. 'idle' means a load
// hasn't run yet OR the last one failed — so re-expanding the section
// retries. The previous gate matched the 'Click to load' placeholder text,
// which the error message replaced, making a single transient failure
// permanent for the rest of the page session.
let _relatedNotesLoadState = 'idle'; // 'idle' | 'loading' | 'loaded'

function toggleRelatedNotes() {
    const section = document.getElementById('sidebar-related');
    const icon = document.getElementById('related-toggle-icon');
    if (section && icon) {
        const isHidden = section.style.display === 'none';
        section.style.display = isHidden ? 'block' : 'none';
        icon.className = isHidden ? 'fas fa-chevron-up' : 'fas fa-chevron-down';

        // Keep aria-expanded on the toggle button in sync so screen
        // readers announce the new state. The button uses aria-controls
        // to point at #sidebar-related so AT users can jump to it.
        const toggleBtn = document.querySelector(
            '[data-action="toggle-related-notes"]'
        );
        if (toggleBtn) {
            toggleBtn.setAttribute('aria-expanded', isHidden ? 'true' : 'false');
        }

        // Load related notes if expanding and not already loaded (or if the
        // previous attempt failed — 'idle' covers both).
        if (isHidden && _relatedNotesLoadState === 'idle') {
            loadRelatedNotesForSidebar();
        }
    }
}

/**
 * Render a single sidebar backlink/related-note card.
 *
 * Shared by loadRelatedNotesForSidebar(), renderBacklinks() and
 * renderOutgoingLinks() so the markup stays in one place. All dynamic values
 * (id, title, preview) are escaped via escapeHtml. `badgeHtml` is appended
 * inside the title div and MUST be a trusted, pre-built static string (e.g.
 * the AI-suggested badge) — it is never escaped.
 */
function renderBacklinkCard({ id, title, preview, badgeHtml = '' }) {
    return `
        <a class="ldr-sidebar-backlink" href="/notes/${escapeHtml(id)}">
            <div class="ldr-sidebar-backlink-title">${escapeHtml(title)}${badgeHtml}</div>
            <div class="ldr-sidebar-backlink-preview">${escapeHtml(preview || '')}</div>
        </a>
    `;
}

/**
 * Load related notes for sidebar
 */
async function loadRelatedNotesForSidebar() {
    const container = document.getElementById('sidebar-related');
    if (!container) return;

    _relatedNotesLoadState = 'loading';
    container.innerHTML = '<div class="ldr-toc-empty"><i class="fas fa-spinner fa-spin"></i> Loading...</div>';

    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/similar?limit=5`, {
            credentials: 'same-origin'
        });

        // A 500/429 (or any non-2xx) is a load FAILURE, not an empty result.
        // safeFetch (unlike safeFetchJson) doesn't throw on non-ok, so guard
        // here before trusting the body — otherwise an error page is rendered
        // as "No related notes found" and the user never sees / can retry it.
        if (!response.ok) {
            throw new Error(`HTTP ${response.status}`);
        }

        const data = await response.json();

        // data.success === false is also a failure (server signalled an error
        // in a 200 body); only an empty list on a genuine success is "empty".
        if (!data.success) {
            throw new Error('Server returned success: false for related notes');
        }

        if (data.similar_notes && data.similar_notes.length > 0) {
            // Real <a> instead of div+data-action="navigate" so keyboard users
            // can activate via Enter and the browser handles middle-click,
            // open-in-new-tab, focus ring, role=link semantics natively.
            // eslint-disable-next-line no-unsanitized/property -- audited 2026-06-15: all interpolations use escapeHtml in renderBacklinkCard
            container.innerHTML = data.similar_notes.map(n => renderBacklinkCard({
                id: n.id,
                title: n.title,
                preview: n.content_preview,
            })).join('');
        } else {
            container.innerHTML = '<div class="ldr-toc-empty">No related notes found</div>';
        }
        _relatedNotesLoadState = 'loaded';
    } catch (error) {
        SafeLogger.error('Error loading related notes:', error);
        // Distinct, retryable error state (mirrors loadVersionHistory /
        // loadNoteResearch) so a transient failure is never mistaken for an
        // empty panel. The Retry button re-runs this loader directly.
        renderSidebarLoadError('sidebar-related', "Couldn't load related notes", loadRelatedNotesForSidebar);
        // Back to 'idle' so a collapse/re-expand also retries the fetch.
        _relatedNotesLoadState = 'idle';
    }
}

// ============================================================================
// AI Features
// ============================================================================

// Button IDs the AI-action handlers pass to setButtonLoading() that
// do NOT exist in the template (the FAB menu items have no per-action
// ids, just data-action). Without a fallback the calls silently
// no-op'd, leaving the user with zero feedback between click and
// result. Route these to the FAB button instead — it's the single
// element guaranteed to be visible while any AI action is running,
// and the .ldr-loading style on it (added in the inline CSS) reads
// as a clear busy indicator.
const AI_ACTION_BUTTON_IDS = new Set([
    'summarize-btn',
    'questions-btn',
    'suggest-tags-btn',
    'concepts-btn',
    'similar-btn',
    'similar-passages-btn',
    'suggest-links-btn',
    'unlinked-mentions-btn',
    'verify-btn',
]);

/**
 * Set button loading state.
 *
 * If the target button doesn't exist AND the id is a known AI-action
 * id, fall back to the FAB button. Multiple AI actions can in
 * principle overlap (rare — the FAB menu closes on click); we use a
 * counter so the FAB only clears its busy state when every in-flight
 * AI call has finished.
 */
let _fabBusyCount = 0;
// Per-action re-entrancy guard for the read-only AI actions, keyed by
// buttonId. Pointer re-clicks are panel-overlay-rejected, but keyboard /
// programmatic re-dispatch of the SAME action otherwise fires duplicate
// embedding-backed requests. Keyed per action (not a single boolean) so
// DISTINCT actions can still run concurrently — orthogonal to the
// multi-action _fabBusyCount busy counter above.
const _aiActionsInFlight = new Set();
// Monotonic token bumped every time the user CLOSES the AI results panel.
// Async AI actions capture it when they open the panel in a loading state
// and re-check before rendering their result: a mismatch means the user
// dismissed the panel while the request was in flight, and rendering would
// pop it back open over the page. This is the same reopen-after-dismiss
// guard the fact-check flow implements with _factCheckGeneration, applied
// to every other AI action (Summarize, Key Concepts, Version Details, …).
let _aiPanelGeneration = 0;
function _aiPanelDismissedSince(generation) {
    return generation !== _aiPanelGeneration;
}
function setButtonLoading(buttonId, loading) {
    const btn = document.getElementById(buttonId);
    if (btn) {
        if (loading) {
            btn.classList.add('ldr-loading');
            btn.disabled = true;
        } else {
            btn.classList.remove('ldr-loading');
            btn.disabled = false;
        }
        return;
    }

    if (!AI_ACTION_BUTTON_IDS.has(buttonId)) return;

    const fabBtn = document.getElementById('fab-button');
    if (!fabBtn) return;
    if (loading) {
        _fabBusyCount += 1;
        fabBtn.classList.add('ldr-loading');
        fabBtn.setAttribute('aria-busy', 'true');
    } else {
        // Floor at 0 so an unmatched "stop" call (defensive cleanup
        // path) can't leave the counter negative and pin the FAB to
        // busy forever.
        _fabBusyCount = Math.max(0, _fabBusyCount - 1);
        if (_fabBusyCount === 0) {
            fabBtn.classList.remove('ldr-loading');
            fabBtn.removeAttribute('aria-busy');
        }
    }
}

/**
 * Show AI results panel
 */
function showAIResults(title, contentHtml) {
    // If a different AI action repurposes the shared panel, stop any running
    // fact-check poll so its later completion can't clobber the new content.
    // The fact-check flow itself renders under the 'Fact-check' title, so it
    // doesn't cancel its own poll.
    if (title !== 'Fact-check') {
        _stopFactCheckPoll();
    }
    const panel = document.getElementById('ldr-ai-results-panel');
    const titleEl = document.getElementById('ldr-ai-results-title');
    const contentEl = document.getElementById('ldr-ai-results-content');

    if (titleEl) titleEl.textContent = title;
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: callers build contentHtml with escapeHtml on all user data
    if (contentEl) contentEl.innerHTML = contentHtml;
    if (panel) {
        panel.style.display = 'block';
        // Don't scroll if it's a floating panel
    }
}

/**
 * Open the shared AI results panel in a loading state for an action that is
 * about to run, so the panel reflects the in-flight action instead of leaving
 * a stale prior result visible during the multi-second call. Routes through
 * showAIResults so the fact-check poll is stopped (a non-'Fact-check' title)
 * exactly as a completed result would. NOT used by the fact-check flow, which
 * manages its own panel lifecycle + generation token.
 */
function showAIResultsLoading(title) {
    // A new action is claiming the shared panel. Bump the generation so a
    // DIFFERENT action already in flight (the panel is shared, actions run
    // concurrently) treats this as a dismissal and drops its own later
    // render/teardown — the panel now belongs to this action.
    _aiPanelGeneration += 1;
    showAIResults(
        title,
        '<div class="ldr-factcheck-status"><div class="ldr-spinner" style="width:16px;height:16px;border-width:2px;"></div> Working…</div>',
    );
}

/**
 * Close AI results panel
 */
function closeAIResults() {
    const panel = document.getElementById('ldr-ai-results-panel');
    // Guard the lookup like showAIResults does — a missing panel must not throw
    // (and abort the fact-check-poll stop below).
    if (panel) panel.style.display = 'none';
    // Invalidate any AI action still in flight for this panel: its
    // completion must not reopen what the user just dismissed.
    _aiPanelGeneration += 1;
    // Stop any in-flight fact-check polling tied to this panel.
    _stopFactCheckPoll();
}

/**
 * Fail an AI action: close the results panel and surface the error as a
 * toast. The AI actions open a loading panel on start (showAIResultsLoading)
 * so a stale prior result isn't shown during the call; on failure the panel
 * must CLOSE rather than stay stuck on "Working…" — errors are reported via
 * a toast only, never an empty/half panel (the established contract, asserted
 * by notes-ai-mocked / notes-ai-error-paths specs).
 */
function failAIAction(message, panelGen) {
    // Only tear down the panel if THIS action still owns it. Without the
    // ownership check, action A failing would close the panel — and bump
    // the generation — while action B's "Working…" is showing, silently
    // dropping B's eventual result. When panelGen is omitted (legacy
    // callers), fall back to the old unconditional close.
    if (panelGen === undefined || !_aiPanelDismissedSince(panelGen)) {
        closeAIResults();
    }
    showNoteError(message);
}

/**
 * Shared scaffold for the read-only AI actions. Every handler previously
 * duplicated the same flow: setButtonLoading(true) -> showAIResultsLoading ->
 * POST -> render on success / failAIAction otherwise -> catch -> finally
 * setButtonLoading(false). This owns the uniform parts; the caller supplies:
 *   - buttonId / title  — the FAB button + AI panel title
 *   - request()         — thunk returning the fetch Promise
 *   - onSuccess(data)   — renders, and returns whether it handled the success.
 *                         A falsy return (e.g. a 200 with a missing/empty key)
 *                         routes to the shared failure path, preserving the old
 *                         `if (data.success && data.KEY)` checks.
 *   - failMessage       — fallback error text
 *
 * The per-action re-entrancy guard is intentionally NOT added here: it
 * would change concurrency behavior and overlaps the deliberate _fabBusyCount
 * busy mechanism — a separate decision left to a focused follow-up.
 */
async function runAiAction({ buttonId, title, request, onSuccess, failMessage }) {
    // Drop a re-dispatch of this same action while it is already running.
    if (_aiActionsInFlight.has(buttonId)) return;
    _aiActionsInFlight.add(buttonId);
    setButtonLoading(buttonId, true);
    showAIResultsLoading(title);
    const panelGen = _aiPanelGeneration;
    try {
        const response = await request();
        const data = await response.json();
        if (data && data.success) {
            // The user closed the "Working…" panel while the request was in
            // flight — drop the render instead of popping the panel back
            // open over the page (finally still clears the busy states).
            if (_aiPanelDismissedSince(panelGen)) return;
            if (onSuccess(data)) return;
        }
        failAIAction((data && data.error) || failMessage, panelGen);
    } catch (error) {
        SafeLogger.error(`Error in AI action "${title}":`, error);
        failAIAction(failMessage, panelGen);
    } finally {
        _aiActionsInFlight.delete(buttonId);
        setButtonLoading(buttonId, false);
    }
}

/**
 * Summarize note using AI
 */
async function summarizeNote() {
    return runAiAction({
        buttonId: 'summarize-btn',
        title: 'Summary',
        failMessage: 'Failed to generate summary',
        request: () => safeFetchWithAuth(`/notes/api/notes/${note.id}/summarize`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            credentials: 'same-origin'
        }),
        onSuccess: (data) => {
            if (!data.summary) return false;
            showAIResults('Summary', `<p>${escapeHtml(data.summary).replace(/\n/g, '<br>')}</p>`);
            return true;
        }
    });
}

/**
 * Extract research questions from note
 */
async function extractResearchQuestions() {
    return runAiAction({
        buttonId: 'questions-btn',
        title: 'Research Questions',
        failMessage: 'Failed to extract questions',
        request: () => safeFetchWithAuth(`/notes/api/notes/${note.id}/research-questions`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            credentials: 'same-origin'
        }),
        onSuccess: (data) => {
            if (!data.questions) return false;
            if (data.questions.length === 0) {
                showAIResults('Research Questions', '<p>No research questions could be extracted from this note.</p>');
                return true;
            }
            const html = data.questions.map(q => `
                <div class="ldr-research-question-item">
                    <span class="ldr-research-question-text">${escapeHtml(q)}</span>
                    <button class="ldr-research-question-btn" data-question="${escapeHtml(q)}">
                        <i class="fas fa-search"></i> Research
                    </button>
                </div>
            `).join('');
            showAIResults('Research Questions', html);

            // Attach click handlers to research question buttons (avoids inline JS escaping issues)
            const resultsContainer = document.getElementById('ldr-ai-results-content');
            if (resultsContainer) {
                resultsContainer.querySelectorAll('.ldr-research-question-btn').forEach(btn => {
                    btn.addEventListener('click', () => {
                        const question = btn.getAttribute('data-question');
                        if (question) {
                            researchQuestion(question);
                        }
                    });
                });
            }
            return true;
        }
    });
}

/**
 * Research a specific question
 */
function researchQuestion(question) {
    document.getElementById('research-query').value = question;
    researchModal.show();
}

/**
 * Suggest tags for the note
 */
async function suggestTags() {
    return runAiAction({
        buttonId: 'suggest-tags-btn',
        title: 'Suggested Tags',
        failMessage: 'Failed to suggest tags',
        request: () => {
            // Use the in-memory note content (the source of truth in read mode,
            // where the FAB lives) rather than the hidden edit textarea, which
            // can hold stale text after actions like accept-suggested-link
            // update note.content without re-syncing the editor.
            const content = note.content || contentEditor.value;
            return safeFetchWithAuth('/notes/api/notes/suggest-tags', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': getCSRFToken()
                },
                body: JSON.stringify({
                    content,
                    existing_tags: note.tags || []
                }),
                credentials: 'same-origin'
            });
        },
        onSuccess: (data) => {
            if (!data.tags) return false;
            if (data.tags.length === 0) {
                showAIResults('Suggested Tags', '<p>No additional tags could be suggested for this note.</p>');
                return true;
            }
            const html = `
                <p style="margin-bottom: 0.75rem; color: var(--text-muted);">Click a tag to add it:</p>
                <div>
                    ${data.tags.map(tag => `
                        <button type="button" class="ldr-suggested-tag" data-tag="${escapeHtml(tag)}" aria-label="Add tag ${escapeHtml(tag)}">
                            <i class="fas fa-plus" aria-hidden="true"></i> ${escapeHtml(tag)}
                        </button>
                    `).join('')}
                </div>
            `;
            showAIResults('Suggested Tags', html);

            // Attach click handlers to suggested tags (avoids inline JS escaping issues)
            const resultsContainer = document.getElementById('ldr-ai-results-content');
            if (resultsContainer) {
                resultsContainer.querySelectorAll('.ldr-suggested-tag').forEach(tagEl => {
                    tagEl.addEventListener('click', () => {
                        const tag = tagEl.getAttribute('data-tag');
                        if (tag) {
                            addSuggestedTag(tag, tagEl);
                        }
                    });
                });
            }
            return true;
        }
    });
}

/**
 * Add a suggested tag to the note
 */
/**
 * Mark a suggested-tag chip as accepted (disabled + checkmark), mirroring
 * setAcceptLinkBtnState so the user can see which suggestions they took.
 */
function markSuggestedTagAccepted(chipEl, tag) {
    if (!chipEl) return;
    chipEl.disabled = true;
    chipEl.classList.add('ldr-suggestion-accepted');
    chipEl.setAttribute('aria-label', `Added tag ${tag}`);
    chipEl.innerHTML = `<i class="fas fa-check" aria-hidden="true"></i> ${escapeHtml(tag)}`;
}

async function addSuggestedTag(tag, chipEl) {
    if (!note.tags) {
        note.tags = [];
    }

    // Same guard as the manual tag input. AI-suggested tags are usually
    // short, but a misbehaving model could produce a long string.
    if (tag.length > MAX_TAG_LENGTH) {
        showNoteError(`Suggested tag was too long; skipped.`);
        return;
    }

    // Re-clicking an already-added tag previously fell through silently (the
    // whole body is gated on !includes). Surface it and reflect the accepted
    // state on the chip instead of a no-op.
    if (note.tags.includes(tag)) {
        markSuggestedTagAccepted(chipEl, tag);
        showSuccess('Already added');
        return;
    }

    const hadUnsavedChanges = hasUnsavedChanges;
    note.tags.push(tag);
    // Suggest Tags is FAB-only, i.e. launched from read mode — update
    // the visible read-mode tag row, not just the hidden edit-mode
    // container.
    renderTags();
    renderReadModeTags();
    hasUnsavedChanges = true;

    // The suggestions panel can stay open across enterEditMode. If the
    // user accepts a chip while editing, behave like the manual tag
    // input: the tag rides along with their eventual Save (persisting
    // here would serialize — and clobber — their unsaved editor text).
    if (isEditMode) {
        markSuggestedTagAccepted(chipEl, tag);
        showSuccess(`Added tag: ${tag}`);
        return;
    }

    // Persist immediately, like togglePin: read mode has no Save button,
    // so without this the tag would be silently lost on navigation.
    // saveNote reads `note` directly in read mode — no editor re-sync.
    const outcome = await saveNote();
    if (outcome === 'failed') {
        // saveNote already surfaced the error toast; undo the
        // optimistic add so the UI doesn't show a never-persisted tag.
        note.tags = note.tags.filter(t => t !== tag);
        renderTags();
        renderReadModeTags();
        // Restore the pre-add unsaved flag ONLY if we're still in read mode.
        // The AI panel survives enterEditMode, so the user could have entered
        // edit mode and typed real title/content edits while this tag save was
        // in flight — those edits now own hasUnsavedChanges. Clobbering it back
        // to the (false) pre-add value would silently discard their work on
        // Cancel and skip the beforeunload warning. (Mirrors the isEditMode
        // re-check acceptSuggestedLink does after its await.) When still in read
        // mode, restoring avoids a spurious beforeunload / Cancel prompt.
        if (!isEditMode) {
            // eslint-disable-next-line require-atomic-updates -- read mode is single-writer for this flag; only restored when the user has NOT entered edit mode during the await
            hasUnsavedChanges = hadUnsavedChanges;
        }
        return;
    }
    // 'queued' (another save was mid-PUT — rare in read mode): the tag is
    // genuinely persisted, not just hoped for — the in-flight save chains
    // a follow-up that re-reads note.tags and carries it.
    markSuggestedTagAccepted(chipEl, tag);
    showSuccess(`Added tag: ${tag}`);
}

/**
 * Extract key concepts from note
 */
async function extractKeyConcepts() {
    return runAiAction({
        buttonId: 'concepts-btn',
        title: 'Key Concepts',
        failMessage: 'Failed to extract concepts',
        request: () => safeFetchWithAuth(`/notes/api/notes/${note.id}/key-concepts`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            credentials: 'same-origin'
        }),
        onSuccess: (data) => {
            if (!data.concepts) return false;
            const { entities, concepts, themes } = data.concepts;

            const renderCategory = (title, items, className) => {
                if (!items || items.length === 0) return '';
                return `
                    <div class="ldr-concepts-category">
                        <div class="ldr-concepts-category-title">${title}</div>
                        <div class="ldr-concepts-chips">
                            ${items.map(item => `<span class="ldr-concept-chip ${className}">${escapeHtml(item)}</span>`).join('')}
                        </div>
                    </div>
                `;
            };

            const html = `
                ${renderCategory('Entities', entities, 'ldr-entity')}
                ${renderCategory('Concepts', concepts, 'ldr-concept')}
                ${renderCategory('Themes', themes, 'ldr-theme')}
            `;

            if (html.trim() === '') {
                showAIResults('Key Concepts', '<p>No key concepts could be extracted from this note.</p>');
            } else {
                showAIResults('Key Concepts', html);
            }
            return true;
        }
    });
}

/**
 * Find similar notes
 */
async function findSimilarNotes() {
    return runAiAction({
        buttonId: 'similar-btn',
        title: 'Similar Notes',
        failMessage: 'Failed to find similar notes',
        request: () => safeFetchWithAuth(`/notes/api/notes/${note.id}/similar?limit=5`, {
            credentials: 'same-origin'
        }),
        onSuccess: (data) => {
            if (!data.similar_notes) return false;
            if (data.similar_notes.length === 0) {
                showAIResults('Similar Notes', '<p>No similar notes found. Try adding more notes or ensure notes are indexed.</p>');
                return true;
            }
            const html = data.similar_notes.map(n => `
                <a class="ldr-ai-result-card" href="/notes/${escapeHtml(n.id)}">
                    <div class="ldr-ai-result-card-title">${escapeHtml(n.title)}</div>
                    <div class="ldr-ai-result-card-preview">${escapeHtml(n.content_preview)}</div>
                    <div class="ldr-ai-result-card-meta">
                        ${window.formatting.similarityBadge(n.similarity)}
                        ${(n.tags || []).slice(0, 2).map(t => `<span>${escapeHtml(t)}</span>`).join('')}
                    </div>
                </a>
            `).join('');
            showAIResults('Similar Notes', html);
            return true;
        }
    });
}

/**
 * Find similar passages (chunk-level). Uses the user's current text
 * selection if any (so they can target a specific paragraph); otherwise
 * falls back to the start of the note. Returns matching passages from OTHER
 * notes, each linking to its source note.
 */
// Return the current selection only if it lies within the rendered note
// content. Page-wide window.getSelection() would otherwise treat text selected
// anywhere (e.g. in a previous AI panel) as the "passage" to search.
function _selectionWithinNote() {
    const sel = window.getSelection && window.getSelection();
    if (!sel || sel.rangeCount === 0 || sel.isCollapsed) return '';
    const container = document.getElementById('note-content-rendered');
    if (!container) return '';
    if (!container.contains(sel.anchorNode) || !container.contains(sel.focusNode)) {
        return '';
    }
    return String(sel).trim();
}

// NOTE: deliberately NOT routed through runAiAction. Unlike the other AI
// actions it resolves a text selection and short-circuits with guidance when
// there's nothing to search (no request at all) — control flow the uniform
// scaffold doesn't model. Kept inline to preserve that exact behavior.
async function findSimilarPassages() {
    if (_aiActionsInFlight.has('similar-passages-btn')) return;
    _aiActionsInFlight.add('similar-passages-btn');
    setButtonLoading('similar-passages-btn', true);
    showAIResultsLoading('Similar Passages');
    const panelGen = _aiPanelGeneration;
    try {
        const sel = _selectionWithinNote();
        const text = (sel && sel.length >= 10)
            ? sel
            : (note.content || '').trim().slice(0, 1000);
        if (!text) {
            showAIResults('Similar Passages', '<p>Select a passage (or add content) to find similar passages.</p>');
            return;
        }

        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/similar-passages`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken() },
            body: JSON.stringify({ text }),
            credentials: 'same-origin'
        });
        const data = await response.json();

        if (data.success && Array.isArray(data.passages)) {
            // Dismissed mid-flight — don't reopen the panel (see runAiAction).
            if (_aiPanelDismissedSince(panelGen)) return;
            if (data.passages.length === 0) {
                showAIResults('Similar Passages', '<p>No similar passages found in your other notes. Try selecting a more specific passage, or index more notes.</p>');
            } else {
                const html = data.passages.map(p => `
                    <a class="ldr-ai-result-card" href="/notes/${escapeHtml(p.note_id)}">
                        <div class="ldr-ai-result-card-title">${escapeHtml(p.note_title)}</div>
                        <div class="ldr-ai-result-card-preview">${escapeHtml(p.snippet)}</div>
                        <div class="ldr-ai-result-card-meta">
                            ${window.formatting.similarityBadge(p.similarity)}
                        </div>
                    </a>
                `).join('');
                showAIResults('Similar Passages', html);
            }
        } else {
            failAIAction(data.error || 'Failed to find similar passages', panelGen);
        }
    } catch (error) {
        SafeLogger.error('Error finding similar passages:', error);
        failAIAction('Failed to find similar passages', panelGen);
    } finally {
        _aiActionsInFlight.delete('similar-passages-btn');
        setButtonLoading('similar-passages-btn', false);
    }
}

/**
 * Suggest notes to link to (semantic). Reuses the AI results panel.
 *
 * Unlike findSimilarNotes (whose cards navigate), each suggestion here
 * carries an Accept button — the card itself is a non-navigable <div>;
 * the action is "create the [[link]]", not "jump to the note".
 */
async function suggestLinks() {
    return runAiAction({
        buttonId: 'suggest-links-btn',
        title: 'Suggested Links',
        failMessage: 'Failed to suggest links',
        request: () => safeFetchWithAuth(`/notes/api/notes/${note.id}/suggested-links?limit=5`, {
            credentials: 'same-origin'
        }),
        onSuccess: (data) => {
            if (!Array.isArray(data.suggestions)) return false;
            if (data.suggestions.length === 0) {
                showAIResults('Suggested Links', '<p>No link suggestions found. Add more notes, or index them for semantic search, to get suggestions.</p>');
                return true;
            }
            const html = data.suggestions.map(n => `
                <div class="ldr-ai-result-card" data-suggestion-id="${escapeHtml(n.id)}">
                    <div class="ldr-ai-result-card-title">${escapeHtml(n.title)}</div>
                    <div class="ldr-ai-result-card-preview">${escapeHtml(n.content_preview || '')}</div>
                    <div class="ldr-ai-result-card-meta ldr-suggest-link-meta">
                        ${window.formatting.similarityBadge(n.similarity)}
                        <button type="button" class="ldr-suggest-link-accept"
                                data-target-id="${escapeHtml(n.id)}"
                                data-target-title="${escapeHtml(n.title)}">
                            <i class="fas fa-plus" aria-hidden="true"></i> Link
                        </button>
                    </div>
                </div>
            `).join('');
            showAIResults('Suggested Links', html);

            // Wire Accept buttons post-render (avoids inline JS / CSP issues).
            const resultsContainer = document.getElementById('ldr-ai-results-content');
            if (resultsContainer) {
                resultsContainer.querySelectorAll('.ldr-suggest-link-accept').forEach(btn => {
                    btn.addEventListener('click', () => acceptSuggestedLink(btn));
                });
            }
            return true;
        }
    });
}

/**
 * Set an Accept button's visual state. Kept synchronous (and out of the
 * async fn) so eslint's require-atomic-updates doesn't flag the DOM-property
 * writes; the innerHTML values are static literals (no injection).
 * @param {HTMLElement} btn
 * @param {'loading'|'linked'|'idle'} state
 */
function setAcceptLinkBtnState(btn, state) {
    if (state === 'loading') {
        btn.disabled = true;
        btn.innerHTML = '<i class="fas fa-spinner fa-spin" aria-hidden="true"></i>';
    } else if (state === 'linked') {
        btn.disabled = true;
        btn.innerHTML = '<i class="fas fa-check" aria-hidden="true"></i> Linked';
    } else {
        btn.disabled = false;
        btn.innerHTML = '<i class="fas fa-plus" aria-hidden="true"></i> Link';
    }
}

/**
 * Accept an AI-suggested link: POST /accept-link (inserts [[Target]] into
 * the note + records the NoteLink), then refresh the read-mode view and the
 * outgoing-links sidebar. The FAB only opens the panel from read mode, but
 * the panel survives enterEditMode, so accepts CAN fire mid-edit — that
 * path refuses with a toast instead of hitting the server.
 */
async function acceptSuggestedLink(btn) {
    const targetId = btn.dataset.targetId;
    const targetTitle = btn.dataset.targetTitle || 'note';
    const card = btn.closest('.ldr-ai-result-card');

    // Mid-edit accept must not POST /accept-link: the server would insert
    // [[Target]] into the SAVED content, which the user's eventual Save
    // (built from the stale editor buffer) would silently overwrite while
    // the button still shows "Linked". Staging the title into the editor
    // is no better: it loses the route's exact-target-id binding (duplicate
    // titles would resolve to the wrong note) and a title containing ']]'
    // or '|' corrupts the wiki-link syntax — and a later Cancel would
    // discard the staged text while the chip kept claiming "Linked".
    // Refuse rather than lie; the chip stays actionable for read mode.
    if (isEditMode) {
        if (window.ui) window.ui.showMessage('Finish editing (Save or Cancel) before accepting links.', 'info');
        return;
    }

    setAcceptLinkBtnState(btn, 'loading');

    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/accept-link`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            body: JSON.stringify({ target_note_id: targetId }),
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            // The link is persisted server-side regardless of the client's
            // current mode. Mark the chip 'linked' up front so a later render
            // error can't fall to the catch below and falsely report failure.
            if (card) card.classList.add('ldr-suggestion-accepted');
            setAcceptLinkBtnState(btn, 'linked');

            const serverContent = (data.note && typeof data.note.content === 'string')
                ? data.note.content
                : null;

            // The entry guard refuses an accept STARTED in edit mode, but the
            // AI panel survives enterEditMode, so the user can enter edit mode
            // while this POST is in flight. Re-check here: the read-mode path
            // below mutates note.content and re-renders the READ view, which is
            // wrong mid-edit — and, worse, the editor buffer seeded at
            // enterEditMode holds the PRE-link content, so the user's eventual
            // Save (which reads the editor) would silently overwrite the
            // just-persisted link.
            if (isEditMode) {
                if (serverContent !== null
                    && contentEditor
                    && contentEditor.value === note.content) {
                    // The editor still holds the pristine pre-accept buffer (no
                    // typing yet): fold the link into BOTH the editor and the
                    // singleton so a later Save keeps it and Cancel reveals it.
                    // eslint-disable-next-line require-atomic-updates -- `note` is the module singleton; not reassigned concurrently
                    note.content = serverContent;
                    contentEditor.value = serverContent;
                    showSuccess(`Linked to "${targetTitle}"`);
                } else {
                    // The user has unsaved WIP we cannot safely merge the link
                    // into. Keep the singleton current (so Cancel reveals the
                    // link) but leave their editor buffer untouched, and warn
                    // that their current edit won't include the link.
                    if (serverContent !== null) {
                        // eslint-disable-next-line require-atomic-updates -- `note` is the module singleton; not reassigned concurrently
                        note.content = serverContent;
                    }
                    if (window.ui) {
                        window.ui.showMessage(
                            `Linked to "${targetTitle}", but your current edits don't include it — reload to see the link.`,
                            'info'
                        );
                    }
                }
                return;
            }

            showSuccess(`Linked to "${targetTitle}"`);
            // Read mode: sync local content from the server response (includes
            // the new [[link]]); safe because read mode has no unsaved edits and
            // no other flow reassigns `note` during this accept.
            if (serverContent !== null) {
                // eslint-disable-next-line require-atomic-updates -- `note` is the module singleton; not reassigned concurrently in read mode
                note.content = serverContent;
            }
            try {
                // Refresh outgoing-links FIRST so note.outgoing_links is
                // populated, THEN re-render so the new [[link]] resolves via the
                // stable target-id path (mirrors the saveNote flow).
                await loadOutgoingLinks();
                renderReadModeContent();
                // accept-link appends [[Target]] via update_note, a
                // content-changing save that snapshots a new NoteVersion
                // server-side. Refresh the version-history pager (as saveNote
                // does) so its _versions/_versionsTotal don't go stale — a
                // stale pager duplicates/misnumbers a row on the next
                // "show older versions" click.
                await loadVersionHistory();
            } catch (e) {
                SafeLogger.error('Post-accept refresh failed (link persisted):', e);
            }
        } else {
            setAcceptLinkBtnState(btn, 'idle');
            showNoteError(data.error || 'Failed to accept link');
        }
    } catch (error) {
        SafeLogger.error('Error accepting suggested link:', error);
        setAcceptLinkBtnState(btn, 'idle');
        showNoteError('Failed to accept link');
    }
}

/**
 * Unlinked mentions: other notes whose text mentions THIS note's title but
 * don't yet link to it. Each row offers a one-click "Link" that adds
 * [[ThisNote]] to the mentioning note (via the existing accept-link route on
 * that note, target = current note), which then shows up as a backlink.
 */
async function findUnlinkedMentions() {
    return runAiAction({
        buttonId: 'unlinked-mentions-btn',
        title: 'Unlinked Mentions',
        failMessage: 'Failed to find unlinked mentions',
        request: () => safeFetchWithAuth(`/notes/api/notes/${note.id}/unlinked-mentions`, {
            credentials: 'same-origin'
        }),
        onSuccess: (data) => {
            if (!Array.isArray(data.mentions)) return false;
            if (data.mentions.length === 0) {
                showAIResults('Unlinked Mentions', '<p>No unlinked mentions found — no other notes mention this note\'s title without linking to it.</p>');
                return true;
            }
            const html = data.mentions.map(m => `
                <div class="ldr-ai-result-card" data-mention-id="${escapeHtml(m.id)}">
                    <div class="ldr-ai-result-card-title">${escapeHtml(m.title)}</div>
                    <div class="ldr-ai-result-card-preview">${escapeHtml(m.content_preview || '')}</div>
                    <div class="ldr-ai-result-card-meta ldr-suggest-link-meta">
                        <span class="text-muted">mentions this note</span>
                        <button type="button" class="ldr-suggest-link-accept"
                                data-mention-id="${escapeHtml(m.id)}">
                            <i class="fas fa-plus" aria-hidden="true"></i> Link
                        </button>
                    </div>
                </div>
            `).join('');
            showAIResults('Unlinked Mentions', html);

            const resultsContainer = document.getElementById('ldr-ai-results-content');
            if (resultsContainer) {
                resultsContainer.querySelectorAll('.ldr-suggest-link-accept').forEach(btn => {
                    btn.addEventListener('click', () => acceptUnlinkedMention(btn));
                });
            }
            return true;
        }
    });
}

/**
 * Accept an unlinked mention: add [[ThisNote]] to the *mentioning* note by
 * calling accept-link on that note with this note as the target. The mention
 * then becomes a backlink, so we refresh the backlinks sidebar.
 */
async function acceptUnlinkedMention(btn) {
    const mentionId = btn.dataset.mentionId;
    const card = btn.closest('.ldr-ai-result-card');
    setAcceptLinkBtnState(btn, 'loading');
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${mentionId}/accept-link`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken() },
            body: JSON.stringify({ target_note_id: note.id }),
            credentials: 'same-origin'
        });
        const data = await response.json();
        if (data.success) {
            showSuccess('Linked');
            // Mark success BEFORE the best-effort backlinks refresh so a
            // refresh error can't fall to the catch below and falsely revert
            // the button / report failure while the link actually persisted
            // (mirrors acceptSuggestedLink's ordering).
            if (card) card.classList.add('ldr-suggestion-accepted');
            setAcceptLinkBtnState(btn, 'linked');
            try {
                await loadBacklinks();
            } catch (e) {
                SafeLogger.error('Post-accept backlinks refresh failed (link persisted):', e);
            }
        } else {
            setAcceptLinkBtnState(btn, 'idle');
            showNoteError(data.error || 'Failed to link');
        }
    } catch (error) {
        SafeLogger.error('Error accepting unlinked mention:', error);
        setAcceptLinkBtnState(btn, 'idle');
        showNoteError('Failed to link');
    }
}

// ============================================================================
// Verify with Research (fact-check)
// ============================================================================

let factCheckPollTimer = null;
// Monotonic ownership token for the shared AI panel during the fact-check
// flow. _stopFactCheckPoll bumps it on every stop: panel close
// (closeAIResults), another AI action repurposing the panel (showAIResults
// with a non-'Fact-check' title), a new verify run, or the poll reaching a
// terminal state. The poll tick and gradeFactCheck capture it and re-check
// after each await — clearInterval can't cancel a tick already suspended on
// an await, and the timer is null during grading, so a stale tick/grade
// would otherwise reopen a dismissed panel or clobber a newer one. A changed
// token means a newer owner exists, so the stale work aborts silently.
let _factCheckGeneration = 0;

function _stopFactCheckPoll() {
    _factCheckGeneration += 1;
    if (factCheckPollTimer) {
        clearInterval(factCheckPollTimer);
        factCheckPollTimer = null;
    }
}

/** Render a transient "working…" state into the AI panel. */
function showFactCheckStatus(messageHtml, claims) {
    const claimsHtml = (claims && claims.length)
        ? '<ul class="ldr-factcheck-claims">'
            + claims.map(c => `<li>${escapeHtml(c)}</li>`).join('')
            + '</ul>'
        : '';
    // messageHtml is built from hardcoded strings + encodeURIComponent(uuid);
    // claims are escaped above.
    showAIResults(
        'Fact-check',
        `<div class="ldr-factcheck-status"><div class="ldr-spinner" style="width:16px;height:16px;border-width:2px;"></div> ${messageHtml}</div>${claimsHtml}`,
    );
}

/**
 * Verify the note's factual claims with a research run.
 * Flow: extract claims (server) → start research (reuse /api/start_research)
 * → link to note → poll status → grade against the report (server) → render
 * verdicts. All LLM work happens server-side in normal requests.
 */
let _verifyInProgress = false;
// Covers the grading phase: the poll callback nulls factCheckPollTimer
// BEFORE awaiting gradeFactCheck, and _verifyInProgress was already cleared
// when the poll was scheduled — without this flag a second 'Verify with
// Research' during the (potentially tens of seconds) grading LLM call would
// pass the guard and start a duplicate research run.
let _gradeInProgress = false;

async function verifyNote() {
    // Re-entrancy guard: the FAB button isn't disabled while an action runs, so
    // without this a second activation (or one while a poll or the grading
    // step is still running) would start a duplicate research run and orphan
    // the first poll.
    if (_verifyInProgress || _gradeInProgress || factCheckPollTimer) {
        return;
    }
    _verifyInProgress = true;
    setButtonLoading('verify-btn', true);
    _stopFactCheckPoll();
    // Panel ownership for the PRE-poll phases (claim extraction + research
    // start — both multi-second calls). Closing the panel or starting
    // another AI action bumps _factCheckGeneration (via _stopFactCheckPoll),
    // so a stale write here must not reopen/clobber the panel. The poll and
    // grade phases carry their own captures of the same token.
    const myGen = _factCheckGeneration;
    // Claim the shared AI panel, like showAIResultsLoading does for the
    // other actions: without this bump, a different AI action already in
    // flight still owns the current generation and its late response
    // would render over the fact-check panel mid-poll.
    _aiPanelGeneration += 1;
    showFactCheckStatus('Extracting checkable claims…', []);

    try {
        // 1. Extract checkable claims + synthesize a research query.
        const fcResp = await safeFetchWithAuth(`/notes/api/notes/${note.id}/fact-check`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken() },
            credentials: 'same-origin',
        });
        const fcData = await fcResp.json();
        if (myGen !== _factCheckGeneration) return;
        if (!fcResp.ok || !fcData.success) {
            showAIResults('Fact-check', `<p>${escapeHtml(fcData.error || 'Could not start fact-check.')}</p>`);
            return;
        }
        const claims = fcData.claims || [];
        showFactCheckStatus(`Extracted ${claims.length} claim(s). Starting research…`, claims);

        // 2. Start a research run (same path as "Research This Note").
        const srResp = await safeFetchWithAuth('/api/start_research', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken() },
            body: JSON.stringify({
                query: fcData.query,
                mode: 'quick',
                metadata: { source_type: 'note', note_id: note.id, fact_check: true },
            }),
            credentials: 'same-origin',
        });
        const srData = await srResp.json();
        if (myGen !== _factCheckGeneration) return;
        // A queued start ({status: 'queued', research_id}) is a success —
        // the queue processor will run it and the status poll below treats
        // 'queued' as non-terminal, so the flow works unchanged. Mirrors the
        // start-research contract in subscriptions.js.
        const started = (srData.status === 'success'
            || srData.status === window.RESEARCH_STATUS.QUEUED)
            && srData.research_id;
        if (!started) {
            showAIResults('Fact-check', `<p>${escapeHtml(srData.message || srData.error || 'Could not start research. Check that an LLM and a search engine are configured.')}</p>`);
            return;
        }
        const researchId = srData.research_id;
        const linked = await linkResearchToNote(researchId);
        if (myGen !== _factCheckGeneration) return;
        if (!linked) {
            // The grade endpoint hard-requires the NoteResearch link, so
            // continuing would poll the full research run only to hit a
            // guaranteed "Research is not linked to this note" 404 at
            // grading time. Stop now with an actionable error instead.
            showAIResults('Fact-check', '<p>Could not attach the research run to this note, so its results could not be graded. Please try again.</p>');
            return;
        }

        // Open in a NEW tab: this fact-check's poll/state lives entirely on
        // this page, so a same-tab navigation would unload it and silently
        // abandon the in-progress verification (the poll fires
        // _stopFactCheckPoll on unload and the verdicts are never rendered).
        const link = `<a href="/results/${encodeURIComponent(researchId)}" target="_blank" rel="noopener noreferrer">view live progress</a>`;
        const phase = srData.status === window.RESEARCH_STATUS.QUEUED
            ? `Research queued (position ${Number(srData.queue_position) || '?'})…`
            : 'Researching…';
        showFactCheckStatus(`${phase} ${link}`, claims);

        // 3. Poll until the research finishes, then grade.
        _startFactCheckPoll(researchId, claims);
    } catch (error) {
        SafeLogger.error('Error verifying note:', error);
        // The panel was opened in a spinner state ('Extracting checkable
        // claims…' / 'Researching…') before this throw; resolve it into a
        // visible error rather than leaving it spinning forever (the
        // toast alone fades). Guard on ownership like the poll/grade paths
        // so we don't clobber a panel another action has since claimed.
        if (myGen === _factCheckGeneration) {
            showAIResults('Fact-check', '<p>Fact-check failed. Please try again.</p>');
        }
        showNoteError('Fact-check failed');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _verifyInProgress = false;
        setButtonLoading('verify-btn', false);
    }
}

// ~10 minutes at the 3s interval. Beyond this the poll gives up rather than
// running forever for a research that never reaches a terminal state.
const FACT_CHECK_MAX_POLLS = 200;

function _startFactCheckPoll(researchId, claims) {
    _stopFactCheckPoll();
    // Capture the generation that owns this poll. Re-checked after the status
    // fetch so a tick already in flight when the panel is closed/repurposed
    // aborts instead of grading and reopening the dismissed panel.
    const myGen = _factCheckGeneration;
    let polls = 0;
    let inFlight = false;
    factCheckPollTimer = setInterval(async () => {
        // setInterval doesn't await the previous callback; skip overlapping
        // ticks so a slow status fetch can't fire gradeFactCheck twice.
        if (inFlight) return;
        inFlight = true;
        try {
            polls += 1;
            if (polls > FACT_CHECK_MAX_POLLS) {
                _stopFactCheckPoll();
                showAIResults('Fact-check', `<p>Fact-check timed out waiting for the research to finish. <a href="/results/${encodeURIComponent(researchId)}">View progress</a>.</p>`);
                return;
            }
            const resp = await safeFetchWithAuth(`/api/research/${encodeURIComponent(researchId)}/status`, {
                credentials: 'same-origin',
            });
            const data = await resp.json();
            // The panel may have been closed (external _stopFactCheckPoll) or
            // repurposed by another AI action while this /status fetch was in
            // flight, which bumps the generation. If so, abort without
            // touching the panel — don't reopen a dismissed panel or grade a
            // cancelled run.
            if (myGen !== _factCheckGeneration) return;
            const status = data.status;
            // A 404/error response (e.g. the research was deleted) carries no
            // `status`. Stop and surface it instead of polling forever.
            if (!resp.ok || !status) {
                _stopFactCheckPoll();
                showAIResults('Fact-check', `<p>${escapeHtml(data.error || 'Lost track of the research run.')}</p>`);
                return;
            }
            if (!window.ResearchStates) return;
            if (window.ResearchStates.isTerminal(status)) {
                _stopFactCheckPoll();
                if (window.ResearchStates.isCompleted(status)) {
                    await gradeFactCheck(researchId, claims);
                } else {
                    showAIResults('Fact-check', `<p>Research did not complete (status: ${escapeHtml(String(status))}).</p>`);
                }
            }
        } catch (e) {
            SafeLogger.error('Fact-check poll error:', e);
        } finally {
            // eslint-disable-next-line require-atomic-updates -- overlap guard reset
            inFlight = false;
        }
    }, 3000);
}

async function gradeFactCheck(researchId, claims) {
    // Set synchronously (the poll callback already nulled the timer), so
    // verifyNote's guard covers the whole grading round-trip.
    _gradeInProgress = true;
    // Capture the panel-ownership generation at entry (the poll tick bumped it
    // via _stopFactCheckPoll just before calling us). If a panel close or
    // another AI action moves it during the grading round-trip, abandon this
    // stale result rather than clobbering/reopening the panel.
    const myGen = _factCheckGeneration;
    showFactCheckStatus('Research complete. Grading claims…', claims);
    // Declared outside try so the catch can still read resp.status: an
    // infra-level 502/504 that returns an HTML body makes resp.json() throw,
    // and the softened "temporarily unavailable" copy keys off the 502.
    let resp;
    try {
        resp = await safeFetchWithAuth(
            `/notes/api/notes/${note.id}/fact-check/${encodeURIComponent(researchId)}/grade`,
            {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken() },
                body: JSON.stringify({ claims }),
                credentials: 'same-origin',
            },
        );
        const data = await resp.json();
        if (myGen !== _factCheckGeneration) return;
        if (data.success && Array.isArray(data.verdicts)) {
            renderVerdicts(data.verdicts, researchId);
        } else {
            // The research COMPLETED successfully; only the grading step
            // failed (e.g. the grader raised on unparseable/refusal output →
            // 500, or report-unavailable → 502). Keep the completed
            // research_id and offer a cheap re-grade instead of discarding it
            // and forcing a full re-research.
            renderGradeFailure(researchId, claims, data.error, resp.status);
        }
    } catch (e) {
        SafeLogger.error('Fact-check grade error:', e);
        // Same ownership check as the success path: don't reopen a panel
        // the user dismissed (or another action claimed) mid-grade.
        if (myGen !== _factCheckGeneration) return;
        // Same rationale as above: a transient/network grade failure must not
        // cost another full research run — keep the research and offer retry.
        // Forward resp.status when we have it (e.g. a proxy 502/504 whose HTML
        // body broke resp.json()) so the 502 softening still applies.
        renderGradeFailure(
            researchId,
            claims,
            'Grading failed.',
            resp ? resp.status : undefined,
        );
    } finally {
        _gradeInProgress = false;
    }
}

/**
 * Render a grade-failure state that KEEPS the completed research run and offers
 * a "Retry grading" affordance (re-calls the grade endpoint) so a transient
 * grader/report error doesn't force the user to re-run the whole research.
 */
function renderGradeFailure(researchId, claims, errorMsg, httpStatus) {
    // Soften the 502 copy (report temporarily unavailable) — it's transient
    // and retryable, unlike a hard error.
    const message = httpStatus === 502
        ? 'The research report was temporarily unavailable for grading.'
        : (errorMsg || 'Grading failed.');
    const html = `
        <p>${escapeHtml(message)}</p>
        <p class="text-muted">The research finished successfully — you can re-grade it without re-running the research.</p>
        <div class="ldr-factcheck-actions">
            <button type="button" class="ldr-note-action-btn ldr-primary ldr-retry-grade-btn"
                    data-research-id="${escapeHtml(researchId)}">
                <i class="fas fa-redo" aria-hidden="true"></i> Retry grading
            </button>
            <a href="/results/${encodeURIComponent(researchId)}" target="_blank" rel="noopener noreferrer">View full research report</a>
        </div>
    `;
    showAIResults('Fact-check', html);

    const resultsContainer = document.getElementById('ldr-ai-results-content');
    const retryBtn = resultsContainer && resultsContainer.querySelector('.ldr-retry-grade-btn');
    if (retryBtn) {
        retryBtn.addEventListener('click', () => {
            // Guard against re-grading while a grade is already running.
            if (_gradeInProgress) return;
            // Bump the panel generation so this retry owns the panel, matching
            // the poll→grade handoff contract.
            _stopFactCheckPoll();
            gradeFactCheck(researchId, claims);
        });
    }
}

function renderVerdicts(verdicts, researchId) {
    const cls = {
        supported: 'ldr-verdict-supported',
        contradicted: 'ldr-verdict-contradicted',
        partially_supported: 'ldr-verdict-partial',
        unverified: 'ldr-verdict-unverified',
    };
    const label = {
        supported: 'Supported',
        contradicted: 'Contradicted',
        partially_supported: 'Partially supported',
        unverified: 'Unverified',
    };
    const safeUrlCheck = window.SemanticSearch && window.SemanticSearch.isSafeExternalUrl;
    if (!safeUrlCheck) {
        // Without the shared URL-safety check we cannot safely render external
        // source links (escapeHtml alone does NOT neutralize a javascript:
        // href). Log it and surface "Sources unavailable" per verdict below,
        // rather than silently dropping every citation as if the verdicts had
        // none (fail-closed silent degradation).
        SafeLogger.error('window.SemanticSearch.isSafeExternalUrl unavailable; fact-check source links cannot be rendered');
    }
    const isSafe = safeUrlCheck || (() => false);

    if (!verdicts.length) {
        showAIResults('Fact-check', '<p>No checkable claims were verified.</p>');
        return;
    }

    const cards = verdicts.map((v) => {
        const verdict = v.verdict || 'unverified';
        // Source links are external/untrusted — gate on isSafeExternalUrl
        // (escapeHtml alone does NOT neutralize a javascript: href).
        const safeSources = (v.sources || [])
            .filter(s => s && isSafe(s.url))
            .map(s => `<a href="${escapeHtml(s.url)}" target="_blank" rel="noopener noreferrer">${escapeHtml(s.title || s.url)}</a>`)
            .join(', ');
        const hadSources = (v.sources || []).some(s => s && s.url);
        let sourcesBlock = '';
        if (safeSources) {
            sourcesBlock = `<div class="ldr-verdict-sources"><i class="fas fa-link" aria-hidden="true"></i> ${safeSources}</div>`;
        } else if (hadSources && !safeUrlCheck) {
            // Links existed but couldn't be safety-checked — say so instead of
            // rendering a verdict that looks like it simply had no sources.
            sourcesBlock = '<div class="ldr-verdict-sources ldr-verdict-sources-unavailable">Sources unavailable</div>';
        }
        const pct = Math.round(Number(v.confidence) || 0);
        return `
            <div class="ldr-verdict-card">
                <div class="ldr-verdict-header">
                    <span class="ldr-verdict-badge ${cls[verdict] || cls.unverified}">${escapeHtml(label[verdict] || 'Unverified')}</span>
                    <span class="ldr-verdict-confidence" title="Confidence" aria-label="Confidence ${pct}%">${pct}%</span>
                </div>
                <div class="ldr-verdict-claim">${escapeHtml(v.claim || '')}</div>
                ${v.reasoning ? `<div class="ldr-verdict-reasoning">${escapeHtml(v.reasoning)}</div>` : ''}
                ${sourcesBlock}
            </div>`;
    }).join('');

    const footer = `<p class="ldr-verdict-footer"><a href="/results/${encodeURIComponent(researchId)}">View full research report</a></p>`;
    showAIResults('Fact-check', cards + footer);
}

// ============================================================================
// Backlinks Features
// ============================================================================

/**
 * Load backlinks for the note
 */
async function loadBacklinks() {
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/backlinks`, {
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            renderBacklinks(data.backlinks || []);
        } else {
            renderSidebarLoadError('sidebar-backlinks', "Couldn't load backlinks", loadBacklinks);
        }
    } catch (error) {
        SafeLogger.error('Error loading backlinks:', error);
        renderSidebarLoadError('sidebar-backlinks', "Couldn't load backlinks", loadBacklinks);
    }
}

/**
 * Load outgoing links (links this note makes to other notes)
 */
async function loadOutgoingLinks() {
    try {
        const response = await safeFetchWithAuth(
            `/notes/api/notes/${note.id}/outgoing-links`,
            { credentials: 'same-origin' }
        );

        const data = await response.json();

        if (data.success) {
            const links = data.outgoing_links || data.links || [];
            // Keep note.outgoing_links in sync: processWikiLinks reads
            // from it to map [[Title]] → data-target-id, so a stale copy
            // (e.g. after a save adds new wiki-links) leaves the inline
            // anchor unresolved until full page reload.
            if (note) note.outgoing_links = links;
            renderOutgoingLinks(links);
        } else {
            renderSidebarLoadError('sidebar-outgoing-links', "Couldn't load outgoing links", loadOutgoingLinks);
        }
    } catch (error) {
        SafeLogger.error('Error loading outgoing links:', error);
        renderSidebarLoadError('sidebar-outgoing-links', "Couldn't load outgoing links", loadOutgoingLinks);
    }
}

/**
 * Render backlinks in sidebar
 */
function renderBacklinks(backlinks) {
    const container = document.getElementById('sidebar-backlinks');
    const countEl = document.getElementById('sidebar-backlinks-count');

    if (!container || !countEl) return;

    countEl.textContent = backlinks.length;

    if (backlinks.length === 0) {
        container.innerHTML = `<div class="ldr-toc-empty">No notes link here</div>`;
        return;
    }

    // Real <a> for keyboard accessibility (Enter/middle-click/open-in-new-tab).
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-05-20: all interpolations use escapeHtml in renderBacklinkCard
    container.innerHTML = backlinks.map(bl => renderBacklinkCard({
        id: bl.id,
        title: bl.title,
        preview: bl.content_preview,
    })).join('');
}

/**
 * Render outgoing links in sidebar (reuses backlink item styles)
 */
function renderOutgoingLinks(links) {
    const container = document.getElementById('sidebar-outgoing-links');
    const countEl = document.getElementById('sidebar-outgoing-links-count');

    if (!container || !countEl) return;

    countEl.textContent = links.length;

    if (links.length === 0) {
        container.innerHTML = `<div class="ldr-toc-empty">No outgoing links</div>`;
        return;
    }

    // Real <a> for keyboard accessibility (Enter/middle-click/open-in-new-tab).
    // A small magic icon marks links created by accepting an AI suggestion.
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-05-25: all interpolations use escapeHtml in renderBacklinkCard; badge markup is static
    container.innerHTML = links.map(link => renderBacklinkCard({
        id: link.id,
        title: link.title,
        preview: link.content_preview,
        // Static, non-user-controlled badge markup; intentionally not escaped.
        badgeHtml: link.auto_suggested ? ' <i class="fas fa-magic ldr-ai-link-badge" title="AI-suggested link" aria-label="AI-suggested link"></i>' : '',
    })).join('');
}

// ============================================================================
// Version History Features
// ============================================================================

/**
 * Load version history for the note.
 *
 * Pages through the route's offset/total support: the first call loads the
 * newest VERSIONS_PAGE_SIZE versions, and `loadVersionHistory(true)` (the
 * "Show older versions" button) appends the next page. Without the offset
 * the UI could never reach versions beyond the newest page even though the
 * data exists (retention cap is 100 versions per note).
 */
const VERSIONS_PAGE_SIZE = 20;
// Accumulated newest-first list + the server's total, for the load-more gate.
let _versions = [];
let _versionsTotal = 0;
// Re-entrancy guard: a double-click on "Show older versions" would compute
// the same offset twice and append duplicate rows.
let _versionsLoading = false;
// A fresh (non-append) refresh requested while a load was in flight —
// e.g. saveNote's post-save refresh racing a "Show older versions"
// append. Without queueing it, the guard above would silently drop the
// refresh and the panel would miss the just-created version.
let _versionsPendingRefresh = false;

async function loadVersionHistory(append = false) {
    if (_versionsLoading) {
        if (!append) _versionsPendingRefresh = true;
        return;
    }
    _versionsLoading = true;
    try {
        const offset = append ? _versions.length : 0;
        const response = await safeFetchWithAuth(
            `/notes/api/notes/${note.id}/versions?limit=${VERSIONS_PAGE_SIZE}&offset=${offset}`,
            { credentials: 'same-origin' }
        );

        const data = await response.json();

        if (data.success) {
            const page = data.versions || [];
            // eslint-disable-next-line require-atomic-updates -- _versionsLoading (single-writer guard) serializes this read-modify-write
            _versions = append ? _versions.concat(page) : page;
            const reportedTotal =
                typeof data.total === 'number' ? data.total : _versions.length;
            // On append, never GROW the total beyond the snapshot the pager is
            // paging over: a version created mid-paging would otherwise leave a
            // stale-positive "N more" button that fetches duplicate/empty rows
            //. A fresh (non-append) load adopts the current total.
            // eslint-disable-next-line require-atomic-updates -- serialized by _versionsLoading
            _versionsTotal = append
                ? Math.min(_versionsTotal, reportedTotal)
                : reportedTotal;
            renderVersionHistory(_versions);
        } else {
            renderSidebarLoadError('ldr-versions-list', "Couldn't load version history", () => loadVersionHistory());
        }
    } catch (error) {
        SafeLogger.error('Error loading version history:', error);
        renderSidebarLoadError('ldr-versions-list', "Couldn't load version history", () => loadVersionHistory());
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _versionsLoading = false;
        if (_versionsPendingRefresh) {
            _versionsPendingRefresh = false;
            // Deferred fresh-load queued while this request was in
            // flight; runs now that the guard is clear.
            loadVersionHistory(false);
        }
    }
}

/**
 * Render version history
 */
function renderVersionHistory(versions) {
    const container = document.getElementById('ldr-versions-list');
    if (!container) return;

    if (versions.length === 0) {
        container.innerHTML = `
            <div class="ldr-versions-empty" style="text-align: center; padding: 1.5rem; color: var(--text-muted);">
                <i class="fas fa-history" style="font-size: 1.5rem; margin-bottom: 0.5rem; opacity: 0.4;"></i>
                <p style="margin: 0;">No version history yet.</p>
            </div>
        `;
        return;
    }

    // "Show older versions" pager — rendered only while the server holds
    // more versions than the panel has loaded (route returns `total`).
    const remaining = _versionsTotal - versions.length;
    const loadMoreHtml = remaining > 0
        ? `<button type="button" class="ldr-version-action-btn ldr-versions-load-more" data-action="load-more-versions">
               <i class="fas fa-chevron-down"></i> Show older versions (${remaining} more)
           </button>`
        : '';

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: all interpolations use escapeHtml/numeric/formatDateTime
    container.innerHTML = versions.map(v => `
        <div class="ldr-version-item">
            <div class="ldr-version-item-info">
                <div class="ldr-version-item-title">
                    Version ${v.version_number}
                    <span class="ldr-version-type-badge ldr-${escapeHtml(v.change_type || 'unknown')}">${escapeHtml((v.change_type || 'unknown').replace('_', ' '))}</span>
                </div>
                <div class="ldr-version-item-meta">
                    <span><i class="fas fa-clock"></i> ${formatDateTime(v.created_at)}</span>
                </div>
                ${v.change_summary ? `<div class="ldr-version-item-summary">${escapeHtml(v.change_summary)}</div>` : ''}
            </div>
            <div class="ldr-version-item-actions">
                <button class="ldr-version-action-btn" data-action="view-version" data-version-id="${escapeHtml(v.id)}" title="View this version" aria-label="View version ${escapeHtml(String(v.version_number))}">
                    <i aria-hidden="true" class="fas fa-eye"></i>
                </button>
                <button class="ldr-version-action-btn" data-action="compare-version" data-version-id="${escapeHtml(v.id)}" title="Compare with current" aria-label="Compare version ${escapeHtml(String(v.version_number))} with current">
                    <i aria-hidden="true" class="fas fa-code-compare"></i>
                </button>
                <button class="ldr-version-action-btn ldr-restore" data-action="restore-version" data-version-id="${escapeHtml(v.id)}" title="Restore this version" aria-label="Restore version ${escapeHtml(String(v.version_number))}">
                    <i aria-hidden="true" class="fas fa-undo"></i>
                </button>
            </div>
        </div>
    `).join('') + loadMoreHtml;
}

/**
 * Compare a specific version with the current note content.
 * Fetches semantic diff from backend and renders a modal.
 */
// In-flight guard for the LLM-backed semantic diff. The compare button isn't
// disabled (the dispatcher only forwards the versionId), so without this a
// repeat click would fire a duplicate multi-second LLM diff.
let _diffInProgress = false;
// Same single-flight guard for the (idempotent) view-version GET: rapid
// clicks on the same row otherwise fire duplicate requests whose responses
// race to repaint the shared AI panel.
let _viewInProgress = false;
// Ownership token for #diffModal, mirroring _factCheckGeneration/
// _aiPanelGeneration. Bumped on entry AND whenever the modal is dismissed, so
// a diff that resolves after the user closed the modal can't reopen it.
let _diffGeneration = 0;
let _diffDismissWired = false;

async function compareVersion(versionId) {
    if (_diffInProgress) return;
    _diffInProgress = true;

    // Open the modal IMMEDIATELY so the user gets feedback for the multi-second
    // LLM diff, reusing the template's "Loading…" placeholder in
    // #diff-modal-body, then populate it once the fetch resolves.
    const modalEl = document.getElementById('diffModal');
    const body = document.getElementById('diff-modal-body');
    // Claim this diff's generation, and wire a one-time dismiss watcher so a
    // Close/backdrop/Esc during the in-flight diff invalidates it — otherwise
    // showDiffModal below would reopen the modal the user just closed (every
    // sibling AI action guards this via a generation token).
    _diffGeneration += 1;
    const myGen = _diffGeneration;
    if (modalEl && !_diffDismissWired) {
        _diffDismissWired = true;
        modalEl.addEventListener('hidden.bs.modal', () => {
            _diffGeneration += 1;
        });
    }
    if (body) {
        body.innerHTML =
            '<div class="ldr-factcheck-status"><div class="ldr-spinner" style="width:16px;height:16px;border-width:2px;"></div> Computing semantic diff…</div>';
    }
    if (modalEl) {
        bootstrap.Modal.getOrCreateInstance(modalEl).show();
    }

    try {
        const response = await safeFetchWithAuth(
            `/notes/api/notes/${note.id}/versions/semantic-diff?` +
            `version1=${encodeURIComponent(versionId)}&version2=current`,
            { credentials: 'same-origin' }
        );
        const data = await response.json();
        // The user dismissed the modal (or opened another) mid-diff — don't
        // repaint or reopen it.
        if (myGen !== _diffGeneration) return;
        if (!data.success) {
            if (body) {
                body.innerHTML = `<p>${escapeHtml(data.error || 'Failed to compute diff')}</p>`;
            }
            showNoteError(data.error || 'Failed to compute diff');
            return;
        }
        showDiffModal(versionId, data.diff || {});
    } catch (error) {
        SafeLogger.error('Error computing diff:', error);
        // Same dismissal guard as the success path.
        if (myGen !== _diffGeneration) return;
        if (body) {
            body.innerHTML = '<p>Failed to compute diff</p>';
        }
        showNoteError('Failed to compute diff');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _diffInProgress = false;
    }
}

/**
 * Render the semantic diff modal.
 */
function showDiffModal(versionId, diff) {
    const modalEl = document.getElementById('diffModal');
    const body = document.getElementById('diff-modal-body');
    if (!modalEl || !body) return;

    const section = (title, items, cls) => {
        if (!items || items.length === 0) return '';
        const list = items.map(
            item => `<li class="ldr-diff-${cls}">${escapeHtml(String(item))}</li>`
        ).join('');
        return `<h6>${title}</h6><ul class="ldr-diff-list">${list}</ul>`;
    };

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-04-15: all values are escapeHtml'd
    body.innerHTML = `
        <p class="ldr-diff-meta">Comparing version ${escapeHtml(versionId.slice(0, 8))} to current</p>
        ${diff.summary ? `<div class="ldr-diff-summary">${escapeHtml(diff.summary)}</div>` : ''}
        ${section('Added', diff.added, 'added')}
        ${section('Removed', diff.removed, 'removed')}
        ${section('Modified', diff.modified, 'modified')}
        ${(diff.unchanged) ? '<p class="ldr-diff-unchanged">No semantic changes.</p>' : ''}
    `;

    const modal = bootstrap.Modal.getOrCreateInstance(modalEl);
    modal.show();
}

/**
 * View a specific version
 */
async function viewVersion(versionId) {
    if (_viewInProgress) return;
    _viewInProgress = true;
    // Open the panel with a loading state up front (matching the sibling
    // version actions), so a racing AI action can't claim the panel mid-fetch.
    showAIResultsLoading('Version Details');
    const panelGen = _aiPanelGeneration;
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/versions/${versionId}`, {
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success && data.version) {
            // Dismissed mid-flight — don't reopen the panel (see runAiAction).
            if (_aiPanelDismissedSince(panelGen)) return;
            const v = data.version;
            const html = `
                <div style="margin-bottom: 1rem;">
                    <strong>Title:</strong> ${escapeHtml(v.title)}
                </div>
                <div style="margin-bottom: 1rem;">
                    <strong>Tags:</strong> ${(v.tags || []).map(t => `<span class="ldr-note-tag" style="display: inline-block; margin: 0.15rem;">${escapeHtml(t)}</span>`).join('') || 'None'}
                </div>
                <div style="margin-bottom: 1rem;">
                    <strong>Created:</strong> ${formatDateTime(v.created_at)}
                </div>
                <div style="margin-bottom: 0.5rem;">
                    <strong>Content:</strong>
                </div>
                <div style="background: var(--bg-tertiary); border: 1px solid var(--border-color); border-radius: 8px; padding: 1rem; max-height: 300px; overflow-y: auto; white-space: pre-wrap; font-family: monospace; font-size: 0.85rem;">
${escapeHtml(v.content)}
                </div>
                <div style="margin-top: 1rem; text-align: right;">
                    <button class="ldr-note-action-btn" data-action="restore-version" data-version-id="${escapeHtml(versionId)}">
                        <i class="fas fa-undo"></i> Restore This Version
                    </button>
                </div>
            `;
            showAIResults('Version Details', html);
        } else {
            // failAIAction, not a bare toast: the panel opened in a
            // loading state above and must CLOSE on failure rather than
            // stay stuck on "Working…" (the documented panel contract).
            failAIAction(data.error || 'Failed to load version', panelGen);
        }
    } catch (error) {
        SafeLogger.error('Error loading version:', error);
        failAIAction('Failed to load version', panelGen);
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _viewInProgress = false;
    }
}

/**
 * Restore a specific version
 */
let _restoreInProgress = false;

async function restoreVersion(versionId) {
    // The same versionId restore button appears in two places (version list +
    // AI panel), and nothing disables it, so guard against a double-trigger
    // interleaving two restores + loadNote reloads.
    if (_restoreInProgress) return;

    // A save PUT is in flight: it WILL commit the edited content server-side,
    // and whichever write resolves last wins — a restore confirmed now could
    // be silently overwritten by the stale pre-restore save (or its success
    // handler could stamp _lastSaved onto the restored note). Mirror
    // exitEditMode: refuse rather than lie.
    if (_saveInProgress) {
        if (window.ui) window.ui.showMessage('Still saving — please wait.', 'info');
        return;
    }

    // Restoring reloads the note from the server's snapshot of the CHOSEN
    // version; it does NOT save the live editor buffer first. The old copy
    // falsely promised "your current content will be saved as a new version",
    // so a mid-edit restore silently discarded unsaved edits. Tell the
    // truth, and explicitly warn when there ARE unsaved edits to lose.
    const message = (isEditMode && hasUnsavedChanges)
        ? 'Restore this version? Your unsaved edits will be discarded.'
        : 'Restore this version? This replaces the current note content with the selected version.';
    if (!confirm(message)) {
        return;
    }

    _restoreInProgress = true;
    try {
        const response = await safeFetchWithAuth(`/notes/api/notes/${note.id}/versions/${versionId}/restore`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            showSuccess('Version restored successfully');
            closeAIResults();
            // Reload the note to show restored content. ``loadNote``
            // already refreshes version history (along with backlinks,
            // outgoing links, etc.); awaiting it avoids a race where a
            // double-click would interleave two in-flight reloads on the
            // module-global ``note`` and DOM state.
            await loadNote(note.id);
        } else {
            showNoteError(data.error || 'Failed to restore version');
        }
    } catch (error) {
        SafeLogger.error('Error restoring version:', error);
        showNoteError('Failed to restore version');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _restoreInProgress = false;
    }
}

// ============================================================================
// Markdown Editor Toolbar
// ============================================================================

/**
 * Initialize the markdown toolbar functionality
 */
function initMarkdownToolbar() {
    const toolbar = document.getElementById('markdown-toolbar');
    const textarea = document.getElementById('note-content');
    const preview = document.getElementById('note-preview');
    const tabs = document.querySelectorAll('.ldr-editor-tab');

    if (!toolbar || !textarea || !preview || tabs.length === 0) {
        SafeLogger.warn('Markdown toolbar elements not found');
        return;
    }

    // Tab switching
    tabs.forEach(tab => {
        tab.addEventListener('click', () => {
            const mode = tab.dataset.mode;
            tabs.forEach(t => t.classList.remove('active'));
            tab.classList.add('active');

            if (mode === 'preview') {
                textarea.style.display = 'none';
                toolbar.style.display = 'none';
                preview.style.display = 'block';
                renderNoteContentInto(preview, textarea.value);
            } else {
                textarea.style.display = 'block';
                toolbar.style.display = 'flex';
                preview.style.display = 'none';
            }
        });
    });

    // Toolbar button clicks
    toolbar.querySelectorAll('.ldr-toolbar-btn').forEach(btn => {
        btn.addEventListener('click', (e) => {
            e.preventDefault();
            const format = btn.dataset.format;
            applyNoteMarkdownFormat(textarea, format);
        });
    });

    // Keyboard shortcuts. Compare case-insensitively: with CapsLock on
    // (or Shift held) e.key is 'B'/'I'/'K' and the shortcuts would no-op.
    textarea.addEventListener('keydown', (e) => {
        if (e.ctrlKey || e.metaKey) {
            const key = e.key.toLowerCase();
            if (key === 'b') {
                e.preventDefault();
                applyNoteMarkdownFormat(textarea, 'bold');
            }
            if (key === 'i') {
                e.preventDefault();
                applyNoteMarkdownFormat(textarea, 'italic');
            }
            if (key === 'k') {
                e.preventDefault();
                applyNoteMarkdownFormat(textarea, 'link');
            }
        }
    });
}

/**
 * Apply markdown formatting to selected text (delegates to shared formatting service)
 */
function applyNoteMarkdownFormat(textarea, format) {
    const extraFormats = {
        notelink: { before: '[[', after: ']]', placeholder: 'Note Title' },
    };
    window.formatting.applyMarkdownFormat(textarea, format, extraFormats);

    // Page-specific side-effects
    textarea.dispatchEvent(new Event('input'));
    hasUnsavedChanges = true;
}

/**
 * Basic markdown rendering (delegates to shared formatting service,
 * then adds [[wiki-link]] rendering specific to the detail page)
 */
function renderNoteMarkdown(text) {
    let html = window.formatting.basicMarkdownRender(text);

    // Note links [[Note Title]] — detail-page-specific. Uses the canonical
    // WIKILINK_TARGET (newline-excluding) so this render path agrees with the
    // stash in formatting.js and processWikiLinks; previously this site used
    // a diverged `[^\]]+` that allowed a target to cross a newline.
    html = html.replace(new RegExp('\\[\\[(' + window.formatting.WIKILINK_TARGET + ')\\]\\]', 'g'), '<span style="background: linear-gradient(135deg, rgba(110, 79, 246, 0.15) 0%, rgba(139, 92, 246, 0.1) 100%); padding: 0.15rem 0.4rem; border-radius: 4px; color: var(--accent-primary);">📝 $1</span>');

    return html;
}

// ============================================================================
// Wiki-Link [[ Autocomplete
// ============================================================================

let wikiLinkDropdown = null;
let wikiLinkActive = false;
let wikiLinkStartPos = -1;
let wikiLinkEndPos = -1;
let wikiLinkSelectedIndex = 0;
let wikiLinkResults = [];
let wikiLinkSearchTimeout = null;
let wikiLinkInitialized = false;
// In-flight controller for searchNotesForLinking. Each new search aborts
// the previous one so a slow response can't overwrite a fresher one
// (typing "qu" then "que" — the "qu" response must not win the race).
let wikiLinkSearchController = null;

/**
 * Detect [[ in textarea and trigger autocomplete
 */
function handleWikiLinkInput(textarea) {
    const cursorPos = textarea.selectionStart;
    const textBefore = textarea.value.substring(0, cursorPos);

    // Find last unclosed [[ before cursor
    const lastOpen = textBefore.lastIndexOf('[[');
    if (lastOpen === -1) {
        closeWikiLinkDropdown();
        return;
    }

    // Check if there's a ]] or newline between [[ and cursor
    const textAfterOpen = textBefore.substring(lastOpen + 2);
    if (textAfterOpen.includes(']]') || textAfterOpen.includes('\n')) {
        closeWikiLinkDropdown();
        return;
    }

    // We have an unclosed [[ — extract query
    const query = textAfterOpen;
    wikiLinkStartPos = lastOpen;
    wikiLinkEndPos = cursorPos;
    wikiLinkActive = true;

    showWikiLinkDropdown(textarea);

    // Invalidate the previous query immediately. Waiting for the replacement
    // debounce to expire leaves old results selectable and lets an older
    // response render against the new input for another 300 ms.
    wikiLinkResults = [];
    wikiLinkSelectedIndex = 0;
    clearTimeout(wikiLinkSearchTimeout);
    if (wikiLinkSearchController) {
        wikiLinkSearchController.abort();
        wikiLinkSearchController = null;
    }

    if (query.length === 0) {
        renderWikiLinkPlaceholder('Type to search notes...');
        return;
    }

    // Debounced search
    renderWikiLinkPlaceholder('Searching...');
    wikiLinkSearchTimeout = setTimeout(
        () => searchNotesForLinking(query),
        300
    );
}

/**
 * Search notes via API
 */
async function searchNotesForLinking(query) {
    // Cancel any in-flight search; we only care about the latest query.
    if (wikiLinkSearchController) {
        wikiLinkSearchController.abort();
    }
    const controller = new AbortController();
    wikiLinkSearchController = controller;

    try {
        const noteId = note ? note.id : '';
        const params = new URLSearchParams({
            query,
            limit: '8',
        });
        if (noteId) params.append('exclude_note_id', noteId);

        const response = await safeFetchWithAuth(
            `/notes/api/notes/search-for-linking?${params}`,
            { credentials: 'same-origin', signal: controller.signal }
        );
        const data = await response.json();

        // Bail if a newer search has already started; without this the
        // older response would race with the newer one to write
        // wikiLinkResults.
        if (wikiLinkSearchController !== controller) return;

        if (data.success && data.notes) {
            wikiLinkResults = data.notes;
            wikiLinkSelectedIndex = 0;
            if (wikiLinkResults.length === 0) {
                renderWikiLinkPlaceholder('No matching notes found');
            } else {
                renderWikiLinkItems(query);
            }
        } else {
            // 200-with-success:false (or a body missing `notes`): clear the
            // PREVIOUS query's results. The Enter/Tab insert gate only checks
            // wikiLinkResults.length, so leaving stale results in place would
            // insert a suggestion from an earlier query. Surface the failure
            // instead of silently keeping the old dropdown.
            wikiLinkResults = [];
            wikiLinkSelectedIndex = 0;
            renderWikiLinkPlaceholder('Could not load suggestions');
            SafeLogger.error(
                'Wiki-link search returned an unsuccessful response',
                data
            );
        }
    } catch (error) {
        if (error.name === 'AbortError') return;
        if (wikiLinkSearchController !== controller) return;
        wikiLinkResults = [];
        wikiLinkSelectedIndex = 0;
        renderWikiLinkPlaceholder('Could not load suggestions');
        SafeLogger.error('Wiki-link search error:', error);
    } finally {
        if (wikiLinkSearchController === controller) {
            wikiLinkSearchController = null;
        }
    }
}

/**
 * Show and position the dropdown
 */
function showWikiLinkDropdown(textarea) {
    if (!wikiLinkDropdown) {
        wikiLinkDropdown = document.createElement('div');
        wikiLinkDropdown.className = 'ldr-wiki-link-dropdown';
        // WCAG 1.3.1 / 4.1.2 — combobox listbox semantics so screen
        // readers announce option count and active item. The
        // accompanying textarea is given role="combobox",
        // aria-controls, aria-expanded, and aria-activedescendant
        // updates in initWikiLinkAutocomplete + updateWikiLinkSelection.
        wikiLinkDropdown.id = 'ldr-wiki-link-listbox';
        wikiLinkDropdown.setAttribute('role', 'listbox');
        wikiLinkDropdown.setAttribute('aria-label', 'Linked note suggestions');
        document.body.appendChild(wikiLinkDropdown);
    }

    const rect = textarea.getBoundingClientRect();
    const dropdownWidth = Math.min(rect.width, 400);

    wikiLinkDropdown.style.left = `${rect.left}px`;
    wikiLinkDropdown.style.top = `${rect.bottom + 4}px`;
    wikiLinkDropdown.style.width = `${dropdownWidth}px`;
    wikiLinkDropdown.style.display = 'block';

    // Open state announced via aria-expanded on the textarea combobox.
    textarea.setAttribute('aria-expanded', 'true');
}

/**
 * Render search results in dropdown
 */
function renderWikiLinkItems(query) {
    if (!wikiLinkDropdown) return;

    const lowerQuery = query.toLowerCase();
    // eslint-disable-next-line no-unsanitized/property -- audited: all interpolations use escapeHtml per segment, highlight wraps escaped text
    wikiLinkDropdown.innerHTML = wikiLinkResults
        .map((n, i) => {
            const rawTitle = n.title || 'Untitled';
            // Highlight on raw title, then escape each segment
            const idx = rawTitle.toLowerCase().indexOf(lowerQuery);
            let displayTitle;
            if (idx >= 0) {
                const before = escapeHtml(rawTitle.substring(0, idx));
                const match = escapeHtml(rawTitle.substring(idx, idx + query.length));
                const after = escapeHtml(rawTitle.substring(idx + query.length));
                displayTitle =
                    `${before}<span class="ldr-highlight">${match}</span>${after}`;
            } else {
                displayTitle = escapeHtml(rawTitle);
            }
            const activeClass =
                i === wikiLinkSelectedIndex ? ' active' : '';
            const isSelected = i === wikiLinkSelectedIndex ? 'true' : 'false';
            // role="option" + aria-selected + a stable id so the textarea's
            // aria-activedescendant can point to the active item.
            return `<div class="ldr-wiki-link-item${activeClass}" role="option" id="ldr-wiki-link-opt-${i}" aria-selected="${isSelected}" data-index="${i}">
                <div class="ldr-wiki-link-item-title">${displayTitle}</div>
            </div>`;
        })
        .join('');

    // Reflect the initial selection (index 0) on the textarea so SR
    // announces the active option as soon as the dropdown opens.
    const textarea = document.getElementById('note-content');
    if (textarea && wikiLinkResults.length > 0) {
        textarea.setAttribute(
            'aria-activedescendant',
            `ldr-wiki-link-opt-${wikiLinkSelectedIndex}`,
        );
    }

    // Announce the suggestion count via the visually-hidden live region.
    // WCAG combobox-pattern recommendation — without this, SR users have
    // no signal that the popup populated and how many items it contains
    // before they start arrowing.
    const statusEl = document.getElementById('ldr-wiki-link-status');
    if (statusEl) {
        const count = wikiLinkResults.length;
        statusEl.textContent = count > 0
            ? `${count} ${count === 1 ? 'suggestion' : 'suggestions'} available. Use up and down arrows to navigate.`
            : 'No matching notes.';
    }

    // Attach click handlers
    wikiLinkDropdown
        .querySelectorAll('.ldr-wiki-link-item')
        .forEach((item) => {
            item.addEventListener('mousedown', (e) => {
                e.preventDefault(); // Prevent blur
                const idx = parseInt(
                    item.getAttribute('data-index'),
                    10
                );
                selectWikiLinkItem(idx);
            });
        });
}

/**
 * Show placeholder text in dropdown
 */
function renderWikiLinkPlaceholder(text) {
    if (!wikiLinkDropdown) return;
    wikiLinkDropdown.innerHTML =
        `<div class="ldr-wiki-link-placeholder">${escapeHtml(text)}</div>`;
}

/**
 * Insert selected note link into textarea
 */
function selectWikiLinkItem(index) {
    const result = wikiLinkResults[index];
    if (!result) return;

    const textarea = document.getElementById('note-content');
    if (!textarea) return;

    // Strip `[` / `]` AND drop any `|` alias portion before wrapping, to
    // match the server-side sanitisation in accept_suggested_link
    // (`.replace("["/"]").partition("|")[0]`). `[`/`]`: a title like
    // `foo]]bar` would otherwise tokenise as `[[foo]]`. `|`: the save-path
    // parser partitions the link text on the FIRST pipe and resolves only
    // the pre-pipe part, so inserting `[[Cost | Benefit]]` verbatim would
    // resolve against "Cost" (wrong note / none) instead of the real title.
    const rawTitle = result.title || 'Untitled';
    const title = rawTitle.replace(/[[\]]/g, '').split('|')[0].trim();
    const replacement = `[[${title}]]`;
    // Replace exactly the [[query span. wikiLinkEndPos is kept current by
    // handleWikiLinkInput on every input event (typing always fires input
    // before this handler can run), so it is never stale from typing.
    // selectionEnd, by contrast, can sit beyond the query after a non-input
    // caret move (End/ArrowRight/mouse click) — taking the max with it
    // silently deleted the text between the query and the relocated caret.
    const before = textarea.value.substring(0, wikiLinkStartPos);
    const after = textarea.value.substring(wikiLinkEndPos);

    textarea.value = before + replacement + after;
    const newPos = wikiLinkStartPos + replacement.length;
    textarea.focus();
    textarea.setSelectionRange(newPos, newPos);

    hasUnsavedChanges = true;
    closeWikiLinkDropdown();
}

// True iff the caret is still somewhere Enter/Tab may reasonably be read as
// "accept this suggestion" rather than "ordinary newline/tab at the new
// caret". A non-input caret move (a mouse click elsewhere in the SAME
// textarea, or Home/End/ArrowLeft/ArrowRight — none of which fire 'input', so
// they never refresh wikiLinkStartPos/EndPos or close the dropdown) leaves
// the autocomplete active but its span stale. Two cases are honored:
//   1. The caret sits exactly where typing left it (wikiLinkEndPos) — the
//      common typing-then-Enter/Tab path.
//   2. The caret sits at the END of the CURRENT LINE, on the same line the
//      query was opened on — e.g. the query was typed with trailing text
//      already after it on the line (or ahead of it, via Home before
//      typing), and the user pressed End to jump past that trailing text
//      before accepting. selectWikiLinkItem always splices the replacement
//      into the tracked [[query span regardless of where the caret ends up,
//      so honoring End here only widens WHEN we accept, not WHERE the link
//      is inserted — the trailing text is never touched.
// Anything else (caret parked mid-way into unrelated trailing text, an
// active selection, or moved to a different line entirely) is treated as the
// user having moved on to edit elsewhere, and Enter/Tab is left alone.
function wikiLinkCaretStillAtQuery() {
    const textarea = document.getElementById('note-content');
    if (!textarea) return false;
    const { selectionStart, selectionEnd, value } = textarea;
    if (selectionStart !== selectionEnd) return false;
    if (selectionStart === wikiLinkEndPos) return true;
    const nextNewline = value.indexOf('\n', wikiLinkEndPos);
    const currentLineEnd = nextNewline === -1 ? value.length : nextNewline;
    return selectionStart === currentLineEnd;
}

/**
 * Handle keyboard navigation in dropdown
 */
function handleWikiLinkKeydown(event) {
    if (!wikiLinkActive) return;

    // Keys pressed while an IME is composing (Enter to confirm a conversion,
    // arrows to pick a candidate) belong to the IME, not the dropdown. Safari
    // fires compositionend before the confirming keydown, so isComposing is
    // already false there and keyCode 229 is the only signal.
    if (event.isComposing || event.keyCode === 229) return;

    if (event.key === 'ArrowDown') {
        event.preventDefault();
        if (wikiLinkResults.length > 0) {
            wikiLinkSelectedIndex =
                (wikiLinkSelectedIndex + 1) % wikiLinkResults.length;
            updateWikiLinkSelection();
        }
    } else if (event.key === 'ArrowUp') {
        event.preventDefault();
        if (wikiLinkResults.length > 0) {
            wikiLinkSelectedIndex =
                (wikiLinkSelectedIndex - 1 + wikiLinkResults.length) %
                wikiLinkResults.length;
            updateWikiLinkSelection();
        }
    } else if (event.key === 'Enter' && wikiLinkResults.length > 0) {
        if (!wikiLinkCaretStillAtQuery()) {
            // Caret moved off the query since the dropdown opened — treat
            // Enter as an ordinary newline, not a suggestion accept.
            closeWikiLinkDropdown();
            return;
        }
        event.preventDefault();
        selectWikiLinkItem(wikiLinkSelectedIndex);
    } else if (event.key === 'Tab' && wikiLinkResults.length > 0) {
        // Tab accepts the active suggestion (standard combobox UX).
        // Without this, Tab moves focus out of the textarea while the
        // dropdown is still visible for 200ms (the blur-close timer),
        // leaving the autocomplete state inconsistent.
        if (!wikiLinkCaretStillAtQuery()) {
            // Caret moved off the query — let Tab move focus natively.
            closeWikiLinkDropdown();
            return;
        }
        event.preventDefault();
        selectWikiLinkItem(wikiLinkSelectedIndex);
    } else if (event.key === 'Escape') {
        event.preventDefault();
        closeWikiLinkDropdown();
    }
}

/**
 * Update visual selection in dropdown
 */
function updateWikiLinkSelection() {
    if (!wikiLinkDropdown) return;
    const items = wikiLinkDropdown.querySelectorAll('.ldr-wiki-link-item');
    items.forEach((item, i) => {
        const isActive = i === wikiLinkSelectedIndex;
        item.classList.toggle('active', isActive);
        // Keep aria-selected in sync so SR announces the active option.
        item.setAttribute('aria-selected', isActive ? 'true' : 'false');
    });
    // Point the textarea's aria-activedescendant at the active item so
    // screen readers track keyboard nav without focus actually moving.
    const textarea = document.getElementById('note-content');
    if (textarea && items[wikiLinkSelectedIndex]) {
        textarea.setAttribute(
            'aria-activedescendant',
            items[wikiLinkSelectedIndex].id || `ldr-wiki-link-opt-${wikiLinkSelectedIndex}`,
        );
    }
    // Scroll into view
    if (items[wikiLinkSelectedIndex]) {
        items[wikiLinkSelectedIndex].scrollIntoView({ block: 'nearest' });
    }
}

/**
 * Close and cleanup the dropdown
 */
function closeWikiLinkDropdown() {
    if (wikiLinkDropdown) {
        wikiLinkDropdown.style.display = 'none';
    }
    wikiLinkActive = false;
    wikiLinkStartPos = -1;
    wikiLinkEndPos = -1;
    wikiLinkSelectedIndex = 0;
    wikiLinkResults = [];
    clearTimeout(wikiLinkSearchTimeout);
    // Abort any in-flight search — a late response after close would
    // otherwise repopulate wikiLinkResults of a closed dropdown, re-arming
    // the invisible-insert bug for the next bare '[['.
    if (wikiLinkSearchController) {
        wikiLinkSearchController.abort();
        wikiLinkSearchController = null;
    }
    // Announce closed state to SR and clear the activedescendant
    // pointer so it doesn't dangle at a hidden element.
    const textarea = document.getElementById('note-content');
    if (textarea) {
        textarea.setAttribute('aria-expanded', 'false');
        textarea.removeAttribute('aria-activedescendant');
    }
    // Clear the live region so the prior suggestion count doesn't
    // linger in SR history when the dropdown closes.
    const statusEl = document.getElementById('ldr-wiki-link-status');
    if (statusEl) statusEl.textContent = '';
}

/**
 * Wire up wiki-link autocomplete on the editor textarea
 */
function initWikiLinkAutocomplete() {
    if (wikiLinkInitialized) return;
    wikiLinkInitialized = true;

    const textarea = document.getElementById('note-content');
    if (!textarea) return;

    // WCAG 1.3.1 / 4.1.2 — combobox semantics on the textarea so the
    // wiki-link autocomplete is discoverable to AT. The dropdown
    // listbox is created lazily in showWikiLinkDropdown and assigned
    // id="ldr-wiki-link-listbox" so this aria-controls reference
    // resolves once the dropdown exists.
    textarea.setAttribute('role', 'combobox');
    textarea.setAttribute('aria-autocomplete', 'list');
    textarea.setAttribute('aria-haspopup', 'listbox');
    textarea.setAttribute('aria-expanded', 'false');
    textarea.setAttribute('aria-controls', 'ldr-wiki-link-listbox');

    textarea.addEventListener('input', () => handleWikiLinkInput(textarea));
    textarea.addEventListener('keydown', (e) => {
        if (wikiLinkActive) handleWikiLinkKeydown(e);
    });
    textarea.addEventListener('blur', () => {
        // Delay to allow mousedown on dropdown items
        setTimeout(closeWikiLinkDropdown, 200);
    });

    // Dismiss editor hint
    const dismissBtn = document.getElementById('dismiss-editor-hint');
    if (dismissBtn) {
        dismissBtn.addEventListener('click', () => {
            const hint = document.getElementById('editor-hint');
            if (hint) hint.style.display = 'none';
        });
    }
}

// ============================================================================
// Test-only export
//
// This module is a flat classic script with no module exports, so Vitest
// (which imports it under happy-dom) cannot otherwise reach these
// module-private functions and their concurrency state. We surface a minimal
// hook — getters for the deferred-refresh queue flags, the wiki-link
// last-writer-wins controller, and the scroll-spy observer — plus a `setNote`
// helper so tests can stand in a note id without running the full page load.
//
// Production code NEVER touches this object. Gated on __VITEST_TEST__ (set
// by the test harness before import) so the hook does not ship in the
// browser at all — matching notes.js / unified_search.js.
// ============================================================================
if (typeof window !== 'undefined' && window.__VITEST_TEST__) {
    window.__noteDetailTest = {
        // Deferred-refresh queue (version history)
        loadVersionHistory,
        renderVersionHistory,
        getVersionsLoading: () => _versionsLoading,
        getPendingRefresh: () => _versionsPendingRefresh,
        getVersions: () => _versions,
        getVersionsTotal: () => _versionsTotal,
        setVersions: (v) => { _versions = v; },
        // Wiki-link autocomplete last-writer-wins
        searchNotesForLinking,
        getWikiLinkResults: () => wikiLinkResults,
        getWikiLinkController: () => wikiLinkSearchController,
        resetWikiLink: () => {
            wikiLinkResults = [];
            wikiLinkSearchController = null;
            wikiLinkSelectedIndex = 0;
        },
        // Scroll-spy observer lifecycle
        setupScrollSpy,
        getTocObserver: () => _tocObserver,
        // Shared sidebar backlink/related card renderer
        renderBacklinkCard,
        // Fact-check verdict renderer (source-link safety degradation)
        renderVerdicts,
        // View-version single-flight guard
        viewVersion,
        // Semantic-diff modal (compare version to current)
        compareVersion,
        resetDiffState: () => {
            _diffInProgress = false;
            _diffGeneration = 0;
            _diffDismissWired = false;
        },
        // Passage-selection scoping
        _selectionWithinNote,
        // Pure / content helpers
        countWords,
        generateTableOfContents,
        processWikiLinks,
        // Index-for-search (RAG) flow
        indexNoteToCollection,
        // Delete + collection-membership mutations (validation + guard)
        deleteNote,
        addToCollection,
        removeFromCollection,
        setCollectionModal: (m) => { collectionModal = m; },
        // Wiki-link autocomplete keyboard nav
        handleWikiLinkKeydown,
        getWikiLinkSelectedIndex: () => wikiLinkSelectedIndex,
        // Wiki-link autocomplete: input debounce + dropdown injection
        // (searchNotesForLinking / getWikiLinkResults are exposed above).
        handleWikiLinkInput,
        setWikiLinkDropdown: (el) => { wikiLinkDropdown = el; },
        setWikiLinkState: ({ active = true, results = [], index = 0, start = 0, end = 0 } = {}) => {
            wikiLinkActive = active;
            wikiLinkResults = results;
            wikiLinkSelectedIndex = index;
            wikiLinkStartPos = start;
            wikiLinkEndPos = end;
        },
        // AI handlers — characterization coverage (safety net) + guard
        resetAiActionGuards: () => _aiActionsInFlight.clear(),
        // Panel dismissal (reopen-after-dismiss guard, _aiPanelGeneration)
        closeAIResults,
        summarizeNote,
        suggestTags,
        // Accept-suggested-link flow (mark-linked-before-refresh robustness)
        acceptSuggestedLink,
        acceptUnlinkedMention,
        // Fact-check entry: re-entrancy guard + start-failure (no poll)
        verifyNote,
        // Grade-failure render (502 softening + retry affordance)
        renderGradeFailure,
        // Fact-check status render + note-collections (is_indexed derivation)
        showFactCheckStatus,
        loadNoteCollections,
        resetVerifyState: () => { _verifyInProgress = false; _stopFactCheckPoll(); },
        // Fact-check status poll -> grade -> renderVerdicts (fake-timer driven)
        _startFactCheckPoll,
        // Sidebar loaders (fetch -> render / load-error routing)
        loadBacklinks,
        loadOutgoingLinks,
        extractResearchQuestions,
        findUnlinkedMentions,
        extractKeyConcepts,
        findSimilarNotes,
        findSimilarPassages,
        suggestLinks,
        // Related-notes sidebar loader (error-vs-empty branch coverage)
        loadRelatedNotesForSidebar,
        getRelatedNotesLoadState: () => _relatedNotesLoadState,
        // Stand in a note without running the full page load
        // Standing in a fresh note also clears the partial-save baseline
        // (renderNote does the same in production), so a prior test's save
        // can't make the next test's identical-state save skip its PUT.
        setNote: (n) => { note = n; _lastSaved = null; },
        getNote: () => note,
        resetNote: () => { note = null; },
        // Start-research-from-note: link-failure notice
        startResearchFromNote,
        setResearchModal: (m) => { researchModal = m; },
        // Restore-version unsaved-edits confirm copy
        restoreVersion,
        setEditState: ({ inEdit = false, unsaved = false } = {}) => {
            isEditMode = inEdit;
            hasUnsavedChanges = unsaved;
        },
        getEditState: () => ({
            inEdit: isEditMode,
            unsaved: hasUnsavedChanges,
        }),
        // Read-mode save re-syncs the editor from `note` before PUT
        togglePin,
        saveNote,
        // Edit-mode lifecycle — Cancel reverts in-edit tag mutations
        enterEditMode,
        exitEditMode,
        // Manual tag add/remove handlers
        handleTagKeypress,
        removeTag,
        // Read-mode suggested-tag add + research-item collapse (both exercised
        // for their in-flight/revert races).
        addSuggestedTag,
        toggleResearchCollapsed,
        // Drag/drop research ordering and serialized mutation ownership
        renderNoteResearch,
        persistResearchOrder,
        setNoteResearch: (items) => { noteResearch = items; },
        getNoteResearch: () => noteResearch,
        bindEditorRefs: () => {
            titleInput = document.getElementById('ldr-note-title');
            contentEditor = document.getElementById('note-content');
        },
    };
}