File size: 221,731 Bytes
abab3e7
d041f14
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d3d7e
abab3e7
 
 
 
 
24adae3
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d1b9cb
 
abab3e7
 
 
 
 
 
 
8d1b9cb
 
 
 
 
abab3e7
 
 
 
 
 
8d1b9cb
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24adae3
 
 
 
 
abab3e7
24adae3
abab3e7
24adae3
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8862f
abab3e7
 
 
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
8d1b9cb
db8862f
abab3e7
 
8d1b9cb
 
 
 
 
 
 
 
 
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da7654a
db8862f
da7654a
db8862f
d013202
da7654a
d013202
db8862f
 
 
 
 
abab3e7
8d1b9cb
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d1b9cb
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8862f
abab3e7
 
 
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
8d1b9cb
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8862f
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
007d8c8
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
007d8c8
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb452ae
 
abab3e7
 
fb452ae
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb452ae
 
 
 
abab3e7
 
 
 
 
 
fb452ae
 
 
 
 
 
abab3e7
 
 
6b43e51
 
 
 
 
abab3e7
 
 
6b43e51
 
 
 
abab3e7
 
6b43e51
abab3e7
 
6b43e51
 
 
abab3e7
6b43e51
d041f14
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b43e51
 
 
 
 
 
 
 
abab3e7
 
 
6b43e51
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
6b43e51
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b43e51
abab3e7
6b43e51
 
abab3e7
6b43e51
abab3e7
 
 
 
 
 
6b43e51
abab3e7
 
 
 
 
 
6b43e51
abab3e7
 
6b43e51
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d3d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d3d7e
 
 
 
 
abab3e7
d0d3d7e
6b43e51
 
 
 
 
 
 
 
d0d3d7e
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b43e51
abab3e7
 
 
 
 
fb452ae
 
abab3e7
6b43e51
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
6b43e51
abab3e7
 
 
 
 
6b43e51
abab3e7
 
 
6b43e51
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
d0d3d7e
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d3d7e
abab3e7
 
 
d0d3d7e
abab3e7
d0d3d7e
 
 
 
abab3e7
 
d013202
 
20786d3
 
 
 
 
d013202
d0d3d7e
 
d013202
 
 
20786d3
 
 
 
 
 
 
 
 
 
abab3e7
d0d3d7e
20786d3
d013202
 
20786d3
d013202
abab3e7
 
 
d0d3d7e
abab3e7
 
d013202
 
20786d3
 
 
d013202
 
20786d3
 
 
 
 
 
d013202
 
 
 
 
 
 
20786d3
 
 
 
d013202
abab3e7
d013202
d0d3d7e
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb452ae
 
abab3e7
fb452ae
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb452ae
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb452ae
 
abab3e7
 
 
 
 
da7654a
 
abab3e7
da7654a
 
d013202
abab3e7
 
 
 
 
d013202
 
 
 
 
 
 
 
 
 
 
 
 
da7654a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d013202
da7654a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d013202
 
da7654a
d013202
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09fc0c4
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
db8862f
abab3e7
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
db8862f
abab3e7
 
 
db8862f
 
 
 
abab3e7
db8862f
abab3e7
db8862f
 
abab3e7
db8862f
abab3e7
 
 
 
 
db8862f
abab3e7
 
 
 
 
db8862f
abab3e7
 
 
 
 
 
db8862f
abab3e7
db8862f
 
 
abab3e7
 
 
 
db8862f
 
 
 
abab3e7
db8862f
 
abab3e7
db8862f
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
db8862f
 
abab3e7
db8862f
abab3e7
 
 
db8862f
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
db8862f
abab3e7
 
db8862f
abab3e7
 
 
 
 
 
 
db8862f
 
 
 
abab3e7
 
 
 
 
 
 
 
db8862f
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
db8862f
 
 
 
abab3e7
 
 
 
 
 
 
 
 
 
db8862f
 
 
 
abab3e7
 
 
 
 
 
 
 
db8862f
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
db8862f
 
 
 
abab3e7
 
 
 
 
 
 
 
db8862f
abab3e7
 
db8862f
 
 
 
abab3e7
 
 
 
 
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
db8862f
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
db8862f
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
db8862f
 
 
abab3e7
 
db8862f
abab3e7
db8862f
 
 
abab3e7
 
 
 
 
db8862f
 
 
abab3e7
db8862f
 
abab3e7
 
 
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
 
db8862f
 
 
 
abab3e7
db8862f
 
 
abab3e7
db8862f
 
abab3e7
 
 
 
 
 
 
 
 
db8862f
 
 
 
 
 
 
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
db8862f
abab3e7
 
 
 
db8862f
 
 
 
 
abab3e7
db8862f
 
abab3e7
 
 
d041f14
abab3e7
 
 
 
 
 
 
d041f14
abab3e7
 
 
db8862f
 
 
 
 
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
412b593
 
abab3e7
 
412b593
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8862f
abab3e7
 
 
 
 
 
 
db8862f
abab3e7
 
 
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
db8862f
 
abab3e7
db8862f
 
abab3e7
 
 
 
db8862f
 
 
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
abab3e7
db8862f
 
 
 
abab3e7
db8862f
 
 
 
 
abab3e7
 
db8862f
 
 
 
 
 
 
8d1b9cb
 
db8862f
 
 
 
abab3e7
db8862f
 
 
 
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
db8862f
 
abab3e7
db8862f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
db8862f
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d3d7e
 
 
abab3e7
 
d0d3d7e
 
 
 
 
 
 
 
 
 
e866214
d0d3d7e
 
e866214
 
d0d3d7e
 
abab3e7
 
 
 
 
 
 
 
d0d3d7e
8d1b9cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d3d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abab3e7
 
 
 
db8862f
abab3e7
 
 
db8862f
 
abab3e7
 
 
 
 
db8862f
 
 
abab3e7
db8862f
 
abab3e7
 
 
db8862f
abab3e7
 
 
 
 
 
 
 
db8862f
 
 
abab3e7
8d1b9cb
db8862f
abab3e7
 
 
 
 
db8862f
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d1b9cb
db8862f
abab3e7
8d1b9cb
db8862f
 
 
abab3e7
8d1b9cb
db8862f
abab3e7
 
 
 
 
db8862f
 
 
abab3e7
 
db8862f
abab3e7
 
 
 
db8862f
 
abab3e7
 
 
 
db8862f
 
abab3e7
 
 
 
 
 
 
 
 
 
 
db8862f
d041f14
db8862f
 
abab3e7
 
 
 
 
 
 
 
 
 
 
 
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
"""
Design System Automation β€” Main Application
==============================================

Flow:
1. User enters URL
2. Agent 1 discovers pages β†’ User confirms
3. Agent 1 extracts tokens (Desktop + Mobile)
4. Agent 2 normalizes tokens
5. Stage 1 UI: User reviews tokens (accept/reject, Desktop↔Mobile toggle)
6. Agent 3 proposes upgrades
7. Stage 2 UI: User selects options with live preview
8. Agent 4 generates JSON
9. Stage 3 UI: User exports
"""

import os
import asyncio
import json
import gradio as gr
from datetime import datetime
from typing import Optional

# Get HF token from environment
HF_TOKEN_FROM_ENV = os.getenv("HF_TOKEN", "")

# =============================================================================
# GLOBAL STATE
# =============================================================================

class AppState:
    """Global application state."""
    def __init__(self):
        self.reset()
    
    def reset(self):
        self.discovered_pages = []
        self.base_url = ""
        self.desktop_raw = None  # ExtractedTokens
        self.mobile_raw = None   # ExtractedTokens
        self.desktop_normalized = None  # NormalizedTokens
        self.mobile_normalized = None   # NormalizedTokens
        self.upgrade_recommendations = None  # UpgradeRecommendations
        self.selected_upgrades = {}  # User selections
        self.color_classification = None  # ClassificationResult from rule-based classifier
        self.logs = []
    
    def log(self, message: str):
        timestamp = datetime.now().strftime("%H:%M:%S")
        self.logs.append(f"[{timestamp}] {message}")
        if len(self.logs) > 500:
            self.logs.pop(0)
    
    def get_logs(self) -> str:
        return "\n".join(self.logs)

state = AppState()


# =============================================================================
# MESSAGE HELPERS
# =============================================================================

def success_message(title: str, details: str, next_step: str) -> str:
    """Generate a formatted success message with next-step guidance."""
    return f"## βœ… {title}\n\n{details}\n\n**Next step:** {next_step}"


def error_message(title: str, details: str, how_to_fix: str) -> str:
    """Generate a formatted error message with fix guidance."""
    return f"## ❌ {title}\n\n{details}\n\n**How to fix:** {how_to_fix}"


# =============================================================================
# LAZY IMPORTS
# =============================================================================

def get_crawler():
    import agents.crawler
    return agents.crawler

def get_extractor():
    import agents.extractor
    return agents.extractor

def get_normalizer():
    import agents.normalizer
    return agents.normalizer

def get_advisor():
    import agents.advisor
    return agents.advisor

def get_schema():
    import core.token_schema
    return core.token_schema


# =============================================================================
# PHASE 1: DISCOVER PAGES
# =============================================================================

async def discover_pages(url: str, progress=gr.Progress()):
    """Discover pages from URL."""
    state.reset()
    
    if not url or not url.startswith(("http://", "https://")):
        return error_message("Invalid URL",
                             "The URL must start with `https://` or `http://`.",
                             "Enter a full URL like `https://example.com` and try again."), "", None
    
    state.log(f"πŸš€ Starting discovery for: {url}")
    progress(0.1, desc="πŸ” Discovering pages...")
    
    try:
        crawler = get_crawler()
        discoverer = crawler.PageDiscoverer()
        
        pages = await discoverer.discover(url)
        
        state.discovered_pages = pages
        state.base_url = url
        
        state.log(f"βœ… Found {len(pages)} pages")
        
        # Format for display
        pages_data = []
        for page in pages:
            pages_data.append([
                True,  # Selected by default
                page.url,
                page.title if page.title else "(No title)",
                page.page_type.value,
                "βœ“" if not page.error else f"⚠ {page.error}"
            ])
        
        progress(1.0, desc="βœ… Discovery complete!")
        
        status = success_message(
            f"Found {len(pages)} Pages",
            f"The crawler discovered **{len(pages)} pages** on `{url}`. Review the table below β€” "
            "use the **Select** checkboxes to choose which pages to scan for design tokens.",
            "Click **'Extract Tokens (Desktop + Mobile)'** to begin extraction."
        )

        return status, state.get_logs(), pages_data
        
    except Exception as e:
        import traceback
        state.log(f"❌ Error: {str(e)}")
        error_detail = str(e).lower()
        if "timeout" in error_detail:
            hint = "The website took too long to respond. Try again, or check if the site is accessible in your browser."
        elif "dns" in error_detail or "name resolution" in error_detail:
            hint = "Could not find this website. Please check the URL for typos."
        elif "ssl" in error_detail or "certificate" in error_detail:
            hint = "SSL/certificate error. Try using `http://` instead of `https://`, or check if the site has a valid certificate."
        else:
            hint = "Check that the URL is correct and the site is publicly accessible. Review the log above for details."
        return error_message("Discovery Failed", str(e)[:200], hint), state.get_logs(), None


# =============================================================================
# PHASE 2: EXTRACT TOKENS
# =============================================================================

async def extract_tokens(pages_data, progress=gr.Progress()):
    """Extract tokens from selected pages (both viewports)."""
    
    state.log(f"πŸ“₯ Received pages_data type: {type(pages_data)}")
    
    if pages_data is None:
        return (error_message("No Pages Discovered",
                              "No pages have been discovered yet.",
                              "Go to **Step 1** above, enter a URL, and click **'Discover Pages'** first."),
                state.get_logs(), None, None)
    
    # Get selected URLs - handle pandas DataFrame
    selected_urls = []
    
    try:
        # Check if it's a pandas DataFrame
        if hasattr(pages_data, 'iterrows'):
            state.log(f"πŸ“₯ DataFrame with {len(pages_data)} rows, columns: {list(pages_data.columns)}")
            
            for idx, row in pages_data.iterrows():
                # Get values by column name or position
                try:
                    # Try column names first
                    is_selected = row.get('Select', row.iloc[0] if len(row) > 0 else False)
                    url = row.get('URL', row.iloc[1] if len(row) > 1 else '')
                except (KeyError, IndexError, TypeError):
                    # Fallback to positional
                    is_selected = row.iloc[0] if len(row) > 0 else False
                    url = row.iloc[1] if len(row) > 1 else ''
                
                if is_selected and url:
                    selected_urls.append(url)
        
        # If it's a dict (Gradio sometimes sends this)
        elif isinstance(pages_data, dict):
            state.log(f"πŸ“₯ Dict with keys: {list(pages_data.keys())}")
            data = pages_data.get('data', [])
            for row in data:
                if isinstance(row, (list, tuple)) and len(row) >= 2 and row[0]:
                    selected_urls.append(row[1])
        
        # If it's a list
        elif isinstance(pages_data, (list, tuple)):
            state.log(f"πŸ“₯ List with {len(pages_data)} items")
            for row in pages_data:
                if isinstance(row, (list, tuple)) and len(row) >= 2 and row[0]:
                    selected_urls.append(row[1])
                    
    except Exception as e:
        state.log(f"❌ Error parsing pages_data: {str(e)}")
        import traceback
        state.log(traceback.format_exc())
    
    state.log(f"πŸ“‹ Found {len(selected_urls)} selected URLs")
    
    # If still no URLs, try using stored discovered pages
    if not selected_urls and state.discovered_pages:
        state.log("⚠️ No URLs from table, using all discovered pages")
        selected_urls = [p.url for p in state.discovered_pages if not p.error][:10]
    
    if not selected_urls:
        return (error_message("No Pages Selected",
                              "No pages are selected for extraction.",
                              "Go back to the pages table above and check the **Select** boxes for the pages you want to extract, then click this button again."),
                state.get_logs(), None, None)
    
    # Limit to 10 pages for performance
    selected_urls = selected_urls[:10]
    
    state.log(f"πŸ“‹ Extracting from {len(selected_urls)} pages:")
    for url in selected_urls[:3]:
        state.log(f"   β€’ {url}")
    if len(selected_urls) > 3:
        state.log(f"   ... and {len(selected_urls) - 3} more")
    
    progress(0.05, desc="πŸš€ Starting extraction...")
    
    try:
        schema = get_schema()
        extractor_mod = get_extractor()
        normalizer_mod = get_normalizer()
        
        # === DESKTOP EXTRACTION ===
        state.log("")
        state.log("=" * 60)
        state.log("πŸ–₯️ DESKTOP EXTRACTION (1440px)")
        state.log("=" * 60)
        state.log("")
        state.log("πŸ“‘ Enhanced extraction from 7 sources:")
        state.log("   1. DOM computed styles (getComputedStyle)")
        state.log("   2. CSS variables (:root { --color: })")
        state.log("   3. SVG colors (fill, stroke)")
        state.log("   4. Inline styles (style='color:')")
        state.log("   5. Stylesheet rules (CSS files)")
        state.log("   6. External CSS files (fetch & parse)")
        state.log("   7. Page content scan (brute-force)")
        state.log("")
        
        progress(0.1, desc="πŸ–₯️ Extracting desktop tokens...")
        
        desktop_extractor = extractor_mod.TokenExtractor(viewport=schema.Viewport.DESKTOP)
        
        def desktop_progress(p):
            progress(0.1 + (p * 0.35), desc=f"πŸ–₯️ Desktop... {int(p*100)}%")
        
        state.desktop_raw = await desktop_extractor.extract(selected_urls, progress_callback=desktop_progress)
        
        # Log extraction details
        state.log("πŸ“Š EXTRACTION RESULTS:")
        state.log(f"   Colors:     {len(state.desktop_raw.colors)} unique")
        state.log(f"   Typography: {len(state.desktop_raw.typography)} styles")
        state.log(f"   Spacing:    {len(state.desktop_raw.spacing)} values")
        state.log(f"   Radius:     {len(state.desktop_raw.radius)} values")
        state.log(f"   Shadows:    {len(state.desktop_raw.shadows)} values")
        
        # Store foreground-background pairs for real AA checking in Stage 2
        if hasattr(desktop_extractor, 'fg_bg_pairs') and desktop_extractor.fg_bg_pairs:
            state.fg_bg_pairs = desktop_extractor.fg_bg_pairs
            state.log(f"   FG/BG Pairs: {len(state.fg_bg_pairs)} unique pairs for AA checking")
        else:
            state.fg_bg_pairs = []

        # Log CSS variables if found
        if hasattr(desktop_extractor, 'css_variables') and desktop_extractor.css_variables:
            state.log("")
            state.log(f"🎨 CSS Variables found: {len(desktop_extractor.css_variables)}")
            for var_name, var_value in list(desktop_extractor.css_variables.items())[:5]:
                state.log(f"   {var_name}: {var_value}")
            if len(desktop_extractor.css_variables) > 5:
                state.log(f"   ... and {len(desktop_extractor.css_variables) - 5} more")
        
        # Log warnings if any
        if desktop_extractor.warnings:
            state.log("")
            state.log("⚠️ Warnings:")
            for w in desktop_extractor.warnings[:3]:
                state.log(f"   {w}")
        
        # Normalize desktop
        state.log("")
        state.log("πŸ”„ Normalizing (deduping, naming)...")
        state.desktop_normalized = normalizer_mod.normalize_tokens(state.desktop_raw)
        state.log(f"   βœ… Normalized: {len(state.desktop_normalized.colors)} colors, {len(state.desktop_normalized.typography)} typography, {len(state.desktop_normalized.spacing)} spacing")
        
        # === MOBILE EXTRACTION ===
        state.log("")
        state.log("=" * 60)
        state.log("πŸ“± MOBILE EXTRACTION (375px)")
        state.log("=" * 60)
        state.log("")
        
        progress(0.5, desc="πŸ“± Extracting mobile tokens...")
        
        mobile_extractor = extractor_mod.TokenExtractor(viewport=schema.Viewport.MOBILE)
        
        def mobile_progress(p):
            progress(0.5 + (p * 0.35), desc=f"πŸ“± Mobile... {int(p*100)}%")
        
        state.mobile_raw = await mobile_extractor.extract(selected_urls, progress_callback=mobile_progress)
        
        # Log extraction details
        state.log("πŸ“Š EXTRACTION RESULTS:")
        state.log(f"   Colors:     {len(state.mobile_raw.colors)} unique")
        state.log(f"   Typography: {len(state.mobile_raw.typography)} styles")
        state.log(f"   Spacing:    {len(state.mobile_raw.spacing)} values")
        state.log(f"   Radius:     {len(state.mobile_raw.radius)} values")
        state.log(f"   Shadows:    {len(state.mobile_raw.shadows)} values")
        
        # Normalize mobile
        state.log("")
        state.log("πŸ”„ Normalizing...")
        state.mobile_normalized = normalizer_mod.normalize_tokens(state.mobile_raw)
        state.log(f"   βœ… Normalized: {len(state.mobile_normalized.colors)} colors, {len(state.mobile_normalized.typography)} typography, {len(state.mobile_normalized.spacing)} spacing")
        
        # === FIRECRAWL CSS EXTRACTION (Agent 1B) ===
        progress(0.88, desc="πŸ”₯ Firecrawl CSS analysis...")
        
        try:
            from agents.firecrawl_extractor import extract_css_colors
            
            # Get base URL for Firecrawl
            base_url = selected_urls[0] if selected_urls else state.base_url
            
            # Extract CSS colors using Firecrawl
            firecrawl_result = await extract_css_colors(
                url=base_url,
                api_key=None,  # Will use fallback method
                log_callback=state.log
            )
            
            # Merge Firecrawl colors into desktop normalized
            firecrawl_colors = firecrawl_result.get("colors", {})
            
            if firecrawl_colors:
                state.log("")
                state.log("πŸ”€ Merging Firecrawl colors with Playwright extraction...")
                
                # Count new colors
                new_colors_count = 0
                
                for hex_val, color_data in firecrawl_colors.items():
                    # Check if this color already exists
                    existing = False
                    for name, existing_color in state.desktop_normalized.colors.items():
                        if existing_color.value.lower() == hex_val.lower():
                            existing = True
                            # Update frequency
                            existing_color.frequency += color_data.get("frequency", 1)
                            if "firecrawl" not in existing_color.contexts:
                                existing_color.contexts.append("firecrawl")
                            break
                    
                    if not existing:
                        # Add new color from Firecrawl
                        from core.token_schema import ColorToken, TokenSource, Confidence
                        
                        new_token = ColorToken(
                            value=hex_val,
                            frequency=color_data.get("frequency", 1),
                            contexts=["firecrawl"] + color_data.get("contexts", []),
                            elements=["css-file"],
                            css_properties=color_data.get("sources", []),
                            contrast_white=color_data.get("contrast_white", 0),
                            contrast_black=color_data.get("contrast_black", 0),
                            source=TokenSource.DETECTED,
                            confidence=Confidence.MEDIUM,
                        )
                        
                        # Generate name based on color characteristics (not garbage like firecrawl.34)
                        # This will be a fallback; semantic analysis may override later
                        new_token.suggested_name = None  # Let consolidation generate proper name

                        state.desktop_normalized.colors[hex_val] = new_token
                        new_colors_count += 1
                
                state.log(f"   βœ… Added {new_colors_count} new colors from Firecrawl")
                state.log(f"   πŸ“Š Total colors now: {len(state.desktop_normalized.colors)}")
        
        except Exception as e:
            state.log(f"   ⚠️ Firecrawl extraction skipped: {str(e)}")
        
        # === SEMANTIC COLOR ANALYSIS (Agent 1C) ===
        progress(0.92, desc="🧠 Semantic color analysis...")
        
        semantic_result = {}
        semantic_preview_html = ""
        
        try:
            from agents.semantic_analyzer import SemanticColorAnalyzer, generate_semantic_preview_html
            
            # Create analyzer (using rule-based for now, can add LLM later)
            semantic_analyzer = SemanticColorAnalyzer(llm_provider=None)
            
            # Run analysis
            semantic_result = semantic_analyzer.analyze_sync(
                colors=state.desktop_normalized.colors,
                log_callback=state.log
            )
            
            # Store in state for Stage 2
            state.semantic_analysis = semantic_result
            
            # Generate preview HTML
            semantic_preview_html = generate_semantic_preview_html(semantic_result)
            
        except Exception as e:
            state.log(f"   ⚠️ Semantic analysis skipped: {str(e)}")
            import traceback
            state.log(traceback.format_exc())
        
        progress(0.95, desc="πŸ“Š Preparing results...")
        
        # Format results for Stage 1 UI
        desktop_data = format_tokens_for_display(state.desktop_normalized)
        mobile_data = format_tokens_for_display(state.mobile_normalized)
        
        # Generate visual previews - AS-IS for Stage 1 (no ramps, no enhancements)
        state.log("")
        state.log("🎨 Generating AS-IS visual previews...")
        
        from core.preview_generator import (
            generate_typography_preview_html,
            generate_colors_asis_preview_html,
            generate_spacing_asis_preview_html,
            generate_radius_asis_preview_html,
            generate_shadows_asis_preview_html,
        )
        
        # Get detected font
        fonts = get_detected_fonts()
        primary_font = fonts.get("primary", "Open Sans")
        
        # Convert typography tokens to dict format for preview
        typo_dict = {}
        for name, t in state.desktop_normalized.typography.items():
            typo_dict[name] = {
                "font_size": t.font_size,
                "font_weight": t.font_weight,
                "line_height": t.line_height or "1.5",
                "letter_spacing": "0",
            }
        
        # Convert color tokens to dict format for preview (with full metadata)
        color_dict = {}
        for name, c in state.desktop_normalized.colors.items():
            color_dict[name] = {
                "value": c.value,
                "frequency": c.frequency,
                "contexts": c.contexts[:3] if c.contexts else [],
                "elements": c.elements[:3] if c.elements else [],
                "css_properties": c.css_properties[:3] if c.css_properties else [],
                "contrast_white": c.contrast_white,
                "contrast_black": getattr(c, 'contrast_black', 0),
            }
        
        # Convert spacing tokens to dict format
        spacing_dict = {}
        for name, s in state.desktop_normalized.spacing.items():
            spacing_dict[name] = {
                "value": s.value,
                "value_px": s.value_px,
            }
        
        # Convert radius tokens to dict format
        radius_dict = {}
        for name, r in state.desktop_normalized.radius.items():
            radius_dict[name] = {"value": r.value}
        
        # Convert shadow tokens to dict format
        shadow_dict = {}
        for name, s in state.desktop_normalized.shadows.items():
            shadow_dict[name] = {"value": s.value}
        
        # Generate AS-IS previews (Stage 1 - raw extracted values)
        typography_preview_html = generate_typography_preview_html(
            typography_tokens=typo_dict,
            font_family=primary_font,
            sample_text="The quick brown fox jumps over the lazy dog",
        )
        
        # AS-IS color preview (no ramps)
        colors_asis_preview_html = generate_colors_asis_preview_html(
            color_tokens=color_dict,
        )
        
        # AS-IS spacing preview
        spacing_asis_preview_html = generate_spacing_asis_preview_html(
            spacing_tokens=spacing_dict,
        )
        
        # AS-IS radius preview
        radius_asis_preview_html = generate_radius_asis_preview_html(
            radius_tokens=radius_dict,
        )
        
        # AS-IS shadows preview
        shadows_asis_preview_html = generate_shadows_asis_preview_html(
            shadow_tokens=shadow_dict,
        )
        
        state.log("   βœ… Typography preview generated")
        state.log("   βœ… Colors AS-IS preview generated (no ramps)")
        state.log("   βœ… Semantic color analysis preview generated")
        state.log("   βœ… Spacing AS-IS preview generated")
        state.log("   βœ… Radius AS-IS preview generated")
        state.log("   βœ… Shadows AS-IS preview generated")
        
        # Get semantic summary for status
        brand_count = len(semantic_result.get("brand", {}))
        text_count = len(semantic_result.get("text", {}))
        bg_count = len(semantic_result.get("background", {}))
        
        state.log("")
        state.log("=" * 50)
        state.log("βœ… EXTRACTION COMPLETE!")
        state.log(f"   Enhanced extraction captured:")
        state.log(f"   β€’ {len(state.desktop_normalized.colors)} colors (DOM + CSS vars + SVG + inline)")
        state.log(f"   β€’ {len(state.desktop_normalized.typography)} typography styles")
        state.log(f"   β€’ {len(state.desktop_normalized.spacing)} spacing values")
        state.log(f"   β€’ {len(state.desktop_normalized.radius)} radius values")
        state.log(f"   β€’ {len(state.desktop_normalized.shadows)} shadow values")
        state.log(f"   Semantic Analysis:")
        state.log(f"   β€’ {brand_count} brand colors identified")
        state.log(f"   β€’ {text_count} text colors identified")
        state.log(f"   β€’ {bg_count} background colors identified")
        state.log("=" * 50)
        
        progress(1.0, desc="βœ… Complete!")
        
        status = f"""## βœ… Extraction Complete!

| Viewport | Colors | Typography | Spacing | Radius | Shadows |
|----------|--------|------------|---------|--------|---------|
| Desktop | {len(state.desktop_normalized.colors)} | {len(state.desktop_normalized.typography)} | {len(state.desktop_normalized.spacing)} | {len(state.desktop_normalized.radius)} | {len(state.desktop_normalized.shadows)} |
| Mobile | {len(state.mobile_normalized.colors)} | {len(state.mobile_normalized.typography)} | {len(state.mobile_normalized.spacing)} | {len(state.mobile_normalized.radius)} | {len(state.mobile_normalized.shadows)} |

**Primary Font:** {primary_font}

**Semantic Analysis:** {brand_count} brand, {text_count} text, {bg_count} background colors

**Enhanced Extraction:** DOM + CSS Variables + SVG + Inline + Stylesheets + Firecrawl

**Next:** Review the tokens below. Accept or reject, then proceed to Stage 2.
"""
        
        # Return all AS-IS previews including semantic
        return (
            status, 
            state.get_logs(), 
            desktop_data, 
            mobile_data, 
            typography_preview_html, 
            colors_asis_preview_html,
            semantic_preview_html,
            spacing_asis_preview_html,
            radius_asis_preview_html,
            shadows_asis_preview_html,
        )
        
    except Exception as e:
        import traceback
        state.log(f"❌ Error: {str(e)}")
        state.log(traceback.format_exc())
        error_detail = str(e).lower()
        if "timeout" in error_detail or "navigation" in error_detail:
            hint = "The page took too long to load. Try selecting fewer pages, or check if the site requires authentication."
        elif "no tokens" in error_detail or "empty" in error_detail:
            hint = "No design tokens could be extracted. The site may use unusual CSS patterns. Try a different page selection."
        else:
            hint = "Check the log above for details. Try selecting fewer pages or a different set of pages."
        return (error_message("Extraction Failed", str(e)[:200], hint),
                state.get_logs(), None, None, "", "", "", "", "", "")


