File size: 326,590 Bytes
bf3e333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>aiprompt-marie-kondo — Image &amp; Prompt Manager</title>
  <style>
:root {
  --bg: #14161a;
  --bg-elev: #1c1f26;
  --bg-cell: #23272f;
  --border: #2e333d;
  --text: #e7e9ee;
  --text-dim: #9aa1ad;
  --accent: #4f8cff;
  --accent-dim: #2c4a80;
  --ok: #37c07a;
  --ok-dim: #1e5a3c;
  --none: #6b7280;
  --warn: #e0a13a;
  --error: #e5484d;
  --dirty: #e0a13a;
  --star: #f2c14e;
  --cell-radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

/* `hidden` must win even over flex/grid/absolute display rules below. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
}

/* ---------- Buttons & inputs ---------- */
.btn {
  background: var(--bg-cell);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
  line-height: 1;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent-dim); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger:hover { border-color: var(--error); color: var(--error); }
.btn:disabled { opacity: 0.45; cursor: default; }

/* ---------- Overflow menu ("⋮") ----------
   Used by the prompt-editor header and every scene header: the occasional
   actions live here so a header row never has to wrap. */
.action-menu { position: relative; }
.action-menu-btn { padding: 7px 9px; font-size: 14px; }
.action-menu-list {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 5;
  min-width: 190px;
  display: flex;
  flex-direction: column;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-cell);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.5);
}
/* Near the bottom of the scrolling list the menu opens upwards (openActionMenu). */
.action-menu.flip-up .action-menu-list { top: auto; bottom: calc(100% + 4px); }
.action-menu-item {
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  padding: 7px 10px;
  font-size: 13px;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.action-menu-item:hover:not(:disabled) { background: var(--accent-dim); }
.action-menu-item:disabled { opacity: 0.45; cursor: default; }
.action-menu-item.danger { color: var(--error); }
.action-menu-item.danger:hover:not(:disabled) { background: rgba(229, 72, 77, 0.16); }

.search {
  width: 100%;
  max-width: 520px;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
}
.search:focus { border-color: var(--accent); }

/* ---------- App nav (section switch) ---------- */
#appnav {
  display: flex;
  align-items: center;
  flex-wrap: wrap; /* narrow windows: wrap instead of pushing buttons off screen */
  gap: 20px;
  padding: 8px 14px;
  background: #0f1115;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.brand { font-weight: 700; font-size: 14px; }
.sections { display: flex; gap: 4px; }
.tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
.tab:hover { color: var(--text); }
.tab.active { background: var(--bg-cell); border-color: var(--border); color: var(--text); }

/* ---------- Sections ---------- */
.section {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative; /* scopes the detail overlay to the section */
}

/* ---------- Top bar ---------- */
#topbar, .topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
  padding: 10px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.bar-left { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; min-width: 0; }
.bar-center { flex: 1 1 220px; display: flex; justify-content: center; min-width: 0; }
.bar-right { display: flex; align-items: center; flex-wrap: wrap; gap: 14px; }

.folder-name {
  color: var(--text-dim);
  font-size: 13px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filters { display: flex; gap: 4px; }
.chip {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 999px;
  cursor: pointer;
}
.chip:hover { color: var(--text); }
.chip.active { background: var(--accent-dim); border-color: var(--accent); color: #fff; }

.counts { font-size: 12px; color: var(--text-dim); white-space: nowrap; }

/* ---------- Grid ---------- */
.grid-view { flex: 1 1 auto; position: relative; min-height: 0; }
.grid-scroll {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
}
.grid-inner { position: relative; width: 100%; }

.cell {
  position: absolute;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: var(--cell-radius);
  overflow: hidden;
  cursor: pointer;
}
.cell:hover { border-color: var(--accent); }
.cell.dirty { box-shadow: inset 0 0 0 2px var(--dirty); }
.cell .thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transition: opacity 0.2s ease;
  background:
    repeating-conic-gradient(#20242c 0% 25%, #262b34 0% 50%) 50% / 20px 20px;
}
.cell .thumb.loaded { opacity: 1; }

.badge {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  border: 2px solid rgba(0,0,0,0.35);
}
.badge.has { background: var(--ok); }
.badge.no  { background: var(--none); }

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-align: center;
  pointer-events: none;
}
.empty-state .muted { color: var(--text-dim); font-size: 13px; }

/* ---------- Detail ---------- */
.detail-view {
  position: absolute;
  inset: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  z-index: 20;
}
.detail-top {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.detail-title {
  font-size: 13px;
  color: var(--text);
  max-width: 40%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dirty-flag { color: var(--dirty); font-size: 12px; }
.spacer { flex: 1 1 auto; }

.detail-body {
  flex: 1 1 auto;
  display: grid;
  /* Prompt is the focus: it takes the large column on the left; the image
     sits in a narrow column on the right. */
  grid-template-columns: 1fr 420px;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
}
.detail-image {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: #0e0f12;
  min-width: 0;
  min-height: 0;
}
/* Privacy: hide the original — the editor takes the full width. */
.detail-view.img-hidden .detail-body { grid-template-columns: 1fr 0; }
.detail-view.img-hidden .detail-image { display: none; }
.detail-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.detail-edit {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  min-width: 0;
  min-height: 0;
}
.prompt-editor {
  flex: 1 1 auto;
  resize: none;
  background: var(--bg-cell);
  color: var(--text);
  border: none;
  outline: none;
  padding: 16px;
  font-size: 14px;
  line-height: 1.5;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.detail-meta {
  flex: 0 0 auto;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
}

/* ---------- Banner ---------- */
.banner {
  background: var(--error);
  color: #fff;
  padding: 10px 14px;
  font-size: 13px;
}
.banner code { background: rgba(0,0,0,0.25); padding: 1px 5px; border-radius: 4px; }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed;
  top: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 300;
  max-width: 380px;
}
.toast {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast.warn  { border-left-color: var(--warn); }
.toast.error { border-left-color: var(--error); }
.toast .toast-actions { display: flex; gap: 8px; justify-content: flex-end; }
.toast .toast-actions .btn { padding: 5px 10px; font-size: 12px; }

/* ---------- Modal (help / example) ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}
.modal-box {
  position: relative;
  z-index: 1;
  width: min(860px, 92vw);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.modal-title { font-weight: 600; font-size: 14px; }

.modal-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 16px;
  color: var(--text);
  font-size: 13px;
  line-height: 1.55;
}
.modal-body.pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.modal-html p { margin: 0 0 12px; }
.modal-html p:last-child { margin-bottom: 0; }
.modal-html .note {
  border-left: 3px solid var(--border);
  padding-left: 10px;
  color: var(--text-dim);
}
.modal-html h3 {
  margin: 22px 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}
.modal-html h3:first-child { margin-top: 0; }
.modal-html h4 {
  margin: 18px 0 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
}
.modal-html h4:first-child { margin-top: 0; }
.modal-html blockquote {
  margin: 0 0 12px;
  border-left: 3px solid var(--border);
  padding-left: 10px;
  color: var(--text-dim);
}
.modal-html details { margin: 0 0 12px; }
.modal-html summary { cursor: pointer; color: var(--accent); margin-bottom: 8px; }
.modal-html strong { color: var(--text); }
.modal-html ul { margin: 0 0 12px; padding-left: 20px; }
.modal-html li { margin: 0 0 5px; }
.modal-html code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
}
.modal-html pre.code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  margin: 0 0 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
}
.modal-html table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 12px;
  font-size: 12px;
}
.modal-html th,
.modal-html td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: left;
  vertical-align: top;
}
.modal-html th { background: var(--bg-cell); }

/* AI settings button status dot: grey=unconfigured, amber=unreachable/checking,
   green=connected. */
#llmSettingsBtn { display: inline-flex; align-items: center; }
.llm-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 7px;
  background: var(--none);
}
.llm-dot-off { background: var(--none); }
.llm-dot-warn { background: var(--warn); }
.llm-dot-on { background: var(--ok); }
.llm-dot-checking { background: var(--warn); animation: llm-dot-pulse 1s ease-in-out infinite; }
@keyframes llm-dot-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ---------- AI settings dialog ---------- */
.llm-form { display: flex; flex-direction: column; gap: 14px; max-width: 560px; }
.llm-field { display: flex; flex-direction: column; gap: 5px; }
.llm-label {
  font-size: 11px;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.llm-label .muted { text-transform: none; letter-spacing: 0; color: var(--text-dim); }
.llm-input {
  width: 100%;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 10px;
  font-size: 13px;
  outline: none;
}
.llm-input:focus { border-color: var(--accent); }
.llm-input-narrow { width: 100px; }
.llm-checkline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}
.llm-checkline input { accent-color: var(--accent); cursor: pointer; }
.llm-model-row { display: flex; gap: 8px; align-items: center; }
.llm-model-row .llm-input { flex: 1 1 auto; }
.llm-actions { display: flex; align-items: center; gap: 12px; margin-top: 4px; }
.llm-reset { padding: 3px 9px; font-size: 11px; }

/* ---------- Prompt-templates manager (full-screen, sidebar + pane) ---------- */
.modal-front { z-index: 210; } /* sits above the AI-settings dialog */
.modal-box-xl { width: min(1200px, 96vw); height: 90vh; max-height: 90vh; }
.pt-body { display: flex; padding: 0; overflow: hidden; }
.pt-layout {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: grid;
  grid-template-columns: 220px 1fr;
}
.pt-sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg-elev);
  overflow-y: auto;
  min-height: 0;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.pt-sidebar-item {
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
.pt-sidebar-item:hover { color: var(--text); }
.pt-sidebar-item.active {
  background: var(--bg-cell);
  border-color: var(--border);
  color: var(--text);
}
.pt-pane {
  overflow-y: auto;
  min-height: 0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.pt-area-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text);
  font-weight: 600;
  margin: 14px 6px 4px;
}
.pt-area-title:first-child { margin-top: 0; }
.pt-type-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin: 6px 6px 2px 14px;
}
.pt-sidebar-item.pt-nested { margin-left: 10px; }
.pt-purpose-title { font-weight: 600; font-size: 15px; }
.pt-tpl-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.pt-tpl-bar .llm-label { flex: 0 0 auto; }
.pt-tpl-bar select { flex: 1 1 auto; }
.pt-field { display: flex; flex-direction: column; gap: 5px; }
.pt-field-head { display: flex; align-items: center; gap: 10px; }
.pt-field-head .llm-label { flex: 1 1 auto; }
.pt-textarea { min-height: 160px; }
.llm-status { font-size: 12px; color: var(--text-dim); }
.llm-status.ok { color: var(--accent); }
.llm-status.warn { color: var(--warn); }
.llm-status.error { color: var(--error); }

/* ---------- Generate-with-AI dialog (image prompts) ---------- */
/* Roomy modal: it can hold a lot of generated text, so give it width + height
   and let the result area grow to fill whatever space is left. */
.modal-box-lg { width: min(1100px, 95vw); height: 88vh; max-height: 88vh; }
.modal-box-lg .modal-body { display: flex; flex-direction: column; }
.ai-gen { display: flex; flex-direction: column; gap: 14px; flex: 1 1 auto; min-height: 0; }
.llm-field-grow { flex: 1 1 auto; min-height: 0; }
.llm-field-grow .ai-gen-ta { flex: 1 1 auto; height: 100%; min-height: 160px; resize: none; }
.ai-gen-cols { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.ai-gen-image {
  flex: 0 0 200px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.ai-gen-image img {
  width: 100%;
  max-height: 240px;
  object-fit: contain;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-cell);
}
.ai-gen-image .btn { width: 100%; }
.ai-gen-image .muted { font-size: 11px; word-break: break-all; }
.ai-gen-imgnote { font-style: italic; }
.ai-gen-fields { flex: 1 1 320px; display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.ai-gen-ta {
  width: 100%;
  box-sizing: border-box;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.5;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  resize: vertical;
  outline: none;
}
.ai-gen-ta:focus { border-color: var(--accent); }
.ai-gen-ta[readonly] { opacity: 0.8; cursor: default; }
.ai-gen-result { min-height: 140px; }
.ai-gen-actions { display: flex; align-items: center; gap: 12px; }

/* ---------- Prompt library ---------- */
.lib-body {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: grid;
  grid-template-columns: 300px 1fr;
  /* Fill the container height so the editor (and its textarea) can grow to the
     full available height instead of collapsing to content height. */
  grid-template-rows: minmax(0, 1fr);
}
.lib-list {
  overflow-y: auto;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--bg-elev);
}
/* Collapsed master list — the editor takes the full width. The track count must
   drop to ONE: `display:none` takes the list out of grid flow, so with two
   tracks the editor would auto-place into the (now 0-wide) first column. */
.lib-body.list-collapsed { grid-template-columns: minmax(0, 1fr); }
.lib-body.list-collapsed .lib-list { display: none; }
/* Compact glyph button that collapses/expands the list. */
.lib-list-toggle { padding: 7px 9px; }
.lib-item {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.lib-item:hover { background: var(--bg-cell); }
.lib-item.active { background: var(--accent-dim); }
.lib-item .t {
  font-size: 13px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lib-item .tags {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lib-right {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
}
.lib-editor {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.lib-empty {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-align: center;
  padding: 24px;
}
.lib-empty .muted { color: var(--text-dim); font-size: 13px; }
.lib-empty code { background: rgba(0,0,0,0.25); padding: 1px 5px; border-radius: 4px; }
.lib-edit-top {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.lib-title-input {
  /* Shrinks before anything else, so the type, stars and actions to its right
     stay on screen; below ~900px it takes a row of its own (see the end). */
  flex: 1 1 220px;
  min-width: 0;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 10px;
  font-size: 14px;
  outline: none;
}
.lib-title-input:focus { border-color: var(--accent); }
/* Prompt-type selector in the editor header. */
.lib-type-select {
  flex: 0 0 auto;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 8px;
  font-size: 12px;
  outline: none;
}
.lib-type-select:focus { border-color: var(--accent); }
/* Plain-text body editor for the "default" prompt type. */
.lib-body-edit { padding: 12px; }
.lib-body-text {
  width: 100%;
  box-sizing: border-box;
  min-height: 240px;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.55;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  resize: none;
  overflow: hidden; /* height managed by auto-grow */
  outline: none;
}
.lib-body-text:focus { border-color: var(--accent); }
/* Header row above the global/override sections (holds the ✨ Generate button). */
.lib-global-head {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 12px 0;
}
.lib-global-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text);
  font-weight: 600;
}
/* Subject-definitions builder row in the globals generator. */
.gen-subj-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.gen-subj-row .llm-input { width: auto; flex: 0 0 auto; }
.gen-next-label {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  color: var(--accent);
}
.gen-file { font-size: 12px; color: var(--text-dim); max-width: 220px; }
.gen-help-row { display: flex; align-items: center; gap: 6px; }
/* Reference list in the globals generator (source of truth = the textareas). */
.gen-list { display: flex; flex-direction: column; gap: 4px; }
.gen-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-cell);
}
.gen-item-btn { padding: 2px 8px; font-size: 12px; line-height: 1.2; }
.gen-item-label {
  flex: 0 0 auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--accent);
}
.gen-item-preview {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Rating (prompt metadata) ---------- */
.rating { display: inline-flex; gap: 1px; }
.rating .star {
  background: none;
  border: none;
  padding: 0 2px;
  cursor: pointer;
  line-height: 1;
  font-size: 18px;
  color: var(--text-dim);
}
.rating .star.on { color: var(--star); }
.rating:hover .star { color: var(--text-dim); }
.rating:hover .star.hot { color: var(--star); }
.lib-item .stars { color: var(--star); }

/* Scrollable editor body (global field + scenes share one scroll region) */
.lib-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
.lib-global {
  padding: 12px 12px 0;
}
/* Prompt-level metadata block (loras, …) above the global sections. */
.lib-meta {
  padding: 12px 12px 0;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}
.lib-meta .scene-label-row { margin-top: 0; }
.lib-meta label {
  font-size: 11px;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.lib-loras-input {
  width: 100%;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 10px;
  font-size: 13px;
  outline: none;
}
.lib-loras-input:focus { border-color: var(--accent); }
.global-tag {
  margin-left: 8px;
  font-size: 10px;
  color: var(--accent);
  border: 1px solid var(--accent-dim);
  border-radius: 999px;
  padding: 1px 7px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  vertical-align: middle;
}

/* Scenes list + cards */
.lib-scenes {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.lib-scenes-footer {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}
.scene-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev);
}
.scene-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.scene-card.collapsed .scene-head { border-bottom: none; }
.scene-card.collapsed .scene-body { display: none; }
.scene-toggle {
  width: 20px;
  height: 20px;
  padding: 0;
  line-height: 1;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
}
.scene-toggle:hover { color: var(--text); }
.scene-title { font-weight: 600; font-size: 13px; cursor: pointer; user-select: none; }
/* Compact reorder arrows in the scene header. */
.scene-move { padding: 7px 9px; font-size: 12px; }

/* "Incomplete" badge + card accent for scenes missing a required section. */
.scene-status {
  font-size: 10px;
  color: var(--warn);
  border: 1px solid var(--warn);
  border-radius: 999px;
  padding: 1px 7px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.scene-card.incomplete { border-color: var(--warn); }
.lib-incomplete { color: var(--warn); }
.scene-body { padding: 10px 12px 12px; }

/* Optional per-scene overrides (subject_definitions, retention_analysis),
   hidden by default behind a toggle to save space. */
.scene-overrides {
  margin-bottom: 12px;
  border: 1px dashed var(--border);
  border-radius: 6px;
  background: var(--bg-cell);
}
.scene-overrides-toggle {
  display: block;
  width: 100%;
  text-align: left;
  padding: 7px 10px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
}
.scene-overrides-toggle:hover { color: var(--text); }
.scene-overrides-body { padding: 0 10px 10px; }
.scene-override-gen { margin: 4px 0 10px; }
.scene-overrides.collapsed { border-style: solid; }
.scene-overrides.collapsed .scene-overrides-body { display: none; }
/* A scene that actually overrides a global section is marked in green, so a
   collapsed group still tells you an override is applied (and to which
   sections) without expanding it. */
.scene-overrides.has-override { border-color: var(--ok-dim); }
.scene-overrides.has-override .scene-overrides-toggle { color: var(--ok); }
.scene-overrides.has-override.collapsed { background: var(--ok-dim); }
.scene-overrides.has-override.collapsed .scene-overrides-toggle { color: var(--text); }
/* Section names inside the label keep their on-disk lowercase spelling. */
.scene-overrides-names {
  text-transform: none;
  letter-spacing: 0;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.scene-overrides-body > .scene-label-row:first-child { margin-top: 4px; }
/* The override group supplies the gap below it, so the next label sits tight. */
.scene-overrides + .scene-label-row { margin-top: 0; }

/* Task-type checkboxes below the summary label, above the field */
.task-types { margin: 2px 0 6px; }
.task-types-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
}
.task-type {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.task-type input { accent-color: var(--accent); cursor: pointer; }
.scene-label-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 12px 0 2px;
}
.scene-body > .scene-label-row:first-child { margin-top: 0; }
.scene-body label {
  font-size: 11px;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.section-help {
  width: 18px;
  height: 18px;
  padding: 0;
  line-height: 1;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-cell);
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.section-help:hover { color: var(--text); border-color: var(--accent); }
.scene-hint {
  margin: 0 0 6px;
  font-size: 11px;
  font-style: italic;
  color: var(--text-dim);
}
/* Live word count on the detailed_description label row (pushed to the right). */
.word-count {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
/* Highlight overlay: backdrop <div> behind a transparent <textarea>.
   Both must share identical box metrics so tokens line up exactly. */
.hl-wrap { position: relative; }
.hl-backdrop,
.scene-section {
  box-sizing: border-box;
  width: 100%;
  margin: 0;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}
.hl-backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background: var(--bg-cell);
  color: transparent; /* only the <mark> backgrounds show through */
  pointer-events: none;
  user-select: none;
}
.hl-backdrop mark {
  margin: 0;
  padding: 0; /* padding would shift following glyphs out of alignment */
  color: transparent;
  border-radius: 3px;
  background: transparent;
}
.hl-backdrop mark.hl-shot { background: rgba(55, 192, 122, 0.30); }   /* [Shot N] */
.hl-backdrop mark.hl-bracket { background: rgba(224, 161, 58, 0.32); } /* [task type] */
.hl-backdrop mark.hl-angle { background: rgba(79, 140, 255, 0.32); }   /* <Subject N> */

.scene-section {
  display: block;
  position: relative;
  z-index: 1;
  border-color: var(--border);
  background: transparent; /* let the backdrop show behind */
  color: var(--text);
  caret-color: var(--text);
  resize: none;
  overflow: hidden; /* height is managed by auto-grow */
  min-height: 40px;
}
.scene-section:focus { border-color: var(--accent); outline: none; }

/* Two-mode empty state for the library pane. */
#libEmpty .lib-empty-select { display: none; }
#libEmpty[data-mode="select"] .lib-empty-nofolder { display: none; }
#libEmpty[data-mode="select"] .lib-empty-select { display: block; }

/* ---------- Narrow / portrait windows ----------
   This is a desktop tool, but in a small or 9:16 window the fixed two-column
   layouts leave one side with (almost) no width and the toolbars run off the
   edge. Every bar above wraps; here the columns give way instead. */
@media (max-width: 900px) {
  /* Prompt editor header: the title takes its own row so the type selector,
     stars and action buttons below it are always reachable. */
  .lib-title-input { flex: 1 1 100%; }

  /* Master list: proportional instead of a fixed 300px, so the editor keeps the
     larger share. "⟨" / Ctrl+B still hides it entirely. */
  .lib-body { grid-template-columns: minmax(140px, 24%) minmax(0, 1fr); }

  /* Image detail: stack the prompt over the image — a fixed 420px image column
     would squeeze the editor to nothing. */
  .detail-body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) 35vh;
  }
  .detail-edit { border-right: none; border-bottom: 1px solid var(--border); }
  /* Image hidden: reclaim the row it left behind (the columns rule above the
     .img-hidden one still applies, it just has one visible child). */
  .detail-view.img-hidden .detail-body { grid-template-rows: minmax(0, 1fr); }
  .detail-title { max-width: 100%; }

  /* Prompt-templates modal: same treatment for its fixed sidebar. */
  .pt-layout { grid-template-columns: minmax(120px, 30%) minmax(0, 1fr); }
}

</style>
</head>
<body>
  <!-- Unsupported-browser banner -->
  <div id="unsupported" class="banner" hidden>
    This app needs the <strong>File System Access API</strong>. Please use a recent
    version of <strong>Chrome, Edge or Opera</strong>, served over <code>localhost</code>.
  </div>

  <!-- Top-level section switch -->
  <header id="appnav">
    <div class="brand">aiprompt-marie-kondo</div>
    <nav class="sections" aria-label="Sections">
      <button class="tab active" data-section="images">Image prompts</button>
      <button class="tab" data-section="library">Prompt library</button>
    </nav>
    <span class="spacer"></span>
    <button id="askAiBtn" class="btn" title="Ask the LLM anything (scratchpad)">💬 Ask AI</button>
    <button id="llmSettingsBtn" class="btn" title="Configure the AI / LLM connection used across the app">
      <span id="llmStatusDot" class="llm-dot llm-dot-off"></span>AI settings
    </button>
  </header>

  <!-- ================= IMAGE PROMPTS SECTION ================= -->
  <div id="imagesSection" class="section">
  <header id="topbar" class="topbar">
    <div class="bar-left">
      <button id="openBtn" class="btn primary">Open folder…</button>
      <button id="reopenBtn" class="btn" hidden></button>
      <span id="folderName" class="folder-name"></span>
    </div>

    <div class="bar-center">
      <input id="search" class="search" type="text"
             placeholder="Search prompts…  (Ctrl+F)" autocomplete="off" spellcheck="false" />
    </div>

    <div class="bar-right">
      <div class="filters" role="group" aria-label="Filter">
        <button class="chip active" data-filter="all">All</button>
        <button class="chip" data-filter="with">With prompt</button>
        <button class="chip" data-filter="without">No prompt</button>
      </div>
      <span id="counts" class="counts"></span>
    </div>
  </header>

  <!-- GRID VIEW -->
  <main id="gridView" class="grid-view">
    <div id="grid" class="grid-scroll">
      <div id="gridInner" class="grid-inner"></div>
    </div>
    <div id="empty" class="empty-state">
      <p>Open a folder of images to begin.</p>
      <p class="muted">Every image is shown with a badge telling you whether it has a prompt.</p>
    </div>
  </main>

  <!-- DETAIL VIEW -->
  <section id="detailView" class="detail-view" hidden>
    <div class="detail-top">
      <button id="backBtn" class="btn">← Back</button>
      <span id="detailTitle" class="detail-title"></span>
      <span id="dirtyFlag" class="dirty-flag" hidden>● Unsaved</span>
      <span class="spacer"></span>
      <button id="toggleImgBtn" class="btn" title="Hide the image for privacy">Hide image ⟩</button>
      <button id="renameBtn" class="btn" title="Rename image (and its .txt)">Rename</button>
      <button id="deleteBtn" class="btn danger" title="Delete image (and its .txt) from disk">Delete</button>
      <button id="prevBtn" class="btn" title="Previous (←)">←</button>
      <button id="nextBtn" class="btn" title="Next (→)">→</button>
      <button id="aiGenBtn" class="btn" title="Generate the prompt from the image with AI" disabled>✨ Generate</button>
      <button id="copyBtn" class="btn" title="Copy prompt (Ctrl+C)">Copy</button>
      <button id="saveBtn" class="btn primary" title="Save (Ctrl+Enter) — stays on this image">Save ⏎</button>
    </div>
    <div class="detail-body">
      <div class="detail-edit">
        <textarea id="promptEditor" class="prompt-editor"
                  placeholder="No prompt yet — write one and press Ctrl+Enter to save."
                  spellcheck="false"></textarea>
        <div id="detailMeta" class="detail-meta"></div>
      </div>
      <div class="detail-image">
        <img id="detailImg" alt="" />
      </div>
    </div>
  </section>
  </div><!-- /#imagesSection -->

  <!-- ================= PROMPT LIBRARY SECTION ================= -->
  <div id="librarySection" class="section" hidden>
    <header id="libToolbar" class="topbar">
      <div class="bar-left">
        <button id="libListToggle" class="btn lib-list-toggle" type="button"
                aria-controls="libList" aria-expanded="true">⟨</button>
        <button id="libOpenBtn" class="btn primary">Open library…</button>
        <button id="libReopenBtn" class="btn" hidden></button>
        <span id="libFolderName" class="folder-name"></span>
      </div>
      <div class="bar-center">
        <input id="libSearch" class="search" type="text"
               placeholder="Search prompts, tags…  (Ctrl+F)" autocomplete="off" spellcheck="false" />
      </div>
      <div class="bar-right">
        <button id="libGuideBtn" class="btn" title="Show the full prompt guide">Prompt guide</button>
        <button id="libHelpBtn" class="btn" title="Show a full example prompt">Prompt example</button>
        <button id="libNewBtn" class="btn" disabled>+ New prompt</button>
        <span id="libCounts" class="counts"></span>
      </div>
    </header>

    <div id="libLayout" class="lib-body">
      <div id="libList" class="lib-list"></div>

      <div class="lib-right">
      <div id="libEditor" class="lib-editor" hidden>
        <div class="lib-edit-top">
          <input id="libTitle" class="lib-title-input" type="text"
                 placeholder="prompt-title" autocomplete="off" spellcheck="false" />
          <select id="libType" class="lib-type-select" title="Prompt type"></select>
          <div id="libRating" class="rating" role="radiogroup" aria-label="Rating (1–5)"></div>
          <span id="libDirty" class="dirty-flag" hidden>● Unsaved</span>
          <span class="spacer"></span>
          <button id="libSaveBtn" class="btn primary" title="Save (Ctrl+S)">Save</button>
          <!-- Everything but Save lives behind "⋮" so the header stays one row. -->
          <div id="libMenu" class="action-menu">
            <button id="libMenuBtn" class="btn action-menu-btn" type="button"
                    title="More actions" aria-haspopup="menu" aria-expanded="false">⋮</button>
            <div class="action-menu-list" role="menu" hidden>
              <button id="libReviewBtn" class="action-menu-item scene-generate" type="button"
                      role="menuitem" disabled hidden>Review with AI</button>
              <button id="libPreviewBtn" class="action-menu-item" type="button" role="menuitem"
                      title="Preview the whole prompt (and copy it)">Preview</button>
              <button id="libDuplicateBtn" class="action-menu-item" type="button" role="menuitem"
                      title="Duplicate this prompt as a new one">Duplicate</button>
              <button id="libDeleteBtn" class="action-menu-item danger" type="button" role="menuitem"
                      title="Delete this prompt file">Delete prompt</button>
            </div>
          </div>
        </div>
        <div id="libScroll" class="lib-scroll">
          <div class="lib-meta">
            <div class="scene-label-row"><label for="libLorasInput">loras</label></div>
            <p class="scene-hint">Comma-separated LoRAs that can help this prompt. Shown here; never copied.</p>
            <input id="libLorasInput" class="lib-loras-input" type="text"
                   placeholder="e.g. add_detail, film_grain, neon_style"
                   autocomplete="off" spellcheck="false" />
          </div>
          <div id="libBody" class="lib-body-edit" hidden>
            <div class="scene-label-row">
              <label for="libBodyText">prompt</label>
              <span class="spacer"></span>
              <button id="libBodyGenBtn" class="btn scene-generate" type="button" disabled>✨ Generate</button>
            </div>
            <p class="scene-hint">The full prompt as plain text.</p>
            <textarea id="libBodyText" class="lib-body-text" spellcheck="false"
                      placeholder="Write the full prompt here…"></textarea>
          </div>
          <div id="libGlobalHead" class="lib-global-head" hidden>
            <span class="lib-global-title">Global</span>
            <button id="libGlobalGenBtn" class="btn scene-generate" type="button" disabled>✨ Generate</button>
          </div>
          <div id="libGlobal" class="lib-global"></div>
          <div id="libScenes" class="lib-scenes"></div>
        </div>
        <div id="libScenesFooter" class="lib-scenes-footer">
          <button id="libAddSceneBtn" class="btn">+ Add scene</button>
          <button id="libCollapseAllBtn" class="btn">Collapse all</button>
          <div id="libEditMeta" class="detail-meta"></div>
        </div>
      </div>

      <div id="libEmpty" class="lib-empty" data-mode="nofolder">
        <p class="lib-empty-nofolder">Open a folder of prompt <code>.txt</code> files to begin.</p>
        <p class="lib-empty-select">Select a prompt on the left, or create a new one.</p>
        <p class="muted">Each file is one reusable prompt; the filename is its title.</p>
      </div>
      </div><!-- /.lib-right -->
    </div>
  </div><!-- /#librarySection -->

  <!-- Image-prompts: generate a prompt from the open image with a vision LLM -->
  <div id="aiGenModal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box modal-box-lg">
      <div class="modal-head">
        <span class="modal-title">Generate prompt with AI</span>
        <span class="spacer"></span>
        <button id="aiGenCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body">
        <div class="ai-gen">
          <div class="ai-gen-cols">
            <div class="ai-gen-image">
              <button id="aiGenToggleImgBtn" class="btn" type="button"
                      title="Show/hide the image preview (it is sent to the AI either way)">Show image</button>
              <img id="aiGenImg" alt="" hidden />
              <div id="aiGenImgName" class="muted"></div>
              <div class="ai-gen-imgnote muted">Hidden by default. Still sent to the AI.</div>
            </div>
            <div class="ai-gen-fields">
              <label class="llm-field">
                <span class="llm-label">Template</span>
                <select id="aiGenTemplate" class="llm-input"></select>
              </label>
              <label class="llm-field">
                <span class="llm-label">System prompt</span>
                <textarea id="aiGenSystem" class="ai-gen-ta" rows="4" spellcheck="false"></textarea>
              </label>
              <label class="llm-field">
                <span class="llm-label">User prompt</span>
                <textarea id="aiGenUser" class="ai-gen-ta" rows="2" spellcheck="false"></textarea>
              </label>
              <div class="ai-gen-actions">
                <button id="aiGenRunBtn" class="btn primary" type="button">Generate</button>
                <button id="aiGenRefineBtn" class="btn" type="button" hidden
                        title="Iterate: sends your user prompt as a follow-up, keeping the conversation and image context">Refine</button>
                <button id="aiGenStopBtn" class="btn" type="button" hidden>Stop</button>
                <span id="aiGenStatus" class="llm-status"></span>
              </div>
            </div>
          </div>

          <label class="llm-field llm-field-grow">
            <span class="llm-label">Result <span class="muted">(editable)</span></span>
            <textarea id="aiGenResult" class="ai-gen-ta ai-gen-result" rows="8"
                      spellcheck="false" placeholder="The generated prompt will appear here…"></textarea>
          </label>
          <div class="ai-gen-actions">
            <span class="spacer"></span>
            <button id="aiGenAcceptBtn" class="btn primary" type="button" disabled>Use as prompt</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Prompt library: generate MiniMax H3 globals (subject_definitions, retention) -->
  <div id="globalsGenModal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box modal-box-lg">
      <div class="modal-head">
        <span class="modal-title">Generate globals with AI</span>
        <span class="spacer"></span>
        <button id="globalsGenCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body">
        <div class="ai-gen">
          <div class="pt-purpose-title">Subject definitions</div>
          <label class="llm-field">
            <span class="llm-label">Template</span>
            <select id="globalsTemplate" class="llm-input"></select>
          </label>
          <p class="scene-hint">
            Pick a label type, a relationship marker and a reference image from disk.
            The model writes the definition line, and a matching retention_analysis
            entry is added for you to complete. Indices auto-increment per type.
          </p>
          <div class="gen-subj-row">
            <select id="globalsLabelType" class="llm-input">
              <option value="Subject">Subject</option>
              <option value="Picture">Picture</option>
            </select>
            <span id="globalsNextLabel" class="gen-next-label"></span>
            <select id="globalsMarker" class="llm-input" title="Retention relationship marker"></select>
            <input id="globalsImageInput" type="file" accept="image/*" class="gen-file" />
            <button id="globalsAddBtn" class="btn primary" type="button" disabled>Add &amp; generate</button>
            <button id="globalsStopBtn" class="btn" type="button" hidden>Stop</button>
            <span id="globalsStatus" class="llm-status"></span>
          </div>

          <div id="globalsList" class="gen-list"></div>
          <p id="globalsListEmpty" class="scene-hint">No references yet — add one above.</p>

          <div class="llm-field">
            <div class="gen-help-row">
              <span class="llm-label">subject_definitions <span class="muted">(editable)</span></span>
              <button id="globalsSubjHelp" class="section-help" type="button"
                      title="About the &quot;subject_definitions&quot; section">?</button>
            </div>
            <textarea id="globalsSubjects" class="ai-gen-ta" rows="7" spellcheck="false"
                      placeholder="Generated <Subject N> / <Picture N> lines appear here…"></textarea>
          </div>

          <div class="pt-purpose-title">Retention analysis</div>
          <p class="scene-hint">Added automatically per reference (no AI). Fill in the shots and replace each <code>TODO</code>.</p>
          <div class="llm-field">
            <div class="gen-help-row">
              <span class="llm-label">retention_analysis <span class="muted">(editable)</span></span>
              <button id="globalsRetHelp" class="section-help" type="button"
                      title="About the &quot;retention_analysis&quot; section">?</button>
            </div>
            <textarea id="globalsRetention" class="ai-gen-ta" rows="6" spellcheck="false"
                      placeholder="&lt;Subject 1&gt; (appears in [Shot 1]): fully_preserved - TODO"></textarea>
          </div>

          <div class="ai-gen-actions">
            <span class="spacer"></span>
            <button id="globalsAcceptBtn" class="btn primary" type="button">Done</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Prompt library: generic text generator (scene, default prompt, …) -->
  <div id="textGenModal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box modal-box-lg">
      <div class="modal-head">
        <span id="textGenTitle" class="modal-title">Generate with AI</span>
        <span class="spacer"></span>
        <button id="textGenCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body">
        <div class="ai-gen">
          <label class="llm-field">
            <span class="llm-label">Template</span>
            <select id="textGenTemplate" class="llm-input"></select>
          </label>
          <label class="llm-field">
            <span class="llm-label">System prompt</span>
            <textarea id="textGenSystem" class="ai-gen-ta" rows="4" spellcheck="false"></textarea>
          </label>
          <label class="llm-field">
            <span class="llm-label">User prompt</span>
            <textarea id="textGenUser" class="ai-gen-ta" rows="3" spellcheck="false"></textarea>
          </label>
          <label id="textGenContWrap" class="llm-checkline" hidden>
            <input id="textGenCont" type="checkbox" />
            <span id="textGenContLabel">Continue from the previous scene</span>
          </label>
          <div id="textGenRefWrap" class="llm-field" hidden>
            <span id="textGenRefLabel" class="llm-label">subject_definitions (applied)</span>
            <textarea id="textGenRef" class="ai-gen-ta" rows="4" readonly
                      title="Sent to the model as context (read-only). Edit it in the Global / override sections."></textarea>
          </div>
          <p id="textGenCtxNote" class="scene-hint"></p>
          <div class="ai-gen-actions">
            <button id="textGenRunBtn" class="btn primary" type="button">Generate</button>
            <button id="textGenRefineBtn" class="btn" type="button" hidden
                    title="Iterate on the result: sends your user prompt as a follow-up, keeping the conversation">Refine</button>
            <button id="textGenStopBtn" class="btn" type="button" hidden>Stop</button>
            <span id="textGenStatus" class="llm-status"></span>
          </div>

          <label class="llm-field llm-field-grow">
            <span class="llm-label">Result <span id="textGenResultHint" class="muted">(editable)</span></span>
            <textarea id="textGenResult" class="ai-gen-ta ai-gen-result" rows="10"
                      spellcheck="false" placeholder="The generated text will appear here…"></textarea>
          </label>
          <div class="ai-gen-actions">
            <span class="spacer"></span>
            <button id="textGenAcceptBtn" class="btn primary" type="button" disabled>Use</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Full-screen prompt-templates manager (sidebar of groups + editor pane) -->
  <div id="promptTemplatesModal" class="modal modal-front" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box modal-box-xl">
      <div class="modal-head">
        <span class="modal-title">Prompt templates</span>
        <span class="spacer"></span>
        <button id="ptSaveBtn" class="btn primary">Save</button>
        <button id="ptCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body pt-body">
        <div class="pt-layout">
          <div id="ptSidebar" class="pt-sidebar"></div>
          <div id="ptPane" class="pt-pane"></div>
        </div>
        <datalist id="ptModelList"></datalist>
      </div>
    </div>
  </div>

  <!-- Ask AI: general-purpose LLM scratchpad -->
  <div id="askModal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box modal-box-lg">
      <div class="modal-head">
        <span class="modal-title">Ask AI</span>
        <span class="spacer"></span>
        <button id="askCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body">
        <div class="ai-gen">
          <label class="llm-field">
            <span class="llm-label">Template <span class="muted">(optional prefill)</span></span>
            <select id="askTemplate" class="llm-input"></select>
          </label>
          <label class="llm-field">
            <span class="llm-label">System prompt</span>
            <textarea id="askSystem" class="ai-gen-ta" rows="3" spellcheck="false"></textarea>
          </label>
          <label class="llm-field">
            <span class="llm-label">User prompt</span>
            <textarea id="askUser" class="ai-gen-ta" rows="4" spellcheck="false"></textarea>
          </label>
          <div class="gen-subj-row">
            <span class="llm-label">Image <span class="muted">(optional)</span></span>
            <input id="askImageInput" type="file" accept="image/*" class="gen-file" />
            <button id="askClearImgBtn" class="btn" type="button" hidden>Clear</button>
            <span class="spacer"></span>
            <button id="askRunBtn" class="btn primary" type="button">Send</button>
            <button id="askRefineBtn" class="btn" type="button" hidden
                    title="Iterate: sends your prompt as a follow-up, keeping the conversation">Refine</button>
            <button id="askStopBtn" class="btn" type="button" hidden>Stop</button>
            <span id="askStatus" class="llm-status"></span>
          </div>
          <label class="llm-field llm-field-grow">
            <span class="llm-label">Response <span class="muted">(editable)</span></span>
            <textarea id="askResult" class="ai-gen-ta ai-gen-result" rows="10"
                      spellcheck="false" placeholder="The response will appear here…"></textarea>
          </label>
          <div class="ai-gen-actions">
            <span class="spacer"></span>
            <button id="askCopyBtn" class="btn" type="button" disabled>Copy</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Prompt library: review the WHOLE MiniMax prompt across all scenes -->
  <div id="reviewModal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box modal-box-lg">
      <div class="modal-head">
        <span id="reviewTitle" class="modal-title">Review prompt with AI</span>
        <span class="spacer"></span>
        <button id="reviewCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body">
        <div class="ai-gen">
          <label class="llm-field">
            <span class="llm-label">Template</span>
            <select id="reviewTemplate" class="llm-input"></select>
          </label>
          <label class="llm-field">
            <span class="llm-label">System prompt</span>
            <textarea id="reviewSystem" class="ai-gen-ta" rows="4" spellcheck="false"></textarea>
          </label>
          <label class="llm-field">
            <span class="llm-label">User prompt</span>
            <textarea id="reviewUser" class="ai-gen-ta" rows="2" spellcheck="false"></textarea>
          </label>
          <label class="llm-field">
            <span id="reviewContextLabel" class="llm-label">Prompt under review <span class="muted">(read-only)</span></span>
            <textarea id="reviewContext" class="ai-gen-ta" rows="6" readonly spellcheck="false"
                      title="The context sent to the model. Edit it in the scene editor."></textarea>
          </label>
          <div class="ai-gen-actions">
            <button id="reviewRunBtn" class="btn primary" type="button">Review</button>
            <button id="reviewRefineBtn" class="btn" type="button" hidden
                    title="Iterate: sends your user prompt as a follow-up, keeping the conversation">Refine</button>
            <button id="reviewStopBtn" class="btn" type="button" hidden>Stop</button>
            <span id="reviewStatus" class="llm-status"></span>
          </div>
          <label class="llm-field llm-field-grow">
            <span class="llm-label">Review <span class="muted">(editable)</span></span>
            <textarea id="reviewResult" class="ai-gen-ta ai-gen-result" rows="10"
                      spellcheck="false" placeholder="The review will appear here…"></textarea>
          </label>
          <div class="ai-gen-actions">
            <button id="reviewApplyBtn" class="btn" type="button" hidden
                    title="Generate a corrected scene that implements the review's suggestions">Apply suggestions</button>
            <span class="spacer"></span>
            <button id="reviewCopyBtn" class="btn" type="button" disabled>Copy</button>
          </div>
          <label id="reviewRewriteWrap" class="llm-field llm-field-grow" hidden>
            <span class="llm-label">Corrected scene <span class="muted">(editable — Accept applies summary + detailed_description)</span></span>
            <textarea id="reviewRewrite" class="ai-gen-ta ai-gen-result" rows="8"
                      spellcheck="false" placeholder="The corrected scene will appear here…"></textarea>
          </label>
          <div id="reviewAcceptRow" class="ai-gen-actions" hidden>
            <span class="spacer"></span>
            <button id="reviewAcceptBtn" class="btn primary" type="button" disabled>Accept into scene</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Global AI/LLM settings dialog (shared by every feature) -->
  <div id="llmModal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box">
      <div class="modal-head">
        <span class="modal-title">AI settings</span>
        <span class="spacer"></span>
        <button id="llmCloseBtn" class="btn">Close</button>
      </div>
      <div class="modal-body">
        <div class="llm-form">
          <p class="scene-hint">
            Connect a local OpenAI-compatible LLM (LM Studio, Ollama, …) to generate,
            complete, and improve prompts across the app. In LM Studio: start the local
            server and enable CORS. Stored locally as settings only — never your content.
          </p>

          <label class="llm-field">
            <span class="llm-label">Base URL</span>
            <input id="llmBaseUrl" type="text" class="llm-input"
                   placeholder="http://localhost:1234/v1" autocomplete="off" spellcheck="false" />
          </label>

          <div class="llm-field">
            <span class="llm-label">Model</span>
            <div class="llm-model-row">
              <input id="llmModel" type="text" class="llm-input" list="llmModelList"
                     placeholder="e.g. llama-3.1-8b-instruct" autocomplete="off" spellcheck="false" />
              <button id="llmRefreshBtn" class="btn" type="button"
                      title="Fetch available models from the server">Refresh</button>
            </div>
            <datalist id="llmModelList"></datalist>
          </div>

          <label class="llm-field">
            <span class="llm-label">API key <span class="muted">(optional — not needed for local)</span></span>
            <input id="llmApiKey" type="password" class="llm-input"
                   placeholder="leave empty for local servers" autocomplete="off" />
          </label>

          <label class="llm-field">
            <span class="llm-label">Temperature</span>
            <input id="llmTemperature" type="number" class="llm-input llm-input-narrow"
                   min="0" max="2" step="0.1" />
          </label>

          <div class="llm-field">
            <label class="llm-checkline">
              <input id="llmIndependent" type="checkbox" />
              <span>Independent context each request</span>
            </label>
            <p class="scene-hint">
              Sends <code>cache_prompt=false</code> so the server doesn't reuse the previous
              request's context — prevents a prior image "leaking" into later text
              generations (LM Studio). Turn off for slightly faster repeated prompts.
            </p>
          </div>

          <div class="llm-actions">
            <button id="llmTestBtn" class="btn" type="button">Test connection</button>
            <span id="llmStatus" class="llm-status"></span>
            <span class="spacer"></span>
            <button id="llmTemplatesBtn" class="btn" type="button"
                    title="Edit the AI prompt templates used across the app">Prompt templates…</button>
            <button id="llmSaveBtn" class="btn primary" type="button">Save</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Generic modal: full example prompt, per-section guidance -->
  <div id="modal" class="modal" hidden>
    <div class="modal-backdrop"></div>
    <div class="modal-box">
      <div class="modal-head">
        <span id="modalTitle" class="modal-title"></span>
        <span class="spacer"></span>
        <button id="modalCopyBtn" class="btn" hidden>Copy</button>
        <button id="modalCloseBtn" class="btn">Close</button>
      </div>
      <div id="modalBody" class="modal-body"></div>
    </div>
  </div>

  <div id="toasts" class="toasts"></div>

  <script>
(function () {
  var __mods = {}, __cache = {};
  function register(id, fn) { __mods[id] = fn; }
  function require(id) {
    if (__cache[id]) return __cache[id].exports;
    var module = { exports: {} };
    __cache[id] = module;
    __mods[id](module, module.exports, require);
    return module.exports;
  }
  register("js/app.js", function (module, exports, require) {
const { FileSystemStorageAdapter } = require("js/storage/fs-adapter.js");
const { idbGet, idbSet } = require("js/storage/idb.js");
const { createGrid } = require("js/grid.js");
const { initModal } = require("js/modal.js");
const { toast } = require("js/toast.js");
const { initLibrary, focusSearch: libraryFocusSearch, isDirty: libraryDirty, onShow: libraryOnShow } = require("js/prompt-library.js");
const { state, applyFilters, effectivePrompt, hasEffectivePrompt, isDirty, withCount } = require("js/store.js");
const { loadLlmConfig, onLlmStatusChange, isLlmOnline } = require("js/llm/llm-config.js");
const { initLlmSettings, openLlmSettings, refreshLlmStatus } = require("js/llm/llm-settings.js");
const { loadLlmTemplates } = require("js/llm/llm-prompts.js");
const { initPromptTemplates } = require("js/llm/llm-prompt-templates.js");
const { initImageAi, openImageAi } = require("js/image-ai.js");
const { initTextAi } = require("js/text-ai.js");
const { initGlobalsAi } = require("js/globals-ai.js");
const { initReviewAi } = require("js/review-ai.js");
const { initAskAi, openAskAi } = require("js/ask-ai.js");

const adapter = new FileSystemStorageAdapter();

// ---- DOM refs ----
const $ = (id) => document.getElementById(id);
const el = {
  unsupported: $("unsupported"),
  openBtn: $("openBtn"),
  reopenBtn: $("reopenBtn"),
  folderName: $("folderName"),
  search: $("search"),
  filters: document.querySelector(".filters"),
  counts: $("counts"),
  gridView: $("gridView"),
  grid: $("grid"),
  gridInner: $("gridInner"),
  empty: $("empty"),
  detailView: $("detailView"),
  detailTitle: $("detailTitle"),
  dirtyFlag: $("dirtyFlag"),
  detailImg: $("detailImg"),
  promptEditor: $("promptEditor"),
  detailMeta: $("detailMeta"),
  backBtn: $("backBtn"),
  toggleImgBtn: $("toggleImgBtn"),
  renameBtn: $("renameBtn"),
  deleteBtn: $("deleteBtn"),
  prevBtn: $("prevBtn"),
  nextBtn: $("nextBtn"),
  aiGenBtn: $("aiGenBtn"),
  copyBtn: $("copyBtn"),
  saveBtn: $("saveBtn"),
  // section switching
  imagesSection: $("imagesSection"),
  librarySection: $("librarySection"),
  tabs: document.querySelector(".sections"),
  llmSettingsBtn: $("llmSettingsBtn"),
  askAiBtn: $("askAiBtn"),
};

// ---- thumbnail URL cache (object URLs are reused across renders) ----
const urlCache = new Map(); // thumbKey -> objectURL
const inflight = new Map(); // thumbKey -> Promise<objectURL>

function loadThumb(entry, priority) {
  const k = entry.thumbKey;
  if (urlCache.has(k)) return Promise.resolve(urlCache.get(k));
  if (inflight.has(k)) return inflight.get(k);
  const p = adapter
    .getThumbnail(entry, { priority })
    .then((blob) => {
      const url = URL.createObjectURL(blob);
      urlCache.set(k, url);
      inflight.delete(k);
      return url;
    })
    .catch((err) => {
      inflight.delete(k);
      throw err;
    });
  inflight.set(k, p);
  return p;
}

// Warm the on-disk cache without holding an object URL (phase-3 backfill, §4).
function warm(entry) {
  if (urlCache.has(entry.thumbKey) || inflight.has(entry.thumbKey)) return;
  adapter.getThumbnail(entry, { priority: false }).catch(() => {});
}

const grid = createGrid(el.grid, el.gridInner, { loadThumb, onOpen: openDetail });

// ---- rendering helpers ----
function updateCounts() {
  if (!state.entries.length) {
    el.counts.textContent = "";
    return;
  }
  el.counts.textContent = `${withCount()} / ${state.entries.length} with prompt`;
}

function renderGrid() {
  el.empty.hidden = state.entries.length > 0;
  updateCounts();
  grid.refresh();
}

// ---------------------------------------------------------------------------
// Detail view (spec §5.3, §9.3, §9.4)
// ---------------------------------------------------------------------------
const originalUrls = new Map(); // name -> objectURL (bounded to prev/cur/next)

async function getOriginalUrl(entry) {
  if (originalUrls.has(entry.name)) return originalUrls.get(entry.name);
  const file = await adapter.getFile(entry);
  const url = URL.createObjectURL(file);
  originalUrls.set(entry.name, url);
  return url;
}

function pruneOriginals(keepNames) {
  for (const [name, url] of originalUrls) {
    if (!keepNames.has(name)) {
      URL.revokeObjectURL(url);
      originalUrls.delete(name);
    }
  }
}

// The image currently open in the detail view. Tracked as an object (not just
// an index) so that saving keeps us on the SAME image even when it leaves the
// active filter (e.g. gaining a prompt under the "No prompt" filter).
let currentImage = null;

function currentEntry() {
  return currentImage;
}

// Position of the open image within the filtered list for prev/next; falls back
// to the last known index if the image has dropped out of the current filter.
function currentIndex() {
  if (!currentImage) return -1;
  const i = state.filtered.indexOf(currentImage);
  return i >= 0 ? i : state.selectedIndex;
}

function updateDirtyUI() {
  const e = currentEntry();
  const dirty = e ? isDirty(e) : false;
  el.dirtyFlag.hidden = !dirty;
  el.saveBtn.disabled = !dirty;
}

/** Enable "Generate with AI" only while online and viewing an image. */
function updateAiBtn() {
  const online = isLlmOnline();
  el.aiGenBtn.disabled = !(online && currentImage && state.view === "detail");
  el.aiGenBtn.title = online
    ? "Generate the prompt from the image with AI"
    : "AI not connected — configure it in AI settings";
}

function updateMeta(entry) {
  const kb = (entry.size / 1024).toFixed(0);
  const when = new Date(entry.mtime).toLocaleString();
  const status = hasEffectivePrompt(entry) ? "has prompt" : "no prompt";
  el.detailMeta.textContent = `${entry.name} · ${entry.ext.toUpperCase()} · ${kb} KB · ${when} · ${status}`;
}

// Privacy toggle (spec §5.3): hide/show the original in the detail view. The
// state persists as you navigate so nothing is revealed by accident. Hiding
// also drops the <img> src so the pixels leave the DOM entirely.
function syncImageVisibility() {
  el.detailView.classList.toggle("img-hidden", state.imageHidden);
  el.toggleImgBtn.textContent = state.imageHidden ? "⟨ Show image" : "Hide image ⟩";
  el.toggleImgBtn.title = state.imageHidden
    ? "Show the image again"
    : "Hide the image for privacy";
}

function showOriginal(entry) {
  getOriginalUrl(entry).then((url) => {
    if (currentEntry() === entry && !state.imageHidden) el.detailImg.src = url;
  });
}

function toggleImageVisibility() {
  state.imageHidden = !state.imageHidden;
  syncImageVisibility();
  const e = currentEntry();
  if (!e) return;
  if (state.imageHidden) el.detailImg.removeAttribute("src");
  else showOriginal(e);
}

async function openDetail(index) {
  if (index < 0 || index >= state.filtered.length) return;
  state.selectedIndex = index;
  state.view = "detail";

  const e = state.filtered[index];
  currentImage = e;
  el.detailView.hidden = false;
  el.detailTitle.textContent = e.name;
  el.promptEditor.value = effectivePrompt(e) ?? "";
  updateDirtyUI();
  updateAiBtn();
  updateMeta(e);
  syncImageVisibility();
  el.prevBtn.disabled = index === 0;
  el.nextBtn.disabled = index === state.filtered.length - 1;

  el.detailImg.removeAttribute("src");
  if (!state.imageHidden) showOriginal(e);

  // Preload neighbors — the dominant flow when captioning sequentially (§5.3).
  const keep = new Set([e.name]);
  for (const j of [index - 1, index + 1]) {
    const n = state.filtered[j];
    if (n) {
      keep.add(n.name);
      getOriginalUrl(n).catch(() => {});
    }
  }
  pruneOriginals(keep);

  el.promptEditor.focus();
}

function backToGrid() {
  state.view = "grid";
  el.detailView.hidden = true;
  updateAiBtn();
  grid.layout();
  grid.scrollToIndex(state.selectedIndex);
}

function nav(delta) {
  const next = currentIndex() + delta;
  if (next >= 0 && next < state.filtered.length) openDetail(next);
}

async function saveCurrent() {
  const e = currentEntry();
  if (!e || !isDirty(e)) return true;
  try {
    await adapter.writePrompt(e, el.promptEditor.value);
    state.dirty.delete(e.baseName);
    applyFilters();
    updateCounts();
    updateDirtyUI();
    return true;
  } catch (err) {
    toast("Could not save: " + err.message, "error");
    return false;
  }
}

// Save and STAY on the current image (no auto-advance). Navigation is manual.
async function saveStay() {
  const e = currentEntry();
  if (!e) return;
  const oldIdx = state.selectedIndex;
  if (!(await saveCurrent())) return;

  // Re-sync navigation position. If the image is still in the filtered view,
  // point at it. If saving dropped it out (e.g. under the "No prompt" filter),
  // keep it displayed and place the cursor so → advances to the image that
  // took its place.
  const idx = state.filtered.indexOf(currentImage);
  state.selectedIndex = idx >= 0 ? idx : Math.max(0, oldIdx) - 1;

  const navIdx = currentIndex();
  el.prevBtn.disabled = navIdx <= 0;
  el.nextBtn.disabled = navIdx >= state.filtered.length - 1;
  updateMeta(e);
  updateDirtyUI();
}

async function copyPrompt() {
  const e = currentEntry();
  if (!e) return;
  try {
    await navigator.clipboard.writeText(effectivePrompt(e) ?? "");
    toast("Prompt copied.");
  } catch {
    toast("Clipboard blocked by the browser.", "warn");
  }
}

el.promptEditor.addEventListener("input", () => {
  const e = currentEntry();
  if (!e) return;
  const cur = el.promptEditor.value;
  const disk = e.prompt ?? "";
  if (cur === disk) state.dirty.delete(e.baseName);
  else state.dirty.set(e.baseName, { text: cur, base: disk });
  updateDirtyUI();
});

async function renameCurrentImage() {
  const e = currentEntry();
  if (!e) return;
  const input = window.prompt("New name (without extension):", e.baseName);
  if (input == null) return; // cancelled
  const newBase = input.trim();
  if (!newBase || newBase === e.baseName) return;

  const oldBase = e.baseName;
  try {
    await adapter.renameImage(e, newBase); // renames image + its .txt together
  } catch (err) {
    toast("Could not rename: " + err.message, "error");
    return;
  }

  // Carry any unsaved prompt edit over to the new base-name key.
  if (state.dirty.has(oldBase)) {
    state.dirty.set(e.baseName, state.dirty.get(oldBase));
    state.dirty.delete(oldBase);
  }

  el.detailTitle.textContent = e.name;
  updateMeta(e);
  applyFilters();
  grid.refresh();
  toast(`Renamed to "${e.name}".`);
}

// Delete the open image and its .txt sidecar, then land on the image that took
// its place (same slot in the filtered list), or fall back to the grid when the
// folder — or the current filter — is left empty.
async function deleteCurrentImage() {
  const e = currentEntry();
  if (!e) return;
  if (
    !window.confirm(
      `Delete "${e.name}"${e.hasPrompt ? " and its .txt prompt" : ""}? This removes it from disk.`
    )
  )
    return;

  try {
    await adapter.deleteImage(e);
  } catch (err) {
    toast("Could not delete: " + err.message, "error");
    return;
  }

  // Drop any in-memory references to the removed image.
  state.dirty.delete(e.baseName);
  const orig = originalUrls.get(e.name);
  if (orig) {
    URL.revokeObjectURL(orig);
    originalUrls.delete(e.name);
  }
  const thumb = urlCache.get(e.thumbKey);
  if (thumb) {
    URL.revokeObjectURL(thumb);
    urlCache.delete(e.thumbKey);
  }

  const slot = currentIndex();
  const gi = state.entries.indexOf(e);
  if (gi >= 0) state.entries.splice(gi, 1);
  applyFilters();
  updateCounts();
  toast(`Deleted "${e.name}".`);

  if (state.filtered.length === 0) {
    currentImage = null;
    state.selectedIndex = -1;
    backToGrid();
    renderGrid();
    return;
  }
  openDetail(Math.min(Math.max(slot, 0), state.filtered.length - 1));
}

el.backBtn.addEventListener("click", backToGrid);
el.toggleImgBtn.addEventListener("click", toggleImageVisibility);
el.renameBtn.addEventListener("click", renameCurrentImage);
el.deleteBtn.addEventListener("click", deleteCurrentImage);
el.prevBtn.addEventListener("click", () => nav(-1));
el.nextBtn.addEventListener("click", () => nav(1));
el.aiGenBtn.addEventListener("click", () => openImageAi(currentEntry()));
el.copyBtn.addEventListener("click", copyPrompt);
el.saveBtn.addEventListener("click", saveStay);

// ---------------------------------------------------------------------------
// Filters & search (spec §9.2)
// ---------------------------------------------------------------------------
el.filters.addEventListener("click", (ev) => {
  const btn = ev.target.closest(".chip");
  if (!btn) return;
  state.filterMode = btn.dataset.filter;
  for (const c of el.filters.querySelectorAll(".chip"))
    c.classList.toggle("active", c === btn);
  applyFilters();
  renderGrid();
});

el.search.addEventListener("input", () => {
  state.search = el.search.value;
  applyFilters();
  renderGrid();
});

// ---------------------------------------------------------------------------
// Section navigation (Image prompts / Prompt library)
// ---------------------------------------------------------------------------
function switchSection(name) {
  if (name === state.section) return;
  state.section = name;
  el.imagesSection.hidden = name !== "images";
  el.librarySection.hidden = name !== "library";
  for (const t of el.tabs.querySelectorAll(".tab"))
    t.classList.toggle("active", t.dataset.section === name);
  // Sections were hidden (0-size) while away — re-measure on return.
  if (name === "images") grid.layout();
  else if (name === "library") libraryOnShow();
}

el.tabs.addEventListener("click", (ev) => {
  const btn = ev.target.closest(".tab");
  if (btn) switchSection(btn.dataset.section);
});

// ---------------------------------------------------------------------------
// Keyboard shortcuts (spec §9.4)
// ---------------------------------------------------------------------------
document.addEventListener("keydown", (ev) => {
  const mod = ev.ctrlKey || ev.metaKey;

  // Ctrl+F focuses the active section's search.
  if (mod && (ev.key === "f" || ev.key === "F")) {
    ev.preventDefault();
    if (state.section === "library") {
      libraryFocusSearch();
    } else {
      el.search.focus();
      el.search.select();
    }
    return;
  }

  // The remaining shortcuts belong to the image detail view only.
  if (state.section !== "images") return;
  if (state.view !== "detail") return;

  if (mod && ev.key === "Enter") {
    ev.preventDefault();
    saveStay();
    return;
  }
  if (ev.key === "Escape") {
    ev.preventDefault();
    backToGrid();
    return;
  }

  const editing = document.activeElement === el.promptEditor;
  if (!editing) {
    if (ev.key === "ArrowLeft") {
      ev.preventDefault();
      nav(-1);
    } else if (ev.key === "ArrowRight") {
      ev.preventDefault();
      nav(1);
    } else if (mod && (ev.key === "c" || ev.key === "C")) {
      ev.preventDefault();
      copyPrompt();
    }
  }
});

// Warn before leaving the page with unsaved edits (spec §7.1).
window.addEventListener("beforeunload", (ev) => {
  if (state.dirty.size || libraryDirty()) {
    ev.preventDefault(); // modern Chromium shows the leave-site prompt
  }
});

// ---------------------------------------------------------------------------
// Opening folders
// ---------------------------------------------------------------------------
async function openFolder(handle) {
  const entries = await adapter.listFolder(handle);
  state.entries = entries;
  state.filtered = [];
  state.folderName = handle.name;
  state.writable = adapter.writable;
  state.dirty.clear();
  state.view = "grid";
  el.detailView.hidden = true;

  el.folderName.textContent = handle.name;
  applyFilters();
  renderGrid();

  await idbSet("lastFolder", handle).catch(() => {});

  if (!adapter.writable) {
    toast(
      "Folder is read-only — thumbnails won't be cached to disk and prompts can't be saved.",
      "warn"
    );
  } else {
    startBackfill();
  }
}

el.openBtn.addEventListener("click", async () => {
  try {
    const handle = await window.showDirectoryPicker({ mode: "readwrite" });
    el.reopenBtn.hidden = true;
    await openFolder(handle);
  } catch (err) {
    if (err && err.name !== "AbortError")
      toast("Could not open folder: " + err.message, "error");
  }
});

// Phase-3 backfill: generate the thumbnails we haven't shown yet, at idle.
function startBackfill() {
  const list = state.entries.slice();
  let i = 0;
  const step = (deadline) => {
    while (
      i < list.length &&
      (!deadline || deadline.timeRemaining() > 3)
    ) {
      warm(list[i++]);
    }
    if (i < list.length) requestIdleCallback(step);
  };
  if ("requestIdleCallback" in window) requestIdleCallback(step);
  else setTimeout(() => step(null), 500);
}

// ---------------------------------------------------------------------------
// External-change detection: re-scan on focus (spec §7.2)
// ---------------------------------------------------------------------------
let scanning = false;
async function rescan() {
  if (!adapter.dirHandle || scanning) return;
  scanning = true;
  try {
    const openEntry = state.view === "detail" ? currentEntry() : null;
    const openName = openEntry ? openEntry.name : null;

    const fresh = await adapter.listFolder(adapter.dirHandle);

    for (const e of fresh) {
      const d = state.dirty.get(e.baseName);
      if (!d) continue;
      const disk = e.prompt ?? "";
      if (disk !== (d.base ?? "")) {
        // Disk changed underneath an unsaved edit -> conflict (§7.2).
        if (e.name === openName) showConflict(e, d, disk);
        else
          toast(
            `"${e.name}" changed on disk — kept your unsaved edit.`,
            "warn"
          );
      }
    }

    state.entries = fresh;
    applyFilters();

    if (openName) {
      const idx = state.filtered.findIndex((x) => x.name === openName);
      if (idx >= 0) {
        state.selectedIndex = idx;
        const e = state.filtered[idx];
        currentImage = e; // re-list produced fresh objects; drop the stale ref
        if (!isDirty(e)) el.promptEditor.value = e.prompt ?? "";
        updateMeta(e);
        updateDirtyUI();
      }
    }

    updateCounts();
    if (state.view === "grid") grid.refresh();
  } catch {
    /* transient errors on focus are non-fatal */
  } finally {
    scanning = false;
  }
}

function showConflict(entry, dirtyRec, diskPrompt) {
  toast(
    `"${entry.name}" was changed on disk while you have unsaved edits.`,
    "warn",
    [
      {
        label: "Keep mine",
        onClick: () => {
          // Rebase so we don't warn again for the same disk state.
          dirtyRec.base = diskPrompt;
          state.dirty.set(entry.baseName, dirtyRec);
        },
      },
      {
        label: "Load disk",
        onClick: () => {
          state.dirty.delete(entry.baseName);
          if (currentEntry() && currentEntry().name === entry.name) {
            el.promptEditor.value = diskPrompt;
          }
          entry.prompt = diskPrompt;
          applyFilters();
          updateDirtyUI();
          updateCounts();
        },
      },
    ]
  );
}

window.addEventListener("focus", rescan);

// ---------------------------------------------------------------------------
// Startup
// ---------------------------------------------------------------------------
async function init() {
  if (!("showDirectoryPicker" in window)) {
    el.unsupported.hidden = false;
    el.openBtn.disabled = true;
    return;
  }

  initModal();
  initLibrary();

  // Global AI/LLM connection: load saved settings and wire the settings dialog.
  // initPromptTemplates() first so its keydown handler is registered after the
  // settings one — the settings dialog then defers Escape while it is open.
  initLlmSettings();
  initPromptTemplates();
  el.llmSettingsBtn.addEventListener("click", openLlmSettings);
  // Image "Generate with AI": applies the accepted prompt through the normal
  // editor flow (marks dirty, user reviews, then saves).
  initImageAi({
    getFile: (entry) => adapter.getFile(entry),
    applyPrompt: (text) => {
      el.promptEditor.value = text;
      el.promptEditor.dispatchEvent(new Event("input", { bubbles: true }));
      el.promptEditor.focus();
    },
  });
  // Text generator used by the Prompt Library Generate features (scene, default).
  initTextAi();
  initGlobalsAi();
  // Whole-prompt reviewer for the Prompt Library (MiniMax); advisory report only.
  initReviewAi();
  // General-purpose LLM scratchpad, opened from the nav.
  initAskAi();
  el.askAiBtn.addEventListener("click", openAskAi);
  // Keep the "Generate" button in sync with connectivity as probes complete.
  onLlmStatusChange(updateAiBtn);
  await Promise.all([loadLlmConfig(), loadLlmTemplates()]);
  refreshLlmStatus(); // fire-and-forget: set the nav indicator without blocking startup

  const handle = await idbGet("lastFolder").catch(() => null);
  if (handle) {
    el.reopenBtn.hidden = false;
    el.reopenBtn.textContent = `Reopen "${handle.name}"`;
    el.reopenBtn.addEventListener("click", async () => {
      let perm = await handle.queryPermission({ mode: "readwrite" });
      if (perm !== "granted")
        perm = await handle.requestPermission({ mode: "readwrite" });
      if (perm === "granted") {
        el.reopenBtn.hidden = true;
        await openFolder(handle);
      } else {
        toast("Permission denied.", "error");
      }
    });
  }
}

init();


  });
  register("js/storage/fs-adapter.js", function (module, exports, require) {
const { hashKey } = require("js/hash.js");
const { createThumbnailWorker } = require("js/workers/worker-factory.js");

/**
 * The ONLY module that talks to the File System Access API (spec §6).
 * No UI component may call the file API directly; everything goes through here.
 * Swapping this out for a Tauri implementation is the entire migration path.
 *
 * @typedef {Object} ImageEntry
 * @property {string}  name         "img_001.png"
 * @property {string}  baseName     "img_001"
 * @property {string}  ext          "png"
 * @property {number}  mtime        image file lastModified (drives thumbKey)
 * @property {number}  size         image file size in bytes
 * @property {number}  promptMtime  prompt .txt lastModified, or 0
 * @property {boolean} hasPrompt
 * @property {string|null} prompt   prompt text loaded during scan, or null
 * @property {string}  thumbKey     hash(name + mtime)
 * @property {FileSystemFileHandle}      _imageHandle   (adapter-internal)
 * @property {FileSystemFileHandle|null} _promptHandle  (adapter-internal)
 */

const THUMB_DIR = ".thumbnails";
const IMAGE_EXTS = new Set(["png", "jpg", "jpeg", "webp"]);

function extOf(name) {
  const dot = name.lastIndexOf(".");
  return dot < 0 ? "" : name.slice(dot + 1).toLowerCase();
}

/** Make a filesystem-safe base name (no separators, no leading dots). */
function sanitizeBaseName(name) {
  return name
    .replace(/[\/\\:*?"<>|\x00-\x1F]/g, "")
    .replace(/^\.+/, "")
    .replace(/\s+/g, " ")
    .trim();
}

/** Fixed-size pool of thumbnail workers with a small priority queue. */
class WorkerPool {
  constructor(createWorker, size) {
    this.idle = [];
    this.queue = [];
    this.pending = new Map();
    this.seq = 0;
    for (let i = 0; i < size; i++) {
      const w = createWorker();
      w.onmessage = (e) => this.#onDone(w, e.data);
      this.idle.push(w);
    }
  }

  /** @param {Blob} blob @param {boolean} priority */
  resize(blob, priority = false) {
    return new Promise((resolve, reject) => {
      const id = ++this.seq;
      this.pending.set(id, { resolve, reject });
      const job = { id, blob };
      if (priority) this.queue.unshift(job);
      else this.queue.push(job);
      this.#dispatch();
    });
  }

  #dispatch() {
    while (this.idle.length && this.queue.length) {
      const w = this.idle.pop();
      const job = this.queue.shift();
      w.postMessage({ id: job.id, blob: job.blob });
    }
  }

  #onDone(w, data) {
    const p = this.pending.get(data.id);
    this.pending.delete(data.id);
    this.idle.push(w);
    if (p) {
      if (data.error) p.reject(new Error(data.error));
      else p.resolve(data.blob);
    }
    this.#dispatch();
  }
}

class FileSystemStorageAdapter {
  constructor() {
    /** @type {FileSystemDirectoryHandle|null} */
    this.dirHandle = null;
    /** @type {FileSystemDirectoryHandle|null} */
    this.thumbDirHandle = null;
    /** True when we can create/write files in the folder. */
    this.writable = true;
    this.pool = new WorkerPool(createThumbnailWorker, 4);
  }

  /**
   * Phase-1 scan (spec §4.1). Non-recursive; skips service directories.
   * Reads each prompt sidecar so the app is immediately usable.
   * @param {FileSystemDirectoryHandle} handle
   * @returns {Promise<ImageEntry[]>}
   */
  async listFolder(handle) {
    this.dirHandle = handle;
    await this.#ensureThumbDir();

    /** @type {Map<string, FileSystemFileHandle>} */
    const files = new Map();
    for await (const [name, h] of handle.entries()) {
      // Skip everything that is not a file. This excludes .thumbnails/ and any
      // other subdirectory — we never walk recursively (spec §8).
      if (h.kind !== "file") continue;
      if (name === THUMB_DIR) continue;
      files.set(name, h);
    }

    const entries = [];
    for (const [name, h] of files) {
      const ext = extOf(name);
      if (!IMAGE_EXTS.has(ext)) continue;

      const base = name.slice(0, name.length - ext.length - 1);
      const file = await h.getFile();
      const promptHandle = files.get(base + ".txt") || null;

      let prompt = null;
      let promptMtime = 0;
      if (promptHandle) {
        const pf = await promptHandle.getFile();
        prompt = await pf.text();
        promptMtime = pf.lastModified;
      }

      entries.push({
        name,
        baseName: base,
        ext,
        mtime: file.lastModified,
        size: file.size,
        promptMtime,
        hasPrompt: promptHandle != null,
        prompt,
        thumbKey: hashKey(name + ":" + file.lastModified),
        _imageHandle: h,
        _promptHandle: promptHandle,
      });
    }

    entries.sort((a, b) =>
      a.name.localeCompare(b.name, undefined, { numeric: true })
    );
    return entries;
  }

  /**
   * Re-read a prompt from disk (used for external-change detection, §7.2).
   * @param {ImageEntry} entry
   * @returns {Promise<string|null>}
   */
  async readPrompt(entry) {
    if (!entry._promptHandle) return null;
    const f = await entry._promptHandle.getFile();
    return f.text();
  }

  /**
   * Persist a prompt, replacing the whole file. Atomic via createWritable
   * (spec §7.1): the API writes a temp .crswap and swaps on close(). On error
   * we abort() so the original is never touched.
   * @param {ImageEntry} entry
   * @param {string} text
   */
  async writePrompt(entry, text) {
    if (!entry._promptHandle) {
      entry._promptHandle = await this.dirHandle.getFileHandle(
        entry.baseName + ".txt",
        { create: true }
      );
    }
    const writable = await entry._promptHandle.createWritable();
    try {
      await writable.write(text);
      await writable.close(); // atomic commit
    } catch (err) {
      await writable.abort(); // discard without touching the original
      throw err;
    }

    const pf = await entry._promptHandle.getFile();
    entry.prompt = text;
    entry.hasPrompt = true;
    entry.promptMtime = pf.lastModified;
  }

  /**
   * Return a thumbnail Blob. Serves from the on-disk .webp cache when present,
   * otherwise generates in a worker and (if writable) caches to disk.
   * @param {ImageEntry} entry
   * @param {{priority?: boolean}} [opts]
   * @returns {Promise<Blob>}
   */
  async getThumbnail(entry, { priority = false } = {}) {
    const cacheName = entry.thumbKey + ".webp";

    if (this.thumbDirHandle) {
      try {
        const h = await this.thumbDirHandle.getFileHandle(cacheName);
        const f = await h.getFile();
        if (f.size > 0) return f;
      } catch {
        /* not cached yet — fall through to generation */
      }
    }

    const original = await entry._imageHandle.getFile();
    const blob = await this.pool.resize(original, priority);

    if (this.thumbDirHandle && this.writable) {
      try {
        const h = await this.thumbDirHandle.getFileHandle(cacheName, {
          create: true,
        });
        const w = await h.createWritable();
        await w.write(blob);
        await w.close();
      } catch {
        /* caching is best-effort; ignore write failures */
      }
    }
    return blob;
  }

  /**
   * The full-resolution original, for the detail view (spec §5.3).
   * @param {ImageEntry} entry
   * @returns {Promise<File>}
   */
  async getFile(entry) {
    return entry._imageHandle.getFile();
  }

  /**
   * Rename an image and its prompt sidecar together. Both `<base>.<ext>` and
   * `<base>.txt` (if present) are renamed to the new base name. Uses the native
   * `FileSystemFileHandle.move()` when available, else copy-then-delete.
   * @param {ImageEntry} entry  mutated in place on success
   * @param {string} newBaseName
   */
  async renameImage(entry, newBaseName) {
    const clean = sanitizeBaseName(newBaseName);
    if (!clean) throw new Error("Name cannot be empty.");
    if (clean === entry.baseName) return entry;

    const newImageName = clean + "." + entry.ext;
    const newPromptName = clean + ".txt";

    // Fail before touching anything if either target is taken.
    await this.#assertFree(newImageName);
    if (entry._promptHandle) await this.#assertFree(newPromptName);

    entry._imageHandle = await this.#moveFile(
      entry._imageHandle,
      entry.name,
      newImageName
    );
    if (entry._promptHandle) {
      entry._promptHandle = await this.#moveFile(
        entry._promptHandle,
        entry.baseName + ".txt",
        newPromptName
      );
    }

    const f = await entry._imageHandle.getFile();
    entry.name = newImageName;
    entry.baseName = clean;
    entry.mtime = f.lastModified;
    entry.size = f.size;
    entry.thumbKey = hashKey(newImageName + ":" + f.lastModified);
    if (entry._promptHandle) {
      entry.promptMtime = (await entry._promptHandle.getFile()).lastModified;
    }
    return entry;
  }

  /**
   * Delete an image and its prompt sidecar together. Removes `<base>.<ext>` and
   * `<base>.txt` (if present) from disk, plus the disposable thumbnail cache
   * entry (best-effort — the cache is never counted and safe to leave behind).
   * The image file is removed last so a failure never leaves an orphaned prompt.
   * @param {ImageEntry} entry
   */
  async deleteImage(entry) {
    if (entry._promptHandle) {
      await this.dirHandle.removeEntry(entry.baseName + ".txt");
      entry._promptHandle = null;
    }
    await this.dirHandle.removeEntry(entry.name);
    entry._imageHandle = null;

    if (this.thumbDirHandle) {
      try {
        await this.thumbDirHandle.removeEntry(entry.thumbKey + ".webp");
      } catch {
        /* thumbnail may not have been generated — nothing to clean up */
      }
    }
  }

  async #assertFree(name) {
    let exists = false;
    try {
      await this.dirHandle.getFileHandle(name);
      exists = true;
    } catch (e) {
      if (e && e.name !== "NotFoundError") throw e;
    }
    if (exists) throw new Error(`"${name}" already exists in this folder.`);
  }

  async #moveFile(handle, oldName, newName) {
    if (typeof handle.move === "function") {
      await handle.move(newName); // handle now references newName
      return handle;
    }
    // Fallback: copy contents to a new file, then remove the old one.
    const file = await handle.getFile();
    const nh = await this.dirHandle.getFileHandle(newName, { create: true });
    const w = await nh.createWritable();
    try {
      await w.write(file);
      await w.close();
    } catch (e) {
      await w.abort();
      throw e;
    }
    await this.dirHandle.removeEntry(oldName);
    return nh;
  }

  /**
   * Ensure .thumbnails/ exists with its self-excluding .gitignore (spec §8).
   * Failure here means the folder is read-only -> degraded mode (spec §7.4).
   */
  async #ensureThumbDir() {
    try {
      this.thumbDirHandle = await this.dirHandle.getDirectoryHandle(THUMB_DIR, {
        create: true,
      });
      try {
        await this.thumbDirHandle.getFileHandle(".gitignore");
      } catch {
        const gi = await this.thumbDirHandle.getFileHandle(".gitignore", {
          create: true,
        });
        const w = await gi.createWritable();
        await w.write("*");
        await w.close();
      }
      this.writable = true;
    } catch {
      this.thumbDirHandle = null;
      this.writable = false;
    }
  }
}

exports.FileSystemStorageAdapter = FileSystemStorageAdapter;
  });
  register("js/hash.js", function (module, exports, require) {
/**
 * Cheap, synchronous, non-cryptographic string hash for thumbnail cache keys.
 * Two independent rolling hashes combined -> 16 hex chars. Collisions are
 * irrelevant here because the key already encodes name + mtime; a collision
 * would at worst reuse a stale thumbnail for an unrelated file with the exact
 * same (name, mtime), which cannot happen within one folder.
 *
 * @param {string} str
 * @returns {string} 16-char lowercase hex
 */
function hashKey(str) {
  let h1 = 0x811c9dc5;
  let h2 = 0x1000193;
  for (let i = 0; i < str.length; i++) {
    const c = str.charCodeAt(i);
    h1 = Math.imul(h1 ^ c, 0x01000193);
    h2 = Math.imul(h2 + c, 0x01000193) ^ (h2 >>> 7);
  }
  const hex = (n) => (n >>> 0).toString(16).padStart(8, "0");
  return hex(h1) + hex(h2);
}

exports.hashKey = hashKey;
  });
  register("js/workers/worker-factory.js", function (module, exports, require) {
function createThumbnailWorker() {
  var src = "/**\n * Thumbnail worker (spec §5.2).\n *\n * Receives an original image Blob, produces a 256px-wide WebP thumbnail.\n * Runs entirely off the main thread and never touches the DOM.\n */\n\nself.onmessage = async (e) => {\n  const { id, blob } = e.data;\n  try {\n    // resizeWidth scales width to 256 with proportional height. High quality\n    // downscale happens inside the decoder, so we never hold the full-size\n    // bitmap in JS memory.\n    const bitmap = await createImageBitmap(blob, {\n      resizeWidth: 256,\n      resizeQuality: \"high\",\n    });\n\n    const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);\n    const ctx = canvas.getContext(\"2d\");\n    ctx.drawImage(bitmap, 0, 0);\n    bitmap.close();\n\n    const out = await canvas.convertToBlob({ type: \"image/webp\", quality: 0.8 });\n    self.postMessage({ id, blob: out });\n  } catch (err) {\n    self.postMessage({ id, error: String((err && err.message) || err) });\n  }\n};\n";
  var url = URL.createObjectURL(new Blob([src], { type: "text/javascript" }));
  return new Worker(url);
}
exports.createThumbnailWorker = createThumbnailWorker;

  });
  register("js/storage/idb.js", function (module, exports, require) {
/**
 * Tiny IndexedDB key/value store.
 *
 * Per the spec (§7.3) IndexedDB is used ONLY for the directory-handle so the
 * folder can be re-authorized with a single click on the next visit. It is
 * NEVER used for prompts or image content — disk is the single source of truth.
 */

const DB_NAME = "image-prompt-manager";
const STORE = "kv";
const VERSION = 1;

function openDB() {
  return new Promise((resolve, reject) => {
    const req = indexedDB.open(DB_NAME, VERSION);
    req.onupgradeneeded = () => {
      req.result.createObjectStore(STORE);
    };
    req.onsuccess = () => resolve(req.result);
    req.onerror = () => reject(req.error);
  });
}

async function idbSet(key, value) {
  const db = await openDB();
  try {
    await new Promise((resolve, reject) => {
      const tx = db.transaction(STORE, "readwrite");
      tx.objectStore(STORE).put(value, key);
      tx.oncomplete = () => resolve();
      tx.onerror = () => reject(tx.error);
    });
  } finally {
    db.close();
  }
}

async function idbGet(key) {
  const db = await openDB();
  try {
    return await new Promise((resolve, reject) => {
      const tx = db.transaction(STORE, "readonly");
      const req = tx.objectStore(STORE).get(key);
      req.onsuccess = () => resolve(req.result);
      req.onerror = () => reject(req.error);
    });
  } finally {
    db.close();
  }
}

exports.idbSet = idbSet;
exports.idbGet = idbGet;
  });
  register("js/grid.js", function (module, exports, require) {
const { state, hasEffectivePrompt, isDirty } = require("js/store.js");

/**
 * Virtualized thumbnail grid (spec §5.2, §9.1). Only visible rows plus a small
 * overscan margin are kept in the DOM. Visible cells request their thumbnail at
 * high priority, which naturally realizes the "viewport-first" queue of §4.
 *
 * @param {HTMLElement} scrollEl  the scroll container (#grid)
 * @param {HTMLElement} innerEl   the sized inner spacer (#gridInner)
 * @param {{
 *   loadThumb: (entry: any, priority: boolean) => Promise<string>,
 *   onOpen: (index: number) => void
 * }} deps
 */
function createGrid(scrollEl, innerEl, { loadThumb, onOpen }) {
  const GAP = 12;
  const MIN_CELL = 180;
  const OVERSCAN_ROWS = 2;

  let cols = 1;
  let cellW = MIN_CELL;
  let rowH = MIN_CELL + GAP;

  /** @type {Map<number, HTMLElement>} index -> cell element */
  const cellPool = new Map();

  function measure() {
    const width = scrollEl.clientWidth - 2 * GAP; // padding on both sides
    cols = Math.max(1, Math.floor((width + GAP) / (MIN_CELL + GAP)));
    cellW = Math.floor((width - GAP * (cols - 1)) / cols);
    rowH = cellW + GAP;
  }

  function makeCell() {
    const el = document.createElement("div");
    el.className = "cell";
    const img = document.createElement("img");
    img.className = "thumb";
    img.decoding = "async";
    const badge = document.createElement("span");
    badge.className = "badge";
    el.append(img, badge);
    el._img = img;
    el._badge = badge;
    el._thumbKey = null;
    el.addEventListener("click", () => {
      const idx = Number(el.dataset.index);
      if (!Number.isNaN(idx)) onOpen(idx);
    });
    return el;
  }

  function positionCell(el, row, col) {
    el.style.width = cellW + "px";
    el.style.height = cellW + "px";
    el.style.transform = `translate(${col * (cellW + GAP)}px, ${row * rowH}px)`;
  }

  function fillCell(el, index) {
    const e = state.filtered[index];
    el.dataset.index = index;
    el.title = e.name;

    const has = hasEffectivePrompt(e);
    el._badge.className = "badge " + (has ? "has" : "no");
    el._badge.textContent = has ? "✓" : "∅";
    el.classList.toggle("dirty", isDirty(e));

    if (el._thumbKey !== e.thumbKey) {
      el._thumbKey = e.thumbKey;
      el._img.classList.remove("loaded");
      el._img.removeAttribute("src");
      loadThumb(e, true)
        .then((url) => {
          if (el._thumbKey === e.thumbKey) {
            el._img.src = url;
            el._img.classList.add("loaded");
          }
        })
        .catch(() => {});
    }
  }

  function renderVisible() {
    const n = state.filtered.length;
    const scrollTop = scrollEl.scrollTop;
    const viewH = scrollEl.clientHeight;

    const firstRow = Math.max(0, Math.floor(scrollTop / rowH) - OVERSCAN_ROWS);
    const lastRow = Math.min(
      Math.ceil((scrollTop + viewH) / rowH) + OVERSCAN_ROWS,
      Math.ceil(n / cols)
    );

    const needed = new Set();
    for (let r = firstRow; r < lastRow; r++) {
      for (let c = 0; c < cols; c++) {
        const i = r * cols + c;
        if (i >= n) break;
        needed.add(i);
        let el = cellPool.get(i);
        if (!el) {
          el = makeCell();
          cellPool.set(i, el);
          innerEl.appendChild(el);
        }
        positionCell(el, r, c);
        fillCell(el, i);
      }
    }

    for (const [i, el] of cellPool) {
      if (!needed.has(i)) {
        el.remove();
        cellPool.delete(i);
      }
    }
  }

  function clearPool() {
    for (const el of cellPool.values()) el.remove();
    cellPool.clear();
  }

  function layout() {
    if (scrollEl.clientWidth === 0) return; // hidden (detail view open)
    const prevCols = cols;
    measure();
    if (cols !== prevCols) clearPool(); // column count changed -> re-tile
    const rows = Math.ceil(state.filtered.length / cols);
    innerEl.style.height = rows * rowH + "px";
    renderVisible();
  }

  /** Full re-tile: call after filter/search/save changes the entry set. */
  function refresh() {
    clearPool();
    layout();
  }

  function scrollToIndex(index) {
    if (index < 0) return;
    measure();
    const row = Math.floor(index / cols);
    scrollEl.scrollTop = row * rowH;
  }

  // --- events ---
  let rafPending = false;
  scrollEl.addEventListener("scroll", () => {
    if (rafPending) return;
    rafPending = true;
    requestAnimationFrame(() => {
      rafPending = false;
      renderVisible();
    });
  });

  const ro = new ResizeObserver(() => layout());
  ro.observe(scrollEl);

  return { refresh, layout, scrollToIndex };
}

exports.createGrid = createGrid;
  });
  register("js/store.js", function (module, exports, require) {
/**
 * In-memory application state (spec §2: runtime state is an in-memory array,
 * loaded on folder open and discarded on close). Disk stays the source of truth.
 */

const { SECTION_ORDER, GLOBAL_SECTIONS } = require("js/scenes.js");

const state = {
  /** Active top-level section: "images" | "library". */
  section: "images",

  // ----- Image-prompts section -----
  /** @type {import("./storage/fs-adapter.js").ImageEntry[]} */
  entries: [],
  /** Current view after filter + search. */
  filtered: [],
  /** "all" | "with" | "without" */
  filterMode: "all",
  search: "",
  /** Index into `filtered` for the open detail image, or -1. */
  selectedIndex: -1,
  /** "grid" | "detail" */
  view: "grid",
  /**
   * Unsaved edits, keyed by baseName.
   * @type {Map<string, { text: string, base: string }>}
   *   text = current editor value, base = disk value at edit start (conflict ref)
   */
  dirty: new Map(),
  folderName: "",
  writable: true,
  /** Privacy toggle: hide the original image in the detail view. */
  imageHidden: false,

  // ----- Prompt-library section -----
  library: {
    /** @type {import("./storage/prompt-library-adapter.js").PromptEntry[]} */
    entries: [],
    filtered: [],
    search: "",
    /** name of the selected prompt, or null */
    selectedName: null,
    /** true when the editor has unsaved changes */
    dirty: false,
    folderName: "",
    /** true when the master list is collapsed so the editor gets full width */
    listCollapsed: false,
  },
};

/** Prompt as the user currently sees it: pending edit if any, else disk value. */
function effectivePrompt(entry) {
  const d = state.dirty.get(entry.baseName);
  return d ? d.text : entry.prompt;
}

function isDirty(entry) {
  return state.dirty.has(entry.baseName);
}

/** Whether an image counts as "having a prompt" (non-empty effective text). */
function hasEffectivePrompt(entry) {
  const p = effectivePrompt(entry);
  return !!(p && p.trim().length);
}

function withCount() {
  let n = 0;
  for (const e of state.entries) if (hasEffectivePrompt(e)) n++;
  return n;
}

/** Recompute `state.filtered` from the current filter mode + search text. */
function applyFilters() {
  const q = state.search.trim().toLowerCase();
  state.filtered = state.entries.filter((e) => {
    const has = hasEffectivePrompt(e);
    if (state.filterMode === "with" && !has) return false;
    if (state.filterMode === "without" && has) return false;
    if (q) {
      const p = (effectivePrompt(e) || "").toLowerCase();
      if (!p.includes(q)) return false;
    }
    return true;
  });
}

/** Recompute `state.library.filtered` from the library search text. */
function applyLibraryFilters() {
  const lib = state.library;
  const q = lib.search.trim().toLowerCase();
  lib.filtered = lib.entries.filter((e) => {
    if (!q) return true;
    if (e.title.toLowerCase().includes(q)) return true;
    if ((e.body || "").toLowerCase().includes(q)) return true; // default-type prompts
    if (GLOBAL_SECTIONS.some((k) => (e.globals?.[k] || "").toLowerCase().includes(q)))
      return true;
    return (e.scenes || []).some((sc) =>
      SECTION_ORDER.some((k) => (sc[k] || "").toLowerCase().includes(q))
    );
  });
}

exports.state = state;
exports.effectivePrompt = effectivePrompt;
exports.isDirty = isDirty;
exports.hasEffectivePrompt = hasEffectivePrompt;
exports.withCount = withCount;
exports.applyFilters = applyFilters;
exports.applyLibraryFilters = applyLibraryFilters;
  });
  register("js/scenes.js", function (module, exports, require) {
/**
 * Prompt-file format + the MiniMax H3 Ref2VA scene/section model.
 *
 * A .txt prompt file is: an optional META block (`type`, `rating`, `loras`),
 * then a BODY. The `type` metadata (see prompt-types.js) selects the body shape:
 *
 *  - "default" (or absent): the body is the whole prompt as plain text.
 *  - "minimax-h3-ref2va": the body is a GLOBAL block plus one or more SCENE
 *    blocks, each with the fixed sections below.
 *
 * MiniMax scenes: the global-capable sections are `subject_definitions`,
 * `retention_analysis`, `overall_soundscape` and `non_diegetic_music` — the
 * prompt defines them once and a scene fills one in only to override it; an
 * empty scene value falls back to the global. `summary` and
 * `detailed_description` are the per-scene content sections.
 *
 * Canonical section order (clipboard copy + on-disk storage):
 *
 *     subject_definitions, summary, retention_analysis,
 *     detailed_description, overall_soundscape, non_diegetic_music
 *
 * A section left empty with no global to fall back on becomes "N/A" when
 * copied. Markers keep the format
 * hand-editable and round-trippable. Metadata is shown in the UI but never
 * copied. A file with no `type` (e.g. an arbitrary .txt) loads as "default".
 */

const { DEFAULT_TYPE, MINIMAX_TYPE, normalizeType } = require("js/prompt-types.js");

/** Every section, in canonical order. */
const SECTION_ORDER = [
  "subject_definitions",
  "summary",
  "retention_analysis",
  "detailed_description",
  "overall_soundscape",
  "non_diegetic_music",
];

/**
 * Sections that have a prompt-level global with per-scene override. The audio
 * pair belongs here because ambience and score are usually set once for the
 * whole prompt; a scene overrides them only when its audio differs.
 */
const GLOBAL_SECTIONS = [
  "subject_definitions",
  "retention_analysis",
  "overall_soundscape",
  "non_diegetic_music",
];

/** Per-scene content sections (these get "N/A" when left empty). */
const CONTENT_SECTIONS = ["summary", "detailed_description"];

/** Short guidance shown discreetly under each section label in the editor. */
const SECTION_HINTS = {
  subject_definitions: "Defines referenced content and its reference labels",
  summary:
    "Summarizes the task type, target video, and main reference relationships",
  retention_analysis:
    "Describes how referenced content is preserved, transferred, or reused",
  detailed_description:
    "Describes visuals, actions, shots, sound, and dialogue in playback order",
  overall_soundscape: "Summarizes ambience and physical sounds",
  non_diegetic_music:
    "Describes background music audible only to the audience",
};

/** Empty placeholder used for sections left blank when copying/saving. */
const EMPTY_SECTION = "N/A";

/** Sections a scene must fill in to be considered complete. */
const REQUIRED_SECTIONS = ["summary", "detailed_description"];

/** True when a section holds real content (not blank and not the "N/A" placeholder). */
function isSectionFilled(value) {
  const v = (value ?? "").trim();
  return v !== "" && v.toUpperCase() !== EMPTY_SECTION.toUpperCase();
}

/** Required sections a scene is missing, in canonical order ([] when complete). */
function missingRequiredSections(scene) {
  return REQUIRED_SECTIONS.filter((k) => !isSectionFilled(scene && scene[k]));
}

/** Highest rating a prompt can have. Ratings run 1..MAX_RATING; 0 = unrated. */
const MAX_RATING = 5;

const META_MARK = "===== META =====";
const GLOBAL_MARK = "===== GLOBAL =====";
const SCENE_MARK = "===== SCENE =====";
const metaSepRe = /^\s*=+\s*META\s*=*\s*$/i;
const globalSepRe = /^\s*=+\s*GLOBAL\s*=*\s*$/i;
const sceneSepRe = /^\s*=+\s*SCENE\s*=*\s*$/i;
// Tolerant section header. Accepts, on its own line: the section name with or
// WITHOUT a colon, optional markdown (**, #, -, >), and a space or underscore
// between words ("detailed description" == "detailed_description"). A colon may
// carry an inline value ("summary: text"). Without a colon it only matches when
// the section name is alone on the line, so content like "summary of the day"
// is not mistaken for a header.
const sectionAlt = SECTION_ORDER.map((s) => s.replace(/_/g, "[ _]")).join("|");
const headerRe = new RegExp(
  `^\\s*(?:[*#>\\-]+\\s*)?\\**\\s*(${sectionAlt})\\**\\s*(?::\\s*(.*?))?\\s*$`,
  "i"
);
const fenceRe = /^\s*```/;
const metaLineRe = /^\s*([A-Za-z_]+)\s*:\s*(.*)$/;

function emptyScene() {
  const s = {};
  for (const k of SECTION_ORDER) s[k] = "";
  return s;
}

function emptyGlobals() {
  const g = {};
  for (const k of GLOBAL_SECTIONS) g[k] = "";
  return g;
}

/** Prompt-level metadata (shown in the UI, never copied). */
function emptyMeta() {
  return { rating: 0, loras: [] };
}

/** Coerce a value to a valid rating integer in 0..MAX_RATING (0 = unrated). */
function clampRating(v) {
  const r = parseInt(v, 10);
  return Number.isFinite(r) ? Math.min(MAX_RATING, Math.max(0, r)) : 0;
}

/** Split a comma-separated string into trimmed, de-duplicated, non-empty items. */
function parseList(str) {
  const seen = new Set();
  const out = [];
  for (const raw of String(str ?? "").split(",")) {
    const v = raw.trim();
    if (v && !seen.has(v)) {
      seen.add(v);
      out.push(v);
    }
  }
  return out;
}

function toScene(data) {
  const s = emptyScene();
  for (const k of SECTION_ORDER) s[k] = typeof data[k] === "string" ? data[k] : "";
  // A global-capable section holding just the "N/A" placeholder is NOT an
  // override: drop it so the value falls back to the global. This matters for
  // files written while the audio sections were still per-scene content (they
  // stored "N/A" when empty), and for AI drafts that answer "N/A".
  for (const k of GLOBAL_SECTIONS) if (!isSectionFilled(s[k])) s[k] = "";
  return s;
}

function trimBlankEdges(lines) {
  let a = 0;
  let b = lines.length;
  while (a < b && lines[a].trim() === "") a++;
  while (b > a && lines[b - 1].trim() === "") b--;
  return lines.slice(a, b).join("\n");
}

/**
 * Split an optional leading META block from the body. Meta lines are the
 * contiguous `key: value` lines right after the `===== META =====` marker; a
 * blank line (or a body marker) ends the block. Without a META marker the whole
 * text is the body.
 * @returns {{ meta: Record<string,string>, body: string }}
 */
function splitMeta(text) {
  const lines = (text ?? "").split(/\r?\n/);
  let i = 0;
  while (i < lines.length && lines[i].trim() === "") i++;
  if (!metaSepRe.test(lines[i] ?? "")) return { meta: {}, body: text ?? "" };

  i++; // past the META marker
  while (i < lines.length && lines[i].trim() === "") i++; // blanks after marker
  const meta = {};
  while (i < lines.length) {
    const line = lines[i];
    if (line.trim() === "") { i++; break; } // a blank ends the meta block
    if (globalSepRe.test(line) || sceneSepRe.test(line)) break;
    const m = line.match(metaLineRe);
    if (!m) break;
    meta[m[1].toLowerCase()] = m[2].trim();
    i++;
  }
  while (i < lines.length && lines[i].trim() === "") i++; // blanks before body
  return { meta, body: lines.slice(i).join("\n") };
}

/** Parse a MiniMax body (GLOBAL + SCENE blocks) into globals + scenes. */
function parseMinimaxBody(text) {
  const lines = (text ?? "").split(/\r?\n/);
  const blocks = [];
  let block = null;
  let section = null;
  let buf = [];

  const flush = () => {
    if (block && section) block.data[section] = trimBlankEdges(buf);
    buf = [];
  };
  const start = (kind) => {
    flush();
    block = { kind, data: {} };
    blocks.push(block);
    section = null;
  };

  for (const line of lines) {
    if (fenceRe.test(line)) continue; // ignore stray ``` markdown fences
    if (globalSepRe.test(line)) { start("global"); continue; }
    if (sceneSepRe.test(line)) { start("scene"); continue; }
    const h = line.match(headerRe);
    if (h) {
      flush();
      if (!block) start("scene"); // header before any marker -> a scene (legacy)
      section = h[1].toLowerCase().replace(/\s+/g, "_"); // "detailed description" -> key
      if (h[2] && h[2].trim()) buf.push(h[2]); // inline value on the header line
      continue;
    }
    if (section) buf.push(line);
  }
  flush();

  const globals = emptyGlobals();
  const scenes = [];
  for (const b of blocks) {
    if (b.kind === "global") {
      for (const k of GLOBAL_SECTIONS) {
        if (typeof b.data[k] === "string") globals[k] = b.data[k];
      }
    } else {
      scenes.push(toScene(b.data));
    }
  }
  if (scenes.length === 0) scenes.push(emptyScene());
  return { globals, scenes };
}

/**
 * True when text carries the MiniMax structure (a GLOBAL/SCENE marker or a
 * recognized `section:` header line). Used to guard converting a default prompt
 * to the MiniMax type — plain free text has none of these and is rejected.
 */
function looksLikeMinimax(text) {
  return (text ?? "")
    .split(/\r?\n/)
    .some((l) => globalSepRe.test(l) || sceneSepRe.test(l) || headerRe.test(l));
}

/**
 * Parse a prompt file into its type + metadata + body/scenes.
 * @param {string} text
 * @returns {{ type:string, meta:{rating:number,loras:string[]}, body:string,
 *            globals: Record<string,string>, scenes: object[] }}
 */
function parsePrompt(text) {
  const { meta: rawMeta, body } = splitMeta(text);
  const meta = emptyMeta();
  meta.rating = clampRating(rawMeta.rating);
  meta.loras = parseList(rawMeta.loras);
  const type = normalizeType(rawMeta.type);

  if (type === MINIMAX_TYPE) {
    const { globals, scenes } = parseMinimaxBody(body);
    return { type, meta, body: "", globals, scenes };
  }
  return {
    type: DEFAULT_TYPE,
    meta,
    body: (body ?? "").replace(/\s+$/, ""),
    globals: emptyGlobals(),
    scenes: [],
  };
}

/** Effective value for a section: the scene's own if set, else the global. */
function effectiveSection(scene, globals, key) {
  if (!GLOBAL_SECTIONS.includes(key)) return scene[key] ?? "";
  // "N/A" is treated as no override (see toScene), so it falls back too.
  return isSectionFilled(scene && scene[key])
    ? scene[key]
    : (globals && globals[key]) || "";
}

/**
 * One scene in the canonical clipboard format. Global-capable sections fall back
 * to the global value; every empty section becomes "N/A".
 * @param {object} scene
 * @param {Record<string,string>} globals
 */
function sceneToText(scene, globals) {
  const parts = [];
  for (const k of SECTION_ORDER) {
    let v = effectiveSection(scene, globals, k);
    if ((v ?? "").trim() === "") v = EMPTY_SECTION;
    parts.push(`${k}:\n${v}`);
  }
  return parts.join("\n\n");
}

/** A scene block for on-disk storage: raw values in canonical order. */
function sceneFileText(scene) {
  return SECTION_ORDER.map((k) => `${k}:\n${scene[k] ?? ""}`).join("\n\n");
}

/** Serialize just the MiniMax body (GLOBAL block + one block per scene). */
function serializeMinimaxBody(globals, scenes) {
  const list = scenes && scenes.length ? scenes : [emptyScene()];
  const globalText = GLOBAL_SECTIONS.map(
    (k) => `${k}:\n${(globals && globals[k]) || ""}`
  ).join("\n\n");
  const blocks = [`${GLOBAL_MARK}\n\n${globalText}`];
  for (const s of list) blocks.push(`${SCENE_MARK}\n\n${sceneFileText(s)}`);
  return blocks.join("\n\n");
}

/**
 * Full file contents for a prompt of any type. The META block is written only
 * when there's something to persist — for MiniMax the `type:` is always written
 * (so it isn't re-read as "default"); for the default type it is omitted (its
 * absence implies "default"), keeping such files as clean plain text.
 * @param {{ type:string, meta:{rating:number,loras:string[]}, body?:string,
 *           globals?:Record<string,string>, scenes?:object[] }} prompt
 */
function serializePrompt({ type, meta, body, globals, scenes }) {
  const t = normalizeType(type);
  const rating = clampRating(meta && meta.rating);
  const loras = meta && Array.isArray(meta.loras) ? meta.loras : [];
  const metaLines = [];
  if (t !== DEFAULT_TYPE) metaLines.push(`type: ${t}`);
  if (rating > 0) metaLines.push(`rating: ${rating}`);
  if (loras.length) metaLines.push(`loras: ${loras.join(", ")}`);

  const blocks = [];
  if (metaLines.length) blocks.push(`${META_MARK}\n\n${metaLines.join("\n")}`);
  if (t === MINIMAX_TYPE) blocks.push(serializeMinimaxBody(globals, scenes));
  else blocks.push((body ?? "").replace(/\s+$/, ""));
  return blocks.join("\n\n");
}

/**
 * Return a copy of the scene where the content sections are "N/A" when empty
 * and the global-capable ones are blank unless they really override the global
 * (so a stale "N/A" from an older file is cleaned up on the next save).
 */
function normalizeScene(scene) {
  const out = {};
  for (const k of SECTION_ORDER) out[k] = scene[k] ?? "";
  for (const k of CONTENT_SECTIONS) {
    if ((out[k] ?? "").trim() === "") out[k] = EMPTY_SECTION;
  }
  for (const k of GLOBAL_SECTIONS) {
    if (!isSectionFilled(out[k])) out[k] = "";
  }
  return out;
}

exports.SECTION_ORDER = SECTION_ORDER;
exports.GLOBAL_SECTIONS = GLOBAL_SECTIONS;
exports.CONTENT_SECTIONS = CONTENT_SECTIONS;
exports.SECTION_HINTS = SECTION_HINTS;
exports.EMPTY_SECTION = EMPTY_SECTION;
exports.REQUIRED_SECTIONS = REQUIRED_SECTIONS;
exports.isSectionFilled = isSectionFilled;
exports.missingRequiredSections = missingRequiredSections;
exports.MAX_RATING = MAX_RATING;
exports.emptyScene = emptyScene;
exports.emptyGlobals = emptyGlobals;
exports.emptyMeta = emptyMeta;
exports.clampRating = clampRating;
exports.parseList = parseList;
exports.parseMinimaxBody = parseMinimaxBody;
exports.looksLikeMinimax = looksLikeMinimax;
exports.parsePrompt = parsePrompt;
exports.effectiveSection = effectiveSection;
exports.sceneToText = sceneToText;
exports.serializeMinimaxBody = serializeMinimaxBody;
exports.serializePrompt = serializePrompt;
exports.normalizeScene = normalizeScene;
  });
  register("js/prompt-types.js", function (module, exports, require) {
/**
 * Prompt TYPES (a.k.a. targets) for the Prompt Library.
 *
 * A prompt's type decides how it is edited, stored, and shown:
 *  - "default"           — a single free-text prompt shown in one text field.
 *  - "minimax-h3-ref2va" — the structured scene format (subject_definitions,
 *                          summary, retention_analysis, …) specific to the
 *                          MiniMax H3 Ref2VA video model (see scenes.js).
 *
 * The type is stored as `type:` metadata in the .txt file. In its ABSENCE the
 * prompt is treated as "default" and the whole file is shown as plain text.
 *
 * (Not to be confused with task-types.js, which are the checkboxes inside a
 * MiniMax scene's summary.)
 */

const DEFAULT_TYPE = "default";
const MINIMAX_TYPE = "minimax-h3-ref2va";

/** Registry, in display order. */
const PROMPT_TYPES = [
  { id: DEFAULT_TYPE, label: "Default (plain text)" },
  { id: MINIMAX_TYPE, label: "MiniMax H3 Ref2VA" },
];

function isKnownType(id) {
  return PROMPT_TYPES.some((t) => t.id === id);
}

/** A known type id, or DEFAULT_TYPE when missing/unknown. */
function normalizeType(id) {
  return isKnownType(id) ? id : DEFAULT_TYPE;
}

function promptTypeLabel(id) {
  return (PROMPT_TYPES.find((t) => t.id === id) || {}).label || id;
}

exports.DEFAULT_TYPE = DEFAULT_TYPE;
exports.MINIMAX_TYPE = MINIMAX_TYPE;
exports.PROMPT_TYPES = PROMPT_TYPES;
exports.isKnownType = isKnownType;
exports.normalizeType = normalizeType;
exports.promptTypeLabel = promptTypeLabel;
  });
  register("js/modal.js", function (module, exports, require) {
const { toast } = require("js/toast.js");

/**
 * Generic modal dialog. Shows either plain text (monospace, e.g. the example
 * prompt) or a trusted HTML fragment (e.g. section help). Optional Copy button.
 */

const $ = (id) => document.getElementById(id);
let el;

function initModal() {
  el = {
    root: $("modal"),
    title: $("modalTitle"),
    body: $("modalBody"),
    copyBtn: $("modalCopyBtn"),
    closeBtn: $("modalCloseBtn"),
  };
  el.closeBtn.addEventListener("click", closeModal);
  el.root.querySelector(".modal-backdrop").addEventListener("click", closeModal);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      ev.stopImmediatePropagation(); // when open over another modal, close only this one
      closeModal();
    }
  });
}

