File size: 192,118 Bytes
105e821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
let puzzleStartTime = null;

document.addEventListener('DOMContentLoaded', () => {
    // DOM elements
    const submitBtn = document.getElementById('submit-answer');
    const userAnswerInput = document.getElementById('user-answer');
    const puzzleImage = document.getElementById('puzzle-image');
    const puzzleImageContainer = document.querySelector('.puzzle-image-container');
    const resultMessage = document.getElementById('result-message');
    const totalCount = document.getElementById('total-count');
    const correctCount = document.getElementById('correct-count');
    const accuracyEl = document.getElementById('accuracy');
    const puzzlePrompt = document.getElementById('puzzle-prompt');
    const puzzleContainer = document.getElementById('puzzle-container');
    const inputGroup = document.querySelector('.input-group');
    const difficultyStars = document.getElementById('difficulty-stars');

    // Debug mode - set to true to show ground truth areas
    const DEBUG_MODE = false;

    // Tracking state
    let currentPuzzle = null;
    let benchmarkStats = {
        total: 0,
        correct: 0
    };
    let clickCoordinates = null;
    let processingClick = false; // Flag to prevent multiple clicks while processing
    let currentRotationAngle = 0; // Track current rotation for Rotation_Match
    let selectedCells = []; // Track selected cells for Unusual_Detection
    let bingoSelectedCells = []; // Track selected cells for Bingo swap
    let selectedAnimalIndex = -1; // Track selected animal index for Select_Animal
    // Add debug type tracking variable 
    // let debugPuzzleType = null;
    
    // Initialize difficulty stars with default value (to show something immediately)
    displayDifficultyStars('Dice_Count');
    
    // Event listeners
    submitBtn.addEventListener('click', submitAnswer);
    userAnswerInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            submitAnswer();
        }
    });

    // Add click event handler directly to the puzzle image
    puzzleImage.addEventListener('click', handleImageClick);

    // Add debug mode selector
    // setupDebugModeSelector();

    // Functions
    function handleImageClick(e) {
        if (currentPuzzle && currentPuzzle.input_type === 'click' && !processingClick) {
            // Prevent multiple clicks while processing
            processingClick = true;
            
            // Get click coordinates relative to the image
            const rect = e.target.getBoundingClientRect();
            const x = Math.round(e.clientX - rect.left);
            const y = Math.round(e.clientY - rect.top);
            
            // Store coordinates for submission
            clickCoordinates = [x, y];
            
            // Show where user clicked
            showClickMarker(x, y);
            
            // Log for debugging
            console.log('Click received:', { x, y, target: e.target.id });
            
            // Special handling for Misleading_Click to show if click is in avoid area
            if (currentPuzzle.puzzle_type === 'Misleading_Click' && currentPuzzle.avoid_area) {
                const { x: areaX, y: areaY, width: areaWidth, height: areaHeight } = currentPuzzle.avoid_area;
                
                // Check if click is within the avoid area
                const inAvoidArea = (
                    areaX <= x && x <= areaX + areaWidth &&
                    areaY <= y && y <= areaY + areaHeight
                );
                
                if (inAvoidArea) {
                    console.log('Click is inside the avoid area! This is incorrect.');
                    
                    // Add a visual indicator
                    const marker = document.querySelector('.click-marker');
                    if (marker) {
                        marker.style.borderColor = 'red';
                        marker.style.backgroundColor = 'rgba(255, 0, 0, 0.7)';
                    }
                } else {
                    console.log('Click is outside the avoid area! This is correct.');
                    
                    // Add a visual indicator
                    const marker = document.querySelector('.click-marker');
                    if (marker) {
                        marker.style.borderColor = 'green';
                        marker.style.backgroundColor = 'rgba(0, 255, 0, 0.7)';
                    }
                }
            }
            // Special handling for Pick_Area to show if click is in the target area
            else if (currentPuzzle.puzzle_type === 'Pick_Area') {
                // Get the ground truth data to validate the click
                fetch('/api/get_ground_truth', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        puzzle_type: currentPuzzle.puzzle_type,
                        puzzle_id: currentPuzzle.puzzle_id
                    })
                })
                .then(response => response.json())
                .then(gtData => {
                    if (gtData.answer && gtData.answer.area) {
                        // Extract area boundaries from the ground truth
                        const [[minX, minY], [maxX, maxY]] = gtData.answer.area;
                        
                        // Basic rectangular check
                        const inRectArea = (minX <= x && x <= maxX && minY <= y && y <= maxY);
                        
                        // For more accurate curve detection:
                        let inPolygonArea = false;
                        if (gtData.answer.polygon) {
                            // If we have a polygon definition for the curved area
                            inPolygonArea = pointInPolygon(x, y, gtData.answer.polygon);
                        }
                        
                        // Determine if the click is in the target area
                        // Use polygon if available, otherwise fall back to rectangular check
                        const inArea = gtData.answer.polygon ? inPolygonArea : inRectArea;
                        
                        // Get the marker element
                        const marker = document.querySelector('.click-marker');
                        if (marker) {
                            if (inArea) {
                                console.log('Click is inside the target area! This is correct.');
                                marker.style.borderColor = 'green';
                                marker.style.backgroundColor = 'rgba(0, 255, 0, 0.7)';
                                
                                // Add a success message
                                const successMsg = document.createElement('div');
                                successMsg.className = 'success-msg';
                                successMsg.textContent = 'In largest area!';
                                successMsg.style.position = 'absolute';
                                successMsg.style.top = '-25px';
                                successMsg.style.left = '50%';
                                successMsg.style.transform = 'translateX(-50%)';
                                successMsg.style.backgroundColor = 'rgba(0, 128, 0, 0.9)';
                                successMsg.style.color = 'white';
                                successMsg.style.padding = '3px 8px';
                                successMsg.style.borderRadius = '3px';
                                successMsg.style.fontSize = '12px';
                                successMsg.style.fontWeight = 'bold';
                                marker.appendChild(successMsg);
                            } else {
                                console.log('Click is outside the target area! This is incorrect.');
                                marker.style.borderColor = 'red';
                                marker.style.backgroundColor = 'rgba(255, 0, 0, 0.7)';
                                
                                // Add an error message
                                const errorMsg = document.createElement('div');
                                errorMsg.className = 'error-msg';
                                errorMsg.textContent = 'Not in largest area!';
                                errorMsg.style.position = 'absolute';
                                errorMsg.style.top = '-25px';
                                errorMsg.style.left = '50%';
                                errorMsg.style.transform = 'translateX(-50%)';
                                errorMsg.style.backgroundColor = 'rgba(255, 0, 0, 0.9)';
                                errorMsg.style.color = 'white';
                                errorMsg.style.padding = '3px 8px';
                                errorMsg.style.borderRadius = '3px';
                                errorMsg.style.fontSize = '12px';
                                errorMsg.style.fontWeight = 'bold';
                                marker.appendChild(errorMsg);
                            }
                        }
                    }
                })
                .catch(error => {
                    console.error('Error validating click for Pick_Area:', error);
                });
            }
            
            // Auto-submit after click
            setTimeout(() => {
                submitAnswer();
            }, 500); // Increase delay slightly to allow for fetch response
        }
    }

    // Function to handle rotation
    function setupRotationControls() {
        // Remove any existing controls first
        const existingControls = document.querySelector('.rotation-controls');
        if (existingControls) {
            existingControls.remove();
        }
        
        // Create rotation controls
        const rotationControls = document.createElement('div');
        rotationControls.className = 'rotation-controls';
        
        // Create left rotation button
        const leftBtn = document.createElement('button');
        leftBtn.className = 'rotate-left';
        leftBtn.innerHTML = '&#8630;'; // Counter-clockwise arrow
        leftBtn.setAttribute('aria-label', 'Rotate left');
        
        // Create right rotation button
        const rightBtn = document.createElement('button');
        rightBtn.className = 'rotate-right';
        rightBtn.innerHTML = '&#8631;'; // Clockwise arrow
        rightBtn.setAttribute('aria-label', 'Rotate right');
        
        // Add buttons to controls
        rotationControls.appendChild(leftBtn);
        rotationControls.appendChild(rightBtn);
        
        // Add to puzzle container
        const imageWrapper = document.querySelector('.puzzle-image-wrapper');
        
        // Create a container for the reference image
        const referenceContainer = document.createElement('div');
        referenceContainer.className = 'reference-image-container';
        const referenceImg = document.createElement('img');
        referenceImg.id = 'reference-image';
        referenceImg.src = currentPuzzle.reference_image;
        referenceImg.alt = 'Reference direction';
        referenceContainer.appendChild(referenceImg);
        
        // Create a container for the object image
        const objectContainer = document.createElement('div');
        objectContainer.className = 'object-image-container';
        const objectImg = document.createElement('img');
        objectImg.id = 'object-image';
        objectImg.src = currentPuzzle.object_image;
        objectImg.alt = 'Rotatable object';
        objectContainer.appendChild(objectImg);
        
        // Create a two-column layout for rotation puzzle
        const rotationLayout = document.createElement('div');
        rotationLayout.className = 'rotation-layout';
        rotationLayout.appendChild(referenceContainer);
        rotationLayout.appendChild(objectContainer);
        
        // Replace the existing puzzle image
        puzzleImageContainer.innerHTML = '';
        puzzleImageContainer.appendChild(rotationLayout);
        
        // Add rotation controls below the image
        imageWrapper.appendChild(rotationControls);
        
        // Add event listeners for rotation buttons
        leftBtn.addEventListener('click', () => rotateObject(-45));
        rightBtn.addEventListener('click', () => rotateObject(45));
        
        // Set initial angle
        currentRotationAngle = currentPuzzle.current_angle || 0;
        updateObjectRotation();
    }
    
    function rotateObject(angleDelta) {
        // Update the current angle
        currentRotationAngle = (currentRotationAngle + angleDelta) % 360;
        if (currentRotationAngle < 0) {
            currentRotationAngle += 360;
        }
        
        // Apply the rotation
        updateObjectRotation();
        
        // Log for debugging
        console.log('Rotated to:', currentRotationAngle);
    }
    
    function updateObjectRotation() {
        const objectImg = document.getElementById('object-image');
        if (objectImg) {
            // Option 1: Use CSS transform to rotate the image
            objectImg.style.transform = `rotate(${currentRotationAngle}deg)`;
            
            // Option 2: Load a pre-rotated image if available
            // This would require having images at each rotation angle
            const baseName = currentPuzzle.object_base;
            // Find the closest pre-rotated image (0, 90, 180, 270)
            const angles = [0, 45, 90, 135, 180, 225, 270, 315];
            const closestAngle = angles.reduce((prev, curr) => 
                Math.abs(curr - currentRotationAngle) < Math.abs(prev - currentRotationAngle) ? curr : prev
            );
            
            // Load the pre-rotated image
            const rotatedImagePath = `/captcha_data/${currentPuzzle.puzzle_type}/${baseName}_${closestAngle}.png`;
            objectImg.src = rotatedImagePath;
            
            // Apply any additional rotation needed
            const remainingRotation = currentRotationAngle - closestAngle;
            if (remainingRotation !== 0) {
                objectImg.style.transform = `rotate(${remainingRotation}deg)`;
            } else {
                objectImg.style.transform = 'none';
            }
        }
    }

    // Function to set up sliding puzzle
    function setupSlidePuzzle() {
        // Remove any existing controls first
        const existingSlider = document.querySelector('.slider-component');
        if (existingSlider) {
            existingSlider.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Create a container for the background image
        const backgroundContainer = document.createElement('div');
        backgroundContainer.className = 'background-container';
        backgroundContainer.style.position = 'relative';
        backgroundContainer.style.width = '100%';
        backgroundContainer.style.height = 'auto';
        
        // Add background image
        const backgroundImg = document.createElement('img');
        backgroundImg.src = currentPuzzle.background_image;
        backgroundImg.alt = 'Slide puzzle background';
        backgroundImg.style.width = '100%';
        backgroundImg.style.height = 'auto';
        backgroundImg.style.display = 'block';
        backgroundContainer.appendChild(backgroundImg);
        
        // Create draggable slider component
        const sliderComponent = document.createElement('div');
        sliderComponent.className = 'slider-component';
        sliderComponent.style.position = 'absolute';
        sliderComponent.style.cursor = 'move';
        sliderComponent.style.zIndex = '10';
        sliderComponent.style.userSelect = 'none';
        sliderComponent.style.touchAction = 'none';
        sliderComponent.style.width = '50px'; 
        
        // Add component image
        const componentImg = document.createElement('img');
        componentImg.src = currentPuzzle.component_image;
        componentImg.alt = 'Slide component';
        componentImg.style.width = '150%';
        componentImg.style.height = 'auto';
        componentImg.style.display = 'block';
        componentImg.draggable = false; // Prevent default dragging behavior
        sliderComponent.appendChild(componentImg);
        
        // Add slider component to the background container
        backgroundContainer.appendChild(sliderComponent);
        
        // Add the whole setup to the puzzle image container
        puzzleImageContainer.appendChild(backgroundContainer);
        
        // Wait for images to load to get proper dimensions
        backgroundImg.onload = () => {
            // Get container dimensions
            const containerWidth = backgroundImg.width;
            const containerHeight = backgroundImg.height;
            
            // Load component image to get its dimensions
            componentImg.onload = () => {
                const originalComponentWidth = componentImg.naturalWidth;
                const originalComponentHeight = componentImg.naturalHeight;
                
                const componentWidth = containerWidth * 0.08;
                
                const aspectRatio = originalComponentWidth / originalComponentHeight;
                const componentHeight = componentWidth / aspectRatio;
                
                sliderComponent.style.width = `${componentWidth}px`;
                
                // Initial position for the slider component - bottom right corner (far from typical target)
                const initialLeft = containerWidth - componentWidth - 20;
                const initialTop = containerHeight - componentHeight - 20;
                
                sliderComponent.style.left = `${initialLeft}px`;
                sliderComponent.style.top = `${initialTop}px`;
                
                // Initialize current position tracking variables
                currentX = initialLeft;
                currentY = initialTop;
                
                // In debug mode, fetch and show the target area
                if (DEBUG_MODE) {
                    fetch('/api/get_ground_truth', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            puzzle_type: currentPuzzle.puzzle_type,
                            puzzle_id: currentPuzzle.puzzle_id
                        })
                    })
                    .then(response => response.json())
                    .then(gtData => {
                        if (gtData.answer) {
                            // Get tolerance value if available
                            const tolerance = gtData.answer.tolerance || 15; // Default to 15px
                            showSliderTargetArea(gtData.answer, backgroundContainer, tolerance);
                        }
                    })
                    .catch(error => {
                        console.error('Error fetching ground truth:', error);
                    });
                }
            };
        };
        
        // Set up draggable functionality
        let isDragging = false;
        let startX, startY, startLeft, startTop;
        
        // Track current position
        let currentX = 0;
        let currentY = 0;
        
        // Mouse events for desktop
        sliderComponent.addEventListener('mousedown', (e) => {
            isDragging = true;
            startX = e.clientX;
            startY = e.clientY;
            startLeft = parseInt(sliderComponent.style.left) || 0;
            startTop = parseInt(sliderComponent.style.top) || 0;
            sliderComponent.style.opacity = '0.8';
            
            // Prevent default browser behavior
            e.preventDefault();
        });
        
        document.addEventListener('mousemove', (e) => {
            if (!isDragging) return;
            
            // Calculate new position
            const deltaX = e.clientX - startX;
            const deltaY = e.clientY - startY;
            
            // Calculate new position
            let newLeft = startLeft + deltaX;
            let newTop = startTop + deltaY;
            
            // Get container dimensions
            const containerRect = backgroundContainer.getBoundingClientRect();
            const sliderRect = sliderComponent.getBoundingClientRect();
            
            // Ensure the slider stays within the container bounds
            if (newLeft < 0) newLeft = 0;
            if (newTop < 0) newTop = 0;
            if (newLeft > containerRect.width - sliderRect.width) 
                newLeft = containerRect.width - sliderRect.width;
            if (newTop > containerRect.height - sliderRect.height) 
                newTop = containerRect.height - sliderRect.height;
            
            sliderComponent.style.left = `${newLeft}px`;
            sliderComponent.style.top = `${newTop}px`;
            
            // Update current position
            currentX = newLeft;
            currentY = newTop;
        });
        
        document.addEventListener('mouseup', () => {
            if (isDragging) {
                isDragging = false;
                sliderComponent.style.opacity = '1';
                
                // Calculate center point
                const componentRect = componentImg.getBoundingClientRect();
                const centerX = currentX + (componentRect.width / 2);
                const centerY = currentY + (componentRect.height / 2);
                
                // Log final position for debugging
                console.log('Slider final position (top-left):', { x: currentX, y: currentY });
                console.log('Slider center position:', { x: centerX, y: centerY });
            }
        });
        
        // Touch events for mobile
        sliderComponent.addEventListener('touchstart', (e) => {
            isDragging = true;
            startX = e.touches[0].clientX;
            startY = e.touches[0].clientY;
            startLeft = parseInt(sliderComponent.style.left) || 0;
            startTop = parseInt(sliderComponent.style.top) || 0;
            sliderComponent.style.opacity = '0.8';
            
            // Prevent default browser behavior
            e.preventDefault();
        });
        
        document.addEventListener('touchmove', (e) => {
            if (!isDragging) return;
            
            // Calculate new position
            const deltaX = e.touches[0].clientX - startX;
            const deltaY = e.touches[0].clientY - startY;
            
            // Calculate new position
            let newLeft = startLeft + deltaX;
            let newTop = startTop + deltaY;
            
            // Get container dimensions
            const containerRect = backgroundContainer.getBoundingClientRect();
            const sliderRect = sliderComponent.getBoundingClientRect();
            
            // Ensure the slider stays within the container bounds
            if (newLeft < 0) newLeft = 0;
            if (newTop < 0) newTop = 0;
            if (newLeft > containerRect.width - sliderRect.width) 
                newLeft = containerRect.width - sliderRect.width;
            if (newTop > containerRect.height - sliderRect.height) 
                newTop = containerRect.height - sliderRect.height;
            
            sliderComponent.style.left = `${newLeft}px`;
            sliderComponent.style.top = `${newTop}px`;
            
            // Update current position
            currentX = newLeft;
            currentY = newTop;
        });
        
        document.addEventListener('touchend', () => {
            if (isDragging) {
                isDragging = false;
                sliderComponent.style.opacity = '1';
                
                // Calculate center point
                const componentRect = componentImg.getBoundingClientRect();
                const centerX = currentX + (componentRect.width / 2);
                const centerY = currentY + (componentRect.height / 2);
                
                // Log final position for debugging
                console.log('Slider final position (top-left):', { x: currentX, y: currentY });
                console.log('Slider center position:', { x: centerX, y: centerY });
            }
        });
        
        // Add submit button for the sliding puzzle
        const submitSection = document.createElement('div');
        submitSection.className = 'slider-submit';
        const sliderSubmitBtn = document.createElement('button');
        sliderSubmitBtn.textContent = 'Submit';
        sliderSubmitBtn.className = 'submit-slider';
        
        sliderSubmitBtn.addEventListener('click', () => {
            // When submitting, we need to get the final position
            // and normalize it to the image dimensions
            const componentRect = componentImg.getBoundingClientRect();
            
            // Calculate center point of the component
            const centerX = currentX + (componentRect.width / 2);
            const centerY = currentY + (componentRect.height / 2);
            
            // Submit this position
            console.log('Submitting slider position:', { x: centerX, y: centerY });
            submitSliderPosition(centerX, centerY);
        });
        
        submitSection.appendChild(sliderSubmitBtn);
        
        // Add to puzzle container
        const imageWrapper = document.querySelector('.puzzle-image-wrapper');
        imageWrapper.appendChild(submitSection);
    }
    
    // Function to show the target area for the slider in debug mode
    function showSliderTargetArea(targetPosition, container, tolerance = 15) {
        if (!DEBUG_MODE || !targetPosition) return;
        
        // Remove any existing debug targets
        const existingTarget = document.querySelector('.target-area');
        if (existingTarget) {
            existingTarget.remove();
        }
        
        // Create a target element
        const targetArea = document.createElement('div');
        targetArea.className = 'target-area';
        
        // Get target coordinates
        const [targetX, targetY] = targetPosition;
        
        // We'll visualize this as a circle
        const diameter = tolerance * 2;
        
        // Style the target area
        targetArea.style.position = 'absolute';
        targetArea.style.left = `${targetX - tolerance}px`;
        targetArea.style.top = `${targetY - tolerance}px`;
        targetArea.style.width = `${diameter}px`;
        targetArea.style.height = `${diameter}px`;
        targetArea.style.borderRadius = '50%';
        targetArea.style.border = '2px dashed green';
        targetArea.style.backgroundColor = 'rgba(0, 255, 0, 0.2)';
        targetArea.style.zIndex = '5';
        targetArea.style.pointerEvents = 'none'; // Allow clicks to pass through
        
        // Add coordinates label
        const coordsLabel = document.createElement('div');
        coordsLabel.className = 'coords-label';
        coordsLabel.textContent = `Target: (${targetX}, ${targetY}) ±${tolerance}px`;
        coordsLabel.style.position = 'absolute';
        coordsLabel.style.top = '-25px';
        coordsLabel.style.left = '0';
        coordsLabel.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        coordsLabel.style.color = 'white';
        coordsLabel.style.padding = '2px 5px';
        coordsLabel.style.fontSize = '10px';
        coordsLabel.style.borderRadius = '3px';
        coordsLabel.style.whiteSpace = 'nowrap';
        targetArea.appendChild(coordsLabel);
        
        // Add to the container
        container.appendChild(targetArea);
        
        // Log the target details
        console.log('Target position:', { 
            x: targetX, 
            y: targetY,
            tolerance: tolerance
        });
    }

    // Function to submit slider position
    function submitSliderPosition(x, y) {
        if (!currentPuzzle) {
            resultMessage.textContent = 'Loading puzzle, please wait...';
            resultMessage.className = 'result-message incorrect';
            return;
        }
        
        // Send position to the server for verification
        fetch('/api/check_answer', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                puzzle_type: currentPuzzle.puzzle_type,
                puzzle_id: currentPuzzle.puzzle_id,
                answer: [x, y]
            })
        })
        .then(response => response.json())
        .then(data => {
            // Update stats
            benchmarkStats.total++;
            if (data.correct) {
                benchmarkStats.correct++;
                resultMessage.textContent = 'Correct! The slider was placed in the right position.';
                resultMessage.className = 'result-message correct';
            } else {
                resultMessage.textContent = 'Incorrect. Please try again with a better position.';
                resultMessage.className = 'result-message incorrect';
            }
            
            updateStats();
            
            // Record benchmark result
            recordBenchmarkResult({
                puzzle_type: currentPuzzle.puzzle_type,
                puzzle_id: currentPuzzle.puzzle_id,
                user_answer: [x, y],
                correct_answer: data.correct_answer,
                correct: data.correct
            });
            
            // Disable the submit button to prevent multiple submissions
            const submitBtn = document.querySelector('.submit-slider');
            if (submitBtn) {
                submitBtn.disabled = true;
            }
            
            // Also disable rotation submit button if it exists
            const rotateSubmitBtn = document.querySelector('.submit-rotation');
            if (rotateSubmitBtn) {
                rotateSubmitBtn.disabled = true;
            }
            
            // Also disable image recognition submit button if it exists
            const imageRecognitionSubmitBtn = document.querySelector('.submit-image-recognition');
            if (imageRecognitionSubmitBtn) {
                imageRecognitionSubmitBtn.disabled = true;
            }
            
            // Also disable bingo submit button if it exists
            const bingoSubmitBtn = document.querySelector('.submit-bingo');
            if (bingoSubmitBtn) {
                bingoSubmitBtn.disabled = true;
            }
            
            // Also disable image matching submit button if it exists
            const imageMatchingSubmitBtn = document.querySelector('.submit-image-matching');
            if (imageMatchingSubmitBtn) {
                imageMatchingSubmitBtn.disabled = true;
            }
            
            // Load a new puzzle after a delay
            setTimeout(loadNewPuzzle, 2000);
        })
        .catch(error => {
            console.error('Error checking answer:', error);
            resultMessage.textContent = 'Error checking answer. Please try again.';
            resultMessage.className = 'result-message incorrect';
        });
    }

    // Add this new function to show the ground truth area
    function showGroundTruthArea(answer) {
        if (!DEBUG_MODE) return;
        
        // Remove any existing debug areas
        const existingArea = document.querySelector('.debug-area');
        if (existingArea) {
            existingArea.remove();
        }
        
        // Create and style the debug area element
        const debugArea = document.createElement('div');
        debugArea.className = 'debug-area';
        debugArea.style.position = 'absolute';
        debugArea.style.pointerEvents = 'none'; // Allow clicks to pass through
        debugArea.style.zIndex = '5';
        
        if (answer && answer.area) {
            // For standard area format (geometry_click, etc.)
            const [[x1, y1], [x2, y2]] = answer.area;
            
            debugArea.style.left = `${x1}px`;
            debugArea.style.top = `${y1}px`;
            debugArea.style.width = `${x2 - x1}px`;
            debugArea.style.height = `${y2 - y1}px`;
            debugArea.style.border = '2px dashed yellow';
            debugArea.style.backgroundColor = 'rgba(255, 255, 0, 0.2)';
            
            // Add coordinates label
            const coordsLabel = document.createElement('div');
            coordsLabel.className = 'coords-label';
            coordsLabel.textContent = `TL: (${x1},${y1}) BR: (${x2},${y2})`;
            coordsLabel.style.position = 'absolute';
            coordsLabel.style.bottom = '0';
            coordsLabel.style.right = '0';
            coordsLabel.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
            coordsLabel.style.color = 'white';
            coordsLabel.style.padding = '2px 5px';
            coordsLabel.style.fontSize = '10px';
            coordsLabel.style.borderRadius = '3px';
            debugArea.appendChild(coordsLabel);
            
            // Log the area details
            console.log('Ground truth area:', { 
                topLeft: [x1, y1], 
                bottomRight: [x2, y2], 
                width: x2 - x1, 
                height: y2 - y1,
                type: answer.type
            });
        } else if (answer && answer.avoid_area) {
            // For Misleading_Click avoid_area format
            const { x, y, width, height } = answer.avoid_area;
            
            debugArea.style.left = `${x}px`;
            debugArea.style.top = `${y}px`;
            debugArea.style.width = `${width}px`;
            debugArea.style.height = `${height}px`;
            debugArea.style.border = '3px dashed red';
            debugArea.style.backgroundColor = 'rgba(255, 0, 0, 0.3)';
            
            // Add coordinates label
            const coordsLabel = document.createElement('div');
            coordsLabel.className = 'coords-label';
            coordsLabel.textContent = `Avoid Area: (${x},${y}) ${width}x${height}`;
            coordsLabel.style.position = 'absolute';
            coordsLabel.style.bottom = '0';
            coordsLabel.style.right = '0';
            coordsLabel.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
            coordsLabel.style.color = 'white';
            coordsLabel.style.padding = '2px 5px';
            coordsLabel.style.fontSize = '10px';
            coordsLabel.style.borderRadius = '3px';
            debugArea.appendChild(coordsLabel);
            
            // Add a "DO NOT CLICK HERE" sign in the middle of the area
            const warningSign = document.createElement('div');
            warningSign.className = 'warning-sign';
            warningSign.textContent = 'DO NOT CLICK HERE';
            warningSign.style.position = 'absolute';
            warningSign.style.top = '50%';
            warningSign.style.left = '50%';
            warningSign.style.transform = 'translate(-50%, -50%)';
            warningSign.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
            warningSign.style.color = '#ff5555';
            warningSign.style.padding = '5px 10px';
            warningSign.style.fontSize = '12px';
            warningSign.style.fontWeight = 'bold';
            warningSign.style.borderRadius = '3px';
            warningSign.style.whiteSpace = 'nowrap';
            warningSign.style.zIndex = '10';
            debugArea.appendChild(warningSign);
            
            // Log the area details
            console.log('Avoid area:', { x, y, width, height });
        } else {
            // If we don't have a valid format, don't show anything
            return;
        }
        
        // Add to the image container
        puzzleImageContainer.appendChild(debugArea);
    }

    // Function to fetch and show geometry click target area
    function fetchAndShowGeometryClickArea(container) {
        if (!DEBUG_MODE || !currentPuzzle) return;
        
        // Fetch ground truth data to show the correct geometric shape area
        fetch('/api/get_ground_truth', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                puzzle_type: currentPuzzle.puzzle_type,
                puzzle_id: currentPuzzle.puzzle_id
            })
        })
        .then(response => response.json())
        .then(gtData => {
            if (gtData.answer) {
                // Call showGroundTruthArea with the answer data
                showGroundTruthArea(gtData.answer);
                
                // Log for debugging
                console.log('Geometry_Click ground truth fetched:', gtData.answer);
            }
        })
        .catch(error => {
            console.error('Error fetching ground truth for Geometry_Click:', error);
        });
    }

    function showClickMarker(x, y) {
        // Remove any existing markers
        const existingMarker = document.querySelector('.click-marker');
        if (existingMarker) {
            existingMarker.remove();
        }
        
        // Create and add new marker
        const marker = document.createElement('div');
        marker.className = 'click-marker';
        marker.style.left = `${x}px`;
        marker.style.top = `${y}px`;
        
        // Add coordinates label to the marker
        const coordsLabel = document.createElement('div');
        coordsLabel.className = 'coords-label';
        coordsLabel.textContent = `(${x},${y})`;
        coordsLabel.style.position = 'absolute';
        coordsLabel.style.top = '20px';
        coordsLabel.style.left = '20px';
        coordsLabel.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        coordsLabel.style.color = 'white';
        coordsLabel.style.padding = '2px 5px';
        coordsLabel.style.fontSize = '10px';
        coordsLabel.style.borderRadius = '3px';
        coordsLabel.style.whiteSpace = 'nowrap';
        marker.appendChild(coordsLabel);
        
        // Add it directly to the image container for proper positioning
        puzzleImageContainer.appendChild(marker);
        
        // Log for debugging
        console.log('Marker placed at:', { x, y });
        
        // Check if this is a Misleading_Click puzzle and we're in debug mode
        if (DEBUG_MODE && currentPuzzle && currentPuzzle.puzzle_type === 'Misleading_Click' && currentPuzzle.avoid_area) {
            // Get the avoid area
            const { x: areaX, y: areaY, width: areaWidth, height: areaHeight } = currentPuzzle.avoid_area;
            
            // Check if click is within the avoid area
            const inAvoidArea = (
                areaX <= x && x <= areaX + areaWidth &&
                areaY <= y && y <= areaY + areaHeight
            );
            
            // Add status indicator
            const statusIndicator = document.createElement('div');
            statusIndicator.className = 'click-status';
            statusIndicator.style.position = 'absolute';
            statusIndicator.style.top = '40px';
            statusIndicator.style.left = '20px';
            statusIndicator.style.padding = '3px 6px';
            statusIndicator.style.borderRadius = '3px';
            statusIndicator.style.fontSize = '10px';
            statusIndicator.style.fontWeight = 'bold';
            
            if (inAvoidArea) {
                statusIndicator.textContent = 'INSIDE AVOID AREA - WRONG';
                statusIndicator.style.backgroundColor = 'rgba(255, 0, 0, 0.8)';
                statusIndicator.style.color = 'white';
                marker.style.borderColor = 'red';
            } else {
                statusIndicator.textContent = 'OUTSIDE AVOID AREA - CORRECT';
                statusIndicator.style.backgroundColor = 'rgba(0, 255, 0, 0.8)';
                statusIndicator.style.color = 'black';
                marker.style.borderColor = 'green';
            }
            
            marker.appendChild(statusIndicator);
            
            // Log result
            console.log('Click check:', { inAvoidArea, message: inAvoidArea ? 'INSIDE avoid area (incorrect)' : 'OUTSIDE avoid area (correct)' });
        }
    }

    // Function to set up unusual detection grid
    function setupUnusualDetectionGrid() {
        // Remove any existing grid
        const existingGrid = document.querySelector('.unusual-detection-grid');
        if (existingGrid) {
            existingGrid.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Get the grid dimensions from the current puzzle data
        const gridSize = currentPuzzle.grid_size || [2, 3]; // Default to 2x3 if not specified
        const [rows, cols] = gridSize;
        
        // Create the grid container
        const gridContainer = document.createElement('div');
        gridContainer.className = 'unusual-detection-grid';
        gridContainer.style.display = 'grid';
        gridContainer.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
        gridContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
        gridContainer.style.gap = '2px';
        gridContainer.style.width = '100%';
        gridContainer.style.aspectRatio = `${cols} / ${rows}`;
        
        // First, load the full image to get its dimensions
        const fullImg = new Image();
        fullImg.onload = () => {
            const imgWidth = fullImg.width;
            const imgHeight = fullImg.height;
            const cellWidth = imgWidth / cols;
            const cellHeight = imgHeight / rows;
            
            // Create individual image elements for each cell
            const totalCells = rows * cols;
            for (let i = 0; i < totalCells; i++) {
                const cell = document.createElement('div');
                cell.className = 'grid-cell';
                cell.dataset.index = i;
                cell.style.position = 'relative';
                cell.style.border = '2px solid #333';
                cell.style.cursor = 'pointer';
                cell.style.overflow = 'hidden';
                
                // Create an individual image for this cell
                const cellImg = document.createElement('img');
                cellImg.className = 'cell-image';
                cellImg.style.width = '100%';
                cellImg.style.height = '100%';
                cellImg.style.objectFit = 'cover';
                cellImg.style.display = 'block';
                cell.appendChild(cellImg);
                
                // Calculate which part of the source image this cell represents
                const row = Math.floor(i / cols);
                const col = i % cols;
                
                // Create a canvas to extract just this portion of the image
                const canvas = document.createElement('canvas');
                canvas.width = cellWidth;
                canvas.height = cellHeight;
                const ctx = canvas.getContext('2d');
                
                // Draw just the portion we want
                ctx.drawImage(
                    fullImg,
                    col * cellWidth, row * cellHeight, // Source x, y
                    cellWidth, cellHeight, // Source width, height
                    0, 0, // Destination x, y
                    cellWidth, cellHeight // Destination width, height
                );
                
                // Set the cell image source to this canvas data
                cellImg.src = canvas.toDataURL();
                
                // Create an overlay for selection state
                const overlay = document.createElement('div');
                overlay.className = 'cell-overlay';
                overlay.style.position = 'absolute';
                overlay.style.top = '0';
                overlay.style.left = '0';
                overlay.style.width = '100%';
                overlay.style.height = '100%';
                overlay.style.backgroundColor = 'rgba(0, 120, 255, 0.5)';
                overlay.style.opacity = '0';
                overlay.style.transition = 'opacity 0.2s ease';
                overlay.style.pointerEvents = 'none';
                cell.appendChild(overlay);
                
                // Add a checkmark icon to indicate selection
                const checkmark = document.createElement('div');
                checkmark.className = 'checkmark';
                checkmark.innerHTML = '✓';
                checkmark.style.position = 'absolute';
                checkmark.style.top = '50%';
                checkmark.style.left = '50%';
                checkmark.style.transform = 'translate(-50%, -50%)';
                checkmark.style.color = 'white';
                checkmark.style.fontSize = '32px';
                checkmark.style.fontWeight = 'bold';
                checkmark.style.opacity = '0';
                checkmark.style.transition = 'opacity 0.2s ease';
                checkmark.style.pointerEvents = 'none';
                cell.appendChild(checkmark);
                
                // Add click event handler for selection
                cell.addEventListener('click', (e) => {
                    toggleCellSelection(i, cell);
                });
                
                // Add the cell to the grid
                gridContainer.appendChild(cell);
            }
            
            // Add the grid to the puzzle image container
            puzzleImageContainer.appendChild(gridContainer);
            
            // Add a submit button below the grid
            const submitSection = document.createElement('div');
            submitSection.className = 'unusual-submit';
            submitSection.style.textAlign = 'center';
            submitSection.style.marginTop = '15px';
            
            const unusualSubmitBtn = document.createElement('button');
            unusualSubmitBtn.textContent = 'Submit';
            unusualSubmitBtn.className = 'submit-unusual';
            unusualSubmitBtn.addEventListener('click', submitAnswer);
            submitSection.appendChild(unusualSubmitBtn);
            
            // Add to puzzle container
            const imageWrapper = document.querySelector('.puzzle-image-wrapper');
            imageWrapper.appendChild(submitSection);
            
            // Reset selected cells
            selectedCells = [];
        };
        
        // Set the source to load the image
        fullImg.src = currentPuzzle.image_path;
        fullImg.style.display = 'none';
    }
    
    function toggleCellSelection(index, cellElement) {
        // Check if this cell is already selected
        const isSelected = selectedCells.includes(index);
        
        if (isSelected) {
            // Deselect the cell
            selectedCells = selectedCells.filter(i => i !== index);
            cellElement.querySelector('.cell-overlay').style.opacity = '0';
            cellElement.querySelector('.checkmark').style.opacity = '0';
            cellElement.style.transform = 'scale(1)';
            cellElement.style.borderColor = '#333';
        } else {
            // Select the cell
            selectedCells.push(index);
            cellElement.querySelector('.cell-overlay').style.opacity = '1';
            cellElement.querySelector('.checkmark').style.opacity = '1';
            cellElement.style.transform = 'scale(0.95)';
            cellElement.style.borderColor = '#0078ff';
        }
        
        console.log('Selected cells:', selectedCells);
    }

    // Function to set up Bingo swap puzzle
    function setupBingoSwap() {
        // Remove any existing grid
        const existingGrid = document.querySelector('.bingo-grid');
        if (existingGrid) {
            existingGrid.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Get the grid dimensions from the current puzzle data
        const gridSize = currentPuzzle.grid_size || [3, 3]; // Default to 3x3 if not specified
        const [rows, cols] = gridSize;
        
        // Create the grid container
        const gridContainer = document.createElement('div');
        gridContainer.className = 'bingo-grid';
        gridContainer.style.display = 'grid';
        gridContainer.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
        gridContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
        gridContainer.style.gap = '2px';
        gridContainer.style.width = '100%';
        gridContainer.style.aspectRatio = `${cols} / ${rows}`;
        
        // First, load the full image to get its dimensions
        const fullImg = new Image();
        fullImg.onload = () => {
            const imgWidth = fullImg.width;
            const imgHeight = fullImg.height;
            const cellWidth = imgWidth / cols;
            const cellHeight = imgHeight / rows;
            
            // Create individual image elements for each cell
            const totalCells = rows * cols;
            for (let i = 0; i < totalCells; i++) {
                const cell = document.createElement('div');
                cell.className = 'grid-cell';
                cell.dataset.index = i;
                cell.style.position = 'relative';
                cell.style.border = '2px solid #333';
                cell.style.cursor = 'pointer';
                cell.style.overflow = 'hidden';
                
                // Create an individual image for this cell
                const cellImg = document.createElement('img');
                cellImg.className = 'cell-image';
                cellImg.style.width = '100%';
                cellImg.style.height = '100%';
                cellImg.style.objectFit = 'cover';
                cellImg.style.display = 'block';
                cell.appendChild(cellImg);
                
                // Calculate which part of the source image this cell represents
                const row = Math.floor(i / cols);
                const col = i % cols;
                
                // Create a canvas to extract just this portion of the image
                const canvas = document.createElement('canvas');
                canvas.width = cellWidth;
                canvas.height = cellHeight;
                const ctx = canvas.getContext('2d');
                
                // Draw just the portion we want
                ctx.drawImage(
                    fullImg,
                    col * cellWidth, row * cellHeight, // Source x, y
                    cellWidth, cellHeight, // Source width, height
                    0, 0, // Destination x, y
                    cellWidth, cellHeight // Destination width, height
                );
                
                // Create a data URL and set it as the image source
                cellImg.src = canvas.toDataURL();
                
                // Create an overlay for selection state
                const overlay = document.createElement('div');
                overlay.className = 'cell-overlay';
                overlay.style.position = 'absolute';
                overlay.style.top = '0';
                overlay.style.left = '0';
                overlay.style.width = '100%';
                overlay.style.height = '100%';
                overlay.style.backgroundColor = 'rgba(0, 120, 255, 0.5)';
                overlay.style.opacity = '0';
                overlay.style.transition = 'opacity 0.2s ease';
                overlay.style.pointerEvents = 'none';
                cell.appendChild(overlay);
                
                // Add click handler for selection
                cell.addEventListener('click', (e) => {
                    toggleBingoCellSelection(i, cell);
                });
                
                // Add the cell to the grid
                gridContainer.appendChild(cell);
            }
            
            // Add the grid to the puzzle image container
            puzzleImageContainer.appendChild(gridContainer);
            
            // Add a submit button below the grid
            const submitSection = document.createElement('div');
            submitSection.className = 'bingo-submit';
            submitSection.style.textAlign = 'center';
            submitSection.style.marginTop = '15px';
            
            const bingoSubmitBtn = document.createElement('button');
            bingoSubmitBtn.textContent = 'Swap and Submit';
            bingoSubmitBtn.className = 'submit-bingo';
            bingoSubmitBtn.addEventListener('click', () => {
                if (bingoSelectedCells.length === 2) {
                    // Visually swap the cells
                    swapBingoCells();
                    // Submit the answer
                    setTimeout(submitAnswer, 500);
                } else {
                    resultMessage.textContent = 'Please select exactly two cells to swap.';
                    resultMessage.className = 'result-message error';
                }
            });
            submitSection.appendChild(bingoSubmitBtn);
            
            // Add to puzzle container
            const imageWrapper = document.querySelector('.puzzle-image-wrapper');
            imageWrapper.appendChild(submitSection);
            
            // Reset selected cells
            bingoSelectedCells = [];
        };
        
        // Set the source to load the image
        fullImg.src = currentPuzzle.image_path;
        fullImg.style.display = 'none';
    }

    function toggleBingoCellSelection(index, cellElement) {
        const overlay = cellElement.querySelector('.cell-overlay');
        
        // Check if this cell is already selected
        const selectedIndex = bingoSelectedCells.indexOf(index);
        
        if (selectedIndex !== -1) {
            // If already selected, unselect it
            bingoSelectedCells.splice(selectedIndex, 1);
            overlay.style.opacity = '0';
        } else {
            // If we already have 2 selected cells, remove the first one
            if (bingoSelectedCells.length >= 2) {
                const firstCell = document.querySelector(`.grid-cell[data-index="${bingoSelectedCells[0]}"]`);
                if (firstCell) {
                    firstCell.querySelector('.cell-overlay').style.opacity = '0';
                }
                bingoSelectedCells.shift(); // Remove the first element
            }
            
            // Add this cell to selected
            bingoSelectedCells.push(index);
            overlay.style.opacity = '0.5';
        }
        
        console.log('Selected cells for Bingo:', bingoSelectedCells);
    }

    function swapBingoCells() {
        if (bingoSelectedCells.length !== 2) return;
        
        // Get the two cells to swap
        const cell1 = document.querySelector(`.grid-cell[data-index="${bingoSelectedCells[0]}"]`);
        const cell2 = document.querySelector(`.grid-cell[data-index="${bingoSelectedCells[1]}"]`);
        
        if (!cell1 || !cell2) return;
        
        // Get the images inside the cells
        const img1 = cell1.querySelector('.cell-image');
        const img2 = cell2.querySelector('.cell-image');
        
        if (!img1 || !img2) return;
        
        // Swap the image sources
        const tempSrc = img1.src;
        img1.src = img2.src;
        img2.src = tempSrc;
        
        // Apply a highlight to the solution line if it exists
        if (currentPuzzle.solution_line) {
            // Get the answer from the ground truth
            const correctSwaps = currentPuzzle.answer;
            const selectedSwapSet = new Set(bingoSelectedCells);
            
            // Check which solution was achieved by comparing our selection with possible answers
            let solutionKey = null;
            
            // Check vertical solution
            if (currentPuzzle.solution_line.vertical && 
                checkIfSolutionMatches(correctSwaps, selectedSwapSet)) {
                solutionKey = 'vertical';
            } 
            // Check horizontal solution
            else if (currentPuzzle.solution_line.horizontal && 
                checkIfSolutionMatches(correctSwaps, selectedSwapSet)) {
                solutionKey = 'horizontal';
            }
            // Check diagonal solution
            else if (currentPuzzle.solution_line.diagonal && 
                checkIfSolutionMatches(correctSwaps, selectedSwapSet)) {
                solutionKey = 'diagonal';
            }
            
            // If we found a matching solution, highlight it
            if (solutionKey && currentPuzzle.solution_line[solutionKey]) {
                for (const cellIndex of currentPuzzle.solution_line[solutionKey]) {
                    const solutionCell = document.querySelector(`.grid-cell[data-index="${cellIndex}"]`);
                    if (solutionCell) {
                        solutionCell.style.border = '2px solid green';
                    }
                }
            }
        }
    }
    
    // Helper function to check if selected cells match any solution
    function checkIfSolutionMatches(correctSwaps, selectedSwapSet) {
        // Go through each possible correct swap and check if our selection matches any of them
        for (const correctSwap of correctSwaps) {
            const correctSwapSet = new Set(correctSwap);
            // Check if our selected cells match this solution (order doesn't matter)
            if (setsEqual(selectedSwapSet, correctSwapSet)) {
                return true;
            }
        }
        return false;
    }
    
    // Helper function to compare sets for equality
    function setsEqual(set1, set2) {
        if (set1.size !== set2.size) return false;
        for (const item of set1) {
            if (!set2.has(item)) return false;
        }
        return true;
    }

    // // Function to set up the debug mode selector

    // function setupDebugModeSelector() {
    //     // Create the debug selector container
    //     const debugContainer = document.createElement('div');
    //     debugContainer.className = 'debug-selector';
    //     debugContainer.style.marginTop = '10px';
    //     debugContainer.style.marginBottom = '10px';
    //     debugContainer.style.padding = '10px';
    //     debugContainer.style.backgroundColor = '#f0f0f0';
    //     debugContainer.style.borderRadius = '4px';
    //     debugContainer.style.display = 'flex';
    //     debugContainer.style.alignItems = 'center';
    //     debugContainer.style.justifyContent = 'center';
    //     debugContainer.style.flexWrap = 'wrap';
        
    //     // Create a label
    //     const label = document.createElement('label');
    //     label.htmlFor = 'debug-type-selector';
    //     label.textContent = 'Puzzle Type: ';
    //     label.style.marginRight = '10px';
    //     label.style.fontWeight = 'bold';
        
    //     // Create the select element
    //     const select = document.createElement('select');
    //     select.id = 'debug-type-selector';
    //     select.style.padding = '5px';
    //     select.style.marginRight = '10px';
        
    //     // Default option - random puzzles
    //     const defaultOption = document.createElement('option');
    //     defaultOption.value = '';
    //     defaultOption.textContent = 'Random (All Types)';
    //     select.appendChild(defaultOption);
        
    //     // Fetch available CAPTCHA types from the API
    //     fetch('/api/types')
    //         .then(response => response.json())
    //         .then(data => {
    //             if (data.types && data.types.length > 0) {
    //                 // Add options for each CAPTCHA type
    //                 data.types.forEach(type => {
    //                     const option = document.createElement('option');
    //                     option.value = type;
    //                     option.textContent = type;
    //                     select.appendChild(option);
    //                 });
                    
    //                 // Check if there's a debug type in URL parameters
    //                 const urlParams = new URLSearchParams(window.location.search);
    //                 const typeParam = urlParams.get('type');
    //                 if (typeParam) {
    //                     select.value = typeParam;
    //                     debugPuzzleType = typeParam;
    //                 }
    //             }
    //         })
    //         .catch(error => {
    //             console.error('Error fetching CAPTCHA types:', error);
    //         });
        
    //     // Create apply button
    //     const applyBtn = document.createElement('button');
    //     applyBtn.textContent = 'Apply';
    //     applyBtn.style.padding = '5px 10px';
    //     applyBtn.style.backgroundColor = '#4CAF50';
    //     applyBtn.style.color = 'white';
    //     applyBtn.style.border = 'none';
    //     applyBtn.style.borderRadius = '4px';
    //     applyBtn.style.cursor = 'pointer';
        
    //     // Add event listener to the button
    //     applyBtn.addEventListener('click', () => {
    //         debugPuzzleType = select.value;
    //         // Update URL parameter
    //         const url = new URL(window.location);
    //         if (debugPuzzleType) {
    //             url.searchParams.set('type', debugPuzzleType);
    //             // Show the debug indicator
    //             const debugIndicator = document.getElementById('debug-indicator');
    //             const debugTypeDisplay = document.getElementById('debug-type-display');
    //             if (debugIndicator && debugTypeDisplay) {
    //                 debugTypeDisplay.textContent = debugPuzzleType;
    //                 debugIndicator.style.display = 'block';
    //             }
    //         } else {
    //             url.searchParams.delete('type');
    //             // Hide the debug indicator
    //             const debugIndicator = document.getElementById('debug-indicator');
    //             if (debugIndicator) {
    //                 debugIndicator.style.display = 'none';
    //             }
    //         }
    //         window.history.pushState({}, '', url);
            
    //         // Load a new puzzle with the selected type
    //         loadNewPuzzle();
    //     });
        
    //     // Initialize the debug indicator if there's a type parameter
    //     if (debugPuzzleType) {
    //         const debugIndicator = document.getElementById('debug-indicator');
    //         const debugTypeDisplay = document.getElementById('debug-type-display');
    //         if (debugIndicator && debugTypeDisplay) {
    //             debugTypeDisplay.textContent = debugPuzzleType;
    //             debugIndicator.style.display = 'block';
    //         }
    //     }
        
    //     // Add elements to container
    //     debugContainer.appendChild(label);
    //     debugContainer.appendChild(select);
    //     debugContainer.appendChild(applyBtn);
        
    //     // Add container to the benchmark stats section
    //     const benchmarkStats = document.querySelector('.benchmark-stats');
    //     benchmarkStats.parentNode.insertBefore(debugContainer, benchmarkStats.nextSibling);
    // }
     // // Function to set up the debug mode selector



    function loadNewPuzzle() {
        // Reset state
        clickCoordinates = null;
        processingClick = false;
        currentRotationAngle = 0;
        selectedCells = [];
        bingoSelectedCells = [];
        
        // Remove any click markers and debug areas
        const existingMarker = document.querySelector('.click-marker');
        if (existingMarker) {
            existingMarker.remove();
        }
        
        const existingArea = document.querySelector('.debug-area');
        if (existingArea) {
            existingArea.remove();
        }
        
        // Remove any rotation controls
        const existingControls = document.querySelector('.rotation-controls');
        if (existingControls) {
            existingControls.remove();
        }
        
        // Remove any existing rotation submit buttons
        const existingRotationSubmit = document.querySelector('.rotation-submit');
        if (existingRotationSubmit) {
            existingRotationSubmit.remove();
        }
        
        // Remove any slider components and submit buttons
        const existingSliderSubmit = document.querySelector('.slider-submit');
        if (existingSliderSubmit) {
            existingSliderSubmit.remove();
        }
        
        // Remove any unusual detection grid and submit buttons
        const existingUnusualSubmit = document.querySelector('.unusual-submit');
        if (existingUnusualSubmit) {
            existingUnusualSubmit.remove();
        }
        
        // Remove any image recognition grid and submit buttons
        const existingImageRecognitionSubmit = document.querySelector('.image-recognition-submit');
        if (existingImageRecognitionSubmit) {
            existingImageRecognitionSubmit.remove();
        }
        
        // Remove any bingo grid and submit buttons
        const existingBingoSubmit = document.querySelector('.bingo-submit');
        if (existingBingoSubmit) {
            existingBingoSubmit.remove();
        }
        
        // After checking and removing existingImageMatchingControls
        const existingImageMatchingControls = document.querySelector('.image-matching-controls');
        if (existingImageMatchingControls) {
            existingImageMatchingControls.remove();
        }
        
        const existingImageMatchingSubmit = document.querySelector('.image-matching-submit');
        if (existingImageMatchingSubmit) {
            existingImageMatchingSubmit.remove();
        }
        
        // Remove any dart count controls and submit buttons
        const existingDartCountSubmit = document.querySelector('.dart-count-submit');
        if (existingDartCountSubmit) {
            existingDartCountSubmit.remove();
        }
        
        // Remove any object match controls and submit buttons
        const existingObjectMatchSubmit = document.querySelector('.object-match-submit');
        if (existingObjectMatchSubmit) {
            existingObjectMatchSubmit.remove();
        }
        
        // Remove any connect icon controls and submit buttons
        const existingConnectIconSubmit = document.querySelector('.connect-icon-submit');
        if (existingConnectIconSubmit) {
            existingConnectIconSubmit.remove();
        }
        
        // Remove any hold button components
        const existingHoldButton = document.querySelector('.hold-button-container');
        if (existingHoldButton) {
            existingHoldButton.remove();
        }
        
        // Reset the puzzle prompt and image
        puzzlePrompt.textContent = 'Loading puzzle...';
        resultMessage.textContent = '';
        resultMessage.className = 'result-message';
        
        // Reset the submit button text
        submitBtn.textContent = 'Submit';
        submitBtn.disabled = false;
        
        // Reset input field display
        userAnswerInput.style.display = 'block';
        
        // Construct URL with debug type parameter if set
        let url = '/api/get_puzzle?mode=sequential';
        // // Function to set up the debug mode selector
        // if (debugPuzzleType) {
        //     url = `/api/get_puzzle?debug_type=${encodeURIComponent(debugPuzzleType)}`;
        // }
        
        // Get a random puzzle from any available type
        fetch(url)
            .then(response => response.json())
            .then(data => {
                console.log("Received puzzle data:", data);
                currentPuzzle = data;
                
                // Update the puzzle prompt
                if (data.prompt) {
                    puzzlePrompt.textContent = data.prompt;
                } else if (data.puzzle_type === 'Dice_Count') {
                    puzzlePrompt.textContent = "Sum up the numbers on all the dice";
                }
                
                // Important: Always display difficulty stars based on puzzle type
                displayDifficultyStars(data.puzzle_type);
                
                // Reset container
                puzzleImageContainer.innerHTML = '';
                
                // Configure input based on puzzle type
                if (data.input_type === 'click') {
                    // Setup for click-based CAPTCHAs (Geometry_Click, Misleading_Click, Pick_Area)
                    puzzleImage.src = data.image_path;
                    inputGroup.style.display = 'none';
                    puzzleImage.style.cursor = 'pointer';
                    puzzleImage.classList.add('clickable');
                    
                    // Add puzzle image back to container
                    if (puzzleImageContainer.innerHTML === '') {
                        puzzleImageContainer.appendChild(puzzleImage);
                    }
                    
                    puzzleImageContainer.style.display = 'block';
                    puzzleImage.style.display = 'block';
                    
                    // Reset click coordinates for new puzzle
                    clickCoordinates = null;
                    
                    // Update prompt text
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else if (data.puzzle_type === 'Geometry_Click') {
                        puzzlePrompt.textContent = "Click on the geometric shape";
                    } else if (data.puzzle_type === 'Misleading_Click') {
                        puzzlePrompt.textContent = "Click the image to continue";
                        
                        // Make sure avoid_area is stored in currentPuzzle object
                        if (data.avoid_area) {
                            currentPuzzle.avoid_area = data.avoid_area;
                            console.log('Loaded avoid_area:', data.avoid_area);
                        }
                    } else if (data.puzzle_type === 'Pick_Area') {
                        puzzlePrompt.textContent = "Click on the largest area outlined by the dotted line";
                    }
                    
                    // For debugging, when image loads, show the target areas
                    puzzleImage.onload = () => {
                        if (DEBUG_MODE) {
                            // Show ground truth area differently based on puzzle type
                            if (data.puzzle_type === 'Pick_Area') {
                                showPickAreaTargets(puzzleImageContainer);
                            } else if (data.puzzle_type === 'Geometry_Click') {
                                fetchAndShowGeometryClickArea(puzzleImageContainer);
                            } else if (data.puzzle_type === 'Misleading_Click') {
                                // For misleading click, show the area to avoid
                                if (data.avoid_area) {
                                    showMisleadingClickArea(puzzleImageContainer, data.avoid_area);
                                }
                            }
                        }
                    };
                } else if (data.input_type === 'rotation') {
                    // Setup for rotation-based CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt first to ensure it's from the rotation puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Use the arrows to rotate the object to match the reference direction.";
                    }
                    
                    // Set up rotation interface
                    setupRotationControls();
                    
                    // Auto-show submit button for rotation puzzles
                    const submitSection = document.createElement('div');
                    submitSection.className = 'rotation-submit';
                    const rotateSubmitBtn = document.createElement('button');
                    rotateSubmitBtn.textContent = 'Submit';
                    rotateSubmitBtn.className = 'submit-rotation';
                    rotateSubmitBtn.addEventListener('click', submitAnswer);
                    submitSection.appendChild(rotateSubmitBtn);
                    
                    // Add to puzzle container
                    const imageWrapper = document.querySelector('.puzzle-image-wrapper');
                    imageWrapper.appendChild(submitSection);
                } else if (data.input_type === 'slide') {
                    // Setup for slide-based CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt for the slide puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Drag the slider component to the correct position.";
                    }
                    
                    // Set up sliding puzzle interface
                    setupSlidePuzzle();
                } else if (data.input_type === 'multiselect') {
                    // Setup for unusual detection CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt for the unusual detection puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Select the unusual items in the image.";
                    }
                    
                    // Set up unusual detection grid
                    setupUnusualDetectionGrid();
                } else if (data.input_type === 'image_grid') {
                    // Setup for image recognition CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt for the image recognition puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else if (data.question) {
                        puzzlePrompt.textContent = data.question;
                    } else {
                        puzzlePrompt.textContent = "Select all images that match the description.";
                    }
                    
                    // Set up image recognition grid
                    setupImageRecognition();
                } else if (data.input_type === 'bingo_swap') {
                    // Setup for Bingo swap CAPTCHA
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt for the Bingo puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Please click two images to exchange their position to line up the same images to a line, you can only exchange the images once.";
                    }
                    
                    // Set up Bingo grid
                    setupBingoSwap();
                } else if (data.input_type === 'image_matching') {
                    // Setup for Image Matching CAPTCHA
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt for the Image Matching puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Using the arrows, match the animal in the left and right image.";
                    }
                    
                    // Set up Image Matching interface
                    setupImageMatching();
                } else if (data.input_type === 'patch_select') {
                    // Hide standard input display but keep it for value storage
                    userAnswerInput.style.display = 'none';
                    
                    // Customize submit button
                    submitBtn.textContent = 'Verify';
                    submitBtn.style.display = 'block';
                    
                    // Setup patch selection grid
                    setupPatchSelectGrid();
                } else if (data.input_type === 'dart_count') {
                    // Hide standard input display but keep it for value storage
                    userAnswerInput.style.display = 'none';
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt for the dart count puzzle
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Use the arrows to find the darts that add up to the target number.";
                    }
                    
                    // Debug log
                    console.log('Setting up Dart Count puzzle with data:', data);
                    
                    // Setup dart count interface
                    setupDartCount();
                } else if (data.input_type === 'select_animal') {
                    // Hide standard input display but keep it for value storage
                    userAnswerInput.style.display = 'none';
                    
                    // Customize submit button
                    submitBtn.textContent = 'Submit';
                    submitBtn.style.display = 'block';
                    
                    // Setup animal selection grid
                    setupSelectAnimalGrid();
                } else if (data.input_type === 'object_match') {
                    // Setup for object match puzzles
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Use the arrows to change the number of objects until it matches the left image.";
                    }
                    
                    // Set up object match interface
                    setupObjectMatch();
                } else if (data.input_type === 'place_dot') {
                    // Setup for Place_Dot CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Click to place a Dot at the end of the car's path";
                    }
                    
                    // Set up place dot interface
                    setupPlaceDot();
                } else if (data.input_type === 'connect_icon') {
                    // Setup for Connect_icon CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Using the arrows, connect the same two icons with the dotted line as shown on the left.";
                    }
                    
                    // Set up connect icon interface
                    setupConnectIcon();
                } else if (data.input_type === 'click_order') {
                    // Setup for Click_Order CAPTCHAs
                    inputGroup.style.display = 'none';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Click the icons in order as shown in the reference image.";
                    }
                    
                    // Set up click order interface
                    setupClickOrder();
                } else if (data.input_type === 'hold_button') {
                    // Setup for Hold_Button CAPTCHAs
                    inputGroup.style.display = 'flex';
                    puzzleImage.style.display = 'none';
                    puzzleImageContainer.style.display = 'block';
                    
                    // Update prompt
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else {
                        puzzlePrompt.textContent = "Hold the button until it finishes loading.";
                    }
                    
                    // Set up hold button interface
                    setupHoldButton();
                    
                    // Ensure input field and submit button are visible
                    userAnswerInput.style.display = 'block';
                    submitBtn.style.display = 'inline-block';
                } else {
                    // Default for text-based CAPTCHAs
                    puzzleImage.src = data.image_path;
                    inputGroup.style.display = 'flex';
                    puzzleImage.style.cursor = 'default';
                    puzzleImage.classList.remove('clickable');
                    
                    // Add puzzle image back to container
                    if (puzzleImageContainer.innerHTML === '') {
                        puzzleImageContainer.appendChild(puzzleImage);
                    }
                    
                    puzzleImageContainer.style.display = 'block';
                    puzzleImage.style.display = 'block';
                    
                    // Update prompt after clearing
                    if (data.prompt) {
                        puzzlePrompt.textContent = data.prompt;
                    } else if (data.puzzle_type === 'Dice_Count') {
                        puzzlePrompt.textContent = "Sum up the numbers on all the dice";
                    }
                    
                    // Reset submit button
                    submitBtn.disabled = false;
                    submitBtn.textContent = 'Submit';
                    
                    // Clear and focus input
                    userAnswerInput.value = '';
                    userAnswerInput.focus();
                    
                    // Set input type based on puzzle type
                    if (data.input_type === 'number') {
                        userAnswerInput.setAttribute('type', 'number');
                        userAnswerInput.setAttribute('placeholder', 'Enter the sum');
                    } else {
                        userAnswerInput.setAttribute('type', 'text');
                        userAnswerInput.setAttribute('placeholder', 'Your answer');
                    }
                    
                    // Ensure the input is visible
                    userAnswerInput.style.display = 'block';
                }
            })
            .catch(error => {
                console.error('Error loading puzzle:', error);
                // Try again after a delay if there was an error
                setTimeout(loadNewPuzzle, 3000);
            });
    }

    // Function to create fireworks effect for correct answers
    function createFireworks() {
        // Create container for fireworks
        const fireworksContainer = document.createElement('div');
        fireworksContainer.className = 'fireworks-container';
        document.body.appendChild(fireworksContainer);
        
        // Create happy face animation
        const happyFaceContainer = document.createElement('div');
        happyFaceContainer.className = 'happy-face-container';
        happyFaceContainer.textContent = '😄';
        happyFaceContainer.style.zIndex = '10000'; // Ensure it's above everything
        document.body.appendChild(happyFaceContainer);
        
        // Create multiple fireworks at random positions
        const colors = [
            '#FF0000', '#00FF00', '#0000FF', '#FFFF00', 
            '#FF00FF', '#00FFFF', '#FFA500', '#FF4500',
            '#FFD700', '#32CD32', '#8A2BE2', '#FF69B4'
        ];
        
        // Create more fireworks (150 instead of 100)
        for (let i = 0; i < 150; i++) {
            const firework = document.createElement('div');
            firework.className = 'firework';
            
            // Random position - spread across the screen, with more concentration near center
            const centerBias = Math.random() > 0.7; // 30% chance to be centered
            const x = centerBias 
                ? window.innerWidth/2 + (Math.random() - 0.5) * window.innerWidth/2
                : Math.random() * window.innerWidth;
            const y = centerBias
                ? window.innerHeight/2 + (Math.random() - 0.5) * window.innerHeight/2
                : Math.random() * window.innerHeight;
            
            // Random color
            const color = colors[Math.floor(Math.random() * colors.length)];
            
            // Random size (larger particles)
            const size = 5 + Math.random() * 8;
            
            // Random delay and duration
            const delay = Math.random() * 1.5;
            const duration = 0.8 + Math.random() * 1.2;
            
            // Apply styles
            firework.style.left = `${x}px`;
            firework.style.top = `${y}px`;
            firework.style.backgroundColor = color;
            firework.style.width = `${size}px`;
            firework.style.height = `${size}px`;
            firework.style.animationDelay = `${delay}s`;
            firework.style.animationDuration = `${duration}s`;
            
            // Add to container
            fireworksContainer.appendChild(firework);
        }
        
        // Remove containers after animation completes
        setTimeout(() => {
            fireworksContainer.remove();
            happyFaceContainer.remove();
        }, 3500);
    }
    
    // Function to create sad face effect for incorrect answers
    function createSadFace() {
        // Create container for sad face
        const sadFaceContainer = document.createElement('div');
        sadFaceContainer.className = 'sad-face-container';
        sadFaceContainer.textContent = '😢';
        document.body.appendChild(sadFaceContainer);
        
        // Remove container after animation completes
        setTimeout(() => {
            sadFaceContainer.remove();
        }, 2000);
    }

    function submitAnswer() {
        // Don't submit if there's no input for number/text input types
        if ((currentPuzzle.input_type === 'number' || currentPuzzle.input_type === 'text') && 
            !userAnswerInput.value.trim()) {
            // Don't submit empty answers for number/text inputs
            return;
        }
        
        // Disable submit button to prevent double submissions
        submitBtn.disabled = true;
        submitBtn.textContent = 'Processing...';
        
        let answerData = {
            puzzle_type: currentPuzzle.puzzle_type,
            puzzle_id: currentPuzzle.puzzle_id
        };
        
        // Handle different input types
        if (currentPuzzle.input_type === 'click' && clickCoordinates) {
            // For click input, send the click coordinates
            answerData.answer = clickCoordinates;
        } else if (currentPuzzle.input_type === 'rotation') {
            // For rotation input, send the current rotation angle
            answerData.answer = currentRotationAngle;
        } else if (currentPuzzle.input_type === 'slide') {
            // For slide puzzle, calculate the current position of the slider
            const sliderComponent = document.querySelector('.slider-component');
            if (sliderComponent) {
                // Get the current position (from CSS left/top values)
                const currentX = parseInt(sliderComponent.style.left) || 0;
                const currentY = parseInt(sliderComponent.style.top) || 0;
                
                // Add slider position to answer data
                answerData.answer = [currentX, currentY];
            } else {
                console.error('Slider component not found');
                // Re-enable submit button
                submitBtn.disabled = false;
                submitBtn.textContent = 'Check Position';
                return;
            }
        } else if (currentPuzzle.input_type === 'multiselect') {
            // For multiselect input, send the selected cell indices
            answerData.answer = selectedCells;
        } else if (currentPuzzle.input_type === 'image_grid') {
            // For image grid selection, send the selected image indices
            answerData.answer = selectedCells;
        } else if (currentPuzzle.input_type === 'bingo_swap') {
            // For bingo swap, send the selected cells to swap
            answerData.answer = bingoSelectedCells;
        } else if (currentPuzzle.input_type === 'image_matching') {
            // For image matching, send the current option index
            const currentOptionIndex = currentPuzzle.current_option_index || 0;
            answerData.answer = currentOptionIndex;
        } else if (currentPuzzle.input_type === 'dart_count') {
            // For dart count, send the selected option index
            const selectedIndex = parseInt(userAnswerInput.value);
            answerData.answer = selectedIndex;
        } else if (currentPuzzle.input_type === 'patch_select') {
            // For patch select, send the selected patch indices
            try {
                // Try to parse the JSON value from the input
                const parsedSelection = JSON.parse(userAnswerInput.value);
                
                // If parsed array is empty but global selectedCells is not, use global
                if (parsedSelection.length === 0 && selectedCells.length > 0) {
                    answerData.answer = selectedCells;
                } else {
                    answerData.answer = parsedSelection;
                }
            } catch (error) {
                console.error('Error parsing selected patches:', error);
                // Fallback to the global array if parsing fails
                answerData.answer = selectedCells;
            }
        } else if (currentPuzzle.input_type === 'select_animal') {
            // For select animal, send the selected animal index
            try {
                // If the value is empty, use the global selectedAnimalIndex
                if (userAnswerInput.value === '[]' || userAnswerInput.value.trim() === '') {
                    answerData.answer = selectedAnimalIndex >= 0 ? [selectedAnimalIndex] : [];
                } else {
                    // Otherwise parse the JSON from the input
                    const selectedAnimal = JSON.parse(userAnswerInput.value);
                    answerData.answer = selectedAnimal;
                }
            } catch (error) {
                console.error('Error parsing selected animal:', error);
                // Use the global variable as a fallback
                answerData.answer = selectedAnimalIndex >= 0 ? [selectedAnimalIndex] : [];
            }
        } else if (currentPuzzle.input_type === 'object_match') {
            // For object match, send the selected option index
            const selectedIndex = parseInt(puzzleImageContainer.dataset.currentOptionIndex);
            answerData.answer = selectedIndex;
        } else if (currentPuzzle.input_type === 'place_dot') {
            // For place_dot input, send the click coordinates
            if (!clickCoordinates) {
                console.error('No dot coordinates found');
                // Re-enable submit button
                submitBtn.disabled = false;
                submitBtn.textContent = 'Submit';
                return;
            }
            answerData.answer = clickCoordinates;
        } else if (currentPuzzle.input_type === 'connect_icon') {
            // For connect_icon, send the current option index
            answerData.answer = parseInt(userAnswerInput.value) || 0;
        } else if (currentPuzzle.input_type === 'hold_button') {
            // For hold button, get the elapsed time from the input field
            answerData.answer = parseFloat(userAnswerInput.value) || 0;
            answerData.elapsed_time = ((Date.now() - puzzleStartTime) / 1000).toFixed(2);
        } else {
            // For text/number inputs, use the input value
            answerData.answer = userAnswerInput.value.trim();
        }
        
        // Send answer to server for verification
        fetch('/api/check_answer', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(answerData)
        })
        .then(response => response.json())
        .then(data => {
            // Update stats
            benchmarkStats.total++;
            if (data.correct) {
                benchmarkStats.correct++;
                resultMessage.textContent = 'Correct!';
                resultMessage.className = 'result-message correct';
                
                // Create fireworks effect for correct answer
                createFireworks();
            } else {
                // Just show "Incorrect" without revealing the correct answer
                resultMessage.textContent = 'Incorrect.';
                resultMessage.className = 'result-message incorrect';
                
                // Create sad face effect for incorrect answer
                createSadFace();
            }
            
            updateStats();
            
            // Record benchmark result
            recordBenchmarkResult({
                puzzle_type: currentPuzzle.puzzle_type,
                puzzle_id: currentPuzzle.puzzle_id,
                user_answer: answerData.answer,
                correct_answer: data.correct_answer,
                correct: data.correct
            });
            
            // Disable the submit button after submission
            if (currentPuzzle.input_type !== 'click') {
                submitBtn.disabled = true;
                
                // Also disable rotation submit button if it exists
                const rotateSubmitBtn = document.querySelector('.submit-rotation');
                if (rotateSubmitBtn) {
                    rotateSubmitBtn.disabled = true;
                }
                
                // Also disable image recognition submit button if it exists
                const imageRecognitionSubmitBtn = document.querySelector('.submit-image-recognition');
                if (imageRecognitionSubmitBtn) {
                    imageRecognitionSubmitBtn.disabled = true;
                }
                
                // Also disable bingo submit button if it exists
                const bingoSubmitBtn = document.querySelector('.submit-bingo');
                if (bingoSubmitBtn) {
                    bingoSubmitBtn.disabled = true;
                }
                
                // Also disable image matching submit button if it exists
                const imageMatchingSubmitBtn = document.querySelector('.submit-image-matching');
                if (imageMatchingSubmitBtn) {
                    imageMatchingSubmitBtn.disabled = true;
                }
            }
            
            // After handling the result and before loading a new puzzle
            setTimeout(() => {
                // Reset the submit button text before loading new puzzle
                submitBtn.textContent = 'Submit';
                
                // Make sure we reset input visibility before loading a new puzzle
                userAnswerInput.style.display = 'block';
                
                loadNewPuzzle();
            }, 2000);
        })
        .catch(error => {
            console.error('Error checking answer:', error);
            resultMessage.textContent = 'Error checking answer. Please try again.';
            resultMessage.className = 'result-message incorrect';
            // Re-enable the submit button on error
            submitBtn.disabled = false;
            submitBtn.textContent = 'Submit';
        });
    }

    function updateStats() {
        totalCount.textContent = benchmarkStats.total;
        correctCount.textContent = benchmarkStats.correct;
        
        const accuracy = benchmarkStats.total > 0 
            ? ((benchmarkStats.correct / benchmarkStats.total) * 100).toFixed(1) 
            : '0.0';
        
        accuracyEl.textContent = `${accuracy}%`;
    }

    function recordBenchmarkResult(result) {
        // Ensure we have the timestamp field
        if (!result.timestamp) {
            result.timestamp = new Date().toISOString();
        }
        
        // Send the benchmark result to be recorded
        fetch('/api/benchmark_results', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(result)
        })
        .then(response => response.json())
        .then(data => {
            console.log('Benchmark result recorded:', data);
        })
        .catch(error => {
            console.error('Error recording benchmark result:', error);
        });
    }
    
    // Auto-start benchmark when page loads
    loadNewPuzzle();

    // Function to update position display for the slider
    function updateSliderPositionDisplay(x, y, componentWidth, componentHeight) {
        // Remove any existing position display
        const existingDisplay = document.querySelector('.slider-position-display');
        if (existingDisplay) {
            existingDisplay.remove();
        }
        
        if (!DEBUG_MODE) return;
        
        // Calculate center point
        const centerX = x + (componentWidth / 2);
        const centerY = y + (componentHeight / 2);
        
        // Create the position display element
        const posDisplay = document.createElement('div');
        posDisplay.className = 'slider-position-display';
        posDisplay.textContent = `Position: (${Math.round(centerX)}, ${Math.round(centerY)})`;
        posDisplay.style.position = 'fixed';
        posDisplay.style.top = '10px';
        posDisplay.style.right = '10px';
        posDisplay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        posDisplay.style.color = 'white';
        posDisplay.style.padding = '5px 10px';
        posDisplay.style.borderRadius = '4px';
        posDisplay.style.fontSize = '12px';
        posDisplay.style.zIndex = '1000';
        
        // Add to document body
        document.body.appendChild(posDisplay);
    }

    // Function to set up image recognition grid
    function setupImageRecognition() {
        // Remove any existing grid
        const existingGrid = document.querySelector('.image-recognition-grid');
        if (existingGrid) {
            existingGrid.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Get the grid dimensions
        const gridSize = currentPuzzle.grid_size || [3, 3]; // Default to 3x3 grid
        const [rows, cols] = gridSize;
        
        // Create the grid container
        const gridContainer = document.createElement('div');
        gridContainer.className = 'image-recognition-grid';
        gridContainer.style.display = 'grid';
        gridContainer.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
        gridContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
        gridContainer.style.gap = '5px';
        gridContainer.style.width = '100%';
        gridContainer.style.aspectRatio = `${cols} / ${rows}`;
        
        // Get the list of images
        const images = currentPuzzle.images || [];
        
        // Create individual cells for each image
        for (let i = 0; i < images.length; i++) {
            const cell = document.createElement('div');
            cell.className = 'grid-cell';
            cell.dataset.index = i;
            cell.style.position = 'relative';
            cell.style.border = '2px solid #333';
            cell.style.cursor = 'pointer';
            cell.style.overflow = 'hidden';
            
            // Create image element
            const img = document.createElement('img');
            img.src = images[i];
            img.style.width = '100%';
            img.style.height = '100%';
            img.style.objectFit = 'cover';
            img.style.display = 'block';
            cell.appendChild(img);
            
            // Create an overlay for selection state
            const overlay = document.createElement('div');
            overlay.className = 'cell-overlay';
            overlay.style.position = 'absolute';
            overlay.style.top = '0';
            overlay.style.left = '0';
            overlay.style.width = '100%';
            overlay.style.height = '100%';
            overlay.style.backgroundColor = 'rgba(0, 120, 255, 0.5)';
            overlay.style.opacity = '0';
            overlay.style.transition = 'opacity 0.2s ease';
            overlay.style.pointerEvents = 'none';
            cell.appendChild(overlay);
            
            // Add a checkmark icon to indicate selection
            const checkmark = document.createElement('div');
            checkmark.className = 'checkmark';
            checkmark.innerHTML = '✓';
            checkmark.style.position = 'absolute';
            checkmark.style.top = '50%';
            checkmark.style.left = '50%';
            checkmark.style.transform = 'translate(-50%, -50%)';
            checkmark.style.color = 'white';
            checkmark.style.fontSize = '32px';
            checkmark.style.fontWeight = 'bold';
            checkmark.style.opacity = '0';
            checkmark.style.transition = 'opacity 0.2s ease';
            checkmark.style.pointerEvents = 'none';
            cell.appendChild(checkmark);
            
            // Add click handler for selection
            cell.addEventListener('click', (e) => {
                toggleCellSelection(i, cell);
            });
            
            // Add the cell to the grid
            gridContainer.appendChild(cell);
        }
        
        // Add the grid to the puzzle image container
        puzzleImageContainer.appendChild(gridContainer);
        
        // Add a submit button below the grid
        const submitSection = document.createElement('div');
        submitSection.className = 'image-recognition-submit';
        submitSection.style.textAlign = 'center';
        submitSection.style.marginTop = '15px';
        
        const submitBtn = document.createElement('button');
        submitBtn.textContent = 'Submit';
        submitBtn.className = 'submit-image-recognition';
        submitBtn.addEventListener('click', submitAnswer);
        submitSection.appendChild(submitBtn);
        
        // Add to puzzle container
        const imageWrapper = document.querySelector('.puzzle-image-wrapper');
        imageWrapper.appendChild(submitSection);
        
        // Reset selected cells
        selectedCells = [];
    }

    // Function to set up Image Matching puzzle
    function setupImageMatching() {
        // Remove any existing controls first
        const existingControls = document.querySelector('.image-matching-controls');
        if (existingControls) {
            existingControls.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Create a container for the reference image
        const referenceContainer = document.createElement('div');
        referenceContainer.className = 'reference-image-container';
        const referenceImg = document.createElement('img');
        referenceImg.id = 'reference-image';
        referenceImg.src = currentPuzzle.reference_image;
        referenceImg.alt = 'Reference image';
        referenceContainer.appendChild(referenceImg);
        
        // Create a container for the option image
        const optionContainer = document.createElement('div');
        optionContainer.className = 'option-image-container';
        const optionImg = document.createElement('img');
        optionImg.id = 'option-image';
        optionImg.src = currentPuzzle.option_images[0]; // Start with the first option
        optionImg.alt = 'Option image';
        optionContainer.appendChild(optionImg);
        
        // Create a two-column layout for image matching puzzle
        const matchingLayout = document.createElement('div');
        matchingLayout.className = 'matching-layout';
        matchingLayout.appendChild(referenceContainer);
        matchingLayout.appendChild(optionContainer);
        
        // Replace the existing puzzle image
        puzzleImageContainer.innerHTML = '';
        puzzleImageContainer.appendChild(matchingLayout);
        
        // Create navigation controls
        const navControls = document.createElement('div');
        navControls.className = 'image-matching-controls';
        
        // Create left navigation button
        const leftBtn = document.createElement('button');
        leftBtn.className = 'navigate-left';
        leftBtn.innerHTML = '&#9664;'; // Left arrow
        leftBtn.setAttribute('aria-label', 'Previous image');
        
        // Create right navigation button
        const rightBtn = document.createElement('button');
        rightBtn.className = 'navigate-right';
        rightBtn.innerHTML = '&#9654;'; // Right arrow
        rightBtn.setAttribute('aria-label', 'Next image');
        
        // Create indicator dots
        const indicatorContainer = document.createElement('div');
        indicatorContainer.className = 'indicator-dots';
        
        for (let i = 0; i < currentPuzzle.option_images.length; i++) {
            const dot = document.createElement('span');
            dot.className = i === 0 ? 'dot active' : 'dot';
            indicatorContainer.appendChild(dot);
        }
        
        // Add buttons and indicators to controls
        navControls.appendChild(leftBtn);
        navControls.appendChild(indicatorContainer);
        navControls.appendChild(rightBtn);
        
        // Add to puzzle container
        const imageWrapper = document.querySelector('.puzzle-image-wrapper');
        imageWrapper.appendChild(navControls);
        
        // Add event listeners for navigation buttons
        let currentIndex = 0;
        
        leftBtn.addEventListener('click', () => {
            currentIndex = (currentIndex - 1 + currentPuzzle.option_images.length) % currentPuzzle.option_images.length;
            updateOptionImage();
        });
        
        rightBtn.addEventListener('click', () => {
            currentIndex = (currentIndex + 1) % currentPuzzle.option_images.length;
            updateOptionImage();
        });
        
        function updateOptionImage() {
            // Update the option image
            optionImg.src = currentPuzzle.option_images[currentIndex];
            
            // Update the indicator dots
            const dots = indicatorContainer.querySelectorAll('.dot');
            dots.forEach((dot, i) => {
                dot.className = i === currentIndex ? 'dot active' : 'dot';
            });
            
            // Update the current index in the puzzle data
            currentPuzzle.current_option_index = currentIndex;
        }
        
        // Auto-show submit button for image matching puzzles
        const submitSection = document.createElement('div');
        submitSection.className = 'image-matching-submit';
        const matchingSubmitBtn = document.createElement('button');
        matchingSubmitBtn.textContent = 'Submit';
        matchingSubmitBtn.className = 'submit-image-matching';
        matchingSubmitBtn.addEventListener('click', submitAnswer);
        submitSection.appendChild(matchingSubmitBtn);
        
        // Add to puzzle container
        imageWrapper.appendChild(submitSection);
    }

    // Function to set up patch selection grid
    function setupPatchSelectGrid() {
        // Remove any existing grid first
        const existingGrid = document.querySelector('.patch-select-grid');
        if (existingGrid) {
            existingGrid.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // IMPORTANT: Reset the global selectedCells array to fix the bug
        // when encountering these puzzles multiple times
        selectedCells = [];
        
        // Create a container for the patch select grid
        const gridContainer = document.createElement('div');
        gridContainer.className = 'patch-select-grid';
        
        // Get grid dimensions from the puzzle data
        const gridSize = currentPuzzle.grid_size || [6, 6];
        const rows = gridSize[0];
        const cols = gridSize[1];
        
        // Set grid styles
        gridContainer.style.display = 'grid';
        gridContainer.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
        gridContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
        gridContainer.style.gap = '3px';
        gridContainer.style.width = '100%';
        gridContainer.style.aspectRatio = `${cols}/${rows}`;
        gridContainer.style.position = 'relative';
        
        // Create image container
        const imageContainer = document.createElement('div');
        imageContainer.className = 'patch-select-image-container';
        imageContainer.style.position = 'absolute';
        imageContainer.style.top = '0';
        imageContainer.style.left = '0';
        imageContainer.style.width = '100%';
        imageContainer.style.height = '100%';
        imageContainer.style.zIndex = '0';
        
        // Add the puzzle image
        const img = document.createElement('img');
        img.src = currentPuzzle.image_path;
        img.alt = 'CAPTCHA image';
        img.style.width = '100%';
        img.style.height = '100%';
        img.style.objectFit = 'cover';
        imageContainer.appendChild(img);
        
        // Add image container to grid container
        gridContainer.appendChild(imageContainer);
        
        // Create grid cells for selection
        // Use the global selectedCells array directly
        
        for (let i = 0; i < rows * cols; i++) {
            const cell = document.createElement('div');
            cell.className = 'patch-select-cell';
            cell.dataset.index = i;
            cell.style.position = 'relative';
            cell.style.zIndex = '1';
            cell.style.cursor = 'pointer';
            
            // Add a checkmark icon to indicate selection
            const checkmark = document.createElement('div');
            checkmark.className = 'checkmark';
            checkmark.innerHTML = '✓';
            checkmark.style.position = 'absolute';
            checkmark.style.top = '50%';
            checkmark.style.left = '50%';
            checkmark.style.transform = 'translate(-50%, -50%)';
            checkmark.style.color = 'white';
            checkmark.style.fontSize = '32px';
            checkmark.style.fontWeight = 'bold';
            checkmark.style.opacity = '0';
            checkmark.style.transition = 'opacity 0.2s ease';
            checkmark.style.pointerEvents = 'none';
            checkmark.style.textShadow = '1px 1px 3px rgba(0, 0, 0, 0.7)';
            checkmark.style.zIndex = '3';
            cell.appendChild(checkmark);
            
            // Add click event to toggle selection
            cell.addEventListener('click', () => {
                // Toggle selection
                if (cell.classList.contains('selected')) {
                    cell.classList.remove('selected');
                    // Hide checkmark
                    checkmark.style.opacity = '0';
                    // Remove from selected array
                    const index = selectedCells.indexOf(i);
                    if (index > -1) {
                        selectedCells.splice(index, 1);
                    }
                } else {
                    cell.classList.add('selected');
                    // Show checkmark
                    checkmark.style.opacity = '1';
                    // Add to selected array
                    selectedCells.push(i);
                }
                
                // Update the answer in the UI
                userAnswerInput.value = JSON.stringify(selectedCells);
                
                // Enable the submit button when squares are selected
                submitBtn.disabled = false;
                
                // Log selected patches for debugging
                console.log('Selected patches:', selectedCells);
            });
            
            gridContainer.appendChild(cell);
        }
        
        // Add the grid to the puzzle container
        puzzleImageContainer.appendChild(gridContainer);
        
        // Update the prompt to include the target object
        puzzlePrompt.textContent = `Select all squares with ${currentPuzzle.target_object}`;
        
        // Hide the regular input and replace with verify button
        userAnswerInput.style.display = 'none';
        submitBtn.textContent = 'Verify';
        submitBtn.style.display = 'inline-block';  // Changed to inline-block
        inputGroup.style.display = 'flex';
        submitBtn.disabled = false; // Ensure the button is enabled
        
        // Clear any previous answer
        userAnswerInput.value = '[]';
    }
    
    // Function to set up Select_Animal grid
    function setupSelectAnimalGrid() {
        // Remove any existing grid first
        const existingGrid = document.querySelector('.animal-select-grid');
        if (existingGrid) {
            existingGrid.remove();
        }
        
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // IMPORTANT: Reset the selectedAnimalIndex to -1 to fix the bug when encountering this puzzle multiple times
        selectedAnimalIndex = -1;
        
        // Create a simple container directly
        const container = document.createElement('div');
        container.style.width = '100%';
        container.style.maxWidth = '800px';
        container.style.margin = '0 auto';
        container.style.position = 'relative';
        
        // Display the image directly
        const img = document.createElement('img');
        img.src = currentPuzzle.image_path;
        img.alt = 'CAPTCHA image with animals';
        img.style.width = '100%';
        img.style.display = 'block';
        img.style.border = '2px solid #ccc';
        img.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.15)';
        container.appendChild(img);
        
        // Get grid dimensions from the puzzle data
        const gridSize = currentPuzzle.grid_size || [2, 3];
        const rows = gridSize[0];
        const cols = gridSize[1];
        
        // Wait for image to load to ensure dimensions are available
        img.onload = function() {
            // Create overlay grid that matches the image dimensions
            const grid = document.createElement('div');
            grid.style.position = 'absolute';
            grid.style.top = '0';
            grid.style.left = '0';
            grid.style.width = '100%';
            grid.style.height = '100%';
            grid.style.display = 'grid';
            grid.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
            grid.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
            
            // IMPORTANT: Create a fresh selectedAnimal object with -1 index to fix the bug
            // when encountering these puzzles multiple times
            const selectedAnimal = { index: -1 };
            
            for (let i = 0; i < rows * cols; i++) {
                const cell = document.createElement('div');
                cell.style.border = '1px solid rgba(255, 255, 255, 0.3)';
                cell.style.cursor = 'pointer';
                cell.style.position = 'relative';
                cell.style.transition = 'all 0.2s ease';
                
                // Add hover effect
                cell.addEventListener('mouseover', () => {
                    cell.style.backgroundColor = 'rgba(76, 175, 80, 0.2)';
                    cell.style.border = '1px solid rgba(76, 175, 80, 0.7)';
                });
                
                cell.addEventListener('mouseout', () => {
                    if (selectedAnimal.index !== i) {
                        cell.style.backgroundColor = 'transparent';
                        cell.style.border = '1px solid rgba(255, 255, 255, 0.3)';
                    }
                });
                
                // Add click event to toggle selection
                cell.addEventListener('click', () => {
                    // Clear previous selection
                    grid.querySelectorAll('div').forEach((c, index) => {
                        if (index !== i) {
                            c.style.backgroundColor = 'transparent';
                            c.style.border = '1px solid rgba(255, 255, 255, 0.3)';
                        }
                    });
                    
                    // Update selection
                    selectedAnimal.index = i;
                    selectedAnimalIndex = i; // Update the global variable
                    cell.style.backgroundColor = 'rgba(76, 175, 80, 0.3)';
                    cell.style.border = '2px solid rgba(76, 175, 80, 0.9)';
                    
                    // Update the answer in the UI
                    userAnswerInput.value = JSON.stringify([i]);
                    
                    // Enable the submit button
                    submitBtn.disabled = false;
                    
                    // Log selected animal for debugging
                    console.log('Selected animal at index:', i);
                });
                
                grid.appendChild(cell);
            }
            
            // Add the grid to the container
            container.appendChild(grid);
        };
        
        // Add the container to the puzzle container
        puzzleImageContainer.appendChild(container);
        
        // Make sure the prompt is clearly visible
        puzzlePrompt.style.fontSize = '20px';
        puzzlePrompt.style.fontWeight = 'bold';
        puzzlePrompt.style.marginBottom = '20px';
        
        // Update the prompt to include the target animal
        puzzlePrompt.textContent = `Pick a ${currentPuzzle.target_object}`;
        
        // Hide the regular input and replace with verify button
        userAnswerInput.style.display = 'none';
        submitBtn.textContent = 'Submit';
        submitBtn.style.display = 'inline-block';
        inputGroup.style.display = 'flex';
        submitBtn.disabled = true; // Disabled until selection is made
        
        // Clear any previous answer
        userAnswerInput.value = '[]';
    }

    /**
     * Setup the Object Match interface with reference image and option controls
     */
    function setupObjectMatch() {
        // Create container for the object match interface
        const matchContainer = document.createElement('div');
        matchContainer.className = 'object-match-container';
        
        // Create a horizontal layout
        const horizontalLayout = document.createElement('div');
        horizontalLayout.className = 'object-match-horizontal-layout';
        
        // Create reference image container
        const referenceContainer = document.createElement('div');
        referenceContainer.className = 'object-match-reference';
        
        // Add reference image
        const referenceImage = document.createElement('img');
        referenceImage.src = currentPuzzle.reference_image || currentPuzzle.additional_data.reference_image;
        referenceImage.alt = 'Reference Image';
        referenceImage.className = 'object-match-reference-img';
        referenceContainer.appendChild(referenceImage);
        
        // Add reference caption
        const referenceCaption = document.createElement('div');
        referenceCaption.className = 'object-match-caption';
        referenceCaption.textContent = 'Match This!';
        referenceContainer.appendChild(referenceCaption);
        
        // Create options container
        const optionsContainer = document.createElement('div');
        optionsContainer.className = 'object-match-options';
        
        // Add option image
        const optionImage = document.createElement('img');
        const optionImages = currentPuzzle.option_images || currentPuzzle.additional_data.option_images;
        optionImage.src = optionImages[0]; // Start with first option
        optionImage.alt = 'Option Image';
        optionImage.className = 'object-match-option-img';
        optionsContainer.appendChild(optionImage);
        
        // Create navigation controls
        const controlsContainer = document.createElement('div');
        controlsContainer.className = 'object-match-controls';
        
        // Left arrow
        const leftArrow = document.createElement('button');
        leftArrow.innerHTML = '&larr;';
        leftArrow.className = 'object-match-arrow left-arrow';
        leftArrow.addEventListener('click', () => updateObjectOption(-1));
        
        // Right arrow
        const rightArrow = document.createElement('button');
        rightArrow.innerHTML = '&rarr;';
        rightArrow.className = 'object-match-arrow right-arrow';
        rightArrow.addEventListener('click', () => updateObjectOption(1));
        
        // Add arrows to controls
        controlsContainer.appendChild(leftArrow);
        controlsContainer.appendChild(rightArrow);
        
        // Add controls to options container
        optionsContainer.appendChild(controlsContainer);
        
        // Add reference and options to horizontal layout
        horizontalLayout.appendChild(referenceContainer);
        horizontalLayout.appendChild(optionsContainer);
        
        // Add horizontal layout to main container
        matchContainer.appendChild(horizontalLayout);
        
        // Add option indicators (dots)
        const indicators = document.createElement('div');
        indicators.className = 'object-match-indicators';
        
        const numOptions = optionImages.length;
        for (let i = 0; i < numOptions; i++) {
            const dot = document.createElement('span');
            dot.className = 'object-match-dot';
            if (i === 0) {
                dot.classList.add('active');
            }
            indicators.appendChild(dot);
        }
        
        // Add indicators to main container
        matchContainer.appendChild(indicators);
        
        // Add submit button
        const submitBtn = document.createElement('button');
        submitBtn.textContent = 'Submit';
        submitBtn.className = 'object-match-submit';
        submitBtn.addEventListener('click', submitAnswer);
        
        // Add containers to puzzle image container
        puzzleImageContainer.appendChild(matchContainer);
        puzzleImageContainer.appendChild(submitBtn);
        
        // Store current index in data attribute
        puzzleImageContainer.dataset.currentOptionIndex = '0';
        
        // Log for debugging
        console.log('Object Match images:', {
            reference: referenceImage.src,
            options: optionImages
        });
    }
    
    /**
     * Update the displayed option image based on navigation direction
     * @param {number} direction - Direction to navigate (-1 for left, 1 for right)
     */
    function updateObjectOption(direction) {
        const container = document.querySelector('.object-match-container');
        const optionImage = document.querySelector('.object-match-option-img');
        const dots = document.querySelectorAll('.object-match-dot');
        
        // Get current index
        let currentIndex = parseInt(puzzleImageContainer.dataset.currentOptionIndex);
        const optionImages = currentPuzzle.option_images || currentPuzzle.additional_data.option_images;
        const numOptions = optionImages.length;
        
        // Calculate new index with wrap-around
        let newIndex = (currentIndex + direction + numOptions) % numOptions;
        
        // Update the option image
        optionImage.src = optionImages[newIndex];
        
        // Update dots
        dots.forEach((dot, index) => {
            if (index === newIndex) {
                dot.classList.add('active');
            } else {
                dot.classList.remove('active');
            }
        });
        
        // Store new index
        puzzleImageContainer.dataset.currentOptionIndex = newIndex.toString();
        
        // Store selected answer for submission
        userAnswerInput.value = newIndex.toString();
        
        // Log for debugging
        console.log('Updated option image:', {
            index: newIndex,
            src: optionImage.src
        });
    }

    /**
     * Setup the Place_Dot interface allowing the user to click on the image to place a dot
     */
    function setupPlaceDot() {
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Create a container for the image with relative positioning
        const container = document.createElement('div');
        container.style.position = 'relative';
        container.style.width = '100%';
        container.style.maxWidth = '800px';
        container.style.margin = '0 auto';
        
        // Create and add the image
        const img = document.createElement('img');
        img.src = `/captcha_data/${currentPuzzle.puzzle_type}/${currentPuzzle.puzzle_id}`;
        img.alt = 'Car path image';
        img.style.width = '100%';
        img.style.display = 'block';
        img.style.cursor = 'crosshair';
        container.appendChild(img);
        
        // Reset any previous click coordinates
        clickCoordinates = null;
        
        // Add click handler to the image
        img.addEventListener('click', (e) => {
            // Remove any existing dot
            const existingDot = container.querySelector('.place-dot-marker');
            if (existingDot) {
                existingDot.remove();
            }
            
            // Get click coordinates relative to the image
            const rect = e.target.getBoundingClientRect();
            const x = Math.round(e.clientX - rect.left);
            const y = Math.round(e.clientY - rect.top);
            
            // Store coordinates for submission
            clickCoordinates = [x, y];
            
            // Create dot marker
            const dot = document.createElement('div');
            dot.className = 'place-dot-marker';
            dot.style.position = 'absolute';
            dot.style.width = '20px';
            dot.style.height = '20px';
            dot.style.borderRadius = '50%';
            dot.style.backgroundColor = 'rgba(255, 0, 0, 0.7)';
            dot.style.border = '2px solid #ff0000';
            dot.style.left = `${x}px`;
            dot.style.top = `${y}px`;
            dot.style.transform = 'translate(-50%, -50%)';
            dot.style.pointerEvents = 'none';
            dot.style.zIndex = '10';
            
            // Add animation
            dot.style.animation = 'pulse 1s infinite alternate';
            
            // Add dot to container
            container.appendChild(dot);
            
            // Enable submit button
            submitBtn.disabled = false;
            
            // Log coordinates for debugging
            console.log('Dot placed at:', { x, y });
        });
        
        // Add the container to the puzzle container
        puzzleImageContainer.appendChild(container);
        
        // In debug mode, fetch the ground truth to show the target area
        if (DEBUG_MODE) {
            fetch('/api/get_ground_truth', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    puzzle_type: currentPuzzle.puzzle_type,
                    puzzle_id: currentPuzzle.puzzle_id
                })
            })
            .then(response => response.json())
            .then(gtData => {
                // Check if we have a target position in the answer
                if (gtData.answer && gtData.answer.target_position) {
                    const targetPosition = gtData.answer.target_position;
                    const tolerance = gtData.answer.tolerance || 15; // Default to 15px
                    showTargetDotArea(container, targetPosition, tolerance);
                }
            })
            .catch(error => {
                console.error('Error fetching ground truth for Place_Dot:', error);
            });
        }
        
        // Update prompt and input elements
        puzzlePrompt.textContent = currentPuzzle.prompt || "Click to place a Dot at the end of the car's path";
        
        // Hide the input field and adjust the submit button
        userAnswerInput.style.display = 'none';
        submitBtn.textContent = 'Submit';
        submitBtn.disabled = true; // Disabled until user places a dot
        submitBtn.style.display = 'inline-block';
        inputGroup.style.display = 'flex';
    }
    
    /**
     * Show the target area for the Place_Dot puzzle in debug mode
     * @param {HTMLElement} container - The container element
     * @param {Array} targetPosition - The target position [x, y]
     * @param {number} tolerance - The tolerance radius in pixels
     */
    function showTargetDotArea(container, targetPosition, tolerance = 15) {
        if (!DEBUG_MODE) return;
        
        // Remove any existing target visualization
        const existingTarget = container.querySelector('.target-dot-area');
        if (existingTarget) {
            existingTarget.remove();
        }
        
        // Get target coordinates
        const [targetX, targetY] = targetPosition;
        
        // Create a target element - visualized as a circle
        const targetArea = document.createElement('div');
        targetArea.className = 'target-dot-area';
        
        // Calculate diameter based on tolerance
        const diameter = tolerance * 2;
        
        // Style the target area
        targetArea.style.position = 'absolute';
        targetArea.style.left = `${targetX - tolerance}px`;
        targetArea.style.top = `${targetY - tolerance}px`;
        targetArea.style.width = `${diameter}px`;
        targetArea.style.height = `${diameter}px`;
        targetArea.style.borderRadius = '50%';
        targetArea.style.border = '2px dashed green';
        targetArea.style.backgroundColor = 'rgba(0, 255, 0, 0.2)';
        targetArea.style.zIndex = '5';
        targetArea.style.pointerEvents = 'none'; // Allow clicks to pass through
        
        // Add coordinates label
        const coordsLabel = document.createElement('div');
        coordsLabel.className = 'coords-label';
        coordsLabel.textContent = `Target: (${targetX}, ${targetY}) ±${tolerance}px`;
        coordsLabel.style.position = 'absolute';
        coordsLabel.style.top = '-25px';
        coordsLabel.style.left = '0';
        coordsLabel.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        coordsLabel.style.color = 'white';
        coordsLabel.style.padding = '2px 5px';
        coordsLabel.style.fontSize = '10px';
        coordsLabel.style.borderRadius = '3px';
        coordsLabel.style.whiteSpace = 'nowrap';
        targetArea.appendChild(coordsLabel);
        
        // Add to the container
        container.appendChild(targetArea);
        
        // Log the target details
        console.log('Place_Dot target position:', { 
            x: targetX, 
            y: targetY,
            tolerance: tolerance
        });
    }

    // Function to set up connect icon interface
    function setupConnectIcon() {
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Create a layout container for the two-column layout
        const layoutContainer = document.createElement('div');
        layoutContainer.className = 'connect-icon-layout';
        layoutContainer.style.display = 'flex';
        layoutContainer.style.justifyContent = 'space-between';
        
        // Create container for reference image
        const refContainer = document.createElement('div');
        refContainer.className = 'reference-image-container';
        refContainer.style.flex = '1';
        refContainer.style.marginRight = '10px';
        refContainer.style.textAlign = 'center';
        
        // Add "Match This!" label above reference image
        const matchLabel = document.createElement('div');
        matchLabel.className = 'match-label';
        matchLabel.textContent = 'Match This!';
        matchLabel.style.backgroundColor = 'black';
        matchLabel.style.color = 'white';
        matchLabel.style.padding = '2px 5px';
        matchLabel.style.marginBottom = '5px';
        matchLabel.style.fontSize = '12px';
        refContainer.appendChild(matchLabel);
        
        // Add reference image
        const refImg = document.createElement('img');
        refImg.id = 'connect-reference-image';
        refImg.src = currentPuzzle.reference_image;
        refImg.alt = 'Reference image';
        refImg.style.maxWidth = '100%';
        refImg.style.border = '1px solid #ccc';
        refContainer.appendChild(refImg);
        
        // Container for option images with arrows
        const optionContainer = document.createElement('div');
        optionContainer.className = 'connect-option-container';
        optionContainer.style.flex = '1';
        optionContainer.style.position = 'relative';
        
        // Create option image display
        const optionImgContainer = document.createElement('div');
        optionImgContainer.className = 'option-image-container';
        optionImgContainer.style.textAlign = 'center';
        
        // Create option image
        const optionImg = document.createElement('img');
        optionImg.id = 'connect-option-image';
        optionImg.src = currentPuzzle.option_images[0]; // Start with the first option
        optionImg.alt = 'Option image';
        optionImg.style.maxWidth = '100%';
        optionImg.style.border = '1px solid #ccc';
        optionImgContainer.appendChild(optionImg);
        optionContainer.appendChild(optionImgContainer);
        
        // Add arrow navigation
        const arrowsContainer = document.createElement('div');
        arrowsContainer.className = 'connect-arrows-container';
        arrowsContainer.style.display = 'flex';
        arrowsContainer.style.justifyContent = 'center';
        arrowsContainer.style.marginTop = '10px';
        
        // Left arrow
        const leftArrow = document.createElement('button');
        leftArrow.className = 'arrow-btn left-arrow';
        leftArrow.innerHTML = '&#8592;'; // Left arrow character
        leftArrow.setAttribute('aria-label', 'Previous option');
        leftArrow.style.margin = '0 10px';
        leftArrow.style.padding = '5px 15px';
        leftArrow.style.fontSize = '20px';
        leftArrow.style.backgroundColor = '#f0f0f0';
        leftArrow.style.border = '1px solid #ccc';
        leftArrow.style.borderRadius = '4px';
        leftArrow.style.cursor = 'pointer';
        
        // Right arrow
        const rightArrow = document.createElement('button');
        rightArrow.className = 'arrow-btn right-arrow';
        rightArrow.innerHTML = '&#8594;'; // Right arrow character
        rightArrow.setAttribute('aria-label', 'Next option');
        rightArrow.style.margin = '0 10px';
        rightArrow.style.padding = '5px 15px';
        rightArrow.style.fontSize = '20px';
        rightArrow.style.backgroundColor = '#f0f0f0';
        rightArrow.style.border = '1px solid #ccc';
        rightArrow.style.borderRadius = '4px';
        rightArrow.style.cursor = 'pointer';
        
        arrowsContainer.appendChild(leftArrow);
        arrowsContainer.appendChild(rightArrow);
        optionContainer.appendChild(arrowsContainer);
        
        // Add pagination dots
        const dotsContainer = document.createElement('div');
        dotsContainer.className = 'pagination-dots';
        dotsContainer.style.display = 'flex';
        dotsContainer.style.justifyContent = 'center';
        dotsContainer.style.marginTop = '10px';
        
        // Create dots based on the number of options
        for (let i = 0; i < currentPuzzle.option_images.length; i++) {
            const dot = document.createElement('span');
            dot.className = 'pagination-dot';
            dot.style.height = '10px';
            dot.style.width = '10px';
            dot.style.margin = '0 5px';
            dot.style.borderRadius = '50%';
            dot.style.backgroundColor = i === 0 ? '#4CAF50' : '#ccc'; // Highlight first dot
            dotsContainer.appendChild(dot);
        }
        
        optionContainer.appendChild(dotsContainer);
        
        // Add all containers to the layout
        layoutContainer.appendChild(refContainer);
        layoutContainer.appendChild(optionContainer);
        puzzleImageContainer.appendChild(layoutContainer);
        
        // Add a submit button
        const submitSection = document.createElement('div');
        submitSection.className = 'connect-icon-submit';
        submitSection.style.textAlign = 'center';
        submitSection.style.marginTop = '15px';
        
        const submitBtn = document.createElement('button');
        submitBtn.textContent = 'Submit';
        submitBtn.className = 'submit-connect';
        submitBtn.style.padding = '10px 20px';
        submitBtn.style.backgroundColor = '#4CAF50';
        submitBtn.style.color = 'white';
        submitBtn.style.border = 'none';
        submitBtn.style.borderRadius = '4px';
        submitBtn.style.fontSize = '16px';
        submitBtn.style.cursor = 'pointer';
        submitBtn.addEventListener('click', submitAnswer);
        submitSection.appendChild(submitBtn);
        
        // Add to puzzle container
        puzzleImageContainer.appendChild(submitSection);
        
        // Set up current option tracking
        let currentOptionIndex = 0;
        
        // Initialize the answer input with the current index
        userAnswerInput.value = currentOptionIndex.toString();
        
        // Function to update the option image
        function updateConnectOptionImage() {
            const optionImg = document.getElementById('connect-option-image');
            if (optionImg) {
                optionImg.src = currentPuzzle.option_images[currentOptionIndex];
            }
            
            // Update dots to highlight current option
            const dots = document.querySelectorAll('.pagination-dot');
            dots.forEach((dot, index) => {
                dot.style.backgroundColor = index === currentOptionIndex ? '#4CAF50' : '#ccc';
            });
            
            // Update the answer input with the current index
            userAnswerInput.value = currentOptionIndex.toString();
        }
        
        // Event listeners for arrows
        leftArrow.addEventListener('click', () => {
            currentOptionIndex = (currentOptionIndex - 1 + currentPuzzle.option_images.length) % currentPuzzle.option_images.length;
            updateConnectOptionImage();
        });
        
        rightArrow.addEventListener('click', () => {
            currentOptionIndex = (currentOptionIndex + 1) % currentPuzzle.option_images.length;
            updateConnectOptionImage();
        });
    }
    
    // Function to set up Click Order interface
    function setupClickOrder() {
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Create a container for the layout
        const layoutContainer = document.createElement('div');
        layoutContainer.className = 'click-order-layout';
        layoutContainer.style.display = 'flex';
        layoutContainer.style.flexDirection = 'column';
        layoutContainer.style.alignItems = 'center';
        
        // Create a container for the main image
        const mainImageContainer = document.createElement('div');
        mainImageContainer.className = 'main-image-container';
        mainImageContainer.style.position = 'relative';
        mainImageContainer.style.marginBottom = '20px';
        mainImageContainer.style.width = '100%';
        
        // Add main image
        const mainImg = document.createElement('img');
        mainImg.id = 'click-order-main-image';
        mainImg.src = currentPuzzle.image_path;
        mainImg.alt = 'Click the icons in order';
        mainImg.style.maxWidth = '100%';
        mainImg.style.border = '1px solid #ccc';
        mainImageContainer.appendChild(mainImg);
        
        // Create a container for the order reference image
        const orderImageContainer = document.createElement('div');
        orderImageContainer.className = 'order-image-container';
        orderImageContainer.style.textAlign = 'center';
        orderImageContainer.style.marginBottom = '20px';
        
        // Add "Order Reference" label
        const orderLabel = document.createElement('div');
        orderLabel.className = 'order-label';
        orderLabel.textContent = 'Click icons in this order:';
        orderLabel.style.backgroundColor = 'black';
        orderLabel.style.color = 'white';
        orderLabel.style.padding = '5px';
        orderLabel.style.marginBottom = '5px';
        orderLabel.style.fontSize = '14px';
        orderImageContainer.appendChild(orderLabel);
        
        // Add order reference image
        const orderImg = document.createElement('img');
        orderImg.id = 'click-order-reference-image';
        orderImg.src = currentPuzzle.order_image;
        orderImg.alt = 'Reference order';
        orderImg.style.maxWidth = '100%';
        orderImg.style.border = '1px solid #ccc';
        orderImageContainer.appendChild(orderImg);
        
        // Add click markers container to show user clicks
        const markersContainer = document.createElement('div');
        markersContainer.className = 'click-markers-container';
        markersContainer.style.position = 'absolute';
        markersContainer.style.top = '0';
        markersContainer.style.left = '0';
        markersContainer.style.width = '100%';
        markersContainer.style.height = '100%';
        markersContainer.style.pointerEvents = 'none'; // Don't block clicks
        mainImageContainer.appendChild(markersContainer);
        
        // Track user clicks
        let userClicks = [];
        
        // Add click indicator
        const clickIndicator = document.createElement('div');
        clickIndicator.className = 'click-indicator';
        clickIndicator.style.marginTop = '10px';
        clickIndicator.style.fontSize = '16px';
        clickIndicator.textContent = 'Clicks: 0';
        
        // Add reset button
        const resetButton = document.createElement('button');
        resetButton.textContent = 'Reset Clicks';
        resetButton.className = 'reset-clicks-btn';
        resetButton.style.padding = '8px 15px';
        resetButton.style.backgroundColor = '#f44336';
        resetButton.style.color = 'white';
        resetButton.style.border = 'none';
        resetButton.style.borderRadius = '4px';
        resetButton.style.marginRight = '10px';
        resetButton.style.cursor = 'pointer';
        
        // Add click event handler for the main image
        mainImg.addEventListener('click', function(e) {
            // Get click coordinates relative to the image
            const rect = e.target.getBoundingClientRect();
            const x = Math.round(e.clientX - rect.left);
            const y = Math.round(e.clientY - rect.top);
            
            // Add click to the array
            userClicks.push([x, y]);
            
            // Show click marker
            addClickMarker(x, y, userClicks.length, markersContainer);
            
            // Update click indicator
            clickIndicator.textContent = `Clicks: ${userClicks.length}`;
            
            // Enable the dedicated submit button if at least one click has been made
            clickOrderSubmitBtn.disabled = false;
            
            // Log for debugging
            console.log(`Click ${userClicks.length} at:`, { x, y });
        });
        
        // Event listener for reset button
        resetButton.addEventListener('click', function() {
            // Clear user clicks
            userClicks = [];
            
            // Clear markers
            markersContainer.innerHTML = '';
            
            // Update click indicator
            clickIndicator.textContent = 'Clicks: 0';
            
            // Disable submit button
            submitBtn.disabled = true;
        });
        
        // Add components to layout
        layoutContainer.appendChild(orderImageContainer);
        layoutContainer.appendChild(mainImageContainer);
        
        // Add controls container
        const controlsContainer = document.createElement('div');
        controlsContainer.className = 'controls-container';
        controlsContainer.style.display = 'flex';
        controlsContainer.style.justifyContent = 'center';
        controlsContainer.style.alignItems = 'center';
        controlsContainer.style.marginTop = '15px';
        
        // Add controls to container
        controlsContainer.appendChild(resetButton);
        controlsContainer.appendChild(clickIndicator);
        
        // Add controls to layout
        layoutContainer.appendChild(controlsContainer);
        
        // Create a dedicated submit button for the Click Order puzzle
        const clickOrderSubmitBtn = document.createElement('button');
        clickOrderSubmitBtn.textContent = 'Submit Order';
        clickOrderSubmitBtn.className = 'click-order-submit-btn';
        clickOrderSubmitBtn.style.padding = '10px 20px';
        clickOrderSubmitBtn.style.backgroundColor = '#4CAF50';
        clickOrderSubmitBtn.style.color = 'white';
        clickOrderSubmitBtn.style.border = 'none';
        clickOrderSubmitBtn.style.borderRadius = '4px';
        clickOrderSubmitBtn.style.marginTop = '15px';
        clickOrderSubmitBtn.style.cursor = 'pointer';
        clickOrderSubmitBtn.style.fontSize = '16px';
        clickOrderSubmitBtn.disabled = true; // Disabled until clicks are made
        
        // Add submit button to layout
        layoutContainer.appendChild(clickOrderSubmitBtn);
        
        // Add layout to puzzle container
        puzzleImageContainer.appendChild(layoutContainer);
        
        // Hide the original input field and submit button
        userAnswerInput.style.display = 'none';
        submitBtn.style.display = 'none';
        inputGroup.style.display = 'none';
        
        // Enable the dedicated submit button when clicks are made
        mainImg.addEventListener('click', function() {
            if (userClicks.length > 0) {
                clickOrderSubmitBtn.disabled = false;
            }
        });
        
        // Reset button should disable submit button
        resetButton.addEventListener('click', function() {
            clickOrderSubmitBtn.disabled = true;
        });
        
        // Add event listener to the dedicated submit button
        clickOrderSubmitBtn.addEventListener('click', function() {
            // Set the clicks as the answer
            userAnswerInput.value = JSON.stringify(userClicks);
            
            // Send the data to the server
            fetch('/api/check_answer', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    puzzle_type: currentPuzzle.puzzle_type,
                    puzzle_id: currentPuzzle.puzzle_id,
                    answer: userClicks
                })
            })
            .then(response => response.json())
            .then(data => {
                // Update stats
                benchmarkStats.total++;
                if (data.correct) {
                    benchmarkStats.correct++;
                    resultMessage.textContent = 'Correct!';
                    resultMessage.className = 'result-message correct';
                } else {
                    resultMessage.textContent = 'Incorrect.';
                    resultMessage.className = 'result-message incorrect';
                }
                
                updateStats();
                
                // Record benchmark result
                recordBenchmarkResult({
                    puzzle_type: currentPuzzle.puzzle_type,
                    puzzle_id: currentPuzzle.puzzle_id,
                    user_answer: userClicks,
                    correct_answer: data.correct_answer,
                    correct: data.correct
                });
                
                // Disable the submit button
                clickOrderSubmitBtn.disabled = true;
                
                // Load a new puzzle after a delay
                setTimeout(loadNewPuzzle, 2000);
            })
            .catch(error => {
                console.error('Error checking answer:', error);
                resultMessage.textContent = 'Error checking answer. Please try again.';
                resultMessage.className = 'result-message incorrect';
                // Re-enable the submit button on error
                clickOrderSubmitBtn.disabled = false;
            });
        });
        
        // In debug mode, show the correct click positions
        if (DEBUG_MODE) {
            showClickOrderAnswerPositions(mainImageContainer);
        }
    }
    
    // Function to add a numbered click marker
    function addClickMarker(x, y, number, container) {
        const marker = document.createElement('div');
        marker.className = 'click-marker';
        marker.style.position = 'absolute';
        marker.style.left = `${x - 15}px`;
        marker.style.top = `${y - 15}px`;
        marker.style.width = '30px';
        marker.style.height = '30px';
        marker.style.borderRadius = '50%';
        marker.style.backgroundColor = 'rgba(255, 0, 0, 0.6)';
        marker.style.border = '2px solid white';
        marker.style.color = 'white';
        marker.style.fontWeight = 'bold';
        marker.style.display = 'flex';
        marker.style.justifyContent = 'center';
        marker.style.alignItems = 'center';
        marker.style.fontSize = '14px';
        marker.style.zIndex = '100';
        marker.style.pointerEvents = 'none'; // Don't block future clicks
        marker.textContent = number.toString();
        
        container.appendChild(marker);
    }
    
    // Function to show correct answer positions in debug mode
    function showClickOrderAnswerPositions(container) {
        fetch('/api/get_ground_truth', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                puzzle_type: currentPuzzle.puzzle_type,
                puzzle_id: currentPuzzle.puzzle_id
            })
        })
        .then(response => response.json())
        .then(gtData => {
            if (gtData.answer && Array.isArray(gtData.answer)) {
                const correctPositions = gtData.answer;
                const tolerance = currentPuzzle.tolerance || 20;
                
                // Create a debug layer
                const debugLayer = document.createElement('div');
                debugLayer.className = 'debug-layer';
                debugLayer.style.position = 'absolute';
                debugLayer.style.top = '0';
                debugLayer.style.left = '0';
                debugLayer.style.width = '100%';
                debugLayer.style.height = '100%';
                debugLayer.style.pointerEvents = 'none';
                
                // Add correct position indicators
                correctPositions.forEach((pos, index) => {
                    const [x, y] = pos;
                    
                    // Create circle for tolerance area
                    const toleranceCircle = document.createElement('div');
                    toleranceCircle.className = 'tolerance-circle';
                    toleranceCircle.style.position = 'absolute';
                    toleranceCircle.style.left = `${x - tolerance}px`;
                    toleranceCircle.style.top = `${y - tolerance}px`;
                    toleranceCircle.style.width = `${tolerance * 2}px`;
                    toleranceCircle.style.height = `${tolerance * 2}px`;
                    toleranceCircle.style.borderRadius = '50%';
                    toleranceCircle.style.border = '2px dashed green';
                    toleranceCircle.style.backgroundColor = 'rgba(0, 255, 0, 0.1)';
                    
                    // Create label with position number
                    const posLabel = document.createElement('div');
                    posLabel.className = 'position-label';
                    posLabel.style.position = 'absolute';
                    posLabel.style.left = `${x}px`;
                    posLabel.style.top = `${y - 20}px`;
                    posLabel.style.transform = 'translate(-50%, -50%)';
                    posLabel.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
                    posLabel.style.color = 'white';
                    posLabel.style.padding = '2px 5px';
                    posLabel.style.borderRadius = '3px';
                    posLabel.style.fontSize = '10px';
                    posLabel.textContent = `${index + 1}: (${x}, ${y})`;
                    
                    debugLayer.appendChild(toleranceCircle);
                    debugLayer.appendChild(posLabel);
                });
                
                container.appendChild(debugLayer);
            }
        })
        .catch(error => {
            console.error('Error fetching ground truth for Click_Order:', error);
        });
    }
    
    // Function to setup the Hold Button CAPTCHA
    function setupHoldButton() {
        // record the start time
        puzzleStartTime = Date.now();
        // Clear the puzzle image container first
        puzzleImageContainer.innerHTML = '';
        
        // Create a container for the button
        const buttonContainer = document.createElement('div');
        buttonContainer.className = 'hold-button-container';
        buttonContainer.style.position = 'relative';
        buttonContainer.style.width = '100%';
        buttonContainer.style.maxWidth = '400px';
        buttonContainer.style.margin = '0 auto';
        buttonContainer.style.textAlign = 'center';
        
        // If the CAPTCHA has an image, show it above the button
        if (currentPuzzle.image_path) {
            const imageElement = document.createElement('img');
            imageElement.src = currentPuzzle.image_path;
            imageElement.alt = 'Hold Button CAPTCHA';
            imageElement.style.display = 'block';
            imageElement.style.width = '100%';
            imageElement.style.maxWidth = '400px';
            imageElement.style.margin = '0 auto 20px';
            imageElement.style.borderRadius = '8px';
            buttonContainer.appendChild(imageElement);
        }
        
        // Create button element
        const button = document.createElement('div');
        button.className = 'hold-button';
        button.style.position = 'relative';
        button.style.width = '100%';
        button.style.height = 'auto';
        button.style.cursor = 'pointer';
        button.style.userSelect = 'none';
        button.style.borderRadius = '50px';
        button.style.border = '3px solid #333';
        button.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)';
        button.style.backgroundColor = '#f8f8f8';
        button.style.padding = '30px 0';
        button.style.fontSize = '28px';
        button.style.fontWeight = 'bold';
        button.style.color = '#333';
        button.style.textAlign = 'center';
        button.style.transition = 'background-color 0.3s';
        button.textContent = 'HOLD';
        
        // Create progress bar
        const progressBar = document.createElement('div');
        progressBar.className = 'hold-progress';
        progressBar.style.position = 'absolute';
        progressBar.style.left = '0';
        progressBar.style.bottom = '0';
        progressBar.style.height = '8px';
        progressBar.style.width = '0%';
        progressBar.style.backgroundColor = '#4CAF50';
        progressBar.style.transition = 'width 0.1s linear';
        progressBar.style.borderRadius = '0 0 50px 50px';
        
        // Get hold time from data
        const requiredHoldTime = currentPuzzle.hold_time || 3; // Default to 3 seconds
        
        // Variables to track holding
        let isHolding = false;
        let holdStartTime = 0;
        let holdTimer = null;
        let completed = false;
        let currentHoldTime = 0;
        
        // Add event listeners for hold detection
        button.addEventListener('mousedown', startHolding);
        button.addEventListener('touchstart', startHolding);
        document.addEventListener('mouseup', stopHolding);
        document.addEventListener('touchend', stopHolding);
        
        function startHolding(e) {
            if (completed) return;
            
            // Prevent default behaviors for touch
            if (e.type === 'touchstart') {
                e.preventDefault();
            }
            
            isHolding = true;
            holdStartTime = Date.now();
            button.style.backgroundColor = '#e0e0e0';
            
            // Start progress animation
            holdTimer = setInterval(() => {
                if (!isHolding) return;
                
                const elapsedTime = (Date.now() - holdStartTime) / 1000; // in seconds
                currentHoldTime = elapsedTime;
                
                // Update progress bar
                const progress = Math.min((elapsedTime / requiredHoldTime) * 100, 100);
                progressBar.style.width = `${progress}%`;
                
                // Check if hold is complete
                if (elapsedTime >= requiredHoldTime && !completed) {
                    completeHold();
                }
            }, 100); // Update every 100ms
        }
        
        function stopHolding() {
            if (!isHolding || completed) return;
            
            isHolding = false;
            button.style.backgroundColor = '#f8f8f8';
            
            // Reset progress if not completed
            if (!completed) {
                progressBar.style.width = '0%';
                clearInterval(holdTimer);
            }
        }
        
        function completeHold() {
            completed = true;
            clearInterval(holdTimer);
            
            // Change button appearance
            button.style.backgroundColor = '#4CAF50';
            button.style.color = 'white';
            button.textContent = 'COMPLETED';
            
            // Set the user answer to the current hold time
            userAnswerInput.value = currentHoldTime.toFixed(2);
            
            // Enable submit button
            submitBtn.disabled = false;
            resultMessage.textContent = "Button hold completed! Click 'Submit' to continue.";
            resultMessage.className = 'result-message instruction';
        }
        
        // Add the progress bar to button
        button.appendChild(progressBar);
        
        // Add button to container
        buttonContainer.appendChild(button);
        
        // Add to puzzle container
        puzzleImageContainer.appendChild(buttonContainer);
        
        // Reset and clear input field
        userAnswerInput.value = '';
        userAnswerInput.style.display = 'none';
        submitBtn.disabled = false;  // Disable submit button until hold is complete
    }

    // Function to show dotted areas in debug mode for Pick_Area
    function showPickAreaTargets(container) {
        if (!DEBUG_MODE || !currentPuzzle) return;
        
        // Fetch ground truth data to show the correct area
        fetch('/api/get_ground_truth', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                puzzle_type: currentPuzzle.puzzle_type,
                puzzle_id: currentPuzzle.puzzle_id
            })
        })
        .then(response => response.json())
        .then(gtData => {
            if (gtData.answer && gtData.answer.area) {
                // Get the area from ground truth
                const areaCoords = gtData.answer.area;
                const areaType = gtData.answer.type || 'largest region';
                
                // Create a marker for the area
                const areaMarker = document.createElement('div');
                areaMarker.className = 'area-marker debug-marker';
                areaMarker.style.position = 'absolute';
                areaMarker.style.border = '3px dashed #ff3333';
                // Use a more transparent background to show the underlying dotted lines
                areaMarker.style.backgroundColor = 'rgba(255, 51, 51, 0.15)';
                areaMarker.style.zIndex = '999';
                // Add border radius to better represent curved areas
                areaMarker.style.borderRadius = '25%';
                
                // Set position and size
                const [topLeft, bottomRight] = areaCoords;
                const [minX, minY] = topLeft;
                const [maxX, maxY] = bottomRight;
                
                areaMarker.style.left = `${minX}px`;
                areaMarker.style.top = `${minY}px`;
                areaMarker.style.width = `${maxX - minX}px`;
                areaMarker.style.height = `${maxY - minY}px`;
                
                // Add a label that better explains what to do
                const label = document.createElement('div');
                label.className = 'debug-label';
                label.style.position = 'absolute';
                label.style.top = '5px';
                label.style.left = '50%';
                label.style.transform = 'translateX(-50%)';
                label.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
                label.style.color = 'white';
                label.style.padding = '5px 10px';
                label.style.fontSize = '14px';
                label.style.fontWeight = 'bold';
                label.style.borderRadius = '3px';
                label.style.whiteSpace = 'nowrap';
                label.style.textAlign = 'center';
                label.textContent = `${areaType}: (${minX},${minY}) to (${maxX},${maxY})`;
                
                areaMarker.appendChild(label);
                
                // Add a note to explain that the actual area follows the dotted lines
                const note = document.createElement('div');
                note.className = 'area-note';
                note.style.position = 'absolute';
                note.style.bottom = '10px';
                note.style.left = '50%';
                note.style.transform = 'translateX(-50%)';
                note.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
                note.style.color = 'white';
                note.style.padding = '5px 10px';
                note.style.fontSize = '12px';
                note.style.borderRadius = '3px';
                note.style.maxWidth = '90%';
                note.style.textAlign = 'center';
                note.textContent = 'Follow the dotted white lines to identify the actual area';
                
                areaMarker.appendChild(note);
                container.appendChild(areaMarker);
                
                // Create indicators to highlight the dotted lines
                // This is a simplistic approach; ideally we would trace the actual dotted lines
                highlightDottedLines(container, areaCoords);
            }
        })
        .catch(error => {
            console.error('Error fetching ground truth for Pick_Area:', error);
        });
    }
    
    // Function to highlight the dotted lines that define the area
    function highlightDottedLines(container, areaCoords) {
        const [topLeft, bottomRight] = areaCoords;
        const [minX, minY] = topLeft;
        const [maxX, maxY] = bottomRight;
        
        // Create a canvas element to draw over the image
        const canvas = document.createElement('canvas');
        canvas.className = 'dotted-line-highlight';
        canvas.style.position = 'absolute';
        canvas.style.top = '0';
        canvas.style.left = '0';
        canvas.style.pointerEvents = 'none'; // Don't interfere with clicks
        canvas.style.zIndex = '998'; // Just below the area marker
        
        // Wait for the image to load to get the correct dimensions
        const img = container.querySelector('img');
        if (!img) return;
        
        if (img.complete) {
            setupCanvas();
        } else {
            img.onload = setupCanvas;
        }
        
        function setupCanvas() {
            canvas.width = img.clientWidth;
            canvas.height = img.clientHeight;
            
            const ctx = canvas.getContext('2d');
            ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)';
            ctx.lineWidth = 3;
            ctx.setLineDash([5, 5]); // Create a dashed line effect
            
            // Draw a path that approximates the dotted lines
            // This is just a rough approximation - would need image processing to trace actual lines
            ctx.beginPath();
            
            // Top line
            ctx.moveTo(minX, minY);
            ctx.lineTo(maxX, minY);
            
            // Right line
            ctx.moveTo(maxX, minY);
            ctx.lineTo(maxX, maxY);
            
            // Bottom line
            ctx.moveTo(maxX, maxY);
            ctx.lineTo(minX, maxY);
            
            // Left line
            ctx.moveTo(minX, maxY);
            ctx.lineTo(minX, minY);
            
            ctx.stroke();
            
            container.appendChild(canvas);
        }
    }

    // Function to show the area to avoid for Misleading_Click puzzles
    function showMisleadingClickArea(container, avoidArea) {
        if (!DEBUG_MODE || !avoidArea) return;
        
        // Create a marker for the area to avoid
        const areaMarker = document.createElement('div');
        areaMarker.className = 'avoid-area-marker debug-marker';
        areaMarker.style.position = 'absolute';
        areaMarker.style.border = '3px dashed red';
        areaMarker.style.backgroundColor = 'rgba(255, 0, 0, 0.3)';
        areaMarker.style.zIndex = '999';
        
        // Set position and size
        const { x, y, width, height } = avoidArea;
        areaMarker.style.left = `${x}px`;
        areaMarker.style.top = `${y}px`;
        areaMarker.style.width = `${width}px`;
        areaMarker.style.height = `${height}px`;
        
        // Add a label
        const label = document.createElement('div');
        label.className = 'debug-label';
        label.style.position = 'absolute';
        label.style.top = '-20px';
        label.style.left = '0';
        label.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        label.style.color = 'white';
        label.style.padding = '2px 5px';
        label.style.fontSize = '12px';
        label.style.borderRadius = '3px';
        label.textContent = `DO NOT CLICK IN THIS AREA: (${x},${y}) ${width}x${height}`;
        
        // Add a warning sign in the middle
        const warningSign = document.createElement('div');
        warningSign.className = 'warning-sign';
        warningSign.textContent = 'DO NOT CLICK HERE';
        warningSign.style.position = 'absolute';
        warningSign.style.top = '50%';
        warningSign.style.left = '50%';
        warningSign.style.transform = 'translate(-50%, -50%)';
        warningSign.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        warningSign.style.color = '#ff5555';
        warningSign.style.padding = '5px 10px';
        warningSign.style.fontSize = '14px';
        warningSign.style.fontWeight = 'bold';
        warningSign.style.borderRadius = '3px';
        warningSign.style.whiteSpace = 'nowrap';
        warningSign.style.zIndex = '10';
        
        areaMarker.appendChild(label);
        areaMarker.appendChild(warningSign);
        container.appendChild(areaMarker);
        
        console.log('Misleading Click area to avoid:', avoidArea);
    }

    /**
     * Checks if a point is inside a polygon defined by an array of points
     * Uses ray-casting algorithm
     * @param {number} x - X coordinate of the point to check
     * @param {number} y - Y coordinate of the point to check
     * @param {array} polygon - Array of points defining the polygon [[x1,y1], [x2,y2], ...]
     * @returns {boolean} True if the point is inside the polygon
     */
    function pointInPolygon(x, y, polygon) {
        if (!polygon || polygon.length < 3) return false;

        let inside = false;
        for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
            const xi = polygon[i][0], yi = polygon[i][1];
            const xj = polygon[j][0], yj = polygon[j][1];
            
            const intersect = ((yi > y) != (yj > y)) &&
                (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) inside = !inside;
        }
        
        return inside;
    }

    /**
     * Sets up the Dart Count interface with reference number and dart images
     */
    function setupDartCount() {
        // Clear the puzzle image container
        puzzleImageContainer.innerHTML = '';
        
        // Create container for the dart count interface
        const dartContainer = document.createElement('div');
        dartContainer.className = 'dart-count-container';
        
        // Create a horizontal layout
        const horizontalLayout = document.createElement('div');
        horizontalLayout.className = 'dart-count-horizontal-layout';
        
        // Create reference container (shows the target number)
        const referenceContainer = document.createElement('div');
        referenceContainer.className = 'dart-count-reference';
        
        // Add reference image - check all possible locations for data
        const referenceImage = document.createElement('img');
        if (currentPuzzle.additional_data && currentPuzzle.additional_data.reference_image) {
            referenceImage.src = currentPuzzle.additional_data.reference_image;
        } else if (currentPuzzle.reference_image) {
            referenceImage.src = currentPuzzle.reference_image;
        } else {
            console.error('Reference image not found for Dart Count puzzle');
        }
        referenceImage.alt = 'Target Number';
        referenceImage.className = 'dart-count-reference-img';
        referenceContainer.appendChild(referenceImage);
        
        // Add reference caption
        const referenceCaption = document.createElement('div');
        referenceCaption.className = 'dart-count-caption';
        referenceCaption.textContent = 'Find sum of darts equal to this';
        referenceContainer.appendChild(referenceCaption);
        
        // Create options container
        const optionsContainer = document.createElement('div');
        optionsContainer.className = 'dart-count-options';
        
        // Get option images from all possible locations
        let optionImages = [];
        if (currentPuzzle.additional_data && currentPuzzle.additional_data.option_images) {
            optionImages = currentPuzzle.additional_data.option_images;
        } else if (currentPuzzle.option_images) {
            optionImages = currentPuzzle.option_images;
        } else {
            console.error('Option images not found for Dart Count puzzle');
            optionImages = [];
        }
        
        // Add option image
        const optionImage = document.createElement('img');
        if (optionImages.length > 0) {
            optionImage.src = optionImages[0]; // Start with first option
        }
        optionImage.alt = 'Dart Option';
        optionImage.className = 'dart-count-option-img';
        optionsContainer.appendChild(optionImage);
        
        // Create navigation controls
        const controlsContainer = document.createElement('div');
        controlsContainer.className = 'dart-count-controls';
        
        // Left arrow
        const leftArrow = document.createElement('button');
        leftArrow.innerHTML = '&larr;';
        leftArrow.className = 'dart-count-arrow left-arrow';
        leftArrow.addEventListener('click', () => updateDartOption(-1));
        
        // Right arrow
        const rightArrow = document.createElement('button');
        rightArrow.innerHTML = '&rarr;';
        rightArrow.className = 'dart-count-arrow right-arrow';
        rightArrow.addEventListener('click', () => updateDartOption(1));
        
        // Add arrows to controls
        controlsContainer.appendChild(leftArrow);
        controlsContainer.appendChild(rightArrow);
        
        // Add controls to options container
        optionsContainer.appendChild(controlsContainer);
        
        // Add reference and options to horizontal layout
        horizontalLayout.appendChild(referenceContainer);
        horizontalLayout.appendChild(optionsContainer);
        
        // Add horizontal layout to main container
        dartContainer.appendChild(horizontalLayout);
        
        // Add option indicators (dots)
        const indicators = document.createElement('div');
        indicators.className = 'dart-count-indicators';
        
        const numOptions = optionImages.length;
        for (let i = 0; i < numOptions; i++) {
            const dot = document.createElement('span');
            dot.className = 'dart-count-dot';
            if (i === 0) {
                dot.classList.add('active');
            }
            indicators.appendChild(dot);
        }
        
        // Add indicators to main container
        dartContainer.appendChild(indicators);
        
        // Add submit button
        const submitBtn = document.createElement('button');
        submitBtn.textContent = 'Submit';
        submitBtn.className = 'dart-count-submit';
        submitBtn.addEventListener('click', submitAnswer);
        
        // Add containers to puzzle image container
        puzzleImageContainer.appendChild(dartContainer);
        puzzleImageContainer.appendChild(submitBtn);
        
        // Store current index in the hidden input for submission
        userAnswerInput.value = '0';
        
        // Log all available data for debugging
        console.log('Dart Count puzzle data:', currentPuzzle);
    }
    
    /**
     * Update the displayed dart option image based on navigation direction
     * @param {number} direction - Direction to navigate (-1 for left, 1 for right)
     */
    function updateDartOption(direction) {
        const optionImage = document.querySelector('.dart-count-option-img');
        const dots = document.querySelectorAll('.dart-count-dot');
        
        // Get option images from all possible locations
        let optionImages = [];
        if (currentPuzzle.additional_data && currentPuzzle.additional_data.option_images) {
            optionImages = currentPuzzle.additional_data.option_images;
        } else if (currentPuzzle.option_images) {
            optionImages = currentPuzzle.option_images;
        } else {
            console.error('Option images not found for Dart Count puzzle');
            return;
        }
        
        // Get current index from input field
        let currentIndex = parseInt(userAnswerInput.value) || 0;
        const numOptions = optionImages.length;
        
        // Calculate new index with wrap-around
        let newIndex = (currentIndex + direction + numOptions) % numOptions;
        
        // Update the option image
        optionImage.src = optionImages[newIndex];
        
        // Update dots
        dots.forEach((dot, index) => {
            if (index === newIndex) {
                dot.classList.add('active');
            } else {
                dot.classList.remove('active');
            }
        });
        
        // Store selected answer for submission
        userAnswerInput.value = newIndex.toString();
        
        // Log for debugging
        console.log('Updated dart option:', {
            index: newIndex,
            src: optionImage.src
        });
    }

    /**
     * Display difficulty stars based on CAPTCHA type
     * @param {string} puzzleType - The type of CAPTCHA puzzle
     */
    function displayDifficultyStars(puzzleType) {
        const difficultyRatings = {
            'Dice_Count': 1,
            'Geometry_Click': 1,
            'Rotation_Match': 3,
            'Slide_Puzzle': 2,
            'Unusual_Detection': 3,
            'Image_Recognition': 2,
            'Bingo': 4,
            'Image_Matching': 2,
            'Patch_Select': 3,
            'Dart_Count': 3,
            'Object_Match': 2,
            'Select_Animal': 1,
            'Coordinates': 3,
            'Path_Finder': 3,
            'Place_Dot': 2,
            'Connect_icon': 3,
            'Click_Order': 4,
            'Hold_Button': 2,
            'Misleading_Click': 4,
            'Pick_Area': 5,
        };

        const difficulty = difficultyRatings[puzzleType] || 1;
        const starsContainer = document.getElementById('difficulty-stars');
        
        // Safety check to ensure the container exists
        if (!starsContainer) {
            console.error('Stars container not found!');
            return;
        }
        
        // Clear the container
        starsContainer.innerHTML = '';

        // Create and append stars
        for (let i = 0; i < 5; i++) {
            const star = document.createElement('span');
            star.className = 'star';
            star.innerHTML = i < difficulty ? '★' : '☆'; // Filled or empty star
            starsContainer.appendChild(star);
        }
        
        // Log for debugging
        console.log(`Displayed ${difficulty} stars for puzzle type: ${puzzleType}`);
    }

    // Function to get a new puzzle
    function getPuzzle(callback) {
        let queryParams = '';
        
        // Check if debug mode is active and add the debug_type parameter if it is
        if (DEBUG_MODE && DEBUG_TYPE) {
            queryParams = `?debug_type=${encodeURIComponent(DEBUG_TYPE)}`;
        }
        
        fetch('/api/get_puzzle' + queryParams)
            .then(response => response.json())
            .then(data => {
                currentPuzzle = data;
                
                // Log the data for debugging
                console.log('Puzzle data:', data);
                
                // Set the prompt and update debug information
                const promptElement = document.getElementById('puzzle-prompt');
                promptElement.textContent = data.prompt;
                
                // Display difficulty stars based on puzzle type
                displayDifficultyStars(data.puzzle_type);
                
                // Update debug indicator if in debug mode
                const debugIndicator = document.getElementById('debug-indicator');
                const debugTypeDisplay = document.getElementById('debug-type-display');
                
                if (DEBUG_MODE && DEBUG_TYPE) {
                    debugIndicator.style.display = 'block';
                    debugTypeDisplay.textContent = DEBUG_TYPE;
                } else {
                    debugIndicator.style.display = 'none';
                }
                
                // Handle different input types
                const imageContainer = document.getElementById('puzzle-image-container');
                const userAnswerInput = document.getElementById('user-answer');
                const submitBtn = document.getElementById('submit-answer');
                
                // Reset the input field and enable submit button
                userAnswerInput.value = '';
                submitBtn.disabled = false;
                
                // Clear any previous result message
                const resultMessage = document.getElementById('result-message');
                resultMessage.textContent = '';
                resultMessage.className = 'result-message';
                
                // Clear the puzzle image container
                imageContainer.innerHTML = '';
                
                // Set up UI based on input type
                if (data.input_type === 'number') {
                    // For numeric input (e.g., Dice_Count)
                    userAnswerInput.type = 'number';
                    userAnswerInput.placeholder = 'Enter number';
                    userAnswerInput.style.display = 'block';
                    submitBtn.style.display = 'block';
                    
                    // Load the image
                    const img = document.createElement('img');
                    img.src = data.image_path;
                    img.alt = 'CAPTCHA Puzzle';
                    img.id = 'puzzle-image';
                    img.onload = function() {
                        imageContainer.appendChild(img);
                    };
                } else if (data.input_type === 'click') {
                    // For click-based puzzles (Geometry_Click, Place_Dot, etc.)
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'none';
                    
                    // Load the image and set up click handler
                    const img = document.createElement('img');
                    img.src = data.image_path;
                    img.alt = 'CAPTCHA Puzzle';
                    img.id = 'puzzle-image';
                    img.onclick = handleImageClick;
                    
                    img.onload = function() {
                        imageContainer.appendChild(img);
                        
                        // For Misleading_Click, show the area to avoid in debug mode
                        if (data.puzzle_type === 'Misleading_Click' && DEBUG_MODE) {
                            showMisleadingClickArea(imageContainer, data.avoid_area);
                        }
                        
                        // For Pick_Area, show the target areas in debug mode
                        if (data.puzzle_type === 'Pick_Area' && DEBUG_MODE) {
                            showPickAreaTargets(imageContainer);
                        }
                    };
                } else if (data.input_type === 'rotation') {
                    // For rotation puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up rotation controls
                    setupRotationControls();
                } else if (data.input_type === 'slide') {
                    // For slide puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up slide puzzle
                    setupSlidePuzzle();
                } else if (data.input_type === 'multiselect') {
                    // For multiple selection puzzles (Unusual_Detection)
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up grid for unusual detection
                    setupUnusualDetectionGrid();
                } else if (data.input_type === 'bingo_swap') {
                    // For bingo swap puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up bingo swap interface
                    setupBingoSwap();
                } else if (data.input_type === 'image_grid') {
                    // For image grid puzzles (Image_Recognition)
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up image recognition grid
                    setupImageRecognition();
                } else if (data.input_type === 'image_matching') {
                    // For image matching puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up image matching interface
                    setupImageMatching();
                } else if (data.input_type === 'patch_select') {
                    // For patch select puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up patch select grid
                    setupPatchSelectGrid();
                } else if (data.input_type === 'dart_count') {
                    // For dart count puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'none';
                    
                    // Set up dart count interface
                    setupDartCount();
                } else if (data.input_type === 'object_match') {
                    // For object match puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up object match interface
                    setupObjectMatch();
                } else if (data.input_type === 'select_animal') {
                    // For animal selection puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up animal selection grid
                    setupSelectAnimalGrid();
                } else if (data.input_type === 'place_dot') {
                    // For place dot puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'none';
                    
                    // Set up place dot interface
                    setupPlaceDot();
                } else if (data.input_type === 'connect_icon') {
                    // For connect icon puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up connect icon interface
                    setupConnectIcon();
                } else if (data.input_type === 'click_order') {
                    // For click order puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up click order interface
                    setupClickOrder();
                } else if (data.input_type === 'hold_button') {
                    // For hold button puzzles
                    userAnswerInput.style.display = 'none';
                    submitBtn.style.display = 'block';
                    
                    // Set up hold button interface
                    setupHoldButton();
                } else {
                    // Default to text input for other types
                    userAnswerInput.type = 'text';
                    userAnswerInput.placeholder = 'Your answer';
                    userAnswerInput.style.display = 'block';
                    submitBtn.style.display = 'block';
                    
                    // Load the image
                    const img = document.createElement('img');
                    img.src = data.image_path;
                    img.alt = 'CAPTCHA Puzzle';
                    img.id = 'puzzle-image';
                    img.onload = function() {
                        imageContainer.appendChild(img);
                    };
                }
                
                // Call the callback if provided
                if (callback && typeof callback === 'function') {
                    callback();
                }
            })
            .catch(error => {
                console.error('Error fetching puzzle:', error);
            });
    }
});