def format_tokens_for_display(normalized) -> dict:
    """Format normalized tokens for Gradio display."""
    if normalized is None:
        return {"colors": [], "typography": [], "spacing": []}
    
    # Colors are now a dict
    colors = []
    color_items = list(normalized.colors.values()) if isinstance(normalized.colors, dict) else normalized.colors
    for c in sorted(color_items, key=lambda x: -x.frequency)[:50]:
        colors.append([
            True,  # Accept checkbox
            c.value,
            c.suggested_name or "",
            c.frequency,
            c.confidence.value if c.confidence else "medium",
            f"{c.contrast_white:.1f}:1" if c.contrast_white else "N/A",
            "βœ“" if c.wcag_aa_small_text else "βœ—",
            ", ".join(c.contexts[:2]) if c.contexts else "",
        ])
    
    # Typography
    typography = []
    typo_items = list(normalized.typography.values()) if isinstance(normalized.typography, dict) else normalized.typography
    for t in sorted(typo_items, key=lambda x: -x.frequency)[:30]:
        typography.append([
            True,  # Accept checkbox
            t.font_family,
            t.font_size,
            str(t.font_weight),
            t.line_height or "",
            t.suggested_name or "",
            t.frequency,
            t.confidence.value if t.confidence else "medium",
        ])
    
    # Spacing
    spacing = []
    spacing_items = list(normalized.spacing.values()) if isinstance(normalized.spacing, dict) else normalized.spacing
    for s in sorted(spacing_items, key=lambda x: x.value_px)[:20]:
        spacing.append([
            True,  # Accept checkbox
            s.value,
            f"{s.value_px}px",
            s.suggested_name or "",
            s.frequency,
            "βœ“" if s.fits_base_8 else "",
            s.confidence.value if s.confidence else "medium",
        ])
    
    # Radius
    radius = []
    radius_items = list(normalized.radius.values()) if isinstance(normalized.radius, dict) else normalized.radius
    for r in sorted(radius_items, key=lambda x: -x.frequency)[:20]:
        radius.append([
            True,  # Accept checkbox
            r.value,
            r.frequency,
            ", ".join(r.elements[:3]) if r.elements else "",
        ])

    return {
        "colors": colors,
        "typography": typography,
        "spacing": spacing,
        "radius": radius,
    }


def switch_viewport(viewport: str):
    """Switch between desktop and mobile view."""
    if viewport == "Desktop (1440px)":
        data = format_tokens_for_display(state.desktop_normalized)
    else:
        data = format_tokens_for_display(state.mobile_normalized)

    return data["colors"], data["typography"], data["spacing"], data["radius"]


# Legacy run_stage2_analysis() removed in v3 β€” use run_stage2_analysis_v2()


def normalized_to_dict(normalized) -> dict:
    """Convert NormalizedTokens to dict for workflow.

    v3: Includes full context (elements, contexts, role_hint, blur_px, etc.)
    so agents can reason about WHY each token is used.
    """
    if not normalized:
        return {}

    result = {
        "colors": {},
        "typography": {},
        "spacing": {},
        "radius": {},
        "shadows": {},
    }

    # Colors β€” include contexts, elements, role_hint for AURORA
    for name, c in normalized.colors.items():
        result["colors"][name] = {
            "value": c.value,
            "frequency": c.frequency,
            "suggested_name": c.suggested_name,
            "contrast_white": c.contrast_white,
            "contrast_black": c.contrast_black,
            "contexts": getattr(c, 'contexts', []),
            "elements": getattr(c, 'elements', []),
            "role_hint": getattr(c, 'role_hint', None),
        }

    # Typography β€” include elements for hierarchy analysis
    for name, t in normalized.typography.items():
        result["typography"][name] = {
            "font_family": t.font_family,
            "font_size": t.font_size,
            "font_weight": t.font_weight,
            "line_height": t.line_height,
            "frequency": t.frequency,
            "elements": getattr(t, 'elements', []),
        }

    # Spacing β€” include contexts and grid-alignment flags
    for name, s in normalized.spacing.items():
        result["spacing"][name] = {
            "value": s.value,
            "value_px": s.value_px,
            "frequency": s.frequency,
            "contexts": getattr(s, 'contexts', []),
            "fits_base_4": getattr(s, 'fits_base_4', False),
            "fits_base_8": getattr(s, 'fits_base_8', False),
        }

    # Radius β€” include grid-alignment flags and elements
    for name, r in normalized.radius.items():
        result["radius"][name] = {
            "value": r.value,
            "value_px": getattr(r, 'value_px', None),
            "frequency": r.frequency,
            "elements": getattr(r, 'elements', []),
            "fits_base_4": getattr(r, 'fits_base_4', False),
            "fits_base_8": getattr(r, 'fits_base_8', False),
        }

    # Shadows β€” include parsed components for elevation analysis
    for name, s in normalized.shadows.items():
        result["shadows"][name] = {
            "value": s.value,
            "frequency": s.frequency,
            "blur_px": getattr(s, 'blur_px', None),
            "y_offset_px": getattr(s, 'y_offset_px', None),
            "elements": getattr(s, 'elements', []),
        }
    
    return result


# =============================================================================
# STAGE 2: NEW ARCHITECTURE (Rule Engine + Benchmark Research + LLM Agents)
# =============================================================================

