[ai_container])
                        .catch(err => console.warn(`MathJax processing error for ${sender} message:`, err));
                }
                g_mermaid.initialize();
            }

            function ai_append_thought_chunk(chunk) {
                const isAtBottom = (window.innerHeight + window.scrollY) >= document.body.offsetHeight - 50;

                prepare_for_ai_turn();
                if (!is_thinking) {
                    is_thinking = true;
                    toggleButton.st