const WORD_THRESHOLD=15;let current=0,answers={},transitioning=false;function countWords(s){return s.trim().split(/\s+/).length}function hasLongOptions(o){return o.some(x=>countWords(x)>WORD_THRESHOLD)}function render(){const q=questions[current],isLong=hasLongOptions(q.options);document.getElementById('progress').textContent=`${current+1} / ${questions.length}`;const questionEl=document.getElementById('question'),subtextEl=document.getElementById('subtext');questionEl.textContent=q.q;if(q.sub){subtextEl.textContent=q.sub;subtextEl.classList.remove('hidden');questionEl.classList.remove('no-sub')}else{subtextEl.classList.add('hidden');questionEl.classList.add('no-sub')}const opts=q.options.map((o,i)=>`<div class="option ${isLong?'small':''} ${answers[current]===i?'selected':''}" onclick="select(${i})"><span class="key">${i+1}</span><span>${o}</span></div>`).join('')+`<div class="option ${isLong?'small':''} ${answers[current]==='other'||(typeof answers[current]==='string'&&answers[current]!=='other')?'selected':''}" onclick="selectOther()"><span class="key">${q.options.length+1}</span><input class="other-input" id="otherInput" placeholder="Other..." value="${typeof answers[current]==='string'?(answers[current]==='other'?'':answers[current]):''}" onkeydown="handleOtherKey(event)" onfocus="selectOther()"></div>`;document.getElementById('options').innerHTML=opts}function transition(cb){if(transitioning)return;transitioning=true;const el=document.getElementById('content');el.classList.add('out');setTimeout(()=>{cb();el.classList.remove('out');transitioning=false},150)}function select(i){answers[current]=i;render();setTimeout(()=>{if(current<questions.length-1){transition(()=>{current++;render()})}else{transitionToResult()}},200)}function selectOther(){const wasOther=answers[current]==='other'||typeof answers[current]==='string';answers[current]='other';if(!wasOther)render();setTimeout(()=>document.getElementById('otherInput').focus(),0)}function handleOtherKey(e){if(e.key==='Enter'){e.preventDefault();next()}}function next(){const input=document.getElementById('otherInput');if((answers[current]==='other'||typeof answers[current]==='string')&&input&&input.value){answers[current]=input.value}if(current<questions.length-1){transition(()=>{current++;render()})}else{transitionToResult()}}function prev(){if(current>0){transition(()=>{current--;render()})}}function transitionToResult(){if(transitioning)return;transitioning=true;const contentEl=document.getElementById('content');contentEl.classList.add('out');setTimeout(()=>{document.getElementById('main').classList.add('hidden');document.getElementById('result').classList.remove('hidden');const resultContent=document.getElementById('resultContent');resultContent.classList.add('out');let out='';questions.forEach((q,i)=>{const a=answers[i];let val=typeof a==='number'?q.options[a]:a||'Not answered';out+=`${q.q}\n→ ${val}\n\n`});document.getElementById('output').textContent=out.trim();navigator.clipboard.writeText(out.trim()).catch(()=>{});setTimeout(()=>{resultContent.classList.remove('out');transitioning=false},50)},150)}function goBack(){const resultContent=document.getElementById('resultContent');resultContent.classList.add('out');setTimeout(()=>{document.getElementById('result').classList.add('hidden');document.getElementById('main').classList.remove('hidden');current=questions.length-1;const contentEl=document.getElementById('content');contentEl.classList.add('out');render();setTimeout(()=>{contentEl.classList.remove('out')},50)},150)}function copy(){navigator.clipboard.writeText(document.getElementById('output').textContent);document.querySelector('.btn.primary').textContent='Copied!';setTimeout(()=>document.querySelector('.btn.primary').textContent='Copy',2000)}document.addEventListener('keydown',e=>{if(transitioning)return;if(document.getElementById('result').classList.contains('hidden')){const q=questions[current],focused=document.activeElement,isTyping=focused&&focused.classList.contains('other-input');if(!isTyping){if(e.key>='1'&&e.key<=String(q.options.length)){e.preventDefault();select(parseInt(e.key)-1)}if(e.key===String(q.options.length+1)){e.preventDefault();selectOther()}}if(e.key==='ArrowRight')next();if(e.key==='ArrowLeft')prev()}else{if(e.key==='ArrowLeft')goBack()}});render();