async def run_stage2_analysis_v2(
    selected_benchmarks: list[str] = None,
    progress=gr.Progress()
):
    """
    Run Stage 2 analysis with new architecture:
    - Layer 1: Rule Engine (FREE)
    - Layer 2: Benchmark Research (Firecrawl + Cache)
    - Layer 3: LLM Agents (Brand ID, Benchmark Advisor, Best Practices)
    - Layer 4: HEAD Synthesizer
    
    Includes comprehensive error handling for graceful degradation.
    """
    
    # Validate Stage 1 completion
    if not state.desktop_normalized or not state.mobile_normalized:
        return create_stage2_error_response(
            error_message("Stage 1 Not Complete",
                          "No extracted tokens found. Stage 1 extraction must be completed before running analysis.",
                          "Go back to **Step 1**, enter a URL, discover pages, and extract tokens first.")
        )
    
    # Default benchmarks if none selected
    if not selected_benchmarks or len(selected_benchmarks) == 0:
        selected_benchmarks = [
            "material_design_3",
            "shopify_polaris", 
            "atlassian_design",
        ]
    
    state.log("")
    state.log("═" * 60)
    state.log("πŸš€ STAGE 2: MULTI-AGENT ANALYSIS")
    state.log("═" * 60)
    state.log(f"   Started: {datetime.now().strftime('%H:%M:%S')}")
    state.log(f"   Benchmarks: {', '.join(selected_benchmarks)}")
    state.log("")
    
    # Import dataclasses early so fallbacks always work
    try:
        from agents.llm_agents import (
            BrandIdentification,
            BenchmarkAdvice,
            BestPracticesResult,
        )
    except ImportError:
        # Minimal v3-compatible fallback dataclasses
        from dataclasses import dataclass, field
        @dataclass
        class BrandIdentification:
            brand_primary: dict = field(default_factory=dict)
            brand_secondary: dict = field(default_factory=dict)
            brand_accent: dict = field(default_factory=dict)
            palette_strategy: str = ""
            cohesion_score: int = 5
            cohesion_notes: str = ""
            naming_map: dict = field(default_factory=dict)
            semantic_names: dict = field(default_factory=dict)
            self_evaluation: dict = field(default_factory=dict)
            reasoning_trace: list = field(default_factory=list)
            validation_passed: bool = False
            retry_count: int = 0
            typography_notes: str = ""
            spacing_notes: str = ""
            radius_notes: str = ""
            shadow_notes: str = ""
            def to_dict(self):
                return {k: getattr(self, k) for k in ['brand_primary', 'brand_secondary', 'brand_accent', 'palette_strategy', 'cohesion_score', 'naming_map', 'self_evaluation']}

        @dataclass
        class BenchmarkAdvice:
            recommended_benchmark: str = ""
            recommended_benchmark_name: str = ""
            reasoning: str = ""
            alignment_changes: list = field(default_factory=list)
            pros_of_alignment: list = field(default_factory=list)
            cons_of_alignment: list = field(default_factory=list)
            alternative_benchmarks: list = field(default_factory=list)
            self_evaluation: dict = field(default_factory=dict)
            reasoning_trace: list = field(default_factory=list)
            typography_comparison: dict = field(default_factory=dict)
            spacing_comparison: dict = field(default_factory=dict)
            color_comparison: dict = field(default_factory=dict)
            radius_comparison: dict = field(default_factory=dict)
            shadow_comparison: dict = field(default_factory=dict)
            def to_dict(self):
                return {k: getattr(self, k) for k in ['recommended_benchmark', 'recommended_benchmark_name', 'reasoning', 'alignment_changes']}

        @dataclass
        class BestPracticesResult:
            overall_score: int = 50
            checks: dict = field(default_factory=dict)
            priority_fixes: list = field(default_factory=list)
            passing_practices: list = field(default_factory=list)
            failing_practices: list = field(default_factory=list)
            self_evaluation: dict = field(default_factory=dict)
            reasoning_trace: list = field(default_factory=list)
            validation_passed: bool = False
            color_assessment: dict = field(default_factory=dict)
            typography_assessment: dict = field(default_factory=dict)
            spacing_assessment: dict = field(default_factory=dict)
            radius_assessment: dict = field(default_factory=dict)
            shadow_assessment: dict = field(default_factory=dict)
            def to_dict(self):
                return {k: getattr(self, k) for k in ['overall_score', 'checks', 'priority_fixes', 'passing_practices', 'failing_practices']}

    # Initialize results with defaults (for graceful degradation)
    rule_results = None
    benchmark_comparisons = []
    brand_result = None
    benchmark_advice = None
    best_practices = None
    final_synthesis = None
    
    progress(0.05, desc="βš™οΈ Running Rule Engine...")
    
    try:
        # =================================================================
        # LAYER 1: RULE ENGINE (FREE) - Critical, must succeed
        # =================================================================
        try:
            from core.rule_engine import run_rule_engine
            
            # Convert tokens to dict
            desktop_dict = normalized_to_dict(state.desktop_normalized)
            mobile_dict = normalized_to_dict(state.mobile_normalized)
            
            # Validate we have data
            if not desktop_dict.get("colors") and not desktop_dict.get("typography"):
                raise ValueError("No tokens extracted from Stage 1")
            
            # Run rule engine
            rule_results = run_rule_engine(
                typography_tokens=desktop_dict.get("typography", {}),
                color_tokens=desktop_dict.get("colors", {}),
                spacing_tokens=desktop_dict.get("spacing", {}),
                radius_tokens=desktop_dict.get("radius", {}),
                shadow_tokens=desktop_dict.get("shadows", {}),
                log_callback=state.log,
                fg_bg_pairs=getattr(state, 'fg_bg_pairs', None),
            )
            
            state.rule_engine_results = rule_results
            state.log("")
            state.log("   βœ… Rule Engine: SUCCESS")
            
        except Exception as e:
            state.log(f"   ❌ Rule Engine FAILED: {str(e)[:100]}")
            state.log("   └─ Cannot proceed without rule engine results")
            import traceback
            state.log(traceback.format_exc()[:500])
            return create_stage2_error_response(
                error_message("Rule Engine Failed",
                              f"The rule engine could not analyze your tokens: {str(e)[:150]}",
                              "This usually means the extracted tokens are incomplete. Try re-running Stage 1 extraction with different pages selected.")
            )
        
        progress(0.20, desc="πŸ”¬ Researching benchmarks...")
        
        # =================================================================
        # LAYER 2: BENCHMARK RESEARCH - Can use fallback
        # =================================================================
        try:
            from agents.benchmark_researcher import BenchmarkResearcher, FALLBACK_BENCHMARKS, BenchmarkData
            
            # Try to get Firecrawl client (optional)
            firecrawl_client = None
            try:
                from agents.firecrawl_extractor import get_firecrawl_client
                firecrawl_client = get_firecrawl_client()
                state.log("   β”œβ”€ Firecrawl client: Available")
            except Exception as fc_err:
                state.log(f"   β”œβ”€ Firecrawl client: Not available ({str(fc_err)[:30]})")
                state.log("   β”‚  └─ Will use cached/fallback data")
            
            # Get HF client for LLM extraction (optional)
            hf_client = None
            try:
                from core.hf_inference import get_inference_client
                hf_client = get_inference_client()
                state.log("   β”œβ”€ HF client: Available")
            except Exception as hf_err:
                state.log(f"   β”œβ”€ HF client: Not available ({str(hf_err)[:30]})")
            
            researcher = BenchmarkResearcher(
                firecrawl_client=firecrawl_client,
                hf_client=hf_client,
            )
            
            # Research selected benchmarks (with fallback)
            try:
                benchmarks = await researcher.research_selected_benchmarks(
                    selected_keys=selected_benchmarks,
                    log_callback=state.log,
                )
            except Exception as research_err:
                state.log(f"   ⚠️ Research failed, using fallback: {str(research_err)[:50]}")
                # Use fallback data
                benchmarks = []
                for key in selected_benchmarks:
                    if key in FALLBACK_BENCHMARKS:
                        data = FALLBACK_BENCHMARKS[key]
                        benchmarks.append(BenchmarkData(
                            key=key,
                            name=key.replace("_", " ").title(),
                            short_name=key.split("_")[0].title(),
                            vendor="",
                            icon="πŸ“¦",
                            typography=data.get("typography", {}),
                            spacing=data.get("spacing", {}),
                            colors=data.get("colors", {}),
                            radius=data.get("radius", {}),
                            shadows=data.get("shadows", {}),
                            fetched_at=datetime.now().isoformat(),
                            confidence="fallback",
                            best_for=[],
                        ))
            
            # Compare to benchmarks
            if benchmarks and rule_results:
                # Count user's radius tiers and shadow levels for comparison
                _user_radius_tiers = len(desktop_dict.get("radius", {}))
                _user_shadow_levels = len(desktop_dict.get("shadows", {}))
                _user_color_count = len(desktop_dict.get("colors", {}))

                benchmark_comparisons = researcher.compare_to_benchmarks(
                    your_ratio=rule_results.typography.detected_ratio,
                    your_base_size=int(rule_results.typography.base_size) if rule_results.typography.sizes_px else 16,
                    your_spacing_grid=rule_results.spacing.detected_base,
                    benchmarks=benchmarks,
                    log_callback=state.log,
                    your_color_count=_user_color_count,
                    your_radius_tiers=_user_radius_tiers,
                    your_shadow_levels=_user_shadow_levels,
                )
                state.benchmark_comparisons = benchmark_comparisons
                state.log("")
                state.log(f"   βœ… Benchmark Research: SUCCESS ({len(benchmarks)} systems)")
            else:
                state.log("   ⚠️ No benchmarks available for comparison")
            
        except Exception as e:
            state.log(f"   ⚠️ Benchmark Research FAILED: {str(e)[:100]}")
            state.log("   └─ Continuing without benchmark comparison...")
            benchmark_comparisons = []
        
        progress(0.40, desc="πŸ€– Running LLM Agents in parallel...")

        # =================================================================
        # LAYER 3: LLM AGENTS β€” v3: ALL token types, ReAct reasoning
        # =================================================================
        try:
            from agents.llm_agents import (
                BrandIdentifierAgent,
                BenchmarkAdvisorAgent,
                BestPracticesValidatorAgent,
                BrandIdentification,
                BenchmarkAdvice,
                BestPracticesResult,
            )

            state.log("")
            state.log("=" * 60)
            state.log("LAYER 3: LLM AGENTS (ReAct + Parallel)")
            state.log("=" * 60)
            state.log("   Each agent researches ALL token types:")
            state.log("   Colors + Typography + Spacing + Radius + Shadows")
            state.log("")

            # Check if HF client is available
            if not hf_client:
                try:
                    from core.hf_inference import get_inference_client
                    hf_client = get_inference_client()
                except Exception:
                    state.log("   HF client not available - skipping LLM agents")
                    hf_client = None

            if hf_client:
                # Initialize agents
                brand_agent = BrandIdentifierAgent(hf_client)
                benchmark_agent = BenchmarkAdvisorAgent(hf_client)
                best_practices_agent = BestPracticesValidatorAgent(hf_client)

                # Full token dict with all context
                desktop_dict = normalized_to_dict(state.desktop_normalized)

                # Prepare shared data for agents
                typo = rule_results.typography
                spacing = rule_results.spacing
                sizes_str = ", ".join([f"{s}px" for s in typo.sizes_px[:10]]) if typo.sizes_px else "N/A"
                sp_vals = ", ".join([f"{v}px" for v in spacing.current_values[:10]]) if spacing.current_values else "N/A"
                color_count = rule_results.color_stats.unique_count
                brand_info_str = ""

                # Radius/shadow formatted strings for ATLAS
                from agents.llm_agents import _fmt_radius, _fmt_shadows
                radius_str = _fmt_radius(desktop_dict.get("radius", {}))
                shadow_str = _fmt_shadows(desktop_dict.get("shadows", {}))

                # ─── AURORA: ALL token types ───
                async def _run_aurora():
                    try:
                        return await brand_agent.analyze(
                            color_tokens=desktop_dict.get("colors", {}),
                            typography_tokens=desktop_dict.get("typography", {}),
                            spacing_tokens=desktop_dict.get("spacing", {}),
                            radius_tokens=desktop_dict.get("radius", {}),
                            shadow_tokens=desktop_dict.get("shadows", {}),
                            log_callback=state.log,
                        )
                    except Exception as e:
                        state.log(f"   AURORA failed: {str(e)[:120]}")
                        return BrandIdentification()

                # ─── ATLAS: ALL token types ───
                async def _run_atlas():
                    if not benchmark_comparisons:
                        state.log("   ATLAS skipped (no benchmarks)")
                        return BenchmarkAdvice()
                    try:
                        return await benchmark_agent.analyze(
                            user_ratio=typo.detected_ratio,
                            user_base=int(typo.base_size) if typo.sizes_px else 16,
                            user_spacing=spacing.detected_base,
                            benchmark_comparisons=benchmark_comparisons,
                            color_count=color_count,
                            brand_info=brand_info_str,
                            user_sizes=sizes_str,
                            spacing_values=sp_vals,
                            radius_data=radius_str,
                            shadow_data=shadow_str,
                            log_callback=state.log,
                        )
                    except Exception as e:
                        state.log(f"   ATLAS failed: {str(e)[:120]}")
                        return BenchmarkAdvice()

                # ─── SENTINEL: ALL token types ───
                async def _run_sentinel():
                    try:
                        return await best_practices_agent.analyze(
                            rule_engine_results=rule_results,
                            radius_tokens=desktop_dict.get("radius", {}),
                            shadow_tokens=desktop_dict.get("shadows", {}),
                            log_callback=state.log,
                        )
                    except Exception as e:
                        state.log(f"   SENTINEL failed: {str(e)[:120]}")
                        return BestPracticesResult(overall_score=rule_results.consistency_score)

                # Execute AURORA + ATLAS + SENTINEL in parallel
                import asyncio
                state.log("   Running 3 agents in parallel: AURORA | ATLAS | SENTINEL")
                state.log("")
                brand_result, benchmark_advice, best_practices = await asyncio.gather(
                    _run_aurora(),
                    _run_atlas(),
                    _run_sentinel(),
                )
            else:
                # No HF client - use defaults
                state.log("   Using default values (no LLM)")
                brand_result = BrandIdentification()
                benchmark_advice = BenchmarkAdvice()
                best_practices = BestPracticesResult(overall_score=rule_results.consistency_score)

        except Exception as e:
            state.log(f"   LLM Agents FAILED: {str(e)[:100]}")
            brand_result = BrandIdentification() if not brand_result else brand_result
            benchmark_advice = BenchmarkAdvice() if not benchmark_advice else benchmark_advice
            best_practices = BestPracticesResult(overall_score=rule_results.consistency_score if rule_results else 50)

        progress(0.70, desc="Synthesizing results...")

        # =================================================================
        # LAYER 4: NEXUS β€” Tree of Thought synthesis
        # =================================================================
        try:
            from agents.llm_agents import HeadSynthesizerAgent, HeadSynthesis, post_validate_stage2

            if hf_client and brand_result and benchmark_advice and best_practices:
                head_agent = HeadSynthesizerAgent(hf_client)

                try:
                    final_synthesis = await head_agent.synthesize(
                        rule_engine_results=rule_results,
                        benchmark_comparisons=benchmark_comparisons,
                        brand_identification=brand_result,
                        benchmark_advice=benchmark_advice,
                        best_practices=best_practices,
                        log_callback=state.log,
                    )
                except Exception as e:
                    state.log(f"   NEXUS failed: {str(e)[:120]}")
                    import traceback
                    state.log(f"   {traceback.format_exc()[:200]}")
                    final_synthesis = None

                # ─── POST-VALIDATION (deterministic) ───
                if final_synthesis:
                    try:
                        pv_issues = post_validate_stage2(
                            aurora=brand_result,
                            sentinel=best_practices,
                            nexus=final_synthesis,
                            rule_engine=rule_results,
                        )
                        if pv_issues:
                            state.log("")
                            state.log(f"   POST-VALIDATION: {len(pv_issues)} issues found")
                            for issue in pv_issues[:10]:
                                state.log(f"      β”œβ”€ {issue}")
                            if len(pv_issues) > 10:
                                state.log(f"      └─ ... and {len(pv_issues) - 10} more")
                        else:
                            state.log("   POST-VALIDATION: All checks passed βœ…")
                    except Exception as pv_err:
                        state.log(f"   POST-VALIDATION error: {str(pv_err)}")

            # Create fallback synthesis if needed
            if not final_synthesis:
                state.log("   Creating fallback synthesis...")
                final_synthesis = create_fallback_synthesis(
                    rule_results, benchmark_comparisons, brand_result, best_practices
                )

            state.final_synthesis = final_synthesis
            state.brand_result = brand_result  # Preserve AURORA naming_map for export

            # ─── AGENT EVALUATION SUMMARY ───
            state.log("")
            state.log("=" * 60)
            state.log("AGENT EVALUATION SUMMARY")
            state.log("=" * 60)

            def _eval_line(name, emoji, result_obj):
                se = getattr(result_obj, 'self_evaluation', None) or {}
                if isinstance(se, dict) and se:
                    conf = se.get('confidence', '?')
                    dq = se.get('data_quality', '?')
                    flags = se.get('flags', [])
                    flag_str = f", flags={flags}" if flags else ""
                    return f"   {emoji} {name}: confidence={conf}/10, data={dq}{flag_str}"
                return f"   {emoji} {name}: no self-evaluation returned"

            if brand_result:
                named = len(brand_result.naming_map) if brand_result.naming_map else 0
                valid = "PASSED" if brand_result.validation_passed else "FALLBACK"
                state.log(_eval_line("AURORA  (Brand ID)", "", brand_result) + f", named={named}, critic={valid}")
            if benchmark_advice:
                state.log(_eval_line("ATLAS   (Benchmark)", "", benchmark_advice))
            if best_practices:
                bp_score = getattr(best_practices, 'overall_score', '?')
                valid = "PASSED" if best_practices.validation_passed else "FIXED"
                state.log(_eval_line("SENTINEL (Practices)", "", best_practices) + f", score={bp_score}/100, critic={valid}")
            if final_synthesis:
                synth_overall = final_synthesis.scores.get('overall', '?') if final_synthesis.scores else '?'
                chosen = final_synthesis.chosen_perspective or "?"
                state.log(_eval_line("NEXUS   (Synthesis)", "", final_synthesis) + f", overall={synth_overall}/100, perspective={chosen}")

            state.log("=" * 60)
            state.log("")

        except Exception as e:
            state.log(f"   Synthesis FAILED: {str(e)[:100]}")
            final_synthesis = create_fallback_synthesis(
                rule_results, benchmark_comparisons, brand_result, best_practices
            )
            state.final_synthesis = final_synthesis

        progress(0.85, desc="πŸ“Š Formatting results...")
        
        # =================================================================
        # FORMAT OUTPUTS FOR UI
        # =================================================================
        
        try:
            # Build status markdown
            status_md = format_stage2_status_v2(
                rule_results=rule_results,
                final_synthesis=final_synthesis,
                best_practices=best_practices,
            )
            
            # Build benchmark comparison HTML
            benchmark_md = format_benchmark_comparison_v2(
                benchmark_comparisons=benchmark_comparisons,
                benchmark_advice=benchmark_advice,
            )
            
            # Build scores dashboard HTML
            scores_html = format_scores_dashboard_v2(
                rule_results=rule_results,
                final_synthesis=final_synthesis,
                best_practices=best_practices,
            )
            
            # Build priority actions HTML
            actions_html = format_priority_actions_v2(
                rule_results=rule_results,
                final_synthesis=final_synthesis,
                best_practices=best_practices,
            )
            
            # Build color recommendations table
            color_recs_table = format_color_recommendations_table_v2(
                rule_results=rule_results,
                brand_result=brand_result,
                final_synthesis=final_synthesis,
            )
            
            # Get fonts and typography data
            fonts = get_detected_fonts()
            base_size = get_base_font_size()

            typography_desktop_data = format_typography_comparison_viewport(
                state.desktop_normalized, base_size, "desktop"
            )
            typography_mobile_data = format_typography_comparison_viewport(
                state.mobile_normalized, base_size, "mobile"
            )

            # Generate spacing comparison table from rule_results
            spacing_data = []
            if rule_results and rule_results.spacing:
                sp = rule_results.spacing
                current_vals = sp.current_values or []
                suggested_8 = [i * 8 for i in range(1, 11)]
                suggested_4 = [i * 4 for i in range(1, 11)]
                for i in range(min(10, max(len(current_vals), 10))):
                    cur = f"{current_vals[i]}px" if i < len(current_vals) else "β€”"
                    g8 = f"{suggested_8[i]}px" if i < len(suggested_8) else "β€”"
                    g4 = f"{suggested_4[i]}px" if i < len(suggested_4) else "β€”"
                    spacing_data.append([cur, g8, g4])

            # Generate base colors, color ramps, radius, shadows markdown
            base_colors_md = format_base_colors()
            color_ramps_md = ""  # Visual ramps are in color_ramps_preview_html
            try:
                from core.preview_generator import generate_color_ramp
                colors = list(state.desktop_normalized.colors.values())
                colors.sort(key=lambda c: -c.frequency)
                ramp_lines = ["### 🌈 Color Ramps (Top Colors)", ""]
                for c in colors[:6]:
                    ramp = generate_color_ramp(c.value)
                    if ramp:
                        shades_str = " β†’ ".join(f"`{s['hex']}`" for s in ramp[::2])  # every other shade
                        ramp_lines.append(f"**{c.value}** ({c.frequency}x): {shades_str}")
                        ramp_lines.append("")
                color_ramps_md = "\n".join(ramp_lines)
            except Exception:
                color_ramps_md = "*Color ramps shown in visual preview above*"

            radius_md = format_radius_with_tokens()
            shadows_md = format_shadows_with_tokens()
            
            # Generate visual previews
            typography_preview_html = ""
            color_ramps_preview_html = ""
            llm_recs_html = ""

            try:
                from core.preview_generator import (
                    generate_typography_preview_html,
                    generate_semantic_color_ramps_html,
                    generate_color_ramps_preview_html,
                )

                primary_font = fonts.get("primary", "Open Sans")
                desktop_typo_dict = {
                    name: {
                        "font_size": t.font_size,
                        "font_weight": t.font_weight,
                        "line_height": t.line_height,
                    }
                    for name, t in state.desktop_normalized.typography.items()
                }
                typography_preview_html = generate_typography_preview_html(desktop_typo_dict, primary_font)

                # Generate color ramps preview (semantic groups)
                semantic_analysis = getattr(state, 'semantic_analysis', {})
                desktop_dict_for_colors = normalized_to_dict(state.desktop_normalized)

                if semantic_analysis:
                    color_ramps_preview_html = generate_semantic_color_ramps_html(
                        semantic_analysis=semantic_analysis,
                        color_tokens=desktop_dict_for_colors.get("colors", {}),
                    )
                else:
                    color_ramps_preview_html = generate_color_ramps_preview_html(
                        color_tokens=desktop_dict_for_colors.get("colors", {}),
                    )

                state.log("   βœ… Color ramps preview generated")

            except Exception as preview_err:
                state.log(f"   ⚠️ Preview generation failed: {str(preview_err)[:80]}")
                typography_preview_html = typography_preview_html or "<div class='placeholder-msg'>Preview unavailable</div>"
                color_ramps_preview_html = "<div class='placeholder-msg'>Color ramps preview unavailable</div>"

            # Generate LLM recommendations HTML
            try:
                # Build recs dict in the format expected by the HTML formatter
                synth_recs = {}
                if final_synthesis:
                    # Convert list of color recs to dict keyed by role
                    # HeadSynthesis uses: {role, current, suggested, reason, accept}
                    # Formatter expects: {current, suggested, action, rationale}
                    color_recs_dict = {}
                    for rec in (final_synthesis.color_recommendations or []):
                        if isinstance(rec, dict) and rec.get("role"):
                            current_val = rec.get("current", "?")
                            suggested_val = rec.get("suggested", current_val)
                            accept = rec.get("accept", True)
                            reason = rec.get("reason", "")
                            # Determine action: if suggested differs from current, it's a change
                            if suggested_val and suggested_val != current_val and not accept:
                                action = "change"
                            elif suggested_val and suggested_val != current_val:
                                action = "change"
                            else:
                                action = "keep"
                            color_recs_dict[rec["role"]] = {
                                "current": current_val,
                                "suggested": suggested_val,
                                "action": action,
                                "rationale": reason,
                            }
                    synth_recs["color_recommendations"] = color_recs_dict

                    # Add AA fixes from rule engine
                    # Formatter expects: {color, role, issue, fix, current_contrast, fixed_contrast}
                    aa_fixes = []
                    if rule_results and rule_results.accessibility:
                        for a in rule_results.accessibility:
                            if not a.passes_aa_normal:
                                best_contrast = a.contrast_on_white if a.best_text_color == "#FFFFFF" else a.contrast_on_black
                                aa_fixes.append({
                                    "color": a.hex_color,
                                    "role": a.name or "unknown",
                                    "issue": f"Fails AA normal ({best_contrast:.1f}:1 < 4.5:1)",
                                    "fix": a.suggested_fix or a.hex_color,
                                    "current_contrast": f"{best_contrast:.1f}",
                                    "fixed_contrast": f"{a.suggested_fix_contrast:.1f}" if a.suggested_fix_contrast else "β€”",
                                })
                    synth_recs["accessibility_fixes"] = aa_fixes

                llm_recs_html = format_llm_color_recommendations_html(
                    final_recs=synth_recs,
                    semantic_analysis=getattr(state, 'semantic_analysis', {}),
                )
            except Exception as recs_err:
                state.log(f"   ⚠️ LLM recs HTML failed: {str(recs_err)[:120]}")
                import traceback
                state.log(f"   └─ {traceback.format_exc()[:200]}")
                llm_recs_html = "<div class='placeholder-msg'>LLM recommendations unavailable</div>"

            # Store upgrade_recommendations for Apply Upgrades button
            aa_failures_list = []
            if rule_results and rule_results.accessibility:
                aa_failures_list = [
                    a.to_dict() for a in rule_results.accessibility
                    if not a.passes_aa_normal
                ]
            state.upgrade_recommendations = {
                "color_recommendations": (final_synthesis.color_recommendations if final_synthesis else []),
                "accessibility_fixes": aa_failures_list,
                "scores": (final_synthesis.scores if final_synthesis else {}),
                "top_3_actions": (final_synthesis.top_3_actions if final_synthesis else []),
            }

        except Exception as format_err:
            state.log(f"   ⚠️ Formatting failed: {str(format_err)[:100]}")
            import traceback
            state.log(traceback.format_exc()[:500])
            # Return minimal results (must match 18 outputs)
            return (
                f"⚠️ Analysis completed with formatting errors: {str(format_err)[:50]}",
                state.get_logs(),
                "",  # benchmark_html
                "<div class='placeholder-msg'>Scores unavailable</div>",
                "<div class='placeholder-msg'>Actions unavailable</div>",
                [],
                None,
                None,
                "<div class='placeholder-msg'>Typography preview unavailable</div>",
                "<div class='placeholder-msg'>Color ramps preview unavailable</div>",
                "<div class='placeholder-msg'>LLM recommendations unavailable</div>",
                [],  # spacing_data
                "*Formatting error - base colors unavailable*",  # base_colors_md
                "*Formatting error - color ramps unavailable*",  # color_ramps_md
                "*Formatting error - radius tokens unavailable*",  # radius_md
                "*Formatting error - shadow tokens unavailable*",  # shadows_md
                "⚠️ Color preview unavailable due to formatting errors.",  # auto_color_preview
                "",  # asis_tobe_html
            )
        
        # Auto-generate color classification preview
        auto_color_preview = ""
        try:
            auto_color_preview = preview_color_classification("semantic")
            state.log("   βœ… Color classification preview auto-generated (semantic convention)")
        except Exception as cp_err:
            state.log(f"   ⚠️ Auto color preview failed: {str(cp_err)}")
            auto_color_preview = "⚠️ Color preview unavailable β€” click 'Preview Color Names' button to generate."

        # Build As-Is β†’ To-Be transformation summary
        asis_tobe_html = ""
        try:
            cards = []
            # Type Scale
            detected_ratio = f"{rule_results.typography.detected_ratio:.2f}" if rule_results else "?"
            rec_ratio = "1.25"
            rec_name = "Major Third"
            if final_synthesis and final_synthesis.type_scale_recommendation:
                rec_ratio = str(final_synthesis.type_scale_recommendation.get("recommended_ratio", "1.25"))
                rec_name = final_synthesis.type_scale_recommendation.get("name", "Major Third")
            cards.append(_render_as_is_to_be(
                "Type Scale", detected_ratio,
                f"{rule_results.typography.scale_name if rule_results else '?'} β€’ Variance: {rule_results.typography.variance:.2f}" if rule_results else "",
                rec_ratio, rec_name, icon="πŸ“"
            ))
            # Spacing
            detected_base = f"{rule_results.spacing.detected_base}px" if rule_results else "?"
            alignment = f"{rule_results.spacing.alignment_percentage:.0f}% aligned" if rule_results else ""
            cards.append(_render_as_is_to_be(
                "Spacing Grid", detected_base, alignment,
                "8px", "Industry standard (Material, Tailwind)", icon="πŸ“"
            ))
            # Colors
            color_count = str(rule_results.color_stats.unique_count) if rule_results else "?"
            aa_fails = rule_results.aa_failures if rule_results else 0
            cards.append(_render_as_is_to_be(
                "Colors", f"{color_count} unique",
                f"{aa_fails} fail AA compliance" if aa_fails else "All pass AA",
                f"~15 semantic" if int(color_count) > 20 else color_count,
                "0 AA failures" if aa_fails else "All pass βœ“", icon="🎨"
            ))
            # Shadows
            shadow_count = 0
            if state.desktop_normalized:
                shadow_count = len(getattr(state.desktop_normalized, 'shadows', {}))
            tobe_shadow_count = max(shadow_count, 5) if shadow_count > 0 else 0  # Always 5 levels (interpolated)
            cards.append(_render_as_is_to_be(
                "Shadows", f"{shadow_count} extracted",
                "Elevation tokens" if shadow_count > 0 else "No shadows found",
                f"{tobe_shadow_count} levels",
                "xs β†’ sm β†’ md β†’ lg β†’ xl" + (f" (interpolated from {shadow_count})" if shadow_count < 5 else ""),
                icon="🌫️"
            ))
            asis_tobe_html = "".join(cards)
        except Exception:
            asis_tobe_html = ""

        progress(0.95, desc="βœ… Complete!")

        # Final log summary
        state.log("")
        state.log("═" * 60)
        state.log("πŸ“Š FINAL RESULTS")
        state.log("═" * 60)
        state.log("")
        overall_score = final_synthesis.scores.get('overall', rule_results.consistency_score) if final_synthesis else rule_results.consistency_score
        state.log(f"   🎯 OVERALL SCORE: {overall_score}/100")
        if final_synthesis and final_synthesis.scores:
            state.log(f"   β”œβ”€ Accessibility:  {final_synthesis.scores.get('accessibility', '?')}/100")
            state.log(f"   β”œβ”€ Consistency:    {final_synthesis.scores.get('consistency', '?')}/100")
            state.log(f"   └─ Organization:   {final_synthesis.scores.get('organization', '?')}/100")
        state.log("")
        if benchmark_comparisons:
            state.log(f"   πŸ† Closest Benchmark: {benchmark_comparisons[0].benchmark.name if benchmark_comparisons else 'N/A'}")
        state.log("")
        state.log("   🎯 TOP 3 ACTIONS:")
        if final_synthesis and final_synthesis.top_3_actions:
            for i, action in enumerate(final_synthesis.top_3_actions[:3]):
                impact = action.get('impact', 'medium')
                icon = "πŸ”΄" if impact == "high" else "🟑" if impact == "medium" else "🟒"
                state.log(f"   β”‚  {i+1}. {icon} {action.get('action', 'N/A')}")
        else:
            state.log(f"   β”‚  1. πŸ”΄ Fix {rule_results.aa_failures} AA compliance failures")
        state.log("")
        state.log("═" * 60)
        state.log(f"   πŸ’° TOTAL COST: ~$0.003")
        state.log(f"   ⏱️  COMPLETED: {datetime.now().strftime('%H:%M:%S')}")
        state.log("═" * 60)

        return (
            status_md,
            state.get_logs(),
            benchmark_md,
            scores_html,
            actions_html,
            color_recs_table,
            typography_desktop_data,
            typography_mobile_data,
            typography_preview_html,
            color_ramps_preview_html,
            llm_recs_html,
            spacing_data,
            base_colors_md,
            color_ramps_md,
            radius_md,
            shadows_md,
            auto_color_preview,
            asis_tobe_html,
        )

    except Exception as e:
        import traceback
        state.log(f"❌ Critical Error: {str(e)}")
        state.log(traceback.format_exc())
        error_detail = str(e).lower()
        if "token" in error_detail or "auth" in error_detail or "401" in error_detail:
            hint = "Your HuggingFace token may be invalid or expired. Go to **Configuration** above and re-enter your token."
        elif "rate" in error_detail or "429" in error_detail:
            hint = "Rate limit reached. Wait a few minutes and try again."
        else:
            hint = "Check the analysis log above for details. Try running the analysis again."
        return create_stage2_error_response(
            error_message("Analysis Failed", str(e)[:200], hint)
        )


def create_fallback_synthesis(rule_results, benchmark_comparisons, brand_result, best_practices):
    """Create a fallback synthesis when LLM synthesis fails.

    v3: includes radius_recommendation, shadow_recommendation, perspective fields.
    """
    try:
        from agents.llm_agents import HeadSynthesis
    except ImportError:
        from dataclasses import dataclass, field
        @dataclass
        class HeadSynthesis:
            executive_summary: str = ""
            scores: dict = field(default_factory=dict)
            benchmark_fit: dict = field(default_factory=dict)
            brand_analysis: dict = field(default_factory=dict)
            top_3_actions: list = field(default_factory=list)
            color_recommendations: list = field(default_factory=list)
            type_scale_recommendation: dict = field(default_factory=dict)
            spacing_recommendation: dict = field(default_factory=dict)
            radius_recommendation: dict = field(default_factory=dict)
            shadow_recommendation: dict = field(default_factory=dict)
            self_evaluation: dict = field(default_factory=dict)
            perspective_a: dict = field(default_factory=dict)
            perspective_b: dict = field(default_factory=dict)
            chosen_perspective: str = ""
            choice_reasoning: str = ""
            reasoning_trace: list = field(default_factory=list)
            def to_dict(self):
                return {k: getattr(self, k) for k in [
                    'executive_summary', 'scores', 'benchmark_fit', 'brand_analysis',
                    'top_3_actions', 'color_recommendations', 'type_scale_recommendation',
                    'spacing_recommendation', 'radius_recommendation', 'shadow_recommendation',
                    'self_evaluation', 'chosen_perspective', 'choice_reasoning',
                ]}

    # Calculate scores from rule engine
    overall = rule_results.consistency_score if rule_results else 50
    accessibility = max(0, 100 - (rule_results.aa_failures * 10)) if rule_results else 50

    # Build actions from rule engine
    actions = []
    if rule_results and rule_results.aa_failures > 0:
        actions.append({
            "action": f"Fix {rule_results.aa_failures} colors failing AA compliance",
            "impact": "high",
            "token_type": "color",
        })
    if rule_results and not rule_results.typography.is_consistent:
        actions.append({
            "action": f"Align type scale to {rule_results.typography.recommendation} ({rule_results.typography.recommendation_name})",
            "impact": "medium",
            "token_type": "typography",
        })
    if rule_results and rule_results.color_stats.unique_count > 30:
        actions.append({
            "action": f"Consolidate {rule_results.color_stats.unique_count} colors to ~15 semantic colors",
            "impact": "medium",
            "token_type": "color",
        })

    return HeadSynthesis(
        executive_summary=f"Your design system scores {overall}/100. Analysis completed with fallback synthesis.",
        scores={
            "overall": overall,
            "accessibility": accessibility,
            "consistency": overall,
            "organization": 50,
        },
        benchmark_fit={
            "closest": benchmark_comparisons[0].benchmark.name if benchmark_comparisons else "Unknown",
            "similarity": f"{benchmark_comparisons[0].overall_match_pct:.0f}%" if benchmark_comparisons else "N/A",
        },
        brand_analysis={
            "primary": brand_result.brand_primary.get("color", "Unknown") if brand_result else "Unknown",
            "cohesion": brand_result.cohesion_score if brand_result else 5,
        },
        top_3_actions=actions[:3],
        color_recommendations=[],
        type_scale_recommendation={
            "current_ratio": rule_results.typography.detected_ratio if rule_results else 1.0,
            "recommended_ratio": rule_results.typography.recommendation if rule_results else 1.25,
        },
        spacing_recommendation={
            "current": f"{rule_results.spacing.detected_base}px" if rule_results else "Unknown",
            "recommended": f"{rule_results.spacing.recommendation}px" if rule_results else "8px",
        },
        radius_recommendation={},
        shadow_recommendation={},
    )


def create_stage2_error_response(error_msg: str):
    """Create error response tuple for Stage 2 (must match 18 outputs)."""
    return (
        error_msg,
        state.get_logs(),
        "",  # benchmark_html
        f"<div class='placeholder-msg'>{error_msg}</div>",  # scores_html
        "",  # actions_html
        [],  # color_recs_table
        None,  # typography_desktop
        None,  # typography_mobile
        "",  # typography_preview
        "",  # color_ramps_preview
        "",  # llm_recs_html
        [],  # spacing_data
        "*Run analysis to see base colors*",  # base_colors_md
        "*Run analysis to see color ramps*",  # color_ramps_md
        "*Run analysis to see radius tokens*",  # radius_md
        "*Run analysis to see shadow tokens*",  # shadows_md
        "",  # auto_color_preview
        "",  # asis_tobe_html
    )


def format_stage2_status_v2(rule_results, final_synthesis, best_practices) -> str:
    """Format Stage 2 status with new architecture results."""
    
    lines = []
    lines.append("## βœ… Analysis Complete!")
    lines.append("")
    
    # Overall Score
    overall = final_synthesis.scores.get('overall', rule_results.consistency_score)
    lines.append(f"### 🎯 Overall Score: {overall}/100")
    lines.append("")
    
    # Executive Summary
    if final_synthesis.executive_summary:
        lines.append(f"*{final_synthesis.executive_summary}*")
        lines.append("")
    
    # Quick Stats
    lines.append("### πŸ“Š Quick Stats")
    lines.append(f"- **AA Failures:** {rule_results.aa_failures}")
    lines.append(f"- **Type Scale:** {rule_results.typography.detected_ratio:.3f} ({rule_results.typography.scale_name})")
    lines.append(f"- **Spacing Grid:** {rule_results.spacing.detected_base}px ({rule_results.spacing.alignment_percentage:.0f}% aligned)")
    lines.append(f"- **Unique Colors:** {rule_results.color_stats.unique_count}")
    lines.append("")
    
    # Cost
    lines.append("### πŸ’° Cost")
    lines.append("**Total:** ~$0.003 (Rule Engine: $0 + LLM: ~$0.003)")
    lines.append("")

    # Next step guidance
    lines.append("---")
    lines.append("**Next:** Review the analysis results below. Accept or reject color recommendations, "
                 "choose your type scale and spacing grid, then click **'Apply Selected Upgrades'** at the bottom.")

    return "\n".join(lines)