/**
 * @param {Object} opts
 * @param {string} [opts.title]
 * @param {string} [opts.html]   trusted HTML fragment (mutually exclusive with text)
 * @param {string} [opts.text]   plain text shown monospace/pre-wrapped
 * @param {string} [opts.copyText] if set, a Copy button copies this to clipboard
 */
function openModal({ title = "", html = null, text = null, copyText = null } = {}) {
  el.title.textContent = title;

  if (html != null) {
    el.body.className = "modal-body modal-html";
    el.body.innerHTML = html;
  } else {
    el.body.className = "modal-body pre";
    el.body.textContent = text ?? "";
  }

  if (copyText != null) {
    el.copyBtn.hidden = false;
    el.copyBtn.onclick = async () => {
      try {
        await navigator.clipboard.writeText(copyText);
        toast("Copied.");
      } catch {
        toast("Clipboard blocked by the browser.", "warn");
      }
    };
  } else {
    el.copyBtn.hidden = true;
    el.copyBtn.onclick = null;
  }

  el.body.scrollTop = 0;
  el.root.hidden = false;
}

function closeModal() {
  if (el) el.root.hidden = true;
}

exports.initModal = initModal;
exports.openModal = openModal;
exports.closeModal = closeModal;
  });
  register("js/toast.js", function (module, exports, require) {
/**
 * Shared toast notifications. Non-blocking; optional action buttons keep the
 * toast on screen until the user chooses (used for conflict resolution).
 *
 * @param {string} message
 * @param {""|"warn"|"error"} [type]
 * @param {{label: string, onClick: () => void}[]} [actions]
 */
function toast(message, type = "", actions = []) {
  const host = document.getElementById("toasts");
  if (!host) return;

  const t = document.createElement("div");
  t.className = "toast" + (type ? " " + type : "");

  const msg = document.createElement("div");
  msg.textContent = message;
  t.appendChild(msg);

  if (actions.length) {
    const row = document.createElement("div");
    row.className = "toast-actions";
    for (const a of actions) {
      const b = document.createElement("button");
      b.className = "btn";
      b.textContent = a.label;
      b.addEventListener("click", () => {
        try {
          a.onClick();
        } finally {
          t.remove();
        }
      });
      row.appendChild(b);
    }
    t.appendChild(row);
  } else {
    setTimeout(() => t.remove(), 4000);
  }

  host.appendChild(t);
}

exports.toast = toast;
  });
  register("js/prompt-library.js", function (module, exports, require) {
const { PromptLibraryAdapter } = require("js/storage/prompt-library-adapter.js");
const { idbGet, idbSet } = require("js/storage/idb.js");
const { state, applyLibraryFilters } = require("js/store.js");
const { SECTION_ORDER, GLOBAL_SECTIONS, CONTENT_SECTIONS, SECTION_HINTS, MAX_RATING, emptyScene, emptyGlobals, emptyMeta, clampRating, parseList, isSectionFilled, normalizeScene, sceneToText, missingRequiredSections, parseMinimaxBody, serializeMinimaxBody, looksLikeMinimax } = require("js/scenes.js");
const { PROMPT_TYPES, DEFAULT_TYPE, MINIMAX_TYPE, normalizeType } = require("js/prompt-types.js");
const { EXAMPLE_PROMPT } = require("js/help-example.js");
const { PROMPT_GUIDE_HTML } = require("js/prompt-guide.js");
const { SECTION_HELP } = require("js/section-help.js");
const { TASK_TYPES, TASK_TYPE_HINTS, parsePrefix, setPrefix } = require("js/task-types.js");
const { highlightHtml } = require("js/highlight.js");
const { openModal } = require("js/modal.js");
const { toast } = require("js/toast.js");
const { isLlmOnline, onLlmStatusChange } = require("js/llm/llm-config.js");
const { openTextAi } = require("js/text-ai.js");
const { openGlobalsAi } = require("js/globals-ai.js");
const { openReviewAi } = require("js/review-ai.js");

/**
 * Prompt Library section (spec §1.2, §3.3): manage reusable prompts stored as
 * one .txt per prompt in a folder of the user's choosing — separate from the
 * images folder.
 *
 * A prompt has a TYPE (see prompt-types.js):
 *  - "default": a single plain-text field holding the whole prompt.
 *  - "minimax-h3-ref2va": prompt-level GLOBAL sections (subject_definitions,
 *    retention_analysis) plus a list of SCENES, each with all sections in
 *    canonical order and optional per-scene overrides (see scenes.js).
 *
 * The editor swaps between a plain-text field and the scene editor based on the
 * type dropdown. The type can be changed to migrate; switching to MiniMax is
 * refused unless the current text is already in MiniMax format.
 *
 * Master list on the left; the editor on the right. Copying/previewing a MiniMax
 * scene yields the canonical "section:\n<text>" format with global fallback and
 * "N/A" for blanks.
 */

const adapter = new PromptLibraryAdapter();
const $ = (id) => document.getElementById(id);

let el;
/** @type {import("./storage/prompt-library-adapter.js").PromptEntry|null} */
let current = null;
let isNew = false;
let baseline = "";
/** Textareas for the prompt-level global sections, keyed by section name. */
let globalTas = {};
/** Current prompt rating shown in the editor (0 = unrated). */
let ratingValue = 0;
/** The type of the prompt currently in the editor (drives which UI shows). */
let editorType = DEFAULT_TYPE;

function initLibrary() {
  el = {
    openBtn: $("libOpenBtn"),
    reopenBtn: $("libReopenBtn"),
    folderName: $("libFolderName"),
    search: $("libSearch"),
    newBtn: $("libNewBtn"),
    counts: $("libCounts"),
    list: $("libList"),
    listToggle: $("libListToggle"),
    layout: $("libLayout"),
    editor: $("libEditor"),
    empty: $("libEmpty"),
    global: $("libGlobal"),
    globalHead: $("libGlobalHead"),
    globalGenBtn: $("libGlobalGenBtn"),
    scroll: $("libScroll"),
    title: $("libTitle"),
    type: $("libType"),
    body: $("libBody"),
    bodyText: $("libBodyText"),
    bodyGenBtn: $("libBodyGenBtn"),
    rating: $("libRating"),
    loras: $("libLorasInput"),
    dirty: $("libDirty"),
    reviewBtn: $("libReviewBtn"),
    previewBtn: $("libPreviewBtn"),
    duplicateBtn: $("libDuplicateBtn"),
    deleteBtn: $("libDeleteBtn"),
    saveBtn: $("libSaveBtn"),
    menu: $("libMenu"),
    scenes: $("libScenes"),
    scenesFooter: $("libScenesFooter"),
    addSceneBtn: $("libAddSceneBtn"),
    collapseAllBtn: $("libCollapseAllBtn"),
    meta: $("libEditMeta"),
    helpBtn: $("libHelpBtn"),
    guideBtn: $("libGuideBtn"),
  };

  // Fill the prompt-type dropdown from the registry.
  for (const t of PROMPT_TYPES) {
    const o = document.createElement("option");
    o.value = t.id;
    o.textContent = t.label;
    el.type.appendChild(o);
  }

  el.openBtn.addEventListener("click", openPicker);
  el.listToggle.addEventListener("click", toggleList);
  el.newBtn.addEventListener("click", newPrompt);
  el.saveBtn.addEventListener("click", save);
  el.duplicateBtn.addEventListener("click", duplicate);
  el.deleteBtn.addEventListener("click", remove);
  el.addSceneBtn.addEventListener("click", addScene);
  el.collapseAllBtn.addEventListener("click", toggleAll);
  el.rating.addEventListener("mouseleave", clearHot);
  el.loras.addEventListener("input", markDirty);
  el.title.addEventListener("input", markDirty);
  el.type.addEventListener("change", onTypeChange);
  el.bodyText.addEventListener("input", () => {
    autoGrow(el.bodyText);
    markDirty();
  });
  el.bodyGenBtn.dataset.genTitle = "Generate this prompt with AI";
  el.bodyGenBtn.addEventListener("click", generateDefault);
  el.globalGenBtn.dataset.genTitle = "Generate subject_definitions / retention_analysis with AI";
  el.globalGenBtn.addEventListener("click", generateGlobals);
  el.reviewBtn.dataset.genTitle = "Review the whole prompt across all scenes with AI";
  el.reviewBtn.addEventListener("click", reviewPrompt);
  el.previewBtn.addEventListener("click", previewPrompt);
  // Enable/disable the "Generate" buttons with LLM connectivity.
  onLlmStatusChange(updateGenButtons);
  el.search.addEventListener("input", () => {
    state.library.search = el.search.value;
    applyLibraryFilters();
    renderList();
  });

  // Show the full example prompt (with a Copy action).
  el.helpBtn.addEventListener("click", () =>
    openModal({
      title: "Example prompt",
      text: EXAMPLE_PROMPT,
      copyText: EXAMPLE_PROMPT,
    })
  );

  // Show the full prompt guide.
  el.guideBtn.addEventListener("click", () =>
    openModal({ title: "Prompt guide", html: PROMPT_GUIDE_HTML })
  );

  // The prompt header's overflow menu is static markup; the scene ones are built
  // by makeActionMenu, which wires itself.
  wireActionMenu(el.menu);
  // Any click outside an open "⋮" menu dismisses it (the menu button and its
  // items handle their own closing).
  document.addEventListener("click", () => closeActionMenus());

  document.addEventListener("keydown", (ev) => {
    if (state.section !== "library") return;
    const mod = ev.ctrlKey || ev.metaKey;
    if (mod && (ev.key === "s" || ev.key === "S")) {
      ev.preventDefault();
      if (!el.saveBtn.disabled) save();
    }
    if (mod && (ev.key === "b" || ev.key === "B")) {
      ev.preventDefault();
      toggleList();
    }
    if (ev.key === "Escape") closeActionMenus();
  });

  applyListCollapsed();
  restore();
}

/** Focus this section's search (called from the global Ctrl+F handler). */
function focusSearch() {
  el.search.focus();
  el.search.select();
}

function isDirty() {
  return state.library.dirty;
}

/** Re-measure textareas that may have been rendered while the section was hidden. */
function onShow() {
  regrowVisible();
}

/**
 * Re-grow every currently-visible textarea for the active type. A textarea
 * measured while hidden (display:none) reports scrollHeight 0 and stays
 * collapsed, so ANYTHING that reveals fields must re-measure afterwards — this is
 * the single place that does it (called from onShow, applyTypeView and
 * applyListCollapsed, which changes how wide the editor is).
 */
function regrowVisible() {
  if (editorType === MINIMAX_TYPE) {
    for (const ta of Object.values(globalTas)) autoGrow(ta);
    for (const c of el.scenes.querySelectorAll(".scene-card")) {
      if (c.classList.contains("collapsed")) continue;
      for (const ta of c.querySelectorAll(".scene-section")) autoGrow(ta);
    }
  } else {
    autoGrow(el.bodyText);
  }
}

/**
 * Collapse/expand the master list (Ctrl+B) so the editor can use the whole
 * width — long sections are much easier to read and write wide.
 */
function toggleList() {
  state.library.listCollapsed = !state.library.listCollapsed;
  applyListCollapsed();
}

function applyListCollapsed() {
  const collapsed = state.library.listCollapsed;
  el.layout.classList.toggle("list-collapsed", collapsed);
  el.listToggle.textContent = collapsed ? "⟩" : "⟨";
  el.listToggle.title = collapsed
    ? "Show the prompt list (Ctrl+B)"
    : "Hide the prompt list (Ctrl+B)";
  el.listToggle.setAttribute("aria-label", el.listToggle.title);
  el.listToggle.setAttribute("aria-expanded", collapsed ? "false" : "true");
  // The editor just changed width, so wrapped textareas need a fresh measure.
  regrowVisible();
}

// ---------------------------------------------------------------------------
// Rendering
// ---------------------------------------------------------------------------
function updateCounts() {
  const n = state.library.entries.length;
  el.counts.textContent = n ? `${n} prompt${n === 1 ? "" : "s"}` : "";
}

function renderList() {
  el.list.textContent = "";
  const frag = document.createDocumentFragment();
  for (const e of state.library.filtered) {
    const item = document.createElement("div");
    item.className = "lib-item";
    if (!isNew && current && e.name === current.name) item.classList.add("active");

    const t = document.createElement("div");
    t.className = "t";
    t.textContent = e.title;
    item.appendChild(t);

    const sub = document.createElement("div");
    sub.className = "tags";
    const type = normalizeType(e.type);
    if (type === MINIMAX_TYPE) {
      const count = e.scenes.length;
      sub.textContent = `${count} scene${count === 1 ? "" : "s"}`;
    } else {
      sub.textContent = "text";
    }
    const rating = e.meta ? e.meta.rating : 0;
    if (rating > 0) {
      const stars = document.createElement("span");
      stars.className = "stars";
      stars.textContent = " · " + starsText(rating);
      sub.appendChild(stars);
    }
    const incomplete =
      type === MINIMAX_TYPE
        ? e.scenes.filter((s) => sceneMissing(s).length > 0).length
        : 0;
    if (incomplete > 0) {
      const flag = document.createElement("span");
      flag.className = "lib-incomplete";
      flag.textContent = ` · ⚠ ${incomplete} incomplete`;
      flag.title = `${incomplete} scene${incomplete === 1 ? "" : "s"} missing a required section or task type`;
      sub.appendChild(flag);
    }
    item.appendChild(sub);

    item.addEventListener("click", () => selectEntry(e));
    frag.appendChild(item);
  }
  el.list.appendChild(frag);
}

function autoGrow(ta) {
  ta.style.height = "auto";
  ta.style.height = ta.scrollHeight + "px";
}

/** Word count of a textarea's value (0 for blank). */
function countWords(text) {
  const t = (text ?? "").trim();
  return t ? t.split(/\s+/).length : 0;
}

/** A label row for a section, with an optional "?" help button. */
function makeLabelRow(key) {
  const labelRow = document.createElement("div");
  labelRow.className = "scene-label-row";
  const label = document.createElement("label");
  label.textContent = key;
  labelRow.appendChild(label);
  if (SECTION_HELP[key]) {
    const help = document.createElement("button");
    help.type = "button";
    help.className = "section-help";
    help.textContent = "?";
    help.title = `About the "${key}" section`;
    help.addEventListener("click", () =>
      openModal({ title: `${key} — guidance`, html: SECTION_HELP[key] })
    );
    labelRow.appendChild(help);
  }
  return labelRow;
}

/** A textarea with a highlight backdrop. `section` (or null) sets dataset. */
function makeHighlightedTextarea(section, value) {
  const wrap = document.createElement("div");
  wrap.className = "hl-wrap";
  const backdrop = document.createElement("div");
  backdrop.className = "hl-backdrop";
  backdrop.setAttribute("aria-hidden", "true");
  backdrop.innerHTML = highlightHtml(value ?? "");
  const ta = document.createElement("textarea");
  ta.className = "scene-section";
  if (section) ta.dataset.section = section;
  ta.value = value ?? "";
  ta.addEventListener("input", () => {
    backdrop.innerHTML = highlightHtml(ta.value);
    autoGrow(ta);
    markDirty();
  });
  wrap.append(backdrop, ta);
  return { wrap, ta, backdrop };
}

/** Task types currently checked in a group, in canonical table order. */
function readChosenTypes(group) {
  const checked = new Set();
  for (const cb of group.querySelectorAll('input[type="checkbox"]')) {
    if (cb.checked) checked.add(cb.value);
  }
  return TASK_TYPES.filter((t) => checked.has(t));
}

/** Build the task-type checkbox row bound to a summary textarea. */
function makeTaskTypes(summaryTa) {
  const group = document.createElement("div");
  group.className = "task-types";

  const grid = document.createElement("div");
  grid.className = "task-types-grid";
  const selected = new Set(parsePrefix(summaryTa.value).types);
  for (const t of TASK_TYPES) {
    const lab = document.createElement("label");
    lab.className = "task-type";
    lab.title = TASK_TYPE_HINTS[t] ?? "";
    const cb = document.createElement("input");
    cb.type = "checkbox";
    cb.value = t;
    cb.checked = selected.has(t);
    cb.addEventListener("change", () => {
      summaryTa.value = setPrefix(summaryTa.value, readChosenTypes(group));
      // Re-run the textarea input pipeline (backdrop highlight, autoGrow,
      // dirty, checkbox sync) from one place.
      summaryTa.dispatchEvent(new Event("input", { bubbles: true }));
    });
    lab.append(cb, document.createTextNode(" " + t));
    grid.appendChild(lab);
  }
  group.appendChild(grid);
  return group;
}

/** Reflect a manually edited summary prefix back into the checkboxes. */
function syncTaskTypes(group, summaryTa) {
  const selected = new Set(parsePrefix(summaryTa.value).types);
  for (const cb of group.querySelectorAll('input[type="checkbox"]')) {
    cb.checked = selected.has(cb.value);
  }
}

/**
 * Build the collapsible group that holds a scene's optional overrides for the
 * global-capable sections (GLOBAL_SECTIONS). It is hidden by default — these
 * fields duplicate the prompt-level globals and are rarely set per scene, so
 * keeping them out of the way saves vertical space.
 * @returns {{group: HTMLDivElement, body: HTMLDivElement, toggle: HTMLButtonElement}}
 */
function makeOverrideGroup() {
  const group = document.createElement("div");
  group.className = "scene-overrides collapsed";

  const toggle = document.createElement("button");
  toggle.type = "button";
  toggle.className = "scene-overrides-toggle";
  toggle.addEventListener("click", () =>
    setOverridesShown(group, group.classList.contains("collapsed"))
  );

  const body = document.createElement("div");
  body.className = "scene-overrides-body";
  group.append(toggle, body);
  setOverridesShown(group, false); // initialize the collapsed-state toggle label
  return { group, body, toggle };
}

/** Show or hide a scene's override group and refresh its toggle label. */
function setOverridesShown(group, shown) {
  group.classList.toggle("collapsed", !shown);
  updateOverrideBadge(group);
  // Textareas measured while hidden have 0 height; re-grow them on reveal.
  if (shown) {
    for (const ta of group.querySelectorAll(".scene-section")) autoGrow(ta);
  }
}

/**
 * Refresh the override group's affordance: flag the group when the scene
 * actually overrides a global-capable section, and name those sections in the
 * collapsed label — an applied override must be obvious without expanding
 * (collapsed it is styled green, see `.scene-overrides.has-override`).
 * The group body holds only the GLOBAL_SECTIONS fields, so every
 * `.scene-section` inside it is an override.
 */
function updateOverrideBadge(group) {
  const overridden = [...group.querySelectorAll(".scene-section")]
    .filter((ta) => isSectionFilled(ta.value))
    .map((ta) => ta.dataset.section);
  group.classList.toggle("has-override", overridden.length > 0);

  const toggle = group.querySelector(".scene-overrides-toggle");
  if (!toggle) return;
  const shown = !group.classList.contains("collapsed");
  if (shown) {
    toggle.textContent = "▾ Hide scene overrides";
    toggle.title = "Hide the optional per-scene overrides";
  } else if (overridden.length) {
    toggle.textContent = "▸ Overrides · ";
    // The section names keep their on-disk lowercase spelling, unlike the
    // uppercased label around them.
    const names = document.createElement("span");
    names.className = "scene-overrides-names";
    names.textContent = overridden.join(", ");
    toggle.appendChild(names);
    toggle.title = `This scene overrides ${overridden.join(", ")} — click to show`;
  } else {
    toggle.textContent = "▸ Scene overrides (optional)";
    toggle.title =
      "Show optional per-scene overrides for " + GLOBAL_SECTIONS.join(", ");
  }
}

/**
 * Mark a button as an AI action: it needs a live LLM, so it is disabled while
 * offline and re-enabled by updateGenButtons() (which keys off `.scene-generate`
 * and `dataset.genTitle`).
 */
function markAiAction(btn, genTitle) {
  btn.type = "button";
  btn.classList.add("scene-generate"); // the look is the caller's business
  btn.dataset.genTitle = genTitle;
  btn.disabled = !isLlmOnline();
  btn.title = isLlmOnline()
    ? genTitle
    : "AI not connected — configure it in AI settings";
  return btn;
}

/**
 * Build a "⋮" overflow menu. Only the actions used while writing stay visible in
 * a header; the occasional ones go here, so the header holds a single row
 * however many actions exist. Used for the scene headers; the prompt header's
 * menu is the same component declared in index.html and wired with
 * `wireActionMenu`.
 * @param {{label:string, onClick:()=>void, title?:string, ai?:string,
 *          danger?:boolean}[]} items  `ai` marks an LLM action (its text is the
 *          tooltip); `danger` styles a destructive one.
 */
function makeActionMenu(items) {
  const wrap = document.createElement("div");
  wrap.className = "action-menu";

  const btn = document.createElement("button");
  btn.type = "button";
  btn.className = "btn action-menu-btn";
  btn.textContent = "⋮";
  btn.title = "More actions";
  btn.setAttribute("aria-haspopup", "menu");
  btn.setAttribute("aria-expanded", "false");

  const list = document.createElement("div");
  list.className = "action-menu-list";
  list.setAttribute("role", "menu");
  list.hidden = true;

  for (const it of items) {
    const item = document.createElement("button");
    item.type = "button";
    item.className = "action-menu-item" + (it.danger ? " danger" : "");
    item.setAttribute("role", "menuitem");
    item.textContent = it.label;
    if (it.ai) markAiAction(item, it.ai);
    else if (it.title) item.title = it.title;
    item.addEventListener("click", it.onClick);
    list.appendChild(item);
  }

  wrap.append(btn, list);
  wireActionMenu(wrap);
  return wrap;
}

/** Give an "⋮" menu (built here or declared in HTML) its open/close behavior. */
function wireActionMenu(wrap) {
  wrap.querySelector(".action-menu-btn").addEventListener("click", (ev) => {
    ev.stopPropagation(); // don't let the document handler close it again
    const wasOpen = isActionMenuOpen(wrap);
    closeActionMenus();
    if (!wasOpen) openActionMenu(wrap);
  });
  // Picking any item dismisses the menu; the item's own handler does the work.
  wrap.querySelector(".action-menu-list").addEventListener("click", closeActionMenus);
}

function isActionMenuOpen(wrap) {
  const list = wrap.querySelector(".action-menu-list");
  return !!list && !list.hidden;
}

/** Open one menu, dropping it upwards when its anchor sits low in the scroll area. */
function openActionMenu(wrap) {
  const list = wrap.querySelector(".action-menu-list");
  list.hidden = false; // must be visible to measure
  wrap.querySelector(".action-menu-btn").setAttribute("aria-expanded", "true");
  // #libScroll clips its overflow, so a menu opening down near the bottom of the
  // scene list would be cut off — flip it above the button instead. (Menus above
  // the scroll area, like the prompt header's, always have room.)
  const room = el.scroll.getBoundingClientRect().bottom - wrap.getBoundingClientRect().bottom;
  wrap.classList.toggle("flip-up", room < list.offsetHeight + 8);
}

/** Close every menu in the editor (at most one is open, but this stays state-free). */
function closeActionMenus() {
  for (const wrap of el.editor.querySelectorAll(".action-menu")) {
    wrap.querySelector(".action-menu-list").hidden = true;
    wrap.querySelector(".action-menu-btn").setAttribute("aria-expanded", "false");
    wrap.classList.remove("flip-up");
  }
}

function makeSceneCard(scene, index, total) {
  const card = document.createElement("div");
  card.className = "scene-card";

  const head = document.createElement("div");
  head.className = "scene-head";

  const toggle = document.createElement("button");
  toggle.type = "button";
  toggle.className = "scene-toggle";
  toggle.title = "Collapse / expand this scene";
  toggle.textContent = "▾";
  toggle.addEventListener("click", () => toggleCard(card));

  const title = document.createElement("span");
  title.className = "scene-title";
  title.textContent = `Scene ${index + 1}`;
  // Clicking the title also toggles, for a bigger hit target.
  title.addEventListener("click", () => toggleCard(card));

  // Flags a scene missing a required section (summary, detailed_description) or
  // a task type. Kept in sync live via the card's input listener below.
  const status = document.createElement("span");
  status.className = "scene-status";
  status.hidden = true;

  const spacer = document.createElement("span");
  spacer.className = "spacer";

  // The header keeps only the actions used while writing a scene; the rest live
  // in the "⋮" menu so the row never wraps.
  const genBtn = document.createElement("button");
  genBtn.className = "btn";
  genBtn.textContent = "✨ Generate";
  markAiAction(genBtn, "Generate a fresh scene with AI");
  genBtn.addEventListener("click", () => generateScene(card, { improve: false }));
  const copyBtn = document.createElement("button");
  copyBtn.className = "btn";
  copyBtn.textContent = "📋 Copy";
  copyBtn.title = "Copy this scene in section:\\n<text> format";
  copyBtn.addEventListener("click", () => copyScene(card));
  const upBtn = document.createElement("button");
  upBtn.className = "btn scene-move";
  upBtn.textContent = "↑";
  upBtn.title = "Move this scene up";
  upBtn.disabled = index === 0;
  upBtn.addEventListener("click", () => moveScene(card, -1));
  const downBtn = document.createElement("button");
  downBtn.className = "btn scene-move";
  downBtn.textContent = "↓";
  downBtn.title = "Move this scene down";
  downBtn.disabled = index === total - 1;
  downBtn.addEventListener("click", () => moveScene(card, 1));

  const menu = makeActionMenu([
    {
      label: "Paste from clipboard",
      title: "Fill this scene's sections from a scene in the clipboard",
      onClick: () => pasteScene(card),
    },
    {
      label: "Improve with AI",
      ai: "Improve this scene's current content with AI",
      onClick: () => generateScene(card, { improve: true }),
    },
    {
      label: "Review with AI",
      ai: "Review this scene against its neighbours with AI",
      onClick: () => reviewScene(card),
    },
    {
      label: "Preview",
      title: "Preview the copied result of this scene",
      onClick: () => previewScene(card),
    },
    {
      label: "Duplicate",
      title: "Add a new scene below with this scene's content",
      onClick: () => duplicateScene(card),
    },
    { label: "Remove", danger: true, onClick: () => removeScene(card) },
  ]);

  head.append(toggle, title, status, spacer, genBtn, copyBtn, upBtn, downBtn, menu);
  card.appendChild(head);

  const body = document.createElement("div");
  body.className = "scene-body";

  // The global-capable sections are per-scene overrides that fall back to the
  // prompt-level globals. They live in a group that is hidden by default; the
  // content sections (summary, detailed_description) render inline.
  const { group: overrideGroup, body: overrideBody } = makeOverrideGroup();
  body.appendChild(overrideGroup);

  for (const key of SECTION_ORDER) {
    const isOverride = GLOBAL_SECTIONS.includes(key);
    const target = isOverride ? overrideBody : body;
    const labelRow = makeLabelRow(key);
    const hint = document.createElement("p");
    hint.className = "scene-hint";
    hint.textContent = SECTION_HINTS[key] ?? "";
    const { wrap, ta } = makeHighlightedTextarea(key, scene[key]);
    target.append(labelRow, hint);

    // Summary: task-type checkboxes after the label, just above the field.
    if (key === "summary") {
      const group = makeTaskTypes(ta);
      ta.addEventListener("input", () => syncTaskTypes(group, ta));
      target.append(group);
    }

    // detailed_description: a live word count in the label row (the guide targets
    // ~350–500 words for this section).
    if (key === "detailed_description") {
      const count = document.createElement("span");
      count.className = "word-count";
      count.title = "Word count — the guide targets ~350–500 words for detailed_description";
      const updateCount = () => {
        const n = countWords(ta.value);
        count.textContent = `${n} word${n === 1 ? "" : "s"}`;
      };
      updateCount();
      ta.addEventListener("input", updateCount);
      labelRow.appendChild(count);
    }
    target.append(wrap);
  }

  // Per-scene override generation: same flow as the prompt-level globals, but
  // targeting this scene's override fields. Lives at the top of the group body.
  const ovGenBtn = document.createElement("button");
  ovGenBtn.type = "button";
  ovGenBtn.className = "btn scene-generate scene-override-gen";
  ovGenBtn.textContent = "✨ Generate";
  ovGenBtn.dataset.genTitle = "Generate this scene's override subject_definitions / retention_analysis with AI";
  ovGenBtn.disabled = !isLlmOnline();
  ovGenBtn.title = isLlmOnline() ? ovGenBtn.dataset.genTitle : "AI not connected — configure it in AI settings";
  ovGenBtn.addEventListener("click", () => generateSceneOverride(card));
  overrideBody.insertBefore(ovGenBtn, overrideBody.firstChild);

  // Reveal the override group up front when a scene already customizes one of
  // those sections, so existing overrides are never hidden from the user.
  const hasOverride = GLOBAL_SECTIONS.some((k) => isSectionFilled(scene[k]));
  if (hasOverride) setOverridesShown(overrideGroup, true);

  card.appendChild(body);
  // Keep the "incomplete" flag and the override marker current: input events
  // from any field bubble here.
  card.addEventListener("input", () => {
    updateSceneStatus(card);
    updateOverrideBadge(overrideGroup);
  });
  updateSceneStatus(card);
  return card;
}

/**
 * What a scene is missing to be complete: the required sections, plus at least
 * one task type in the summary (the [...] prefix). Returns [] when complete.
 */
function sceneMissing(scene) {
  const missing = missingRequiredSections(scene);
  if (parsePrefix(scene.summary || "").types.length === 0) missing.push("task type");
  return missing;
}

/** Flag a scene card when it is missing anything required. */
function updateSceneStatus(card) {
  const missing = sceneMissing(sceneFromCard(card));
  card.classList.toggle("incomplete", missing.length > 0);
  const badge = card.querySelector(".scene-status");
  if (!badge) return;
  badge.hidden = missing.length === 0;
  badge.textContent = missing.length ? "⚠ Incomplete" : "";
  badge.title = missing.length ? "Missing: " + missing.join(", ") : "";
}

/** Collapse or expand one scene card. Re-grows textareas when expanding. */
function setCollapsed(card, collapsed) {
  card.classList.toggle("collapsed", collapsed);
  const toggle = card.querySelector(".scene-toggle");
  if (toggle) toggle.textContent = collapsed ? "▸" : "▾";
  if (!collapsed) {
    for (const ta of card.querySelectorAll(".scene-section")) autoGrow(ta);
  }
}

/** Toggle a single card (from its caret or title) and refresh the "all" label. */
function toggleCard(card) {
  setCollapsed(card, !card.classList.contains("collapsed"));
  updateCollapseAllLabel();
}

/** Collapse everything if anything is expanded; otherwise expand everything. */
function toggleAll() {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  if (cards.length === 0) return;
  const anyExpanded = cards.some((c) => !c.classList.contains("collapsed"));
  for (const c of cards) setCollapsed(c, anyExpanded);
  updateCollapseAllLabel();
}

/** Keep the "Collapse all"/"Expand all" button label in sync with the cards. */
function updateCollapseAllLabel() {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  const allCollapsed =
    cards.length > 0 && cards.every((c) => c.classList.contains("collapsed"));
  el.collapseAllBtn.textContent = allCollapsed ? "Expand all" : "Collapse all";
  el.collapseAllBtn.disabled = cards.length === 0;
}

/** Snapshot of which scene cards are currently collapsed, by index. */
function readCollapsed() {
  return [...el.scenes.querySelectorAll(".scene-card")].map((c) =>
    c.classList.contains("collapsed")
  );
}

/**
 * @param {import("./scenes.js").Scene[]} scenes
 * @param {boolean[]} [collapsed]  per-index collapsed state to restore
 */
function renderScenes(scenes, collapsed = []) {
  el.scenes.textContent = "";
  const frag = document.createDocumentFragment();
  const cards = scenes.map((sc, i) => makeSceneCard(sc, i, scenes.length));
  cards.forEach((c) => frag.appendChild(c));
  el.scenes.appendChild(frag);
  // Apply collapsed state, then grow only the textareas that are visible
  // (a collapsed card has 0-height textareas until it is expanded).
  cards.forEach((c, i) => {
    if (collapsed[i]) setCollapsed(c, true);
  });
  for (const c of cards) {
    if (c.classList.contains("collapsed")) continue;
    for (const ta of c.querySelectorAll(".scene-section")) autoGrow(ta);
  }
  updateCollapseAllLabel();
}

/** Render the prompt-level (global) sections. */
function renderGlobals(globals) {
  el.global.textContent = "";
  globalTas = {};
  for (const key of GLOBAL_SECTIONS) {
    const labelRow = makeLabelRow(key);
    const tag = document.createElement("span");
    tag.className = "global-tag";
    tag.textContent = "global";
    labelRow.querySelector("label").after(tag);
    const hint = document.createElement("p");
    hint.className = "scene-hint";
    hint.textContent =
      "Shared by all scenes. A scene fills this in only to override it.";
    const { wrap, ta } = makeHighlightedTextarea(null, (globals && globals[key]) || "");
    globalTas[key] = ta;
    el.global.append(labelRow, hint, wrap);
    autoGrow(ta);
  }
}

function readGlobals() {
  const g = {};
  for (const key of GLOBAL_SECTIONS) g[key] = globalTas[key] ? globalTas[key].value : "";
  return g;
}

// ---------------------------------------------------------------------------
// Rating — prompt-level metadata (shown in the UI, never copied to the clipboard)
// ---------------------------------------------------------------------------
/** Render the 1..MAX_RATING star control for `value` (0 = unrated). */
function renderRating(value) {
  ratingValue = clampRating(value);
  el.rating.textContent = "";
  for (let i = 1; i <= MAX_RATING; i++) {
    const star = document.createElement("button");
    star.type = "button";
    star.className = "star" + (i <= ratingValue ? " on" : "");
    star.textContent = i <= ratingValue ? "★" : "☆";
    star.title =
      `Rate ${i} / ${MAX_RATING}` + (ratingValue === i ? " · click again to clear" : "");
    star.addEventListener("mouseenter", () => hotUpTo(i)); // hover preview
    // Clicking the current rating again clears it (back to unrated).
    star.addEventListener("click", () => setRating(ratingValue === i ? 0 : i));
    el.rating.appendChild(star);
  }
}

/** Light every star up to `n` during hover (preview of the value a click sets). */
function hotUpTo(n) {
  el.rating.querySelectorAll(".star").forEach((s, i) => s.classList.toggle("hot", i < n));
}

function clearHot() {
  for (const s of el.rating.querySelectorAll(".star")) s.classList.remove("hot");
}

function setRating(value) {
  renderRating(value);
  markDirty();
}

/** Prompt-level metadata read from the editor. */
function readMeta() {
  return { rating: ratingValue, loras: parseList(el.loras.value) };
}

/** Read-only star string (e.g. "★★★☆☆") for a saved rating; "" when unrated. */
function starsText(rating) {
  const r = clampRating(rating);
  return r ? "★".repeat(r) + "☆".repeat(MAX_RATING - r) : "";
}

/** Read the scenes currently shown in the editor, in order. */
function readScenes() {
  const scenes = [];
  for (const card of el.scenes.querySelectorAll(".scene-card")) {
    const scene = emptyScene();
    for (const ta of card.querySelectorAll(".scene-section")) {
      scene[ta.dataset.section] = ta.value;
    }
    scenes.push(scene);
  }
  return scenes;
}

function updateMeta() {
  const name = isNew || !current?.name ? "(unsaved)" : current.name;
  if (editorType === MINIMAX_TYPE) {
    const n = el.scenes.querySelectorAll(".scene-card").length;
    el.meta.textContent = `${name} · ${n} scene${n === 1 ? "" : "s"}`;
  } else {
    el.meta.textContent = `${name} · plain text`;
  }
}

// ---------------------------------------------------------------------------
// Dirty tracking
// ---------------------------------------------------------------------------
function snapshot() {
  return JSON.stringify({
    t: el.title.value,
    ty: el.type.value,
    m: readMeta(),
    b: el.bodyText.value,
    g: readGlobals(),
    s: readScenes(),
  });
}

function setDirty(b) {
  state.library.dirty = b;
  el.dirty.hidden = !b;
  el.saveBtn.disabled = !b;
}

function markDirty() {
  setDirty(isNew || snapshot() !== baseline);
  updateMeta();
}

function setBaseline() {
  baseline = snapshot();
}

// ---------------------------------------------------------------------------
// Editor state
// ---------------------------------------------------------------------------
function loadIntoEditor(entry) {
  el.empty.hidden = true;
  el.editor.hidden = false;
  el.title.value = entry.title || "";
  editorType = normalizeType(entry.type);
  el.type.value = editorType;
  renderRating(entry.meta ? entry.meta.rating : 0);
  el.loras.value = (entry.meta && entry.meta.loras ? entry.meta.loras : []).join(", ");
  if (editorType === MINIMAX_TYPE) {
    el.bodyText.value = "";
    renderGlobals(entry.globals || emptyGlobals());
    // Deep-copy scenes so edits don't mutate the stored entry until Save.
    renderScenes((entry.scenes && entry.scenes.length ? entry.scenes : [emptyScene()]).map((s) => ({ ...s })));
  } else {
    el.bodyText.value = entry.body || "";
    el.global.textContent = "";
    globalTas = {};
    el.scenes.textContent = "";
    autoGrow(el.bodyText);
  }
  applyTypeView(editorType);
  setBaseline();
  setDirty(false);
  updateMeta();
}

/** Show the fields that belong to the current type; hide the others. */
function applyTypeView(type) {
  const minimax = type === MINIMAX_TYPE;
  el.body.hidden = minimax;
  el.global.hidden = !minimax;
  el.globalHead.hidden = !minimax;
  el.scenes.hidden = !minimax;
  el.scenesFooter.hidden = !minimax;
  el.reviewBtn.hidden = !minimax; // whole-prompt review only applies to MiniMax
  updateGenButtons(); // sync Generate buttons for the now-visible fields
  // Fields just became visible; re-grow any textareas measured while hidden.
  regrowVisible();
}

/** Open the AI generator for the prompt-level globals; result fills the fields. */
function generateGlobals() {
  const g = readGlobals();
  openGlobalsAi({
    subjectDefs: g.subject_definitions || "",
    retention: g.retention_analysis || "",
    onApply: ({ subjectDefs, retention }) => {
      setGlobalValue("subject_definitions", subjectDefs);
      if ((retention ?? "").trim() !== "") setGlobalValue("retention_analysis", retention);
    },
  });
}

/** Set a global textarea's value and run its input pipeline (highlight/grow/dirty). */
function setGlobalValue(key, value) {
  const ta = globalTas[key];
  if (!ta) return;
  ta.value = value;
  ta.dispatchEvent(new Event("input", { bubbles: true }));
}

/** Open the globals generator scoped to a single scene's override fields. */
function generateSceneOverride(card) {
  const subjTa = card.querySelector('.scene-section[data-section="subject_definitions"]');
  const retTa = card.querySelector('.scene-section[data-section="retention_analysis"]');
  openGlobalsAi({
    subjectDefs: subjTa ? subjTa.value : "",
    retention: retTa ? retTa.value : "",
    onApply: ({ subjectDefs, retention }) => {
      if (subjTa) {
        subjTa.value = subjectDefs;
        subjTa.dispatchEvent(new Event("input", { bubbles: true }));
      }
      if (retTa) {
        retTa.value = retention;
        retTa.dispatchEvent(new Event("input", { bubbles: true }));
      }
      const group = card.querySelector(".scene-overrides");
      if (group) setOverridesShown(group, true); // keep the applied overrides visible
    },
  });
}

function clearEditor() {
  current = null;
  isNew = false;
  editorType = DEFAULT_TYPE;
  globalTas = {};
  el.global.textContent = "";
  el.scenes.textContent = "";
  el.bodyText.value = "";
  el.editor.hidden = true;
  el.empty.hidden = false;
  el.empty.dataset.mode = adapter.dirHandle ? "select" : "nofolder";
  setDirty(false);
}

function maybeDiscard() {
  if (!state.library.dirty) return true;
  return window.confirm("Discard unsaved changes to this prompt?");
}

function selectEntry(entry) {
  if (current && entry.name === current.name && !isNew) return;
  if (!maybeDiscard()) return;
  current = entry;
  isNew = false;
  state.library.selectedName = entry.name;
  loadIntoEditor(entry);
  renderList();
}

function newPrompt() {
  if (!adapter.dirHandle) {
    toast("Open a library folder first.", "warn");
    return;
  }
  if (!maybeDiscard()) return;
  current = {
    name: "",
    title: "",
    mtime: 0,
    type: DEFAULT_TYPE, // new prompts start as plain text; switchable to MiniMax
    meta: emptyMeta(),
    body: "",
    globals: emptyGlobals(),
    scenes: [],
    _handle: null,
  };
  isNew = true;
  state.library.selectedName = null;
  loadIntoEditor(current);
  setDirty(true); // a brand-new prompt is unsaved by definition
  renderList();
  el.title.focus();
}

/**
 * Switch the prompt type. Converting default -> MiniMax requires the current
 * text to be in MiniMax format (otherwise it fails and reverts). MiniMax ->
 * default flattens the scenes/globals into editable plain text.
 */
function onTypeChange() {
  const newType = el.type.value;
  if (newType === editorType) return;

  if (newType === MINIMAX_TYPE) {
    const body = el.bodyText.value;
    // Empty text -> start a fresh MiniMax scene. Only reject non-empty text
    // that isn't already in MiniMax format (nothing safe to migrate).
    if (body.trim() && !looksLikeMinimax(body)) {
      toast(
        "This text isn't in MiniMax H3 Ref2VA format (no scene/section structure), so it can't be converted.",
        "error"
      );
      el.type.value = editorType; // revert the dropdown
      return;
    }
    const { globals, scenes } = parseMinimaxBody(body);
    editorType = MINIMAX_TYPE;
    el.bodyText.value = "";
    renderGlobals(globals);
    renderScenes(scenes);
  } else {
    // Flatten the structured prompt into plain text (lossless + re-parseable).
    const body = serializeMinimaxBody(readGlobals(), readScenes().map(normalizeScene));
    editorType = DEFAULT_TYPE;
    el.bodyText.value = body;
    el.global.textContent = "";
    globalTas = {};
    el.scenes.textContent = "";
    autoGrow(el.bodyText);
  }
  applyTypeView(editorType);
  updateCollapseAllLabel();
  updateMeta();
  markDirty();
}

/** A unique "<base> copy" title that doesn't collide with existing prompts. */
function suggestCopyTitle(base) {
  const titles = new Set(state.library.entries.map((e) => e.title));
  const first = `${base} copy`;
  if (!titles.has(first)) return first;
  let n = 2;
  while (titles.has(`${base} copy ${n}`)) n++;
  return `${base} copy ${n}`;
}

/**
 * Duplicate the currently open prompt (its scenes as shown in the editor) into
 * a new, unsaved prompt with a suggested unique title. Nothing is written until
 * the user presses Save.
 */
function duplicate() {
  if (!current) return;
  const meta = readMeta();
  const base = (el.title.value || current.title || "prompt").trim() || "prompt";
  const isMinimax = editorType === MINIMAX_TYPE;
  current = {
    name: "",
    title: suggestCopyTitle(base),
    mtime: 0,
    type: editorType,
    meta,
    body: isMinimax ? "" : el.bodyText.value,
    globals: isMinimax ? readGlobals() : emptyGlobals(),
    scenes: isMinimax ? readScenes().map((s) => ({ ...s })) : [],
    _handle: null,
  };
  isNew = true;
  state.library.selectedName = null;
  loadIntoEditor(current);
  setDirty(true); // it's a new, unsaved copy
  renderList();
  el.title.focus();
  el.title.select();
  toast("Duplicated — review the title and Save.");
}

// ---------------------------------------------------------------------------
// Scene actions
// ---------------------------------------------------------------------------
function addScene() {
  const scenes = readScenes();
  // Collapse every existing scene so the freshly added one is the sole focus.
  const collapsed = scenes.map(() => true);
  scenes.push(emptyScene());
  collapsed.push(false); // the new scene starts expanded
  renderScenes(scenes, collapsed);
  el.scroll.scrollTop = el.scroll.scrollHeight;
  const cards = el.scenes.querySelectorAll(".scene-card");
  const last = cards[cards.length - 1];
  // Focus the first visible content field (overrides start hidden).
  last?.querySelector('.scene-section[data-section="summary"]')?.focus();
  markDirty();
}

/** Move a scene up (dir -1) or down (dir +1), swapping with its neighbour. */
function moveScene(card, dir) {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  const idx = cards.indexOf(card);
  if (idx < 0) return;
  const to = idx + dir;
  if (to < 0 || to >= cards.length) return;
  const collapsed = readCollapsed();
  const scenes = readScenes();
  // Swap the scene and its collapsed state with the neighbour.
  [scenes[idx], scenes[to]] = [scenes[to], scenes[idx]];
  [collapsed[idx], collapsed[to]] = [collapsed[to], collapsed[idx]];
  renderScenes(scenes, collapsed);
  el.scenes.querySelectorAll(".scene-card")[to]?.scrollIntoView({ block: "nearest" });
  markDirty();
}

/** Append a copy of a scene at the end, using its current editor content. */
function duplicateScene(card) {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  const idx = cards.indexOf(card);
  if (idx < 0) return;
  const collapsed = readCollapsed();
  const scenes = readScenes();
  scenes.push({ ...scenes[idx] }); // copy of the scene's sections, appended last
  collapsed.push(false); // the new copy starts expanded
  renderScenes(scenes, collapsed);
  el.scroll.scrollTop = el.scroll.scrollHeight;
  const newCard = el.scenes.querySelector(".scene-card:last-child");
  newCard?.querySelector('.scene-section[data-section="summary"]')?.focus();
  markDirty();
  toast("Scene duplicated.");
}

/** Non-empty sections of a scene as canonical text, for LLM context. */
function sceneContextText(scene) {
  return SECTION_ORDER.filter((k) => (scene[k] ?? "").trim() !== "")
    .map((k) => `${k}:\n${scene[k].trim()}`)
    .join("\n\n");
}

/** Text of a section only when it holds real content (not blank/"N/A"). */
function filledSection(v) {
  const t = (v ?? "").trim();
  return t && t.toUpperCase() !== "N/A" ? t : "";
}

/** True when a parsed scene has at least one recognized section with content. */
function sceneHasContent(scene) {
  return !!scene && SECTION_ORDER.some((k) => filledSection(scene[k]));
}

/** Non-empty CONTENT sections of a scene as canonical text (excludes globals). */
function contentContextText(scene) {
  return CONTENT_SECTIONS.filter((k) => filledSection(scene[k]))
    .map((k) => `${k}:\n${scene[k].trim()}`)
    .join("\n\n");
}

/**
 * Open the AI generator for a scene.
 * @param {HTMLElement} card
 * @param {{ improve?: boolean }} [opts]  improve=true refines the scene's current
 *   content; otherwise it's a fresh generation (no current content fed back, so
 *   retrying gives a different take).
 */
function generateScene(card, { improve = false } = {}) {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  const idx = cards.indexOf(card);
  const scene = sceneFromCard(card);
  const globals = readGlobals();

  // Always give the model the defined subjects so it reuses the <Subject N>
  // labels and never invents appearance.
  const subjects =
    filledSection(scene.subject_definitions) ||
    filledSection(globals.subject_definitions);

  const opts = {
    purpose: "scene.generate",
    title: improve ? "Improve scene with AI" : "Generate scene with AI",
    // Shown read-only in the modal AND sent as context, so you can see the
    // subject_definitions the model will use.
    reference: subjects
      ? { label: "subject_definitions (applied)", text: subjects }
      : undefined,
    resultHint: "(editable · canonical scene format)",
    acceptLabel: "Use as scene",
    // Only allow applying when the result parses into at least one known section.
    validate: (text) => sceneHasContent(parseMinimaxBody(text).scenes[0]),
    onApply: (text) => applySceneToCard(card, parseMinimaxBody(text).scenes[0] || {}),
  };

  if (improve) {
    // Refine the scene's current content.
    opts.context = contentContextText(scene);
    opts.contextLabel = "current scene content to improve/complete";
  } else if (idx > 0) {
    // Fresh generation of scene 2+: offer to continue from the previous scene
    // (its detailed description, or its summary as a fallback) for continuity.
    const prev = sceneFromCard(cards[idx - 1]);
    const prevContext =
      filledSection(prev.detailed_description) || filledSection(prev.summary);
    if (prevContext) {
      opts.continuation = {
        label: `Continue from the previous scene (Scene ${idx})`,
        contextLabel:
          "PREVIOUS scene (continuity reference ONLY — keep the same subjects, setting and state; do NOT reuse its wording, shots, events or actions — the new scene must show DIFFERENT action per the TASK)",
        context: prevContext,
      };
    }
  }

  openTextAi(opts);
}

/** Open the AI generator for the default plain-text prompt; fills the body field. */
function generateDefault() {
  openTextAi({
    purpose: "library.default.generate",
    title: "Generate prompt with AI",
    context: el.bodyText.value,
    contextLabel: "Current prompt to improve/complete",
    resultHint: "(editable · plain text)",
    acceptLabel: "Use as prompt",
    onApply: (text) => {
      el.bodyText.value = text;
      el.bodyText.dispatchEvent(new Event("input", { bubbles: true }));
      el.bodyText.focus();
    },
  });
}

/**
 * Assemble the whole-prompt context for an AI review: the shared
 * subject_definitions plus, per scene, its summary and detailed_description (the
 * "essentials" for a cross-scene consistency pass). A scene that overrides
 * subject_definitions has that override noted so mismatches surface. Empty
 * content is sent as "N/A" so the reviewer can flag it.
 */
function reviewContextText() {
  const globals = readGlobals();
  const scenes = readScenes();
  const parts = [
    `subject_definitions:\n${filledSection(globals.subject_definitions) || "N/A"}`,
  ];
  scenes.forEach((s, i) => {
    const block = [`===== SCENE ${i + 1} =====`];
    const override = filledSection(s.subject_definitions);
    if (override) block.push(`subject_definitions (scene override):\n${override}`);
    block.push(`summary:\n${filledSection(s.summary) || "N/A"}`);
    block.push(`detailed_description:\n${filledSection(s.detailed_description) || "N/A"}`);
    parts.push(block.join("\n\n"));
  });
  return parts.join("\n\n");
}

/** Open the whole-prompt reviewer (MiniMax only). Advisory report — nothing is applied. */
function reviewPrompt() {
  openReviewAi({
    purpose: "library.h3.review",
    title: "Review prompt with AI",
    contextLabel:
      "Prompt under review (read-only — subject_definitions + each scene's summary & detailed_description)",
    context: reviewContextText(),
  });
}

/**
 * Assemble the context for a single-scene review: the globals plus the current
 * scene and its immediate neighbours (summary + detailed_description each). The
 * CURRENT scene is marked explicitly so the reviewer/rewriter target only it.
 */
function sceneReviewContextText(cards, idx) {
  const globals = readGlobals();
  const cur = sceneFromCard(cards[idx]);
  const subj =
    filledSection(cur.subject_definitions) ||
    filledSection(globals.subject_definitions);
  const essentials = (s) =>
    `summary:\n${filledSection(s.summary) || "N/A"}\n\n` +
    `detailed_description:\n${filledSection(s.detailed_description) || "N/A"}`;

  const parts = [`subject_definitions:\n${subj || "N/A"}`];
  if (idx > 0) {
    parts.push(
      `===== PREVIOUS SCENE (Scene ${idx}) — context only =====\n\n${essentials(sceneFromCard(cards[idx - 1]))}`
    );
  }
  parts.push(
    `===== CURRENT SCENE (Scene ${idx + 1}) — the one under review =====\n\n${essentials(cur)}`
  );
  if (idx < cards.length - 1) {
    parts.push(
      `===== NEXT SCENE (Scene ${idx + 2}) — context only =====\n\n${essentials(sceneFromCard(cards[idx + 1]))}`
    );
  }
  return parts.join("\n\n");
}

/**
 * Open the single-scene reviewer for a card. Advisory report, plus "Apply
 * suggestions" which rewrites the scene's summary + detailed_description for the
 * user to confirm before it is applied to the card (audio/overrides untouched).
 */
function reviewScene(card) {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  const idx = cards.indexOf(card);
  if (idx < 0) return;
  const n = idx + 1;
  openReviewAi({
    purpose: "library.h3.scene-review",
    title: `Review scene ${n} with AI`,
    contextLabel: `Scene ${n} + neighbours (read-only)`,
    context: sceneReviewContextText(cards, idx),
    rewrite: {
      purpose: "scene.generate",
      instruction:
        `Rewrite ONLY the CURRENT scene (Scene ${n}), applying the review notes above. Keep the same <Subject N>/<Picture N> labels, subjects, setting, wardrobe and continuity with the previous and next scenes; introduce no new labels and no contradictions. Output ONLY these two sections in canonical format and nothing else:\n\nsummary:\n<text>\n\ndetailed_description:\n<text>`,
      acceptLabel: `Accept into scene ${n}`,
      validate: (text) => {
        const s = parseMinimaxBody(text).scenes[0] || {};
        return !!(filledSection(s.summary) || filledSection(s.detailed_description));
      },
      onApply: (text) => {
        const s = parseMinimaxBody(text).scenes[0] || {};
        applySceneToCard(card, {
          summary: s.summary ?? "",
          detailed_description: s.detailed_description ?? "",
        });
        toast(`Scene ${n} updated — review and save.`);
      },
    },
  });
}

/**
 * Fill a scene card from another scene (only non-empty sections overwrite).
 * @param {HTMLElement} card
 * @param {object} scene
 * @param {{ trustPrefix?: boolean }} [opts]  `trustPrefix` takes the summary's
 *   task-type prefix from `scene` when it has one — true for a paste (the user
 *   copied that prefix), false for AI output (the task type is not the model's
 *   call).
 */
function applySceneToCard(card, scene, { trustPrefix = false } = {}) {
  let overrideSet = false;
  for (const ta of card.querySelectorAll(".scene-section")) {
    const key = ta.dataset.section;
    const val = (scene[key] ?? "").trim();
    if (val === "") continue; // don't wipe existing fields with blanks
    if (key === "summary") {
      // The task type is the user's choice: the card's own checkboxes win unless
      // the source is trusted and carries a prefix of its own.
      const source = parsePrefix(scene[key]);
      const own = parsePrefix(ta.value).types;
      const types = trustPrefix && source.types.length ? source.types : own;
      ta.value = setPrefix(source.rest, types);
    } else {
      ta.value = scene[key];
    }
    ta.dispatchEvent(new Event("input", { bubbles: true })); // highlight+grow+dirty
    if (GLOBAL_SECTIONS.includes(key)) overrideSet = true;
  }
  if (overrideSet) {
    const group = card.querySelector(".scene-overrides");
    if (group) setOverridesShown(group, true);
  }
  setCollapsed(card, false); // make sure the applied result is visible
}

/** Enable/disable every "Generate" button (scenes + default body) with LLM state. */
function updateGenButtons() {
  const online = isLlmOnline();
  for (const b of el.editor.querySelectorAll(".scene-generate")) {
    b.disabled = !online;
    b.title = online
      ? b.dataset.genTitle || "Generate with AI"
      : "AI not connected — configure it in AI settings";
  }
}

function removeScene(card) {
  const cards = [...el.scenes.querySelectorAll(".scene-card")];
  const idx = cards.indexOf(card);
  if (idx < 0) return;
  if (!window.confirm(`Remove scene ${idx + 1}? This can't be undone.`)) return;
  const collapsed = readCollapsed();
  const scenes = readScenes();
  scenes.splice(idx, 1);
  collapsed.splice(idx, 1);
  if (scenes.length === 0) {
    scenes.push(emptyScene()); // always keep one
    collapsed.push(false);
  }
  renderScenes(scenes, collapsed);
  markDirty();
}

/** Read a scene from its card's textareas. */
function sceneFromCard(card) {
  const scene = emptyScene();
  for (const ta of card.querySelectorAll(".scene-section")) {
    scene[ta.dataset.section] = ta.value;
  }
  return scene;
}

/** Show the exact copied result of a scene (with global fallback and N/A). */
function previewScene(card) {
  const n = [...el.scenes.querySelectorAll(".scene-card")].indexOf(card) + 1;
  const text = sceneToText(sceneFromCard(card), readGlobals());
  openModal({ title: `Scene ${n} preview`, text, copyText: text });
}

/**
 * The whole MiniMax prompt as copyable text: every scene in canonical
 * "section:\n<value>" format (global fallback + "N/A" for blanks), each under a
 * "===== Scene N =====" header, in editor order.
 */
function minimaxPreviewText() {
  const globals = readGlobals();
  return [...el.scenes.querySelectorAll(".scene-card")]
    .map(
      (card, i) =>
        `===== Scene ${i + 1} =====\n\n${sceneToText(sceneFromCard(card), globals)}`
    )
    .join("\n\n\n");
}

/**
 * Preview (and copy) the whole prompt as it stands in the editor — every scene
 * for MiniMax, or the plain body for a default prompt. Reads the live editor, so
 * it reflects unsaved edits.
 */
function previewPrompt() {
  const text =
    editorType === MINIMAX_TYPE ? minimaxPreviewText() : el.bodyText.value;
  const title = el.title.value.trim() || (current && current.title) || "Prompt";
  openModal({ title: `${title} — preview`, text, copyText: text });
}

async function copyScene(card) {
  const scene = sceneFromCard(card);
  try {
    await navigator.clipboard.writeText(sceneToText(scene, readGlobals()));
    toast("Scene copied.");
  } catch {
    toast("Clipboard blocked by the browser.", "warn");
  }
}

/**
 * Fill this scene from the clipboard. The clipboard may hold a whole scene or
 * just a few `section:` blocks (in any spelling parseMinimaxBody accepts, so
 * text copied out of the model's UI or hand-written works too): each section
 * found lands in its own field and the rest of the scene is left untouched.
 */
async function pasteScene(card) {
  let text = "";
  try {
    text = await navigator.clipboard.readText();
  } catch {
    toast("Clipboard blocked by the browser.", "warn");
    return;
  }

  const parsedScenes = parseMinimaxBody(text).scenes;
  const parsed = parsedScenes[0] || {};
  if (!sceneHasContent(parsed)) {
    toast("No scene sections found in the clipboard.", "warn");
    return;
  }

  // Keep only the sections that actually carry something new. "Copy" writes the
  // RESOLVED sections (the global fallback already applied), so a pasted value
  // identical to the prompt-level global is not an override.
  const globals = readGlobals();
  const scene = {};
  for (const key of SECTION_ORDER) {
    const value = filledSection(parsed[key]);
    if (!value) continue;
    if (GLOBAL_SECTIONS.includes(key) && value === filledSection(globals[key])) continue;
    scene[key] = parsed[key];
  }
  const keys = Object.keys(scene);
  if (keys.length === 0) {
    toast("The clipboard only repeats the global sections — nothing to paste.");
    return;
  }

  // Never drop written content silently: confirm when the paste replaces some.
  // For the summary only the text past the task-type prefix counts as written.
  const existing = sceneFromCard(card);
  const replaced = keys.filter((key) =>
    isSectionFilled(key === "summary" ? parsePrefix(existing[key]).rest : existing[key])
  );
  if (replaced.length > 0) {
    const msg =
      `Replace ${replaced.length} section${replaced.length === 1 ? "" : "s"} ` +
      `already written in this scene?\n\n${replaced.join(", ")}`;
    if (!window.confirm(msg)) return;
  }

  applySceneToCard(card, scene, { trustPrefix: true });
  const n = [...el.scenes.querySelectorAll(".scene-card")].indexOf(card) + 1;
  const extra =
    parsedScenes.length > 1
      ? ` (first of ${parsedScenes.length} scenes in the clipboard)`
      : "";
  toast(
    `Pasted ${keys.length} section${keys.length === 1 ? "" : "s"} into scene ${n}${extra}.`
  );
}

// ---------------------------------------------------------------------------
// Prompt actions
// ---------------------------------------------------------------------------
async function save() {
  if (!current) return;
  const title = el.title.value;
  if (!title.trim()) {
    toast("Give the prompt a title.", "warn");
    el.title.focus();
    return;
  }
  const meta = readMeta();
  const type = editorType;

  // Build the per-type payload. For MiniMax, empty content sections are
  // persisted as "N/A"; global-capable sections stay raw so the fallback works.
  let payload;
  let scenes;
  let collapsed;
  if (type === MINIMAX_TYPE) {
    collapsed = readCollapsed();
    scenes = readScenes().map(normalizeScene);
    payload = { title, type, meta, globals: readGlobals(), scenes, body: "" };
  } else {
    payload = { title, type, meta, body: el.bodyText.value, globals: emptyGlobals(), scenes: [] };
  }

  try {
    const wasNew = isNew;
    await adapter.save(current, payload);
    isNew = false;
    if (wasNew) state.library.entries.push(current);
    state.library.entries.sort((a, b) =>
      a.title.localeCompare(b.title, undefined, { numeric: true })
    );
    state.library.selectedName = current.name;
    applyLibraryFilters();
    if (type === MINIMAX_TYPE) renderScenes(scenes, collapsed); // reflect "N/A" fills
    setBaseline();
    setDirty(false);
    updateMeta();
    renderList();
    updateCounts();
    toast("Prompt saved.");
  } catch (err) {
    toast("Could not save: " + err.message, "error");
  }
}

async function remove() {
  if (!current) return;
  if (isNew) {
    clearEditor();
    return;
  }
  if (
    !window.confirm(`Delete "${current.title}"? This removes the file from disk.`)
  ) {
    return;
  }
  try {
    await adapter.remove(current);
    const idx = state.library.entries.indexOf(current);
    if (idx >= 0) state.library.entries.splice(idx, 1);
    applyLibraryFilters();
    clearEditor();
    renderList();
    updateCounts();
    toast("Prompt deleted.");
  } catch (err) {
    toast("Could not delete: " + err.message, "error");
  }
}

// ---------------------------------------------------------------------------
// Opening the library folder
// ---------------------------------------------------------------------------
async function openLibrary(handle) {
  if (!maybeDiscard()) return;
  const entries = await adapter.listFolder(handle);
  state.library.entries = entries;
  state.library.folderName = handle.name;
  state.library.selectedName = null;
  state.library.search = el.search.value = "";
  current = null;
  isNew = false;

  el.folderName.textContent = handle.name;
  el.newBtn.disabled = false;
  applyLibraryFilters();
  clearEditor();
  renderList();
  updateCounts();

  await idbSet("libraryFolder", handle).catch(() => {});
}

async function openPicker() {
  try {
    const handle = await window.showDirectoryPicker({ mode: "readwrite" });
    el.reopenBtn.hidden = true;
    await openLibrary(handle);
  } catch (err) {
    if (err && err.name !== "AbortError")
      toast("Could not open library: " + err.message, "error");
  }
}

async function restore() {
  const handle = await idbGet("libraryFolder").catch(() => null);
  if (!handle) return;
  el.reopenBtn.hidden = false;
  el.reopenBtn.textContent = `Reopen "${handle.name}"`;
  el.reopenBtn.addEventListener("click", async () => {
    let perm = await handle.queryPermission({ mode: "readwrite" });
    if (perm !== "granted")
      perm = await handle.requestPermission({ mode: "readwrite" });
    if (perm === "granted") {
      el.reopenBtn.hidden = true;
      await openLibrary(handle);
    } else {
      toast("Permission denied.", "error");
    }
  });
}

exports.initLibrary = initLibrary;
exports.focusSearch = focusSearch;
exports.isDirty = isDirty;
exports.onShow = onShow;
  });
  register("js/storage/prompt-library-adapter.js", function (module, exports, require) {
const { parsePrompt, serializePrompt } = require("js/scenes.js");

/**
 * Disk access for the standalone Prompt Library (spec §1.2, §3.3).
 *
 * A separate folder from the images folder. One `.txt` per prompt; the filename
 * is the title. Each file holds one or more SCENES, each with the four fixed
 * sections (see scenes.js). Like the image adapter, this is the only place the
 * library touches the File System Access API.
 *
 * @typedef {Object} PromptEntry
 * @property {string}   name    "cookie-sketch.txt"
 * @property {string}   title   "cookie-sketch"
 * @property {number}   mtime
 * @property {string}   type    prompt type ("default" | "minimax-h3-ref2va")
 * @property {{rating:number, loras:string[]}} meta  prompt-level metadata (shown, never copied)
 * @property {string}   body    full prompt text (used by the "default" type)
 * @property {Record<string,string>} globals  global sections shared by scenes (minimax)
 * @property {import("../scenes.js").Scene[]} scenes  (minimax)
 * @property {FileSystemFileHandle|null} _handle
 */

/** Make a filesystem-safe title (no separators, no leading dots). */
function sanitizeTitle(t) {
  return t
    .replace(/[\/\\:*?"<>|\x00-\x1F]/g, "")
    .replace(/^\.+/, "")
    .replace(/\s+/g, " ")
    .trim();
}

async function atomicWrite(handle, text) {
  const w = await handle.createWritable();
  try {
    await w.write(text);
    await w.close(); // atomic swap (spec §7.1)
  } catch (err) {
    await w.abort();
    throw err;
  }
}

function makeEntry(name, handle, mtime, parsed) {
  return {
    name,
    title: name.replace(/\.txt$/i, ""),
    mtime,
    type: parsed.type,
    meta: parsed.meta,
    body: parsed.body,
    globals: parsed.globals,
    scenes: parsed.scenes,
    _handle: handle,
  };
}

class PromptLibraryAdapter {
  constructor() {
    /** @type {FileSystemDirectoryHandle|null} */
    this.dirHandle = null;
  }

  /**
   * @param {FileSystemDirectoryHandle} handle
   * @returns {Promise<PromptEntry[]>}
   */
  async listFolder(handle) {
    this.dirHandle = handle;
    const entries = [];
    for await (const [name, h] of handle.entries()) {
      if (h.kind !== "file") continue;
      if (name.startsWith(".")) continue; // skip dotfiles
      if (!name.toLowerCase().endsWith(".txt")) continue;
      const f = await h.getFile();
      const parsed = parsePrompt(await f.text());
      entries.push(makeEntry(name, h, f.lastModified, parsed));
    }
    entries.sort((a, b) =>
      a.title.localeCompare(b.title, undefined, { numeric: true })
    );
    return entries;
  }

  /**
   * Persist a prompt (all its scenes). Handles first-save (create) and rename
   * (write new, delete old) — both user-initiated via the Save button.
   * @param {PromptEntry} entry  mutated in place on success
   * @param {{ title: string, type: string, meta: {rating:number, loras:string[]}, body?: string, globals?: Record<string,string>, scenes?: import("../scenes.js").Scene[] }} fields
   */
  async save(entry, { title, type, meta, body, globals, scenes }) {
    const clean = sanitizeTitle(title);
    if (!clean) throw new Error("Title cannot be empty.");

    const content = serializePrompt({ type, meta, body, globals, scenes });
    const newName = clean + ".txt";

    if (entry._handle && newName === entry.name) {
      await atomicWrite(entry._handle, content);
    } else {
      await this.#assertNoCollision(newName);
      const nh = await this.dirHandle.getFileHandle(newName, { create: true });
      await atomicWrite(nh, content);
      if (entry._handle && entry.name && entry.name !== newName) {
        try {
          await this.dirHandle.removeEntry(entry.name); // complete the rename
        } catch {
          /* leaving the old file behind is safe; surface nothing */
        }
      }
      entry._handle = nh;
    }

    const f = await entry._handle.getFile();
    entry.name = newName;
    entry.title = clean;
    entry.mtime = f.lastModified;
    entry.type = type;
    entry.meta = meta;
    entry.body = body ?? "";
    entry.globals = globals || {};
    entry.scenes = scenes || [];
    return entry;
  }

  /** @param {PromptEntry} entry */
  async remove(entry) {
    if (!entry._handle || !entry.name) return;
    await this.dirHandle.removeEntry(entry.name);
  }

  async #assertNoCollision(newName) {
    let exists = false;
    try {
      await this.dirHandle.getFileHandle(newName);
      exists = true;
    } catch (e) {
      if (e && e.name !== "NotFoundError") throw e;
    }
    if (exists) {
      throw new Error(
        `A prompt named "${newName.replace(/\.txt$/i, "")}" already exists.`
      );
    }
  }
}

exports.PromptLibraryAdapter = PromptLibraryAdapter;
  });
  register("js/help-example.js", function (module, exports, require) {
/**
 * Reference example shown by the "Help" button in the Prompt Library.
 * Read-only documentation — rendered via textContent so the angle-bracket
 * tokens (<Subject 1>, <d>…</d>, etc.) are shown literally, not parsed as HTML.
 */
const EXAMPLE_PROMPT = `subject_definitions:
<Subject 1> is the coffee-shop environment in <Picture 1>, featuring an exposed brick wall, an orange tufted sofa with patterned pillows, a neon sign, and a wooden coffee table.
<Subject 2> is the fluffy white Samoyed in <Picture 2>, <Picture 3>, and <Picture 4>, with thick white fur, pointed ears, a dark nose, and a curved tail.
<Subject 3> is the young blonde woman in <Video 1>, with long blonde hair and a light-pink button-down shirt with rolled-up sleeves.
<Subject 4> is the young man in <Video 2>, with short wavy brown hair and a dark-grey hoodie with drawstrings.
<Audio 1> is the voice-timbre reference for <Subject 3> (S1), containing a spoken English vocal layer.

summary:
[reference generation + audio reference] The target video shows <Subject 3> eating a cookie in <Subject 1>. <Subject 4> enters with <Subject 2>, which lunges toward the cookie. The three-shot exchange uses <Audio 1> as the voice-timbre reference for <Subject 3> and ends with a canned audience laugh.

retention_analysis:
<Subject 1> (appears in [Shot 1], [Shot 2], [Shot 3]): fully_preserved - the exposed brick wall, orange tufted sofa, patterned pillows, neon sign, and wooden coffee table are retained.
<Subject 2> (appears in [Shot 1], [Shot 2]): fully_preserved - the Samoyed's thick white fur, pointed ears, dark nose, and curved tail are retained.
<Subject 3> (appears in [Shot 1], [Shot 2], [Shot 3]): fully_preserved - the blonde woman's identity, long hair, and light-pink shirt are retained.
<Subject 4> (appears in [Shot 1], [Shot 2]): fully_preserved - the young man's short wavy brown hair and dark-grey hoodie are retained.
<Audio 1>: reference - its vocal timbre guides the dialogue delivery of <Subject 3> without copying the original signal.

detailed_description:
The target video uses a realistic multi-camera sitcom style with warm indoor lighting.
[Shot 1] A medium shot establishes <Subject 1>, the coffee shop with its exposed brick wall, orange tufted sofa, patterned pillows, neon sign, and wooden coffee table. <Subject 3> (S1), the young woman with long blonde hair and a light-pink button-down shirt with rolled-up sleeves, sits on the sofa holding a chocolate-chip cookie. From the left, <Subject 4>, the young man with short wavy brown hair and a dark-grey hoodie with drawstrings, enters holding the leash of <Subject 2>, the thick-furred white Samoyed with pointed ears, a dark nose, and a curved tail. The dog lunges toward the cookie and pulls the leash taut. <Subject 3> (S1) jerks her hand back and, using the clear youthful voice timbre referenced from <Audio 1>, exclaims with light annoyance, <d>[English] Hey! Watch your dog!</d> She closes her lips and guards the cookie while <Subject 4> pulls the dog back.
[Shot 2] At 00:03.000, the shot cuts to a close-up of <Subject 4> (S2), the young man in the dark-grey hoodie from Shot 1, sitting beside <Subject 3> on the sofa and holding <Subject 2> securely in his arms. <Subject 4> (S2) says in a casual young male voice with a playful tone and an easy conversational pace, <d>[English] He just likes cookies more than me.</d> He closes his mouth into an apologetic smile and strokes the dog's thick white fur.
[Shot 3] At 00:05.000, the shot cuts to a close-up of <Subject 3> (S1), the blonde woman in the light-pink shirt from Shot 1. Her annoyance softens as she looks toward the Samoyed. <Subject 3> (S1) replies in the same clear youthful voice referenced from <Audio 1> with an amused cadence, <d>[English] Well, he has good taste at least.</d> She smiles and raises the cookie in a small toast-like gesture. A classic canned audience laugh begins immediately after the line and continues through the final frame.

overall_soundscape:
Soft indoor coffee-shop room tone continues throughout the scene.

non_diegetic_music:
N/A`;

exports.EXAMPLE_PROMPT = EXAMPLE_PROMPT;
  });
  register("js/prompt-guide.js", function (module, exports, require) {
const { SECTION_HELP } = require("js/section-help.js");
const { EXAMPLE_PROMPT } = require("js/help-example.js");
const { escapeHtml } = require("js/highlight.js");

/**
 * The full Full-Reference Mode Rewrite Output Format Guide, as a trusted HTML
 * fragment for the "Prompt guide" modal. Sections 2, 3, and 5 reuse the same
 * per-section help fragments; the complete example (section 7) is injected from
 * EXAMPLE_PROMPT (escaped). Everything else is authored here with angle-bracket
 * tokens escaped so they render literally.
 */

const intro = `
<p><strong>Full-Reference Mode Rewrite Output Format Guide.</strong> This explains
how rewrite outputs are organized and written in full-reference mode.</p>
<p>Write all six rewrite sections in English. Preserve the original language only
for dialogue and lyrics inside <code>&lt;d&gt;</code> and for text visibly present
in the scene.</p>
<p><strong>Description detail:</strong> Make <code>detailed_description</code> as
detailed and explicit as possible. For each shot, clearly establish the current
composition, subject appearance and position, environment and lighting, actions
and state changes, camera movement, current sound, and the points where
referenced content actually appears or takes effect. Avoid reducing the
description to a plot summary or a list of reference relationships.</p>
<blockquote>The basic formats for shots, camera movement, speakers, dialogue, and
ordinary sound are shared with the Video Prompt Writing Guide
(T2VA / I2VA / FL2VA / L2VA). This guide focuses on the reference labels,
analysis sections, and format differences specific to full-reference mode.</blockquote>
`;

const section1 = `
<table>
  <thead><tr><th>Section</th><th>Purpose</th></tr></thead>
  <tbody>
    <tr><td><code>subject_definitions</code></td><td>Defines referenced content and its reference labels</td></tr>
    <tr><td><code>summary</code></td><td>Summarizes the task type, target video, and main reference relationships</td></tr>
    <tr><td><code>retention_analysis</code></td><td>Describes how referenced content is preserved, transferred, or reused</td></tr>
    <tr><td><code>detailed_description</code></td><td>Describes visuals, actions, shots, sound, and dialogue in playback order</td></tr>
    <tr><td><code>overall_soundscape</code></td><td>Summarizes ambience and physical sounds</td></tr>
    <tr><td><code>non_diegetic_music</code></td><td>Describes background music audible only to the audience</td></tr>
  </tbody>
</table>
`;

const section6 = `
<p>The definitions of these two sound categories follow the Video Prompt Writing
Guide (T2VA / I2VA / FL2VA / L2VA).</p>
<p><code>overall_soundscape</code> summarizes ambience and physical sounds across
the full video. Dialogue, singing, and sound events synchronized to a particular
shot remain in <code>detailed_description</code>:</p>
<pre class="code">overall_soundscape: Quiet indoor room tone and a low ventilation hum continue throughout the video.</pre>
<p><code>non_diegetic_music</code> describes background music that the characters
cannot hear and that is audible only to the audience. When music is present, state
its instrumentation, tempo, and dynamic development:</p>
<pre class="code">non_diegetic_music: A restrained solo-piano score at a slow tempo, with sustained low cello underneath and no swell.</pre>
<p>When reference audio is used, state its copy or reference relationship only in
the section that matches the audible layer: ambience and sound effects belong in
<code>overall_soundscape</code>, while audience-only score belongs in
<code>non_diegetic_music</code>. If the same audio provides both kinds of content,
describe the corresponding relationship in each section:</p>
<pre class="code">overall_soundscape: The copied ambience layer from &lt;Audio 1&gt; continues throughout the target video.
non_diegetic_music: &lt;Audio 2&gt; is directly reused as the complete audience-only score.</pre>
<p>Write complete dialogue and lyrics only inside <code>&lt;d&gt;</code> in
<code>detailed_description</code>; do not repeat them in these two sections.</p>
`;

const section7 = `
<details>
  <summary>Show the complete example</summary>
  <pre class="code">${escapeHtml(EXAMPLE_PROMPT)}</pre>
</details>
`;

const PROMPT_GUIDE_HTML = [
  intro,
  "<h3>1. Overall structure</h3>",
  section1,
  "<h3>2. Reference labels and definitions (subject_definitions)</h3>",
  SECTION_HELP.subject_definitions,
  "<h3>3. summary</h3>",
  SECTION_HELP.summary,
  "<h3>4. retention_analysis</h3>",
  SECTION_HELP.retention_analysis,
  "<h3>5. detailed_description</h3>",
  SECTION_HELP.detailed_description,
  "<h3>6. overall_soundscape and non_diegetic_music</h3>",
  section6,
  "<h3>7. Complete example</h3>",
  section7,
].join("\n");

exports.PROMPT_GUIDE_HTML = PROMPT_GUIDE_HTML;
  });
  register("js/section-help.js", function (module, exports, require) {
/**
 * Per-section guidance shown by the "?" button next to a section label.
 * Values are trusted, hand-authored HTML fragments (no user input). Literal
 * angle-bracket tokens such as <Subject N> / <Audio 1> / <d>…</d> are escaped
 * as entities so they render as text rather than being parsed as HTML tags.
 */

const summary = `
<p>This section uses one short English paragraph to summarize the target video
and its reference relationships. It begins with a square-bracketed task-type
prefix:</p>
<pre class="code">[reference generation] ...
[video editing + reference generation + audio reuse] ...</pre>
<p>Choose task types according to the actual role each reference asset plays in
the target video:</p>
<table>
  <thead>
    <tr><th>Task type</th><th>When to use it</th></tr>
  </thead>
  <tbody>
    <tr>
      <td><code>keyframe completion</code></td>
      <td>An image serves as the target video's first frame, keyframe, last
      frame, edited keyframe, or another concrete frame anchor</td>
    </tr>
    <tr>
      <td><code>reference generation</code></td>
      <td>An image, video, or audio asset provides generation guidance for a
      character, scene, style, action, camera movement, storyboard, and so on,
      without serving as a concrete frame or as the source video being edited
      or continued</td>
    </tr>
    <tr>
      <td><code>video editing</code></td>
      <td>An existing source video is directly modified; editing an image or
      generating between still keyframes does not belong to this type</td>
    </tr>
    <tr>
      <td><code>video continuation</code></td>
      <td>New content continues, extends, resumes, or transitions from an
      existing source video</td>
    </tr>
    <tr>
      <td><code>audio reuse</code></td>
      <td>The same audio signal is reused in full or in part</td>
    </tr>
    <tr>
      <td><code>audio reference</code></td>
      <td>The audio signal is not copied directly; only its music style, timbre,
      dialogue or lyric content, sound-effect texture, beat, or continuity is
      referenced</td>
    </tr>
  </tbody>
</table>
<p>When a task satisfies multiple relationships, combine the task types with
<code> + </code> and do not repeat a type. For example, continuing from a source
video while using an image as the last frame is written as
<code>[video continuation + keyframe completion]</code>. Editing a source video
while retaining its original audio may be written as
<code>[video editing + audio reuse]</code>.</p>
<p>The mere presence of video or audio does not automatically create a
corresponding task type. If a reference video provides only camera movement,
cuts, or rhythm, it normally belongs to <code>reference generation</code>. Use
<code>video editing</code> or <code>video continuation</code> only when that
video is directly edited or continued.</p>
<p>When editing a source video, use <code>audio reuse</code> as well if its
original audio remains audible. When continuing a source video without directly
copying the audio signal, use <code>audio reference</code> if the new audio only
continues the original track's audible characteristics.</p>
<p>The summary uses the previously defined <code>&lt;Subject N&gt;</code>,
<code>&lt;Picture N&gt;</code>, <code>&lt;Video N&gt;</code>, and
<code>&lt;Audio N&gt;</code> labels to describe the main subjects, shot flow,
and roles of the reference assets. Do not introduce new reference labels in this
section.</p>
<p>For video-editing tasks, begin the summary after the task-type prefix
with:</p>
<pre class="code">The target video is an edited version of &lt;Video 1&gt;.</pre>
`;

const detailed_description = `
<p>This is the main body of a full-reference rewrite. It describes visuals,
actions, sound, and dialogue shot by shot in target-video playback order and
inserts reference labels where they apply.</p>

<h4>Basic format</h4>
<p>The basic format follows the Video Prompt Writing Guide
(T2VA / I2VA / FL2VA / L2VA):</p>
<ul>
  <li>Write the body in English. Preserve the original language of dialogue,
  lyrics, and visible text.</li>
  <li><code>[Shot 1]</code> marks the opening shot and has no timestamp. Later
  shots use <code>[Shot N] At MM:SS.mmm, ...</code> to mark cut times.</li>
  <li>Write camera movement as natural English within the current shot,
  including movement type, amplitude, and speed when they need to be
  expressed.</li>
  <li>Give vocal sources stable <code>(S1)</code>, <code>(S2)</code>, and
  subsequent IDs. Write dialogue and lyrics as
  <code>&lt;d&gt;[Language] ...&lt;/d&gt;</code>.</li>
  <li>Use <code>&lt;scenetrans&gt;</code>, <code>&lt;cutoff&gt;</code>, and the
  corresponding continuity descriptions for dialogue crossing a cut, speech
  truncated by the video ending, and continuous audio across shots.</li>
</ul>
<p>For complete rules and examples covering camera vocabulary, group speech,
voice-over, dialogue across cuts, and visible text, see the Video Prompt Writing
Guide (T2VA / I2VA / FL2VA / L2VA).</p>

<h4>Full-reference mode differences</h4>
<table>
  <thead>
    <tr><th>Dimension</th><th>T2VA</th><th>Full-reference mode</th></tr>
  </thead>
  <tbody>
    <tr>
      <td>Main field</td>
      <td><code>integrated_multimodal_description</code></td>
      <td><code>detailed_description</code></td>
    </tr>
    <tr>
      <td>Style opening</td>
      <td>Written after <code>[Shot 1]</code></td>
      <td>Established in one or two English sentences before
      <code>[Shot 1]</code></td>
    </tr>
    <tr>
      <td>Reference information</td>
      <td>Does not use full-reference labels</td>
      <td>Inserts <code>&lt;Subject N&gt;</code>, <code>&lt;Picture N&gt;</code>,
      <code>&lt;Video N&gt;</code>, and <code>&lt;Audio N&gt;</code> at their
      first appearance and where their roles apply</td>
    </tr>
    <tr>
      <td>Audio relationships</td>
      <td>Describes the target video's own sound</td>
      <td>Cites <code>&lt;Audio N&gt;</code> in the corresponding shot or audio
      phase and states whether the signal is copied or referenced</td>
    </tr>
  </tbody>
</table>
<p>Opening example:</p>
<pre class="code">The target video is in a cinematic, literary music-video style with soft lighting and a slightly desaturated color palette.
[Shot 1] The scene opens in a crowded urban street...
[Shot 2] At 00:09.000, the shot cuts to an extreme close-up...</pre>
<p>For generation tasks, <code>detailed_description</code> is normally 350-500
English words. Dialogue-dense content prioritizes fitting the complete spoken
timeline rather than mechanically reaching a word count. Video-editing
descriptions scale with the complexity of the source video and do not have to
follow the generation-task range. A single shot does not automatically justify a
shorter description; distribute detail across multiple shots according to their
information load.</p>

<h4>Using reference labels in shots</h4>
<p>At the first clear appearance of an important <code>&lt;Subject N&gt;</code>,
describe its referenced characteristics, position in the frame, and current
action within what is actually visible in the shot. Continue using the same
label in later shots without redefining what the label represents.</p>
<p>Use natural phrasing for concrete frame anchors:</p>
<pre class="code">the shot begins from &lt;Picture 1&gt;
the shot's keyframe corresponds to &lt;Picture 2&gt;
the shot ends on &lt;Picture 3&gt;</pre>
<p>When editing or continuing an original video, cite <code>&lt;Video N&gt;</code>
naturally where its source state, structure, or continuation relationship
applies. Cite <code>&lt;Audio N&gt;</code> in the shot or semantic phase where the
audio relationship is active.</p>

<h4>Speakers, audio sources, and dialogue</h4>
<p>The basic speaker-ID and <code>&lt;d&gt;</code> formats follow T2VA. When a
referenced subject physically speaks, retain both the visual reference label and
the speaker ID:</p>
<pre class="code">&lt;Subject 2&gt; (S1) turns toward the woman and says, &lt;d&gt;[English] Last summer, I went to my grandfather's house. He talked about you.&lt;/d&gt;</pre>
<p><code>&lt;Subject N&gt;</code> identifies the referenced subject, while
<code>(Sx)</code> identifies the actual speaker. When the subject speaks, write
<code>&lt;Subject N&gt; (Sx)</code>. If the same subject speaks off-screen, keep
the same form and mark it as <code>off-screen</code>. When the speaker does not
correspond to a defined subject, use a stable voice description followed by
<code>(Sx)</code>.</p>
<p>When verbal content is only a cue within a directly reused BGM or complete
soundtrack, and no person, character, narrator, or other independent vocal source
physically produces it, use <code>&lt;Audio N&gt;</code> as the audible source and
do not invent an additional <code>(Sx)</code>. If a concrete person, character,
narrator, or other independent vocal source produces the voice, assign and reuse
<code>(Sx)</code> for that source:</p>
<pre class="code">When &lt;Audio 1&gt; reaches the phrase &lt;d&gt;[English] I'm lonely lonely lonely lonely lonely I'm lonely&lt;/d&gt;, &lt;Subject 1&gt; performs the corresponding hand gesture without becoming a separate speaker source.</pre>
<p>When dialogue, narration, or lyrics from reference audio are directly reused,
or when the input prompt explicitly requests their reperformance, preserve the
exact source words and original language inside <code>&lt;d&gt;</code>. Write
<code>[unclear]</code> for unintelligible spans instead of guessing or
paraphrasing them. Standardize punctuation to the basic written marks needed to
express the sentence, such as <code>,</code>, <code>.</code>, <code>?</code>, and
<code>!</code>; remove repeated tildes, emoji, bullets, and repeated or
decorative punctuation. End complete statements, questions, and exclamations with
<code>.</code>, <code>?</code>, or <code>!</code> respectively before
<code>&lt;/d&gt;</code>.</p>
<p>When only timbre, rhythm, emotion, or delivery is referenced, do not carry the
original dialogue from the reference audio into the target video.</p>
<p>Assign <code>(Sx)</code> once according to the order of actual vocal events in
the target video. Reuse the corresponding ID at every actual vocal event in
<code>detailed_description</code>; an <code>&lt;Audio N&gt;</code> definition bound
to a target speaker in <code>subject_definitions</code> also reuses the same
<code>(Sx)</code> but never assigns a new one independently. Do not write
<code>(Sx)</code> in <code>retention_analysis</code>. Verbal cues that exist only
within a directly reused BGM or complete soundtrack use
<code>&lt;Audio N&gt;</code>; voices physically produced by a concrete person,
character, narrator, or other independent vocal source use <code>(Sx)</code>.</p>
`;

const overall_soundscape = `
<p>The definitions of the two sound categories follow the Video Prompt Writing
Guide (T2VA / I2VA / FL2VA / L2VA).</p>
<p><code>overall_soundscape</code> summarizes ambience and physical sounds across
the full video. Dialogue, singing, and sound events synchronized to a particular
shot remain in <code>detailed_description</code>:</p>
<pre class="code">overall_soundscape: Quiet indoor room tone and a low ventilation hum continue throughout the video.</pre>
<p>When reference audio is used, state its copy or reference relationship only in
the section that matches the audible layer: ambience and sound effects belong in
<code>overall_soundscape</code>, while audience-only score belongs in
<code>non_diegetic_music</code>. If the same audio provides both kinds of
content, describe the corresponding relationship in each section:</p>
<pre class="code">overall_soundscape: The copied ambience layer from &lt;Audio 1&gt; continues throughout the target video.
non_diegetic_music: &lt;Audio 2&gt; is directly reused as the complete audience-only score.</pre>
<p>Write complete dialogue and lyrics only inside <code>&lt;d&gt;</code> in
<code>detailed_description</code>; do not repeat them in this section.</p>
`;

const non_diegetic_music = `
<p>The definitions of the two sound categories follow the Video Prompt Writing
Guide (T2VA / I2VA / FL2VA / L2VA).</p>
<p><code>non_diegetic_music</code> describes background music that the characters
cannot hear and that is audible only to the audience. When music is present,
state its instrumentation, tempo, and dynamic development:</p>
<pre class="code">non_diegetic_music: A restrained solo-piano score at a slow tempo, with sustained low cello underneath and no swell.</pre>
<p>When reference audio is used, state its copy or reference relationship only in
the section that matches the audible layer: ambience and sound effects belong in
<code>overall_soundscape</code>, while audience-only score belongs in
<code>non_diegetic_music</code>. If the same audio provides both kinds of
content, describe the corresponding relationship in each section:</p>
<pre class="code">overall_soundscape: The copied ambience layer from &lt;Audio 1&gt; continues throughout the target video.
non_diegetic_music: &lt;Audio 2&gt; is directly reused as the complete audience-only score.</pre>
<p>Write complete dialogue and lyrics only inside <code>&lt;d&gt;</code> in
<code>detailed_description</code>; do not repeat them in this section.</p>
`;

const subject_definitions = `
<p>Full-reference rewrites use four types of labels to identify the source and
role of referenced content:</p>
<table>
  <thead><tr><th>Label</th><th>Meaning</th></tr></thead>
  <tbody>
    <tr><td><code>&lt;Subject N&gt;</code></td><td>Visible content abstracted from reference assets that can be reused or modified in the target video</td></tr>
    <tr><td><code>&lt;Picture N&gt;</code></td><td>A reference image used as a concrete target frame or shot-planning anchor</td></tr>
    <tr><td><code>&lt;Video N&gt;</code></td><td>A reference video that provides an editing source, continuation starting point, or whole-video temporal structure</td></tr>
    <tr><td><code>&lt;Audio N&gt;</code></td><td>An audio signal that is copied or referenced</td></tr>
  </tbody>
</table>
<p class="note">Once a reference label is assigned to a piece of content, it keeps
the same meaning across <code>subject_definitions</code>, <code>summary</code>,
<code>retention_analysis</code>, <code>detailed_description</code>, and the audio
sections.</p>
<p><code>subject_definitions</code> defines each piece of referenced content that
must be tracked separately later, such as a person, an environment, a source
video's structure, or an audio track. Give each item its own line and explain
what its label denotes, its reference role, and the main features to follow; name
the corresponding source asset when its provenance needs to be made explicit. If
<code>&lt;Picture N&gt;</code> or <code>&lt;Video N&gt;</code> only identifies the
source of another referenced item and will not be analyzed or used separately
later, cite it inside that item's definition without adding a separate line.
<code>retention_analysis</code> records where each referenced item appears and
whether it is fully preserved, partially preserved, transferred, or reused.</p>

<h4>2.1 &lt;Subject N&gt;</h4>
<p><code>&lt;Subject N&gt;</code> is used for reusable visible content, including:</p>
<ul>
  <li>People, animals, or objects</li>
  <li>Scenes, backgrounds, or environments</li>
  <li>Clothing, props, interfaces, or visual effects</li>
  <li>Styles, actions, expressions, or poses</li>
</ul>
<p>It represents a content unit that will actually be used in the target video,
rather than the source file itself. One subject may be defined by multiple
reference assets, and one reference asset may provide multiple subjects.</p>
<pre class="code">&lt;Subject 1&gt; is the young woman in &lt;Picture 1&gt;, with long dark hair, a blue cardigan, and a thin silver necklace.</pre>
<p>When the same subject comes from multiple assets, combine the sources and state
what each asset provides:</p>
<pre class="code">&lt;Subject 1&gt; is the woman whose appearance comes from &lt;Picture 1&gt; and whose walking motion comes from &lt;Video 1&gt;.</pre>

<h4>2.2 &lt;Picture N&gt;</h4>
<p>Use a standalone <code>&lt;Picture N&gt;</code> when the reference image itself
serves as a shot's first frame, keyframe, last frame, edited keyframe, or
composition anchor:</p>
<pre class="code">&lt;Picture 2&gt; is the first frame of [Shot 1], showing a woman seated beside a café window.</pre>
<p>If an image is used only to define a character, scene, costume, or style, do
not create a standalone picture entry. Instead, cite the image source inside the
corresponding <code>&lt;Subject N&gt;</code> definition.</p>
<p>When an image acts as a storyboard or shot-planning reference, state which
shots it maps to and what planning information it provides:</p>
<pre class="code">&lt;Picture 3&gt; is a storyboard reference for [Shot 1] and [Shot 2], defining their viewpoint, subject placement, and shot order.</pre>

<h4>2.3 &lt;Video N&gt;</h4>
<p><code>&lt;Video N&gt;</code> is reserved for whole-video relationships, such as:</p>
<ul>
  <li>Editing an original video</li>
  <li>Continuing from the end of an original video</li>
  <li>Referencing the original video's camera movement, cuts, rhythm, or temporal structure</li>
</ul>
<pre class="code">&lt;Video 1&gt; is the source video for the target video edit.</pre>
<p>If a person, object, scene, action, or effect from a reference video is reused
as visible content, it still belongs under <code>&lt;Subject N&gt;</code>.
<code>&lt;Video N&gt;</code> identifies the asset or structural source and does not
replace subject labels.</p>

<h4>2.4 &lt;Audio N&gt;</h4>
<p><code>&lt;Audio N&gt;</code> represents a standalone audio asset or an enabled
synchronized audio track from a reference video. Common uses include:</p>
<ul>
  <li>Copying all or part of an audio signal</li>
  <li>Referencing a background-music style</li>
  <li>Referencing a speaker's voice timbre and delivery</li>
  <li>Using dialogue, lyrics, or sound effects from the original audio</li>
  <li>Referencing beat, rhythm, or audio continuity</li>
</ul>
<p>When an <code>&lt;Audio N&gt;</code> explicitly corresponds to a target speaker,
reuse that speaker's global ID in the definition: write
<code>&lt;Subject N&gt; (Sx)</code> when the speaker maps to a defined subject, or
use a stable voice description followed by <code>(Sx)</code> otherwise. The ID
comes from the target video's global speaker order and is not independently
assigned or renumbered in the audio definition. See Section 5.4 for the
speaker-numbering rules:</p>
<pre class="code">&lt;Audio 1&gt; is the voice-timbre reference for &lt;Subject 1&gt; (S1).</pre>
<p>When one audio asset serves multiple roles, describe those roles in one natural
sentence rather than creating additional subsections.</p>

<h4>2.5 Visual and Audio Tracks from the Same Reference Video</h4>
<p><code>&lt;Video N&gt;</code> and <code>&lt;Audio N&gt;</code> are numbered
independently. Each index indicates only the label's order within its own
category and does not encode a pairing between the two categories. The same
reference video may therefore correspond to <code>&lt;Video 1&gt;</code> and
<code>&lt;Audio 2&gt;</code>; different indices do not prevent them from coming
from the same source asset.</p>
<p>An ordinary reference video does not create <code>&lt;Audio N&gt;</code> merely
because the file contains sound.</p>
<p>An <code>&lt;Audio N&gt;</code> definition primarily states the audio's role and
does not have to name the <code>&lt;Video N&gt;</code> it comes from. State the
shared source only when needed to remove provenance ambiguity, for example:</p>
<pre class="code">&lt;Video 1&gt; is the source video for the target video edit.
&lt;Audio 2&gt; is the synchronized audio track of &lt;Video 1&gt; and is reused in the target video.</pre>
`;

const retention_analysis = `
<p>This section describes how each piece of referenced content is preserved,
transferred, copied, or referenced in the target video. Use one line for each
reference label and preserve the meaning established in
<code>subject_definitions</code>.</p>
<h4>4.1 Visible content</h4>
<p><code>&lt;Subject N&gt;</code>, <code>&lt;Picture N&gt;</code>, and
<code>&lt;Video N&gt;</code> use the following relationship markers. These markers
are fixed English values in the output format:</p>
<table>
  <thead><tr><th>Relationship marker</th><th>Meaning</th></tr></thead>
  <tbody>
    <tr><td><code>fully_preserved</code></td><td>The defined role of the referenced content is fully preserved</td></tr>
    <tr><td><code>partially_preserved</code></td><td>The referenced content is still used, but some defined characteristics are changed or only partially retained</td></tr>
    <tr><td><code>attribute_transfer</code></td><td>Referenced characteristics are transferred to a different identifiable target subject</td></tr>
    <tr><td><code>weak_reference</code></td><td>Only broad similarity in style, category, composition, or atmosphere is retained</td></tr>
  </tbody>
</table>
<p>Subject entry:</p>
<pre class="code">&lt;Subject 1&gt; (appears in [Shot 1], [Shot 3]): fully_preserved - ...</pre>
<p>Picture entry:</p>
<pre class="code">&lt;Picture 2&gt; ([Shot 1] first frame): fully_preserved - ...</pre>
<p>Video-structure entry:</p>
<pre class="code">&lt;Video 1&gt; (cut and pacing structure): weak_reference - ...</pre>
<h4>4.2 Audio</h4>
<p><code>&lt;Audio N&gt;</code> uses the following relationship markers:</p>
<table>
  <thead><tr><th>Relationship marker</th><th>Meaning</th></tr></thead>
  <tbody>
    <tr><td><code>fully_copy</code></td><td>The complete source audio serves as the target video's complete final audio track</td></tr>
    <tr><td><code>partially_copy</code></td><td>Only part of the timeline or selected audio layers are copied, or other sounds are added, removed, or replaced after copying</td></tr>
    <tr><td><code>reference</code></td><td>The signal is not copied directly; only timbre, rhythm, music style, dialogue content, or sound texture is referenced</td></tr>
    <tr><td><code>weak_reference</code></td><td>Only broad similarity in category or atmosphere is retained</td></tr>
  </tbody>
</table>
<pre class="code">&lt;Audio 1&gt;: fully_copy - &lt;Audio 1&gt; is reused 1:1 as the target video's complete final audio track.</pre>
<pre class="code">&lt;Audio 2&gt;: reference - the target speaker follows &lt;Audio 2&gt;'s voice timbre and measured delivery without copying the original signal.</pre>
<p>Choose each relationship marker only within the reference role already defined
for that label in <code>subject_definitions</code>. Do not treat newly added
actions, backgrounds, or plot events in the target video as losses of reference
fidelity.</p>
`;

/** @type {Record<string, string>} section name -> HTML help fragment */
const SECTION_HELP = {
  subject_definitions,
  summary,
  retention_analysis,
  detailed_description,
  overall_soundscape,
  non_diegetic_music,
};

exports.SECTION_HELP = SECTION_HELP;
  });
  register("js/highlight.js", function (module, exports, require) {
/**
 * Token highlighting for the prompt fields. Because a <textarea> cannot render
 * styled spans, we mirror its text into a backdrop <div> where key tokens are
 * wrapped in <mark>. Three token classes:
 *
 *   hl-shot     [Shot 1], [Shot 2] ...      (a bracket starting with "Shot")
 *   hl-bracket  other [ ... ] tokens        (task-type prefix, [English], [unclear])
 *   hl-angle    < ... > tokens              (<Subject 1>, <Audio 1>, <d>, </d> ...)
 */

function escapeHtml(s) {
  return String(s).replace(
    /[&<>]/g,
    (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" }[c])
  );
}

function highlightHtml(raw) {
  const re = /(<[^>\n]*>)|(\[[^\]\n]*\])/g;
  let out = "";
  let last = 0;
  let m;
  while ((m = re.exec(raw)) !== null) {
    out += escapeHtml(raw.slice(last, m.index));
    const tok = m[0];
    let cls;
    if (m[1]) cls = "hl-angle";
    else cls = /^\[\s*Shot\b/i.test(tok) ? "hl-shot" : "hl-bracket";
    out += `<mark class="${cls}">${escapeHtml(tok)}</mark>`;
    last = re.lastIndex;
  }
  out += escapeHtml(raw.slice(last));
  return out;
}

exports.escapeHtml = escapeHtml;
exports.highlightHtml = highlightHtml;
  });
  register("js/task-types.js", function (module, exports, require) {
/**
 * Task types for the summary section's square-bracketed prefix
 * (e.g. `[video editing + reference generation + audio reuse]`).
 *
 * The checkbox UI drives this prefix; the prefix lives inside the summary text,
 * so nothing extra is persisted — on reload the checkboxes are re-derived from
 * the leading `[...]` of the summary.
 */

const TASK_TYPES = [
  "keyframe completion",
  "reference generation",
  "video editing",
  "video continuation",
  "audio reuse",
  "audio reference",
];

const TASK_TYPE_HINTS = {
  "keyframe completion":
    "An image serves as the target video's first frame, keyframe, last frame, edited keyframe, or another concrete frame anchor",
  "reference generation":
    "An image, video, or audio asset provides generation guidance for a character, scene, style, action, camera movement, storyboard, and so on, without serving as a concrete frame or as the source video being edited or continued",
  "video editing":
    "An existing source video is directly modified; editing an image or generating between still keyframes does not belong to this type",
  "video continuation":
    "New content continues, extends, resumes, or transitions from an existing source video",
  "audio reuse": "The same audio signal is reused in full or in part",
  "audio reference":
    "The audio signal is not copied directly; only its music style, timbre, dialogue or lyric content, sound-effect texture, beat, or continuity is referenced",
};

/**
 * Split a summary into its leading `[...]` task-type prefix and the rest.
 * @param {string} text
 * @returns {{ types: string[], rest: string }}
 */
function parsePrefix(text) {
  const m = (text ?? "").match(/^\s*\[([^\]]*)\]\s*/);
  if (!m) return { types: [], rest: text ?? "" };
  const types = m[1]
    .split("+")
    .map((s) => s.trim())
    .filter(Boolean);
  return { types, rest: (text ?? "").slice(m[0].length) };
}

/**
 * Rewrite the leading task-type prefix of a summary. An empty selection removes
 * the prefix entirely.
 * @param {string} text
 * @param {string[]} chosen  task types, already in the desired order
 * @returns {string}
 */
function setPrefix(text, chosen) {
  const { rest } = parsePrefix(text);
  if (!chosen.length) return rest;
  return `[${chosen.join(" + ")}] ${rest}`;
}

exports.TASK_TYPES = TASK_TYPES;
exports.TASK_TYPE_HINTS = TASK_TYPE_HINTS;
exports.parsePrefix = parsePrefix;
exports.setPrefix = setPrefix;
  });
  register("js/llm/llm-config.js", function (module, exports, require) {
const { idbGet, idbSet } = require("js/storage/idb.js");

/**
 * Global, app-wide LLM configuration.
 *
 * This is intentionally NOT tied to any one section: image prompts, the prompt
 * library, and future features all read the same settings to talk to a local
 * (or remote) OpenAI-compatible endpoint (LM Studio, Ollama, llama.cpp, ...).
 *
 * Persisted in IndexedDB as CONFIG only (base URL, model, key, temperature) —
 * never prompt or image content, so disk stays the single source of truth for
 * content. NOTE: an API key, if set, is stored in plaintext in IndexedDB; for
 * local servers no key is needed, so leave it empty.
 */

const IDB_KEY = "llmConfig";

/** Sensible default for LM Studio's local server. */
const DEFAULT_LLM_CONFIG = {
  baseUrl: "http://localhost:1234/v1",
  model: "",
  apiKey: "",
  temperature: 0.7,
  // Send `cache_prompt: false` so the server rebuilds context from our messages
  // each call instead of reusing the previous request's KV cache. Prevents a
  // prior image from "leaking" into later text generations in LM Studio.
  independentContext: true,
};

/**
 * Live, app-wide config. Read this object anywhere; change it only through
 * `saveLlmConfig` so the persisted copy stays in sync.
 */
const llmConfig = { ...DEFAULT_LLM_CONFIG };

/** Trim and drop any trailing slash so paths can be appended cleanly. */
function normalizeBaseUrl(url) {
  return String(url || "").trim().replace(/\/+$/, "");
}

/** Load persisted config into `llmConfig`. Call once at startup. */
async function loadLlmConfig() {
  const saved = await idbGet(IDB_KEY).catch(() => null);
  if (saved && typeof saved === "object") Object.assign(llmConfig, saved);
  llmConfig.baseUrl = normalizeBaseUrl(llmConfig.baseUrl);
  return llmConfig;
}

/** Merge a patch into the config and persist it. */
async function saveLlmConfig(patch) {
  Object.assign(llmConfig, patch);
  llmConfig.baseUrl = normalizeBaseUrl(llmConfig.baseUrl);
  await idbSet(IDB_KEY, { ...llmConfig });
  return llmConfig;
}

/** True when there is enough config to attempt a call (base URL + model). */
function isLlmConfigured() {
  return !!(llmConfig.baseUrl && llmConfig.model);
}

/**
 * Live connectivity status, updated by the last probe (see refreshLlmStatus).
 * "off" = not configured, "checking" = probing, "warn" = configured but
 * unreachable, "on" = connected. Features enable AI actions off this.
 */
const llmStatus = { state: "off" };
const statusListeners = new Set();

/** Subscribe to status changes; returns an unsubscribe function. */
function onLlmStatusChange(cb) {
  statusListeners.add(cb);
  return () => statusListeners.delete(cb);
}

/** Set the current status and notify subscribers. */
function setLlmStatus(state) {
  llmStatus.state = state;
  for (const cb of statusListeners) {
    try {
      cb(state);
    } catch {
      /* a listener error must not break the others */
    }
  }
}

/** True when the last probe found the LLM reachable. */
function isLlmOnline() {
  return llmStatus.state === "on";
}

exports.DEFAULT_LLM_CONFIG = DEFAULT_LLM_CONFIG;
exports.llmConfig = llmConfig;
exports.loadLlmConfig = loadLlmConfig;
exports.saveLlmConfig = saveLlmConfig;
exports.isLlmConfigured = isLlmConfigured;
exports.llmStatus = llmStatus;
exports.onLlmStatusChange = onLlmStatusChange;
exports.setLlmStatus = setLlmStatus;
exports.isLlmOnline = isLlmOnline;
  });
  register("js/text-ai.js", function (module, exports, require) {
const { chat } = require("js/llm/llm-client.js");
const { isLlmOnline } = require("js/llm/llm-config.js");
const { listTemplates, getSelectedTemplateId, setSelectedTemplate, getTemplateValue, getTemplateModel } = require("js/llm/llm-prompts.js");
const { toast } = require("js/toast.js");

/**
 * Generic text-generation modal used by the Prompt Library Generate features
 * (MiniMax scene, Default plain-text prompt, and future ones).
 *
 * A caller opens it for a template PURPOSE with an optional context string; the
 * modal sends system + user (+ context) to the LLM and hands the RAW result back
 * via `onApply`, so each caller applies it its own way (parse into a scene, set a
 * text field, …). Purpose-specific input UIs (e.g. loading images) live in the
 * caller, not here.
 */

const $ = (id) => document.getElementById(id);
const GEN_TIMEOUT_MS = 120000;

let el;
/** @type {{purpose:string,onApply:(t:string)=>void,context?:string,contextLabel?:string,validate?:(t:string)=>boolean}|null} */
let cfg = null;
let inflight = null;
let baseHint = "(editable)";
/** Conversation so far (null before the first generation). Refine appends turns. */
let history = null;

function initTextAi() {
  el = {
    root: $("textGenModal"),
    title: $("textGenTitle"),
    closeBtn: $("textGenCloseBtn"),
    template: $("textGenTemplate"),
    system: $("textGenSystem"),
    user: $("textGenUser"),
    contWrap: $("textGenContWrap"),
    contCheck: $("textGenCont"),
    contLabel: $("textGenContLabel"),
    refWrap: $("textGenRefWrap"),
    refLabel: $("textGenRefLabel"),
    ref: $("textGenRef"),
    ctxNote: $("textGenCtxNote"),
    runBtn: $("textGenRunBtn"),
    refineBtn: $("textGenRefineBtn"),
    stopBtn: $("textGenStopBtn"),
    status: $("textGenStatus"),
    resultHint: $("textGenResultHint"),
    result: $("textGenResult"),
    acceptBtn: $("textGenAcceptBtn"),
  };

  el.closeBtn.addEventListener("click", close);
  el.root.querySelector(".modal-backdrop").addEventListener("click", close);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      close();
    }
  });

  el.runBtn.addEventListener("click", run);
  el.refineBtn.addEventListener("click", refine);
  el.stopBtn.addEventListener("click", abortInflight);
  el.acceptBtn.addEventListener("click", accept);
  el.template.addEventListener("change", onTemplateChange);
  el.result.addEventListener("input", () => syncButtons());
}

