+Math.round(v).toLocaleString();return '₩'+Math.round(cv).toLocaleString()}}}}}})}
function toggleLog(){useLog=!useLog;document.getElementById('logToggle').classList.toggle('active',useLog);if(lastResult)renderChart(lastResult)}
function renderDDChart(r){const ctx=document.getElementById('ddChart').getContext('2d');if(ddChart)ddChart.destroy();const st=Math.max(1,Math.floor(r.dates.length/600));const lb=r.dates.filter((_,i)=>i%st===0);const d=r.dd.filter((_,i)=>i%st===0);ddChart=new Chart(ctx,{type:'line',data:{labels:lb,datasets:[{data:d,borderColor:'#ff3b5c',backgroundColor:'rgba(255,59,92,0.06)',fill:true,borderWidth:1,pointRadius:0,tension:0.1}]},options:{responsive:true,maintainAspectRatio:false,plugins:{legend:{display:false},tooltip:{backgroundColor:'#1a1a28',borderColor:'#2a2a42',borderWidth:1,callbacks:{label:c=>`Drawdown: ${c.parsed.y.toFixed(1)}%`}}},scales:{x:{grid:{color:'rgba(42,42,66,0.3)'},ticks:{color:'#6a6a88',maxTicksLimit:7,font:{family:'Noto Sans KR',size:10}}},y:{grid:{color:'rgba(42,42,66,0.3)'},ticks:{color:'#6a6a88',font:{family:'Noto Sans KR',size:10},callback:v=>v.toFixed(0)+'%'}}}}})}
function renderTable(r){const tb=document.querySelector('#yearlyTable tbody');tb.innerHTML='';for(const[yr,d]of Object.entries(r.yearly)){const ret=((d.end-d.start)/d.start*100);tb.innerHTML+=`
${yr}
${fmtCurr(d.start)}
${fmtCurr(d.end)}
${ret>=0?'+':''}${ret.toFixed(1)}%
-${(d.maxDD*100).toFixed(1)}%
${fmtCurr(d.invested)}
`}}
function showToast(m,e=false){const t=document.getElementById('toast');t.textContent=m;t.className='toast show'+(e?' error':'');setTimeout(()=>t.classList.remove('show'),3000)}
document.addEventListener('keydown',e=>{if(e.key==='Enter'&&selectedTicker)runBacktest()});
init();