def format_benchmark_comparison_v2(benchmark_comparisons, benchmark_advice) -> str:
    """Format benchmark comparison as visual HTML cards with progress bars."""
    return _render_benchmark_cards(benchmark_comparisons, benchmark_advice)


def format_scores_dashboard_v2(rule_results, final_synthesis, best_practices) -> str:
    """Format scores dashboard HTML."""
    
    overall = final_synthesis.scores.get('overall', rule_results.consistency_score)
    accessibility = final_synthesis.scores.get('accessibility', 100 - (rule_results.aa_failures * 5))
    consistency = final_synthesis.scores.get('consistency', rule_results.consistency_score)
    organization = final_synthesis.scores.get('organization', 50)
    
    def score_color(score):
        if score >= 80:
            return "#10b981"  # Green
        elif score >= 60:
            return "#f59e0b"  # Yellow
        else:
            return "#ef4444"  # Red
    
    html = f"""
    <style>
        .scores-grid {{ display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin: 20px 0; }}
        .score-card {{ border-radius: 12px; padding: 20px; text-align: center; }}
        .score-card-secondary {{ background: #f8fafc; border: 1px solid #e2e8f0; }}
        .score-card .score-label {{ font-size: 12px; color: #64748b; margin-top: 4px; }}
        .dark .score-card-secondary {{ background: #1e293b !important; border-color: #475569 !important; }}
        .dark .score-card .score-label {{ color: #94a3b8 !important; }}
    </style>
    <div class="scores-grid">
        <div class="score-card" style="background: linear-gradient(135deg, {score_color(overall)}22 0%, {score_color(overall)}11 100%);
                    border: 2px solid {score_color(overall)};">
            <div style="font-size: 32px; font-weight: 700; color: {score_color(overall)};">{overall}</div>
            <div class="score-label">OVERALL</div>
        </div>
        <div class="score-card score-card-secondary">
            <div style="font-size: 24px; font-weight: 600; color: {score_color(accessibility)};">{accessibility}</div>
            <div class="score-label">Accessibility</div>
        </div>
        <div class="score-card score-card-secondary">
            <div style="font-size: 24px; font-weight: 600; color: {score_color(consistency)};">{consistency}</div>
            <div class="score-label">Consistency</div>
        </div>
        <div class="score-card score-card-secondary">
            <div style="font-size: 24px; font-weight: 600; color: {score_color(organization)};">{organization}</div>
            <div class="score-label">Organization</div>
        </div>
    </div>
    """
    
    return html


def format_priority_actions_v2(rule_results, final_synthesis, best_practices) -> str:
    """Format priority actions HTML."""
    
    actions = final_synthesis.top_3_actions if final_synthesis.top_3_actions else []
    
    # If no synthesis actions, build from rule engine
    if not actions and best_practices and best_practices.priority_fixes:
        actions = best_practices.priority_fixes
    
    if not actions:
        # Default actions from rule engine
        actions = []
        if rule_results.aa_failures > 0:
            actions.append({
                "action": f"Fix {rule_results.aa_failures} colors failing AA compliance",
                "impact": "high",
                "effort": "30 min",
            })
        if not rule_results.typography.is_consistent:
            actions.append({
                "action": f"Align type scale to {rule_results.typography.recommendation} ({rule_results.typography.recommendation_name})",
                "impact": "medium", 
                "effort": "1 hour",
            })
        if rule_results.color_stats.unique_count > 30:
            actions.append({
                "action": f"Consolidate {rule_results.color_stats.unique_count} colors to ~15 semantic colors",
                "impact": "medium",
                "effort": "2 hours",
            })
    
    html_items = []
    for i, action in enumerate(actions[:3]):
        impact = action.get('impact', 'medium')
        border_color = "#ef4444" if impact == "high" else "#f59e0b" if impact == "medium" else "#10b981"
        impact_bg = "#fee2e2" if impact == "high" else "#fef3c7" if impact == "medium" else "#dcfce7"
        impact_text = "#991b1b" if impact == "high" else "#92400e" if impact == "medium" else "#166534"
        icon = "πŸ”΄" if impact == "high" else "🟑" if impact == "medium" else "🟒"
        
        html_items.append(f"""
        <div class="priority-action-card" style="border-left: 4px solid {border_color};">
            <div style="display: flex; justify-content: space-between; align-items: flex-start;">
                <div>
                    <div class="priority-action-title">
                        {icon} {action.get('action', 'N/A')}
                    </div>
                    <div class="priority-action-detail">
                        {action.get('details', '')}
                    </div>
                </div>
                <div style="display: flex; gap: 8px;">
                    <span style="background: {impact_bg}; color: {impact_text}; padding: 4px 8px;
                                 border-radius: 12px; font-size: 11px; font-weight: 600;">
                        {impact.upper()}
                    </span>
                    <span class="priority-effort-badge">
                        {action.get('effort', '?')}
                    </span>
                </div>
            </div>
        </div>
        """)
    
    return f"""
    <style>
        .priority-actions-wrap {{ margin: 20px 0; }}
        .priority-actions-wrap h3 {{ margin-bottom: 16px; color: #1e293b; }}
        .priority-action-card {{ background: white; border: 1px solid #e2e8f0; border-radius: 8px; padding: 16px; margin-bottom: 12px; }}
        .priority-action-title {{ font-weight: 600; color: #1e293b; margin-bottom: 4px; }}
        .priority-action-detail {{ font-size: 13px; color: #64748b; }}
        .priority-effort-badge {{ background: #f1f5f9; color: #475569; padding: 4px 8px; border-radius: 12px; font-size: 11px; }}
        .dark .priority-actions-wrap h3 {{ color: #f1f5f9 !important; }}
        .dark .priority-action-card {{ background: #1e293b !important; border-color: #475569 !important; }}
        .dark .priority-action-title {{ color: #f1f5f9 !important; }}
        .dark .priority-action-detail {{ color: #94a3b8 !important; }}
        .dark .priority-effort-badge {{ background: #334155 !important; color: #cbd5e1 !important; }}
    </style>
    <div class="priority-actions-wrap">
        <h3>🎯 Priority Actions</h3>
        {''.join(html_items)}
    </div>
    """


def format_color_recommendations_table_v2(rule_results, brand_result, final_synthesis) -> list:
    """Format color recommendations as table data."""
    
    rows = []
    
    # Add AA failures with fixes
    for a in rule_results.accessibility:
        if not a.passes_aa_normal and a.suggested_fix:
            role = "brand.primary" if brand_result and brand_result.brand_primary.get("color") == a.hex_color else a.name
            rows.append([
                True,  # Accept checkbox
                role,
                a.hex_color,
                f"Fails AA ({a.contrast_on_white:.1f}:1)",
                a.suggested_fix,
                f"{a.suggested_fix_contrast:.1f}:1",
            ])
    
    # Add recommendations from synthesis
    if final_synthesis and final_synthesis.color_recommendations:
        for rec in final_synthesis.color_recommendations:
            if rec.get("current") != rec.get("suggested"):
                # Check if not already in rows
                if not any(r[2] == rec.get("current") for r in rows):
                    rows.append([
                        rec.get("accept", True),
                        rec.get("role", "unknown"),
                        rec.get("current", ""),
                        rec.get("reason", ""),
                        rec.get("suggested", ""),
                        "",
                    ])
    
    return rows


def build_analysis_status(final_recs: dict, cost_tracking: dict, errors: list) -> str:
    """Build status markdown from analysis results."""
    
    lines = ["## 🧠 Multi-Agent Analysis Complete!"]
    lines.append("")
    
    # Cost summary
    if cost_tracking:
        total_cost = cost_tracking.get("total_cost", 0)
        lines.append(f"### πŸ’° Cost Summary")
        lines.append(f"**Total estimated cost:** ${total_cost:.4f}")
        lines.append(f"*(Free tier: $0.10/mo | Pro: $2.00/mo)*")
        lines.append("")
    
    # Final recommendations
    if final_recs and "final_recommendations" in final_recs:
        recs = final_recs["final_recommendations"]
        lines.append("### πŸ“‹ Recommendations")
        
        if recs.get("type_scale"):
            lines.append(f"**Type Scale:** {recs['type_scale']}")
            if recs.get("type_scale_rationale"):
                lines.append(f"  *{recs['type_scale_rationale'][:100]}*")
        
        if recs.get("spacing_base"):
            lines.append(f"**Spacing:** {recs['spacing_base']}")
        
        lines.append("")
    
    # Summary
    if final_recs.get("summary"):
        lines.append("### πŸ“ Summary")
        lines.append(final_recs["summary"])
        lines.append("")
    
    # Confidence
    if final_recs.get("overall_confidence"):
        lines.append(f"**Confidence:** {final_recs['overall_confidence']}%")
    
    # Errors
    if errors:
        lines.append("")
        lines.append("### ⚠️ Warnings")
        for err in errors[:3]:
            lines.append(f"- {err[:100]}")
    
    return "\n".join(lines)


def format_multi_agent_comparison(llm1: dict, llm2: dict, final: dict) -> str:
    """Format comparison from multi-agent analysis."""
    
    lines = ["### πŸ“Š Multi-Agent Analysis Comparison"]
    lines.append("")
    
    # Agreements
    if final.get("agreements"):
        lines.append("#### βœ… Agreements (High Confidence)")
        for a in final["agreements"][:5]:
            topic = a.get("topic", "?")
            finding = a.get("finding", "?")[:80]
            lines.append(f"- **{topic}**: {finding}")
        lines.append("")
    
    # Disagreements and resolutions
    if final.get("disagreements"):
        lines.append("#### πŸ”„ Resolved Disagreements")
        for d in final["disagreements"][:3]:
            topic = d.get("topic", "?")
            resolution = d.get("resolution", "?")[:100]
            lines.append(f"- **{topic}**: {resolution}")
        lines.append("")
    
    # Score comparison
    lines.append("#### πŸ“ˆ Score Comparison")
    lines.append("")
    lines.append("| Category | LLM 1 (Qwen) | LLM 2 (Llama) |")
    lines.append("|----------|--------------|---------------|")
    
    categories = ["typography", "colors", "accessibility", "spacing"]
    for cat in categories:
        llm1_score = llm1.get(cat, {}).get("score", "?") if isinstance(llm1.get(cat), dict) else "?"
        llm2_score = llm2.get(cat, {}).get("score", "?") if isinstance(llm2.get(cat), dict) else "?"
        lines.append(f"| {cat.title()} | {llm1_score}/10 | {llm2_score}/10 |")
    
    return "\n".join(lines)


def format_spacing_comparison_from_rules(rule_calculations: dict) -> list:
    """Format spacing comparison from rule engine."""
    if not rule_calculations:
        return []
    
    spacing_options = rule_calculations.get("spacing_options", {})
    
    data = []
    for i in range(10):
        current = f"{(i+1) * 4}px" if i < 5 else f"{(i+1) * 8}px"
        grid_8 = spacing_options.get("8px", [])
        grid_4 = spacing_options.get("4px", [])
        
        val_8 = f"{grid_8[i+1]}px" if i+1 < len(grid_8) else "β€”"
        val_4 = f"{grid_4[i+1]}px" if i+1 < len(grid_4) else "β€”"
        
        data.append([current, val_8, val_4])
    
    return data


def format_color_ramps_from_rules(rule_calculations: dict) -> str:
    """Format color ramps from rule engine."""
    if not rule_calculations:
        return "*No color ramps generated*"
    
    ramps = rule_calculations.get("color_ramps", {})
    if not ramps:
        return "*No color ramps generated*"
    
    lines = ["### 🌈 Generated Color Ramps"]
    lines.append("")
    
    for name, ramp in list(ramps.items())[:6]:
        lines.append(f"**{name}**")
        if isinstance(ramp, list) and len(ramp) >= 10:
            lines.append("| 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 |")
            lines.append("|---|---|---|---|---|---|---|---|---|---|")
            row = "| " + " | ".join([f"`{ramp[i]}`" for i in range(10)]) + " |"
            lines.append(row)
        lines.append("")
    
    return "\n".join(lines)


def get_detected_fonts() -> dict:
    """Get detected font information."""
    if not state.desktop_normalized:
        return {"primary": "Unknown", "weights": []}
    
    fonts = {}
    weights = set()
    
    for t in state.desktop_normalized.typography.values():
        family = t.font_family
        weight = t.font_weight
        
        if family not in fonts:
            fonts[family] = 0
        fonts[family] += t.frequency
        
        if weight:
            try:
                weights.add(int(weight))
            except (ValueError, TypeError):
                pass
    
    primary = max(fonts.items(), key=lambda x: x[1])[0] if fonts else "Unknown"
    
    return {
        "primary": primary,
        "weights": sorted(weights) if weights else [400],
        "all_fonts": fonts,
    }


def get_base_font_size() -> int:
    """Detect base font size from typography."""
    if not state.desktop_normalized:
        return 16
    
    # Find most common size in body range (14-18px)
    sizes = {}
    for t in state.desktop_normalized.typography.values():
        size_str = str(t.font_size).replace('px', '').replace('rem', '').replace('em', '')
        try:
            size = float(size_str)
            if 14 <= size <= 18:
                sizes[size] = sizes.get(size, 0) + t.frequency
        except (ValueError, TypeError):
            pass
    
    if sizes:
        return int(max(sizes.items(), key=lambda x: x[1])[0])
    return 16


def format_brand_comparison(recommendations) -> str:
    """Format brand comparison as markdown table."""
    if not recommendations.brand_analysis:
        return "*Brand analysis not available*"
    
    lines = [
        "### πŸ“Š Design System Comparison (5 Top Brands)",
        "",
        "| Brand | Type Ratio | Base Size | Spacing | Notes |",
        "|-------|------------|-----------|---------|-------|",
    ]
    
    for brand in recommendations.brand_analysis[:5]:
        name = brand.get("brand", "Unknown")
        ratio = brand.get("ratio", "?")
        base = brand.get("base", "?")
        spacing = brand.get("spacing", "?")
        notes = brand.get("notes", "")[:50] + ("..." if len(brand.get("notes", "")) > 50 else "")
        lines.append(f"| {name} | {ratio} | {base}px | {spacing} | {notes} |")
    
    return "\n".join(lines)


def format_font_families_display(fonts: dict) -> str:
    """Format detected font families for display."""
    lines = []
    
    primary = fonts.get("primary", "Unknown")
    weights = fonts.get("weights", [400])
    all_fonts = fonts.get("all_fonts", {})
    
    lines.append(f"### Primary Font: **{primary}**")
    lines.append("")
    lines.append(f"**Weights detected:** {', '.join(map(str, weights))}")
    lines.append("")
    
    if all_fonts and len(all_fonts) > 1:
        lines.append("### All Fonts Detected")
        lines.append("")
        lines.append("| Font Family | Usage Count |")
        lines.append("|-------------|-------------|")
        
        sorted_fonts = sorted(all_fonts.items(), key=lambda x: -x[1])
        for font, count in sorted_fonts[:5]:
            lines.append(f"| {font} | {count:,} |")
    
    lines.append("")
    lines.append("*Note: This analysis focuses on English typography only.*")
    
    return "\n".join(lines)


def format_llm_color_recommendations_html(final_recs: dict, semantic_analysis: dict) -> str:
    """Generate HTML showing LLM color recommendations with before/after comparison."""
    
    if not final_recs:
        return '''
        <div class="placeholder-msg" style="text-align: center;">
            <p>No LLM recommendations available yet. Run analysis first.</p>
        </div>
        '''
    
    color_recs = final_recs.get("color_recommendations", {})
    aa_fixes = final_recs.get("accessibility_fixes", [])
    
    if not color_recs and not aa_fixes:
        return '''
        <div class="llm-no-recs" style="padding: 20px; border-radius: 8px; border: 1px solid #28a745; background: #d4edda;">
            <p style="margin: 0; color: #155724;">βœ… No color changes recommended. Your colors look good!</p>
        </div>
        <style>
            .dark .llm-no-recs { background: #14532d !important; border-color: #22c55e !important; }
            .dark .llm-no-recs p { color: #86efac !important; }
        </style>
        '''
    
    # Build recommendations HTML
    recs_html = ""
    
    # Process color recommendations
    for role, rec in color_recs.items():
        if not isinstance(rec, dict):
            continue
        if role in ["generate_ramps_for", "changes_made"]:
            continue
            
        current = rec.get("current", "?")
        suggested = rec.get("suggested", current)
        action = rec.get("action", "keep")
        rationale = rec.get("rationale", "")
        
        if action == "keep" or suggested == current:
            # No change needed
            recs_html += f'''
            <div class="llm-rec-row keep">
                <div class="rec-color-box" style="background: {current};"></div>
                <div class="rec-details">
                    <span class="rec-role">{role}</span>
                    <span class="rec-current">{current}</span>
                    <span class="rec-action keep">βœ“ Keep</span>
                </div>
            </div>
            '''
        else:
            # Change suggested
            recs_html += f'''
            <div class="llm-rec-row change">
                <div class="rec-comparison">
                    <div class="rec-before">
                        <div class="rec-color-box" style="background: {current};"></div>
                        <span class="rec-label">Before</span>
                        <span class="rec-hex">{current}</span>
                    </div>
                    <span class="rec-arrow">β†’</span>
                    <div class="rec-after">
                        <div class="rec-color-box" style="background: {suggested};"></div>
                        <span class="rec-label">After</span>
                        <span class="rec-hex">{suggested}</span>
                    </div>
                </div>
                <div class="rec-details">
                    <span class="rec-role">{role}</span>
                    <span class="rec-rationale">{rationale[:80]}...</span>
                </div>
            </div>
            '''
    
    # Process accessibility fixes
    for fix in aa_fixes:
        if not isinstance(fix, dict):
            continue
        
        color = fix.get("color", "?")
        role = fix.get("role", "unknown")
        issue = fix.get("issue", "contrast issue")
        fix_color = fix.get("fix", color)
        current_contrast = fix.get("current_contrast", "?")
        fixed_contrast = fix.get("fixed_contrast", "?")
        
        if fix_color and fix_color != color:
            recs_html += f'''
            <div class="llm-rec-row aa-fix">
                <div class="rec-comparison">
                    <div class="rec-before">
                        <div class="rec-color-box" style="background: {color};"></div>
                        <span class="rec-label">⚠️ {current_contrast}:1</span>
                        <span class="rec-hex">{color}</span>
                    </div>
                    <span class="rec-arrow">β†’</span>
                    <div class="rec-after">
                        <div class="rec-color-box" style="background: {fix_color};"></div>
                        <span class="rec-label">βœ“ {fixed_contrast}:1</span>
                        <span class="rec-hex">{fix_color}</span>
                    </div>
                </div>
                <div class="rec-details">
                    <span class="rec-role">{role}</span>
                    <span class="rec-issue">πŸ”΄ {issue}</span>
                </div>
            </div>
            '''
    
    if not recs_html:
        return '''
        <div class="llm-no-recs" style="padding: 20px; border-radius: 8px; border: 1px solid #28a745; background: #d4edda;">
            <p style="margin: 0; color: #155724;">βœ… No color changes recommended. Your colors look good!</p>
        </div>
        <style>
            .dark .llm-no-recs { background: #14532d !important; border-color: #22c55e !important; }
            .dark .llm-no-recs p { color: #86efac !important; }
        </style>
        '''
    
    html = f'''
    <style>
        .llm-recs-container {{
            font-family: system-ui, -apple-system, sans-serif;
            background: #f5f5f5 !important;
            border-radius: 12px;
            padding: 16px;
        }}
        
        .llm-rec-row {{
            display: flex;
            align-items: center;
            padding: 12px;
            margin-bottom: 12px;
            border-radius: 8px;
            background: #ffffff !important;
            border: 1px solid #e0e0e0 !important;
        }}
        
        .llm-rec-row.change {{
            border-left: 4px solid #f59e0b !important;
        }}
        
        .llm-rec-row.aa-fix {{
            border-left: 4px solid #dc2626 !important;
            background: #fef2f2 !important;
        }}
        
        .llm-rec-row.keep {{
            border-left: 4px solid #22c55e !important;
            background: #f0fdf4 !important;
        }}
        
        .rec-comparison {{
            display: flex;
            align-items: center;
            gap: 12px;
            margin-right: 20px;
        }}
        
        .rec-before, .rec-after {{
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 4px;
        }}
        
        .rec-color-box {{
            width: 48px;
            height: 48px;
            border-radius: 8px;
            border: 2px solid rgba(0,0,0,0.15) !important;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }}
        
        .rec-label {{
            font-size: 11px;
            font-weight: 600;
            color: #666 !important;
        }}
        
        .rec-hex {{
            font-family: 'SF Mono', Monaco, monospace;
            font-size: 11px;
            color: #333 !important;
        }}
        
        .rec-arrow {{
            font-size: 20px;
            color: #666 !important;
            font-weight: bold;
        }}
        
        .rec-details {{
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 4px;
        }}
        
        .rec-role {{
            font-weight: 700;
            font-size: 14px;
            color: #1a1a1a !important;
        }}
        
        .rec-action {{
            font-size: 12px;
            padding: 2px 8px;
            border-radius: 4px;
        }}
        
        .rec-action.keep {{
            background: #dcfce7 !important;
            color: #166534 !important;
        }}
        
        .rec-rationale {{
            font-size: 12px;
            color: #666 !important;
        }}
        
        .rec-issue {{
            font-size: 12px;
            color: #991b1b !important;
            font-weight: 500;
        }}

        /* Dark mode */
        .dark .llm-recs-container {{ background: #0f172a !important; }}
        .dark .llm-rec-row {{ background: #1e293b !important; border-color: #475569 !important; }}
        .dark .llm-rec-row.aa-fix {{ background: #450a0a !important; }}
        .dark .llm-rec-row.keep {{ background: #14532d !important; }}
        .dark .rec-label {{ color: #94a3b8 !important; }}
        .dark .rec-hex {{ color: #cbd5e1 !important; }}
        .dark .rec-arrow {{ color: #94a3b8 !important; }}
        .dark .rec-role {{ color: #f1f5f9 !important; }}
        .dark .rec-rationale {{ color: #94a3b8 !important; }}
        .dark .rec-issue {{ color: #fca5a5 !important; }}
        .dark .rec-action.keep {{ background: #14532d !important; color: #86efac !important; }}
    </style>
    
    <div class="llm-recs-container">
        {recs_html}
    </div>
    '''
    
    return html


def format_llm_color_recommendations_table(final_recs: dict, semantic_analysis: dict) -> list:
    """Generate table data for LLM color recommendations with accept/reject checkboxes."""
    
    rows = []
    
    if not final_recs:
        return rows
    
    color_recs = final_recs.get("color_recommendations", {})
    aa_fixes = final_recs.get("accessibility_fixes", [])
    
    # Process color recommendations
    for role, rec in color_recs.items():
        if not isinstance(rec, dict):
            continue
        if role in ["generate_ramps_for", "changes_made"]:
            continue
            
        current = rec.get("current", "?")
        suggested = rec.get("suggested", current)
        action = rec.get("action", "keep")
        rationale = rec.get("rationale", "")[:50]
        
        if action != "keep" and suggested != current:
            # Calculate contrast improvement
            try:
                from core.color_utils import get_contrast_with_white
                old_contrast = get_contrast_with_white(current)
                new_contrast = get_contrast_with_white(suggested)
                contrast_str = f"{old_contrast:.1f} β†’ {new_contrast:.1f}"
            except (ValueError, TypeError, ZeroDivisionError):
                contrast_str = "?"
            
            rows.append([
                True,  # Accept checkbox (default True)
                role,
                current,
                rationale or action,
                suggested,
                contrast_str,
            ])
    
    # Process accessibility fixes
    for fix in aa_fixes:
        if not isinstance(fix, dict):
            continue
        
        color = fix.get("color", "?")
        role = fix.get("role", "unknown")
        issue = fix.get("issue", "contrast")[:40]
        fix_color = fix.get("fix", color)
        current_contrast = fix.get("current_contrast", "?")
        fixed_contrast = fix.get("fixed_contrast", "?")
        
        if fix_color and fix_color != color:
            rows.append([
                True,  # Accept checkbox
                f"{role} (AA fix)",
                color,
                issue,
                fix_color,
                f"{current_contrast}:1 β†’ {fixed_contrast}:1",
            ])
    
    return rows


