// 1단계: 문서 없이 질문→답변만
async function ask() {
  const box = document.getElementById('q');
  const q = box.value.trim();
  if (!q) return;
  add('나', q);
  box.value = '';
  const r = await fetch('/ask', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ question: q }),
  });
  const d = await r.json();
  add('봇', d.answer);
}

function add(who, text) {
  const l = document.getElementById('log');
  l.innerHTML += '<p><b>' + who + ':</b> ' + text + '</p>';
  l.scrollTop = l.scrollHeight;
}

document.getElementById('q').addEventListener('keydown', e => { if (e.key === 'Enter') ask(); });
