(function() {
  const raw = document.getElementById('reasoning-raw');
  const rendered = document.getElementById('reasoning-rendered');
  if (raw && rendered && raw.textContent) {
    rendered.innerHTML = simpleMarkdown(raw.textContent.trim());
  }

  function simpleMarkdown(text) {
    // very small markdown renderer: convert headings, bold/italic, code, and line breaks
    let html = text
      .replace(/^### (.*$)/gim, '<h5>$1</h5>')
      .replace(/^## (.*$)/gim, '<h4>$1</h4>')
      .replace(/^# (.*$)/gim, '<h3>$1</h3>')
      .replace(/\*\*(.*?)\*\*/gim, '<strong>$1</strong>')
      .replace(/\*(.*?)\*/gim, '<em>$1</em>')
      .replace(/`([^`]+)`/gim, '<code>$1</code>')
      .replace(/\n$/gim, '<br />');
    html = html.replace(/\n/g, '<br />');
    return html;
  }
})();
