<!-- Header -->
<header class="bg-white border-b border-slate-200 px-6 py-4 flex items-center justify-between shadow-xs">
    <div class="flex items-center space-x-3">
        <div class="bg-indigo-600 text-white p-2 rounded-lg shadow-sm">
            <i class="fa-solid fa-layer-group text-lg"></i>
        </div>
        <div>
            <h1 class="font-bold text-lg text-slate-900 leading-tight">Workspace Studio</h1>
            <p class="text-xs text-slate-500">Collaborative environment & editor</p>
        </div>
    </div>
    <div class="flex items-center space-x-3">
        <button id="save-btn" class="bg-indigo-600 hover:bg-indigo-700 text-white text-sm font-medium px-4 py-2 rounded-lg transition-colors shadow-xs flex items-center space-x-2">
            <i class="fa-solid fa-floppy-disk"></i>
            <span>Save Changes</span>
        </button>
    </div>
</header>

<!-- Main Workspace Layout -->
<main class="flex-1 flex overflow-hidden">
    
    <!-- Left Pane: Chat / Assistant Panel -->
    <section class="w-1/3 min-w-[320px] bg-white border-r border-slate-200 flex flex-col">
        <div class="p-4 border-b border-slate-100 flex items-center justify-between">
            <h2 class="font-semibold text-slate-700 text-sm flex items-center space-x-2">
                <i class="fa-solid fa-comments text-indigo-500"></i>
                <span>Assistant & Chatbox</span>
            </h2>
            <span class="text-xs bg-indigo-50 text-indigo-600 px-2 py-0.5 rounded-full font-medium">Active</span>
        </div>

        <!-- Chat Message History -->
        <div id="chat-messages" class="flex-1 p-4 overflow-y-auto space-y-4 text-sm bg-slate-50/50">
            <div class="flex items-start space-x-3">
                <div class="bg-indigo-600 text-white w-7 h-7 rounded-full flex items-center justify-center shrink-0 font-bold text-xs">AI</div>
                <div class="bg-white border border-slate-200 p-3 rounded-2xl rounded-tl-none shadow-xs text-slate-700">
                    Hello! I am ready to help you edit and collaborate on your document on the right. How can we improve it today?
                </div>
            </div>
        </div>

        <!-- Chat Input Form -->
        <div class="p-4 bg-white border-t border-slate-200">
            <form id="chat-form" class="flex items-center space-x-2">
                <input type="text" id="chat-input" placeholder="Ask for feedback or edits..." class="flex-1 bg-slate-100 border border-slate-200 rounded-xl px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:bg-white transition-all">
                <button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white w-10 h-10 rounded-xl flex items-center justify-center transition-colors shadow-xs shrink-0">
                    <i class="fa-solid fa-paper-plane text-xs"></i>
                </button>
            </form>
        </div>
    </section>

    <!-- Right Pane: Document Editor -->
    <section class="flex-1 bg-slate-100 flex flex-col overflow-hidden">
        <div class="bg-white border-b border-slate-200 px-6 py-3 flex items-center justify-between shadow-xs">
            <div class="flex items-center space-x-2 text-slate-700 text-sm font-medium">
                <i class="fa-regular fa-file-lines text-slate-400"></i>
                <span id="doc-title-display">Document Editor</span>
            </div>
            <div class="text-xs text-slate-400 flex items-center space-x-1">
                <i class="fa-solid fa-circle text-[8px] text-emerald-500"></i>
                <span>Synced</span>
            </div>
        </div>

        <!-- Editor Textarea Area -->
        <div class="flex-1 p-6 overflow-y-auto flex justify-center">
            <div class="w-full max-w-3xl bg-white shadow-sm border border-slate-200 rounded-2xl p-8 flex flex-col h-full min-h-[500px]">
                <textarea id="document-editor" class="w-full flex-1 resize-none focus:outline-none text-slate-700 leading-relaxed font-normal text-base" placeholder="Start typing your document contents here...">Welcome to your collaborative workspace! 
You can write or paste your text in this editor. Use the chatbox on the left to request critiques, feedback, proofreading, or suggestions.All mathematical or scientific notation can be formatted using LaTeX-style syntax like $E = mc^2$ or display formulas $$\int_{0}^{\infty} e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$.


</main>

<!-- Client-side Interactive Logic -->
<script>
    const chatForm = document.getElementById('chat-form');
    const chatInput = document.getElementById('chat-input');
    const chatMessages = document.getElementById('chat-messages');
    const documentEditor = document.getElementById('document-editor');
    const saveBtn = document.getElementById('save-btn');

    chatForm.addEventListener('submit', (e) => {
        e.preventDefault();
        const text = chatInput.value.trim();
        if (!text) return;

        // Append user message
        const userMsgDiv = document.createElement('div');
        userMsgDiv.className = 'flex items-start space-x-3 justify-end';
        userMsgDiv.innerHTML = `
            <div class="bg-slate-800 text-white p-3 rounded-2xl rounded-tr-none shadow-xs text-slate-100 max-w-[80%]">
                ${escapeHtml(text)}
            </div>
            <div class="bg-slate-700 text-white w-7 h-7 rounded-full flex items-center justify-center shrink-0 font-bold text-xs">U</div>
        `;
        chatMessages.appendChild(userMsgDiv);
        chatInput.value = '';
        chatMessages.scrollTop = chatMessages.scrollHeight;

        // Simulate AI response after short delay
        setTimeout(() => {
            const aiMsgDiv = document.createElement('div');
            aiMsgDiv.className = 'flex items-start space-x-3';
            aiMsgDiv.innerHTML = `
                <div class="bg-indigo-600 text-white w-7 h-7 rounded-full flex items-center justify-center shrink-0 font-bold text-xs">AI</div>
                <div class="bg-white border border-slate-200 p-3 rounded-2xl rounded-tl-none shadow-xs text-slate-700 max-w-[80%]">
                    I received your message! Let's work on improving your document together. Feel free to ask for specific critiques or edits.
                </div>
            `;
            chatMessages.appendChild(aiMsgDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }, 600);
    });

    saveBtn.addEventListener('click', () => {
        const originalText = saveBtn.innerHTML;
        saveBtn.innerHTML = `<i class="fa-solid fa-check"></i><span>Saved!</span>`;
        saveBtn.classList.remove('bg-indigo-600', 'hover:bg-indigo-700');
        saveBtn.classList.add('bg-emerald-600', 'hover:bg-emerald-700');
        
        setTimeout(() => {
            saveBtn.innerHTML = originalText;
            saveBtn.classList.remove('bg-emerald-600', 'hover:bg-emerald-700');
            saveBtn.classList.add('bg-indigo-600', 'hover:bg-indigo-700');
        }, 2000);
    });

    function escapeHtml(str) {
        return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
    }
</script>