/**
 * `continuation` (optional) shows a checkbox; when ticked, its context is added
 * to the request so the model can build on prior content (e.g. a previous scene).
 *
 * `validate(text)` (optional): when it returns false, the accept button is
 * disabled and the result hint warns — so an unparseable result must be fixed.
 *
 * @param {{ purpose:string, title:string, context?:string, contextLabel?:string,
 *   resultHint?:string, acceptLabel?:string, onApply:(text:string)=>void,
 *   continuation?:{ label:string, contextLabel:string, context:string },
 *   validate?:(text:string)=>boolean }} opts
 */
function openTextAi(opts) {
  if (!isLlmOnline()) {
    toast("AI not connected — check AI settings.", "warn");
    return;
  }
  cfg = opts;
  el.title.textContent = opts.title || "Generate with AI";
  baseHint = opts.resultHint || "(editable)";
  el.resultHint.textContent = baseHint;
  el.acceptBtn.textContent = opts.acceptLabel || "Use";
  // Optional "continue from …" checkbox (on by default when offered).
  if (opts.continuation) {
    el.contLabel.textContent = opts.continuation.label;
    el.contCheck.checked = true;
    el.contWrap.hidden = false;
  } else {
    el.contWrap.hidden = true;
  }
  // Optional read-only reference (e.g. the subject_definitions being applied).
  if (opts.reference && (opts.reference.text || "").trim()) {
    el.refLabel.textContent = opts.reference.label || "Reference";
    el.ref.value = opts.reference.text;
    el.refWrap.hidden = false;
  } else {
    el.refWrap.hidden = true;
    el.ref.value = "";
  }
  populateTemplateSelect();
  const sel = getSelectedTemplateId(opts.purpose);
  el.template.value = sel;
  applyTemplate(sel);
  el.result.value = "";
  history = null; // fresh session — no conversation yet
  el.runBtn.textContent = "Generate";
  el.refineBtn.hidden = true; // appears after the first generation
  updateCtxNote();
  setBusy(false);
  setStatus("Ready.", "");
  el.root.hidden = false;
}

