+${m.points} pts `).join(""); $grid.querySelectorAll(".card").forEach(el=>el.onclick=()=>toggle(el.dataset.key)); refresh(); } function refresh(){ const n=state.selected.length; document.querySelectorAll("#dt-grid .card").forEach(btn=>{ btn.setAttribute("aria-selected",state.selected.includes(btn.dataset.key)); }); $count.textContent=n; $status.textContent=`${n} selected.`; updateCTA(); } function toggle(k){ if(state.locked)return; if(!state.startedAt){ state.startedAt=Date.now(); document.dispatchEvent(new CustomEvent("dt:start")); } const i=state.selected.indexOf(k); if(i>-1)state.selected.splice(i,1); else state.selected.push(k); refresh();save(); } function updateCTA(){ const active=state.locked||state.selected.length>=5; $next.classList.toggle("active",active); } function load(){try{return JSON.parse(localStorage.getItem(STORE))}catch(e){return null}} function save(){ localStorage.setItem(STORE,JSON.stringify(state)); document.dispatchEvent(new CustomEvent("dt:update")); } $next.onclick=()=>{ if(!$next.classList.contains("active"))return; const detailed=state.selected.map(k=>map[k]).filter(Boolean); const teamScore=detailed.reduce((a,m)=>a+m.points,0); const teamStrength=Math.round(teamScore/detailed.length); localStorage.setItem("dt.v1.results",JSON.stringify({ selected:state.selected,detailed,teamScore,teamStrength, locked:state.locked,createdAt:Date.now() })); location.href=NEXT_URL; }; // ⏱ Timer Functions function remaining(){ if(!state.startedAt)return TIME; return Math.max(0,TIME-Math.floor((Date.now()-state.startedAt)/1000)); } function tick(){ const left=remaining(); if(left<=0){ stop(); $timer.classList.add("timeup"); $timer.textContent="Time Up!"; state.locked=true; save(); document.dispatchEvent(new CustomEvent("dt:locked")); return; } $timer.textContent=left+"s"; } function start(){ stop(); $timer.style.display="block"; $timer.classList.remove("timeup"); timer=setInterval(tick,1000); tick(); } function stop(){if(timer){clearInterval(timer);timer=null;}} document.addEventListener("dt:start",()=>start()); if(state.startedAt&&!state.locked)start(); document.addEventListener("dt:reset",()=>{ stop(); state={selected:[],locked:false,startedAt:null}; $timer.style.display="none"; $timer.classList.remove("timeup"); $timer.textContent=TIME+"s"; refresh();save(); }); render(); })();