def format_typography_comparison_viewport(normalized_tokens, base_size: int, viewport: str) -> list:
    """Format typography comparison for a specific viewport."""
    if not normalized_tokens:
        return []
    
    # Get current typography sorted by size
    current_typo = list(normalized_tokens.typography.values())
    
    # Parse and sort sizes
    def parse_size(t):
        size_str = str(t.font_size).replace('px', '').replace('rem', '').replace('em', '')
        try:
            return float(size_str)
        except (ValueError, TypeError):
            return 16
    
    current_typo.sort(key=lambda t: -parse_size(t))
    sizes = [parse_size(t) for t in current_typo]
    
    # Use detected base or default
    base = base_size if base_size else 16
    
    # Scale factors for mobile (typically 0.85-0.9 of desktop)
    mobile_factor = 0.875 if viewport == "mobile" else 1.0
    
    # Token names (13 levels)
    token_names = [
        "display.2xl", "display.xl", "display.lg", "display.md",
        "heading.xl", "heading.lg", "heading.md", "heading.sm",
        "body.lg", "body.md", "body.sm",
        "caption", "overline"
    ]
    
    # Generate scales - use base size and round to sensible values
    def round_to_even(val):
        """Round to even numbers for cleaner type scales."""
        return int(round(val / 2) * 2)
    
    scales = {
        "1.2": [round_to_even(base * mobile_factor * (1.2 ** (8-i))) for i in range(13)],
        "1.25": [round_to_even(base * mobile_factor * (1.25 ** (8-i))) for i in range(13)],
        "1.333": [round_to_even(base * mobile_factor * (1.333 ** (8-i))) for i in range(13)],
    }
    
    # Build comparison table
    data = []
    for i, name in enumerate(token_names):
        current = f"{int(sizes[i])}px" if i < len(sizes) else "β€”"
        s12 = f"{scales['1.2'][i]}px"
        s125 = f"{scales['1.25'][i]}px"
        s133 = f"{scales['1.333'][i]}px"
        keep = current
        data.append([name, current, s12, s125, s133, keep])
    
    return data


def format_base_colors() -> str:
    """Format base colors (detected) separately from ramps."""
    if not state.desktop_normalized:
        return "*No colors detected*"
    
    colors = list(state.desktop_normalized.colors.values())
    colors.sort(key=lambda c: -c.frequency)
    
    lines = [
        "### 🎨 Base Colors (Detected)",
        "",
        "These are the primary colors extracted from your website:",
        "",
        "| Color | Hex | Role | Frequency | Contrast |",
        "|-------|-----|------|-----------|----------|",
    ]
    
    for color in colors[:10]:
        hex_val = color.value
        role = "Primary" if color.suggested_name and "primary" in color.suggested_name.lower() else \
               "Text" if color.suggested_name and "text" in color.suggested_name.lower() else \
               "Background" if color.suggested_name and "background" in color.suggested_name.lower() else \
               "Border" if color.suggested_name and "border" in color.suggested_name.lower() else \
               "Accent"
        freq = f"{color.frequency:,}"
        contrast = f"{color.contrast_white:.1f}:1" if color.contrast_white else "β€”"
        
        # Create a simple color indicator
        lines.append(f"| 🟦 | `{hex_val}` | {role} | {freq} | {contrast} |")
    
    return "\n".join(lines)


def format_color_ramps_visual(recommendations) -> str:
    """Format color ramps with visual display showing all shades."""
    if not state.desktop_normalized:
        return "*No colors to display*"
    
    colors = list(state.desktop_normalized.colors.values())
    colors.sort(key=lambda c: -c.frequency)
    
    lines = [
        "### 🌈 Generated Color Ramps",
        "",
        "Full ramp (50-950) generated for each base color:",
        "",
    ]
    
    from core.color_utils import generate_color_ramp
    
    for color in colors[:6]:  # Top 6 colors
        hex_val = color.value
        role = color.suggested_name.split('.')[1] if color.suggested_name and '.' in color.suggested_name else "color"
        
        # Generate ramp
        try:
            ramp = generate_color_ramp(hex_val)
            
            lines.append(f"**{role.upper()}** (base: `{hex_val}`)")
            lines.append("")
            lines.append("| 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 |")
            lines.append("|---|---|---|---|---|---|---|---|---|---|")
            
            # Create row with hex values
            row = "|"
            for i in range(10):
                if i < len(ramp):
                    row += f" `{ramp[i]}` |"
                else:
                    row += " β€” |"
            lines.append(row)
            lines.append("")
            
        except Exception as e:
            lines.append(f"**{role}** (`{hex_val}`) β€” Could not generate ramp: {str(e)}")
            lines.append("")
    
    return "\n".join(lines)


def format_radius_with_tokens() -> str:
    """Format radius with token name suggestions."""
    if not state.desktop_normalized or not state.desktop_normalized.radius:
        return "*No border radius values detected.*"
    
    radii = list(state.desktop_normalized.radius.values())
    
    lines = [
        "### πŸ”˜ Border Radius Tokens",
        "",
        "| Detected | Suggested Token | Usage |",
        "|----------|-----------------|-------|",
    ]
    
    # Sort by pixel value
    def parse_radius(r):
        val = str(r.value).replace('px', '').replace('%', '')
        try:
            return float(val)
        except (ValueError, TypeError):
            return 999
    
    radii.sort(key=lambda r: parse_radius(r))
    
    token_map = {
        (0, 2): ("radius.none", "Sharp corners"),
        (2, 4): ("radius.xs", "Subtle rounding"),
        (4, 6): ("radius.sm", "Small elements"),
        (6, 10): ("radius.md", "Buttons, cards"),
        (10, 16): ("radius.lg", "Modals, panels"),
        (16, 32): ("radius.xl", "Large containers"),
        (32, 100): ("radius.2xl", "Pill shapes"),
    }
    
    for r in radii[:8]:
        val = str(r.value)
        px = parse_radius(r)
        
        if "%" in str(r.value) or px >= 50:
            token = "radius.full"
            usage = "Circles, avatars"
        else:
            token = "radius.md"
            usage = "General use"
            for (low, high), (t, u) in token_map.items():
                if low <= px < high:
                    token = t
                    usage = u
                    break
        
        lines.append(f"| {val} | `{token}` | {usage} |")
    
    return "\n".join(lines)


def format_shadows_with_tokens() -> str:
    """Format shadows with token name suggestions."""
    if not state.desktop_normalized or not state.desktop_normalized.shadows:
        return "*No shadow values detected.*"
    
    shadows = list(state.desktop_normalized.shadows.values())
    
    lines = [
        "### 🌫️ Shadow Tokens",
        "",
        "| Detected Value | Suggested Token | Use Case |",
        "|----------------|-----------------|----------|",
    ]
    
    shadow_sizes = ["shadow.xs", "shadow.sm", "shadow.md", "shadow.lg", "shadow.xl", "shadow.2xl"]
    
    for i, s in enumerate(shadows[:6]):
        val = str(s.value)[:40] + ("..." if len(str(s.value)) > 40 else "")
        token = shadow_sizes[i] if i < len(shadow_sizes) else f"shadow.custom-{i}"
        
        # Guess use case based on index
        use_cases = ["Subtle elevation", "Cards, dropdowns", "Modals, dialogs", "Popovers", "Floating elements", "Dramatic effect"]
        use = use_cases[i] if i < len(use_cases) else "Custom"
        
        lines.append(f"| `{val}` | `{token}` | {use} |")
    
    return "\n".join(lines)


def format_spacing_comparison(recommendations) -> list:
    """Format spacing comparison table."""
    if not state.desktop_normalized:
        return []
    
    # Get current spacing
    current_spacing = list(state.desktop_normalized.spacing.values())
    current_spacing.sort(key=lambda s: s.value_px)
    
    data = []
    for s in current_spacing[:10]:
        current = f"{s.value_px}px"
        grid_8 = f"{snap_to_grid(s.value_px, 8)}px"
        grid_4 = f"{snap_to_grid(s.value_px, 4)}px"
        
        # Mark if value fits
        if s.value_px == snap_to_grid(s.value_px, 8):
            grid_8 += " βœ“"
        if s.value_px == snap_to_grid(s.value_px, 4):
            grid_4 += " βœ“"
        
        data.append([current, grid_8, grid_4])
    
    return data


def snap_to_grid(value: float, base: int) -> int:
    """Snap value to grid."""
    return round(value / base) * base


def reset_to_original():
    """Reset all upgrade selections to defaults."""
    state.selected_upgrades = {}
    state.log("")
    state.log("↩️ Reset all upgrade selections to original values.")

    return (
        "Scale 1.25 (Major Third) ⭐",     # type_scale_radio
        "8px Base Grid ⭐",                  # spacing_radio
        True,                                 # color_ramps_checkbox
        "## ↩️ Reset Complete\n\nAll selections reverted to defaults. Review and apply again when ready.",  # apply_status
        state.get_logs(),                     # stage2_log
    )


def apply_selected_upgrades(type_choice: str, spacing_choice: str, apply_ramps: bool, color_recs_table: list = None):
    """Apply selected upgrade options including LLM color recommendations."""
    if not state.upgrade_recommendations:
        return "## ❌ Run Analysis First\n\nPlease run the **v2 Analysis** before applying upgrades.", state.get_logs()

    state.log("")
    state.log("═" * 50)
    state.log("✨ APPLYING SELECTED UPGRADES")
    state.log("═" * 50)

    # Store selections
    state.selected_upgrades = {
        "type_scale": type_choice,
        "spacing": spacing_choice,
        "color_ramps": apply_ramps,
    }

    state.log(f"   πŸ“ Type Scale: {type_choice}")
    state.log(f"   πŸ“ Spacing: {spacing_choice}")
    state.log(f"   🌈 Color Ramps: {'Yes' if apply_ramps else 'No'}")

    # Process accepted color recommendations
    accepted_color_changes = []
    rejected_count = 0
    # Normalize color_recs_table: Gradio 6 may pass a DataFrame or list-of-lists
    _color_rows = []
    if color_recs_table is not None:
        try:
            import pandas as pd
            if isinstance(color_recs_table, pd.DataFrame) and not color_recs_table.empty:
                _color_rows = color_recs_table.values.tolist()
            elif isinstance(color_recs_table, (list, tuple)) and len(color_recs_table) > 0:
                _color_rows = list(color_recs_table)
        except Exception:
            if isinstance(color_recs_table, (list, tuple)):
                _color_rows = list(color_recs_table)

    if _color_rows:
        state.log("")
        state.log("   🎨 LLM Color Recommendations:")
        for row in _color_rows:
            if len(row) >= 5:
                accept = row[0]  # Boolean checkbox
                role = row[1]    # Role name
                current = row[2] # Current color
                issue = row[3]   # Issue description
                suggested = row[4]  # Suggested color

                if accept and suggested and current != suggested:
                    accepted_color_changes.append({
                        "role": role,
                        "from": current,
                        "to": suggested,
                        "reason": issue,
                    })
                    state.log(f"   β”œβ”€ βœ… ACCEPTED: {role}")
                    state.log(f"   β”‚  └─ {current} β†’ {suggested}")
                elif not accept:
                    rejected_count += 1
                    state.log(f"   β”œβ”€ ❌ REJECTED: {role} (keeping {current})")

    # Store accepted changes
    state.selected_upgrades["color_changes"] = accepted_color_changes

    state.log("")
    if accepted_color_changes:
        state.log(f"   πŸ“Š {len(accepted_color_changes)} color change(s) will be applied to export")
    if rejected_count:
        state.log(f"   πŸ“Š {rejected_count} color change(s) rejected (keeping original)")

    state.log("")
    state.log("βœ… Upgrades applied! Proceed to Stage 3 for export.")
    state.log("═" * 50)

    # Build visible feedback summary
    summary_parts = []
    summary_parts.append(f"**Type Scale:** {type_choice}")
    summary_parts.append(f"**Spacing:** {spacing_choice}")
    summary_parts.append(f"**Color Ramps:** {'βœ… Enabled' if apply_ramps else '❌ Disabled'}")
    if accepted_color_changes:
        summary_parts.append(f"**Color Changes:** {len(accepted_color_changes)} accepted")
    if rejected_count:
        summary_parts.append(f"**Rejected:** {rejected_count} kept as-is")

    status_md = f"""## βœ… Upgrades Applied Successfully!

{chr(10).join('- ' + p for p in summary_parts)}

πŸ‘‰ **Proceed to Stage 3** to export your upgraded tokens.
"""
    return status_md, state.get_logs()


# =============================================================================
# EXPORT HELPERS β€” Semantic Token Naming
# =============================================================================

def _get_radius_token_name(value_str, seen_names: dict = None) -> str:
    """Map radius px value to semantic token name (radius.sm, radius.md, etc.)."""
    val = str(value_str).replace('px', '').replace('%', '')
    try:
        px = float(val)
    except (ValueError, TypeError):
        return "radius.md"

    # Handle percentage values (e.g., "50%" for circular)
    if "%" in str(value_str):
        base_name = "radius.full"
    # "none" is ONLY for exactly 0px
    elif px == 0:
        base_name = "radius.none"
    elif px >= 9999:
        # Large values (like 9999px) are essentially "full"
        base_name = "radius.full"
    else:
        # Semantic naming based on pixel ranges (inclusive both ends for clarity)
        mapping = [
            (1, 1, "radius.xs"),      # 1px = xs
            (2, 3, "radius.sm"),      # 2-3px = sm
            (4, 7, "radius.md"),      # 4-7px = md
            (8, 11, "radius.lg"),     # 8-11px = lg
            (12, 19, "radius.xl"),    # 12-19px = xl
            (20, 31, "radius.2xl"),   # 20-31px = 2xl
            (32, 99, "radius.3xl"),   # 32-99px = 3xl
        ]
        base_name = "radius.md"
        for low, high, name in mapping:
            if low <= px <= high:
                base_name = name
                break

    # Handle duplicates: if two radii map to same semantic name, skip the duplicate.
    # Old behavior appended ".{px}" which created invalid nested DTCG structures.
    if seen_names is not None:
        if base_name in seen_names:
            return None  # Signal caller to skip this duplicate radius
        seen_names[base_name] = True
    return base_name


def _get_shadow_blur(value_str: str) -> float:
    """Extract blur radius from shadow value for sorting."""
    import re
    # Shadow format: "Xpx Ypx BLURpx SPREADpx color"
    parts = re.findall(r'([\d.]+)px', str(value_str))
    if len(parts) >= 3:
        return float(parts[2])  # blur is 3rd px value
    elif len(parts) >= 1:
        return float(parts[0])
    return 0


def _parse_shadow_to_tokens_studio(value_str: str) -> dict:
    """Parse CSS shadow string to Figma Tokens Studio boxShadow format.

    Input: "rgba(0, 0, 0, 0.5) 0px 2px 4px 0px" or "0px 2px 4px 0px rgba(0,0,0,0.5)"
    Output: {"x": "0", "y": "2", "blur": "4", "spread": "0", "color": "rgba(0,0,0,0.5)", "type": "dropShadow"}
    """
    import re
    value_str = str(value_str).strip()

    # Extract color (rgba/rgb/hex)
    color_match = re.search(r'(rgba?\([^)]+\)|#[0-9a-fA-F]{3,8})', value_str)
    color = color_match.group(1) if color_match else "rgba(0,0,0,0.25)"

    # Extract px values
    px_values = re.findall(r'(-?[\d.]+)px', value_str)

    # Standard order: x y blur spread
    x = px_values[0] if len(px_values) > 0 else "0"
    y = px_values[1] if len(px_values) > 1 else "0"
    blur = px_values[2] if len(px_values) > 2 else "0"
    spread = px_values[3] if len(px_values) > 3 else "0"

    # Determine if inset
    shadow_type = "innerShadow" if "inset" in value_str.lower() else "dropShadow"

    return {
        "x": x,
        "y": y,
        "blur": blur,
        "spread": spread,
        "color": color,
        "type": shadow_type,
    }


# =============================================================================
# W3C DTCG FORMAT HELPERS
# =============================================================================

def _flat_key_to_nested(flat_key: str, value: dict, root: dict):
    """Convert 'color.brand.primary' into nested dict structure.

    Example: _flat_key_to_nested('color.brand.primary', token, {})
    Result: {'color': {'brand': {'primary': token}}}

    Safety: If a path segment is already a DTCG leaf token ($type/$value),
    the new token is SKIPPED to avoid creating invalid nested structures
    like: {"$type":"color","$value":"#abc","2":{"$type":"color","$value":"#def"}}
    """
    parts = flat_key.split('.')
    current = root
    for part in parts[:-1]:
        if part not in current:
            current[part] = {}
        node = current[part]
        # Guard: don't navigate into an existing leaf token
        if isinstance(node, dict) and ('$type' in node or '$value' in node):
            # This path would nest a child inside a DTCG leaf β€” skip silently
            return
        current = node
    current[parts[-1]] = value


def _to_dtcg_token(value, token_type: str, description: str = None,
                    source: str = None, extensions: dict = None) -> dict:
    """Wrap value in W3C DTCG v1 (2025.10) format.

    Spec: https://www.designtokens.org/tr/drafts/format/

    Args:
        value: The token value
        token_type: W3C DTCG type β€” must be one of:
            color, dimension, fontFamily, fontWeight, number,
            duration, cubicBezier, shadow, strokeStyle, border,
            transition, gradient, typography
        description: Optional human-readable description
        source: Optional source indicator (extracted, recommended, semantic)
        extensions: Optional dict for $extensions (custom metadata like frequency, confidence)
    """
    token = {"$type": token_type, "$value": value}
    if description and source:
        token["$description"] = f"[{source}] {description}"
    elif description:
        token["$description"] = description
    if extensions:
        token["$extensions"] = {"com.design-system-automation": extensions}
    return token


def _shadow_to_dtcg(shadow_dict: dict) -> dict:
    """Convert our internal shadow format to W3C DTCG shadow spec.

    Input: {"x": "0", "y": "2", "blur": "4", "spread": "0", "color": "rgba(...)"}
    Output: {"color": "...", "offsetX": "0px", "offsetY": "2px", "blur": "4px", "spread": "0px"}
    """
    return {
        "color": shadow_dict.get("color", "rgba(0,0,0,0.25)"),
        "offsetX": str(shadow_dict.get("x", "0")) + "px",
        "offsetY": str(shadow_dict.get("y", "0")) + "px",
        "blur": str(shadow_dict.get("blur", "0")) + "px",
        "spread": str(shadow_dict.get("spread", "0")) + "px",
    }


def _get_semantic_color_overrides() -> dict:
    """Build color hex->semantic name map.

    v3.2: Color classifier is the PRIMARY naming authority (deterministic, reproducible).
    AURORA is a SECONDARY enhancer β€” it can only ADD semantic role names
    (brand.primary, text.secondary, etc.) but cannot override palette names.

    Authority chain:
      1. Color classifier (rule-based, covers ALL colors)
      2. AURORA naming_map (LLM, only brand/text/bg/border/feedback roles accepted)
      3. Normalizer suggested_name (fallback)
    """
    overrides = {}  # hex -> semantic_name

    # PRIMARY: Color classifier (deterministic, covers ALL colors)
    classified = getattr(state, 'color_classification', None)
    if classified and hasattr(classified, 'colors'):
        for cc in classified.colors:
            hex_clean = cc.hex.strip().lower()
            if hex_clean.startswith('#') and cc.token_name:
                overrides[hex_clean] = cc.token_name

    # SECONDARY: AURORA naming_map β€” ONLY accept semantic role upgrades
    # AURORA can promote "color.blue.500" to "color.brand.primary"
    # but cannot rename palette colors to different palette names
    _SEMANTIC_ROLES = {'brand.', 'text.', 'bg.', 'border.', 'feedback.'}
    brand_result = getattr(state, 'brand_result', None)
    if brand_result:
        naming_map = getattr(brand_result, 'naming_map', None)
        if isinstance(naming_map, dict) and naming_map:
            for hex_val, name in naming_map.items():
                hex_clean = str(hex_val).strip().lower()
                if not hex_clean.startswith('#') or not name:
                    continue
                clean_name = name if name.startswith('color.') else f'color.{name}'
                # Only accept semantic role names from AURORA
                name_after_color = clean_name[6:]  # strip "color."
                is_semantic_role = any(name_after_color.startswith(r) for r in _SEMANTIC_ROLES)
                if is_semantic_role:
                    overrides[hex_clean] = clean_name

    return overrides


def _is_valid_hex_color(value: str) -> bool:
    """Validate that a string is a proper hex color (not CSS garbage)."""
    import re
    if not value or not isinstance(value, str):
        return False
    # Must be exactly #RGB, #RRGGBB, or #RRGGBBAA
    clean = value.strip().lower()
    return bool(re.match(r'^#([a-f0-9]{3}|[a-f0-9]{6}|[a-f0-9]{8})$', clean))


def _generate_color_name_from_hex(hex_val: str, used_names: set = None) -> str:
    """DEPRECATED: Use normalizer._generate_preliminary_name() instead.

    Kept as thin wrapper for backward compatibility.
    Delegates to normalizer's naming logic via color_utils.categorize_color().
    """
    from core.color_utils import categorize_color, parse_color
    import colorsys

    used_names = used_names or set()

    hex_clean = hex_val.lstrip('#').lower()
    if len(hex_clean) == 3:
        hex_clean = ''.join([c * 2 for c in hex_clean])

    try:
        r = int(hex_clean[0:2], 16) / 255
        g = int(hex_clean[2:4], 16) / 255
        b = int(hex_clean[4:6], 16) / 255
    except (ValueError, IndexError):
        return "color.other.500"

    h, l, s = colorsys.rgb_to_hls(r, g, b)
    color_family = categorize_color(hex_val) or "neutral"

    # Numeric shade from lightness (matches normalizer._generate_preliminary_name)
    if l >= 0.95: shade = "50"
    elif l >= 0.85: shade = "100"
    elif l >= 0.75: shade = "200"
    elif l >= 0.65: shade = "300"
    elif l >= 0.50: shade = "400"
    elif l >= 0.40: shade = "500"
    elif l >= 0.30: shade = "600"
    elif l >= 0.20: shade = "700"
    elif l >= 0.10: shade = "800"
    else: shade = "900"

    base_name = f"color.{color_family}.{shade}"
    final_name = base_name
    suffix = 1
    while final_name in used_names:
        suffix += 1
        final_name = f"{base_name}_{suffix}"
    return final_name


def _consolidate_colors(colors_dict: dict, overrides: dict, max_colors: int = 30) -> dict:
    """Consolidate colors: semantic first, then top by frequency, capped."""
    if not colors_dict:
        return {}

    result = {}
    remaining = []
    used_generated_names = set()  # Track generated names to avoid conflicts

    for name, c in colors_dict.items():
        hex_val = c.value.lower() if hasattr(c, 'value') else str(c.get('value', '')).lower()

        # IMPORTANT: Skip invalid/garbage color values (CSS parsing errors)
        if not _is_valid_hex_color(hex_val):
            continue

        freq = c.frequency if hasattr(c, 'frequency') else c.get('frequency', 0)

        # Check if this color has a semantic override
        semantic_name = overrides.get(hex_val)
        if semantic_name:
            result[semantic_name] = {
                "value": hex_val,
                "type": "color",
                "source": "semantic",
            }
        else:
            # Check for garbage names (firecrawl.N, numeric-only, etc.)
            base_name = (c.suggested_name if hasattr(c, 'suggested_name') else name) or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").lower()

            # Detect garbage names and generate proper color-based names
            is_garbage_name = (
                'firecrawl' in clean_name.lower() or
                clean_name.split('.')[-1].isdigit() or  # Ends with just a number
                len(clean_name.split('.')) == 2 and clean_name.split('.')[-1].isdigit()  # color.34
            )

            if is_garbage_name:
                # Generate proper name based on color characteristics
                clean_name = _generate_color_name_from_hex(hex_val, used_generated_names)
                used_generated_names.add(clean_name)
            elif not clean_name.startswith("color."):
                clean_name = f"color.{clean_name}"

            remaining.append((clean_name, hex_val, freq))

    # Sort remaining by frequency (highest first), take up to max
    remaining.sort(key=lambda x: -x[2])
    slots_left = max_colors - len(result)
    for clean_name, hex_val, freq in remaining[:slots_left]:
        if clean_name not in result:
            result[clean_name] = {
                "value": hex_val,
                "type": "color",
                "source": "detected",
            }

    return result