function close() {
  abortInflight();
  el.root.hidden = true;
}

function populateTemplateSelect() {
  el.template.textContent = "";
  for (const t of listTemplates(cfg.purpose)) {
    const o = document.createElement("option");
    o.value = t.id;
    o.textContent = t.name;
    el.template.appendChild(o);
  }
}

function applyTemplate(id) {
  el.system.value = getTemplateValue(cfg.purpose, id, "system");
  el.user.value = getTemplateValue(cfg.purpose, id, "user");
}

function onTemplateChange() {
  applyTemplate(el.template.value);
  setSelectedTemplate(cfg.purpose, el.template.value);
}

function updateCtxNote() {
  el.ctxNote.textContent =
    cfg.context && cfg.context.trim()
      ? "The current content is sent as context to improve/complete."
      : "Generating from your instructions.";
}

/** Build the first-turn messages from the fields + context (fresh generation). */
function buildFreshMessages() {
  // Reference context first, then the user's brief LAST, so the model treats
  // the brief as the task to perform rather than as more context to echo.
  const parts = [];
  if (cfg.reference && (cfg.reference.text || "").trim()) {
    parts.push(`${cfg.reference.label}:\n${cfg.reference.text.trim()}`);
  }
  const ctx = (cfg.context || "").trim();
  // A pre-structured context (with its own internal labels) passes an empty
  // contextLabel and is included as-is; otherwise it gets a wrapping label.
  if (ctx) parts.push(cfg.contextLabel ? `${cfg.contextLabel}:\n${ctx}` : ctx);
  if (cfg.continuation && el.contCheck.checked && cfg.continuation.context.trim()) {
    parts.push(`${cfg.continuation.contextLabel}:\n${cfg.continuation.context.trim()}`);
  }
  if (el.user.value.trim()) parts.push(`TASK (do this now):\n${el.user.value.trim()}`);
  const userText = parts.join("\n\n") || el.user.value;

  const messages = [];
  if (el.system.value.trim()) messages.push({ role: "system", content: el.system.value });
  messages.push({ role: "user", content: userText });
  return messages;
}

