{{define "header-left"}}
    <div class="flex items-center gap-3">
      <a href="/app/projects" class="text-2xl font-bold text-white tracking-tight"><span class="text-[#c9a84c]">Deal</span>space</a>
      <span class="text-white/20">/</span>
      <a id="backToProject" href="/app/projects" class="text-sm text-[#b0bec5] hover:text-white transition">Project</a>
      <span class="text-white/20">/</span>
      <span id="reqRef" class="text-sm text-white font-medium">—</span>
    </div>
{{end}}

{{define "content"}}
  <div class="max-w-4xl mx-auto px-6 py-8 space-y-6">

    <!-- REQUEST CARD -->
    <div class="rounded-xl overflow-hidden" style="background:var(--ds-sf);border:1px solid var(--ds-bd)">
      <!-- Section header with meta -->
      <div class="px-6 pt-5 pb-4 border-b" style="border-color:var(--ds-bd)">
        <div class="flex items-start justify-between gap-4">
          <div class="flex flex-wrap items-center gap-2">
            <span id="reqItemBadge" class="hidden px-2.5 py-1 rounded text-xs font-semibold tracking-wide" style="background:var(--ds-ac);color:var(--ds-act)"></span>
            <span id="reqStatus" class="px-2.5 py-1 rounded-full text-xs font-medium capitalize"></span>
            <span id="reqPriority" class="hidden px-2.5 py-1 rounded-full text-xs font-medium"></span>
            <span id="reqDue" class="hidden text-xs font-medium" style="color:var(--ds-tx3)"></span>
          </div>
          <button id="editReqBtn" onclick="startEditRequest()" class="shrink-0 p-1.5 rounded hover:bg-white/[0.08] transition" style="color:var(--ds-tx3)" title="Edit request">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/></svg>
          </button>
        </div>
      </div>
      <!-- Request body -->
      <div class="px-6 py-5">
        <p id="reqTitle" class="text-base leading-relaxed font-medium" style="color:var(--ds-tx)">Loading...</p>
        <p id="reqDesc" class="text-sm leading-relaxed mt-3" style="color:var(--ds-tx2)"></p>
        <!-- Edit mode -->
        <div id="reqEditMode" class="hidden mt-5 space-y-3 p-4 rounded-lg" style="background:var(--ds-bg);border:1px solid var(--ds-bd)">
          <div><label class="block text-xs font-medium mb-1.5" style="color:var(--ds-tx3)">Request text</label>
            <textarea id="editReqTitle" rows="4" class="w-full px-3 py-2 rounded text-sm focus:outline-none" style="background:var(--ds-sf);border:1px solid var(--ds-bd);color:var(--ds-tx)"></textarea></div>
          <div class="flex gap-3">
            <div class="flex-1"><label class="block text-xs font-medium mb-1.5" style="color:var(--ds-tx3)">Priority</label>
              <select id="editReqPriority" class="w-full px-3 py-2 rounded text-sm focus:outline-none" style="background:var(--ds-sf);border:1px solid var(--ds-bd);color:var(--ds-tx)">
                <option value="critical">Critical</option><option value="high">High</option><option value="medium">Medium</option><option value="low">Low</option>
              </select></div>
            <div class="flex-1"><label class="block text-xs font-medium mb-1.5" style="color:var(--ds-tx3)">Status</label>
              <select id="editReqStatus" class="w-full px-3 py-2 rounded text-sm focus:outline-none" style="background:var(--ds-sf);border:1px solid var(--ds-bd);color:var(--ds-tx)">
                <option value="open">Open</option><option value="in_process">In Process</option><option value="partial">Partial</option><option value="complete">Complete</option>
              </select></div>
          </div>
          <div><label class="block text-xs font-medium mb-1.5" style="color:var(--ds-tx3)">Description (optional)</label>
            <textarea id="editReqDesc" rows="2" class="w-full px-3 py-2 rounded text-sm focus:outline-none" style="background:var(--ds-sf);border:1px solid var(--ds-bd);color:var(--ds-tx)"></textarea></div>
          <div class="flex gap-2 mt-1">
            <button onclick="saveEditRequestDetail()" class="px-4 py-2 rounded text-sm font-semibold transition" style="background:var(--ds-ac);color:var(--ds-act)">Save</button>
            <button onclick="cancelEditRequestDetail()" class="px-4 py-2 rounded text-sm transition" style="background:var(--ds-hv);color:var(--ds-tx)">Cancel</button>
          </div>
        </div>
      </div>
    </div>

    <!-- RESPONSE CARD -->
    <div class="rounded-xl overflow-hidden" style="background:var(--ds-sf);border:1px solid var(--ds-bd)">
      <div class="px-6 pt-4 pb-3 border-b flex items-center justify-between" style="border-color:var(--ds-bd)">
        <span class="text-xs font-semibold uppercase tracking-wider" style="color:var(--ds-tx3)">Files &amp; Response</span>
        <div id="answeredBanner" class="hidden px-3 py-1 rounded-full text-xs font-semibold bg-green-500/15 text-green-300">&#10003; Answered</div>
      </div>
      <div class="px-6 py-4">
        <div id="answers" class="space-y-2 mb-4"></div>
        <!-- empty state shown by JS if no answers -->
        <div id="answersEmpty" class="hidden py-6 text-center text-sm" style="color:var(--ds-tx3)">No files submitted yet.</div>
        <div id="uploadArea" class="mt-2 border border-dashed rounded-lg px-5 py-6 text-center cursor-pointer transition" style="border-color:var(--ds-bd)"
          onclick="document.getElementById('fileInput').click()"
          ondragover="event.preventDefault();this.style.borderColor='var(--ds-ac)'"
          ondragleave="this.style.borderColor='var(--ds-bd)'"
          ondrop="event.preventDefault();this.style.borderColor='var(--ds-bd)';uploadFiles(event.dataTransfer.files)">
          <svg class="w-6 h-6 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="color:var(--ds-tx3)"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"/></svg>
          <p class="text-sm font-medium" style="color:var(--ds-tx2)">Drop files or click to upload</p>
          <p class="text-xs mt-0.5" style="color:var(--ds-tx3)">PDF, DOCX, XLSX, images accepted</p>
          <input id="fileInput" type="file" multiple class="hidden" onchange="uploadFiles(this.files)">
        </div>
        <div id="uploadStatus" class="mt-3 text-sm" style="color:var(--ds-tx2)"></div>
        <button id="markAnsweredBtn" onclick="markAnswered()" class="hidden mt-4 px-5 py-2.5 rounded text-sm font-semibold bg-green-600 hover:bg-green-700 text-white transition">
          &#10003; Mark as Answered
        </button>
      </div>
    </div>

    <!-- DISCUSSION CARD -->
    <div class="rounded-xl overflow-hidden" style="background:var(--ds-sf);border:1px solid var(--ds-bd)">
      <div id="channelTabs" class="flex items-center overflow-x-auto border-b" style="border-color:var(--ds-bd)">
        <div class="px-5 py-3 text-xs" style="color:var(--ds-tx3)">Loading channels...</div>
      </div>
      <div id="channelComments" class="px-6 py-4 space-y-4 min-h-[80px] max-h-[400px] overflow-y-auto">
        <p class="text-sm" style="color:var(--ds-tx3)">Select a channel above.</p>
      </div>
      <div id="commentComposer" class="border-t px-5 py-4" style="border-color:var(--ds-bd)">
        <div id="composeReadOnly" class="hidden py-1 text-xs text-center" style="color:var(--ds-tx3)">Read only in this channel.</div>
        <div id="composeForm" class="flex gap-3 items-end">
          <textarea id="commentText" rows="2" placeholder="Post an announcement..."
            class="flex-1 px-3 py-2.5 rounded-lg text-sm resize-none focus:outline-none focus:ring-1"
            style="background:var(--ds-bg);border:1px solid var(--ds-bd);color:var(--ds-tx);--tw-ring-color:var(--ds-ac)"></textarea>
          <button onclick="postComment(null)" class="px-4 py-2.5 rounded-lg text-sm font-semibold transition shrink-0" style="background:var(--ds-ac);color:var(--ds-act)">Post</button>
        </div>
      </div>
    </div>

  </div>
{{end}}