def preview_color_classification(convention: str = "semantic"):
    """Preview color classification before export. 100% rule-based, no LLM."""
    from core.color_classifier import classify_colors, generate_classification_preview

    if not state.desktop_normalized or not state.desktop_normalized.colors:
        return "⚠️ No colors extracted yet. Run Stage 1 extraction first."

    result = classify_colors(
        state.desktop_normalized.colors,
        convention=convention or "semantic",
        log_callback=state.log,
    )

    # Store for use by export functions
    state.color_classification = result

    preview = generate_classification_preview(result)

    # Also append the decision log
    log_section = "\n\nπŸ“‹ DECISION LOG:\n" + "\n".join(result.log)

    return preview + log_section


def export_stage1_json(convention: str = "semantic"):
    """Export Stage 1 tokens (as-is extraction) to W3C DTCG format."""
    if not state.desktop_normalized:
        gr.Warning("No tokens extracted yet. Complete Stage 1 extraction first.")
        return json.dumps({
            "error": "No tokens extracted yet.",
            "how_to_fix": "Go to Step 1, enter a URL, discover pages, and extract tokens first.",
            "stage": "Stage 1 required"
        }, indent=2)

    # W3C DTCG format: nested structure, no wrapper, $value/$type
    result = {}
    token_count = 0

    # =========================================================================
    # COLORS β€” Nested structure with $value, $type, $description
    # =========================================================================
    if state.desktop_normalized and state.desktop_normalized.colors:
        from core.color_classifier import classify_colors
        classification = classify_colors(
            state.desktop_normalized.colors,
            convention=convention or "semantic",
            log_callback=state.log,
        )
        for c in classification.colors:
            ext = {"frequency": c.frequency, "confidence": c.confidence, "category": c.category}
            if c.evidence:
                ext["evidence"] = c.evidence[:3]  # Top 3 evidence items
            dtcg_token = _to_dtcg_token(
                c.hex, "color",
                description=f"{c.category}: {c.role}",
                extensions=ext,
            )
            _flat_key_to_nested(c.token_name, dtcg_token, result)
            token_count += 1

    # =========================================================================
    # TYPOGRAPHY β€” Nested structure with viewport suffix
    # =========================================================================
    # Desktop typography
    if state.desktop_normalized and state.desktop_normalized.typography:
        for name, t in state.desktop_normalized.typography.items():
            base_name = t.suggested_name or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
            if not clean_name.startswith("font."):
                clean_name = f"font.{clean_name}"

            flat_key = f"{clean_name}.desktop"
            typo_value = {
                "fontFamily": t.font_family,
                "fontSize": t.font_size,
                "fontWeight": str(t.font_weight),
                "lineHeight": t.line_height or "1.5",
            }
            dtcg_token = _to_dtcg_token(typo_value, "typography", description="Extracted from site")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    # Mobile typography
    if state.mobile_normalized and state.mobile_normalized.typography:
        for name, t in state.mobile_normalized.typography.items():
            base_name = t.suggested_name or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
            if not clean_name.startswith("font."):
                clean_name = f"font.{clean_name}"

            flat_key = f"{clean_name}.mobile"
            typo_value = {
                "fontFamily": t.font_family,
                "fontSize": t.font_size,
                "fontWeight": str(t.font_weight),
                "lineHeight": t.line_height or "1.5",
            }
            dtcg_token = _to_dtcg_token(typo_value, "typography", description="Extracted from site")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    # =========================================================================
    # SPACING β€” Nested structure with viewport suffix
    # =========================================================================
    # Desktop spacing
    if state.desktop_normalized and state.desktop_normalized.spacing:
        for name, s in state.desktop_normalized.spacing.items():
            base_name = s.suggested_name or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
            if not clean_name.startswith("space."):
                clean_name = f"space.{clean_name}"

            flat_key = f"{clean_name}.desktop"
            dtcg_token = _to_dtcg_token(s.value, "dimension", description="Extracted from site")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    # Mobile spacing
    if state.mobile_normalized and state.mobile_normalized.spacing:
        for name, s in state.mobile_normalized.spacing.items():
            base_name = s.suggested_name or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
            if not clean_name.startswith("space."):
                clean_name = f"space.{clean_name}"

            flat_key = f"{clean_name}.mobile"
            dtcg_token = _to_dtcg_token(s.value, "dimension", description="Extracted from site")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    # =========================================================================
    # BORDER RADIUS β€” W3C DTCG "dimension" type
    # =========================================================================
    if state.desktop_normalized and state.desktop_normalized.radius:
        seen_radius = {}
        for name, r in state.desktop_normalized.radius.items():
            token_name = _get_radius_token_name(r.value, seen_radius)
            if token_name is None:
                continue  # Duplicate radius β€” skip
            flat_key = token_name
            ext = {"frequency": r.frequency}
            if hasattr(r, 'fits_base_4') and r.fits_base_4 is not None:
                ext["fitsBase4"] = r.fits_base_4
            if hasattr(r, 'fits_base_8') and r.fits_base_8 is not None:
                ext["fitsBase8"] = r.fits_base_8
            dtcg_token = _to_dtcg_token(r.value, "dimension",
                                         description=f"Border radius ({name})",
                                         extensions=ext)
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    # =========================================================================
    # SHADOWS β€” W3C DTCG shadow format
    # =========================================================================
    if state.desktop_normalized and state.desktop_normalized.shadows:
        shadow_tier_names = ["xs", "sm", "md", "lg", "xl", "2xl"]
        sorted_shadows = sorted(
            state.desktop_normalized.shadows.items(),
            key=lambda x: _get_shadow_blur(x[1].value),
        )
        for i, (name, s) in enumerate(sorted_shadows):
            size_name = shadow_tier_names[i] if i < len(shadow_tier_names) else str(i + 1)
            flat_key = f"shadow.{size_name}"
            parsed = _parse_shadow_to_tokens_studio(s.value)
            dtcg_shadow_value = _shadow_to_dtcg(parsed)
            ext = {"frequency": s.frequency, "rawCSS": s.value}
            if hasattr(s, 'blur_px') and s.blur_px is not None:
                ext["blurPx"] = s.blur_px
            dtcg_token = _to_dtcg_token(dtcg_shadow_value, "shadow",
                                         description=f"Elevation {size_name}",
                                         extensions=ext)
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    json_str = json.dumps(result, indent=2, default=str)
    gr.Info(f"Stage 1 exported: {token_count} tokens (W3C DTCG format)")
    return json_str


def export_tokens_json(convention: str = "semantic"):
    """Export final tokens with selected upgrades applied - FLAT structure for Figma Tokens Studio."""
    if not state.desktop_normalized:
        gr.Warning("No tokens extracted yet. Complete Stage 1 extraction first.")
        return json.dumps({
            "error": "No tokens extracted yet.",
            "how_to_fix": "Complete Stage 1 extraction first, then optionally run Stage 2 analysis before exporting.",
            "stage": "Stage 1 required"
        }, indent=2)

    # Get selected upgrades
    upgrades = getattr(state, 'selected_upgrades', {})
    if not upgrades:
        state.log("⚠️ Exporting final JSON without Stage 2 upgrades applied. Consider running Stage 2 analysis first.")
    type_scale_choice = upgrades.get('type_scale', 'Keep Current')
    spacing_choice = upgrades.get('spacing', 'Keep Current')
    apply_ramps = upgrades.get('color_ramps', True)
    
    # Determine ratio from choice
    ratio = None
    if "1.2" in type_scale_choice:
        ratio = 1.2
    elif "1.25" in type_scale_choice:
        ratio = 1.25
    elif "1.333" in type_scale_choice:
        ratio = 1.333
    
    # Determine spacing base
    spacing_base = None
    if "8px" in spacing_choice:
        spacing_base = 8
    elif "4px" in spacing_choice:
        spacing_base = 4
    
    # W3C DTCG format: nested structure, no wrapper
    result = {}
    token_count = 0

    fonts_info = get_detected_fonts()
    primary_font = fonts_info.get("primary", "sans-serif")
    
    # =========================================================================
    # COLORS β€” Rule-based classification + optional ramps
    # =========================================================================
    if state.desktop_normalized and state.desktop_normalized.colors:
        from core.color_utils import generate_color_ramp
        from core.color_classifier import classify_colors

        classification = classify_colors(
            state.desktop_normalized.colors,
            convention=convention or "semantic",
            log_callback=state.log,
        )

        # Semantic categories get light/dark variants; palette gets full 50-900 ramps
        _SEMANTIC_CATS = {"brand", "text", "bg", "border", "feedback"}
        _PALETTE_SHADES = ["50", "100", "200", "300", "400", "500", "600", "700", "800", "900"]
        _SEMANTIC_VARIANT_SHADES = ["50", "200", "800", "950"]

        # Track which palette hue families already have ramps (avoid duplicates)
        _palette_hues_with_ramps = set()

        for c in classification.colors:
            flat_key = c.token_name
            is_semantic = c.category in _SEMANTIC_CATS

            if apply_ramps and not is_semantic:
                # PALETTE colors: full 50-900 ramp under hue family
                # token_name = "color.blue.700" β†’ base = "color.blue" (strip shade)
                parts = flat_key.rsplit(".", 1)
                hue_base = parts[0] if len(parts) > 1 else flat_key

                # Only one ramp per hue family (first/most-used color wins)
                if hue_base in _palette_hues_with_ramps:
                    continue
                _palette_hues_with_ramps.add(hue_base)

                try:
                    ramp = generate_color_ramp(c.hex)
                    for shade in _PALETTE_SHADES:
                        shade_hex = ramp.get(shade)
                        if shade_hex:
                            shade_key = f"{hue_base}.{shade}"
                            dtcg_token = _to_dtcg_token(shade_hex, "color")
                            _flat_key_to_nested(shade_key, dtcg_token, result)
                            token_count += 1
                except (ValueError, KeyError, TypeError, IndexError):
                    dtcg_token = _to_dtcg_token(c.hex, "color")
                    _flat_key_to_nested(flat_key, dtcg_token, result)
                    token_count += 1

            elif apply_ramps and is_semantic:
                # SEMANTIC colors: base + tint/shade variants
                # Build as a namespace dict (not sequential leaf calls)
                # so that base and variants coexist without nesting conflict
                try:
                    ramp = generate_color_ramp(c.hex)
                    # Emit base as "DEFAULT" inside a namespace
                    default_key = f"{flat_key}.DEFAULT"
                    dtcg_token = _to_dtcg_token(c.hex, "color")
                    _flat_key_to_nested(default_key, dtcg_token, result)
                    token_count += 1
                    for variant_shade in _SEMANTIC_VARIANT_SHADES:
                        variant_hex = ramp.get(variant_shade)
                        if variant_hex:
                            variant_key = f"{flat_key}.{variant_shade}"
                            dtcg_token = _to_dtcg_token(variant_hex, "color")
                            _flat_key_to_nested(variant_key, dtcg_token, result)
                            token_count += 1
                except (ValueError, KeyError, TypeError, IndexError):
                    # Fallback: just base color
                    dtcg_token = _to_dtcg_token(c.hex, "color")
                    _flat_key_to_nested(flat_key, dtcg_token, result)
                    token_count += 1

            else:
                # No ramps β€” base color only
                dtcg_token = _to_dtcg_token(c.hex, "color")
                _flat_key_to_nested(flat_key, dtcg_token, result)
                token_count += 1
    
    # =========================================================================
    # TYPOGRAPHY - FLAT structure with viewport suffix
    # =========================================================================
    base_size = get_base_font_size()
    token_names = [
        "font.display.2xl", "font.display.xl", "font.display.lg", "font.display.md",
        "font.heading.xl", "font.heading.lg", "font.heading.md", "font.heading.sm",
        "font.body.lg", "font.body.md", "font.body.sm", "font.caption", "font.overline"
    ]
    
    # Weight + lineHeight mapping by token tier
    _weight_map = {
        "display": "700", "heading": "600",
        "body": "400", "caption": "400", "overline": "500",
    }
    _lh_map = {
        "display": "1.2", "heading": "1.3",
        "body": "1.5", "caption": "1.4", "overline": "1.2",
    }

    def _tier_from_token(token_name: str) -> str:
        """Extract tier (display/heading/body/caption/overline) from token name."""
        for tier in ("display", "heading", "body", "caption", "overline"):
            if tier in token_name:
                return tier
        return "body"

    # Desktop typography β€” W3C DTCG format
    MIN_FONT_SIZE_DESKTOP = 10  # Floor: no text style below 10px
    MIN_FONT_SIZE_MOBILE = 10   # Floor: same for mobile
    if ratio:
        scales = [max(MIN_FONT_SIZE_DESKTOP, int(round(base_size * (ratio ** (8-i)) / 2) * 2)) for i in range(13)]
        for i, token_name in enumerate(token_names):
            tier = _tier_from_token(token_name)
            flat_key = f"{token_name}.desktop"
            typo_value = {
                "fontFamily": primary_font,
                "fontSize": f"{scales[i]}px",
                "fontWeight": _weight_map.get(tier, "400"),
                "lineHeight": _lh_map.get(tier, "1.5"),
            }
            dtcg_token = _to_dtcg_token(typo_value, "typography")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1
    elif state.desktop_normalized and state.desktop_normalized.typography:
        for name, t in state.desktop_normalized.typography.items():
            base_name = t.suggested_name or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
            if not clean_name.startswith("font."):
                clean_name = f"font.{clean_name}"

            flat_key = f"{clean_name}.desktop"
            typo_value = {
                "fontFamily": t.font_family,
                "fontSize": t.font_size,
                "fontWeight": str(t.font_weight),
                "lineHeight": t.line_height or "1.5",
            }
            dtcg_token = _to_dtcg_token(typo_value, "typography")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1

    # Mobile typography β€” W3C DTCG format
    if ratio:
        mobile_factor = 0.875
        scales = [max(MIN_FONT_SIZE_MOBILE, int(round(base_size * mobile_factor * (ratio ** (8-i)) / 2) * 2)) for i in range(13)]
        for i, token_name in enumerate(token_names):
            tier = _tier_from_token(token_name)
            flat_key = f"{token_name}.mobile"
            typo_value = {
                "fontFamily": primary_font,
                "fontSize": f"{scales[i]}px",
                "fontWeight": _weight_map.get(tier, "400"),
                "lineHeight": _lh_map.get(tier, "1.5"),
            }
            dtcg_token = _to_dtcg_token(typo_value, "typography")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1
    elif state.mobile_normalized and state.mobile_normalized.typography:
        for name, t in state.mobile_normalized.typography.items():
            base_name = t.suggested_name or name
            clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
            if not clean_name.startswith("font."):
                clean_name = f"font.{clean_name}"

            flat_key = f"{clean_name}.mobile"
            typo_value = {
                "fontFamily": t.font_family,
                "fontSize": t.font_size,
                "fontWeight": str(t.font_weight),
                "lineHeight": t.line_height or "1.5",
            }
            dtcg_token = _to_dtcg_token(typo_value, "typography")
            _flat_key_to_nested(flat_key, dtcg_token, result)
            token_count += 1
    
    # =========================================================================
    # SPACING β€” W3C DTCG format with nested structure
    # =========================================================================
    spacing_token_names = [
        "space.1", "space.2", "space.3", "space.4", "space.5",
        "space.6", "space.8", "space.10", "space.12", "space.16"
    ]

    if spacing_base:
        # Generate grid-aligned spacing for both viewports
        for i, token_name in enumerate(spacing_token_names):
            value = spacing_base * (i + 1)

            # Desktop
            desktop_key = f"{token_name}.desktop"
            dtcg_token = _to_dtcg_token(f"{value}px", "dimension")
            _flat_key_to_nested(desktop_key, dtcg_token, result)
            token_count += 1

            # Mobile (same values)
            mobile_key = f"{token_name}.mobile"
            dtcg_token = _to_dtcg_token(f"{value}px", "dimension")
            _flat_key_to_nested(mobile_key, dtcg_token, result)
            token_count += 1
    else:
        # Keep original with nested structure
        if state.desktop_normalized and state.desktop_normalized.spacing:
            for name, s in state.desktop_normalized.spacing.items():
                base_name = s.suggested_name or name
                clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
                if not clean_name.startswith("space."):
                    clean_name = f"space.{clean_name}"

                desktop_key = f"{clean_name}.desktop"
                dtcg_token = _to_dtcg_token(s.value, "dimension")
                _flat_key_to_nested(desktop_key, dtcg_token, result)
                token_count += 1

        if state.mobile_normalized and state.mobile_normalized.spacing:
            for name, s in state.mobile_normalized.spacing.items():
                base_name = s.suggested_name or name
                clean_name = base_name.replace(" ", ".").replace("_", ".").replace("-", ".").lower()
                if not clean_name.startswith("space."):
                    clean_name = f"space.{clean_name}"

                mobile_key = f"{clean_name}.mobile"
                dtcg_token = _to_dtcg_token(s.value, "dimension")
                _flat_key_to_nested(mobile_key, dtcg_token, result)
                token_count += 1

    # =========================================================================
    # BORDER RADIUS β€” W3C DTCG format (uses "dimension" type per spec)
    # =========================================================================
    if state.desktop_normalized and state.desktop_normalized.radius:
        seen_radius = {}
        for name, r in state.desktop_normalized.radius.items():
            token_name = _get_radius_token_name(r.value, seen_radius)
            if token_name is None:
                continue  # Duplicate radius β€” skip
            dtcg_token = _to_dtcg_token(r.value, "dimension")
            _flat_key_to_nested(token_name, dtcg_token, result)
            token_count += 1

    # =========================================================================
    # SHADOWS — W3C DTCG format — always produce 5 elevation levels (xs→xl)
    # Interpolates between extracted shadows to fill missing levels.
    # =========================================================================
    TARGET_SHADOW_COUNT = 5
    shadow_names = ["shadow.xs", "shadow.sm", "shadow.md", "shadow.lg", "shadow.xl"]

    if state.desktop_normalized and state.desktop_normalized.shadows:
        sorted_shadows = sorted(
            state.desktop_normalized.shadows.items(),
            key=lambda x: _get_shadow_blur(x[1].value),
        )

        # Parse all extracted shadows into numeric components
        parsed_shadows = []
        for name, s in sorted_shadows:
            p = _parse_shadow_to_tokens_studio(s.value)
            parsed_shadows.append({
                "x": float(p.get("x", 0)),
                "y": float(p.get("y", 0)),
                "blur": float(p.get("blur", 0)),
                "spread": float(p.get("spread", 0)),
                "color": p.get("color", "rgba(0,0,0,0.25)"),
            })

        # Interpolation helpers
        def _lerp(a, b, t):
            return a + (b - a) * t

        def _lerp_shadow(s1, s2, t):
            """Interpolate between two shadow dicts at factor t (0.0=s1, 1.0=s2)."""
            import re
            interp = {
                "x": round(_lerp(s1["x"], s2["x"], t), 1),
                "y": round(_lerp(s1["y"], s2["y"], t), 1),
                "blur": round(_lerp(s1["blur"], s2["blur"], t), 1),
                "spread": round(_lerp(s1["spread"], s2["spread"], t), 1),
            }
            alpha1, alpha2 = 0.25, 0.25
            m1 = re.search(r'rgba?\([^)]*,\s*([\d.]+)\)', s1["color"])
            m2 = re.search(r'rgba?\([^)]*,\s*([\d.]+)\)', s2["color"])
            if m1: alpha1 = float(m1.group(1))
            if m2: alpha2 = float(m2.group(1))
            interp_alpha = round(_lerp(alpha1, alpha2, t), 3)
            interp["color"] = f"rgba(0, 0, 0, {interp_alpha})"
            return interp

        final_shadows = []
        n = len(parsed_shadows)
        if n >= TARGET_SHADOW_COUNT:
            final_shadows = parsed_shadows[:TARGET_SHADOW_COUNT]
        elif n == 1:
            base = parsed_shadows[0]
            for i in range(TARGET_SHADOW_COUNT):
                factor = (i + 1) / 3.0
                final_shadows.append({
                    "x": round(base["x"] * factor, 1),
                    "y": round(max(1, base["y"] * factor), 1),
                    "blur": round(max(1, base["blur"] * factor), 1),
                    "spread": round(base["spread"] * factor, 1),
                    "color": f"rgba(0, 0, 0, {round(0.04 + i * 0.04, 3)})",
                })
        elif n >= 2:
            for i in range(TARGET_SHADOW_COUNT):
                t = i / (TARGET_SHADOW_COUNT - 1)
                src_pos = t * (n - 1)
                lo = int(src_pos)
                hi = min(lo + 1, n - 1)
                frac = src_pos - lo
                final_shadows.append(_lerp_shadow(parsed_shadows[lo], parsed_shadows[hi], frac))

        for i, shadow in enumerate(final_shadows):
            token_name = shadow_names[i] if i < len(shadow_names) else f"shadow.{i + 1}"
            dtcg_value = {
                "color": shadow["color"],
                "offsetX": f"{shadow['x']}px",
                "offsetY": f"{shadow['y']}px",
                "blur": f"{shadow['blur']}px",
                "spread": f"{shadow['spread']}px",
            }
            dtcg_token = _to_dtcg_token(dtcg_value, "shadow")
            _flat_key_to_nested(token_name, dtcg_token, result)
            token_count += 1

    json_str = json.dumps(result, indent=2, default=str)
    upgrades_note = " (with upgrades)" if upgrades else " (no upgrades applied)"
    gr.Info(f"Final export: {token_count} tokens{upgrades_note}")
    return json_str


# =============================================================================
# UI HELPERS
# =============================================================================

def _render_stepper(active: int = 1, completed: list = None) -> str:
    """Render the horizontal progress stepper HTML.

    Args:
        active: Current active step (1-5)
        completed: List of completed step numbers
    """
    if completed is None:
        completed = []

    steps = [
        ("1", "Discover"),
        ("2", "Extract"),
        ("3", "Analyze"),
        ("4", "Review"),
        ("5", "Export"),
    ]

    parts = []
    for i, (num, label) in enumerate(steps):
        step_num = i + 1
        if step_num in completed:
            cls = "completed"
            icon = "βœ“"
        elif step_num == active:
            cls = "active"
            icon = num
        else:
            cls = ""
            icon = num

        parts.append(f'<div class="step-item {cls}"><span class="step-num">{icon}</span>{label}</div>')

        if i < len(steps) - 1:
            conn_cls = "done" if step_num in completed else ""
            parts.append(f'<div class="step-connector {conn_cls}"></div>')

    return f'<div class="progress-stepper">{"".join(parts)}</div>'