/** Send a message list; sets the result and returns the assistant text (null on error). */
async function send(messages) {
  abortInflight();
  const ctrl = new AbortController();
  inflight = ctrl;
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
  setBusy(true);
  setStatus("Generating…", "");
  try {
    const text = await chat(messages, {
      signal: ctrl.signal,
      model: getTemplateModel(cfg.purpose, el.template.value),
    });
    el.result.value = text.trim();
    setStatus(
      el.result.value ? "Done." : "The model returned an empty response.",
      el.result.value ? "ok" : "warn"
    );
    return el.result.value;
  } catch (err) {
    setStatus(err.message, "error");
    return null;
  } finally {
    clearTimeout(timer);
    inflight = null;
    setBusy(false);
  }
}

/** Once a result exists, reveal Refine and relabel the primary button. */
function afterGenerated() {
  el.runBtn.textContent = "Regenerate";
  el.refineBtn.hidden = false;
}

/** Fresh generation: rebuild from the fields, discarding any prior conversation. */
async function run() {
  const messages = buildFreshMessages();
  const text = await send(messages);
  if (text != null) {
    history = [...messages, { role: "assistant", content: el.result.value }];
    afterGenerated();
  }
}

/** Refine: keep the conversation and send the current user prompt as a new turn. */
async function refine() {
  if (!history) return run();
  // Use the (possibly hand-edited) result as the last assistant turn.
  const base = history.slice();
  if (base.length && base[base.length - 1].role === "assistant") {
    base[base.length - 1] = { role: "assistant", content: el.result.value };
  }
  const instruction = el.user.value.trim() || "Refine the previous result.";
  const messages = [...base, { role: "user", content: instruction }];
  const text = await send(messages);
  if (text != null) {
    history = [...messages, { role: "assistant", content: el.result.value }];
    afterGenerated();
  }
}

