function formatMessage(message) {
    function escapeHTML(text) {
        return text
            .replace(/&/g, "&amp;")
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;")
            .replace(/"/g, "&quot;")
            .replace(/'/g, "&#039;");
    }

    function processCodeBlock(language, code) {
        const detectedLanguage = language && language.trim() !== "" ? language : "markup";
        const preservedFormatting = code.trim().split('\n').map(line => escapeHTML(line)).join('\n');

        const highlightedCode = Prism.languages[detectedLanguage]
            ? Prism.highlight(preservedFormatting, Prism.languages[detectedLanguage], detectedLanguage)
            : preservedFormatting;

        return `
        <div class="code-container">
          <div class="code-header">
            <span class="language-name">${escapeHTML(language) || "code"}</span>
            <div class="code-actions">
              <button class="copy-btn" onclick="copyCode(this)">📋 Copy</button>
              <button class="fullscreen-btn" onclick="openFullscreen(this)">⛶ Fullscreen</button>
            </div>
          </div>
          <pre><code class="language-${escapeHTML(language) || 'markup'}">${highlightedCode}</code></pre>
        </div>`;
    }

    function processLists(content) {
        return content
            .replace(/^([ \t]*)([-*]|\d+\.)\s+(.*)$/gm, (match, indent, marker, listContent) => {
                const indentLevel = indent.length / 4; // Assume 1 tab = 4 spaces
                return `<li style="margin-left: ${indentLevel * 20}px;">${listContent.trim()}</li>`;
            })
            .replace(/(<li>.*?<\/li>)+/gs, match =>
                `<${/\d+\./.test(match) ? 'ol' : 'ul'}>${match}</${/\d+\./.test(match) ? 'ol' : 'ul'}>`
            );
    }

    return message
        .replace(/```(\w+)?\n([\s\S]*?)```/g, (match, lang, code) => processCodeBlock(lang, code))
        .replace(/<urimage>([\s\S]+?)<\/urimage>/g, (_, content) => {
            const cleanedContent = content.replace(/\n+/g, ' ').trim();
            return `<img src="https://image.pollinations.ai/prompt/${escapeHTML(cleanedContent)}?width=1024&height=1024&enhance=false&nologo=true&privacy=true&safe=false&nsfw=true&isChild=false" 
                         alt="${escapeHTML(cleanedContent)}" 
                         style="width: 100%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: 3px;" />`;
        })
        .split(/\n\n+/)
        .map((block) => {
            if (block.startsWith('<div class="code-container">')) {
                return block;
            }

            return block
                .replace(/`([^`\n]+)`/g, (_, inlineCode) => `<code>${escapeHTML(inlineCode)}</code>`)
                .replace(/^(#{1,6})\s*(.*?)$/gm, (match, hashes, content) => {
                    const level = hashes.length;
                    return `<h${level}>${escapeHTML(content.trim())}</h${level}>`;
                })
                .replace(/^([ \t]*[-*\d]+\.?)\s+(.*)$/gm, processLists)
                .replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
                .replace(/(^|[^*])\*(.*?)\*(?!\*)/g, "$1<em>$2</em>")
                .replace(/(?:^|\n\n)([^<\n].*?)(?=\n\n|$)/gs, (match, paragraph) => `<p>${paragraph.trim()}</p>`);
        })
        .join("\n");
  }