let timerInterval=null,startTime=null,elapsedTime=0,timerRunning=false;function toggleTimer(){const btn=document.getElementById('timerBtn');if(!timerRunning){startTime=Date.now()-elapsedTime;timerInterval=setInterval(updateTimer,100);btn.textContent='Stop Timer';btn.classList.add('running');timerRunning=true}else{clearInterval(timerInterval);btn.textContent='Resume Timer';btn.classList.remove('running');timerRunning=false}}function updateTimer(){elapsedTime=Date.now()-startTime;const seconds=Math.floor(elapsedTime/1000),minutes=Math.floor(seconds/60),displaySeconds=seconds%60;document.getElementById('timer').textContent=`${String(minutes).padStart(2,'0')}:${String(displaySeconds).padStart(2,'0')}`}function stopTimerForWin(){if(timerRunning){clearInterval(timerInterval);timerRunning=false;const finalTimeDiv=document.getElementById('finalTime'),timerDisplay=document.getElementById('timer').textContent;finalTimeDiv.innerHTML=`🎉 Completed in ${timerDisplay}! 🎉`;finalTimeDiv.style.display='block';finalTimeDiv.className='final-time';document.getElementById('timerBtn').style.display='none'}} const gridSize=15;let grid=[],selectedCells=[],foundWords=new Set(),isSelecting=false;function createGrid(){grid=Array(gridSize).fill().map(()=>Array(gridSize).fill(''));words.forEach(word=>{let placed=false,attempts=0;while(!placed&&attempts<100){const direction=Math.floor(Math.random()*8),row=Math.floor(Math.random()*gridSize),col=Math.floor(Math.random()*gridSize);if(canPlaceWord(word,row,col,direction)){placeWord(word,row,col,direction);placed=true}attempts++}});for(let i=0;i=gridSize||newCol<0||newCol>=gridSize)return false;if(grid[newRow][newCol]!==''&&grid[newRow][newCol]!==word[i])return false}return true}function placeWord(word,row,col,direction){const directions=[[0,1],[1,0],[1,1],[-1,1],[0,-1],[-1,0],[-1,-1],[1,-1]],[dx,dy]=directions[direction];for(let i=0;istartSelection(i,j));cell.addEventListener('mouseenter',()=>addToSelection(i,j));cell.addEventListener('mouseup',endSelection);cell.addEventListener('touchstart',e=>{e.preventDefault();startSelection(i,j)});cell.addEventListener('touchmove',e=>{e.preventDefault();const touch=e.touches[0],element=document.elementFromPoint(touch.clientX,touch.clientY);if(element&&element.classList.contains('cell')){const row=parseInt(element.dataset.row),col=parseInt(element.dataset.col);addToSelection(row,col)}});cell.addEventListener('touchend',e=>{e.preventDefault();endSelection()});gridElement.appendChild(cell)}}function renderWordList(){const wordListElement=document.getElementById('wordList');wordListElement.innerHTML='';words.forEach(word=>{const wordItem=document.createElement('div');wordItem.className='word-item'+(foundWords.has(word)?' found':'');wordItem.textContent=word;wordListElement.appendChild(wordItem)});document.getElementById('foundCount').textContent=foundWords.size;document.getElementById('totalCount').textContent=words.length;if(foundWords.size===words.length)stopTimerForWin()}function startSelection(row,col){isSelecting=true;selectedCells=[{row,col}];updateSelection()}function addToSelection(row,col){if(!isSelecting)return;const last=selectedCells[selectedCells.length-1];if(last.row!==row||last.col!==col){selectedCells.push({row,col});updateSelection()}}function endSelection(){if(!isSelecting)return;isSelecting=false;const selectedWord=selectedCells.map(cell=>grid[cell.row][cell.col]).join(''),reversedWord=selectedWord.split('').reverse().join('');if(words.includes(selectedWord)&&!foundWords.has(selectedWord)){foundWords.add(selectedWord);markAsFound()}else if(words.includes(reversedWord)&&!foundWords.has(reversedWord)){foundWords.add(reversedWord);markAsFound()}else clearSelection();renderWordList()}function updateSelection(){document.querySelectorAll('.cell').forEach(cell=>cell.classList.remove('selected'));selectedCells.forEach(({row,col})=>{const cell=document.querySelector(`[data-row="${row}"][data-col="${col}"]`);if(cell&&!cell.classList.contains('found'))cell.classList.add('selected')})}function markAsFound(){selectedCells.forEach(({row,col})=>{const cell=document.querySelector(`[data-row="${row}"][data-col="${col}"]`);if(cell){cell.classList.remove('selected');cell.classList.add('found')}});selectedCells=[]}function clearSelection(){document.querySelectorAll('.cell.selected').forEach(cell=>cell.classList.remove('selected'));selectedCells=[]}createGrid();renderGrid();renderWordList();document.addEventListener('mouseup',endSelection);