function accept() {
  const text = el.result.value;
  if (!text.trim()) return;
  cfg.onApply?.(text);
  toast("Applied — review and save.");
  close();
}

function abortInflight() {
  if (inflight) {
    inflight.abort();
    inflight = null;
  }
}

function setBusy(busy) {
  el.stopBtn.hidden = !busy;
  el.refineBtn.disabled = busy;
  syncButtons(busy); // handles runBtn + acceptBtn
}

function syncButtons(busy = false) {
  el.runBtn.disabled = busy;
  const text = el.result.value;
  const hasResult = text.trim().length > 0;
  const valid = !cfg?.validate || (hasResult && cfg.validate(text));
  el.acceptBtn.disabled = busy || !hasResult || !valid;
  // Warn in the result hint when there is content that can't be parsed.
  el.resultHint.textContent =
    cfg?.validate && hasResult && !valid
      ? `${baseHint} — ⚠ can't be parsed; fix it to enable "${cfg.acceptLabel || "Use"}"`
      : baseHint;
}

function setStatus(text, kind) {
  el.status.textContent = text;
  el.status.className = "llm-status" + (kind ? " " + kind : "");
}

exports.initTextAi = initTextAi;
exports.openTextAi = openTextAi;
  });
  register("js/llm/llm-client.js", function (module, exports, require) {
const { llmConfig } = require("js/llm/llm-config.js");

/**
 * The ONLY module that performs LLM network calls (mirrors the storage-adapter
 * rule: no feature fetches a model endpoint directly). It speaks the OpenAI-
 * compatible REST protocol, so the same code works with LM Studio, Ollama
 * (`:11434/v1`), llama.cpp, Jan, vLLM, and cloud OpenAI — only the base URL and
 * model change, both read from the global `llmConfig`.
 *
 * Every feature (generate / complete / improve prompts, ...) should build its
 * messages and call `chat()` here.
 */

function authHeaders() {
  const h = { "Content-Type": "application/json" };
  if (llmConfig.apiKey) h.Authorization = `Bearer ${llmConfig.apiKey}`;
  return h;
}

function endpoint(path) {
  const base = String(llmConfig.baseUrl || "").replace(/\/+$/, "");
  if (!base) throw new Error("No LLM base URL configured — open AI settings.");
  return base + path;
}

/** A cross-origin (CORS) block and a down server both surface as a TypeError. */
function networkError(err) {
  // An aborted fetch (our timeout) throws a DOMException named "AbortError".
  if (err && err.name === "AbortError") {
    return new Error(
      `Timed out reaching the LLM at "${llmConfig.baseUrl}" — is the server ` +
        `running and the URL correct?`
    );
  }
  return new Error(
    `Could not reach the LLM at "${llmConfig.baseUrl}". Is the server running ` +
      `and is CORS enabled? (${err.message})`
  );
}

/**
 * Send a chat completion and return the assistant's text.
 *
 * `content` may be a plain string, or — for vision models — an array of OpenAI
 * content parts, e.g. `[{type:"text",text}, {type:"image_url",image_url:{url}}]`
 * where `url` can be a `data:` URL (base64), which is how we send images.
 *
 * @param {{role:"system"|"user"|"assistant", content:string|object[]}[]} messages
 * @param {{ temperature?: number, model?: string, signal?: AbortSignal,
 *           responseFormat?: object }} [opts]
 * @returns {Promise<string>}
 */
async function chat(messages, opts = {}) {
  const model = opts.model || llmConfig.model;
  if (!model) throw new Error("No model selected — open AI settings.");

  const body = {
    model,
    messages,
    temperature: opts.temperature ?? llmConfig.temperature ?? 0.7,
    stream: false,
  };
  // llama.cpp/LM Studio extension: don't reuse the previous request's cached
  // context (avoids a prior image bleeding into a fresh generation). Unknown to
  // servers that don't support it, which simply ignore it.
  if (llmConfig.independentContext) body.cache_prompt = false;
  if (opts.responseFormat) body.response_format = opts.responseFormat;

  let res;
  try {
    res = await fetch(endpoint("/chat/completions"), {
      method: "POST",
      headers: authHeaders(),
      body: JSON.stringify(body),
      signal: opts.signal,
    });
  } catch (err) {
    throw networkError(err);
  }
  if (!res.ok) {
    const detail = await res.text().catch(() => "");
    throw new Error(`LLM error ${res.status}: ${detail.slice(0, 300)}`);
  }
  const data = await res.json();
  return data?.choices?.[0]?.message?.content ?? "";
}

/**
 * List model ids from the endpoint (GET /models) to populate the settings
 * picker. Throws on failure so the caller can show the reason.
 * @param {{ signal?: AbortSignal }} [opts]
 * @returns {Promise<string[]>}
 */
async function listModels(opts = {}) {
  let res;
  try {
    res = await fetch(endpoint("/models"), {
      headers: authHeaders(),
      signal: opts.signal,
    });
  } catch (err) {
    throw networkError(err);
  }
  if (!res.ok) throw new Error(`LLM error ${res.status}`);
  const data = await res.json();
  const list = Array.isArray(data?.data) ? data.data : [];
  return list
    .map((m) => m.id)
    .filter(Boolean)
    .sort((a, b) => a.localeCompare(b));
}

/**
 * Probe the endpoint for the settings "Test connection" button and the status
 * indicator.
 * @param {{ signal?: AbortSignal }} [opts]
 * @returns {Promise<{ok: boolean, message: string, models: string[]}>}
 */
async function testConnection(opts = {}) {
  try {
    const models = await listModels(opts);
    return {
      ok: true,
      models,
      message: models.length
        ? `Connected — ${models.length} model(s) available.`
        : "Connected, but no models are loaded on the server.",
    };
  } catch (err) {
    return { ok: false, models: [], message: err.message };
  }
}

exports.chat = chat;
exports.listModels = listModels;
exports.testConnection = testConnection;
  });
  register("js/llm/llm-prompts.js", function (module, exports, require) {
const { idbGet, idbSet } = require("js/storage/idb.js");
const { DEFAULT_TYPE, MINIMAX_TYPE, promptTypeLabel } = require("js/prompt-types.js");
const { DEFAULT_PROMPTS } = require("js/llm/llm-default-prompts.js");

/**
 * Named prompt TEMPLATES used by AI features.
 *
 * Model:
 *  - A "purpose" (e.g. `image.generate`) defines the fields a template holds
 *    (system + user) and a built-in, read-only "Default".
 *  - Each purpose has zero or more USER templates (named bundles of those field
 *    values), plus the built-in Default. At use time a feature picks one.
 *  - The last-picked template id per purpose is remembered (`selected`).
 *
 * Purposes are placed in a hierarchy so the manager sidebar mirrors the app:
 *
 *   area ("Image prompts" | "Prompt Library")
 *     └─ promptType (Prompt Library only: the library prompt type, e.g. H3)
 *          └─ purpose ("Scene", "Subject definitions", "Section refiner", …)
 *
 * Add a purpose here and it shows up automatically under its area/type. Different
 * purposes can back very different Generate UIs (a scene generator vs. one that
 * loads images from disk); that behavior lives in the feature module — this file
 * only stores the prompt templates.
 *
 * Persisted in IndexedDB as config only (never content).
 *
 * A template may also pin its own `model` (empty = use the global model from AI
 * settings). LM Studio loads it on demand when a request names a model that
 * isn't loaded (needs Just-In-Time model loading enabled), so switching models
 * per template can slow that first call.
 *
 * @typedef {{key:string,label:string,description:string}} FieldDef
 * @typedef {{id:string,area:string,promptType:(string|null),label:string,description:string,fields:FieldDef[],default:Record<string,string>}} PurposeDef
 * @typedef {{id:string,name:string,values:Record<string,string>,model?:string}} UserTemplate
 */

const IDB_KEY = "llmTemplates";
const LEGACY_KEY = "llmPrompts"; // single-value model, migrated on first load

/** Reserved id of the built-in, read-only template. */
const DEFAULT_TEMPLATE_ID = "default";

/** Built-in default prompts for a purpose (from the generated map). */
const def = (id) => DEFAULT_PROMPTS[id] || {};

/** @type {PurposeDef[]} */
const PURPOSE_DEFS = [
  {
    id: "image.generate",
    area: "Image prompts",
    promptType: null,
    label: "Generate",
    description:
      "Used by the ✨ Generate button on an image to caption it with a vision model.",
    fields: [
      {
        key: "system",
        label: "System prompt",
        description: "The model's role/instructions.",
      },
      {
        key: "user",
        label: "User prompt",
        description: "The request sent alongside the image.",
      },
    ],
    default: def("image.generate"),
  },
  {
    id: "scene.generate",
    area: "Prompt Library",
    promptType: MINIMAX_TYPE,
    label: "Scene",
    description:
      "Used by the ✨ Generate button on a MiniMax H3 Ref2VA scene.",
    fields: [
      {
        key: "system",
        label: "System prompt",
        description: "Teaches the model the canonical scene/section format.",
      },
      {
        key: "user",
        label: "User prompt",
        description: "Your brief. The current scene is sent as context to refine.",
      },
    ],
    default: def("scene.generate"),
  },
  {
    id: "library.h3.subject",
    area: "Prompt Library",
    promptType: MINIMAX_TYPE,
    label: "Subject definitions",
    description:
      "Used by the globals ✨ Generate: describes a reference image as one " +
      "subject_definitions line for a <Subject N> or <Picture N> label.",
    fields: [
      {
        key: "system",
        label: "System prompt",
        description: "Teaches the single-line subject_definitions format.",
      },
      {
        key: "user",
        label: "User prompt",
        description: "Base instruction; the target label is appended per image.",
      },
    ],
    default: def("library.h3.subject"),
  },
  {
    id: "library.h3.review",
    area: "Prompt Library",
    promptType: MINIMAX_TYPE,
    label: "Review",
    description:
      "Used by the 🔍 Review button: reviews the WHOLE prompt (globals + all " +
      "scenes) for cross-scene consistency and improvements. Advisory only.",
    fields: [
      {
        key: "system",
        label: "System prompt",
        description: "The reviewer's role and the H3 review checklist.",
      },
      {
        key: "user",
        label: "User prompt",
        description: "Your review brief. The whole prompt is sent as context.",
      },
    ],
    default: def("library.h3.review"),
  },
  {
    id: "library.h3.scene-review",
    area: "Prompt Library",
    promptType: MINIMAX_TYPE,
    label: "Scene review",
    description:
      "Used by the 🔍 Review button on a scene: reviews ONE scene against its " +
      "neighbours + globals; 'Apply suggestions' can rewrite the scene.",
    fields: [
      {
        key: "system",
        label: "System prompt",
        description: "The reviewer's role and the single-scene review checklist.",
      },
      {
        key: "user",
        label: "User prompt",
        description: "Your review brief. The scene + its neighbours are sent as context.",
      },
    ],
    default: def("library.h3.scene-review"),
  },
  {
    id: "library.default.generate",
    area: "Prompt Library",
    promptType: DEFAULT_TYPE,
    label: "Generate",
    description:
      "Used by the ✨ Generate button on a Default (plain-text) prompt.",
    fields: [
      {
        key: "system",
        label: "System prompt",
        description: "The model's role/instructions.",
      },
      {
        key: "user",
        label: "User prompt",
        description: "Your brief. The current prompt is sent as context to refine.",
      },
    ],
    default: def("library.default.generate"),
  },
];

function getPurpose(id) {
  return PURPOSE_DEFS.find((p) => p.id === id) || null;
}

/**
 * The purpose hierarchy for the manager sidebar, in first-seen order:
 *   [{ area, groups: [{ type, label, purposes: PurposeDef[] }] }]
 * A group with `type: null` (label null) is a flat list directly under the area;
 * otherwise `label` is the prompt-type heading (e.g. "MiniMax H3 Ref2VA").
 */
function promptTree() {
  const areas = [];
  for (const p of PURPOSE_DEFS) if (!areas.includes(p.area)) areas.push(p.area);
  return areas.map((area) => {
    const groups = [];
    for (const p of PURPOSE_DEFS.filter((x) => x.area === area)) {
      const type = p.promptType || null;
      let g = groups.find((x) => x.type === type);
      if (!g) {
        g = { type, label: type ? promptTypeLabel(type) : null, purposes: [] };
        groups.push(g);
      }
      g.purposes.push(p);
    }
    return { area, groups };
  });
}

/**
 * Live, app-wide store: `{ [purposeId]: { selected: string, templates: UserTemplate[] } }`.
 * The built-in Default is NOT stored here; it always comes from PURPOSE_DEFS.
 */
const llmTemplates = {};

const clone = (o) => JSON.parse(JSON.stringify(o));

function persist() {
  return idbSet(IDB_KEY, clone(llmTemplates));
}

/** Ensure every known purpose has a well-formed entry. */
function normalize() {
  for (const p of PURPOSE_DEFS) {
    const s = llmTemplates[p.id];
    if (!s || typeof s !== "object") {
      llmTemplates[p.id] = { selected: DEFAULT_TEMPLATE_ID, templates: [], defaultModel: "" };
      continue;
    }
    if (!Array.isArray(s.templates)) s.templates = [];
    if (!s.selected) s.selected = DEFAULT_TEMPLATE_ID;
    if (typeof s.defaultModel !== "string") s.defaultModel = ""; // model for built-in Default
  }
}

/** Carry a customized single-value config (old model) into a "Custom" template. */
async function migrateFromLegacy() {
  const old = await idbGet(LEGACY_KEY).catch(() => null);
  if (!old || typeof old !== "object") return;
  const p = getPurpose("image.generate");
  const sys = old["image.generate.system"];
  const usr = old["image.generate.user"];
  const changed =
    (sys != null && sys !== p.default.system) ||
    (usr != null && usr !== p.default.user);
  if (changed) {
    llmTemplates["image.generate"] = {
      selected: "t_migrated",
      templates: [
        {
          id: "t_migrated",
          name: "Custom",
          values: { system: sys ?? p.default.system, user: usr ?? p.default.user },
        },
      ],
    };
  }
}

/** Load persisted templates. Call once at startup. */
async function loadLlmTemplates() {
  const saved = await idbGet(IDB_KEY).catch(() => null);
  if (saved && typeof saved === "object") Object.assign(llmTemplates, saved);
  else await migrateFromLegacy();
  normalize();
  return llmTemplates;
}

/**
 * Persist the full edited structure (from the templates manager). Validates each
 * `selected` still points at an existing template, else falls back to Default.
 * @param {Record<string, {selected:string, templates:UserTemplate[]}>} draft
 */
async function saveLlmTemplates(draft) {
  for (const p of PURPOSE_DEFS) {
    const d = (draft && draft[p.id]) || {};
    const templates = Array.isArray(d.templates)
      ? d.templates.map((t) => cleanTemplate(p, t))
      : [];
    let selected = d.selected || DEFAULT_TEMPLATE_ID;
    if (
      selected !== DEFAULT_TEMPLATE_ID &&
      !templates.some((t) => t.id === selected)
    ) {
      selected = DEFAULT_TEMPLATE_ID;
    }
    llmTemplates[p.id] = { selected, templates, defaultModel: (d.defaultModel || "").trim() };
  }
  await persist();
  return llmTemplates;
}

/** Keep only known fields; ensure id/name are present. */
function cleanTemplate(purpose, t) {
  const values = {};
  for (const f of purpose.fields) values[f.key] = (t.values && t.values[f.key]) || "";
  return { id: t.id, name: t.name || "Untitled", values, model: t.model || "" };
}

/** Persist just the selected template id for a purpose (used at generation). */
async function setSelectedTemplate(purposeId, templateId) {
  const s = llmTemplates[purposeId] || (llmTemplates[purposeId] = {
    selected: DEFAULT_TEMPLATE_ID,
    templates: [],
  });
  s.selected = templateId;
  await persist();
}

/** All templates for a purpose: the built-in Default first, then user ones. */
function listTemplates(purposeId) {
  const p = getPurpose(purposeId);
  const builtin = {
    id: DEFAULT_TEMPLATE_ID,
    name: "Default",
    values: { ...(p ? p.default : {}) },
    model: (llmTemplates[purposeId] && llmTemplates[purposeId].defaultModel) || "",
    builtin: true,
  };
  const users = ((llmTemplates[purposeId] && llmTemplates[purposeId].templates) || []).map(
    (t) => ({ ...t, builtin: false })
  );
  return [builtin, ...users];
}

/** A template by id (falls back to the built-in Default). */
function getTemplate(purposeId, templateId) {
  const all = listTemplates(purposeId);
  return all.find((t) => t.id === templateId) || all[0];
}

/** The remembered selection id, validated against the current list. */
function getSelectedTemplateId(purposeId) {
  const sel = (llmTemplates[purposeId] && llmTemplates[purposeId].selected) || DEFAULT_TEMPLATE_ID;
  return listTemplates(purposeId).some((t) => t.id === sel) ? sel : DEFAULT_TEMPLATE_ID;
}

/** A single field value for a template (falls back to the built-in default). */
function getTemplateValue(purposeId, templateId, fieldKey) {
  const t = getTemplate(purposeId, templateId);
  const p = getPurpose(purposeId);
  return (t?.values?.[fieldKey] ?? p?.default?.[fieldKey] ?? "").toString();
}

/** The model a template pins, or "" to use the global model (AI settings). */
function getTemplateModel(purposeId, templateId) {
  if (templateId === DEFAULT_TEMPLATE_ID) {
    return (llmTemplates[purposeId] && llmTemplates[purposeId].defaultModel) || "";
  }
  const t = getTemplate(purposeId, templateId);
  return (t && t.model) || "";
}

exports.DEFAULT_TEMPLATE_ID = DEFAULT_TEMPLATE_ID;
exports.PURPOSE_DEFS = PURPOSE_DEFS;
exports.getPurpose = getPurpose;
exports.promptTree = promptTree;
exports.llmTemplates = llmTemplates;
exports.loadLlmTemplates = loadLlmTemplates;
exports.saveLlmTemplates = saveLlmTemplates;
exports.setSelectedTemplate = setSelectedTemplate;
exports.listTemplates = listTemplates;
exports.getTemplate = getTemplate;
exports.getSelectedTemplateId = getSelectedTemplateId;
exports.getTemplateValue = getTemplateValue;
exports.getTemplateModel = getTemplateModel;
  });
  register("js/llm/llm-default-prompts.js", function (module, exports, require) {
// AUTO-GENERATED by scripts/gen-default-prompts.mjs — DO NOT EDIT BY HAND.
// Edit the plain-text sources in prompts/ and re-run the generator:
//   node scripts/gen-default-prompts.mjs
// (build.mjs also runs it before bundling, so dist/ stays fresh.)

/** Built-in default AI prompts, keyed by purpose id then field. */
const DEFAULT_PROMPTS = {
  "image.generate": {
    "system": "You are an expert vision captioner for text-to-image and LoRA training. Look at the image and write ONE concise prompt describing it: main subject(s), appearance, clothing, action or pose, setting, composition, lighting, color palette, and art style. Use comma-separated phrases. Output only the prompt text — no preamble, no quotes, no explanations.",
    "user": "Write a detailed prompt for this image.",
  },
  "library.default.generate": {
    "system": "You are an expert prompt writer. Based on the user's request, write a single, complete, ready-to-use prompt as plain text. Output only the prompt text — no preamble, no commentary, no markdown.",
    "user": "Write a detailed prompt.",
  },
  "library.h3.review": {
    "system": "You are a senior reviewer for MiniMax H3 Ref2VA (full-reference) video prompts. You are given a whole prompt: the shared subject_definitions and, for each scene, its summary and detailed_description. REVIEW the prompt as a set — do NOT rewrite it.\n\nCheck, and report problems + concrete fixes, for:\n1. Subject/label consistency: every <Subject N> / <Picture N> used in a scene must be defined in subject_definitions; a label must keep the same identity across scenes; a scene must NOT re-describe a defined subject's fixed details (appearance, wardrobe, a setting's fixtures) — it should reference it by label. Flag invented, undefined, or redefined labels, and descriptions that contradict each other between scenes.\n2. Cross-scene continuity: subjects, setting, wardrobe and tone should stay continuous unless a change is clearly intended. Flag contradictions (a subject's clothing/appearance changing for no reason, day↔night jumps, a prop appearing or vanishing between scenes).\n3. No repetition: consecutive scenes must show NEW, different action — flag scenes that repeat, restate, or reuse a previous scene's wording, shots, or events.\n4. Format adherence per scene:\n   - summary: one short paragraph using only existing <Subject N> / <Picture N> labels; it should carry a \"[task type]\" prefix.\n   - detailed_description: TWO parts — (1) one short style+setting opening sentence with no [Shot] tag, then (2) a line starting with \"[Shot 1] …\". Default is a single shot with no timestamps unless multiple shots are clearly intended. Flag a missing opening sentence, a missing \"[Shot 1]\", or unjustified extra shots/timestamps.\n5. Completeness: flag empty / \"N/A\" / vague required content and note where a scene is underspecified for the model.\n\nReport format (plain text, concise, actionable):\n- \"Cross-scene issues\": consistency/continuity problems spanning scenes, each naming the scenes involved and a concrete fix.\n- \"Per-scene notes\": grouped under \"Scene N\", only scenes that have issues.\n- \"Suggestions\": a short, prioritized list of the highest-impact improvements.\nCite scenes as \"Scene N\". Do NOT rewrite the prompt or output full replacement scenes — give targeted, minimal fixes. If something is already solid, say so briefly.",
    "user": "Review this prompt for cross-scene consistency and suggest improvements.",
  },
  "library.h3.scene-review": {
    "system": "You are a senior reviewer for MiniMax H3 Ref2VA (full-reference) video prompts. You are given the shared subject_definitions and three scenes: the PREVIOUS scene and the NEXT scene as read-only context, and the CURRENT scene, which is the ONLY one you review. REVIEW the CURRENT scene — do NOT rewrite it here.\n\nCheck, and report problems + concrete fixes, for the CURRENT scene:\n1. Subject/label consistency: every <Subject N> / <Picture N> it uses must be defined in subject_definitions and keep the same identity as in the neighbouring scenes; it must NOT re-describe a defined subject's fixed details (appearance, wardrobe, a setting's fixtures) — reference them by label. Flag invented, undefined, or redefined labels, and details that contradict the neighbours.\n2. Continuity with the neighbours: subjects, setting, wardrobe and tone should follow on from the PREVIOUS scene and set up the NEXT one, unless a change is clearly intended. Flag contradictions (clothing/appearance changing for no reason, day↔night jumps, a prop appearing or vanishing).\n3. No repetition: the CURRENT scene must show NEW, different action — flag any repetition, restatement, or reuse of the PREVIOUS scene's wording, shots, or events.\n4. Format: summary is one short paragraph using only existing labels with a \"[task type]\" prefix; detailed_description is TWO parts — one short style+setting opening sentence with no [Shot] tag, then a line starting with \"[Shot 1] …\" (single shot, no timestamps unless multiple shots are clearly intended). Flag a missing opening sentence, a missing \"[Shot 1]\", or unjustified extra shots/timestamps.\n5. Completeness: flag empty / \"N/A\" / vague required content and note where the scene is underspecified.\n\nReport concise, actionable notes for the CURRENT scene only (do NOT critique the neighbours), followed by a short \"Suggestions\" list of the highest-impact fixes. Do NOT output a rewritten scene here.",
    "user": "Review the current scene for consistency with its neighbours and suggest improvements.",
  },
  "library.h3.subject": {
    "system": "You are an expert at writing MiniMax H3 Ref2VA `subject_definitions`. You are given a reference image and a target label such as <Subject 1> or <Picture 1>. Write EXACTLY ONE line defining that label.\n\n- For <Subject N>: describe the MAIN subject of the image.\n  · If it is a person / people (or a specific animal / object / product): describe only that subject's identifying features (approximate age, gender, build, skin tone, hair, notable facial features and expression, clothing/accessories with colors); do NOT describe the surrounding background, environment, or lighting. If several people appear, describe each briefly.\n  · If the image is a scene/environment with no salient person, animal, or object (e.g. an empty room, a public bathroom, a landscape): the environment IS the subject — describe its type, layout, key surfaces/fixtures/objects, materials, colors, and overall look.\n- For <Picture N>: describe the image as a concrete frame/composition anchor.\n\nBegin the line with the exact given label followed by ' is '. Output ONLY that one line — no preamble, no quotes, no extra lines.",
    "user": "Describe this reference image for the given label.",
  },
  "scene.generate": {
    "system": "You write ONE scene for the MiniMax H3 Ref2VA (full-reference) video model. Output ONLY these sections, in order, each as \"section:\" on its own line with a blank line between them:\n\nsummary\ndetailed_description\noverall_soundscape\nnon_diegetic_music\n\nWrite everything in English except dialogue/lyrics inside <d> and text visible on screen.\n\nGENERAL\n- summary: one short paragraph using only the existing <Subject N> / <Picture N> labels; introduce no new labels.\n- If subject_definitions are provided, reuse those exact <Subject N> / <Picture N> labels; never invent new ones or redefine them.\n- Everything in subject_definitions (people, environments/settings, objects) is already defined there: reference it by its <Subject N> / <Picture N> label and do NOT re-describe or invent its fixed details (a person's clothing/hair/features, a setting's layout/fixtures/materials). Name the setting by its label (e.g. \"set in <Subject 1>\") instead of describing it; describe only actions, positions, and state changes.\n- HONOR THE BRIEF'S AUDIO: include exactly the sounds, ambience, music, songs, and lyrics the user requests, in the correct section, using their intent verbatim. Never drop them or replace them with different invented audio.\n- If a previous scene is provided (CONTINUATION), write the NEXT scene that continues it: keep continuity of subjects, setting, wardrobe and tone, but SHOW NEW, DIFFERENT action driven by the TASK; do NOT repeat, restate, summarize, or reuse the previous scene's wording, shots, or events. In continuation mode, detailed_description MUST START with exactly \"Continue directly from the incoming H3 Motion Context, \" (this replaces the style/setting opening sentence) and then flow into \"[Shot 1] …\". Otherwise (no previous scene) write a fresh opening scene with the style/setting opening.\n\nDETAILED_DESCRIPTION (main body, playback order; be detailed and explicit — aim for a rich ~350-500 words, but do not pad; dialogue-dense scenes may be shorter to fit the spoken timeline)\n- FORMAT it in TWO parts, in this exact order:\n  (1) ONE SHORT opening sentence (≈8–50 words, no [Shot] tag) establishing just the video type/style (from the brief; do NOT default to \"cinematic\" unless asked) and the setting. Keep it brief — the detail belongs in the shot, not here. If any subject_definition is a place/room/environment, that is the SETTING — reference it by its <Subject N> label, don't describe it (e.g. \"Amateur home-video set in <Subject 1>.\").\n  (2) On the NEXT line, start with the literal token \"[Shot 1]\" (no timestamp) and describe the shot: composition, subjects by their <Subject N> labels, action, camera, and synchronized sound.\n  Never merge the two: the style+setting sentence comes first, THEN \"[Shot 1] ...\". Never start detailed_description directly with \"[Shot 1]\".\n- DEFAULT: ONE shot only. Use \"[Shot 1]\" with NO timestamp, keep the whole scene inside it, and move the camera instead of cutting. Do not add more shots or any timestamps.\n- Multiple shots ONLY if the brief explicitly asks: then add \"[Shot 2]\", \"[Shot 3]\", … each starting with a strictly increasing cut time (e.g. \"At 00:03.500, the camera cuts to …\").\n- Per shot, ground every detail in what is visible or audible: composition, subject position and action, environment and lighting (if the setting is a defined subject, name it by its <Subject N> label instead of re-describing it), state changes, camera movement, and synchronized sound/dialogue; note where each <Subject N>/<Picture N> appears or takes effect. Do NOT write a plot summary or a bare list of reference relationships. Cite a frame anchor naturally (e.g. \"the shot begins from <Picture 1>\").\n- Keep EVERY on-screen subject alive: give each present <Subject N> believable ongoing behavior — small idle actions, natural reactions, gaze/weight shifts, breathing, fidgets, gestures — so no one stands frozen while another acts (unless stillness is clearly intended). Secondary subjects should react to the main action instead of staying static.\n\nCAMERA — natural English within the sentence, never stacked labels. Form: motion (+ \"with small/large amplitude\") (+ \"at slow/fast speed\"); omit medium amplitude and normal speed.\nMotions: Zoom In/Out, Push In/Pull Out, Pan Left/Right, Truck Left/Right, Tilt Up/Down, Pedestal Up/Down, Arc Shot, Tracking Shot, Static Shot, Shake Slightly/Strongly, POV, Roll Clockwise/Counterclockwise.\ne.g. \"The camera pushes in with small amplitude at slow speed toward her hands.\"\n\nDIALOGUE & MUSIC\n- Vocal sources get stable IDs (S1), (S2)...; joint speech (S1,S2). When a defined subject speaks, write \"<Subject N> (Sx)\"; for a speaker who is not a defined subject, use a short stable voice description + (Sx). Keep identity/action OUTSIDE <d>; inside only <d>[Language] exact words</d>, verbatim (no translation).\n- Voiceover: \"says in an off-screen voiceover\" and note the lips stay closed.\n- Music the characters can hear (ambient or from a device) is DIEGETIC → describe it in detailed_description (genre + source; reused lyrics in <d>[Language] ...</d>), NOT in non_diegetic_music. On-screen text in double quotes, verbatim.\n\nOVERALL_SOUNDSCAPE: 1-4 English sentences summarizing WHOLE-VIDEO ambience and general physical / non-verbal sounds (wind, footsteps, impacts, breathing...). Keep it GENERAL: do NOT reference <Subject N> labels, name a shot, or narrate sounds synchronized to a specific action — those action-synced sounds belong in detailed_description. Reflect any ambience the brief requests. No dialogue, songs, or music here. Use \"N/A\" only for deliberate full silence.\n\nNON_DIEGETIC_MUSIC: By DEFAULT write exactly \"N/A\". Only when the brief explicitly asks for background/score music, describe that audience-only music in 1-3 English sentences (instrumentation, tempo, rhythm, dynamics; no mood words).\n\nOutput ONLY these four sections — no preamble, no commentary, no markdown.",
    "user": "Generate a complete, vivid scene.",
  },
};

exports.DEFAULT_PROMPTS = DEFAULT_PROMPTS;
  });
  register("js/globals-ai.js", function (module, exports, require) {
const { chat } = require("js/llm/llm-client.js");
const { isLlmOnline } = require("js/llm/llm-config.js");
const { listTemplates, getSelectedTemplateId, setSelectedTemplate, getTemplateValue, getTemplateModel } = require("js/llm/llm-prompts.js");
const { openModal } = require("js/modal.js");
const { SECTION_HELP } = require("js/section-help.js");
const { toast } = require("js/toast.js");

/**
 * "Generate globals with AI" for MiniMax H3 Ref2VA prompt-level sections.
 *
 * Per added reference the user picks a label type (Subject/Picture), a
 * relationship marker, and a disk image. The image is sent to a vision model,
 * which returns one `<Label N> is …` line for `subject_definitions`; a matching
 * `retention_analysis` entry is created DETERMINISTICALLY (no LLM) with the
 * chosen marker and a `- TODO` to complete.
 *
 * The two textareas are the source of truth. A live list is derived from them;
 * deleting/reordering in the list re-serializes BOTH fields with sequential
 * per-type indices, so numbering always stays in sync. Editing the textareas by
 * hand is allowed — the list re-derives from them; any list operation then
 * re-numbers, and non-conforming lines are dropped (the user's responsibility).
 */

const $ = (id) => document.getElementById(id);
const SUBJECT_PURPOSE = "library.h3.subject";
const GEN_TIMEOUT_MS = 120000;

/** Fixed relationship markers for visible content (guide §4.1). */
const RELATIONSHIP_MARKERS = [
  "fully_preserved",
  "partially_preserved",
  "attribute_transfer",
  "weak_reference",
];

const LABEL_RE = /^\s*<(Subject|Picture)\s+(\d+)>\s?(.*)$/;

let el;
/** @type {((v:{subjectDefs:string,retention:string})=>void)|null} */
let applyFn = null;
/** @type {AbortController|null} */
let inflight = null;

function initGlobalsAi() {
  el = {
    root: $("globalsGenModal"),
    closeBtn: $("globalsGenCloseBtn"),
    template: $("globalsTemplate"),
    labelType: $("globalsLabelType"),
    nextLabel: $("globalsNextLabel"),
    marker: $("globalsMarker"),
    imageInput: $("globalsImageInput"),
    addBtn: $("globalsAddBtn"),
    stopBtn: $("globalsStopBtn"),
    status: $("globalsStatus"),
    list: $("globalsList"),
    listEmpty: $("globalsListEmpty"),
    subjects: $("globalsSubjects"),
    subjHelp: $("globalsSubjHelp"),
    retention: $("globalsRetention"),
    retHelp: $("globalsRetHelp"),
    acceptBtn: $("globalsAcceptBtn"),
  };

  for (const m of RELATIONSHIP_MARKERS) {
    const o = document.createElement("option");
    o.value = m;
    o.textContent = m;
    el.marker.appendChild(o);
  }

  el.closeBtn.addEventListener("click", close);
  el.root.querySelector(".modal-backdrop").addEventListener("click", close);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      close();
    }
  });

  // Same field guidance as the scene editor's "?" help buttons.
  el.subjHelp.addEventListener("click", () =>
    openModal({ title: "subject_definitions — guidance", html: SECTION_HELP.subject_definitions })
  );
  el.retHelp.addEventListener("click", () =>
    openModal({ title: "retention_analysis — guidance", html: SECTION_HELP.retention_analysis })
  );

  el.template.addEventListener("change", () =>
    setSelectedTemplate(SUBJECT_PURPOSE, el.template.value)
  );
  el.labelType.addEventListener("change", updateNextLabel);
  el.imageInput.addEventListener("change", () => {
    el.addBtn.disabled = el.imageInput.files.length === 0;
  });
  el.addBtn.addEventListener("click", addReference);
  el.stopBtn.addEventListener("click", abortInflight);
  el.acceptBtn.addEventListener("click", accept);
  // Editing subjects by hand drives retention: mirror it line-for-line as you
  // type (without rewriting the field you're in), then re-index on blur.
  el.subjects.addEventListener("input", onSubjectsInput);
  el.subjects.addEventListener("change", normalize);
  el.retention.addEventListener("input", renderList);
}