{{define "scripts"}}
<script>
    const reqID = location.pathname.split('/').pop();
    function isBuyerRole() {
      const r = localStorage.getItem('ds_test_role') || '';
      return r === 'buyer' || r === 'seller' || r === 'advisor';
    }

    const statusColors = { open: 'bg-yellow-500/20 text-yellow-300', answered: 'bg-green-500/20 text-green-300', closed: 'bg-gray-500/20 text-gray-300', 'under-review': 'bg-blue-500/20 text-blue-300' };
    const priorityColors = { high: 'bg-red-500/20 text-red-300', medium: 'bg-yellow-500/20 text-yellow-300', low: 'bg-blue-500/20 text-blue-300' };

    let currentRequest = null;
    let currentData = null;
    let currentAnswers = [];

    async function applyRoleRestrictions() {
      try {
        const res = await fetchAPI('/api/auth/me');
        const me = res.ok ? await res.json() : {};
        // Use server role first, fall back to localStorage (survives service restarts)
        const role = me.test_role || localStorage.getItem('ds_test_role') || '';
        const isReadOnly = role === 'buyer' || role === 'seller' || role === 'advisor';
        if (isReadOnly) {
          const ua = document.getElementById('uploadArea');
          if (ua) ua.style.display = 'none';
          const mb = document.getElementById('markAnsweredBtn');
          if (mb) mb.style.display = 'none';
          const eb = document.getElementById('editReqBtn');
          if (eb) eb.style.display = 'none';
          const cc = document.getElementById('commentComposer');
          if (cc) cc.style.display = 'none';
        }
      } catch(e) {}
    }

    async function loadAll() {
      try {
        const res = await fetchAPI('/api/requests/' + reqID);
        if (!res.ok) { document.getElementById('reqTitle').textContent = 'Not found'; return; }
        const json = await res.json();
        currentRequest = json.request;
        currentData = parseData(currentRequest.data_text);
        const children = json.children || [];

        renderRequest(currentRequest, currentData);
        currentAnswers = children.filter(c => c.type === 'answer' || c.type === 'document');
        renderAnswers(currentAnswers);
        allComments = children.filter(c => c.type === 'comment');
        await initChannels();
      } catch(e) {
        document.getElementById('reqTitle').textContent = 'Error loading request';
      }
    }

    function renderRequest(req, d) {
      const title = d.title || req.summary || 'Untitled';
      document.title = title + ' \u2014 Dealspace';
      document.getElementById('reqRef').textContent = d.item_number || title;
      document.getElementById('reqTitle').textContent = title;
      // Don't repeat description if it's the same as the title
      const desc = d.description || '';
      document.getElementById('reqDesc').textContent = desc === title ? '' : desc;

      // Apply role-based UI restrictions
      applyRoleRestrictions();

      if (req.project_id) {
        document.getElementById('backToProject').href = '/app/projects/' + req.project_id;
        // Fetch project name for breadcrumb
        fetchAPI('/api/projects/' + req.project_id).then(r=>r.json()).then(p=>{
          const pname = (p.data_text ? (()=>{try{return JSON.parse(p.data_text)}catch{return{}}})() : {}).name || 'Project';
          document.getElementById('backToProject').textContent = pname;
        }).catch(()=>{});
      }

      const badge = document.getElementById('reqItemBadge');
      const badgeParts = [d.section, d.item_number].filter(Boolean);
      if (badgeParts.length) { badge.textContent = badgeParts.join(' \u00b7 '); badge.classList.remove('hidden'); }

      const prioEl = document.getElementById('reqPriority');
      if (d.priority) {
        const pc = priorityColors[d.priority] || 'bg-gray-500/20 text-gray-300';
        prioEl.className = 'px-2.5 py-1 rounded-full text-xs font-medium capitalize ' + pc;
        prioEl.textContent = d.priority + ' priority';
        prioEl.classList.remove('hidden');
      }

      if (d.due_date) {
        const dueEl = document.getElementById('reqDue');
        dueEl.textContent = '\ud83d\udcc5 Due: ' + d.due_date;
        dueEl.classList.remove('hidden');
      }

      const status = d.status || 'open';
      const sc = statusColors[status] || 'bg-gray-500/20 text-gray-300';
      const statusEl = document.getElementById('reqStatus');
      statusEl.className = 'px-2.5 py-1 rounded-full text-xs font-medium capitalize ' + sc;
      statusEl.textContent = status;

      if (status === 'answered') {
        document.getElementById('answeredBanner').classList.remove('hidden');
        document.getElementById('markAnsweredBtn').classList.add('hidden');
      } else {
        document.getElementById('answeredBanner').classList.add('hidden');
        document.getElementById('markAnsweredBtn').classList.remove('hidden');
      }
    }

    // ---- Edit request detail ----
    function startEditRequest() {
      if (!currentData) return;
      document.getElementById('editReqTitle').value = currentData.title || '';
      document.getElementById('editReqDesc').value = currentData.description || '';
      document.getElementById('editReqPriority').value = currentData.priority || 'medium';
      document.getElementById('editReqStatus').value = currentData.status || 'open';
      document.getElementById('reqTitle').classList.add('hidden');
      document.getElementById('reqDesc').classList.add('hidden');
      document.getElementById('editReqBtn').classList.add('hidden');
      document.getElementById('reqMetaBar').classList.add('hidden');
      document.getElementById('reqEditMode').classList.remove('hidden');
    }

    function cancelEditRequestDetail() {
      document.getElementById('reqTitle').classList.remove('hidden');
      document.getElementById('reqDesc').classList.remove('hidden');
      document.getElementById('editReqBtn').classList.remove('hidden');
      document.getElementById('reqMetaBar').classList.remove('hidden');
      document.getElementById('reqEditMode').classList.add('hidden');
    }

    async function saveEditRequestDetail() {
      if (!currentRequest || !currentData) return;
      const projectID = currentRequest.project_id;
      const updatedData = {
        ...currentData,
        title: document.getElementById('editReqTitle').value.trim(),
        description: document.getElementById('editReqDesc').value.trim(),
        priority: document.getElementById('editReqPriority').value,
        status: document.getElementById('editReqStatus').value
      };
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/entries/' + reqID, {
          method: 'PUT',
          body: JSON.stringify({ data: JSON.stringify(updatedData), version: currentRequest.version })
        });
        if (res.ok) {
          cancelEditRequestDetail();
          loadAll();
        }
      } catch(e) {
        console.error('Save failed:', e);
      }
    }

    // ---- Answers / Files ----
    function renderAnswers(docs) {
      const el = document.getElementById('answers');
      const emptyEl = document.getElementById('answersEmpty');
      if (!docs || docs.length === 0) {
        el.innerHTML = '';
        if (emptyEl) emptyEl.classList.remove('hidden');
        return;
      }
      if (emptyEl) emptyEl.classList.add('hidden');
      const projectID = currentRequest.project_id;
      el.innerHTML = docs.map(a => {
        const d = parseData(a.data_text);
        const name = d.filename || d.name || a.summary || 'Document';
        const icon = name.endsWith('.pdf') ? '\ud83d\udcc4' : name.match(/\.(jpg|jpeg|png|gif)$/i) ? '\ud83d\uddbc\ufe0f' : name.match(/\.(xlsx?|csv)$/i) ? '\ud83d\udcca' : '\ud83d\udcce';
        const objectID = d.object_id || '';
        const downloadUrl = objectID ? '/api/projects/' + projectID + '/objects/' + objectID + '?filename=' + encodeURIComponent(name) : '#';
        const previewUrl = objectID ? '/api/projects/' + projectID + '/objects/' + objectID + '/preview?filename=' + encodeURIComponent(name) : '';
        const isVideo = /\.(mp4|mov|avi|mkv|webm)$/i.test(name);
        return `<div class="flex items-center gap-3 px-3 py-2.5 rounded-lg bg-[#0a1628] border border-white/[0.05]">
          <span class="text-xl">${icon}</span>
          <div class="flex-1 min-w-0">
            <div class="flex items-center gap-1">
              <span class="text-white text-sm font-medium truncate" id="fname-${a.entry_id}">${escHtml(name)}</span>
              <button onclick="startRenameFile('${a.entry_id}')" class="shrink-0 p-0.5 rounded hover:bg-white/[0.08] text-[#b0bec5] hover:text-white transition" title="Rename">
                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/></svg>
              </button>
            </div>
            <div class="text-[#8899a6] text-xs">${a.created_at ? new Date(a.created_at * 1000).toLocaleString() : ''}</div>
          </div>
          <div class="flex items-center gap-2 shrink-0">
            ${objectID && previewUrl ? `<a href="${previewUrl}" target="_blank" class="px-2 py-1 rounded text-xs font-medium bg-white/[0.06] text-[#b0bec5] hover:text-white hover:bg-white/[0.1] transition">${isVideo ? 'Play' : (isBuyerRole() ? 'View' : 'Preview')}</a>` : ''}
            ${objectID && !isBuyerRole() ? `<a href="${downloadUrl}" class="text-sm hover:underline" style="color:var(--ds-ac)">Download</a>` : ''}
          </div>
        </div>`;
      }).join('');
    }

    // ---- Rename file ----
    function startRenameFile(answerId) {
      const span = document.getElementById('fname-' + answerId);
      if (!span) return;
      const currentName = span.textContent;
      const container = span.parentElement;
      container.innerHTML = `<input type="text" value="" id="rename-input-${answerId}"
        style="flex:1;padding:2px 6px;background:#0a1628;border:1px solid rgba(255,255,255,.12);border-radius:4px;color:white;font-size:13px;outline:none"
        onkeydown="handleRenameKey(event,'${answerId}')" onblur="saveRenameFile('${answerId}')">`;
      const input = document.getElementById('rename-input-' + answerId);
      if (input) { input.value = currentName; input.focus(); input.select(); }
    }

    function handleRenameKey(e, answerId) {
      if (e.key === 'Enter') { e.preventDefault(); e.target.blur(); }
      if (e.key === 'Escape') { e.target.dataset.cancelled = 'true'; e.target.blur(); }
    }

    async function saveRenameFile(answerId) {
      const input = document.getElementById('rename-input-' + answerId);
      if (!input || input.dataset.cancelled === 'true') { loadAll(); return; }
      const newName = input.value.trim();
      if (!newName) { loadAll(); return; }

      const answer = currentAnswers.find(a => a.entry_id === answerId);
      if (!answer) { loadAll(); return; }

      const projectID = currentRequest.project_id;
      const d = parseData(answer.data_text);
      d.filename = newName;
      d.name = newName;

      try {
        await fetchAPI('/api/projects/' + projectID + '/entries/' + answerId, {
          method: 'PUT',
          body: JSON.stringify({ data: JSON.stringify(d), version: answer.version || 0 })
        });
      } catch(e) {
        console.error('Rename failed:', e);
      }
      loadAll();
    }

    // renderComments replaced by channel system below

    async function uploadFiles(files) {
      if (!currentRequest) return;
      const projectID = currentRequest.project_id;
      const status = document.getElementById('uploadStatus');
      for (const file of files) {
        status.textContent = 'Uploading ' + file.name + '...';
        const fd = new FormData();
        fd.append('file', file);
        try {
          const uploadRes = await fetchAPI('/api/projects/' + projectID + '/objects', { method: 'POST', body: fd });
          if (!uploadRes.ok) { status.textContent = 'Upload failed for ' + file.name; continue; }
          const uploadData = await uploadRes.json();

          const entryBody = {
            project_id: projectID,
            parent_id: reqID,
            type: 'document',
            data: JSON.stringify({ filename: file.name, name: file.name, content_type: file.type, object_id: uploadData.object_id })
          };
          const entryRes = await fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify(entryBody) });
          if (entryRes.ok) { status.textContent = file.name + ' uploaded.'; }
          else { status.textContent = 'Failed to save ' + file.name; }
        } catch(e) { status.textContent = 'Error: ' + e.message; }
      }
      setTimeout(() => status.textContent = '', 3000);
      loadAll();
    }

    // ---- Channel-based comment system ----
    let activeChannel = 'announcements';
    let allComments = [];
    let dealOrgs = [];
    let replyingToId = null;

    async function initChannels() {
      if (!currentRequest) return;
      try {
        const res = await fetchAPI('/api/projects/' + currentRequest.project_id + '/orgs');
        dealOrgs = await res.json() || [];
      } catch(e) { dealOrgs = []; }
      renderChannelTabs();
      switchChannel('announcements');
    }

    function renderChannelTabs() {
      const container = document.getElementById('channelTabs');
      const tabs = [
        { key: 'announcements', label: '📢 Announcements' },
        ...dealOrgs.map(o => {
          const d = parseData(o.data_text);
          return { key: 'org:' + o.entry_id, label: '🔒 ' + escHtml(d.org_name || d.name || 'Org') };
        })
      ];
      container.innerHTML = tabs.map(t =>
        `<button onclick="switchChannel(${JSON.stringify(t.key)})"
          class="px-4 py-3 text-sm font-medium whitespace-nowrap border-b-2 -mb-px transition
            ${activeChannel === t.key ? 'border-[#c9a84c] text-white' : 'border-transparent text-[#b0bec5] hover:text-white'}">
          ${t.label}
        </button>`
      ).join('');
    }

    function switchChannel(key) {
      activeChannel = key;
      cancelReply();
      renderChannelTabs();
      renderChannelComments();
      const ta = document.getElementById('commentText');
      ta.placeholder = key === 'announcements' ? 'Post an announcement...' : 'Message this channel...';
    }

    function renderChannelComments() {
      const el = document.getElementById('channelComments');
      const ch = allComments.filter(c => (parseData(c.data_text).channel || 'announcements') === activeChannel);
      const top = ch.filter(c => !parseData(c.data_text).parent_comment_id);
      const nested = ch.filter(c => !!parseData(c.data_text).parent_comment_id);
      if (top.length === 0) {
        el.innerHTML = '<p class="text-[#8899a6] text-sm">No messages yet.</p>';
        return;
      }
      el.innerHTML = top.map(c => {
        const d = parseData(c.data_text);
        const replies = nested.filter(r => parseData(r.data_text).parent_comment_id === c.entry_id);
        return renderOneComment(c, d, false) + replies.map(r => renderOneComment(r, parseData(r.data_text), true)).join('');
      }).join('');
      el.scrollTop = el.scrollHeight;
    }

    function renderOneComment(c, d, isReply) {
      const av = (d.author || '?')[0].toUpperCase();
      const ts = c.created_at ? new Date(c.created_at * 1000).toLocaleString('en-US', {month:'short',day:'numeric',hour:'numeric',minute:'2-digit'}) : '';
      const replyBtn = isReply ? '' : `<button onclick="startReply('${c.entry_id}')" class="text-xs text-[#8899a6] hover:text-[#b0bec5] ml-2 transition">Reply</button>`;
      const indent = isReply ? 'ml-10 pl-4 border-l-2 border-[#c9a84c]/30' : '';
      return `<div class="flex gap-3 ${indent}" data-comment-id="${c.entry_id}">
        <div class="w-7 h-7 rounded-full bg-[#c9a84c]/20 flex items-center justify-center text-[#c9a84c] text-xs font-semibold shrink-0 mt-0.5">${av}</div>
        <div class="flex-1 min-w-0">
          <div class="flex items-baseline gap-2 mb-1">
            <span class="text-white text-sm font-medium">${escHtml(d.author || 'Unknown')}</span>
            <span class="text-[#8899a6] text-xs">${ts}</span>
            ${replyBtn}
          </div>
          <p class="text-[#b0bec5] text-sm leading-relaxed">${escHtml(d.text || '')}</p>
        </div>
      </div>`;
    }

    function startReply(commentId) {
      cancelReply();
      replyingToId = commentId;
      const el = document.querySelector(`[data-comment-id="${commentId}"]`);
      if (!el) return;
      const form = document.createElement('div');
      form.id = 'replyForm';
      form.className = 'ml-10 pl-4 border-l-2 border-[#c9a84c]/30 mt-2';
      form.innerHTML = `<div class="flex gap-2 items-end">
        <textarea id="replyText" rows="2" placeholder="Reply..." class="flex-1 px-3 py-2 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white placeholder-[#8899a6] focus:outline-none focus:border-[#c9a84c] text-sm resize-none"></textarea>
        <div class="flex flex-col gap-1">
          <button onclick="postComment('${commentId}')" class="px-3 py-1.5 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-xs transition">Reply</button>
          <button onclick="cancelReply()" class="px-3 py-1.5 bg-white/[0.05] hover:bg-white/[0.08] text-[#b0bec5] rounded-lg text-xs transition">Cancel</button>
        </div>
      </div>`;
      el.insertAdjacentElement('afterend', form);
      document.getElementById('replyText').focus();
    }

    function cancelReply() {
      replyingToId = null;
      const f = document.getElementById('replyForm');
      if (f) f.remove();
    }

    async function postComment(parentCommentId) {
      if (!currentRequest) return;
      const text = parentCommentId
        ? (document.getElementById('replyText')?.value || '').trim()
        : (document.getElementById('commentText')?.value || '').trim();
      if (!text) return;
      const projectID = currentRequest.project_id;
      const body = {
        project_id: projectID,
        parent_id: reqID,
        type: 'comment',
        data: JSON.stringify({ text, author: user.name || user.email, channel: activeChannel, parent_comment_id: parentCommentId || null })
      };
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify(body) });
        if (res.ok) {
          if (parentCommentId) cancelReply(); else document.getElementById('commentText').value = '';
          await loadAll();
        }
      } catch(e) {}
    }

    async function markAnswered() {
      if (!currentRequest || !currentData) return;
      const projectID = currentRequest.project_id;
      const updatedData = { ...currentData, status: 'answered' };
      const body = {
        data: JSON.stringify(updatedData),
        version: currentRequest.version
      };
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/entries/' + reqID, { method: 'PUT', body: JSON.stringify(body) });
        if (res.ok) loadAll();
      } catch(e) {}
    }

    // Drag and drop
    const ua = document.getElementById('uploadArea');
    ua.addEventListener('dragover', e => { e.preventDefault(); ua.classList.add('border-[#c9a84c]/60'); });
    ua.addEventListener('dragleave', () => ua.classList.remove('border-[#c9a84c]/60'));
    ua.addEventListener('drop', e => { e.preventDefault(); ua.classList.remove('border-[#c9a84c]/60'); uploadFiles(e.dataTransfer.files); });

    loadAll();
</script>
{{end}}