def _render_benchmark_cards(benchmark_comparisons, benchmark_advice) -> str:
    """Render visual benchmark cards with progress bars instead of markdown table."""
    if not benchmark_comparisons:
        return "<div class='placeholder-msg'>No benchmark comparison available</div>"

    medals = ["πŸ₯‡", "πŸ₯ˆ", "πŸ₯‰"]
    cards_html = []

    # Recommendation banner
    rec_html = ""
    if benchmark_advice and benchmark_advice.recommended_benchmark_name:
        rec_html = f"""
        <div style="background: #f5f3ff; border: 1px solid #c4b5fd; border-radius: 8px;
                    padding: 14px 18px; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <span style="font-size: 20px;">πŸ†</span>
            <div>
                <div style="font-weight: 600; color: #4C1D95; font-size: 14px;">
                    Recommended: {benchmark_advice.recommended_benchmark_name}
                </div>
                <div style="font-size: 12px; color: #6D28D9; margin-top: 2px;">
                    {benchmark_advice.reasoning or ''}
                </div>
            </div>
        </div>"""

    for i, c in enumerate(benchmark_comparisons[:5]):
        b = c.benchmark
        medal = medals[i] if i < 3 else f"#{i+1}"

        def bar_color(pct):
            if pct >= 80: return "#10b981"
            elif pct >= 50: return "#f59e0b"
            else: return "#ef4444"

        categories = [
            ("Type", c.type_match_pct),
            ("Spacing", c.spacing_match_pct),
            ("Colors", c.color_match_pct),
            ("Radius", c.radius_match_pct),
            ("Shadows", c.shadow_match_pct),
        ]

        bars = ""
        for cat_name, pct in categories:
            color = bar_color(pct)
            bars += f"""
            <div class="bm-bar-row">
                <div class="bm-bar-label">{cat_name}</div>
                <div class="bm-bar-track">
                    <div class="bm-bar-fill" style="width: {pct:.0f}%; background: {color};"></div>
                </div>
                <div class="bm-bar-value" style="color: {color};">{pct:.0f}%</div>
            </div>"""

        cards_html.append(f"""
        <div class="bm-card">
            <div class="bm-card-header">
                <div style="display: flex; align-items: center;">
                    <span class="bm-medal">{medal}</span>
                    <span class="bm-card-name">{b.icon} {b.short_name}</span>
                </div>
                <div class="bm-card-pct">{c.overall_match_pct:.0f}%</div>
            </div>
            {bars}
        </div>""")

    # Alignment changes
    changes_html = ""
    if benchmark_advice and benchmark_advice.alignment_changes:
        items = []
        for change in benchmark_advice.alignment_changes[:4]:
            token_type = change.get('token_type', '')
            icon = {"typography": "πŸ“", "spacing": "πŸ“", "colors": "🎨", "radius": "πŸ”˜", "shadows": "πŸŒ—"}.get(token_type, "πŸ”§")
            items.append(f"<li>{icon} <strong>{change.get('change', '?')}</strong>: {change.get('from', '?')} β†’ {change.get('to', '?')}</li>")
        changes_html = f"""
        <div style="margin-top: 16px; padding: 14px 18px; background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 8px;">
            <div style="font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #1e293b;">πŸ”§ To Align with Top Match</div>
            <ul style="margin: 0; padding-left: 20px; font-size: 13px; color: #1a1a1a; line-height: 1.8;">
                {''.join(items)}
            </ul>
        </div>"""

    return f"""
    {rec_html}
    <div class="bm-card-grid">
        {''.join(cards_html)}
    </div>
    {changes_html}
    """


def _render_as_is_to_be(category: str, as_is_value: str, as_is_detail: str,
                         to_be_value: str, to_be_detail: str, icon: str = "πŸ“") -> str:
    """Render an As-Is β†’ To-Be comparison card for a token category."""
    return f"""
    <div class="comparison-grid">
        <div class="comparison-card as-is">
            <div class="comparison-label as-is-label">{icon} {category} β€” AS-IS</div>
            <div class="comparison-value">{as_is_value}</div>
            <div class="comparison-detail">{as_is_detail}</div>
        </div>
        <div class="comparison-arrow">β†’</div>
        <div class="comparison-card to-be">
            <div class="comparison-label to-be-label">{icon} {category} β€” TO-BE</div>
            <div class="comparison-value">{to_be_value}</div>
            <div class="comparison-detail">{to_be_detail}</div>
        </div>
    </div>
    """


# =============================================================================
# UI BUILDING
# =============================================================================