/** As the user edits subject_definitions, keep retention_analysis in sync. */
function onSubjectsInput() {
  syncRetentionToSubjects();
  renderList();
}

/**
 * Rebuild retention_analysis to have exactly one line per subject label present
 * in subject_definitions (same labels and order), keeping each label's existing
 * retention text and defaulting any new one. Does not rewrite subjects, so
 * typing there is never disrupted. Deleting a subject line thus drops its
 * retention line too.
 */
function syncRetentionToSubjects() {
  const retByLabel = new Map();
  for (const line of el.retention.value.split("\n")) {
    const m = line.match(LABEL_RE);
    if (m) retByLabel.set(`${m[1]} ${m[2]}`, m[3]);
  }
  const out = [];
  for (const line of el.subjects.value.split("\n")) {
    const m = line.match(LABEL_RE);
    if (!m) continue;
    const label = `<${m[1]} ${m[2]}>`;
    const rest = retByLabel.get(`${m[1]} ${m[2]}`) ?? retentionRest(m[1], "fully_preserved");
    out.push(`${label} ${rest}`.trimEnd());
  }
  el.retention.value = out.join("\n");
}

/** Re-serialize both fields with sequential per-type indices (closes gaps). */
function normalize() {
  serializeItems(parseItems());
}

/**
 * @param {{ subjectDefs?:string, retention?:string,
 *   onApply:(v:{subjectDefs:string,retention:string})=>void }} opts
 */
function openGlobalsAi({ subjectDefs = "", retention = "", onApply }) {
  if (!isLlmOnline()) {
    toast("AI not connected — check AI settings.", "warn");
    return;
  }
  applyFn = onApply;
  el.subjects.value = subjectDefs;
  el.retention.value = retention;
  populateTemplateSelect();
  el.imageInput.value = "";
  el.addBtn.disabled = true;
  renderList();
  setStatus("", "");
  el.root.hidden = false;
}

function close() {
  abortInflight();
  el.root.hidden = true;
}

function populateTemplateSelect() {
  el.template.textContent = "";
  for (const t of listTemplates(SUBJECT_PURPOSE)) {
    const o = document.createElement("option");
    o.value = t.id;
    o.textContent = t.name;
    el.template.appendChild(o);
  }
  el.template.value = getSelectedTemplateId(SUBJECT_PURPOSE);
}

// ---------------------------------------------------------------------------
// Items <-> text (the textareas are the source of truth)
// ---------------------------------------------------------------------------

/** Parse the textareas into items: subject lines drive them, retention matched by label. */
function parseItems() {
  const retByLabel = new Map();
  for (const line of el.retention.value.split("\n")) {
    const m = line.match(LABEL_RE);
    if (m) retByLabel.set(`${m[1]} ${m[2]}`, m[3]);
  }
  const items = [];
  for (const line of el.subjects.value.split("\n")) {
    const m = line.match(LABEL_RE);
    if (!m) continue; // non-conforming lines aren't managed by the list
    const type = m[1];
    const rest = retByLabel.get(`${m[1]} ${m[2]}`);
    items.push({
      type,
      subjectBody: m[3],
      retentionRest: rest != null ? rest : retentionRest(type, "fully_preserved"),
    });
  }
  return items;
}

/** Write items back to both textareas with sequential per-type indices. */
function serializeItems(items) {
  const counts = { Subject: 0, Picture: 0 };
  const subj = [];
  const ret = [];
  for (const it of items) {
    counts[it.type] += 1;
    const label = `<${it.type} ${counts[it.type]}>`;
    subj.push(`${label} ${it.subjectBody}`.trimEnd());
    ret.push(`${label} ${it.retentionRest}`.trimEnd());
  }
  el.subjects.value = subj.join("\n");
  el.retention.value = ret.join("\n");
  renderList();
}

function retentionRest(type, marker) {
  const loc = type === "Subject" ? "(appears in [Shot 1])" : "([Shot 1] first frame)";
  return `${loc}: ${marker} - TODO`;
}

// ---------------------------------------------------------------------------
// List UI
// ---------------------------------------------------------------------------
function renderList() {
  const items = parseItems();
  el.list.textContent = "";
  el.listEmpty.hidden = items.length > 0;

  const counts = { Subject: 0, Picture: 0 };
  items.forEach((it, i) => {
    counts[it.type] += 1;
    const label = `<${it.type} ${counts[it.type]}>`;

    const row = document.createElement("div");
    row.className = "gen-item";

    const up = mkMini("↑", "Move up", () => move(i, -1));
    up.disabled = i === 0;
    const down = mkMini("↓", "Move down", () => move(i, 1));
    down.disabled = i === items.length - 1;

    const lab = document.createElement("span");
    lab.className = "gen-item-label";
    lab.textContent = label;

    const preview = document.createElement("span");
    preview.className = "gen-item-preview";
    preview.textContent = it.subjectBody;
    preview.title = it.subjectBody;

    const del = mkMini("✕", "Remove", () => remove(i));
    del.classList.add("danger");

    row.append(up, down, lab, preview, del);
    el.list.appendChild(row);
  });

  updateNextLabel(items);
}

function mkMini(text, title, onClick) {
  const b = document.createElement("button");
  b.type = "button";
  b.className = "btn gen-item-btn";
  b.textContent = text;
  b.title = title;
  b.addEventListener("click", onClick);
  return b;
}

function remove(i) {
  const items = parseItems();
  items.splice(i, 1);
  serializeItems(items);
}

function move(i, dir) {
  const items = parseItems();
  const to = i + dir;
  if (to < 0 || to >= items.length) return;
  [items[i], items[to]] = [items[to], items[i]];
  serializeItems(items);
}

function updateNextLabel(items = parseItems()) {
  const type = el.labelType.value;
  const n = items.filter((it) => it.type === type).length + 1;
  el.nextLabel.textContent = `<${type} ${n}>`;
}

// ---------------------------------------------------------------------------
// Add a reference (image -> subject line via LLM, + deterministic retention)
// ---------------------------------------------------------------------------
async function addReference() {
  const file = el.imageInput.files[0];
  if (!file) {
    setStatus("Choose an image first.", "warn");
    return;
  }
  const type = el.labelType.value;
  const marker = el.marker.value;
  const label = el.nextLabel.textContent; // for the model's phrasing only

  abortInflight();
  const ctrl = new AbortController();
  inflight = ctrl;
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
  setBusy(true);
  setStatus(`Generating ${label}…`, "");

  try {
    const dataUrl = await fileToDataUrl(file);
    const tid = el.template.value || getSelectedTemplateId(SUBJECT_PURPOSE);
    const system = getTemplateValue(SUBJECT_PURPOSE, tid, "system");
    const user = getTemplateValue(SUBJECT_PURPOSE, tid, "user");
    const text = await chat(
      [
        { role: "system", content: system },
        {
          role: "user",
          content: [
            {
              type: "text",
              text: `${user}\n\nTarget label: ${label}. Output exactly one line starting with "${label} is ".`,
            },
            { type: "image_url", image_url: { url: dataUrl } },
          ],
        },
      ],
      { signal: ctrl.signal, model: getTemplateModel(SUBJECT_PURPOSE, tid) }
    );

    const body = text
      .trim()
      .replace(/\r?\n+/g, " ")
      .replace(LABEL_RE, "$3"); // strip any label the model included
    if (!body) {
      setStatus("The model returned an empty response.", "warn");
      return;
    }

    const items = parseItems();
    items.push({ type, subjectBody: body, retentionRest: retentionRest(type, marker) });
    serializeItems(items); // re-indexes everything and re-renders the list

    el.imageInput.value = "";
    el.addBtn.disabled = true;
    setStatus("Added.", "ok");
  } catch (err) {
    setStatus(err.message, "error");
  } finally {
    clearTimeout(timer);
    inflight = null;
    setBusy(false);
  }
}

function accept() {
  applyFn?.({ subjectDefs: el.subjects.value, retention: el.retention.value });
  toast("Globals applied — review and save.");
  close();
}

function abortInflight() {
  if (inflight) {
    inflight.abort();
    inflight = null;
  }
}

function setBusy(busy) {
  el.stopBtn.hidden = !busy;
  el.addBtn.disabled = busy || el.imageInput.files.length === 0;
  el.labelType.disabled = busy;
  el.marker.disabled = busy;
  el.template.disabled = busy;
}

function setStatus(text, kind) {
  el.status.textContent = text;
  el.status.className = "llm-status" + (kind ? " " + kind : "");
}

function fileToDataUrl(file) {
  return new Promise((resolve, reject) => {
    const r = new FileReader();
    r.onload = () => resolve(r.result);
    r.onerror = () => reject(r.error || new Error("read failed"));
    r.readAsDataURL(file);
  });
}

exports.RELATIONSHIP_MARKERS = RELATIONSHIP_MARKERS;
exports.initGlobalsAi = initGlobalsAi;
exports.openGlobalsAi = openGlobalsAi;
  });
  register("js/review-ai.js", function (module, exports, require) {
const { chat } = require("js/llm/llm-client.js");
const { isLlmOnline } = require("js/llm/llm-config.js");
const { listTemplates, getSelectedTemplateId, setSelectedTemplate, getTemplateValue, getTemplateModel } = require("js/llm/llm-prompts.js");
const { toast } = require("js/toast.js");

/**
 * "Review with AI" for the Prompt Library MiniMax H3 Ref2VA type. One modal,
 * two modes selected by the caller:
 *
 *  - WHOLE PROMPT (advisory): the shared subject_definitions plus, per scene, its
 *    summary and detailed_description, so the model spots cross-scene consistency
 *    problems. Read-only report — nothing is applied. (purpose only, no rewrite.)
 *
 *  - ONE SCENE (advisory + apply): the current scene plus its neighbors and the
 *    globals, reviewed for consistency; then "Apply suggestions" makes a SECOND
 *    call that rewrites the scene implementing the notes, shown for confirmation
 *    before "Accept into scene" applies it. Enabled by passing `rewrite`.
 *
 * The caller assembles the context and, for scene mode, owns parse+apply through
 * `rewrite.onApply` — this module stays free of domain parsing (mirrors the way
 * text-ai.js hands the raw result back).
 */

const $ = (id) => document.getElementById(id);
const DEFAULT_PURPOSE = "library.h3.review";
const GEN_TIMEOUT_MS = 180000; // reviews (and rewrites) can be long

let el;
/**
 * @type {{ purpose?:string, title?:string, contextLabel?:string, context?:string,
 *   rewrite?:{ purpose:string, instruction:string, acceptLabel?:string,
 *     validate?:(t:string)=>boolean, onApply:(t:string)=>void } } | null}
 */
let cfg = null;
/** The assembled context sent with every review turn (set on open). */
let contextText = "";
/** @type {AbortController|null} */
let inflight = null;
/** Review conversation so far (null before the first review). Refine appends turns. */
let history = null;
/** True once a review result exists (gates "Apply suggestions"). */
let hasReview = false;

function reviewPurpose() {
  return (cfg && cfg.purpose) || DEFAULT_PURPOSE;
}

function initReviewAi() {
  el = {
    root: $("reviewModal"),
    title: $("reviewTitle"),
    closeBtn: $("reviewCloseBtn"),
    template: $("reviewTemplate"),
    system: $("reviewSystem"),
    user: $("reviewUser"),
    contextLabel: $("reviewContextLabel"),
    context: $("reviewContext"),
    runBtn: $("reviewRunBtn"),
    refineBtn: $("reviewRefineBtn"),
    stopBtn: $("reviewStopBtn"),
    status: $("reviewStatus"),
    result: $("reviewResult"),
    applyBtn: $("reviewApplyBtn"),
    copyBtn: $("reviewCopyBtn"),
    rewriteWrap: $("reviewRewriteWrap"),
    rewrite: $("reviewRewrite"),
    acceptRow: $("reviewAcceptRow"),
    acceptBtn: $("reviewAcceptBtn"),
  };

  el.closeBtn.addEventListener("click", close);
  el.root.querySelector(".modal-backdrop").addEventListener("click", close);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      close();
    }
  });

  el.template.addEventListener("change", onTemplateChange);
  el.runBtn.addEventListener("click", run);
  el.refineBtn.addEventListener("click", refine);
  el.stopBtn.addEventListener("click", abortInflight);
  el.result.addEventListener("input", syncCopy);
  el.copyBtn.addEventListener("click", copy);
  el.applyBtn.addEventListener("click", applySuggestions);
  el.rewrite.addEventListener("input", () => syncAccept());
  el.acceptBtn.addEventListener("click", accept);
}

/**
 * @param {{ purpose?:string, title?:string, contextLabel?:string, context:string,
 *   rewrite?:{ purpose:string, instruction:string, acceptLabel?:string,
 *     validate?:(t:string)=>boolean, onApply:(t:string)=>void } }} opts
 */
function openReviewAi(opts) {
  if (!isLlmOnline()) {
    toast("AI not connected — check AI settings.", "warn");
    return;
  }
  cfg = opts;
  el.title.textContent = opts.title || "Review prompt with AI";
  el.contextLabel.textContent = opts.contextLabel || "Prompt under review (read-only)";
  contextText = (opts.context || "").trim();
  el.context.value = contextText;

  populateTemplateSelect();
  const sel = getSelectedTemplateId(reviewPurpose());
  el.template.value = sel;
  applyTemplate(sel);

  history = null; // fresh session
  hasReview = false;
  el.result.value = "";
  el.rewrite.value = "";
  el.runBtn.textContent = "Review";
  el.refineBtn.hidden = true; // appears after the first review

  const sceneMode = !!opts.rewrite;
  el.applyBtn.hidden = !sceneMode; // only scene mode can implement suggestions
  el.acceptBtn.textContent = (opts.rewrite && opts.rewrite.acceptLabel) || "Accept into scene";
  el.rewriteWrap.hidden = true; // revealed once a corrected scene is produced
  el.acceptRow.hidden = true;

  setBusy(false);
  setStatus("Ready.", "");
  el.root.hidden = false;
  el.user.focus();
}

function close() {
  abortInflight();
  el.root.hidden = true;
}

function populateTemplateSelect() {
  el.template.textContent = "";
  for (const t of listTemplates(reviewPurpose())) {
    const o = document.createElement("option");
    o.value = t.id;
    o.textContent = t.name;
    el.template.appendChild(o);
  }
}

function applyTemplate(id) {
  el.system.value = getTemplateValue(reviewPurpose(), id, "system");
  el.user.value = getTemplateValue(reviewPurpose(), id, "user");
}

function onTemplateChange() {
  applyTemplate(el.template.value);
  setSelectedTemplate(reviewPurpose(), el.template.value);
}

/**
 * Low-level chat call shared by review, refine and rewrite. Owns the inflight/
 * timeout/busy plumbing; returns the assistant text, or null on error/abort.
 */
async function callModel(messages, model, verb) {
  abortInflight();
  const ctrl = new AbortController();
  inflight = ctrl;
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
  setBusy(true);
  setStatus(verb, "");
  try {
    const text = await chat(messages, { signal: ctrl.signal, model });
    return (text || "").trim();
  } catch (err) {
    setStatus(err.message, "error");
    return null;
  } finally {
    clearTimeout(timer);
    inflight = null;
    setBusy(false);
  }
}

/** First-turn review messages: system + (context, then the brief LAST). */
function buildReviewMessages() {
  const parts = [];
  if (contextText) parts.push(`UNDER REVIEW:\n${contextText}`);
  if (el.user.value.trim()) parts.push(`TASK (do this now):\n${el.user.value.trim()}`);
  const messages = [];
  if (el.system.value.trim()) messages.push({ role: "system", content: el.system.value });
  messages.push({ role: "user", content: parts.join("\n\n") || el.user.value });
  return messages;
}

/** Once a review exists, reveal Refine, relabel the primary button, allow Apply. */
function afterReviewed() {
  el.runBtn.textContent = "Re-review";
  el.refineBtn.hidden = false;
}

/** Store the completed review result and status. */
function setReviewResult(text) {
  el.result.value = text;
  setStatus(
    text ? "Done." : "The model returned an empty response.",
    text ? "ok" : "warn"
  );
  hasReview = true;
  syncButtons();
}

/** Fresh review: rebuild from the fields + context, discarding any prior turns. */
async function run() {
  const messages = buildReviewMessages();
  const text = await callModel(messages, getTemplateModel(reviewPurpose(), el.template.value), "Reviewing…");
  if (text == null) return;
  setReviewResult(text);
  history = [...messages, { role: "assistant", content: text }];
  afterReviewed();
}

/** Refine: keep the conversation and send the current user prompt as a new turn. */
async function refine() {
  if (!history) return run();
  const base = history.slice();
  if (base.length && base[base.length - 1].role === "assistant") {
    base[base.length - 1] = { role: "assistant", content: el.result.value };
  }
  const instruction = el.user.value.trim() || "Refine the previous review.";
  const messages = [...base, { role: "user", content: instruction }];
  const text = await callModel(messages, getTemplateModel(reviewPurpose(), el.template.value), "Reviewing…");
  if (text == null) return;
  setReviewResult(text);
  history = [...messages, { role: "assistant", content: text }];
  afterReviewed();
}

/**
 * Scene mode only: a SECOND call that rewrites the scene implementing the review
 * notes. Uses the rewrite purpose's own system (canonical scene format) rather
 * than the reviewer's, and hands the result to the "Corrected scene" field for
 * confirmation. Nothing is applied until Accept.
 */
async function applySuggestions() {
  if (!cfg || !cfg.rewrite || !hasReview) return;
  const rp = cfg.rewrite.purpose;
  const tid = getSelectedTemplateId(rp);
  const system = getTemplateValue(rp, tid, "system");
  const parts = [];
  if (contextText) parts.push(contextText);
  if (el.result.value.trim()) parts.push(`REVIEW NOTES (apply these fixes):\n${el.result.value.trim()}`);
  parts.push(cfg.rewrite.instruction);
  const messages = [];
  if (system.trim()) messages.push({ role: "system", content: system });
  messages.push({ role: "user", content: parts.join("\n\n") });

  const text = await callModel(messages, getTemplateModel(rp, tid), "Applying suggestions…");
  if (text == null) return;
  el.rewrite.value = text;
  el.rewriteWrap.hidden = false;
  el.acceptRow.hidden = false;
  setStatus(
    text ? "Corrected scene ready — review it, then Accept." : "The model returned an empty response.",
    text ? "ok" : "warn"
  );
  syncButtons();
  el.rewrite.scrollIntoView?.({ block: "nearest" });
}

function accept() {
  const text = el.rewrite.value;
  if (!text.trim() || !cfg || !cfg.rewrite) return;
  cfg.rewrite.onApply?.(text);
  close();
}

async function copy() {
  try {
    await navigator.clipboard.writeText(el.result.value);
    toast("Review copied.");
  } catch {
    toast("Clipboard blocked by the browser.", "warn");
  }
}

function abortInflight() {
  if (inflight) {
    inflight.abort();
    inflight = null;
  }
}

function setBusy(busy) {
  el.stopBtn.hidden = !busy;
  el.runBtn.disabled = busy;
  el.refineBtn.disabled = busy;
  syncButtons(busy);
}

/** Reconcile Apply / Accept / Copy with the current mode and state. */
function syncButtons(busy = false) {
  const sceneMode = !!(cfg && cfg.rewrite);
  el.applyBtn.disabled = busy || !sceneMode || !hasReview;
  syncAccept(busy);
  syncCopy();
}

function syncAccept(busy = false) {
  const sceneMode = !!(cfg && cfg.rewrite);
  const text = el.rewrite.value;
  const valid =
    sceneMode &&
    text.trim().length > 0 &&
    (!cfg.rewrite.validate || cfg.rewrite.validate(text));
  el.acceptBtn.disabled = busy || !valid;
}

function syncCopy() {
  el.copyBtn.disabled = el.result.value.trim().length === 0;
}

function setStatus(text, kind) {
  el.status.textContent = text;
  el.status.className = "llm-status" + (kind ? " " + kind : "");
}

exports.initReviewAi = initReviewAi;
exports.openReviewAi = openReviewAi;
  });
  register("js/llm/llm-settings.js", function (module, exports, require) {
const { llmConfig, saveLlmConfig, DEFAULT_LLM_CONFIG, isLlmConfigured, setLlmStatus } = require("js/llm/llm-config.js");
const { listModels, testConnection } = require("js/llm/llm-client.js");
const { openPromptTemplates } = require("js/llm/llm-prompt-templates.js");
const { toast } = require("js/toast.js");

/**
 * The global "AI settings" dialog: edits the app-wide `llmConfig` (base URL,
 * model, API key, temperature). Opened from the top nav; every feature that uses
 * the LLM relies on what is configured here.
 */

const $ = (id) => document.getElementById(id);
let el;

/** How long a probe may run before we abort it and report a timeout. */
const TEST_TIMEOUT_MS = 15000; // explicit Test / Refresh (user is waiting)
const STATUS_TIMEOUT_MS = 8000; // background nav indicator

/** An AbortSignal that fires after `ms`; call clear() when the work is done. */
function timeoutSignal(ms) {
  const ctrl = new AbortController();
  const timer = setTimeout(() => ctrl.abort(), ms);
  return { signal: ctrl.signal, clear: () => clearTimeout(timer) };
}

function initLlmSettings() {
  el = {
    root: $("llmModal"),
    closeBtn: $("llmCloseBtn"),
    baseUrl: $("llmBaseUrl"),
    model: $("llmModel"),
    modelList: $("llmModelList"),
    apiKey: $("llmApiKey"),
    temperature: $("llmTemperature"),
    independent: $("llmIndependent"),
    refreshBtn: $("llmRefreshBtn"),
    testBtn: $("llmTestBtn"),
    status: $("llmStatus"),
    templatesBtn: $("llmTemplatesBtn"),
    saveBtn: $("llmSaveBtn"),
  };

  el.closeBtn.addEventListener("click", closeLlmSettings);
  el.root.querySelector(".modal-backdrop").addEventListener("click", closeLlmSettings);
  document.addEventListener("keydown", (ev) => {
    if (el.root.hidden || ev.key !== "Escape") return;
    // The templates manager opens on top of this dialog; let it handle Escape.
    const templates = $("promptTemplatesModal");
    if (templates && !templates.hidden) return;
    ev.preventDefault();
    closeLlmSettings();
  });

  el.refreshBtn.addEventListener("click", refreshModels);
  el.testBtn.addEventListener("click", test);
  el.templatesBtn.addEventListener("click", openPromptTemplates);
  el.saveBtn.addEventListener("click", save);
}

function openLlmSettings() {
  populate();
  el.root.hidden = false;
  el.baseUrl.focus();
}

function closeLlmSettings() {
  el.root.hidden = true;
}

/** Reflect the current config into the form fields. */
function populate() {
  el.baseUrl.value = llmConfig.baseUrl || DEFAULT_LLM_CONFIG.baseUrl;
  el.model.value = llmConfig.model || "";
  el.apiKey.value = llmConfig.apiKey || "";
  el.temperature.value =
    llmConfig.temperature ?? DEFAULT_LLM_CONFIG.temperature;
  el.independent.checked = llmConfig.independentContext !== false;
  setStatus("", "");
}

/** Read the form into a config patch (base URL is normalized on save). */
function readForm() {
  const t = parseFloat(el.temperature.value);
  return {
    baseUrl: el.baseUrl.value,
    model: el.model.value.trim(),
    apiKey: el.apiKey.value,
    temperature: Number.isFinite(t) ? t : DEFAULT_LLM_CONFIG.temperature,
    independentContext: el.independent.checked,
  };
}

function setStatus(text, kind) {
  el.status.textContent = text;
  el.status.className = "llm-status" + (kind ? " " + kind : "");
}

/** Fill the model datalist from the endpoint, using the URL/key in the form. */
function fillModelList(models) {
  el.modelList.textContent = "";
  for (const id of models) {
    const opt = document.createElement("option");
    opt.value = id;
    el.modelList.appendChild(opt);
  }
}

async function refreshModels() {
  await withFormApplied(async () => {
    setStatus("Loading models…", "");
    const t = timeoutSignal(TEST_TIMEOUT_MS);
    try {
      const models = await listModels({ signal: t.signal });
      fillModelList(models);
      if (!el.model.value && models.length) el.model.value = models[0];
      setStatus(
        models.length ? `Found ${models.length} model(s).` : "No models loaded.",
        models.length ? "ok" : "warn"
      );
    } catch (err) {
      setStatus(err.message, "error");
    } finally {
      t.clear();
    }
  });
}

async function test() {
  await withFormApplied(async () => {
    setStatus("Testing…", "");
    const t = timeoutSignal(TEST_TIMEOUT_MS);
    const r = await testConnection({ signal: t.signal });
    t.clear();
    setStatus(r.message, r.ok ? "ok" : "error");
    if (r.ok && r.models.length) fillModelList(r.models);
  });
}

async function save() {
  await saveLlmConfig(readForm());
  toast("AI settings saved.");
  closeLlmSettings();
  refreshLlmStatus(); // re-probe so the nav indicator reflects the new config
}

/**
 * Update the nav "AI settings" status dot to reflect the SAVED config:
 * grey = not configured, amber = configured but unreachable / checking,
 * green = connected (verified). Safe to call fire-and-forget.
 */
async function refreshLlmStatus() {
  const dot = $("llmStatusDot");
  const btn = $("llmSettingsBtn");

  if (!isLlmConfigured()) {
    apply(dot, btn, "off", "AI not configured — click to set it up");
    return;
  }

  apply(dot, btn, "checking", "Checking AI connection…");
  const t = timeoutSignal(STATUS_TIMEOUT_MS);
  const r = await testConnection({ signal: t.signal });
  t.clear();

  apply(
    dot,
    btn,
    r.ok ? "on" : "warn",
    r.ok
      ? `AI connected — ${llmConfig.model} @ ${llmConfig.baseUrl}`
      : `AI configured but unreachable: ${r.message}`
  );
}

/** Update the nav dot + tooltip and broadcast the status to feature listeners. */
function apply(dot, btn, state, title) {
  if (dot) dot.className = "llm-dot llm-dot-" + state;
  if (btn) btn.title = title;
  setLlmStatus(state);
}

/**
 * Refresh/Test must use the URL and key the user is currently typing, not just
 * the last saved values. Apply the form to `llmConfig` for the duration of the
 * probe, then restore the saved state so nothing is persisted until Save.
 */
async function withFormApplied(fn) {
  const snapshot = { ...llmConfig };
  Object.assign(llmConfig, readForm());
  try {
    await fn();
  } finally {
    Object.assign(llmConfig, snapshot);
  }
}

exports.initLlmSettings = initLlmSettings;
exports.openLlmSettings = openLlmSettings;
exports.refreshLlmStatus = refreshLlmStatus;
  });
  register("js/llm/llm-prompt-templates.js", function (module, exports, require) {
const { PURPOSE_DEFS, DEFAULT_TEMPLATE_ID, getPurpose, promptTree, llmTemplates, saveLlmTemplates } = require("js/llm/llm-prompts.js");
const { listModels } = require("js/llm/llm-client.js");
const { toast } = require("js/toast.js");

/**
 * Full-screen "Prompt templates" manager.
 *
 * Sidebar = purposes (grouped: Image prompts, later Scene / Field prompts).
 * Pane   = for the active purpose: a template picker (built-in Default + your
 * named templates) with New / Duplicate / Delete, then the editable fields.
 * The Default is read-only; duplicate it to customize.
 *
 * All edits happen on a local `draft` (a deep copy of the store) so switching
 * purposes/templates never loses changes; Save commits the whole draft.
 */

const $ = (id) => document.getElementById(id);
const uid = () => "t_" + Math.random().toString(36).slice(2, 9);

let el;
/** @type {Record<string, {selected:string, templates:any[]}>} */
let draft = {};
let activePurpose = null;
/** Id of the template currently shown in the editor. */
let viewingId = DEFAULT_TEMPLATE_ID;

function initPromptTemplates() {
  el = {
    root: $("promptTemplatesModal"),
    sidebar: $("ptSidebar"),
    pane: $("ptPane"),
    modelList: $("ptModelList"),
    saveBtn: $("ptSaveBtn"),
    closeBtn: $("ptCloseBtn"),
  };

  el.closeBtn.addEventListener("click", close);
  el.root.querySelector(".modal-backdrop").addEventListener("click", close);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      close();
    }
  });
  el.saveBtn.addEventListener("click", save);
}

function openPromptTemplates() {
  draft = JSON.parse(JSON.stringify(llmTemplates));
  for (const p of PURPOSE_DEFS) {
    if (!draft[p.id]) draft[p.id] = { selected: DEFAULT_TEMPLATE_ID, templates: [], defaultModel: "" };
    if (!Array.isArray(draft[p.id].templates)) draft[p.id].templates = [];
    if (typeof draft[p.id].defaultModel !== "string") draft[p.id].defaultModel = "";
  }
  activePurpose = PURPOSE_DEFS[0]?.id || null;
  viewingId = DEFAULT_TEMPLATE_ID;
  renderSidebar();
  renderPane();
  el.root.hidden = false;
  loadModelSuggestions(); // best-effort, fills the per-template model datalist
}

/** Populate the model datalist from the server (best-effort; ignored on error). */
async function loadModelSuggestions() {
  try {
    const models = await listModels();
    el.modelList.textContent = "";
    for (const id of models) {
      const o = document.createElement("option");
      o.value = id;
      el.modelList.appendChild(o);
    }
  } catch {
    /* offline / no server — the model field still works as free text */
  }
}

function close() {
  el.root.hidden = true;
}

function renderSidebar() {
  el.sidebar.textContent = "";
  for (const { area, groups } of promptTree()) {
    const areaEl = document.createElement("div");
    areaEl.className = "pt-area-title";
    areaEl.textContent = area;
    el.sidebar.appendChild(areaEl);

    for (const g of groups) {
      if (g.label) {
        const typeEl = document.createElement("div");
        typeEl.className = "pt-type-title";
        typeEl.textContent = g.label;
        el.sidebar.appendChild(typeEl);
      }
      for (const p of g.purposes) {
        const item = document.createElement("button");
        item.type = "button";
        item.className =
          "pt-sidebar-item" +
          (p.id === activePurpose ? " active" : "") +
          (g.label ? " pt-nested" : "");
        item.textContent = p.label;
        item.addEventListener("click", () => {
          activePurpose = p.id;
          viewingId = DEFAULT_TEMPLATE_ID;
          renderSidebar();
          renderPane();
        });
        el.sidebar.appendChild(item);
      }
    }
  }
}

/** The template object being viewed (a real draft ref for user templates). */
function viewing() {
  const purpose = getPurpose(activePurpose);
  if (viewingId === DEFAULT_TEMPLATE_ID) {
    return { id: DEFAULT_TEMPLATE_ID, name: "Default", values: { ...purpose.default }, builtin: true };
  }
  return draft[activePurpose].templates.find((t) => t.id === viewingId) || null;
}

function mkBtn(text, onClick, opts = {}) {
  const b = document.createElement("button");
  b.type = "button";
  b.className = "btn" + (opts.className ? " " + opts.className : "");
  b.textContent = text;
  if (opts.disabled) b.disabled = true;
  b.addEventListener("click", onClick);
  return b;
}

function renderPane() {
  el.pane.textContent = "";
  const purpose = getPurpose(activePurpose);
  if (!purpose) return;

  const title = document.createElement("div");
  title.className = "pt-purpose-title";
  title.textContent = purpose.label;
  const desc = document.createElement("p");
  desc.className = "scene-hint";
  desc.textContent = purpose.description || "";
  el.pane.append(title, desc);

  // Template picker + actions.
  const bar = document.createElement("div");
  bar.className = "pt-tpl-bar";
  const label = document.createElement("span");
  label.className = "llm-label";
  label.textContent = "Template";

  const select = document.createElement("select");
  select.className = "llm-input";
  const list = [
    { id: DEFAULT_TEMPLATE_ID, name: "Default" },
    ...draft[activePurpose].templates,
  ];
  for (const t of list) {
    const opt = document.createElement("option");
    opt.value = t.id;
    opt.textContent = t.name;
    select.appendChild(opt);
  }
  select.value = viewingId;
  select.addEventListener("change", () => {
    viewingId = select.value;
    renderPane();
  });

  const newBtn = mkBtn("+ New", () => {
    const id = uid();
    draft[activePurpose].templates.push({
      id,
      name: "New template",
      values: { ...purpose.default },
      model: "",
    });
    viewingId = id;
    renderPane();
  });
  const dupBtn = mkBtn("Duplicate", () => {
    const cur = viewing();
    const id = uid();
    draft[activePurpose].templates.push({
      id,
      name: "Copy of " + cur.name,
      values: { ...cur.values },
      model: cur.model || "",
    });
    viewingId = id;
    renderPane();
  });
  const delBtn = mkBtn(
    "Delete",
    () => {
      if (viewingId === DEFAULT_TEMPLATE_ID) return;
      if (!window.confirm("Delete this template?")) return;
      const s = draft[activePurpose];
      s.templates = s.templates.filter((t) => t.id !== viewingId);
      if (s.selected === viewingId) s.selected = DEFAULT_TEMPLATE_ID;
      viewingId = DEFAULT_TEMPLATE_ID;
      renderPane();
    },
    { className: "danger", disabled: viewingId === DEFAULT_TEMPLATE_ID }
  );

  bar.append(label, select, newBtn, dupBtn, delBtn);
  el.pane.append(bar);

  const cur = viewing();
  const builtin = !!cur.builtin;

  if (builtin) {
    const note = document.createElement("p");
    note.className = "scene-hint";
    note.textContent =
      "Built-in default — its prompts are read-only, but you can set its Model below (no need to duplicate).";
    el.pane.append(note);
  } else {
    const nameField = document.createElement("div");
    nameField.className = "pt-field";
    const nlabel = document.createElement("span");
    nlabel.className = "llm-label";
    nlabel.textContent = "Name";
    const nameInput = document.createElement("input");
    nameInput.type = "text";
    nameInput.className = "llm-input";
    nameInput.value = cur.name;
    nameInput.addEventListener("input", () => {
      cur.name = nameInput.value;
      const opt = [...select.options].find((o) => o.value === cur.id);
      if (opt) opt.textContent = nameInput.value; // keep the picker label in sync
    });
    nameField.append(nlabel, nameInput);
    el.pane.append(nameField);
  }

  // Per-template model (empty = the global model from AI settings). Editable for
  // EVERY template, including the built-in Default (via its defaultModel).
  const modelField = document.createElement("div");
  modelField.className = "pt-field";
  const mlabel = document.createElement("span");
  mlabel.className = "llm-label";
  mlabel.textContent = "Model";
  const mdesc = document.createElement("p");
  mdesc.className = "scene-hint";
  mdesc.textContent =
    "Leave empty to use the global model (AI settings). LM Studio loads it on demand.";
  const modelInput = document.createElement("input");
  modelInput.type = "text";
  modelInput.className = "llm-input";
  modelInput.setAttribute("list", "ptModelList");
  modelInput.placeholder = "(global default)";
  modelInput.value = builtin ? draft[activePurpose].defaultModel || "" : cur.model || "";
  modelInput.addEventListener("input", () => {
    const v = modelInput.value.trim();
    if (builtin) draft[activePurpose].defaultModel = v;
    else cur.model = v;
  });
  modelField.append(mlabel, mdesc, modelInput);
  el.pane.append(modelField);

  for (const f of purpose.fields) {
    const field = document.createElement("div");
    field.className = "pt-field";
    const head = document.createElement("div");
    head.className = "pt-field-head";
    const flabel = document.createElement("span");
    flabel.className = "llm-label";
    flabel.textContent = f.label;
    head.append(flabel);
    const fdesc = document.createElement("p");
    fdesc.className = "scene-hint";
    fdesc.textContent = f.description || "";
    const ta = document.createElement("textarea");
    ta.className = "ai-gen-ta pt-textarea";
    ta.spellcheck = false;
    ta.value = cur.values[f.key] ?? purpose.default[f.key] ?? "";
    if (builtin) {
      ta.readOnly = true;
    } else {
      ta.addEventListener("input", () => {
        cur.values[f.key] = ta.value;
      });
    }
    field.append(head, fdesc, ta);
    el.pane.append(field);
  }
}

async function save() {
  await saveLlmTemplates(draft);
  toast("Prompt templates saved.");
  close();
}

exports.initPromptTemplates = initPromptTemplates;
exports.openPromptTemplates = openPromptTemplates;
  });
  register("js/image-ai.js", function (module, exports, require) {
const { chat } = require("js/llm/llm-client.js");
const { isLlmOnline } = require("js/llm/llm-config.js");
const { listTemplates, getSelectedTemplateId, setSelectedTemplate, getTemplateValue, getTemplateModel } = require("js/llm/llm-prompts.js");
const { toast } = require("js/toast.js");

/** The prompt purpose this feature uses. */
const PURPOSE = "image.generate";

/**
 * "Generate with AI" for the Image-prompts section: sends the open image plus a
 * system/user prompt to a vision-capable LLM and returns a prompt the user can
 * edit, retry, or accept as the image's prompt.
 *
 * This is a feature module: it owns the #aiGenModal and the generation flow, but
 * gets the image file and applies the accepted result through callbacks so it
 * stays decoupled from app.js.
 */

const $ = (id) => document.getElementById(id);

/** Abort a generation that hangs (server accepted the request but never replies). */
const GEN_TIMEOUT_MS = 120000;

let el;
/** @type {{ getFile: (entry:any)=>Promise<File>, applyPrompt: (text:string)=>void }} */
let deps;
let currentEntry = null;
let currentDataUrl = null;
/** Preview starts hidden for privacy; the image is still sent to the LLM. */
let imageShown = false;
/** @type {AbortController|null} */
let inflight = null;
/** Conversation so far (null before the first generation). Refine appends turns. */
let history = null;

function initImageAi(d) {
  deps = d;
  el = {
    root: $("aiGenModal"),
    closeBtn: $("aiGenCloseBtn"),
    img: $("aiGenImg"),
    toggleImgBtn: $("aiGenToggleImgBtn"),
    imgName: $("aiGenImgName"),
    template: $("aiGenTemplate"),
    system: $("aiGenSystem"),
    user: $("aiGenUser"),
    runBtn: $("aiGenRunBtn"),
    refineBtn: $("aiGenRefineBtn"),
    stopBtn: $("aiGenStopBtn"),
    status: $("aiGenStatus"),
    result: $("aiGenResult"),
    acceptBtn: $("aiGenAcceptBtn"),
  };

  el.closeBtn.addEventListener("click", close);
  el.root.querySelector(".modal-backdrop").addEventListener("click", close);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      close();
    }
  });

  el.runBtn.addEventListener("click", run); // "Generate", relabels to "Regenerate"
  el.refineBtn.addEventListener("click", refine);
  el.stopBtn.addEventListener("click", abortInflight);
  el.acceptBtn.addEventListener("click", accept);
  el.toggleImgBtn.addEventListener("click", toggleImage);
  el.template.addEventListener("change", onTemplateChange);
  el.result.addEventListener("input", () => syncButtons());
}

async function openImageAi(entry) {
  if (!entry) return;
  if (!isLlmOnline()) {
    toast("AI not connected — check AI settings.", "warn");
    return;
  }
  currentEntry = entry;
  currentDataUrl = null;
  imageShown = false; // always start hidden for privacy
  // Populate the template picker (built-in Default + your saved templates),
  // preselect the last-used one, and prefill the fields from it. The prompts
  // stay editable per generation without changing the saved template.
  populateTemplateSelect();
  const sel = getSelectedTemplateId(PURPOSE);
  el.template.value = sel;
  applyTemplate(sel);
  el.result.value = "";
  history = null; // fresh session
  el.runBtn.textContent = "Generate";
  el.refineBtn.hidden = true;
  el.imgName.textContent = entry.name;
  syncImageToggle();
  setBusy(false);
  setStatus("Loading image…", "");
  el.root.hidden = false;

  try {
    const file = await deps.getFile(entry);
    currentDataUrl = await fileToDataUrl(file);
    syncImageToggle(); // reveal now only if the user already asked to show it
    setStatus("Ready.", "");
  } catch (err) {
    setStatus("Could not load image: " + err.message, "error");
  }
  syncButtons();
}

/** Fill the template <select> with Default + the user's saved templates. */
function populateTemplateSelect() {
  el.template.textContent = "";
  for (const t of listTemplates(PURPOSE)) {
    const opt = document.createElement("option");
    opt.value = t.id;
    opt.textContent = t.name;
    el.template.appendChild(opt);
  }
}

/** Load a template's field values into the editable prompt fields. */
function applyTemplate(templateId) {
  el.system.value = getTemplateValue(PURPOSE, templateId, "system");
  el.user.value = getTemplateValue(PURPOSE, templateId, "user");
}

/** Picking a template fills the fields and is remembered for next time. */
function onTemplateChange() {
  applyTemplate(el.template.value);
  setSelectedTemplate(PURPOSE, el.template.value);
}

function toggleImage() {
  imageShown = !imageShown;
  syncImageToggle();
}

/** Reflect `imageShown` into the preview; keeps pixels out of the DOM when hidden. */
function syncImageToggle() {
  el.toggleImgBtn.textContent = imageShown ? "Hide image" : "Show image";
  if (imageShown && currentDataUrl) {
    el.img.src = currentDataUrl;
    el.img.hidden = false;
  } else {
    el.img.removeAttribute("src");
    el.img.hidden = true;
  }
}

function close() {
  abortInflight();
  imageShown = false;
  el.img.removeAttribute("src"); // drop the pixels from the DOM
  el.root.hidden = true;
}

/** First-turn messages: system + the user prompt with the image attached. */
function buildFreshMessages() {
  return [
    { role: "system", content: el.system.value },
    {
      role: "user",
      content: [
        { type: "text", text: el.user.value },
        { type: "image_url", image_url: { url: currentDataUrl } },
      ],
    },
  ];
}

/** Send a message list; sets the result and returns the assistant text (null on error). */
async function send(messages) {
  abortInflight();
  const ctrl = new AbortController();
  inflight = ctrl;
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
  setBusy(true);
  setStatus("Generating…", "");
  try {
    const text = await chat(messages, {
      signal: ctrl.signal,
      model: getTemplateModel(PURPOSE, el.template.value),
    });
    el.result.value = text.trim();
    setStatus(
      el.result.value ? "Done." : "The model returned an empty response.",
      el.result.value ? "ok" : "warn"
    );
    return el.result.value;
  } catch (err) {
    setStatus(err.message, "error");
    return null;
  } finally {
    clearTimeout(timer);
    inflight = null;
    setBusy(false);
  }
}

function afterGenerated() {
  el.runBtn.textContent = "Regenerate";
  el.refineBtn.hidden = false;
}

/** Fresh generation from the image + fields (discards any prior conversation). */
async function run() {
  if (!currentDataUrl) {
    setStatus("Image not loaded yet.", "warn");
    return;
  }
  const messages = buildFreshMessages();
  const text = await send(messages);
  if (text != null) {
    history = [...messages, { role: "assistant", content: el.result.value }];
    afterGenerated();
  }
}

/** Refine: keep the conversation (incl. the image turn) and send the user prompt anew. */
async function refine() {
  if (!history) return run();
  const base = history.slice();
  if (base.length && base[base.length - 1].role === "assistant") {
    base[base.length - 1] = { role: "assistant", content: el.result.value };
  }
  const instruction = el.user.value.trim() || "Refine the previous result.";
  const messages = [...base, { role: "user", content: instruction }];
  const text = await send(messages);
  if (text != null) {
    history = [...messages, { role: "assistant", content: el.result.value }];
    afterGenerated();
  }
}

function accept() {
  const text = el.result.value;
  if (!text.trim()) return;
  deps.applyPrompt(text);
  toast("Prompt applied — review and save.");
  close();
}

function abortInflight() {
  if (inflight) {
    inflight.abort();
    inflight = null;
  }
}

/** Enable/disable controls for the generating vs idle states. */
function setBusy(busy) {
  el.stopBtn.hidden = !busy;
  el.refineBtn.disabled = busy;
  syncButtons(busy);
}

/** Generate needs a loaded image; Accept needs a (non-empty) result; neither while busy. */
function syncButtons(busy = false) {
  const hasResult = el.result.value.trim().length > 0;
  el.runBtn.disabled = busy || !currentDataUrl;
  el.acceptBtn.disabled = busy || !hasResult;
}

function setStatus(text, kind) {
  el.status.textContent = text;
  el.status.className = "llm-status" + (kind ? " " + kind : "");
}

function fileToDataUrl(file) {
  return new Promise((resolve, reject) => {
    const r = new FileReader();
    r.onload = () => resolve(r.result);
    r.onerror = () => reject(r.error || new Error("read failed"));
    r.readAsDataURL(file);
  });
}

exports.initImageAi = initImageAi;
exports.openImageAi = openImageAi;
  });
  register("js/ask-ai.js", function (module, exports, require) {
const { chat } = require("js/llm/llm-client.js");
const { isLlmOnline } = require("js/llm/llm-config.js");
const { PURPOSE_DEFS, listTemplates, getTemplateValue, getTemplateModel } = require("js/llm/llm-prompts.js");
const { toast } = require("js/toast.js");

/**
 * "Ask AI" — a general-purpose LLM playground, independent of any feature flow.
 * Pick any prompt template (across all purposes) to prefill, edit the system and
 * user prompts, optionally attach an image, send, and read/copy the response.
 * Nothing is applied anywhere; it's a scratchpad.
 */

const $ = (id) => document.getElementById(id);
const GEN_TIMEOUT_MS = 120000;

let el;
let inflight = null;
/** Conversation so far (null before the first send). Refine appends turns. */
let history = null;

function initAskAi() {
  el = {
    root: $("askModal"),
    closeBtn: $("askCloseBtn"),
    template: $("askTemplate"),
    system: $("askSystem"),
    user: $("askUser"),
    imageInput: $("askImageInput"),
    clearImgBtn: $("askClearImgBtn"),
    runBtn: $("askRunBtn"),
    refineBtn: $("askRefineBtn"),
    stopBtn: $("askStopBtn"),
    status: $("askStatus"),
    result: $("askResult"),
    copyBtn: $("askCopyBtn"),
  };

  el.closeBtn.addEventListener("click", close);
  el.root.querySelector(".modal-backdrop").addEventListener("click", close);
  document.addEventListener("keydown", (ev) => {
    if (!el.root.hidden && ev.key === "Escape") {
      ev.preventDefault();
      close();
    }
  });

  el.template.addEventListener("change", onTemplateChange);
  el.imageInput.addEventListener("change", () => {
    el.clearImgBtn.hidden = el.imageInput.files.length === 0;
  });
  el.clearImgBtn.addEventListener("click", () => {
    el.imageInput.value = "";
    el.clearImgBtn.hidden = true;
  });
  el.runBtn.addEventListener("click", run);
  el.refineBtn.addEventListener("click", refine);
  el.stopBtn.addEventListener("click", abortInflight);
  el.result.addEventListener("input", syncCopy);
  el.copyBtn.addEventListener("click", copy);
}

function openAskAi() {
  populateTemplates();
  history = null; // fresh session
  el.refineBtn.hidden = true; // appears after the first send
  setBusy(false);
  setStatus("", "");
  syncCopy();
  el.root.hidden = false;
  el.user.focus();
}

function close() {
  abortInflight();
  el.root.hidden = true;
}

/** Build the template picker: a Custom entry, then all templates by purpose. */
function populateTemplates() {
  const prev = el.template.value;
  el.template.textContent = "";
  const custom = document.createElement("option");
  custom.value = "";
  custom.textContent = "— Custom (no template) —";
  el.template.appendChild(custom);
  for (const p of PURPOSE_DEFS) {
    const og = document.createElement("optgroup");
    og.label = `${p.area} › ${p.label}`;
    for (const t of listTemplates(p.id)) {
      const o = document.createElement("option");
      o.value = `${p.id}::${t.id}`;
      o.textContent = t.name;
      og.appendChild(o);
    }
    el.template.appendChild(og);
  }
  // Keep the previous selection if it still exists.
  el.template.value = [...el.template.options].some((o) => o.value === prev) ? prev : "";
}

/** Selecting a template prefills system + user; Custom leaves them as-is. */
function onTemplateChange() {
  const [pid, tid] = el.template.value.split("::");
  if (!pid) return;
  el.system.value = getTemplateValue(pid, tid, "system");
  el.user.value = getTemplateValue(pid, tid, "user");
}

/** First-turn messages: optional system + the user prompt (with image if attached). */
async function buildFreshMessages() {
  const messages = [];
  if (el.system.value.trim()) messages.push({ role: "system", content: el.system.value });
  const file = el.imageInput.files[0];
  if (file) {
    const dataUrl = await fileToDataUrl(file);
    messages.push({
      role: "user",
      content: [
        { type: "text", text: el.user.value },
        { type: "image_url", image_url: { url: dataUrl } },
      ],
    });
  } else {
    messages.push({ role: "user", content: el.user.value });
  }
  return messages;
}

/** Send a message list; sets the result and returns the assistant text (null on error). */
async function send(messages) {
  abortInflight();
  const ctrl = new AbortController();
  inflight = ctrl;
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
  setBusy(true);
  setStatus("Sending…", "");
  try {
    const [pid, tid] = el.template.value.split("::");
    const model = pid ? getTemplateModel(pid, tid) : "";
    const text = await chat(messages, { signal: ctrl.signal, model });
    el.result.value = text.trim();
    setStatus(el.result.value ? "Done." : "Empty response.", el.result.value ? "ok" : "warn");
    return el.result.value;
  } catch (err) {
    setStatus(err.message, "error");
    return null;
  } finally {
    clearTimeout(timer);
    inflight = null;
    setBusy(false);
    syncCopy();
  }
}

/** Fresh send: rebuild from the fields, discarding any prior conversation. */
async function run() {
  let messages;
  try {
    messages = await buildFreshMessages();
  } catch (err) {
    setStatus("Could not read image: " + err.message, "error");
    return;
  }
  const text = await send(messages);
  if (text != null) {
    history = [...messages, { role: "assistant", content: el.result.value }];
    el.refineBtn.hidden = false;
  }
}

/** Refine: keep the conversation and send the current user prompt as a new turn. */
async function refine() {
  if (!history) return run();
  const base = history.slice();
  if (base.length && base[base.length - 1].role === "assistant") {
    base[base.length - 1] = { role: "assistant", content: el.result.value };
  }
  const instruction = el.user.value.trim() || "Refine the previous response.";
  const messages = [...base, { role: "user", content: instruction }];
  const text = await send(messages);
  if (text != null) {
    history = [...messages, { role: "assistant", content: el.result.value }];
    el.refineBtn.hidden = false;
  }
}

async function copy() {
  try {
    await navigator.clipboard.writeText(el.result.value);
    toast("Response copied.");
  } catch {
    toast("Clipboard blocked by the browser.", "warn");
  }
}

function abortInflight() {
  if (inflight) {
    inflight.abort();
    inflight = null;
  }
}

function setBusy(busy) {
  el.runBtn.disabled = busy;
  el.refineBtn.disabled = busy;
  el.stopBtn.hidden = !busy;
}

function syncCopy() {
  el.copyBtn.disabled = el.result.value.trim().length === 0;
}

function setStatus(text, kind) {
  el.status.textContent = text;
  el.status.className = "llm-status" + (kind ? " " + kind : "");
}

function fileToDataUrl(file) {
  return new Promise((resolve, reject) => {
    const r = new FileReader();
    r.onload = () => resolve(r.result);
    r.onerror = () => reject(r.error || new Error("read failed"));
    r.readAsDataURL(file);
  });
}

exports.initAskAi = initAskAi;
exports.openAskAi = openAskAi;
  });
  require("js/app.js");
})();

</script>
</body>
</html>