def create_ui():
    """Create the Gradio interface with corporate branding."""
    
    # Corporate theme β€” Deep Violet accent (distinctive, not blue)
    corporate_theme = gr.themes.Base(
        primary_hue=gr.themes.colors.violet,
        secondary_hue=gr.themes.colors.slate,
        neutral_hue=gr.themes.colors.slate,
        font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
        font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "ui-monospace", "monospace"],
    ).set(
        # Colors
        body_background_fill="#f8fafc",
        body_background_fill_dark="#0f172a",
        block_background_fill="white",
        block_background_fill_dark="#1e293b",
        block_border_color="#e2e8f0",
        block_border_color_dark="#334155",
        block_label_background_fill="#f5f3ff",
        block_label_background_fill_dark="#1e293b",
        block_title_text_color="#0f172a",
        block_title_text_color_dark="#f1f5f9",

        # Primary button β€” Deep Violet
        button_primary_background_fill="#6D28D9",
        button_primary_background_fill_hover="#5B21B6",
        button_primary_text_color="white",

        # Secondary button
        button_secondary_background_fill="#f5f3ff",
        button_secondary_background_fill_hover="#ede9fe",
        button_secondary_text_color="#1e293b",

        # Input fields
        input_background_fill="#ffffff",
        input_background_fill_dark="#1e293b",
        input_border_color="#cbd5e1",
        input_border_color_dark="#475569",

        # Shadows and radius
        block_shadow="0 1px 3px rgba(0,0,0,0.1)",
        block_shadow_dark="0 1px 3px rgba(0,0,0,0.3)",
        block_border_width="1px",
        block_radius="8px",

        # Text
        body_text_color="#1e293b",
        body_text_color_dark="#e2e8f0",
        body_text_size="14px",
    )
    
    # Custom CSS β€” Deep Violet theme + Progress Stepper + Always-visible log
    custom_css = """
    /* ═══════════════════════════════════════════════════════════════
       GLOBAL
       ═══════════════════════════════════════════════════════════════ */
    .gradio-container {
        max-width: 1400px !important;
        margin: 0 auto !important;
    }

    /* ═══════════════════════════════════════════════════════════════
       HEADER β€” Deep Violet gradient
       ═══════════════════════════════════════════════════════════════ */
    .app-header {
        background: linear-gradient(135deg, #4C1D95 0%, #7C3AED 50%, #8B5CF6 100%);
        padding: 28px 32px;
        border-radius: 12px;
        margin-bottom: 8px;
        color: white;
        position: relative;
        overflow: hidden;
    }
    .app-header::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -30%;
        width: 60%;
        height: 200%;
        background: radial-gradient(ellipse, rgba(255,255,255,0.08) 0%, transparent 70%);
        pointer-events: none;
    }
    .app-header h1 {
        margin: 0 0 6px 0;
        font-size: 26px;
        font-weight: 700;
        letter-spacing: -0.3px;
    }
    .app-header p {
        margin: 0;
        opacity: 0.85;
        font-size: 13px;
    }

    /* ═══════════════════════════════════════════════════════════════
       PROGRESS STEPPER β€” horizontal workflow indicator
       ═══════════════════════════════════════════════════════════════ */
    .progress-stepper {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 16px 20px;
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 10px;
        margin-bottom: 20px;
        gap: 0;
    }
    .step-item {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 6px 14px;
        border-radius: 20px;
        font-size: 13px;
        font-weight: 500;
        color: #94a3b8;
        transition: all 0.3s ease;
        white-space: nowrap;
    }
    .step-item.active {
        background: #f5f3ff;
        color: #6D28D9;
        font-weight: 600;
    }
    .step-item.completed {
        color: #10b981;
        font-weight: 500;
    }
    .step-num {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px;
        font-weight: 700;
        background: #e2e8f0;
        color: #94a3b8;
        flex-shrink: 0;
    }
    .step-item.active .step-num {
        background: #6D28D9;
        color: white;
    }
    .step-item.completed .step-num {
        background: #10b981;
        color: white;
    }
    .step-connector {
        width: 32px;
        height: 2px;
        background: #e2e8f0;
        flex-shrink: 0;
    }
    .step-connector.done {
        background: #10b981;
    }

    /* ═══════════════════════════════════════════════════════════════
       STAGE HEADERS β€” Violet accent
       ═══════════════════════════════════════════════════════════════ */
    .stage-header {
        background: linear-gradient(90deg, #f5f3ff 0%, #ffffff 100%);
        padding: 16px 20px;
        border-radius: 8px;
        border-left: 4px solid #6D28D9;
        margin-bottom: 16px;
    }
    .stage-header h2 {
        margin: 0;
        font-size: 18px;
        color: #1e293b;
    }

    /* ═══════════════════════════════════════════════════════════════
       LOG β€” Always visible during loading (z-index trick)
       ═══════════════════════════════════════════════════════════════ */
    .log-container textarea {
        font-family: 'JetBrains Mono', monospace !important;
        font-size: 12px !important;
        line-height: 1.6 !important;
        background: #0f172a !important;
        color: #e2e8f0 !important;
        border-radius: 8px !important;
    }
    /* Keep log visible above Gradio loading overlay */
    .always-visible-log {
        position: relative;
        z-index: 1001;
    }
    .always-visible-log .wrap {
        opacity: 1 !important;
    }

    /* ═══════════════════════════════════════════════════════════════
       COLOR SWATCH
       ═══════════════════════════════════════════════════════════════ */
    .color-swatch {
        display: inline-block;
        width: 24px;
        height: 24px;
        border-radius: 4px;
        margin-right: 8px;
        vertical-align: middle;
        border: 1px solid rgba(0,0,0,0.1);
    }

    /* ═══════════════════════════════════════════════════════════════
       SCORE BADGES
       ═══════════════════════════════════════════════════════════════ */
    .score-badge {
        display: inline-block;
        padding: 4px 12px;
        border-radius: 20px;
        font-weight: 600;
        font-size: 13px;
    }
    .score-badge.high { background: #dcfce7; color: #166534; }
    .score-badge.medium { background: #fef3c7; color: #92400e; }
    .score-badge.low { background: #fee2e2; color: #991b1b; }

    /* ═══════════════════════════════════════════════════════════════
       BENCHMARK CARDS β€” Visual cards with progress bars
       ═══════════════════════════════════════════════════════════════ */
    .benchmark-card {
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        padding: 16px;
        margin-bottom: 12px;
    }
    .benchmark-card.selected {
        border-color: #6D28D9;
        background: #f5f3ff;
    }
    .bm-card-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
        gap: 16px;
        margin: 16px 0;
    }
    .bm-card {
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 10px;
        padding: 18px;
        transition: all 0.2s ease;
    }
    .bm-card:first-child {
        border: 2px solid #6D28D9;
        box-shadow: 0 0 0 3px rgba(109,40,217,0.1);
    }
    .bm-card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 14px;
    }
    .bm-card-name {
        font-weight: 600;
        font-size: 15px;
        color: #1e293b;
    }
    .bm-card-pct {
        font-weight: 700;
        font-size: 20px;
        color: #6D28D9;
    }
    .bm-card:first-child .bm-card-pct {
        color: #6D28D9;
    }
    .bm-bar-row {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 8px;
    }
    .bm-bar-label {
        width: 70px;
        font-size: 11px;
        color: #64748b;
        text-align: right;
        flex-shrink: 0;
    }
    .bm-bar-track {
        flex: 1;
        height: 6px;
        background: #e2e8f0;
        border-radius: 3px;
        overflow: hidden;
    }
    .bm-bar-fill {
        height: 100%;
        border-radius: 3px;
        transition: width 0.5s ease;
    }
    .bm-bar-value {
        width: 40px;
        font-size: 11px;
        color: #475569;
        font-weight: 600;
    }
    .bm-medal {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 26px;
        height: 26px;
        border-radius: 50%;
        font-size: 14px;
        margin-right: 6px;
    }

    /* ═══════════════════════════════════════════════════════════════
       AS-IS vs TO-BE COMPARISON CARDS
       ═══════════════════════════════════════════════════════════════ */
    .comparison-grid {
        display: grid;
        grid-template-columns: 1fr auto 1fr;
        gap: 0;
        margin: 16px 0;
        align-items: stretch;
    }
    .comparison-card {
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 10px;
        padding: 18px;
    }
    .comparison-card.as-is {
        border-left: 4px solid #94a3b8;
    }
    .comparison-card.to-be {
        border-left: 4px solid #6D28D9;
        background: #faf5ff;
    }
    .comparison-arrow {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 12px;
        font-size: 24px;
        color: #6D28D9;
    }
    .comparison-label {
        font-size: 11px;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 10px;
    }
    .comparison-label.as-is-label { color: #94a3b8; }
    .comparison-label.to-be-label { color: #6D28D9; }
    .comparison-value {
        font-size: 22px;
        font-weight: 700;
        color: #1e293b;
        margin-bottom: 4px;
    }
    .comparison-detail {
        font-size: 12px;
        color: #64748b;
    }

    /* ═══════════════════════════════════════════════════════════════
       ACTION ITEMS
       ═══════════════════════════════════════════════════════════════ */
    .action-item {
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        padding: 16px;
        margin-bottom: 8px;
    }
    .action-item.high-priority {
        border-left: 4px solid #ef4444;
    }
    .action-item.medium-priority {
        border-left: 4px solid #f59e0b;
    }

    /* ═══════════════════════════════════════════════════════════════
       PROGRESS BAR (generic)
       ═══════════════════════════════════════════════════════════════ */
    .progress-bar {
        height: 4px;
        background: #e2e8f0;
        border-radius: 2px;
        overflow: hidden;
    }
    .progress-bar-fill {
        height: 100%;
        background: linear-gradient(90deg, #6D28D9, #8B5CF6);
        transition: width 0.3s ease;
    }

    /* ═══════════════════════════════════════════════════════════════
       TABLES
       ═══════════════════════════════════════════════════════════════ */
    table {
        border-collapse: collapse;
        width: 100%;
    }
    th {
        background: #f5f3ff;
        color: #1e293b;
        padding: 12px;
        text-align: left;
        font-weight: 600;
        border-bottom: 2px solid #e2e8f0;
    }
    td {
        padding: 12px;
        color: #1e293b;
        border-bottom: 1px solid #e2e8f0;
    }

    /* ═══════════════════════════════════════════════════════════════
       SECTION DESCRIPTIONS
       ═══════════════════════════════════════════════════════════════ */
    .section-desc p, .section-desc {
        font-size: 13px !important;
        color: #64748b !important;
        line-height: 1.5 !important;
        margin-top: -4px !important;
        margin-bottom: 12px !important;
    }
    .dark .section-desc p, .dark .section-desc {
        color: #94a3b8 !important;
    }

    /* ═══════════════════════════════════════════════════════════════
       SUCCESS / ERROR MESSAGES
       ═══════════════════════════════════════════════════════════════ */
    .success-msg { background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 8px; padding: 16px; margin: 8px 0; }
    .success-msg h2 { color: #166534 !important; }
    .dark .success-msg { background: #052e16 !important; border-color: #166534 !important; }
    .dark .success-msg h2 { color: #bbf7d0 !important; }
    .dark .success-msg p { color: #d1d5db !important; }
    .error-msg { background: #fef2f2; border: 1px solid #fecaca; border-radius: 8px; padding: 16px; margin: 8px 0; }
    .error-msg h2 { color: #991b1b !important; }
    .dark .error-msg { background: #450a0a !important; border-color: #991b1b !important; }
    .dark .error-msg h2 { color: #fecaca !important; }
    .dark .error-msg p { color: #d1d5db !important; }

    /* ═══════════════════════════════════════════════════════════════
       PLACEHOLDER MESSAGES
       ═══════════════════════════════════════════════════════════════ */
    .placeholder-msg {
        padding: 20px;
        background: #f5f3ff;
        border-radius: 8px;
        color: #6D28D9;
        border: 1px dashed #c4b5fd;
        text-align: center;
    }
    .placeholder-msg.placeholder-lg {
        padding: 40px;
    }

    /* ═══════════════════════════════════════════════════════════════
       DARK MODE
       ═══════════════════════════════════════════════════════════════ */

    /* Stepper */
    .dark .progress-stepper {
        background: #1e293b;
        border-color: #334155;
    }
    .dark .step-item { color: #64748b; }
    .dark .step-item.active { background: #2e1065; color: #c4b5fd; }
    .dark .step-item.completed { color: #34d399; }
    .dark .step-num { background: #334155; color: #64748b; }
    .dark .step-item.active .step-num { background: #7C3AED; color: white; }
    .dark .step-item.completed .step-num { background: #10b981; color: white; }
    .dark .step-connector { background: #334155; }
    .dark .step-connector.done { background: #10b981; }

    /* Stage header */
    .dark .stage-header {
        background: linear-gradient(90deg, #1e293b 0%, #0f172a 100%);
        border-left-color: #7C3AED;
    }
    .dark .stage-header h2 { color: #f1f5f9; }
    .dark .stage-header-subtitle, .dark .tip-text { color: #94a3b8 !important; }

    /* Benchmark cards */
    .dark .benchmark-card { background: #1e293b; border-color: #334155; }
    .dark .benchmark-card.selected { border-color: #7C3AED; background: #2e1065; }
    .dark .bm-card { background: #1e293b; border-color: #334155; }
    .dark .bm-card:first-child { border-color: #7C3AED; box-shadow: 0 0 0 3px rgba(124,58,237,0.15); }
    .dark .bm-card-name { color: #f1f5f9; }
    .dark .bm-card-pct { color: #c4b5fd; }
    .dark .bm-bar-label { color: #94a3b8; }
    .dark .bm-bar-track { background: #334155; }
    .dark .bm-bar-value { color: #cbd5e1; }

    /* Comparison cards */
    .dark .comparison-card { background: #1e293b; border-color: #334155; }
    .dark .comparison-card.to-be { background: #2e1065; border-left-color: #7C3AED; }
    .dark .comparison-arrow { color: #c4b5fd; }
    .dark .comparison-label.to-be-label { color: #c4b5fd; }
    .dark .comparison-value { color: #f1f5f9; }
    .dark .comparison-detail { color: #94a3b8; }

    /* Action items */
    .dark .action-item {
        background: #1e293b;
        border-color: #475569;
        color: #e2e8f0;
    }
    .dark .action-item.high-priority { border-left-color: #ef4444; }
    .dark .action-item.medium-priority { border-left-color: #f59e0b; }

    /* Placeholder */
    .dark .placeholder-msg {
        background: #1e1b2e !important;
        color: #a78bfa !important;
        border-color: #4c1d95 !important;
    }

    /* Tables */
    .dark table th {
        background: #1e293b !important;
        color: #e2e8f0 !important;
        border-bottom-color: #475569 !important;
    }
    .dark table td {
        color: #e2e8f0 !important;
        border-bottom-color: #334155 !important;
    }
    .dark table tr { background: #0f172a !important; }
    .dark table tr:nth-child(even) { background: #1e293b !important; }

    /* Typography preview */
    .dark .typography-preview { background: #1e293b !important; }
    .dark .typography-preview th { background: #334155 !important; color: #e2e8f0 !important; border-bottom-color: #475569 !important; }
    .dark .typography-preview td { color: #e2e8f0 !important; }
    .dark .typography-preview .meta-row { background: #1e293b !important; border-top-color: #334155 !important; }
    .dark .typography-preview .scale-name,
    .dark .typography-preview .scale-label { color: #f1f5f9 !important; background: #475569 !important; }
    .dark .typography-preview .meta { color: #cbd5e1 !important; }
    .dark .typography-preview .preview-cell { background: #0f172a !important; border-bottom-color: #334155 !important; }
    .dark .typography-preview .preview-text { color: #f1f5f9 !important; }
    .dark .typography-preview tr:hover .preview-cell { background: #1e293b !important; }

    /* Colors AS-IS preview */
    .dark .colors-asis-header { color: #e2e8f0 !important; background: #1e293b !important; }
    .dark .colors-asis-preview { background: #0f172a !important; }
    .dark .color-row-asis { background: #1e293b !important; border-color: #475569 !important; }
    .dark .color-name-asis { color: #f1f5f9 !important; }
    .dark .frequency { color: #cbd5e1 !important; }
    .dark .color-meta-asis .aa-pass { color: #22c55e !important; background: #14532d !important; }
    .dark .color-meta-asis .aa-fail { color: #f87171 !important; background: #450a0a !important; }
    .dark .context-badge { background: #334155 !important; color: #e2e8f0 !important; }

    /* Color ramps preview */
    .dark .color-ramps-preview { background: #0f172a !important; }
    .dark .ramps-header-info { color: #e2e8f0 !important; background: #1e293b !important; }
    .dark .ramp-header { background: #1e293b !important; }
    .dark .ramp-header-label { color: #cbd5e1 !important; }
    .dark .color-row { background: #1e293b !important; border-color: #475569 !important; }
    .dark .color-name { color: #f1f5f9 !important; background: #475569 !important; }
    .dark .color-hex { color: #cbd5e1 !important; }

    /* Spacing preview */
    .dark .spacing-asis-preview { background: #0f172a !important; }
    .dark .spacing-row-asis { background: #1e293b !important; }
    .dark .spacing-label { color: #f1f5f9 !important; }

    /* Radius preview */
    .dark .radius-asis-preview { background: #0f172a !important; }
    .dark .radius-item { background: #1e293b !important; }
    .dark .radius-label { color: #f1f5f9 !important; }

    /* Shadows preview */
    .dark .shadows-asis-preview { background: #0f172a !important; }
    .dark .shadow-item { background: #1e293b !important; }
    .dark .shadow-box { background: #334155 !important; }
    .dark .shadow-label { color: #f1f5f9 !important; }
    .dark .shadow-value { color: #94a3b8 !important; }

    /* Semantic color ramps */
    .dark .sem-ramps-preview { background: #0f172a !important; }
    .dark .sem-category { background: #1e293b !important; border-color: #475569 !important; }
    .dark .sem-cat-title { color: #f1f5f9 !important; border-bottom-color: #475569 !important; }
    .dark .sem-color-row { background: #0f172a !important; border-color: #334155 !important; }
    .dark .sem-role { color: #f1f5f9 !important; }
    .dark .sem-hex { color: #cbd5e1 !important; }
    .dark .llm-rec { background: #422006 !important; border-color: #b45309 !important; }
    .dark .rec-label { color: #fbbf24 !important; }
    .dark .rec-issue { color: #fde68a !important; }
    .dark .rec-arrow { color: #fbbf24 !important; }
    .dark .llm-summary { background: #2e1065 !important; border-color: #7C3AED !important; }
    .dark .llm-summary h4 { color: #c4b5fd !important; }
    .dark .llm-summary ul, .dark .llm-summary li { color: #ddd6fe !important; }

    /* Score badges */
    .dark .score-badge.high { background: #14532d; color: #86efac; }
    .dark .score-badge.medium { background: #422006; color: #fde68a; }
    .dark .score-badge.low { background: #450a0a; color: #fca5a5; }

    /* Markdown tables */
    .dark .prose table th, .dark .markdown-text table th { background: #1e293b !important; color: #e2e8f0 !important; border-color: #475569 !important; }
    .dark .prose table td, .dark .markdown-text table td { color: #e2e8f0 !important; border-color: #334155 !important; }
    .dark .prose table tr, .dark .markdown-text table tr { background: #0f172a !important; }
    .dark .prose table tr:nth-child(even), .dark .markdown-text table tr:nth-child(even) { background: #1e293b !important; }

    /* Generic dark HTML text */
    .dark .gradio-html p, .dark .gradio-html span, .dark .gradio-html div { color: #e2e8f0; }
    """
    
    with gr.Blocks(
        title="Design System Automation v3",
        theme=corporate_theme,
        css=custom_css
    ) as app:
        
        # Header with branding
        gr.HTML("""
        <div class="app-header">
            <h1>🎨 Design System Automation</h1>
            <p>Reverse-engineer design systems from live websites β€’ AI-powered analysis β€’ Figma-ready export</p>
        </div>
        """)

        # Progress stepper β€” always visible, updated by JS
        progress_stepper = gr.HTML(
            value=_render_stepper(active=1),
            elem_classes=["progress-stepper-wrap"]
        )

        # =================================================================
        # CONFIGURATION
        # =================================================================

        with gr.Accordion("βš™οΈ Configuration", open=not bool(HF_TOKEN_FROM_ENV)):
            gr.Markdown("**HuggingFace Token** β€” Required for Stage 2 AI analysis (LLM agents). "
                        "Get a free token at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens). "
                        "Stage 1 (extraction) works without a token. If set as an environment variable, it loads automatically.",
                        elem_classes=["section-desc"])
            with gr.Row():
                hf_token_input = gr.Textbox(
                    label="HF Token", placeholder="hf_xxxx" if not HF_TOKEN_FROM_ENV else "Token loaded from environment",
                    type="password", scale=4, value="",
                )
                save_token_btn = gr.Button("πŸ’Ύ Save", scale=1)
            token_status = gr.Markdown("βœ… Token loaded from environment" if HF_TOKEN_FROM_ENV else "⏳ Enter token")
            
            def save_token(token):
                if token and len(token) > 10:
                    os.environ["HF_TOKEN"] = token.strip()
                    return "βœ… **Token saved!** You can now use Stage 2 AI analysis. Close this section and enter a URL below to begin."
                return "❌ **Invalid token** β€” please enter a valid HuggingFace token (starts with `hf_`, at least 10 characters). Get one free at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)."
            
            save_token_btn.click(save_token, [hf_token_input], [token_status])
        
        # =================================================================
        # URL INPUT & PAGE DISCOVERY
        # =================================================================
        
        with gr.Accordion("πŸ” Step 1: Discover Pages", open=True):
            gr.Markdown("Enter the homepage URL of any website. The crawler will find up to 20 internal pages "
                        "(homepage, about, contact, product pages, etc.). You then select which pages to scan "
                        "for design tokens (colors, typography, spacing, radius, and shadows).",
                        elem_classes=["section-desc"])

            with gr.Row():
                url_input = gr.Textbox(label="Website URL", placeholder="https://example.com", scale=4)
                discover_btn = gr.Button("πŸ” Discover Pages", variant="primary", scale=1)
            gr.Markdown("*Enter the full URL including `https://` β€” the crawler will follow internal links from this page.*",
                        elem_classes=["section-desc"])

            discover_status = gr.Markdown("")

            with gr.Row():
                log_output = gr.Textbox(label="πŸ“‹ Log", lines=8, interactive=False)

            pages_table = gr.Dataframe(
                headers=["Select", "URL", "Title", "Type", "Status"],
                datatype=["bool", "str", "str", "str", "str"],
                label="Discovered Pages",
                interactive=True,
                visible=False,
            )
            gr.Markdown("*Use the **Select** checkbox to choose pages for extraction. Uncheck pages you want to skip "
                        "(login pages, error pages, etc.). **Type** shows the detected page category. Up to 10 pages will be processed.*",
                        elem_classes=["section-desc"])

            gr.Markdown("*Extraction scans each selected page at two viewport sizes β€” Desktop (1440px) and Mobile (375px) β€” "
                        "pulling colors, typography, spacing, radius, and shadows from computed CSS.*",
                        elem_classes=["section-desc"])
            extract_btn = gr.Button("πŸš€ Extract Tokens (Desktop + Mobile)", variant="primary", visible=False)
        
        # =================================================================
        # STAGE 1: EXTRACTION REVIEW
        # =================================================================
        
        with gr.Accordion("πŸ“Š Stage 1: Review Extracted Tokens", open=False) as stage1_accordion:

            extraction_status = gr.Markdown("")

            gr.Markdown("Review the design tokens extracted from your website. Use the **viewport toggle** to switch between "
                        "Desktop (1440px) and Mobile (375px) data. **Accept or reject** individual tokens using the checkboxes β€” "
                        "rejected tokens will be excluded from your design system export.",
                        elem_classes=["section-desc"])

            viewport_toggle = gr.Radio(
                choices=["Desktop (1440px)", "Mobile (375px)"],
                value="Desktop (1440px)",
                label="Viewport",
            )

            with gr.Tabs():
                with gr.Tab("🎨 Colors"):
                    gr.Markdown("*Each row is a unique color found on the site. **Confidence** shows extraction certainty. "
                                "**AA** indicates WCAG accessibility pass/fail for normal text. **Context** shows where the color was used.*",
                                elem_classes=["section-desc"])
                    colors_table = gr.Dataframe(
                        headers=["Accept", "Color", "Suggested Name", "Frequency", "Confidence", "Contrast", "AA", "Context"],
                        datatype=["bool", "str", "str", "number", "str", "str", "str", "str"],
                        label="Colors",
                        interactive=True,
                    )
                    with gr.Accordion("πŸ‘οΈ Visual Preview", open=False):
                        stage1_colors_preview = gr.HTML(
                            value="<div class='placeholder-msg'>Colors preview will appear after extraction...</div>",
                            label="Colors Preview"
                        )

                with gr.Tab("πŸ“ Typography"):
                    gr.Markdown("*Detected font styles sorted by frequency. **Size** is computed font-size, **Weight** is font-weight "
                                "(400=regular, 700=bold). **Suggested Name** is a semantic token name (e.g., heading.xl). "
                                "Uncheck rows to exclude from your design system.*",
                                elem_classes=["section-desc"])
                    typography_table = gr.Dataframe(
                        headers=["Accept", "Font", "Size", "Weight", "Line Height", "Suggested Name", "Frequency", "Confidence"],
                        datatype=["bool", "str", "str", "str", "str", "str", "number", "str"],
                        label="Typography",
                        interactive=True,
                    )
                    with gr.Accordion("πŸ‘οΈ Visual Preview", open=False):
                        stage1_typography_preview = gr.HTML(
                            value="<div class='placeholder-msg'>Typography preview will appear after extraction...</div>",
                            label="Typography Preview"
                        )

                with gr.Tab("πŸ“ Spacing"):
                    gr.Markdown("*Spacing values (margins, paddings, gaps) extracted from the site. **Base 8** shows whether "
                                "the value aligns with the 8px grid standard. Values are sorted smallest to largest. "
                                "Uncheck irregular spacing values you want to exclude.*",
                                elem_classes=["section-desc"])
                    spacing_table = gr.Dataframe(
                        headers=["Accept", "Value", "Pixels", "Suggested Name", "Frequency", "Base 8", "Confidence"],
                        datatype=["bool", "str", "str", "str", "number", "str", "str"],
                        label="Spacing",
                        interactive=True,
                    )
                    with gr.Accordion("πŸ‘οΈ Visual Preview", open=False):
                        stage1_spacing_preview = gr.HTML(
                            value="<div class='placeholder-msg'>Spacing preview will appear after extraction...</div>",
                            label="Spacing Preview"
                        )

                with gr.Tab("πŸ”˜ Radius"):
                    gr.Markdown("*Border-radius values found across UI elements (buttons, cards, inputs). **Context** shows "
                                "which elements use each value. A consistent radius scale creates a cohesive UI.*",
                                elem_classes=["section-desc"])
                    radius_table = gr.Dataframe(
                        headers=["Accept", "Value", "Frequency", "Context"],
                        datatype=["bool", "str", "number", "str"],
                        label="Border Radius",
                        interactive=True,
                    )
                    with gr.Accordion("πŸ‘οΈ Visual Preview", open=False):
                        stage1_radius_preview = gr.HTML(
                            value="<div class='placeholder-msg'>Radius preview will appear after extraction...</div>",
                            label="Radius Preview"
                        )

                with gr.Tab("πŸŒ‘ Shadows"):
                    gr.Markdown("*Box shadow values used for elevation and depth across the site. "
                                "Shows blur radius, spread, and color for each shadow layer.*",
                                elem_classes=["section-desc"])
                    stage1_shadows_preview = gr.HTML(
                        value="<div class='placeholder-msg'>Shadows preview will appear after extraction...</div>",
                        label="Shadows Preview"
                    )

                with gr.Tab("🧠 Semantic Colors"):
                    gr.Markdown("*Colors automatically categorized by their usage role: Brand (primary, secondary, accent), "
                                "Text (headings, body, muted), Background, Border, and Feedback (success, warning, error).*",
                                elem_classes=["section-desc"])
                    stage1_semantic_preview = gr.HTML(
                        value="<div class='placeholder-msg'>Semantic color analysis will appear after extraction...</div>",
                        label="Semantic Colors Preview"
                    )
            
            gr.Markdown("---")
            gr.Markdown("When you are satisfied with the accepted tokens, **proceed to Stage 2** for AI-powered analysis "
                        "and improvement suggestions. Or **download the raw Stage 1 JSON** for immediate use in Figma Tokens Studio.",
                        elem_classes=["section-desc"])
            with gr.Row():
                proceed_stage2_btn = gr.Button("➑️ Proceed to Stage 2: AI Upgrades", variant="primary")
                download_stage1_btn = gr.Button("πŸ“₯ Download Stage 1 JSON", variant="secondary")
        
        # =================================================================
        # STAGE 2: AI UPGRADES
        # =================================================================
        
        with gr.Accordion("🧠 Stage 2: AI-Powered Analysis", open=False) as stage2_accordion:

            # Stage header
            gr.HTML("""
            <div class="stage-header">
                <h2>🧠 Stage 2: Multi-Agent Analysis</h2>
                <p class="stage-header-subtitle" style="color: #64748b; margin-top: 4px;">Rule Engine + Benchmark Research + LLM Agents</p>
            </div>
            """)

            stage2_status = gr.Markdown("Click **'Run Analysis'** below to start AI-powered design system analysis. "
                                       "This runs a 4-layer pipeline: Rule Engine β†’ Benchmark Research β†’ LLM Agents β†’ Head Synthesizer.")

            # ── Config + Run button ──
            with gr.Row():
                with gr.Column(scale=3):
                    benchmark_checkboxes = gr.CheckboxGroup(
                        choices=[
                            ("🟒 Material Design 3", "material_design_3"),
                            ("🍎 Apple HIG", "apple_hig"),
                            ("πŸ›’ Shopify Polaris", "shopify_polaris"),
                            ("πŸ”΅ Atlassian", "atlassian_design"),
                            ("πŸ”· IBM Carbon", "ibm_carbon"),
                            ("🌊 Tailwind CSS", "tailwind_css"),
                            ("🐜 Ant Design", "ant_design"),
                            ("⚑ Chakra UI", "chakra_ui"),
                        ],
                        value=["material_design_3", "shopify_polaris", "atlassian_design"],
                        label="πŸ“Š Benchmarks to Compare Against",
                    )
                with gr.Column(scale=1):
                    analyze_btn_v2 = gr.Button(
                        "πŸš€ Run Analysis",
                        variant="primary",
                        size="lg",
                    )
                    gr.Markdown(
                        "<small style='color: #64748b;'>Cost: ~$0.003 per run</small>",
                        elem_classes=["section-desc"])

            # ── Always-visible log panel ──
            with gr.Group(elem_classes=["always-visible-log"]):
                stage2_log = gr.Textbox(
                    label="πŸ“‹ Live Analysis Log",
                    lines=20,
                    interactive=False,
                    elem_classes=["log-container"]
                )

            # ═══════════════════════════════════════════════
            # TAB-BASED RESULTS (reduce scrolling)
            # ═══════════════════════════════════════════════
            with gr.Tabs():

                # ── Tab 1: Scores & Actions ──
                with gr.Tab("πŸ“Š Scores & Actions"):
                    gr.Markdown("*Overall scores for your design system. Each score is 0–100. "
                                "Priority actions show the highest-impact fixes.*",
                                elem_classes=["section-desc"])

                    scores_dashboard = gr.HTML(
                        value="<div class='placeholder-msg placeholder-lg'>Scores will appear after analysis...</div>",
                        label="Scores"
                    )

                    priority_actions_html = gr.HTML(
                        value="<div class='placeholder-msg'>Priority actions will appear after analysis...</div>",
                        label="Priority Actions"
                    )

                    # As-Is vs To-Be summary cards
                    stage2_asis_tobe = gr.HTML(
                        value="<div class='placeholder-msg'>As-Is β†’ To-Be transformation summary will appear after analysis...</div>",
                        label="Transformation Summary"
                    )

                # ── Tab 2: Benchmarks ──
                with gr.Tab("πŸ“ˆ Benchmarks"):
                    gr.Markdown("*Your tokens compared against industry design systems. Visual cards show per-category match.*",
                                elem_classes=["section-desc"])
                    benchmark_comparison_md = gr.HTML(
                        value="<div class='placeholder-msg'>Benchmark comparison will appear after analysis...</div>",
                        label="Benchmarks"
                    )

                # ── Tab 3: Typography ──
                with gr.Tab("πŸ“ Typography"):
                    gr.Markdown("*Type scale analysis with standard ratio comparisons. Choose a scale to apply.*",
                                elem_classes=["section-desc"])

                    with gr.Accordion("πŸ‘οΈ Typography Visual Preview", open=True):
                        stage2_typography_preview = gr.HTML(
                            value="<div class='placeholder-msg'>Typography preview will appear after analysis...</div>",
                            label="Typography Preview"
                        )

                    with gr.Row():
                        with gr.Column(scale=2):
                            gr.Markdown("### πŸ–₯️ Desktop (1440px)")
                            typography_desktop = gr.Dataframe(
                                headers=["Token", "Current", "Scale 1.2", "Scale 1.25 ⭐", "Scale 1.333", "Keep"],
                                datatype=["str", "str", "str", "str", "str", "str"],
                                label="Desktop Typography",
                                interactive=False,
                            )
                        with gr.Column(scale=2):
                            gr.Markdown("### πŸ“± Mobile (375px)")
                            typography_mobile = gr.Dataframe(
                                headers=["Token", "Current", "Scale 1.2", "Scale 1.25 ⭐", "Scale 1.333", "Keep"],
                                datatype=["str", "str", "str", "str", "str", "str"],
                                label="Mobile Typography",
                                interactive=False,
                            )

                    with gr.Row():
                        with gr.Column():
                            type_scale_radio = gr.Radio(
                                choices=["Keep Current", "Scale 1.2 (Minor Third)", "Scale 1.25 (Major Third) ⭐", "Scale 1.333 (Perfect Fourth)"],
                                value="Scale 1.25 (Major Third) ⭐",
                                label="Select Type Scale",
                                interactive=True,
                            )
                            gr.Markdown("*Font family preserved. Sizes rounded to even numbers.*",
                                        elem_classes=["section-desc"])

                # ── Tab 4: Colors ──
                with gr.Tab("🎨 Colors"):
                    gr.Markdown("*Complete color analysis: base colors, AI ramps (50–950), and recommendations.*",
                                elem_classes=["section-desc"])

                    # Color Naming Convention Preview
                    with gr.Accordion("🏷️ Naming Convention β€” Preview Before Export", open=True):
                        gr.Markdown("**Choose how colors are named.** 100% rule-based β€” no LLM.",
                                    elem_classes=["section-desc"])
                        with gr.Row():
                            naming_convention_stage2 = gr.Dropdown(
                                choices=["semantic", "tailwind", "material"],
                                value="semantic",
                                label="🎨 Naming Convention",
                                info="semantic = color.brand.primary | tailwind = brand-primary | material = color.brand.primary",
                                scale=2,
                            )
                            preview_colors_btn_stage2 = gr.Button("πŸ‘οΈ Preview", variant="secondary", scale=1)
                        color_preview_output_stage2 = gr.Textbox(
                            label="Color Classification Preview (Rule-Based)",
                            lines=18, max_lines=40, interactive=False,
                            placeholder="Click 'Preview' to see how colors will be named in the export.",
                        )

                    # LLM Recommendations
                    with gr.Accordion("πŸ€– LLM Color Recommendations", open=True):
                        gr.Markdown("*AI-suggested accessibility fixes and improvements.*",
                                    elem_classes=["section-desc"])
                        llm_color_recommendations = gr.HTML(
                            value="<div class='placeholder-msg'>LLM recommendations will appear after analysis...</div>",
                            label="LLM Recommendations"
                        )
                        color_recommendations_table = gr.Dataframe(
                            headers=["Accept", "Role", "Current", "Issue", "Suggested", "Contrast"],
                            datatype=["bool", "str", "str", "str", "str", "str"],
                            label="Color Recommendations",
                            interactive=True,
                            col_count=(6, "fixed"),
                        )

                    # Color Ramps
                    with gr.Accordion("πŸ‘οΈ Color Ramps (Semantic Groups)", open=True):
                        stage2_color_ramps_preview = gr.HTML(
                            value="<div class='placeholder-msg'>Color ramps preview will appear after analysis...</div>",
                            label="Color Ramps Preview"
                        )

                    base_colors_display = gr.Markdown("*Base colors will appear after analysis*")
                    color_ramps_display = gr.Markdown("*Color ramps will appear after analysis*")
                    color_ramps_checkbox = gr.Checkbox(
                        label="βœ“ Generate color ramps (keeps base colors, adds 50-950 shades)",
                        value=True,
                    )

                # ── Tab 5: Spacing / Radius / Shadows ──
                with gr.Tab("πŸ“ Spacing Β· Radius Β· Shadows"):

                    gr.Markdown("## πŸ“ Spacing")
                    gr.Markdown("*Spacing values compared against 8px and 4px grids.*",
                                elem_classes=["section-desc"])
                    with gr.Row():
                        with gr.Column(scale=2):
                            spacing_comparison = gr.Dataframe(
                                headers=["Current", "8px Grid", "4px Grid"],
                                datatype=["str", "str", "str"],
                                label="Spacing Comparison",
                                interactive=False,
                            )
                        with gr.Column(scale=1):
                            spacing_radio = gr.Radio(
                                choices=["Keep Current", "8px Base Grid ⭐", "4px Base Grid"],
                                value="8px Base Grid ⭐",
                                label="Spacing System",
                                interactive=True,
                            )

                    gr.Markdown("---")
                    gr.Markdown("## πŸ”˜ Border Radius")
                    gr.Markdown("*Radius tokens mapped to standard scale (none β†’ full).*",
                                elem_classes=["section-desc"])
                    radius_display = gr.Markdown("*Radius tokens will appear after analysis*")

                    gr.Markdown("---")
                    gr.Markdown("## 🌫️ Shadows")
                    gr.Markdown("*Elevation tokens (shadow.xs β†’ shadow.2xl).*",
                                elem_classes=["section-desc"])
                    shadows_display = gr.Markdown("*Shadow tokens will appear after analysis*")

            # ── Apply / Reset ──
            gr.Markdown("---")
            gr.Markdown("**Apply** saves your choices to the export. **Reset** reverts to extracted values.",
                        elem_classes=["section-desc"])
            with gr.Row():
                apply_upgrades_btn = gr.Button("✨ Apply Selected Upgrades", variant="primary", scale=2)
                reset_btn = gr.Button("↩️ Reset to Original", variant="secondary", scale=1)
            apply_status = gr.Markdown("", elem_classes=["apply-status-box"])
        
        # =================================================================
        # STAGE 3: EXPORT
        # =================================================================
        
        with gr.Accordion("πŸ“¦ Stage 3: Export", open=False) as stage3_accordion:
            gr.Markdown("Export your finalized design tokens as JSON, compatible with **Figma Tokens Studio**.",
                        elem_classes=["section-desc"])
            gr.Markdown("""
- **Naming Convention:** Choose how colors are named in the export. Preview before exporting to verify.
- **Stage 1 JSON (As-Is):** Raw extracted tokens β€” useful for archival or baseline comparison.
- **Final JSON (Upgraded):** Tokens with your selected improvements applied. **Recommended export.**
            """, elem_classes=["section-desc"])

            with gr.Row():
                naming_convention = gr.Dropdown(
                    choices=["semantic", "tailwind", "material"],
                    value="semantic",
                    label="🎨 Naming Convention",
                    info="semantic = color.brand.primary | tailwind = brand-primary | material = color.brand.primary",
                    scale=2,
                )
                preview_colors_btn = gr.Button("πŸ‘οΈ Preview Color Names", variant="secondary", scale=1)

            color_preview_output = gr.Textbox(
                label="Color Classification Preview (Rule-Based β€” No LLM)",
                lines=15,
                max_lines=30,
                interactive=False,
            )

            with gr.Row():
                export_stage1_btn = gr.Button("πŸ“₯ Export Stage 1 (As-Is)", variant="secondary")
                export_final_btn = gr.Button("πŸ“₯ Export Final (Upgraded)", variant="primary")

            gr.Markdown("*The generated JSON uses a flat token structure compatible with Figma Tokens Studio. "
                        "Copy the contents or save as a `.json` file.*",
                        elem_classes=["section-desc"])
            export_output = gr.Code(label="Tokens JSON", language="json", lines=25)

            # Stage 2 color naming preview (primary β€” visible before export)
            preview_colors_btn_stage2.click(
                preview_color_classification,
                inputs=[naming_convention_stage2],
                outputs=[color_preview_output_stage2],
            )
            # Sync naming convention: Stage 2 dropdown β†’ Stage 3 dropdown
            naming_convention_stage2.change(
                lambda v: v,
                inputs=[naming_convention_stage2],
                outputs=[naming_convention],
            )
            # Stage 3 also syncs back
            naming_convention.change(
                lambda v: v,
                inputs=[naming_convention],
                outputs=[naming_convention_stage2],
            )

            # Stage 3 preview (kept for convenience)
            preview_colors_btn.click(
                preview_color_classification,
                inputs=[naming_convention],
                outputs=[color_preview_output],
            )
            export_stage1_btn.click(
                export_stage1_json,
                inputs=[naming_convention],
                outputs=[export_output],
            )
            export_final_btn.click(
                export_tokens_json,
                inputs=[naming_convention],
                outputs=[export_output],
            )
        
        # =================================================================
        # EVENT HANDLERS
        # =================================================================

        # Store data for viewport toggle
        desktop_data = gr.State({})
        mobile_data = gr.State({})

        # ── Discover pages ──
        discover_btn.click(
            fn=discover_pages,
            inputs=[url_input],
            outputs=[discover_status, log_output, pages_table],
        ).then(
            fn=lambda: (gr.update(visible=True), gr.update(visible=True),
                        _render_stepper(active=2, completed=[1])),
            outputs=[pages_table, extract_btn, progress_stepper],
        )

        # ── Extract tokens ──
        extract_btn.click(
            fn=extract_tokens,
            inputs=[pages_table],
            outputs=[extraction_status, log_output, desktop_data, mobile_data,
                     stage1_typography_preview, stage1_colors_preview,
                     stage1_semantic_preview,
                     stage1_spacing_preview, stage1_radius_preview, stage1_shadows_preview],
        ).then(
            fn=lambda d: (d.get("colors", []), d.get("typography", []), d.get("spacing", []), d.get("radius", [])),
            inputs=[desktop_data],
            outputs=[colors_table, typography_table, spacing_table, radius_table],
        ).then(
            fn=lambda: (gr.update(open=True), gr.update(open=True),
                        _render_stepper(active=3, completed=[1, 2])),
            outputs=[stage1_accordion, stage2_accordion, progress_stepper],
        )

        # ── Viewport toggle ──
        viewport_toggle.change(
            fn=switch_viewport,
            inputs=[viewport_toggle],
            outputs=[colors_table, typography_table, spacing_table, radius_table],
        )

        # ── Stage 2: Analyze ──
        analyze_btn_v2.click(
            fn=run_stage2_analysis_v2,
            inputs=[benchmark_checkboxes],
            outputs=[
                stage2_status,
                stage2_log,
                benchmark_comparison_md,
                scores_dashboard,
                priority_actions_html,
                color_recommendations_table,
                typography_desktop,
                typography_mobile,
                stage2_typography_preview,
                stage2_color_ramps_preview,
                llm_color_recommendations,
                spacing_comparison,
                base_colors_display,
                color_ramps_display,
                radius_display,
                shadows_display,
                color_preview_output_stage2,
                stage2_asis_tobe,
            ],
        ).then(
            fn=lambda: (gr.update(open=True),
                        _render_stepper(active=4, completed=[1, 2, 3])),
            outputs=[stage3_accordion, progress_stepper],
        )

        # ── Stage 2: Apply upgrades ──
        apply_upgrades_btn.click(
            fn=apply_selected_upgrades,
            inputs=[type_scale_radio, spacing_radio, color_ramps_checkbox, color_recommendations_table],
            outputs=[apply_status, stage2_log],
        ).then(
            fn=lambda: (gr.update(open=True),
                        _render_stepper(active=5, completed=[1, 2, 3, 4])),
            outputs=[stage3_accordion, progress_stepper],
        )

        # ── Stage 2: Reset to original ──
        reset_btn.click(
            fn=reset_to_original,
            outputs=[type_scale_radio, spacing_radio, color_ramps_checkbox, apply_status, stage2_log],
        )

        # ── Stage 1: Download JSON ──
        download_stage1_btn.click(
            fn=export_stage1_json,
            outputs=[export_output],
        )

        # ── Proceed to Stage 2 ──
        proceed_stage2_btn.click(
            fn=lambda: gr.update(open=True),
            outputs=[stage2_accordion],
        )
        
        # =================================================================
        # FOOTER
        # =================================================================
        
        gr.Markdown("""
        ---
        <div style="text-align: center; color: #94a3b8; font-size: 12px; padding: 12px 0;">
        <strong>Design System Automation v3</strong> Β· Playwright + Firecrawl + HuggingFace<br/>
        Rule Engine (FREE) + ReAct LLM Agents (AURORA Β· ATLAS Β· SENTINEL Β· NEXUS)
        </div>
        """)
    
    return app


# =============================================================================
# MAIN
# =============================================================================

if __name__ == "__main__":
    app = create_ui()
    app.launch(server_name="0.0.0.0", server_port=7860)