File size: 179,918 Bytes
108b56e
 
 
d2d2c24
05f6f37
402a927
 
de4c2b9
 
3a04ed7
05f6f37
 
 
c9f1319
8aaf42b
 
 
90837ad
402a927
05f6f37
02baeee
 
402a927
 
 
05f6f37
01a84ee
 
 
 
 
 
 
 
 
 
 
 
 
 
ab8b381
01a84ee
 
 
 
 
 
 
 
09a8e4b
01a84ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beb33a2
402a927
 
 
 
 
 
 
 
 
 
 
 
 
beb33a2
402a927
 
beb33a2
402a927
 
 
 
 
beb33a2
402a927
 
7835b45
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c172a06
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c172a06
402a927
 
 
 
 
 
 
 
 
 
 
 
 
3f493ad
5a160ac
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
db66925
402a927
 
9f7d347
402a927
db66925
402a927
 
db66925
402a927
 
3bddbef
402a927
 
5f22904
402a927
 
c264689
402a927
 
 
 
 
 
4f64445
402a927
 
 
 
 
 
4f64445
402a927
4f64445
402a927
 
4f64445
60c0305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
 
 
 
 
 
 
4f64445
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7241714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5f7d67
 
ec534d6
2666540
 
 
 
 
d253a12
0fc51a4
a5f7d67
7241714
a5f7d67
2666540
a5f7d67
2666540
 
a5f7d67
 
2666540
a5f7d67
2666540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6eaf1b
3acc815
 
 
 
 
0fc51a4
d253a12
3acc815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2666540
 
ec534d6
 
2666540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6eaf1b
2666540
 
 
 
e3e1814
 
 
 
 
 
 
2666540
 
7241714
 
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2d2c24
 
 
7241714
 
 
 
 
 
 
 
 
402a927
 
 
 
 
132f6ec
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132f6ec
 
 
 
 
ebbad48
4f64445
 
 
 
 
 
2f1fe79
8fdb5c7
e7bcaf5
5a160ac
 
db9a2da
f26ed89
db9a2da
ecebfd6
db9a2da
5a160ac
ecebfd6
db9a2da
ecebfd6
 
db9a2da
5a160ac
 
db9a2da
 
5a160ac
ecebfd6
 
5a160ac
 
 
 
 
db9a2da
5a160ac
e90e656
 
 
 
 
 
 
 
 
 
 
 
 
5a160ac
 
8fdb5c7
ecebfd6
cf000a0
 
c172a06
 
5a160ac
0fc51a4
c172a06
9f1160e
c172a06
5a160ac
9f1160e
c172a06
9f1160e
c172a06
 
9f1160e
c172a06
 
 
 
9f1160e
c172a06
 
5a160ac
 
9f1160e
 
 
cf000a0
8fdb5c7
 
e7bcaf5
5a160ac
5f22904
f26ed89
5a160ac
db9a2da
8fdb5c7
ecebfd6
5f22904
5a160ac
db9a2da
5a160ac
8fdb5c7
5a160ac
db9a2da
5a160ac
ecebfd6
77eca04
5a160ac
 
7835b45
db9a2da
8fdb5c7
 
b55670a
 
 
 
4cae79c
b55670a
 
 
 
 
 
 
 
4cae79c
b55670a
 
 
 
 
 
 
 
 
db9a2da
b55670a
 
45140d2
b55670a
 
 
b9b8e62
db9a2da
 
5f22904
7835b45
b9b8e62
2219cef
21936f5
7e262e3
 
 
cf000a0
 
 
 
 
 
 
 
 
 
 
 
21936f5
 
 
 
 
 
 
 
 
 
d7c3042
cf000a0
 
 
 
 
 
2219cef
402a927
2219cef
 
21936f5
2219cef
7241714
d2d2c24
 
7241714
a5f7d67
 
 
7241714
 
 
 
 
a5f7d67
 
7241714
 
 
 
 
 
 
 
a5f7d67
2219cef
 
 
 
 
15a48c9
21936f5
 
2219cef
21936f5
2219cef
 
 
3f6dd32
2219cef
402a927
 
0192ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21936f5
9f1dbce
 
21936f5
 
 
d2d2c24
 
 
21936f5
 
8f3c1b0
21936f5
 
 
 
 
 
9f1dbce
 
 
 
 
21936f5
 
 
 
132f6ec
 
9f1dbce
 
132f6ec
 
21936f5
 
 
 
9f1dbce
21936f5
 
9f1dbce
 
402a927
21936f5
 
 
 
 
 
 
 
 
9f1dbce
21936f5
 
 
9f1dbce
7681ebc
0192ae5
 
 
 
21936f5
 
9f1dbce
21936f5
 
 
 
 
 
 
 
f421726
7681ebc
 
f421726
7681ebc
6268f3a
 
21936f5
 
 
 
 
 
 
f421726
 
7681ebc
21936f5
c5b93e3
 
21936f5
c5b93e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21936f5
 
 
7681ebc
21936f5
bdc4d51
21936f5
c5b93e3
21936f5
7681ebc
 
c5b93e3
21936f5
c5b93e3
7681ebc
 
 
 
9f1dbce
38b0af6
21936f5
 
38b0af6
8f3c1b0
 
 
 
 
21936f5
 
 
 
0192ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21936f5
8f3c1b0
7681ebc
51ba605
 
 
 
 
21936f5
 
 
9f1dbce
0192ae5
e144dbb
5dcaba5
 
7681ebc
 
5dcaba5
 
7681ebc
21936f5
38b0af6
e144dbb
9f1dbce
e144dbb
21936f5
4cae79c
21936f5
 
 
 
 
 
9f1dbce
21936f5
9f1dbce
21936f5
9f1dbce
21936f5
 
9f1dbce
21936f5
9f1dbce
21936f5
 
 
8f3c1b0
21936f5
 
f421726
51ba605
 
 
 
 
d2d2c24
 
 
 
f421726
 
 
 
 
 
 
 
 
51ba605
f421726
 
 
 
 
 
 
 
 
 
 
51ba605
f421726
 
 
 
21936f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c2f9de
 
 
 
80c53e2
8c2f9de
21936f5
 
 
402a927
21936f5
 
402a927
 
21936f5
77eca04
 
d2d2c24
 
77eca04
 
21936f5
 
 
77eca04
 
 
cf000a0
 
 
 
 
77eca04
 
cf000a0
77eca04
 
cf000a0
77eca04
 
cf000a0
 
77eca04
402a927
77eca04
3f6dd32
77eca04
402a927
 
36989f3
c771d4a
d57395c
 
 
 
 
719b28d
d57395c
 
 
 
 
 
 
 
354a2a1
d57395c
 
 
719b28d
7f78677
d57395c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719b28d
d57395c
073e11f
d57395c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719b28d
d57395c
 
 
 
 
 
 
354a2a1
d57395c
 
 
719b28d
 
8135aec
 
 
 
d57395c
 
 
 
 
 
 
 
 
 
719b28d
d57395c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8135aec
 
d57395c
 
 
 
 
6dbee56
d57395c
 
 
719b28d
d57395c
 
 
 
 
 
 
 
 
354a2a1
d57395c
 
 
 
719b28d
7f78677
d57395c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719b28d
d57395c
719b28d
d57395c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eae6eff
8135aec
df16717
 
 
 
 
 
 
 
7f78677
df16717
 
 
 
 
 
 
 
 
 
 
 
 
 
1bd735b
 
 
719b28d
df16717
354a2a1
1bd735b
df16717
 
 
d7c3042
 
3acc815
d7c3042
 
 
 
df16717
 
 
 
 
402a927
36989f3
f491046
8b69e8e
65cacf9
e21a0a3
719b28d
e21a0a3
a6eaf1b
719b28d
8b69e8e
e21a0a3
 
 
f491046
e21a0a3
36989f3
 
 
e21a0a3
f491046
39895bb
 
 
36989f3
 
39895bb
820e2d4
f072885
402a927
f072885
 
e21a0a3
39895bb
36989f3
e21a0a3
 
36989f3
39895bb
543793b
402a927
36989f3
60b13ae
c1dd1b0
 
 
f1b37cc
 
 
cf1dd09
556f43a
f1b37cc
 
c1dd1b0
 
556f43a
 
 
 
 
 
 
c1dd1b0
8cc1113
2542fd3
60b13ae
a24e2f4
bfb85f0
283c1a2
 
 
 
 
 
 
 
60b13ae
a24e2f4
1bd735b
8b69e8e
2542fd3
1bd735b
677e9f8
 
 
 
 
 
 
 
 
 
26f13a8
1bd735b
677e9f8
1bd735b
556f43a
1bd735b
556f43a
677e9f8
 
9f88c4e
cc476de
677e9f8
1bd735b
 
2542fd3
 
18cc689
 
2542fd3
da90972
bfb85f0
18cc689
9f88c4e
7f78677
921eabf
 
 
2a0b51a
 
 
 
 
 
2542fd3
921eabf
da90972
921eabf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f88c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2542fd3
2a0b51a
 
 
 
 
2542fd3
 
2a0b51a
 
2542fd3
2a0b51a
 
 
 
 
 
2542fd3
 
2a0b51a
 
 
 
2542fd3
 
2a0b51a
26f13a8
2542fd3
 
 
 
 
f7d9365
2542fd3
 
 
 
 
 
 
2a0b51a
2542fd3
fee243a
a24e2f4
402a927
a24e2f4
d072490
 
a24e2f4
7f78677
d23401a
 
 
 
 
 
 
 
 
 
 
 
 
 
073e11f
d23401a
 
 
 
 
 
 
63566e3
d23401a
 
7f78677
d23401a
e49c980
2542fd3
e3e1814
 
 
 
 
 
 
2542fd3
e0e249b
9feb3a6
074c65c
 
9feb3a6
e0e249b
074c65c
 
 
9feb3a6
074c65c
 
f072885
402a927
f072885
074c65c
 
 
 
 
e0e249b
074c65c
e0e249b
402a927
 
36989f3
 
 
 
 
 
 
 
 
 
09a8e4b
36989f3
 
09a8e4b
36989f3
 
 
 
 
 
 
283c1a2
ffdc0dc
a92d4f3
 
283c1a2
bfb85f0
d57395c
a92d4f3
 
d072490
283c1a2
 
 
 
 
 
 
 
 
 
bfb85f0
 
2b2b2f4
c75cae4
283c1a2
baf15be
283c1a2
58b3e65
283c1a2
 
354a2a1
a76f63d
e7887f3
a76f63d
e7887f3
a76f63d
e7887f3
 
354a2a1
 
a76f63d
 
fcb808a
a76f63d
354a2a1
d7c3042
 
 
 
 
 
283c1a2
 
 
 
1bd735b
 
ffdc0dc
402a927
283c1a2
354a2a1
319411a
283c1a2
 
 
402a927
cb30c2c
402a927
96211e0
05f6f37
 
 
 
 
 
 
8e4a64e
05f6f37
d879330
05f6f37
 
 
e082495
05f6f37
fe9f22d
 
d879330
 
 
 
 
80c53e2
05f6f37
 
 
 
 
 
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed43430
402a927
 
ed43430
402a927
 
 
 
ed43430
402a927
 
cb30c2c
402a927
cb30c2c
402a927
 
cb30c2c
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e90e656
402a927
 
 
 
 
 
 
 
 
 
 
 
e90e656
402a927
 
 
 
b18eb92
60c0305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10fd028
402a927
 
 
02baeee
402a927
05f6f37
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
9feb3a6
 
05f6f37
bd3707a
 
f7044c8
 
 
bd3707a
 
 
f7044c8
bd3707a
f7044c8
 
 
 
 
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
05f6f37
60b13ae
05f6f37
55e4cf1
 
 
 
 
 
402a927
 
55e4cf1
402a927
 
 
 
 
a24e2f4
402a927
55e4cf1
402a927
55e4cf1
 
 
 
 
402a927
 
 
 
 
 
 
 
 
55e4cf1
402a927
55e4cf1
 
402a927
 
 
 
 
 
 
c75cae4
a5f7d67
55e4cf1
a5f7d67
55e4cf1
 
 
 
 
 
 
 
 
 
402a927
55e4cf1
402a927
55e4cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d57a74
402a927
 
55e4cf1
508a2e2
402a927
55e4cf1
 
402a927
 
 
 
 
 
55e4cf1
 
 
 
402a927
55e4cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
55e4cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
55e4cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
 
 
 
 
 
 
 
 
 
 
 
a5f7d67
402a927
 
 
55e4cf1
402a927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55e4cf1
65fa006
36989f3
3d57a74
 
55e4cf1
3d57a74
 
c43af1c
3d57a74
 
073e11f
55e4cf1
 
 
3d57a74
1bd735b
402a927
55e4cf1
 
 
 
 
402a927
55e4cf1
 
 
65fa006
55e4cf1
 
ed0c60a
 
 
 
 
55e4cf1
 
402a927
cb30c2c
fe9f22d
05f6f37
 
 
 
 
 
 
 
 
 
 
 
48496cb
05f6f37
 
402a927
05f6f37
 
 
 
 
 
 
 
18cc689
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18cc689
 
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18cc689
 
05f6f37
 
18cc689
05f6f37
78bba40
05f6f37
 
 
 
85926f0
5f439dd
 
85926f0
05f6f37
 
 
074c65c
 
 
 
 
 
 
 
 
05f6f37
39895bb
6986d5c
39895bb
05f6f37
6986d5c
39895bb
05f6f37
 
8bc7fbc
da90972
01a84ee
da90972
 
 
 
 
e764258
6986d5c
 
 
 
01a84ee
6986d5c
 
01a84ee
3f1860f
 
 
01a84ee
 
6986d5c
01a84ee
 
 
 
 
 
fb148fd
01a84ee
6986d5c
01a84ee
 
 
 
556f43a
6986d5c
556f43a
6986d5c
556f43a
e43b6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09a8e4b
 
 
 
e43b6fc
09a8e4b
 
 
 
 
e43b6fc
 
09a8e4b
 
 
 
e43b6fc
09a8e4b
e43b6fc
09a8e4b
 
 
 
 
 
e43b6fc
 
09a8e4b
 
e43b6fc
 
 
556f43a
6986d5c
 
 
 
556f43a
 
01a84ee
 
 
aaf17eb
6986d5c
01a84ee
aaf17eb
6986d5c
 
01a84ee
aaf17eb
c257904
6986d5c
 
fb148fd
6986d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f6f37
559e3f2
 
 
 
05f6f37
 
 
 
a2689f9
05f6f37
 
77ddce7
cb30c2c
05f6f37
 
 
 
 
 
 
402a927
05f6f37
559e3f2
 
05f6f37
 
559e3f2
05f6f37
 
 
559e3f2
 
05f6f37
a2689f9
05f6f37
b6e2558
05f6f37
 
77ddce7
05f6f37
 
 
 
 
 
 
18cc689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d879330
 
 
 
 
 
 
 
 
 
 
 
18cc689
559e3f2
05f6f37
 
 
 
 
 
 
a21a550
05f6f37
 
 
 
 
 
 
18cc689
 
 
 
 
 
05f6f37
 
 
 
 
559e3f2
 
 
 
 
 
 
df0ce20
559e3f2
18cc689
 
 
 
 
 
 
 
 
 
 
559e3f2
 
18cc689
559e3f2
 
 
 
 
 
 
 
 
 
 
 
e0e249b
559e3f2
 
 
 
cb30c2c
559e3f2
 
cf84868
 
 
 
 
 
02baeee
cf84868
d879330
02baeee
18cc689
cf84868
 
18cc689
 
 
 
 
e90e656
cb30c2c
e0e249b
9aa07d6
108b56e
6e60564
 
 
 
 
e0e249b
559e3f2
e3c2eb8
 
e0e249b
559e3f2
 
cb30c2c
559e3f2
8a512ae
c553142
559e3f2
e3c2eb8
559e3f2
 
 
05f6f37
559e3f2
 
 
 
d879330
559e3f2
 
e3c2eb8
cb30c2c
559e3f2
 
02baeee
d879330
 
 
559e3f2
 
 
 
 
 
1eacd00
c553142
18cc689
 
c553142
 
18cc689
 
 
 
 
 
60b13ae
e0e249b
283c1a2
60b13ae
283c1a2
36989f3
e0e249b
559e3f2
9978a7a
921eabf
 
9978a7a
559e3f2
18cc689
 
 
c553142
05f6f37
18cc689
 
 
 
 
 
0e2454e
9222b74
 
48496cb
0e2454e
 
 
 
 
 
9222b74
 
c553142
48496cb
18cc689
 
c553142
18cc689
 
 
 
 
 
 
 
 
 
c553142
 
18cc689
402a927
18cc689
 
 
 
 
 
 
 
 
 
 
05f6f37
48496cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402a927
05f6f37
 
 
559e3f2
 
 
 
18cc689
05f6f37
559e3f2
 
 
05f6f37
 
 
 
 
 
 
 
 
 
 
559e3f2
05f6f37
559e3f2
 
 
18cc689
559e3f2
 
18cc689
05f6f37
559e3f2
18cc689
05f6f37
 
559e3f2
 
05f6f37
 
559e3f2
 
 
77ddce7
559e3f2
05f6f37
559e3f2
 
18cc689
f807498
1a68fb8
4c364e9
 
 
 
 
 
 
 
 
 
 
 
 
d072490
4c364e9
1a68fb8
05f6f37
 
 
 
073e11f
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40373c8
05f6f37
40373c8
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108b56e
05f6f37
 
 
e8edadb
05f6f37
 
 
559e3f2
05f6f37
559e3f2
a2689f9
 
78bba40
 
 
05f6f37
e8edadb
a2689f9
 
a8fe78b
a2689f9
e8edadb
05f6f37
 
a2689f9
 
 
 
 
 
e8edadb
 
05f6f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f2f0ea
 
 
05f6f37
ad51d2b
e8edadb
 
2219cef
 
f6e0aa5
2219cef
 
8c4be8b
21936f5
 
780d258
a8fe78b
a2689f9
 
 
 
 
862f326
a8fe78b
 
5f22904
18cc689
559900b
 
 
18cc689
beb33a2
 
a4554cc
beb33a2
 
 
 
 
a2689f9
beb33a2
05f6f37
 
 
a8fe78b
05f6f37
ad51d2b
05f6f37
 
 
 
 
 
78bba40
05f6f37
 
 
 
 
 
 
 
 
a2689f9
0b47078
05f6f37
18cc689
05f6f37
 
 
 
 
 
f39ad5a
05f6f37
 
 
 
 
 
c5cdafd
05f6f37
 
 
 
 
 
 
 
 
 
d072490
05f6f37
5f22904
 
8c38be8
fefabf5
5f22904
 
21936f5
 
 
 
 
 
 
 
 
 
 
 
2219cef
 
21936f5
2219cef
 
 
beb33a2
 
21936f5
3f6dd32
beb33a2
5f22904
05f6f37
 
 
b18c05c
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
# =============================================================================
# By T.Sakai / G.Huet
# =============================================================================
import os, time, io, json, random, re, requests, openai, base64, html
import gradio as gr
import PyPDF2, tempfile, logging, difflib
import markdown, unicodedata, pdfkit
from PIL import Image
from io import BytesIO
from bs4 import BeautifulSoup
from datetime import datetime
from reportlab.lib.pagesizes import A4
from xhtml2pdf import pisa
from bs4.element import Tag
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException

# ============================================================================= Global Settings
MAX_MESSAGE_LENGTH = 1048576
SUMMARIZATION_REQUEST_COUNT = 0
TOTAL_SUMMARIZED_WORDS = 0
# Set up logging basic configuration
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# ============================================================================= Helper functions

def capture_visual_screenshot(srcdoc: str) -> str:
    """
    Opens a temporary HTML file from the provided srcdoc string,
    loads it in a headless Chrome browser using Selenium,
    waits for the content to render,
    takes a screenshot, crops it, and returns a base64-encoded PNG image.
    """
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    driver = None
    try:
        driver = webdriver.Chrome(options=options)
        driver.set_window_size(1100, 800)  # Adjust per your expected visual dimensions

        # Write the srcdoc to a temporary HTML file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmp_file:
            tmp_file.write(srcdoc.encode("utf-8"))
            tmp_file.flush()
            file_url = "file://" + tmp_file.name

        driver.get(file_url)
        time.sleep(5)  # Allow time for JavaScript (e.g., mermaid) to render

        screenshot_png = driver.get_screenshot_as_png()
        image = Image.open(BytesIO(screenshot_png))
        # Crop the image to remove extra whitespace
        cropped_image = image.crop(image.getbbox())
        buffered = BytesIO()
        cropped_image.save(buffered, format='PNG')
        base64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
        return base64_img
    except WebDriverException as e:
        logging.error("Error in capture_visual_screenshot: %s", e)
        return ""
    finally:
        if driver:
            driver.quit()

def replace_visual_iframes(soup: BeautifulSoup) -> BeautifulSoup:
    """
    Finds all <iframe class="visual-frame"> tags in the provided BeautifulSoup object,
    uses capture_visual_screenshot() to get a base64 image,
    and replaces each iframe with an <img> tag embedding the screenshot.
    """
    iframes = soup.find_all("iframe", class_="visual-frame")
    for iframe in iframes:
        srcdoc = iframe.get("srcdoc")
        if srcdoc:
            base64_img = capture_visual_screenshot(srcdoc)
            if base64_img:
                new_img = soup.new_tag("img")
                new_img["src"] = "data:image/png;base64," + base64_img
                new_img["style"] = "max-width:100%; display:block; margin:auto; page-break-after:avoid;"
                iframe.replace_with(new_img)
            else:
                logging.error("Failed to capture screenshot for an iframe.")
    return soup

def process_pdf(url: str) -> str:
    try:
        headers = {"User-Agent": get_random_header()}
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        f = io.BytesIO(r.content)
        reader = PyPDF2.PdfReader(f)
        text = ""
        for page in reader.pages:
            page_text = page.extract_text()
            if page_text:
                text += page_text + "\n"
        if text.strip():
            logging.info(f"process_pdf: Successfully extracted text from {url}")
            return text
        else:
            logging.info(f"process_pdf: No text extracted from {url}")
            return "PDF content could not be extracted."
    except Exception as e:
        err = f"Error processing PDF: {e}"
        logging.error(err)
        return err

def compress_text(text: str, target_length: int) -> str:
    global SUMMARIZATION_REQUEST_COUNT, TOTAL_SUMMARIZED_WORDS
    prompt = f"Summarize the following text in a way that preserves all valuable information, and output a compressed version not exceeding {target_length} characters:\n\n{text}"
    summary = llm_call(prompt, model="gpt-4o-mini", max_tokens_param=100000)
    SUMMARIZATION_REQUEST_COUNT += 1
    TOTAL_SUMMARIZED_WORDS += len(summary.split())
    logging.info(f"compress_text: Compressed text length: {len(summary)} -- Requests: {SUMMARIZATION_REQUEST_COUNT}, Total words: {TOTAL_SUMMARIZED_WORDS}")
    return summary

def summarize_large_text(text: str, target_length: int, chunk_size: int = 1000, overlap: int = 200) -> str:
    """
    Summarizes a large text by splitting it into overlapping chunks, summarizing each chunk,
    and then combining the intermediate summaries into a final summary.
    
    The prompt for these intermediate calls explicitly instructs to preserve key details
    and to include any tables or structured data present.
    
    Parameters:
      text         : The input text to summarize.
      target_length: The maximum number of tokens (or an approximate target length) for the final summary.
      chunk_size   : The number of words to include in each chunk.
      overlap      : The number of overlapping words between chunks.
    
    Returns:
      The final combined summary as a string.
    """
    words = text.split()
    if len(words) <= chunk_size:
        # If the text is short, simply return it (or you could call a simple summarization)
        if len(words) < 500:
            return "Not a coherent text or not worth processing - discard."
        else:
            return text
    chunks = []
    i = 0
    while i < len(words):
        # Create a chunk from i to i+chunk_size words.
        chunk = " ".join(words[i:i+chunk_size])
        chunks.append(chunk)
        # Move forward by chunk_size - overlap words.
        i += (chunk_size - overlap)
    
    summary_chunks = []
    for chunk in chunks:
        chunk_prompt = (f"""
Summarize the following text, preserving all key details and ensuring that any tables or structured data are also summarized:
{chunk}
Maintain the original sources.
Keep all mentions of names, people/titles, dates, papers, reports, organisation/institute/NGO/government bodies quotes, products, project names, ... 
"""
        )
        summary_chunk = llm_call(prompt=chunk_prompt, model="gpt-4o-mini", temperature=0, max_tokens_param=500)
        summary_chunk = re.sub(r'\{\[\{(.*?)\}\]\}', r'\1', summary_chunk)
        summary_chunk = re.sub(r'\[\{(.*?)\}\]', r'\1', summary_chunk)
        global SUMMARIZATION_REQUEST_COUNT, TOTAL_SUMMARIZED_WORDS
        SUMMARIZATION_REQUEST_COUNT += 1
        TOTAL_SUMMARIZED_WORDS += len(summary_chunk.split())
        summary_chunks.append(summary_chunk.strip())
    
    combined_summary = "\n".join(summary_chunks)
    # Now, produce one final summary that fuses all the intermediate summaries.
    final_prompt = (f"""
Combine the following summaries into one concise summary that preserves all critical details, including any relevant table or structured data:
{combined_summary}
Maintain the original sources.
Keep all mentions of names, people/titles, dates, papers, reports, organisation/institute/NGO/government bodies quotes, products, project names, ...
"""
    )
    final_summary = llm_call(prompt=final_prompt, model="gpt-4o-mini", temperature=0, max_tokens_param=target_length)
    final_summary = re.sub(r'\{\[\{(.*?)\}\]\}', r'\1', final_summary)
    final_summary = re.sub(r'\[\{(.*?)\}\]', r'\1', final_summary)
    return final_summary.strip()


def analyze_with_gpt4o(query: str, snippet: str, breadth: int, url: str = "url unknown", title: str = "title unknown") -> dict:
    # If snippet is a callable, call it to get the string.
    if callable(snippet):
        snippet = snippet()
    snippet_words = len(snippet.split())
    
    # Define a word threshold after which we start the chunking summarization.
    CHUNK_WORD_THRESHOLD = 1500
    if snippet_words > CHUNK_WORD_THRESHOLD:
        # Adjust the target_length as needed (here using 2000 tokens as an example).
        snippet = summarize_large_text(snippet, target_length=2000, chunk_size=1000, overlap=200)
        snippet_words = len(snippet.split())
    
    client = os.getenv('OPENAI_API_KEY')  # alternatively, pass your API key here if needed.

    prompt = (f"""
Analyze the following content from a query result:

{snippet}

Research topic:
{query}

url:
{url}

title:
{title}

Instructions:
1.  Relevance: Determine if the content is relevant to the research topic. Answer with a single word: "yes" or "no".

2.  Structure: If the content is relevant, provide a comprehensive summary structured into the following sections. Prioritize extreme conciseness and token efficiency while preserving all key information. Aim for the shortest possible summary that retains all essential facts, figures, arguments, and quotes. The total summary should not exceed 1000 words, but shorter is strongly preferred.
    -   Key Facts (at least 10, if present): List the core factual claims using short, declarative sentences or bullet points. Apply lemmatization and standard abbreviations.
    -   Key Figures: Extract numerical data (statistics, dates, percentages) and include any necessary context (units, references, explanations) required to interpret these numbers. Present them concisely (list or table format).
    -   Key Arguments (at least 5, if present): Identify main arguments or claims. Summarize supporting evidence and counter-arguments concisely.
    -   Key Quotes: Include significant quotes (with the author's name in parenthesis) if people verbatim were mentioned in the query result. Attribute quotes correctly. Paraphrase if needed, indicating that it's a paraphrase. Use symbols (e.g., &, +, ->, =) to conserve tokens.
    -   Structured Summary (10 to 50 sentences): Provide a structured summary that includes anecdotes, people, and locations to ensure the report is relatable.

Note: General Optimization Guidelines:
    -   Lemmatize words (e.g., "running" -> "run").
    -   Use common abbreviations.
    -   Remove redundancy and unnecessary words.
    -   Shorten words carefully (e.g., "information" -> "info") without causing ambiguity.
    -   Use symbols where appropriate.

3.  Follow-up Search Queries: Generate at least {breadth} follow-up search queries relevant to the research topic and the summarized content. Use search operators (AND, OR, quotation marks) as needed.

4. Ensure that the summary length and level of detail is proportional to the source length.
Source length: {snippet_words} words. You may produce a more detailed summary if the text is long.

// Special guidance for follow-up search queries
1. Query Progression:
   - Begin with foundational/conceptual queries
   - Progress to methodological/technical terms
   - Culminate in specialized/applied combinations

2. Term Optimization:
   - Use Boolean logic (AND/OR) strategically
   - Include both general terminology AND domain-specific jargon
   - Add temporal filters when relevant (e.g., "since 2018", "2020-2023")
   - Consider geographical/cultural modifiers if applicable

3. Query Structure:
   - Prioritize conceptual combinations over simple keyword matching
   - Use quotation marks for exact phrases and hyphenation for compound terms
   - Include emerging terminology variants (e.g., "LLMs" OR "large language models")

// IMPORTANT: Format your response as a proper JSON object with these fields:
- "relevant": "yes" or "no"
- url: full url
- title: title
- "summary": {{...your structured summary with all parts...}}
- "followups": [array of follow-up queries]
"""
    )

    try:
        response = llm_call(prompt=prompt, model="gpt-4o-mini", temperature=0, max_tokens_param=8000)
        response = re.sub(r'\{\[\{(.*?)\}\]\}', r'\1', response)
        response = re.sub(r'\[\{(.*?)\}\]', r'\1', response)
        if not response:
            logging.error("analyze_with_gpt4o: Empty response received from API.")
            return {"relevant": "no", "summary": "", "followups": []}
        
        # Check if the response already begins with "yes" or "no" (non-JSON format)
        if response.strip().lower().startswith("yes") or response.strip().lower().startswith("no"):
            # Handle non-JSON format
            lines = response.strip().split("\n")
            relevance = "yes" if lines[0].strip().lower() == "yes" else "no"
            
            # Extract the follow-up queries (usually in brackets at the end)
            followups_match = re.search(r'\[(.*?)\]', response, re.DOTALL)
            followups = []
            if followups_match:
                followups_text = followups_match.group(1)
                # Parse the lines within brackets as comma-separated or quoted items
                followups = [q.strip().strip('"\'') for q in re.findall(r'"([^"]*)"', followups_text)]
                if not followups:  # Try without quotes
                    followups = [q.strip() for q in followups_text.split(",")]
            
            # Everything else is the summary
            summary_text = response
            if followups_match:
                summary_text = response[:followups_match.start()].strip()
            if summary_text.startswith("yes") or summary_text.startswith("no"):
                summary_text = "\n".join(lines[1:]).strip()
            
            return {
                "relevant": relevance,
                "summary": summary_text,
                "followups": followups if followups else []
            }
            
        else:
            # Standard JSON parsing
            # Remove Markdown code fences if present
            if response.startswith("```"):
                response = re.sub(r"^```(json)?", "", response)
                response = re.sub(r"```$", "", response).strip()
            response = response.strip()
            # Optionally remove any start/end markers like "json" if present:
            if response.lower().startswith("json"):
                response = response[4:].strip()
            try:
                result = json.loads(response)
                return result
            except json.JSONDecodeError:
                # If JSON parsing fails, try to extract the information using regex
                logging.warning("JSON parsing failed, attempting regex extraction")
                relevance_match = re.search(r'"relevant":\s*"(yes|no)"', response, re.IGNORECASE)
                relevance = relevance_match.group(1) if relevance_match else "no"
                
                # Extract follow-up queries using regex
                followups_match = re.search(r'"followups":\s*\[(.*?)\]', response, re.DOTALL)
                followups = []
                if followups_match:
                    followups_text = followups_match.group(1)
                    followups = [q.strip().strip('"\'') for q in re.findall(r'"([^"]*)"', followups_text)]
                
                # Extract summary (everything else)
                summary = response
                
                return {
                    "relevant": relevance,
                    "summary": summary,
                    "followups": followups
                }
                
    except Exception as e:
        logging.error(f"analyze_with_gpt4o error: {e}")
        return {"relevant": "no", "summary": "", "followups": []}

def get_random_header():
    headers = [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36",
        "Mozilla/5.0 (X11; Ubuntu; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
        "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"
    ]
    return random.choice(headers)

def process_url(url: str, retries: int = 3, timeout: int = 15) -> str:
    """
    Process a URL with multiple user agents and retries to handle 403 errors
    and other common web scraping issues.
    
    Args:
        url: The URL to retrieve content from
        retries: Number of retry attempts (with different user agents)
        timeout: Connection timeout in seconds
        
    Returns:
        The page content as a string, or an error message
    """
    if url.lower().endswith(".pdf"):
        return process_pdf(url)
        
    user_agents = [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/91.0.864.59 Safari/537.36",
        "Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1"
    ]
    
    referrers = [
        "https://www.google.com/",
        "https://www.bing.com/",
        "https://search.yahoo.com/",
        "https://duckduckgo.com/",
        "https://www.baidu.com/"
    ]
    
    for attempt in range(retries):
        try:
            # Choose a different user agent and referrer for each attempt
            headers = {
                "User-Agent": user_agents[attempt % len(user_agents)],
                "Referer": referrers[attempt % len(referrers)],
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                "Accept-Language": "en-US,en;q=0.5",
                "Accept-Encoding": "gzip, deflate, br",
                "Connection": "keep-alive",
                "Upgrade-Insecure-Requests": "1",
                "Cache-Control": "max-age=0"
            }
            
            # Add a delay between attempts that increases with each retry
            if attempt > 0:
                delay = random.uniform(2, 5) * attempt
                logging.info(f"Retry {attempt+1}/{retries} for {url} - waiting {delay:.1f} seconds")
                time.sleep(delay)
            
            response = requests.get(url, headers=headers, timeout=timeout)
            
            if response.status_code == 200:
                logging.info(f"Successfully retrieved content from {url} on attempt {attempt+1}")
                return response.text
            elif response.status_code == 403:
                logging.warning(f"403 Forbidden on attempt {attempt+1} for {url}, trying different user agent")
                continue
            elif response.status_code == 404:
                return f"Error: Page not found (404) for {url}"
            else:
                response.raise_for_status()
                
        except requests.exceptions.Timeout:
            logging.warning(f"Timeout on attempt {attempt+1} for {url}")
            if attempt == retries - 1:
                return f"Error: Timeout after {retries} attempts for {url}"
                
        except requests.exceptions.TooManyRedirects:
            return f"Error: Too many redirects for {url}"
            
        except requests.exceptions.ConnectionError:
            logging.warning(f"Connection error on attempt {attempt+1} for {url}")
            if attempt == retries - 1:
                return f"Error: Could not connect to {url} after {retries} attempts"
                
        except Exception as e:
            logging.error(f"Error retrieving content from {url} (attempt {attempt+1}): {e}")
            if attempt == retries - 1:
                return f"Error accessing URL: {str(e)}"
    
    return f"Content could not be retrieved from {url} after {retries} attempts"

def clean_content(raw_content: str) -> str:
    # Parse HTML using BeautifulSoup (if not HTML, it will safely return the text)
    soup = BeautifulSoup(raw_content, "html.parser")
    
    # Remove unwanted elements by tag name
    for unwanted in soup(["script", "style", "header", "footer", "nav", "aside"]):
        unwanted.decompose()

    # Optionally, remove any tags with common advert or boilerplate keywords in the class or id:
    for tag in soup.find_all(attrs={"class": re.compile("ad|banner|popup", re.IGNORECASE)}):
        tag.decompose()
    for tag in soup.find_all(attrs={"id": re.compile("ad|banner|popup", re.IGNORECASE)}):
        tag.decompose()
        
    # Extract text and collapse multiple spaces/newlines.
    text = soup.get_text(separator=" ")
    text = re.sub(r'\s+', ' ', text).strip()
    
    # Additionally, remove any obfuscating strings (example: repeated symbols) if known.
    text = re.sub(r'[\u2022\u2023]+', '', text)  # remove bullet point symbols, adjust as needed

    return text

def display_image():
    return "visual.png"

def parse_link_request(user_message: str):
    # Look for a URL pattern in the message (simple regex for http/https)
    url_regex = r"(https?://\S+)"
    match = re.search(url_regex, user_message)
    if match:
        url = match.group(0)
        # Remove URL from the message to extract the question
        question = user_message.replace(url, "").strip()
        # Optionally, remove trigger words like 'open this link:' if needed
        question = re.sub(r"open\s*this\s*link[:\s]*", "", question, flags=re.I).strip()
        return url, question
    return None, user_message

def handle_link_request(user_message: str):
    # Parse the message to extract URL and question
    url, specific_question = parse_link_request(user_message)
    if not url:
        return "No valid URL was detected. Please include a link."
    
    # Fetch content from the URL
    fetched_content = process_url(url)
    if fetched_content.startswith("Error"):
        return f"Error fetching content from the URL: {fetched_content}"
    
    # Build a prompt to answer the specific question using fetched content
    prompt = f"""Using the details from the following content, answer the question in detail.
Content:\n{fetched_content}
Question: {specific_question}
Answer:"""
    
    answer = llm_call(prompt=prompt, model="o3-mini", temperature=0, max_tokens_param=1000)
    return answer

def clean_llm_response(response: str) -> str:
    """
    Clean the raw LLM response by removing code fences and ensuring
    that the result is valid JSON.
    """
    # Remove any leading/trailing whitespace and code fence markers.
    cleaned = response.strip()
    # Remove code fences (handle cases where there might be a language hint as well).
    cleaned = re.sub(r'^```(?:json)?', '', cleaned)
    cleaned = re.sub(r'```$', '', cleaned)
    
    # Replace newline characters with spaces.
    cleaned = cleaned.replace("\n", " ")
    # Collapse multiple spaces into one.
    cleaned = re.sub(r'\s+', ' ', cleaned)
    
    return cleaned.strip()

# ============================================================================= Improve

def remove_text_from_html(report_html: str, text_to_remove: str) -> str:
    soup = BeautifulSoup(report_html, "html.parser")
    # Iterate over all tags in the document
    for tag in soup.find_all():
        # Get the text contained (stripped)
        tag_text = tag.get_text(strip=True)
        if tag_text == text_to_remove:
            # The only content of this tag is text_to_remove; remove the entire tag.
            tag.decompose()
        else:
            # Otherwise, update all NavigableStrings in that tag.
            # (This covers the case where the text appears within more content.)
            for child in tag.find_all(string=True):
                if text_to_remove in child:
                    new_text = child.replace(text_to_remove, "")
                    child.replace_with(new_text)
    return str(soup)

def fix_visual_after_section(report_html: str, section_title: str, extra_instructions: str) -> str:
    """
    Given a report HTML, a target section name (from inside [[...]]), and extra instructions,
    this function finds the first header (h1–h4) that contains the section name (ignoring common TOC headers),
    then finds the first <iframe> following that header.
    It sends the current iframe 'srcdoc' (which should contain a mermaid diagram)
    to the LLM with extra instructions and expects back a corrected mermaid code (starting with a mermaid diagram keyword).
    It then rebuilds and reassigns the iframe's srcdoc with the improved code.
    The text within the mermaid code should not contain [ ], this would be interpreted otherwise. Use it wisely
    Make sure there's no parenthesis or line break in the mermaid code content.
    """
    soup = BeautifulSoup(report_html, "html.parser")
    header = None
    # Look for header tags (h1–h4) that contain the section title (ignoring "table of contents")
    for tag in soup.find_all(["h1", "h2", "h3", "h4"]):
        header_text = tag.get_text(strip=True)
        if section_title.lower() in header_text.lower() and "table of contents" not in header_text.lower():
            header = tag
            break
    if not header:
        logging.error(f"fix_visual_after_section: Section '{section_title}' not found.")
        return report_html

    # Find the first <iframe> after the header
    iframe = header.find_next("iframe")
    if not iframe or not iframe.has_attr("srcdoc"):
        logging.error(f"fix_visual_after_section: No <iframe> found after section '{section_title}'.")
        return report_html

    current_code = iframe["srcdoc"]
    logging.info(f"fix_visual_after_section: Current iframe srcdoc length: {len(current_code)}")
    
    # Build prompt: ask the LLM to simplify/adjust the mermaid code.
    prompt = f"""Improve the following mermaid visualization code.
Extra instructions: {extra_instructions}
Current mermaid code:
{current_code}

Return only the improved mermaid code (the content that should go inside the <div class="visual-frame">...</div>) with no extra wrapping HTML or commentary.

// Important
- No introduction, conclusions or code fences -> Output the result directly - create only the content for the mermaid
- Do no put any comment of #color coding and classes inside the mermaid code generated, it's supposed to be only focused on the mermaid code required to render it
- Avoid the formatting commands (ex: %%...%% ) and do not use classDef, class or linkStyle for formatting
- Do not skip lines or add line breaks in the content of the mermaid code
- The text within the mermaid code should not contain [ ], this would be interpreted otherwise. Use it wisely
- Do not add items in the mermaid code between parenthesis ex: [xxx(yyy)] , rather use [xxx - yyy] / Do not put it in parenthesis, even after the hyphen - or for units.
- If you need to add a remark in the code, do it like this after an hyphen: "[abc - 95% Context Coverage]". Simply don't use parenthesis except for the below prescribed formatting, mermaid if very format sensitive
Example: do not write:
G --> H[Performance Evaluation (45% Speed Improvement, 35% Risk Profiling, 50% Fraud Detection)]
Instead, use
G --> H[Performance Evaluation - 45% Speed Improvement, 35% Risk Profiling, 50% Fraud Detection]
- Do not add any formatting reference in the content of the mermaid code (ex - green ...), this will lead to errors when submitted to the mermaid api
- Do not use the pipe symbol | in the mermaid code, it would create an error 
- Do not use the gantt chart visual placeholder to generate a graph (ex: barchart), Gantt charts should only be used for timelines / project plans
Note: Visual placeholders are solely supposed to generate diagrams not for graphs.
- Do not use the quotation mark inside the text of the diagram 
- Do not use commas in the content of each object / nodes, they will create rendering issues
- Do not use any [x] reference notation for citations (ex: [1], [2], ...) inside the mermaid code - we will manage sources separately

// Examples
Note: Pay attention for each example to what type of parenthesis / bracket is used and respect it scrupulously

-- flowchart --
Important:
- If the flow is "deeper" than broad (>3 levels), choose TD (Top Down) ==> TD is the default one if you have a doubt
- If the flow is "broader" than deep (>3 branches at the same level), choose LR (Left Right)
- There can be mix of flows that can be elaborate
- Take in consideration that the resulting iframe content will render 1200px width and and 700px high. Make the best of the areas real-estate.

Top Down:

flowchart TD
    A[Christmas] -->|Get money| B(Go shopping)
    B --> C{{Let me think}}
    C -->|One| D[Laptop]
    C -->|Two| E[iPhone]
    C -->|Three| F[fa:fa-car Car]


Left Right:

flowchart LR
    A[Anticipated AI Advancements by 2025]
    
    subgraph Infinite_Context [Infinite Context Framework]
      I1[Token Limits: Unlimited]
      I2[Summarization Accuracy: Significantly Improved]
      I3[Industry Impact: Legal, Academic, IoT]
      I4[Use-case Efficiency: High Impact]
      I5[Integration with Emerging Reasoning Models & Agentic Architecture]
    end

    subgraph Traditional_Context [Traditional Fixed Context Windows]
      T1[Token Limits: 2048-4096]
      T2[Summarization Accuracy: Moderate]
      T3[Industry Applications: Limited Scope]
      T4[Use-case Impact: Constrained]
    end

    subgraph Comparative_Innovations [Comparative Metrics & Emerging Technologies]
      C1[Test Time Compute Enhancements]
      C2[Innovations: VJEPA, Synthetic Data, Deepseek]
      C3[New Paradigms: TTT, Novel Architectural Approaches]
      C4[Disruption Factors: Deployment Speed, Employment Impact, Lower Barriers]
    end

    A --> Infinite_Context
    A --> Traditional_Context
    A --> Comparative_Innovations

    class I1,I2,I3,I4,I5 infinite;
    class T1,T2,T3,T4 trad;
    class C1,C2,C3,C4 comp;

Mixed: For elaborate visuals
// example 1

flowchart LR
    subgraph subgraph1
        direction TB
        top1[top] --> bottom1[bottom]
    end
    subgraph subgraph2
        direction TB
        top2[top] --> bottom2[bottom]
    end
    %% ^ These subgraphs are identical, except for the links to them:

    %% Link *to* subgraph1: subgraph1 direction is maintained
    outside --> subgraph1
    %% Link *within* subgraph2:
    %% subgraph2 inherits the direction of the top-level graph (LR)
    outside ---> top2

// example 2
flowchart LR
subgraph "One"
  a("`The **cat**
  in the hat`") -- "edge label" --> b{{"`The **dog** in the hog`"}}
end
subgraph "`**Two**`"
  c("`The **cat**
  in the hat`") -- "`Bold **edge label**`" --> d("The dog in the hog")
end

-- sequence --
sequenceDiagram
    Alice->>+John: Hello John, how are you?
    Alice->>+John: John, can you hear me?
    John-->>-Alice: Hi Alice, I can hear you!
    John-->>-Alice: I feel great!

-- gantt --
gantt
    title A Gantt Diagram
    dateFormat  YYYY-MM-DD
    section Section
    A task           :a1, 2014-01-01, 30d
    Another task     :after a1  , 20d
    section Another
    Task in sec      :2014-01-12  , 12d
    another task      : 24d

-- pie --
pie title Pets adopted by volunteers
    "Dogs" : 386
    "Cats" : 85
    "Rats" : 15

-- mindmap --
mindmap
  root[mindmap]
    Origins
      Long history
      Popularisation
        British popular psychology author Tony Buzan
    Research
      On effectivness
      On Automatic creation
        Uses
            Creative techniques
            Strategic planning
            Argument mapping
    Tools
      Pen and paper
      Mermaid
"""
    
    improved_code = llm_call(prompt=prompt, model="o3-mini", temperature=0, max_tokens_param=1500)
    # Remove code fence markers and all leading whitespace
    improved_code = improved_code.replace("```", "").lstrip()
    logging.info(f"fix_visual_after_section: Improved code received from LLM:\n{improved_code}")
    
    # Verify that the result starts with a valid mermaid diagram keyword.
    valid_keywords = ["mindmap", "flowchart", "sequence", "gantt", "pie"]
    if not any(improved_code.lower().startswith(keyword) for keyword in valid_keywords):
        logging.error("fix_visual_after_section: Improved code does not start with a valid mermaid keyword.")
        return report_html

    # Rebuild the srcdoc using the improved mermaid code.
    new_srcdoc = f"""<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/mermaid@10.4.0/dist/mermaid.min.js"></script>
        <script>
            mermaid.initialize({{ startOnLoad: true, securityLevel: "loose", theme: "default" }});
        </script>
        <style>
            .mermaid {{
                background-color: #f7f2f2;
                color: black;
                padding: 10px;
                border-radius: 5px;
                border: 1px solid #ccc;
                max-width: 1200px;
                margin: 0 auto;
                min-height: 400px;
            }}
        </style>
    </head>
    <body>
        <div class="visual-frame">
{improved_code}
        </div>
    </body>
</html>"""

    # Remove parenthesis in mermaid formatting
    new_srcdoc = re.sub(
        r"<html>\.\.\.\[\.\.\.\((.*?)\)\] </html>",
        r"<html>...[...- \1] </html>",
        new_srcdoc
    )
    iframe["srcdoc"] = new_srcdoc
    logging.info("fix_visual_after_section: Successfully updated the iframe srcdoc with improved mermaid code.")
    return str(soup)

def snippet_in_tag(tag: Tag, snippet: str) -> bool:
    """
    Check if the snippet is found in a tag's visible text.
    If the tag is an iframe with a srcdoc attribute, also check the unparsed srcdoc content.
    """
    # Check tag's normal text.
    if tag.get_text() and snippet in tag.get_text():
        return True
    # For iframes, check the srcdoc attribute.
    if tag.name.lower() == "iframe" and tag.has_attr("srcdoc"):
        srcdoc = tag["srcdoc"]
        # Parse the srcdoc content.
        srcdoc_soup = BeautifulSoup(srcdoc, "html.parser")
        if srcdoc_soup.get_text() and snippet in srcdoc_soup.get_text():
            return True
    return False

def expand_snippet_area(soup: BeautifulSoup, snippet: str) -> Tag:
    allowed_tags = {"div", "table"}

    logging.info("Searching for all elements containing the snippet: '%s'", snippet)
    # Get all tags where the snippet is found either in the text or inside an iframe's srcdoc.
    candidates = soup.find_all(lambda tag: snippet_in_tag(tag, snippet))
    if not candidates:
        logging.info("No element containing the snippet was found. Returning None.")
        return None

    # Choose the candidate with the greatest depth (i.e. the most ancestors).
    candidate = max(candidates, key=lambda tag: len(list(tag.parents)))
    logging.info("Candidate element selected based on depth (<%s>): %s", candidate.name, candidate)

    iframe_candidate = None
    allowed_candidate = None

    # Iterate upward from the candidate's direct parent.
    current = candidate.parent
    while current is not None and current.name.lower() != "body":
        logging.info("Evaluating parent element: <%s>", current.name)
        tag_name = current.name.lower()
        if tag_name == "iframe":
            iframe_candidate = current
            logging.info("Found an <iframe> container; updating iframe_candidate.")
        elif tag_name in allowed_tags and allowed_candidate is None:
            allowed_candidate = current
            logging.info("Found allowed container <%s>; setting allowed_candidate.", tag_name)
        current = current.parent

    if iframe_candidate is not None:
        logging.info("Returning the iframe container.")
        return iframe_candidate
    elif allowed_candidate is not None:
        logging.info("No iframe found; returning the first allowed container (div/table).")
        return allowed_candidate
    else:
        logging.info("No iframe, div, or table container found; returning candidate element.")
        return candidate

def fine_tune_report(adjustment_request: str, openai_api_key: str, serpapi_api_key: str, report_html: str, 
                     initial_request: str, qa: str, target_style: str, knowledge_crumbs: str, 
                     complementary_guidance: str) -> (str, str):
    # unescape report
    report_html = html.unescape(report_html)
    
    # Quick adjustments
    if "remove the following text:" in adjustment_request.lower():
        # Extract the target text and process removal directly.
        text_to_remove_match = re.search(r"remove the following text:\s*\[\[([^\]]+)\]\]", adjustment_request, re.I)
        if text_to_remove_match:
            text_to_remove = text_to_remove_match.group(1).strip()
            # Return the report after locally removing the text.
            return remove_text_from_html(report_html, text_to_remove), qa
    
    os.environ["OPENAI_API_KEY"] = openai_api_key
    os.environ["SERPAPI_API_KEY"] = serpapi_api_key

    logging.info("fine_tune_report: Starting fine-tuning process based on the adjustment request.")

    # Step 1: (LLM call to get unique strings)
    
    prompt_identify = (f"""
You are a meticulous technical editor.

Below is the full report HTML and a user adjustment request.
Extract one or more unique plain-text string(s) (without any HTML tags or formatting) that uniquely appear in the area(s) targeted by the adjustment request.

// Examples

    if the user request to "Add xyz in the conclusion", the unique string to identify should be specific to the conclusion
    if the user request to "correct the graph after section 1.2", the unique string should be one of the string that appear specifically in the graph after section 1.2 (ex: the title)
    if the user request is "Remove any mention about the car industry", the unique string(s) should be a sentence that would be in a paragraph of the report that would talk about car industry
    --> The unique string is what would allow to identify precisely through a search the section targeted by the user request, it has to be concise and unique.

Output them in a JSON object with the key "identified_unique_strings" mapped to a list of strings.
Ensure these strings exactly match the content in the report.

Important:
- If the unique string appears within a code snippet (like a javascript graph or a mermaid diagram), do not use the code as part of the snippet. For example, instead of 
 "A[Fundamental AI Research - Emerging Theories and Paradigms] --> B[Algorithm Innovation - Novel ML and NLP Models]"
simply use "Fundamental AI Research - Emerging Theories and Paradigms".
- The unique string identified should not contain a line break (ex: <br> or \n), take only a snippet that originally didn't contain a line break

Full Report HTML:
{report_html}

User Adjustment Request:
{adjustment_request}

Only output valid JSON."""
    )
    response_identify = llm_call(prompt=prompt_identify, model="o3-mini", temperature=0, max_tokens_param=10000)
    logging.info("fine_tune_report: Raw unique string identification response: %s", response_identify)

    try:
        response_identify = clean_llm_response(response_identify.strip().strip("```"))
        id_data = json.loads(response_identify)
        unique_strings = id_data.get("identified_unique_strings", [])
    except Exception as e:
        logging.error("fine_tune_report: Error parsing unique strings JSON: %s", e)
        unique_strings = []
    
    if not unique_strings:
        logging.warning("fine_tune_report: No unique strings were identified for adjustment. Returning original report.")
        return report_html, qa
    
    # Step 2: Parse the report HTML once.
    soup = BeautifulSoup(report_html, "html.parser")
    corrections_summary = []
    
    for uniq_str in unique_strings:
        uniq_str = uniq_str.strip()
        logging.info("fine_tune_report: Processing unique string: '%s'", uniq_str)
        # Use expand_snippet_area to get the container Tag directly.
        container_tag = expand_snippet_area(soup, uniq_str)
        if container_tag is None:
            logging.warning("fine_tune_report: Could not locate a container for unique string: '%s'", uniq_str)
            continue

        if "remove" in adjustment_request.lower():
            # Attempt to extract target phrase from the removal instruction.
            m = re.search(r'remove\s+(?:the\s+duplicate\s+)?mention\s+of\s+the\s+source:\s*(.+)', adjustment_request.lower())
            if m:
                target = m.group(1).strip()
                text_lower = container_tag.get_text().lower()
                if target in text_lower:
                    # Remove this container entirely.
                    container_tag.replace_with(BeautifulSoup("", "html.parser"))
                    logging.info("fine_tune_report: Removed section containing target '%s'", target)
                    corrections_summary.append(f"Section containing {target} removed as per request.")
                    continue  # Skip further processing of this snippet.
        
        original_container_html = str(container_tag)
        logging.info("fine_tune_report: Found container for unique string adjustment:\n\n%s\n", original_container_html)

        # Step 3: Call the LLM to adjust this container.
        prompt_adjust = (f"""
You are a technical editor.
Given the following HTML container (with its outer tags) extracted from a larger report and based on the user adjustment request,
produce a corrected version by making only the necessary changes. Preserve inline citations, formatting, and context.
The updated version will be put back in the exact same location and must have the same outer tags.
If the original container is a mermaid code, make sure the new version does not include parenthesis or line break in the content of the mermaid pseudo-code.

- Overall Report HTML:
{report_html}
- Knowledge Crumbs:
{knowledge_crumbs}

- Original Container to Adjust:
{original_container_html}

- User Adjustment Request:
{adjustment_request}

Additional Guidance:
- Target Style: {target_style}
- Complementary Guidance:
{complementary_guidance}

Ensure the updated content remains consistent with the overall report style.
Output a JSON object with exactly two keys:
- "improved" (the corrected container's full HTML) and
- "summary" (a brief explanation of the changes)

Only output valid JSON with no comments or code fences."""
        )
        
        response_adjust = llm_call(prompt=prompt_adjust, model="o3-mini", temperature=0, max_tokens_param=10000)
        logging.info("fine_tune_report: Raw container adjustment response: %s", response_adjust)
        try:
            response_adjust = clean_llm_response(response_adjust.strip().strip("```"))
            logging.info("Cleaned container adjustment response: %s", response_adjust)
            adjust_data = json.loads(response_adjust)
            corrected_container = adjust_data.get("improved", "").strip()
            container_summary = adjust_data.get("summary", "").strip()
        except Exception as e:
            logging.error("fine_tune_report: Error parsing container adjustment JSON: %s", e)
            continue

        if not corrected_container:
            logging.warning("fine_tune_report: No improved container was generated; skipping correction for this container.")
            continue

        corrections_summary.append(f"Container corrected: {container_summary}")

        # Step 4: Replace the original container with the updated one in our soup.
        container_tag.replace_with(BeautifulSoup(corrected_container, "html.parser"))
        logging.info("fine_tune_report: Updated container re-injected.")

    updated_report_html = str(soup)
    
    # Step 5 (and 6): Update the reference table if needed.
    prompt_refs = (
        f"\nYou are a technical editor.\n\n"
        "Review the following updated report HTML. If any new inline citations (e.g., [x]) have been added that are not in the original reference table,\n"
        "generate an updated References Summary Table as valid HTML. Output only the updated table without any additional comments.\n\n"
        f"Updated Report HTML:\n{updated_report_html}"
    )
    # Increase token limit to 1500 for this call
    updated_refs = llm_call(prompt=prompt_refs, model="o3-mini", temperature=0, max_tokens_param=1500)
    updated_refs = updated_refs.strip().strip("```").strip()
    
    if updated_refs and not updated_refs.lower().startswith("error: empty response"):
        soup_updated = BeautifulSoup(updated_report_html, "html.parser")
        ref_heading = soup_updated.find(lambda tag: tag.name in ["h1", "h2", "h3", "h4"] and "references summary table" in tag.get_text(strip=True).lower())
        if ref_heading:
            next_sibling = ref_heading.find_next_sibling()
            if next_sibling:
                try:
                    new_ref_html = BeautifulSoup(updated_refs, "html.parser")
                    next_sibling.replace_with(new_ref_html)
                    logging.info("fine_tune_report: Reference table updated successfully.")
                except Exception as e:
                    logging.error("fine_tune_report: Error updating reference table: %s", e)
            else:
                logging.info("fine_tune_report: No sibling after reference heading; skipping reference update.")
            updated_report_html = str(soup_updated)
        else:
            logging.info("fine_tune_report: No reference table heading found; reference update skipped.")
    else:
        logging.info("fine_tune_report: No valid updated reference table returned; leaving unchanged.")

    global_summary = "Corrections Applied Based on User Request:\n" + "\n".join(corrections_summary)
    updated_qa = qa.strip() + "\n----------\n" + global_summary

    logging.info("fine_tune_report: Fine-tuning complete.")
    return updated_report_html, updated_qa

def suggest_improvements(report_html: str, openai_api_key: str, serpapi_api_key: str, initial_query: str, clarification_text: str, crumbs_box: str, additional_clarifications: str) -> str:
    os.environ["OPENAI_API_KEY"] = openai_api_key
    os.environ["SERPAPI_API_KEY"] = serpapi_api_key

    prompt = (f"""
You are a technical editor.
Based on the following full HTML report, generate improvement suggestions - at least 3."
Format each proposal as a numbered list item in the following style:\n"
Examples:
1) in the section xyz, adjust ...
2) after the paragraph abc, detail the graph further ...
3) in the focus placeholder xxx, add a mention about ...
4) make a reference to ... in the section 3.2
...
n) final improvement suggestion...

// Pay attention to:
- The initial user request:
{initial_query}
- The Q&A:
{clarification_text}
- the knowledge crumbs (from search):
{crumbs_box}
- additional clarifications form the user:
{additional_clarifications}

Note: Discard impractical recommendation (in pdf or gr.html)
Only output the suggestions exactly as a numbered list (text)

Full Report HTML:
{report_html}

Now provide your suggestions."""
    )
    suggestions = llm_call(prompt=prompt, model="o3-mini", temperature=0, max_tokens_param=1000)
    return suggestions.strip().strip("```").strip()

def improve_report_from_chat(user_message: str, chat_history: list, report_text: str, crumbs_text: str, style: str, initial_request: str, qa: str, complementary_guidance: str):
    adjustment_request = user_message.replace("@improve", "").strip()

    report_text = html.unescape(report_text)
    
    # --- CASE 1: Removal request ---
    removal_match = re.search(r"remove the following text:\s*\[\[([^\]]+)\]\]", adjustment_request, re.I)
    if removal_match:
        text_to_remove = removal_match.group(1).strip()
        updated_report = remove_text_from_html(report_text, text_to_remove)
        answer = f"Removed text: '{text_to_remove}' from the report."
        chat_history.append([user_message, answer])
        return chat_history, "", updated_report

    # --- CASE 2: Visual fix request ---
    visual_fix_match = re.search(r"fix visual after section\s+\[\[([^\]]+)\]\](?::\s*(.*))?", adjustment_request, re.I)
    if visual_fix_match:
        section_name = visual_fix_match.group(1).strip()
        extra_instructions = visual_fix_match.group(2).strip() if visual_fix_match.group(2) else ""
        updated_report = fix_visual_after_section(report_text, section_name, extra_instructions)
        answer = f"Fixed visual after section '{section_name}'."
        chat_history.append([user_message, answer])
        return chat_history, "", updated_report

    # --- DEFAULT: Proceed with existing LLM-based improvement (fine_tune_report) ---
    updated_report, _ = fine_tune_report(
        adjustment_request,
        os.getenv("OPENAI_API_KEY"),
        os.getenv("SERPAPI_API_KEY"),
        report_text,
        initial_request=initial_request,
        qa=qa,
        target_style=style,
        knowledge_crumbs=crumbs_text,
        complementary_guidance=complementary_guidance
    )
    answer = f"Report updated with your improvement: {adjustment_request}"
    chat_history.append([user_message, answer])
    return chat_history, "", updated_report

# ============================================================================= Expand

def get_max_reference(report_html: str) -> int:
    """
    Searches the provided report HTML for the References Summary Table and returns
    the maximum reference number currently used.
    """
    soup_ = BeautifulSoup(report_html, "html.parser")
    max_ref = 0
    # Locate a heading that includes "references summary table" (case insensitive)
    ref_heading = soup_.find(lambda tag: tag.name in ["h1", "h2", "h3", "h4"] and "references summary table" in tag.get_text(strip=True).lower())
    if ref_heading:
        next_sibling = ref_heading.find_next_sibling()
        if next_sibling:
            text = next_sibling.get_text(separator="\n")
            for line in text.splitlines():
                m = re.match(r'\s*(\d+)\s*\|', line)
                if m:
                    num = int(m.group(1))
                    max_ref = max(max_ref, num)
    return max_ref

def expand_report(expansion_request: str, openai_api_key: str, serpapi_api_key: str, report_html: str, 
                  initial_request: str, qa: str, target_style: str, knowledge_crumbs: str, 
                  complementary_guidance: str) -> (str, str):
    os.environ["OPENAI_API_KEY"] = openai_api_key
    os.environ["SERPAPI_API_KEY"] = serpapi_api_key

    # unescape report
    report_html = html.unescape(report_html)
    
    logging.info("expansion_report: Starting fine-tuning process based on the adjustment request.")

    # Step 1: Identify unique strings in the targeted sections.
    prompt_identify = (f"""
You are a meticulous technical editor.

Below is the full report HTML and a user expansion request.
Extract one or more unique plain-text string(s) (without any HTML tags or formatting) that uniquely appear in the area(s) targeted by the expansion request.

// Examples:
    - If the user requests "Expand xyz in the conclusion", the unique string should be specific to the conclusion.
    - If the user requests "Elaborate on the graph after section 1.2", the unique string should be one of the strings that appear specifically in that graph (e.g., its title).
    - If the user requests "Add any mention about the car industry", the unique string should be a sentence that appears in a paragraph discussing that industry.
--> The unique string is what allows you to identify precisely the targeted section; it must be concise and unique.

Output them in a JSON object with the key "identified_unique_strings" mapped to a list of strings.
Ensure these strings exactly match the content in the report.

Important:
- If the unique string appears within a code snippet (like a javascript graph or a mermaid diagram), do not use the code as part of the snippet. For example, instead of 
 "A[Fundamental AI Research - Emerging Theories and Paradigms] --> B[Algorithm Innovation - Novel ML and NLP Models]"
simply use "Fundamental AI Research - Emerging Theories and Paradigms".
- The unique string identified should not contain a line break (ex: <br> or \n), take only a snippet that originally didn't contain a line break


Full Report HTML:
{report_html}

User Expansion Request:
{expansion_request}

Only output valid JSON.
""")
    response_identify = llm_call(prompt=prompt_identify, model="o3-mini", temperature=0, max_tokens_param=5000)
    logging.info("expansion_report: Raw unique string identification response: %s", response_identify)

    try:
        response_identify = clean_llm_response(response_identify.strip().strip("```"))
        id_data = json.loads(response_identify)
        unique_strings = id_data.get("identified_unique_strings", [])
    except Exception as e:
        logging.error("expansion_report: Error parsing unique strings JSON: %s", e)
        unique_strings = []

    if not unique_strings:
        logging.warning("expansion_report: No unique strings were identified for adjustment. Returning original report.")
        return report_html, qa

    # Determine current maximum reference number.
    current_max_ref = get_max_reference(report_html)
    logging.info(f"expansion_report: Current max reference number is {current_max_ref}")

    new_references_list = []
    soup = BeautifulSoup(report_html, "html.parser")
    corrections_summary = []

    for uniq_str in unique_strings:
        uniq_str = uniq_str.strip()
        logging.info("expansion_report: Processing unique string: '%s'", uniq_str)
        container_tag = expand_snippet_area(soup, uniq_str)
        if container_tag is None:
            logging.warning("expansion_report: Could not locate a container for unique string: '%s'", uniq_str)
            continue

        original_container_html = str(container_tag)
        logging.info("expansion_report: Found container for unique string adjustment (length %d): %s", 
                     len(original_container_html), original_container_html[:200])
        
        # Check if it is a removal request.
        if "remove" in expansion_request.lower():
            m = re.search(r'remove\s+(?:the\s+duplicate\s+)?mention\s+of\s+the\s+source:\s*(.+)', expansion_request.lower())
            if m:
                target = m.group(1).strip()
                text_lower = container_tag.get_text().lower()
                if target in text_lower:
                    container_tag.replace_with(BeautifulSoup("", "html.parser"))
                    logging.info("expansion_report: Removed section containing target '%s'", target)
                    corrections_summary.append(f"Section containing {target} removed as per request.")
                    continue
        
        # --- Modified prompt: focus only on the container / snippet.
        prompt_adjust = (f"""
You are a technical editor. You are tasked to read a report and expand a very specific snippet in that report.
Read the report first:

{report_html}
-------------
Having the report in memory and using only the following container snippet (including its outer HTML tags),
produce an expanded version of the container by adding between one and three paragraphs containing additional relevant details.

// IMPORTANT
- DO NOT output the full report; output only the updated snippet with no comments
- DO NOT output the full report; output only the updated snippet with no comments
- DO NOT output the full report; output only the updated snippet with no comments
- No title or subtitles, only the snippet expanded with complementary paragraphs aligned with the user request
- No title or subtitles, only the snippet expanded with complementary paragraphs aligned with the user request
- No title or subtitles, only the snippet expanded with complementary paragraphs aligned with the user request
- Do not refer or duplicate other concepts already addressed in other parts of the report.
- Do not refer or duplicate other concepts already addressed in other parts of the report.
- Do not refer or duplicate other concepts already addressed in other parts of the report.

// Inputs
- Original Container to Adjust:
{original_container_html}

- Full User Expansion Request:
{expansion_request}
Note: the user request is addressing several containers, just apply the guidance that are relevant for this specific container

// Additional Guidance:
- Target Style: {target_style}
- Complementary Guidance: {complementary_guidance}
- Preserve inline citations (formatted as [x]); if new citations are added, list them separately in "newref".
- The citations ref should be aligned with those in the References summary table if the reference used already exists.

// Output
Output a JSON object with exactly three keys:
  "result": the expanded container's HTML (only the snippet),
  "newref": a string with new reference lines (each line formatted as "new reference number | name | author | url"; empty if none),
  "summary": a brief explanation of the changes made.
""")
        response_adjust = llm_call(prompt=prompt_adjust, model="o3-mini", temperature=0, max_tokens_param=10000)
        logging.info("expansion_report: Raw container adjustment response: %s", response_adjust)
        try:
            response_adjust = response_adjust.strip().strip("json").strip("```").strip()
            logging.info("expansion_report: Cleaned container adjustment response: %s", response_adjust)
            adjust_data = json.loads(response_adjust)
            corrected_container = adjust_data.get("result", "").strip()
            new_refs_str = adjust_data.get("newref", "").strip()
            container_summary = adjust_data.get("summary", "").strip()
        except Exception as e:
            logging.error("expansion_report: Error parsing container adjustment JSON: %s", e)
            continue

        if new_refs_str:
            for line in new_refs_str.splitlines():
                line = line.strip()
                if line:
                    ref_parts = line.split("|")
                    if len(ref_parts) >= 4:
                        current_max_ref += 1
                        ref_name = ref_parts[1].strip()
                        ref_author = ref_parts[2].strip()
                        ref_url = ref_parts[3].strip()
                        new_ref_line = f"{current_max_ref} | {ref_name} | {ref_author} | {ref_url}"
                        new_references_list.append(new_ref_line)
                        logging.info("expansion_report: Added new reference: %s", new_ref_line)
        else:
            logging.info("expansion_report: No new references found for this container.")

        corrections_summary.append(f"Container expanded: {container_summary}")
        # Reintegrate: replace the original container with only the expanded snippet.
        new_content_soup = BeautifulSoup(corrected_container, "html.parser")
        if new_content_soup.contents:
            container_tag.replace_with(new_content_soup.contents[0])
        else:
            container_tag.replace_with(new_content_soup)
        logging.info("expansion_report: Updated container re-injected.")

    updated_report_html = str(soup)

    new_refs_text = "\n".join(new_references_list) if new_references_list else ""
    prompt_refs = (
        "\nYou are a technical editor.\n\n"
        "Review the following updated report HTML along with the list of new references added during the expansion process. "
        "Every inline citation found in the updated report must have a corresponding entry in the References Summary Table. "
        "Merge any new references with the existing ones without duplication.\n\n" +
        (f"New References Added:\n{new_refs_text}\n\n" if new_refs_text else "New References Added: None\n\n") +
        f"Updated Report HTML:\n{updated_report_html}\n\n"
        "Output only the updated References Summary Table as valid HTML with no additional commentary."
    )
    updated_refs = llm_call(prompt=prompt_refs, model="o3-mini", temperature=0, max_tokens_param=10000)
    updated_refs = updated_refs.strip().strip("```").strip()

    if updated_refs and not updated_refs.lower().startswith("error: empty response"):
        soup_updated = BeautifulSoup(updated_report_html, "html.parser")
        ref_heading = soup_updated.find(lambda tag: tag.name in ["h1", "h2", "h3", "h4"] and "references summary table" in tag.get_text(strip=True).lower())
        if ref_heading:
            next_sibling = ref_heading.find_next_sibling()
            if next_sibling:
                try:
                    new_ref_html = BeautifulSoup(updated_refs, "html.parser")
                    next_sibling.replace_with(new_ref_html)
                    logging.info("expansion_report: Reference table updated successfully.")
                except Exception as e:
                    logging.error("expansion_report: Error updating reference table: %s", e)
            else:
                logging.info("expansion_report: No sibling after reference heading; skipping reference update.")
            updated_report_html = str(soup_updated)
        else:
            logging.info("expansion_report: No reference table heading found; reference update skipped.")
    else:
        logging.info("expansion_report: No valid updated reference table returned; leaving unchanged.")

    global_summary = "Corrections Applied Based on User Request:\n" + "\n".join(corrections_summary)
    updated_qa = qa.strip() + "\n----------\n" + global_summary
    logging.info("expansion_report: Expansion complete. Summary of changes:\n%s", global_summary)
    return updated_report_html, updated_qa

def expand_report_from_chat(user_message: str, chat_history: list, report_text: str, crumbs_text: str, style: str, initial_request: str, qa: str, complementary_guidance: str):
    """
    If the user enters a message with '@expand', first generate expansion suggestions from the current report.
    Then, use those suggestions (if any) as the expansion instruction to update the report HTML.
    Returns the updated chat history and the updated report.
    """

    # unescape report
    report_text = html.unescape(report_text)
    
    expand_request = user_message.replace("@expand", "").strip()
    
    suggestions = suggest_expansions(report_text, os.getenv("OPENAI_API_KEY"), os.getenv("SERPAPI_API_KEY"),
                                     initial_request, qa, crumbs_text, complementary_guidance)
    
    if suggestions.strip():
        expansion_instruction = suggestions
    else:
        expansion_instruction = expand_request
    
    updated_report, _ = expand_report(
        expansion_instruction,
        os.getenv("OPENAI_API_KEY"),
        os.getenv("SERPAPI_API_KEY"),
        report_text,
        initial_request=initial_request,
        qa=qa,
        target_style=style,
        knowledge_crumbs=crumbs_text,
        complementary_guidance=complementary_guidance
    )
    
    answer = f"Report updated with expansion: {expansion_instruction}"
    chat_history.append([user_message, answer])
    return chat_history, "", updated_report

def suggest_expansions(report_html: str, openai_api_key: str, serpapi_api_key: str, initial_query: str, clarification_text: str, crumbs_box: str, additional_clarifications: str) -> str:
    os.environ["OPENAI_API_KEY"] = openai_api_key
    os.environ["SERPAPI_API_KEY"] = serpapi_api_key

    prompt = (f"""
You are a technical editor.
Based on the following full HTML report, generate expansion suggestions - at least 3."
Format each proposal as a numbered list item in the following style:\n"
Examples:
1) in the section xyz, elaborate on this topic ...
2) after the paragraph abc, develop the graph further to integrate abc...
3) in the focus placeholder xxx, add a paragraph about ...
4) add several pragraphs on ... in the section 3.2
...
n) final expansion suggestion...

// Pay attention to:
- The initial user request:
{initial_query}
- The Q&A:
{clarification_text}
- the knowledge crumbs (from search):
{crumbs_box}
- additional clarifications form the user:
{additional_clarifications}

Note: Discard impractical recommendation (in pdf or gr.html)
Only output the suggestions exactly as a numbered list (text)

Full Report HTML:
{report_html}

// Important
- Your suggestions should not conflict with the content already present in other sections of the report
- This an "expansion", not an "addition", which means you should not ask to add a new section or sub-section but just add a paragraph or two at most
- You're not supposed to add any new title or brand new section
- Focus on the paragraph (within p tag) or sections (between 2 h1 titles - excluding visuals) that are not of the proper dimension for the topic addressed.

Your suggestions will help to make the report more comprehensive and ensure all the relevant knowledge available is used. 
Now provide your suggestions to expand the report."""
    )
    suggestions = llm_call(prompt=prompt, model="o3-mini", temperature=0, max_tokens_param=5000)
    return suggestions.strip().strip("```").strip()

# ============================================================================= Chat

def send_chat_message(user_message, openai_api_key, serpapi_api_key, chat_history, report_text, crumbs_text, style, initial_request, qa, complementary_guidance):
    os.environ["OPENAI_API_KEY"] = openai_api_key
    os.environ["SERPAPI_API_KEY"] = serpapi_api_key

    report_text = html.unescape(report_text)
    
    if "@improve" in user_message.lower():
        return improve_report_from_chat(user_message, chat_history, report_text, crumbs_text, style, initial_request, qa, complementary_guidance)
    elif "@expand" in user_message.lower():
        return expand_report_from_chat(user_message, chat_history, report_text, crumbs_text, style, initial_request, qa, complementary_guidance)
    if "http://" in user_message or "https://" in user_message:
        answer = handle_link_request(user_message)
    else:
        system_prompt = f"""
You are a knowledgeable research assistant.

Based on the following
- Report:
{report_text}

- Source Crumbs:
{crumbs_text}

- User Question:
{user_message}

Provide a response in the desired style: {style}

Your Answer:"""
        answer = llm_call(prompt=system_prompt, model="o3-mini", temperature=0, max_tokens_param=10000)
    updated_history = chat_history + [[user_message, answer]]
    return updated_history, "", report_text

# ============================================================================= Graph placeholder

def generate_graph_snippet(placeholder_text: str, context: str, initial_query: str, crumbs: str) -> str:    
    graph_examples = """
--- Example 1 ---

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Global Comparative Analysis of Ethical Framework Adoption</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 20px;
      }
      h2 {
        text-align: left;
        margin-bottom: 0;
      }
      #chart {
        max-width: 100%;
        max-height: 700px;
      }
      .axis-label {
        font-size: 14px;
        fill: #333;
      }
      .tooltip {
        position: absolute;
        background: #f9f9f9;
        padding: 5px 10px;
        border: 1px solid #d3d3d3;
        border-radius: 4px;
        pointer-events: none;
        font-size: 12px;
      }
    </style>
  </head>
  <body>
    <h2>Global Comparative Analysis of Ethical Framework Adoption</h2>
    <div id="chart"></div>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <script>
      const data = [
        { region: "North America", adoption: 80, initiatives: 5, trust: 75 },
        { region: "Europe",         adoption: 85, initiatives: 6, trust: 80 },
        { region: "Asia",           adoption: 70, initiatives: 4, trust: 65 },
        { region: "South America",  adoption: 60, initiatives: 3, trust: 70 },
        { region: "Africa",         adoption: 50, initiatives: 2, trust: 55 },
        { region: "Oceania",        adoption: 75, initiatives: 4, trust: 78 }
      ];

      const margin = { top: 50, right: 30, bottom: 50, left: 60 };
      const containerWidth = 1100px;
      const width = containerWidth - margin.left - margin.right;
      const height = 700 - margin.top - margin.bottom;
      
      const svg = d3.select("#chart")
                    .append("svg")
                    .attr("width", width + margin.left + margin.right)
                    .attr("height", height + margin.top + margin.bottom)
                    .append("g")
                    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
      
      const x = d3.scaleLinear()
                  .domain([40, 90])
                  .range([0, width]);
      
      const y = d3.scaleLinear()
                  .domain([50, 85])
                  .range([height, 0]);
      
      const r = d3.scaleSqrt()
                  .domain([2, 6])
                  .range([10, 30]);
      
      svg.append("g")
         .attr("transform", "translate(0," + height + ")")
         .call(d3.axisBottom(x));
      
      svg.append("g")
         .call(d3.axisLeft(y));
      
      svg.append("text")
         .attr("class", "axis-label")
         .attr("x", width)
         .attr("y", height + 40)
         .attr("text-anchor", "end")
         .text("Ethical Guideline Adoption (%)");
      
      svg.append("text")
         .attr("class", "axis-label")
         .attr("transform", "rotate(-90)")
         .attr("y", -50)
         .attr("x", 0)
         .attr("text-anchor", "end")
         .text("Public Trust Index");
      
      svg.selectAll("circle")
         .data(data)
         .enter()
         .append("circle")
           .attr("cx", d => x(d.adoption))
           .attr("cy", d => y(d.trust))
           .attr("r", d => r(d.initiatives))
           .attr("fill", "#69b3a2")
           .attr("opacity", 0.7);
      
      const tooltip = d3.select("body")
                        .append("div")
                        .attr("class", "tooltip")
                        .style("opacity", 0);
      
      svg.selectAll("circle")
         .on("mouseover", function(event, d) {
           tooltip.transition()
                  .duration(200)
                  .style("opacity", 0.9);
           tooltip.html("Region: " + d.region + "<br/>Adoption: " + d.adoption + "%<br/>Initiatives: " + d.initiatives + "<br/>Trust: " + d.trust)
                  .style("left", (event.pageX + 10) + "px")
                  .style("top", (event.pageY - 28) + "px");
         })
         .on("mouseout", function() {
           tooltip.transition()
                  .duration(500)
                  .style("opacity", 0);
         });
    </script>
  </body>
</html>


--- Example 2 ---

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hybrid Architectures vs Traditional Deep Learning: Speed and Accuracy Comparison</title>
  <style>
    body {
      margin: 0;
      font-family: Arial, sans-serif;
    }
    h1 {
      text-align: left;
      margin: 20px 0;
    }
    #chart {
      max-width: 100%;
      max-height: 700px;
      margin: auto;
      background-color: #fff;
      border: 1px solid #ddd;
      box-shadow: 0px 0px 8px rgba(0,0,0,0.1);
    }
  </style>
</head>
<body>
  <h1>Hybrid Architectures vs Traditional Deep Learning: Speed and Accuracy</h1>
  <div id="chart"></div>
  <script src="https://d3js.org/d3.v6.min.js"></script>
  <script>
    // Set margins and dimensions
    const margin = { top: 40, right: 40, bottom: 60, left: 60 },
          width = 1100px - margin.left - margin.right,
          height = 600 - margin.top - margin.bottom;

    // Sample benchmarking data
    const data = [
      { arch: "Hybrid Architectures", processing: 110, accuracy: 94, source: "Maryland Graduate" },
      { arch: "Hybrid Architectures", processing: 130, accuracy: 90, source: "Study [2]" },
      { arch: "Hybrid Architectures", processing: 150, accuracy: 88, source: "Study [3]" },
      { arch: "Traditional Deep Learning", processing: 180, accuracy: 85, source: "Study [2]" },
      { arch: "Traditional Deep Learning", processing: 220, accuracy: 80, source: "Study [3]" },
      { arch: "Traditional Deep Learning", processing: 240, accuracy: 83, source: "Maryland Graduate" }
    ];

    // Create SVG container
    const svg = d3.select("#chart")
                  .append("svg")
                  .attr("width", width + margin.left + margin.right)
                  .attr("height", height + margin.top + margin.bottom)
                  .append("g")
                  .attr("transform", `translate(${margin.left},${margin.top})`);

    // Scales
    const x = d3.scaleLinear()
                .domain([d3.min(data, d => d.processing) - 10, d3.max(data, d => d.processing) + 10])
                .range([0, width]);

    const y = d3.scaleLinear()
                .domain([d3.min(data, d => d.accuracy) - 5, d3.max(data, d => d.accuracy) + 5])
                .range([height, 0]);

    // Color scale based on architecture type
    const color = d3.scaleOrdinal()
                    .domain(["Hybrid Architectures", "Traditional Deep Learning"])
                    .range(["#1f77b4", "#ff7f0e"]);

    // Add X Axis
    svg.append("g")
       .attr("class", "axis")
       .attr("transform", `translate(0, ${height})`)
       .call(d3.axisBottom(x));

    // Add Y Axis
    svg.append("g")
       .attr("class", "axis")
       .call(d3.axisLeft(y));

    // X Axis label
    svg.append("text")
       .attr("x", width / 2)
       .attr("y", height + margin.bottom - 10)
       .attr("text-anchor", "middle")
       .text("Processing Time (ms)");

    // Y Axis label
    svg.append("text")
       .attr("transform", "rotate(-90)")
       .attr("x", -height / 2)
       .attr("y", -margin.left + 15)
       .attr("text-anchor", "middle")
       .text("Accuracy (%)");

    // Tooltip
    const tooltip = d3.select("body")
                      .append("div")
                      .style("position", "absolute")
                      .style("padding", "8px")
                      .style("background", "rgba(0,0,0,0.6)")
                      .style("color", "#fff")
                      .style("border-radius", "4px")
                      .style("visibility", "hidden")
                      .style("font-size", "12px");

    // Draw data points
    svg.selectAll("circle")
       .data(data)
       .enter()
       .append("circle")
       .attr("cx", d => x(d.processing))
       .attr("cy", d => y(d.accuracy))
       .attr("r", 7)
       .attr("fill", d => color(d.arch))
       .attr("stroke", "#333")
       .attr("stroke-width", 1)
       .on("mouseover", (event, d) => {
          tooltip.html(`<strong>${d.arch}</strong><br/>Processing: ${d.processing} ms<br/>Accuracy: ${d.accuracy}%<br/>Source: ${d.source}`)
                 .style("visibility", "visible");
       })
       .on("mousemove", (event) => {
          tooltip.style("top", (event.pageY - 10) + "px")
                 .style("left", (event.pageX + 10) + "px");
       })
       .on("mouseout", () => {
          tooltip.style("visibility", "hidden");
       });

    // Legend
    const legendData = color.domain();
    const legend = svg.selectAll(".legend")
                      .data(legendData)
                      .enter()
                      .append("g")
                      .attr("class", "legend")
                      .attr("transform", (d, i) => `translate(0, ${i * 20})`);

    legend.append("rect")
          .attr("x", width - 18)
          .attr("width", 18)
          .attr("height", 18)
          .style("fill", color);

    legend.append("text")
          .attr("x", width - 24)
          .attr("y", 9)
          .attr("dy", ".35em")
          .style("text-anchor", "end")
          .text(d => d);
  </script>
</head>
<body>
</body>
</html>


--- Example 3 ---

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Performance Improvements in AI Applications: Healthcare, Education, and Finance</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 10px;
      background-color: #f5f5f5;
    }
    h1 {
      text-align: left;
      margin-bottom: 10px;
      color: #333;
    }
    #chart {
      max-width: 100%;
      max-height: 700px;
      background-color: #fff;
      border: 1px solid #ddd;
      box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
      margin: auto;
    }
    .axis text {
      fill: #555;
    }
    .axis path,
    .axis line {
      stroke: #ccc;
    }
  </style>
</head>
<body>
  <h1>Performance Improvements in AI Applications</h1>
  <div id="chart"></div>
  <script src="https://d3js.org/d3.v7.min.js"></script>
  <script>
    const data = [
      { sector: "Healthcare", metric: "Accuracy", value: 87 },
      { sector: "Healthcare", metric: "Speed", value: 72 },
      { sector: "Healthcare", metric: "Cost Efficiency", value: 95 },
      { sector: "Education", metric: "Accuracy", value: 80 },
      { sector: "Education", metric: "Speed", value: 68 },
      { sector: "Education", metric: "Cost Efficiency", value: 75 },
      { sector: "Finance", metric: "Accuracy", value: 92 },
      { sector: "Finance", metric: "Speed", value: 85 },
      { sector: "Finance", metric: "Cost Efficiency", value: 88 }
    ];

    const sectors = ["Healthcare", "Education", "Finance"];
    const metrics = ["Accuracy", "Speed", "Cost Efficiency"];

    const margin = { top: 60, right: 30, bottom: 50, left: 60 };
    const containerWidth = 1100px;
    const width = containerWidth - margin.left - margin.right;
    const height = 700 - margin.top - margin.bottom;

    const svg = d3.select("#chart")
      .append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .style("display", "block")
      .append("g")
      .attr("transform", `translate(${margin.left},${margin.top})`);

    const x0 = d3.scaleBand()
      .domain(sectors)
      .range([0, width])
      .paddingInner(0.2);

    const x1 = d3.scaleBand()
      .domain(metrics)
      .range([0, x0.bandwidth()])
      .padding(0.1);

    const y = d3.scaleLinear()
      .domain([0, 100])
      .range([height, 0])
      .nice();

    const color = d3.scaleOrdinal()
      .domain(metrics)
      .range(["#4682b4", "#6b8e23", "#d2691e"]);

    svg.append("g")
      .attr("class", "x-axis")
      .attr("transform", `translate(0, ${height})`)
      .call(d3.axisBottom(x0))
      .selectAll("text")
      .style("font-size", "12px");

    svg.append("g")
      .attr("class", "y-axis")
      .call(d3.axisLeft(y).ticks(10).tickFormat(d => d + "%"))
      .selectAll("text")
      .style("font-size", "12px");

    const sectorGroups = svg.selectAll(".sector")
      .data(sectors)
      .enter()
      .append("g")
      .attr("class", "sector")
      .attr("transform", d => `translate(${x0(d)},0)`);

    sectorGroups.selectAll("rect")
      .data(sector => data.filter(d => d.sector === sector))
      .enter()
      .append("rect")
      .attr("x", d => x1(d.metric))
      .attr("y", d => y(d.value))
      .attr("width", x1.bandwidth())
      .attr("height", d => height - y(d.value))
      .attr("fill", d => color(d.metric));

    sectorGroups.selectAll("text.label")
      .data(sector => data.filter(d => d.sector === sector))
      .enter()
      .append("text")
      .attr("class", "label")
      .attr("x", d => x1(d.metric) + x1.bandwidth() / 2)
      .attr("y", d => y(d.value) - 5)
      .attr("text-anchor", "middle")
      .style("font-size", "11px")
      .style("fill", "#333")
      .text(d => d.value + "%");

    svg.append("text")
      .attr("x", width / 2)
      .attr("y", -30)
      .attr("text-anchor", "middle")
      .style("font-size", "18px")
      .style("fill", "#333")
      .text("Improvement in Key Performance Metrics for AI Applications");

    const legend = svg.selectAll(".legend")
      .data(metrics)
      .enter()
      .append("g")
      .attr("class", "legend")
      .attr("transform", (d, i) => `translate(0,${i * 20})`);

    legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .attr("fill", d => color(d));

    legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .style("fill", "#333")
      .style("font-size", "12px")
      .text(d => d);
  </script>
</html>"""
    
    prompt = f"""
Generate a full HTML code (including CSS and JavaScript) that displays a simple, elegant graph using D3.js.
The output MUST include only:
 • A descriptive title for the graph
 • The graph code (using D3.js) that uses data relevant to:
{placeholder_text}

Ensure that no extraneous content (such as discussion, concluding remarks, or additional text) is included.
Take in consideration that the resulting  content will render 1200px width and and 700px high.
Output the result:
- starting with <html> and
- ending with </html>
- with no additional comments or code fences
- use proper indentation in the output
// Sources:
Keep in mind the:
- context:
{context}

- the knowledge inputs:
{crumbs}

// Requirements
- Use elaborate but effective visual through call to the D3.js library.
- No introduction, conclusions or code fences -> Output the result directly
- It should start with <html> and end with </html>
- Use a responsive design. Do not fix the width of the main container in the generated HTML. Instead, set the meta viewport to "width=device-width, initial-scale=1" and use CSS (such as viewBox attributes in SVG) to ensure the chart scales properly.
- Make the visuals content rich, there's no point having a visual if its content has no real value.
- Make sure all the items are visible and don't require hovering the mouse to be displayed - this report is meant to be printed.
- For any element where text is displayed, if you choose a dark background, the fond has to be white / if you choose a clear background, the font has to be black
- Use your judgement to decide between box plots, bubble charts, calendar view, chord diagrams, histograms, ...
- Your response should start with <html> and end with </html> - no intro before, no comments after.

// Purpose and grounding
- It has to convey some relevant insights grounded from the context and knowledge input
- Make sure that the message conveyed really adds value once integrated in a report or posted on social media
- It CANNOT be hallucinated or hypothetical, it has to be factual and based on relevant info
- You will be penalised if you lie or mix things that are not related to each other
- Do not "wing it" with clumsy output, have a deep thinking to it

// Examples
{graph_examples}

Now provide the result directly.
"""
    result = llm_call(prompt, model="o3-mini", temperature=0, max_tokens_param=10000)
    result = result.strip().strip("```").strip()
    
   
    # Create a responsive container with proper width constraints - fixed width and height for consistency
    htmloutput = f"""
    <div class="graph-container" style="max-width:1100px; width:100%; margin:0 auto; overflow:hidden;">
        <iframe 
            class="visual-frame" 
            style="border:none; width:100%; height:700px;" 
            srcdoc="{result}">
        </iframe>
    </div>
    """
    
    logging.info(f"The code produced for this graph placeholder:\n{placeholder_text}")
    return htmloutput

def replace_graph_placeholders(report_html: str, context: str, initial_query: str, crumbs: str) -> str:
    pattern = r"\[\[Graph Placeholder (\d+):(.*?)\]\]"
    
    def placeholder_replacer(match):
        placeholder_num = match.group(1)
        instructions = match.group(2).strip()
        logging.info(f"Generating graph {placeholder_num}")
        
        try:
            visual_html = generate_graph_snippet(instructions, context, initial_query, crumbs)
            prompt_improve = f"Based on the instructions:\n{instructions}\n\nThe context:\n{context}\n\nThe knowledge crumbs:\n{crumbs}\n\nProvide a refined version to this graph code:\n{visual_html}\n\nThe improved result should be EXACTLY in the same html format (no introduction, no conclusion, no code fences)."
            visual_html = llm_call(prompt_improve, model="o3-mini", temperature=0, max_tokens_param=10000)
            visual_html = visual_html.strip().strip("```").strip()

            return f'<!-- Graph {placeholder_num} Start -->\n<div class="graph-container" style="margin:20px 0;">\n{visual_html}\n</div>\n<!-- Graph {placeholder_num} End -->'
        except Exception as e:
            logging.error(f"Graph {placeholder_num} failed: {str(e)}")
            error_msg = f'<div class="error-message" style="color:red; border:1px solid red; padding:10px; margin:10px 0;">Error generating Graph {placeholder_num}: {str(e)}</div>'
            return error_msg

    return re.sub(pattern, placeholder_replacer, report_html, flags=re.DOTALL)

# ============================================================================= Visual placeholder

def generate_visual_snippet(placeholder_text: str, context: str, initial_query: str, crumbs: str) -> str:
    # remove special lines
    def remove_special_lines(input_string):
        lines = input_string.splitlines()
        filtered_lines = [
            line for line in lines
            if not (
                (line.strip().startswith("%%") and line.strip().endswith("%%")) or
                line.strip().startswith("class")
            )
        ]
        cleanedlines = "\n".join(filtered_lines)
        return cleanedlines

    def process_multiline_string(input_string):
        def process_parentheses(match):
            content = match.group(2)  # Extract text inside parentheses
            return f"{match.group(1)} - {content}"  # Replace with a dash and the content
        pattern = r"(\[.*)\(([^)]+)\)\]"  # Captures lines with content followed by parentheses
        processed_string = re.sub(pattern, lambda m: process_parentheses(m), input_string, flags=re.MULTILINE)
    
    prompt = f"""
Generate a mermaid code displaying a simple but effective and elegant visualization based on the following requirements:
{placeholder_text}

It will be integrated in a broader report about:
{initial_query}

// Sources:
Keep in mind the:
- context:
{context}
- the knowledge inputs
{crumbs}

// Requirements
- Use standard mermaid visual rendering (ex: flowchart, sequence, gantt, pie, mindmap)
- Ieep it simple, no fancy characters, just simple text in the pseudo-code (not event parenthesis)
- Make the visuals content rich, there's no point having a visual if its content has no real value.
- It has to convey some relevant insights grounded in data provided in the search results, do not make things up - particularly on the relationship between items
- Use only this type of quotation marks: ", others would lead to failure to render
- Take in consideration that the resulting iframe content will render 1200px width and and 700px high.
- It has to convey some relevant insights grounded from the context and knowledge input
- It CANNOT be hallucinated or hypothetical, it has to be factual and based on relevant info
- You will be penalised if you lie or mix things that are not related to each other
- Do not "wing it" with clumsy output, have a deep thinking to it

// Important
- No introduction, conclusions or code fences -> Output the result directly - create only the content for the mermaid
- Do no put any comment of #color coding and classes inside the mermaid code generated, it's supposed to be only focused on the mermaid code required to render it
- Avoid the formatting commands (ex: %%...%% ) and do not use classDef, class or linkStyle for formatting
- Do not add items in the mermaid code between parenthesis ex: [xxx(yyy)] , rather use [xxx - yyy] / Do not put it in parenthesis, even after the hyphen - or for units.
- If you need to add a remark in the code, do it like this after an hyphen: "[abc - 95% Context Coverage]". Simply don't use parenthesis except for the below prescribed formatting, mermaid if very format sensitive
Example: do not write:
G --> H[Performance Evaluation (45% Speed Improvement, 35% Risk Profiling, 50% Fraud Detection)]
Instead, use
G --> H[Performance Evaluation - 45% Speed Improvement, 35% Risk Profiling, 50% Fraud Detection]
- Do not add any formatting reference in the content of the mermaid code (ex - green ...), this will lead to errors when submitted to the mermaid api
- Do not use the pipe symbol | in the mermaid code, it would create an error 
- Do not use the gantt chart visual placeholder to generate a graph (ex: barchart), Gantt charts should only be used for timelines / project plans
Note: Visual placeholders are solely supposed to generate diagrams not for graphs.
- Do not use the quotation mark inside the text of the diagram 
- Do not use commas in the content of each object / nodes, they will create rendering issues
- Do not use any [x] reference notation for citations (ex: [1], [2], ...) inside the mermaid code - we will manage sources separately

// Examples
Note: Pay attention for each example to what type of parenthesis / bracket is used and respect it scrupulously

-- flowchart --
Important:
- If the flow is "deeper" than broad (>3 levels), choose TD (Top Down) ==> TD is the default one if you have a doubt
- If the flow is "broader" than deep (>3 branches at the same level), choose LR (Left Right)
- There can be mix of flows that can be elaborate
- Take in consideration that the resulting iframe content will render 1200px width and and 700px high. Make the best of the areas real-estate.

Top Down:

flowchart TD
    A[Christmas] -->|Get money| B(Go shopping)
    B --> C{{Let me think}}
    C -->|One| D[Laptop]
    C -->|Two| E[iPhone]
    C -->|Three| F[fa:fa-car Car]


Left Right:

flowchart LR
    A[Anticipated AI Advancements by 2025]
    
    subgraph Infinite_Context [Infinite Context Framework]
      I1[Token Limits: Unlimited]
      I2[Summarization Accuracy: Significantly Improved]
      I3[Industry Impact: Legal, Academic, IoT]
      I4[Use-case Efficiency: High Impact]
      I5[Integration with Emerging Reasoning Models & Agentic Architecture]
    end

    subgraph Traditional_Context [Traditional Fixed Context Windows]
      T1[Token Limits: 2048-4096]
      T2[Summarization Accuracy: Moderate]
      T3[Industry Applications: Limited Scope]
      T4[Use-case Impact: Constrained]
    end

    subgraph Comparative_Innovations [Comparative Metrics & Emerging Technologies]
      C1[Test Time Compute Enhancements]
      C2[Innovations: VJEPA, Synthetic Data, Deepseek]
      C3[New Paradigms: TTT, Novel Architectural Approaches]
      C4[Disruption Factors: Deployment Speed, Employment Impact, Lower Barriers]
    end

    A --> Infinite_Context
    A --> Traditional_Context
    A --> Comparative_Innovations

    class I1,I2,I3,I4,I5 infinite;
    class T1,T2,T3,T4 trad;
    class C1,C2,C3,C4 comp;

Mixed: For elaborate visuals
// example 1

flowchart LR
    subgraph subgraph1
        direction TB
        top1[top] --> bottom1[bottom]
    end
    subgraph subgraph2
        direction TB
        top2[top] --> bottom2[bottom]
    end
    %% ^ These subgraphs are identical, except for the links to them:

    %% Link *to* subgraph1: subgraph1 direction is maintained
    outside --> subgraph1
    %% Link *within* subgraph2:
    %% subgraph2 inherits the direction of the top-level graph (LR)
    outside ---> top2

// example 2
flowchart LR
subgraph "One"
  a("`The **cat**
  in the hat`") -- "edge label" --> b{{"`The **dog** in the hog`"}}
end
subgraph "`**Two**`"
  c("`The **cat**
  in the hat`") -- "`Bold **edge label**`" --> d("The dog in the hog")
end

-- sequence --
sequenceDiagram
    Alice->>+John: Hello John, how are you?
    Alice->>+John: John, can you hear me?
    John-->>-Alice: Hi Alice, I can hear you!
    John-->>-Alice: I feel great!

-- gantt --
gantt
    title A Gantt Diagram
    dateFormat  YYYY-MM-DD
    section Section
    A task           :a1, 2014-01-01, 30d
    Another task     :after a1  , 20d
    section Another
    Task in sec      :2014-01-12  , 12d
    another task      : 24d

-- pie --
pie title Pets adopted by volunteers
    "Dogs" : 386
    "Cats" : 85
    "Rats" : 15

-- mindmap --
mindmap
  root[mindmap]
    Origins
      Long history
      Popularisation
        British popular psychology author Tony Buzan
    Research
      On effectivness
      On Automatic creation
        Uses
            Creative techniques
            Strategic planning
            Argument mapping
    Tools
      Pen and paper
      Mermaid

"""
    
    result = llm_call(prompt, model="o3-mini", temperature=0, max_tokens_param=10000)
    result = result.strip().strip("```").strip()
    #result = remove_special_lines(result)
    #result = process_multiline_string(result)

    htmloutput = f"""<iframe class="visual-frame" srcdoc='
        <!DOCTYPE html>
        <html>
            <head>
                <script src="https://cdn.jsdelivr.net/npm/mermaid@10.4.0/dist/mermaid.min.js"></script>
                <script>
                    mermaid.initialize({{ startOnLoad: true, securityLevel: "loose", theme: "default" }});
                </script>
                <style>
                    .mermaid {{
                        background-color: #f7f2f2;
                        color: black;
                        padding: 10px;
                        border-radius: 5px;
                        border: 1px solid #ccc;
                        max-width: 1200px;
                        margin: 0 auto;
                        min-height: 400px;
                    }}
                </style>
            </head>
            <body>
                <div class="mermaid">
    {result}
                </div>
            </body>
        </html>' width="1200px" height="700px" style="border:none;"></iframe>
        """
    
    logging.info(f"The code produced for this visual placeholder:\n{placeholder_text}\n\n {htmloutput}\n\n")

    # Remove parenthesis in mermaid formatting
    htmloutput = re.sub(
        r"<iframe>\.\.\.\[\.\.\.\((.*?)\)\] </iframe>",
        r"<iframe>...[...- \1] </iframe>",
        htmloutput
    )
    return htmloutput

def replace_visual_placeholders(report_html: str, context: str, initial_query: str, crumbs: str) -> str:
    pattern = r"\[\[Visual Placeholder (\d+):(.*?)\]\]"  # Capture placeholder number
    replacements = []

    def placeholder_replacer(match):
        placeholder_num = match.group(1)
        instructions = match.group(2).strip()
        logging.info(f"Generating visual {placeholder_num}")
        
        try:
            visual_html = generate_visual_snippet(instructions, context, initial_query, crumbs)
            prompt_improve = f"Based on the instructions:\n{instructions}\n\nThe context:\n{context}\n\nThe knowledge crumbs:\n{crumbs}\n\nProvide a refined version to this visual code:\n{visual_html}\n\nThe improved result should be EXACTLY in the same html format (no introduction, no conclusion, no code fences)."
            visual_html = llm_call(prompt_improve, model="o3-mini", temperature=0, max_tokens_param=10000)
            visual_html = visual_html.strip().strip("```").strip()
            # Add error boundary and logging
            return f'<!-- Visual {placeholder_num} Start -->\n{visual_html}\n<!-- Visual {placeholder_num} End -->'
        except Exception as e:
            logging.error(f"Visual {placeholder_num} failed: {str(e)}")
            return f'<!-- ERROR GENERATING VISUAL {placeholder_num} -->'

    return re.sub(pattern, placeholder_replacer, report_html, flags=re.DOTALL)

# ============================================================================= Focus placeholder

def replace_focus_placeholders(report_html: str, context: str, initial_query: str, crumbs: str) -> str:
    pattern = r"\[\[Focus Placeholder (\d+):(.*?)\]\]"  # Capture placeholder number
    def placeholder_replacer(match):
        placeholder_num = match.group(1)
        instructions = match.group(2).strip()
        logging.info(f"Generating focus box {placeholder_num}")
        try:
            focus_html = generate_focus_snippet(instructions, context, initial_query, crumbs)
            # Remove any outer HTML or body tags from the generated snippet:
            focus_html = re.sub(r"<\/?(html|head|body)[^>]*>", "", focus_html, flags=re.DOTALL|re.IGNORECASE).strip()
            # Wrap the entire focus content in one div with a dedicated CSS class
            return (
                f'<!-- Focus {placeholder_num} Start -->'
                f'<div class="focus-placeholder">{focus_html}</div>'
                f'<!-- Focus {placeholder_num} End -->'
            )
        except Exception as e:
            logging.error(f"Focus {placeholder_num} failed: {str(e)}")
            return f'<!-- ERROR GENERATING FOCUS {placeholder_num} -->'
    return re.sub(pattern, placeholder_replacer, report_html, flags=re.DOTALL)

def generate_focus_snippet(placeholder_text: str, context: str, initial_query: str, crumbs: str) -> str:
    prompt = f"""
Generate a complete, self-contained inner-HTML code (only the content inside <div> tags, no outer <html> tags) with inline CSS.
The code should provide an elaborate technical drill‐down on the topic:
{placeholder_text}

The output must be between 1000 and 2000 words.
- Use clear sub‑titles (bold them or use <h3> tags) and paragraphs to break the content.

It will be integrated in a broader report (in html, within the <body>, no need to add it) about:
{initial_query}

// Sources:
Keep in mind the:
- context:
{context}
- the knowledge inputs
{crumbs}

// Critical Requirements
- It MUST include inline citations using the format [1], [2], etc., that exactly match the reference numbering used in the overall report.
Note: no need to add a reference table at the end of the focus box, all the references / citations have already been listed at the end of the report (just refer to the [x] provided in the placeholder instructions)
Note2: citations sources in-line need to be in this format: blablabla - Source [x] / "pdf" is not a source, provide the title or author
- Do not add inline citations for every sentence, use is ontly when people, numbers or organisation names or projects are mentioned
- Use html with css to render it, keep it sober and focused on the content
- The output has to be boxed in a border of 1px Black
- Font size minimum 12px for readability in PDF
- Skip lines and add intermediate titles for key paragraphs
- You have freedom on the structure, but it has to cover all potential aspects on the topic in between 500 and 1000 words

// Mentioning sources, organisations and individuals
- We will perform a post-processing on the output
- For this reasons use this format for any specific name, organisation or project: {{[{{name}}]}}
ex1: {{[{{Google}}]}} CEO, {{[{{Sundar Pichai}}]}} ...
ex2: in a report from the {{[{{university of Berkeley}}]}} titled "{{[{{The great acceleration}}]}}"...
ex3: the CEO of {{[{{Softbank}}]}} , {{[{{Masayoshi Son}}]}}, said that "the best way to..."
ex4: the project {{[{{Stargate}}]}}, anounced by {{[{{OpenAI}}]}} in collaboration with {{[{{Salesforce}}]}}
ex5: Mr. {{[{{Michael Parrot}}]}}, Marketing director in {{[{{Panasonic}}]}}, mentioned that ...
Note: the output will be processed through regex and the identifiers removed, but this way we can keep track of all sources and citations without disclosing them.
- This should apply to names, people/titles, dates, papers, reports, organisation/institute/NGO/government bodies quotes, products, project names, ... 
- You should have approximately 10 mention of organisations, people, projects or people, use the prescribed format
- DO NOT MENTION this formmatting requirement, just apply it. The user doesn't have to know about this technicality.
- Make sure not to bundle together several sources, use one source at a time when mentioning it, do the same for the authors. They can be put together only if both were involved and mentioned in the research results.
Note: LinkedIn is not a source - if you want to use a source related to LinkedIn, you should check the author of the page visited, this is the real source, mention the name of the author as "'authorName' from LinkedIn Pulse"

// Purpose and grounding
- It has to convey some relevant insights grounded from the context and knowledge input
- It CANNOT be hallucinated or hypothetical, it has to be factual and based on relevant info
- You will be penalised if you lie or mix things that are not related to each other
- Do not "wing it" with clumsy output, have a deep thinking to it

// Important
- Make it real, with anecdotes from the content
- Ground the content on the inputs shared above (context and knowledge inputs) with facts and numbers
- Take a deep breath, think step by step and think it well.
- Title should be h2 level (inside the focus box) then the content
- No introduction, conclusions, code fences -> Output the result directly
""" 
    result = llm_call(prompt, model="o3-mini", temperature=0, max_tokens_param=10000)
    result = result.strip().strip("```").strip()
    result = re.sub(r'\{\[\{(.*?)\}\]\}', r'\1', result)
    result = re.sub(r'\[\{(.*?)\}\]', r'\1', result)
    logging.info(f"The code produced for this focus placeholder:\n{placeholder_text}\n\n {result}\n\n")
    return result
    
# ============================================================================= LLMCall

def llm_call(prompt: str, messages: list = None, model: str = "o3-mini", temperature: float = 0.0, max_tokens_param: int = 10000) -> str:
    if messages is None or len(messages) == 0:
        messages = [{"role": "user", "content": prompt}]
    if len(messages[0]['content']) > MAX_MESSAGE_LENGTH:
        messages[0]['content'] = messages[0]['content'][:MAX_MESSAGE_LENGTH]
    try:
        client = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
        params = {
            "model": model,
            "messages": messages
        }
        if model == "o3-mini":
            params["max_completion_tokens"] = max_tokens_param
        else:
            params["max_tokens"] = max_tokens_param
            params["temperature"]= temperature
        response = client.chat.completions.create(**params)
        result = response.choices[0].message.content
        result = result.strip().strip("json").strip("```").strip()
        
        if not result:
            logging.error("Empty response from LLM for prompt: " + prompt)
            return "Error: empty response"
        
        logging.info(f"llm_call completed with model {model}. Response preview: {result}\n\n\nThe original prompt sent was:\n{prompt[:5000]}")
        return result
    except Exception as e:
        err_msg = f"Error calling OpenAI API: {e}"
        logging.error(err_msg)
        return err_msg

# ============================================================================= Search Engine

def filter_search_results(results: list, visited_urls: set, query: str, clarifications: str) -> list:
    # Filter out already seen results
    new_results = []
    for idx, res in enumerate(results):
        url = res.get("link", "")
        if url and url not in visited_urls:
            new_results.append(res)
    if not new_results:
        return []
    # Build text describing the new results
    results_text = ""
    for idx, res in enumerate(new_results):
        title = res.get("title", "No Title")
        snippet = res.get("snippet", "No Snippet")
        url = res.get("link", "")
        results_text += f"\nResult {idx}:\nTitle: {title}\nSnippet: {snippet}\nURL: {url}\n"
    prompt = (f"""
The following search results were obtained for the query
'{query}'

with clarifications:
{clarifications}

// Requirements
- For each result, decide whether it might be of interest for deeper research.
- Even if not completely certain, lean towards including more potential references.
- Return your decision as a JSON object where each key is the result index (as an integer) and the value is either 'yes' or 'no'.

// EXAMPLE
{{"0": "yes", "1":"no", "2": "yes", ...}}

Consider the title, snippet, and URL in your decision.

// Results to process
{results_text}

// IMPORTANT
Output only the JSON object, no code fences."""
    )
    llm_response = llm_call(prompt, model="gpt-4o-mini", max_tokens_param=500, temperature=0)
    # Remove any Markdown code fences if present
    llm_response = llm_response.strip().strip("json").strip("```").strip()
    try:
        decision_map = json.loads(llm_response)
    except Exception as e:
        logging.error(f"filter_search_results: JSON decode error: {e}; Full response: {llm_response}")
        decision_map = {}
    filtered = []
    for idx, res in enumerate(new_results):
        url = res.get("link", "")
        # Add URL to visited regardless
        visited_urls.add(url)
        decision = decision_map.get(str(idx), "no").strip().lower()
        if decision == "yes":
            filtered.append(res)
    # If no results were judged "yes", fall back to all new results so that we still process them.
    if not filtered:
        logging.info("filter_search_results: No results selected after filtering, defaulting to all new results.")
        return new_results
    logging.info(f"filter_search_results: {len(filtered)} results selected out of {len(new_results)} candidates")
    return filtered

def make_multilingual_query(query: str, context: str, languagesdetected: str) -> str:
    finalquery = f"({query})"  # original query is wrapped in parentheses
    languages_detected_list = languagesdetected.split(",")
    for lang in languages_detected_list:
        prompt2 = f"""The research query is: "{query}".
Based on this query and context: "{context}", and with the detected language {lang}, provide the translated version of the query in that language.
The translation must be less than 20 words and preserve search operators like AND, OR, parenthesis, quotation marks, and exclusion hyphens.
Output only the translated query."""
        translatedquery = llm_call(prompt2, model="gpt-4o-mini", max_tokens_param=50, temperature=0)
        finalquery += f" OR ({translatedquery})"
    logging.info(f"make_multilingual_query: Transformed query: {finalquery}")
    return finalquery

def generate_query_tree(context: str, breadth: int, depth: int, selected_engines: list) -> list:
    # If selected_engines is None, provide a fallback string
    list_engines = "all relevant search engines" if selected_engines is None else ','.join(map(str, selected_engines))
    
    prompt = f"""
Generate {breadth} search queries for academic research exploring: "{context}"

// Research Strategy Requirements
1. Query Progression:
   - Begin with foundational/conceptual queries
   - Progress to methodological/technical terms
   - Culminate in specialized/applied combinations

2. Term Optimization:
   - Use Boolean logic (AND/OR) strategically
   - Include both general terminology AND domain-specific jargon
   - Add temporal filters when relevant (e.g., "since 2018", "2020-2023")
   - Consider geographical/cultural modifiers if applicable

3. Query Structure:
   - Prioritize conceptual combinations over simple keyword matching
   - Use quotation marks for exact phrases and hyphenation for compound terms
   - Include emerging terminology variants (e.g., "LLMs" OR "large language models")

// Output Requirements
- Strictly valid JSON format: {{"queries": ["query1", "query2"]}}
- No Markdown, code fences, or supplementary text
- Clean string formatting without special characters

// Example (breadth=4):
{{
  "queries": [
    "Fundamental theories AND (Artificial Intelligence OR machine learning)",
    "(Computational mathematics OR statistical modeling) AND research paradigms",
    "\"Deep learning architectures\" AND (optimization techniques OR neural networks)",
    "\"Generative AI\" AND industrial applications AND (2020-2024 OR recent developments)"
  ]
}}

Generate queries that systematically explore the research landscape from multiple conceptual angles."""

    messages = []
    llm_response = llm_call(prompt=prompt, messages=messages, model="o3-mini", temperature=0, max_tokens_param=1500)
    logging.info(f"Generated query tree: {llm_response}")

    cleaned_response = llm_response.strip().strip("`").strip()
    try:
        queries = json.loads(cleaned_response)['queries']
        logging.info(f"Queries generated in queries: {queries}")
        final_queries = queries[:min(len(queries), breadth)]
        logging.info(f"Queries generated in final_queries: {final_queries}")
    except (json.JSONDecodeError, KeyError, TypeError) as e:
        logging.error(f"Error parsing LLM response in generate_query_tree: {e}")
        final_queries = []  # or use a fallback default query list, e.g.
        # final_queries = [ "default search query" ]
    return final_queries

def generate_serp_queries(context: str, breadth: int, depth: int, initial_query: str, selected_engines=None, results_per_query: int = 10) -> list:
    queries = generate_query_tree(context, breadth, depth, selected_engines)
    logging.info(f"Queries generated from generate_query_tree:{queries}")
    prompt = f"""The research topic is: "{initial_query}".
Based on this query and the overall context: "{context}", suggest one or several languages (other than English) that might be relevant.

Output either:
- "No local attributes detected"
- One language (e.g., "Spanish")
- Multiple languages comma separated (e.g., "Italian,Putonghua,Cantonese")
Output only the result.
"""
    languages_detected = llm_call(prompt, model="gpt-4o-mini", temperature=0, max_tokens_param=50)
    logging.info(f"languages detected: {languages_detected}")
    if languages_detected != "No local attributes detected":
        queries = [make_multilingual_query(q, context, languages_detected) for q in queries]
    if not selected_engines or len(selected_engines) == 0:
        prompt_engines = f"""
Examine these queries:
{queries}
and considering the research context:
{context}
Identify among these search engines:
google,google_scholar,google_trends,google_news,google_ai_overview,bing,bing_news,baidu,baidu_news,yandex,youtube_video,linkedin,linkedin_profile,duckduckgo_news,yelp_reviews
Which are most relevant? Output a comma separated list (e.g., "google,baidu").
If none are found, output "google".
"""
        identified_engines = llm_call(prompt_engines, model="o3-mini", temperature=0, max_tokens_param=50)
        logging.info(f"Identified engines are:{identified_engines}")

        # Split and strip engines; if result is empty (or all empty strings), default to ["google"]
        selected_engines = [e.strip() for e in identified_engines.split(",") if e.strip()]
        if not selected_engines:
            selected_engines = ["google"]
    else:
        selected_engines = selected_engines
    final_queries = []
    for q in queries:
        for engine in selected_engines:
            final_queries.append((q, engine))
    logging.info(f"generate_serp_queries: Final query-engine pairs: {final_queries}")
    return final_queries

def perform_serpapi_search(query: str, engine: str, num_results: int = 10) -> list:
    api_key = os.getenv("SERPAPI_API_KEY")
    params = {
        "q": query,
        "api_key": api_key,
        "engine": engine,
        "num": str(num_results)
    }
    attempt = 0
    results = []
    while attempt < 3:
        try:
            headers = {"User-Agent": get_random_header()}
            response = requests.get("https://serpapi.com/search", params=params, headers=headers)
            if response.status_code == 200:
                try:
                    data = response.json()
                    # Check if data is a string; if so, convert it
                    if isinstance(data, str):
                        data = json.loads(data)
                except Exception as je:
                    logging.error("perform_serpapi_search: Failed to parse JSON response. Raw response: " + response.text)
                    data = {}
                # Double-check the data type and try to convert otherwise if necessary
                if not isinstance(data, dict):
                    try:
                        data = json.loads(data)
                    except Exception as e:
                        logging.error("perform_serpapi_search: Could not convert response to a dictionary. Error: " + str(e))
                        data = {}
                results = data.get("organic_results", [])
                if len(results) > 0:
                    logging.info(f"perform_serpapi_search: Found {len(results)} results for engine {engine}")
                    break
            time.sleep(random.uniform(1, 3))
        except Exception as e:
            logging.error(f"perform_serpapi_search attempt {attempt} error: {e}")
            time.sleep(random.uniform(1, 3))
        attempt += 1
        time.sleep(2)
    if not results:
        logging.info(f"perform_serpapi_search: No results found for query: {query}, engine: {engine}")
    return results

# ============================================================================= Final report

def generate_final_report(initial_query: str, context: str, reportstyle: str, learnings: list, visited_urls: list,
                          aggregated_crumbs: str, references: list, pages: int = 8) -> str:
    fallback_text = ""
    if not learnings:
        fallback_text = "No external summaries were directly extracted. It is not possible to analyze relevance."
    combined_learnings = "\n".join(learnings) if learnings else fallback_text
    word_count = pages * 500
    prompt = (f"""

--------------- Objectives -----------
Produce a comprehensive report in html format.
The report should be detailed, grounded in facts and sources provided
It should be  {pages} long () or {word_count} words ) (excluding html formatting).
It should include tables, focus boxes as well as visuals and graphs (see below).
Exclusing Abstract, Table of contents, Abstracts, Conclusion and all placeholders, there should be approximately {round(pages/2,0)}
The report must follow this writing style: {reportstyle}.

--------------- Formatting -----------
- Add on top of the report the report title (with the <h1> tag) - this is the only part that should be centered (in-line style)
- All text alignment has to be on the left
- Titles for sections and sub-sections should systematically use the tags:
  <h1> for sections (ex: <h1>3. Examination of State-of-the-Art of AI</h1>)
  <h2> for sub-sections (ex: <h2>3.2 AI Performance in Mathematics</h2>)
  <h3> for sub-sub-sections (ex: <h3>3.2.1 Illustration with math conjecture demonstration</h3>)
  <h4> for bulletpoint title (ex: <h4>item to detail:</h4> description of the item to detail ...)
  Note:
  * Put "Table of contents" and "Abstract" title in h1 format.
- No more than 10 sentences per div blocks, skip lines and add line breaks when changing topic.
- For the numbering of titles or numbered lists, use numbers (ex: 1.) and sub-units (1.1, 1.2... 1.1.1...,1.1.2...).
  Note:
    * Exclude the use of html numbered lists format, they don't get correctly implemented. Use plain text format for numbering of sections and sub-sections
    * Do not put a numbered list (ex: 1.1, ...) for every sentences! It should be used parcimoniously for real sub-sections.
- Put paragraphs, sentences that are part of the same section in a div tag, this will be used for formatting.
- Avoid Chinese characters in the output (use the Pinyin version) since they won't display correcly in the pdf (it displays as black boxes)
- For the Table of contents: do not mention the pages, but make each item on separate line
- The Table of contents should not mention the abstract and table of contents, the numbering should start from the introduction and end with References Summary Table
- Exceptionally - for sections requiring specific improvements - put it between <div class="improvable-chunk">...</div> (but don't mention it in the report, this will be managed through post-processing)

--------------- Tables -----------
- The report must include between {round(pages/10,0)} and {round(pages/5,0)} tables from the sources used (add citations if necessary) and use facts and figures extensively to ground the analysis.
- Use inline formatting for the tables with homogeneous border and colors

--------------- Citations -----------
- The report must include inline citations (e.g., [1], [2], etc.) from real sources provided in the search results below - be selective, don't put it at every sentence or every paragraph.
   Note: citations sources in-line need to be in this format: blablabla - Source [x] / "pdf" is not a source, provide the title or author
- Do not add inline citations for every sentence, use is ontly when people, numbers or organisation names or projects are mentioned
- The name of the reference table should be: "References Summary Table"
- The reference table at the end containing the citations details should have 4 columns: the ref number, the title of the document, the author(s, the URL - with hyperlink)
- The report MUST include a References Summary Table with between 10 (for a 8 page report) and 30 references (for a 40 pages report). All inline citations (e.g., [1], [2], …) present in the report and in any focus placeholders MUST have a corresponding entry in this table with its full URL.
- For the reference citations, add systematically the urls from the Learnings (no need to put them in numbered list format since we alredy have the [x] that serves as number list)
- Do not add any inline citations reference in the visual and graph placeholders descriptions belo, you can add them in focus though.
- Do not make false references / citations. It has to be grounded from the sources in the rsearch results / crumbs below (no example.com/... type references!)
- The references / citations should be only coming from the most reputable sources amongst all the Learnings and Results from searches below
- The table generated should have in-line styling to have word-wrap and 100% width

// Instructions:
1. Integrate numbers from the sources but always mention the source
2. Whenever you mention a figure or quote, add an inline reference [x] matching its source from the references.
3. Again, Specifically name relevant organizations, tools, project names, and people encountered in the crumbs or learnings.
   Note: This is for academic purposes, so thorough citations and referencing are essential.
4. Focus on reputable sources that will not be disputed (generally social media posts cannot be an opposable sources, but some of them may mention reputable sources)
   Note: put the full reference url (no generic domain address), down to the html page or the pdf

// Format when mentioning sources, organisations and individuals
- We will perform a post-processing on the output
- For this reasons use this format for any specific name, organisation or project: {{[{{name}}]}}
example 1: {{[{{Organisation}}]}}'s CEO, {{[{{CEO name}}]}} ...
example 2: in a report from the {{[{{University name}}]}} titled "{{[{{report title}}]}}"...
example 3: the CEO of {{[{{Company name}}]}} , {{[{{Name}}]}}, said that "the best way to..."
eexample 4: the project {{[{{project name}}]}}, anounced by {{[{{...}}]}} in collaboration with {{[{{...}}]}}
example 5: Mr. {{[{{person}}]}}, Marketing director in {{[{{company}}]}}, mentioned that ...
Note: the output will be processed through regex and the identifiers removed, but this way we can keep track of all sources and citations without disclosing them.
- This should apply to names, people/titles, dates, papers, reports, organisation/institute/NGO/government bodies quotes, products, project names, ... 
- You should have approximately {2 * pages} mention of organisations, people, projects or people, use the prescribed format
- The same item cannot be mentioned more than 3 times, don't over do it
- Do not mix sources that are not directly related in the search results, don't put together organisations or people that have nothing to do with each other
- DO NOT MENTION this formmatting requirement, just apply it. The user doesn't have to know about this technicality.
Note: LinkedIn is not a relevant source - if you want to use a source related to LinkedIn, you should check the author of the page visited, this is the real source, mention the name of the author as "'authorName' from LinkedIn Pulse"

--------------- Source grounding -----------
// Use the following learnings and merged reference details from a deep research process on this topic:
'{initial_query}'

// Taking also into consideration the context:
{context}

// Key learnings to use as source:
{json.dumps(learnings, indent=2)}

// Results from searches you can mention in details:
{aggregated_crumbs}

--------------- Placeholders -----------
In order to enrich the content, within the core sections (between introduction and conclusion), you can inject some placeholders that will be developped later on.
There are 3 types: visual, graphs, focus - each with their own purpose

// Visual placeholders //
- Create special visual placeholders that will be rendered in mermaid afterwards.
- The Visual placeholders should follow this format:

Source:source_name [y]
[[Visual Placeholder n:
- Purpose of this visual is:...
- Relevant content to generate it:
o ex: arguments
o ex: lists of elements
o ex: data points
o ...
- Message to convey: ...
]]

with:
- n as the reference number,
- source_name as the full name of the main source used and
- y as the number ref of the source reference in the reference table.
Important note for visual placeholders:
- on the line before [[...]] mention the source with the reference number [x] in the form: ""Source: abc [n]" - only one source should be mentioned
- after [[ put "Visual Placeholder n:" explicitly (with n as the ref number of the placeholder box created). This will be used in a regex
- the only types of mermaid diagram that can be generated are: flowchart, sequence, gantt, pie, mindmap (no charts) // Take this into consideration when providing the instructions for the diagram
- do not make mention in the report to "visual placeholders" just mention the visual and the number..
- in the placeholder, no need to add the references to the source or its ref number, but make sure ALL of the data points required has a source from the learning and reference material hereafter 
- these placeholders text should contain:
    o the purpose of the future visual 
    o the relevant data to generate it
- there should be between {round(pages/10,0)} and {round(pages/5,0)} of these visuals placeholders within the report (all between introduction and conclusion)
- 2 visual placeholders cannot be in the same section
Note: the placeholders will then be processed separately by a llm to generate the specific code to display each of them so the instruction need to be clear enough.

// Graph placeholders //
- Create special graph placeholders that will be rendered in d3.js afterwards based on your guidance:

Source:source_name [y]
[[Graph Placeholder n:
- Purpose of this graph is:...
- Relevant numbers to generate it:
table format
- Message to convey: ...
]]

with:
- n as the reference number,
- source_name as the  full name of the main source used and
- y as the source reference in the reference table.
- the table containing all the required data has to include data points FROM the learnings / results from the search below
Important note for graph placeholders:
- on the line before [[...]] mention the source with the reference number [x] in the form: ""Source: abc [n]" - only one source should be mentioned
- use p tag for the source and source reference number
- after [[ put "Graph Placeholder n:" explicitly (with n as the ref number of the graph created). This will be used in a regex
- Do not make things up - every data points have to be from a real source
- All types of graphs (using d3.js library) can be generated // Take this into consideration when providing the instructions for the graph data
- do not make mention in the report to "graph placeholders" just mention graph.
- in the placeholder, no need to add the references to the source or its ref number, but make sure ALL of the data points required has a source from the learning and reference material hereafter 
- these placeholders text should contain:
    o the purpose of the future graph 
    o the relevant data to generate it
- there should be between {round(pages/10,0)} and {round(pages/5,0)} of these graphs placeholders within the report (all between introduction and conclusion)
- 2 graph placeholders cannot be in the same section 
Note: the placeholders will then be processed separately by a llm to generate the specific code to display each of them so the instruction need to be clear enough.

// Focus placeholders //
- To drill down on specific topic that would be deserve to be developped extensively separately, create special focus placeholders in [[...]] double backets
Note: outside of the placeholder, do not make reference in the report to "focus placeholders" just mention the "Focus box n".
- in the Focus placeholder, make a mention to the prescribed sources used (no need to add the source before or after the placeholder)
- do not make the placeholder on the exact same topic as the section or the sub-section where it is positioned, it has to be either:
    o a special case that deserves attention
    o a recent development / innovation
    o a theoretical drill-down
    o a contrarian point of view / objection
- these placeholders text should contain:
    o the purpose of the focus box 
    o the relevant data to generate it
    o the guidance in terms of style and message to convey
    Note: Be specific if you want some particular point developped, keep it consistent across the report.
- there should be between {round(pages/20,0)} and {round(pages/10,0)} of these focus placeholders within the report (all between introduction and conclusion)
- 2 focus placeholders cannot be in the same section and should be a few pages apart in the report
- Mention all the sources that should be used to generate this focus placeholder and list also the references that will be mentioned in the References section later (ex: [1], [2])
Note: the Focus placeholders will then be processed separately by a llm to generate the specific code to display each of them so the instruction need to be clear enough.

// Format:
[[Focus Placeholder n:
- Topic of this focus:...
- Relevant info to generate it:...
- Specific angle of the focus placeholder:...
- Key elements to mention:
o ...
o ...
...
]]

with:
- n as the reference number,

Important note for focus placeholders:
- after [[ put "Focus Placeholder n:" explicitly (with n as the ref number of the focus box created). This will be used in a regex
- Do not add a title for the Focus placeholder just before the [[...]], the content that will replace the focus placeholder - generated later on - will already include a title
- placeholders (visual, graph or focus) can only appear in the sections or sub-sections not in introduction, the conclusion, the references or after the references

--------------- Report structure -----------
Use the following report structure with consistency:

{{Do not add anything before - no introductory meta comment or content}}

- Abstract
- Table of contents
- Introduction
- [Sections and sub-sections, depending on the size and relevant topic - including visual, graph and focus placeholders]
- Conclusion
- References Summary Table
- Report ending formatting (as mentioned before)

{{Do not add anything after - no conclusive meta comment or content}}

--------------- Best practices  -----------
You generated the below best practices based on millions of reports reviewed and analyzed, follow them:

I. Overall Structure and Organization:

    Standard Structure: Follow the clear and organized structure as prescribed above
    Logical Flow: Ensure a logical flow of ideas, connecting various parts to form a unified whole. Use narrative links between sentences and paragraphs.
    Headings and Subheadings: Use clear, informative, and short headings and subheadings to divide the report into sections and facilitate browsing and scanning. Number them for longer reports.
    Consistent Layout: Maintain a consistent page layout and design features throughout the report. Use the same formatting for titles, headings, bulleted lists, and labels.
    White Space: Use plenty of margins and white space to avoid a "wall of words" and give the reader's eyes a chance to rest.

II. Content and Writing Style:

    Reader-Centric Approach: Design the report with the reader's needs and expectations in mind.
    Clear and Concise Language: Use clear, direct, and precise language. Avoid meaningless expressions. Explain any technical terms.
    Objective Voice: Strive to be as objective as possible when expressing opinions. Record opinions in the context of supporting facts.
    Active Voice: Use active voice to make sentences more direct and engaging.
    Conciseness: Avoid repetition and redundant phrases.
    Accurate and Factual: Be factual, consistent, and accurate.
    Evidence-Based: Support claims with credible evidence and properly cited sources.
    Paragraph Structure: Write strong paragraphs that present a topic, discuss it, and conclude it. Avoid one-sentence paragraphs.
    Avoid Bias: Represent the work of other researchers and your own point of view fairly and accurately.

III. Abstract/Executive Summary:

    Concise Overview: Provide a quick overview of the report's purpose, methods, findings, and conclusions.
    Key Points: Express the thesis or central idea and key points.
    Keywords: Include important keywords related to method and content for searchability.
    Standalone: The abstract should be a self-contained text, understandable on its own.

IV. Introduction:

    Engaging Opening: Capture the reader's attention with a hook, such as a compelling story, quotation, question, or statistic.
    Background Information: Provide necessary context and background information on the topic.
    Purpose and Scope: Clearly state the purpose and scope of the report.
    Thesis Statement: Include a clear and focused thesis statement that expresses the aim or focus of the paper.
    Roadmap: (Optional) Provide a brief overview of the report's structure.

V. Body (Main Sections):

    Organize by Theme/Topic: Structure each section to focus on a specific aspect of the report.
    Methodology: Describe the research methods and materials used in detail.
    Results: Present the research results clearly and objectively.
    Discussion: Interpret the results and explain their significance.

VI. Conclusion:

    Summarize Main Points: Briefly restate the primary findings and analysis.
    Restate Thesis: Reiterate the thesis statement in a fresh way.
    Offer Recommendations: (If applicable) Suggest actions based on the findings.
    Highlight Implications: Explain the significance of the findings and their potential impact.
    Concluding Statement: End with a powerful statement that leaves a lasting impression.
    Avoid New Information: Do not introduce new ideas or evidence in the conclusion.

VII. Visuals (Tables, Graphs, Images):

    Purposeful Use: Incorporate visuals to illustrate complex data and make findings easier to understand.
    Appropriate Chart Types: Choose chart types that effectively convey the message and suit the nature of the data (e.g., bar charts for comparison, line charts for trends).
    Simplicity: Opt for simplicity in design, avoiding unnecessary embellishments.
    Clear Labeling: Label axes, legends, and data points clearly.
    Consistent Formatting: Use a consistent color scheme and style.
    Data Accuracy: Verify the accuracy of data (it has to be related to the source provided) before creating visualizations.
    Titles: Tables and charts should have sequential numbered titles above them (e.g., "Table 1 – Table Title").

VIII. References and Citations:

    Accurate Citations: Cite all sources properly and consistently, following a recognized citation style (e.g., MLA, APA).
    Reputable Sources: Use reputable and industry-vetted sources.
    Current Sources: Use current sources (published within the last 5 years, if possible).
    Complete Reference List: Include a complete list of references in alphabetical order.

--------------- Ending -----------
End the report with the following sequence:

    <iframe class="visual-frame" srcdoc='
    <!DOCTYPE html>
    <html>
    </head>
        <body>
            <div>
-end-
            </div>
        </body>
    </html>' width="100px" height="15px" style="border:none;"></iframe>

Then close the html code from the broader report
</body>
</html>

--------------- Your turn -----------
Take a deep breath, do your best.
Now, produce the report please.
"""
    )
    tokentarget = word_count * 5  # rough multiplier for token target
    report = llm_call(prompt, model="o3-mini", max_tokens_param=tokentarget)
    # Post-processing
    report = re.sub(r'\{\[\{(.*?)\}\]\}', r'\1', report)
    report = re.sub(r'\[\{(.*?)\}\]', r'\1', report)
    
    # If the report is too long, compress it.
    if len(report) > MAX_MESSAGE_LENGTH:
        report = compress_text(report, MAX_MESSAGE_LENGTH)
    if report.startswith("Error calling OpenAI API"):
        logging.error(f"generate_final_report error: {report}")
        return f"Error generating report: {report}"
    logging.info("generate_final_report: Report generated successfully.")
    return report

# ============================================================================= Final report



def generate_tailored_questions(openai_api_key: str, query: str, existing_qa: str, existing_report: str, 
                                existing_log: str, crumbs: str) -> str:
    os.environ["OPENAI_API_KEY"] = openai_api_key
    query = query or ""
    existing_qa = existing_qa or ""
    existing_report = existing_report or ""
    existing_log = existing_log or ""
    crumbs = crumbs or ""
    prompt = (
        f"Context:\nSearch Topic: {query}\nExisting Clarification Q&A: {existing_qa}\nExisting Report: {existing_report}\n"
        f"Existing Process Log: {existing_log}\nCrumbs: {crumbs}\n\n"
        "Based on the above, generate FIVE new clarification questions, preferably open questions like 'What objectives would you like to reach with this search?' or 'Are there specific aspects you would like to search in particular such as xxx, yyy, ...?' "
        "Each question should be followed by 'Response:' then 2 empty lines."
    )
    new_questions = llm_call(prompt, model="gpt-4o", temperature=0, max_tokens_param=1000)
    logging.info(f"generate_tailored_questions: Generated questions: {new_questions}")
    if not new_questions.strip():
        new_questions = "No questions asked."
    return new_questions + "\n\n" + existing_qa

def backup_fields(research_query: str, 
                  include_domains: str, exclude_keywords: str, additional_clarifications: str, 
                  selected_engines, results_per_query, breadth, depth, clarification_text: str, 
                  existing_report: str, existing_log: str, crumbs_box: str, final_report: str, existing_queries_box: str) -> str:
    data = {
        "openai_api_key": "",
        "serpapi_api_key": "",
        "research_query": research_query,
        "include_domains": include_domains,
        "exclude_keywords": exclude_keywords,
        "additional_clarifications": additional_clarifications,
        "selected_engines": selected_engines,
        "results_per_query": results_per_query,
        "breadth": breadth,
        "depth": depth,
        "clarification_text": clarification_text,
        "existing_report": existing_report,
        "existing_log": existing_log,
        "crumbs_box": crumbs_box,
        "final_report": final_report,
        "existing_queries": existing_queries_box
    }
    backup_json = json.dumps(data, indent=2)
    logging.info(f"backup_fields: Data backed up: {backup_json}")
    return backup_json

def load_fields(backup_json: str):
    try:
        data = json.loads(backup_json)
        logging.info(f"load_fields: Loaded data: {data}")
        return (data.get("openai_api_key", ""),
                data.get("serpapi_api_key", ""),
                data.get("research_query", ""),
                data.get("include_domains", ""),
                data.get("exclude_keywords", ""),
                data.get("additional_clarifications", ""),
                data.get("selected_engines", []),
                data.get("results_per_query", 10),
                data.get("breadth", 4),
                data.get("depth", 2),
                data.get("clarification_text", ""),
                data.get("existing_report", ""),
                data.get("existing_log", ""),
                data.get("crumbs_box", ""),
                data.get("final_report", ""),
                data.get("existing_queries",""))
    except Exception as e:
        logging.error(f"load_fields error: {e}")
        return ("", "", "", "", "", "", [], 10, 4, 2, "", "", "", "", "", "")

def refine_query(query: str, openai_api_key: str, reportstyle: str) -> str:
    os.environ["OPENAI_API_KEY"] = openai_api_key
    if not query:
        logging.info("refine_query: Empty query provided.")
        return ""
    prompt = f"""Reformulate the following research query: '{query}'
    Use this following style: {reportstyle}
    The result should be a string of text, without any quotation mark before and after - Only the updated and refined version of the query, nothing else."""
    refined = llm_call(prompt, model="gpt-4o-mini", temperature=0, max_tokens_param=100)
    logging.info(f"refine_query: Refined query: {refined}")
    return refined

def validate_visual_html(html: str) -> bool:
    """Basic sanity check for generated visuals"""
    checks = [
        ("<svg" in html) or ("plotly" in html.lower()),
        "background" not in html.lower() or "#fff" in html.lower(),
        not re.search(r"color\s*:\s*#000000", html, re.I)
    ]
    return all(checks)

class ReportGenerator:
    def __init__(self, render_with_selenium: bool = False):
        # Flag to determine if we are rendering the final PDF using Selenium
        self.render_with_selenium = render_with_selenium

    def generate_report_html(self, solution_content: str) -> str:
        # Normalize text and fix dash characters.
        solution_content = unicodedata.normalize('NFKC', solution_content)
        solution_content = re.sub(r'[\u2010\u2011\u2012\u2013\u2014\u2015]', "-", solution_content)
        solution_content = re.sub(r'\[(.*?)\]\(.*?\)', r'\1', solution_content)
        html_content = markdown.markdown(solution_content, extensions=['extra', 'tables'])
        # Insert page break divs before key sections
        html_content = html_content.replace("<h2>Table of Contents</h2>", "<div class='page-break'></div><h2>Table of Contents</h2>")
        html_content = html_content.replace("<h2>Introduction</h2>", "<div class='page-break'></div><h2>Introduction</h2>")
        html_content = html_content.replace("<h2>Conclusion</h2>", "<div class='page-break'></div><h2>Conclusion</h2>")
        html_content = html_content.replace("<h2>References</h2>", "<div class='page-break'></div><h2>References</h2>")
        html_content = html_content.replace("<h2>Surprise-Me Extension Report</h2>", "<div class='page-break'></div><h2>Surprise-Me Extension Report</h2>")
        
        logging.info(f"ReportGenerator: HTML report generated successfully:\n{html_content}")
        return html_content

    def generate_report_pdf(self, solution_content: str, metadata: dict = None) -> bytes:
        # Generate the full HTML report (including text, placeholders, and mermaid visuals as iframes)
        html_report = self.generate_report_html(solution_content)

        # Add header if provided in metadata
        date_str = datetime.now().strftime("%Y-%m-%d")
        header = ""
        if metadata:
            header = (f"<p>Search Query: {metadata.get('Query name', 'N/A')}<br>"
                      f"Author: {metadata.get('User name', 'N/A')} | Date: {metadata.get('Date', date_str)}</p>")
        soup = BeautifulSoup(html_report, "html.parser")
        if soup.body:
            soup.body.insert(0, BeautifulSoup(header, "html.parser"))
        logging.info("ReportGenerator: Soup report generated:\n%s", soup)

        # Replace all iframes (class 'visual-frame') with images
        soup = replace_visual_iframes(soup)
        
        # Extract only the body content if <body> exists, otherwise use full HTML
        body_tag = soup.find("body")
        body_content = body_tag.decode_contents() if body_tag else str(soup)

        # Reassemble a clean HTML document with inline CSS styles for PDF conversion.
        final_html = f"""<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {{ 
            font-family: Helvetica, sans-serif; 
            margin: 40px; 
            background: white; 
        }}
        h1 {{ 
            font-size: 20pt; 
            margin-bottom: 12px; 
            text-align: left; 
            font-weight: bold;
        }}
        h2 {{ 
            font-size: 16pt; 
            margin-bottom: 10px; 
            text-align: left; 
            font-weight: bold;
        }}
        h3 {{ 
            font-size: 14pt; 
            margin-bottom: 8px; 
            text-align: left; 
            font-weight: bold;
        }}
        h4 {{ 
            font-size: 12pt; 
            text-align: left; 
            font-weight: bold;
        }}
        p {{ 
            font-size: 11pt; 
            line-height: 1.5; 
            margin-bottom: 10px; 
            white-space: pre-wrap;
        }}
        pre, div {{ 
            white-space: pre-wrap;
        }}
        ol, ul {{ 
            font-size: 11pt; 
            margin-left: 20px; 
            line-height: 1.5;
        }}
        hr {{ 
            border: 1px solid #ccc; 
            margin: 20px 0;
        }}
        /* ---------------------- TABLE STYLES ---------------------- */
        table {{ 
             border-collapse: collapse; 
             width: 100%; 
             margin-bottom: 10px; 
             table-layout: fixed;  /* Added to force a fixed layout for wrapping */
        }}
        th, td {{ 
             border: 1px solid #ccc; 
             padding: 8px; 
             text-align: left; 
             word-wrap: break-word;      /* Ensure long words are wrapped */
             overflow-wrap: break-word;  /* Modern equivalent */
        }}
        th {{ 
             background-color: #f2f2f2; 
        }}
        /* ------------------ FOCUS PLACEHOLDER STYLES ------------------ */
        .focus-placeholder {{
             border: 1px solid black;  /* This creates the outer border */
             padding: 10px;
             font-size: 12pt;
             line-height: 1.6;
             margin-bottom: 10px;
             word-wrap: break-word;
        }}
        /* Ensure that child elements inside the focus placeholder do not get extra borders */
        .focus-placeholder > * {{
             border: none;
        }}
        .page-break {{ 
             page-break-before: always; 
        }}
    </style>
  </head>
  <body>
    {body_content}
  </body>
</html>
"""
        logging.info("ReportGenerator: Final HTML for PDF conversion generated.")

        # Generate the PDF using xhtml2pdf (pisa)
        pdf_buffer = io.BytesIO()
        pisa_status = pisa.CreatePDF(final_html, dest=pdf_buffer, 
                                     link_callback=lambda uri, rel: uri)
        if pisa_status.err:
            logging.error("Error generating PDF with xhtml2pdf.")
            return None
        logging.info("ReportGenerator: PDF generated successfully.")
        return pdf_buffer.getvalue()

def handle_generate_report(query_name: str, user_name: str, final_report: str):
    try:
        report_generator = ReportGenerator(render_with_selenium=False)
        metadata = {
            "Query name": query_name,
            "User name": user_name,
            "Date": datetime.now().strftime("%Y-%m-%d"),
            "Time": datetime.now().strftime("%H:%M:%S"),
        }

        pdf_bytes = report_generator.generate_report_pdf(solution_content=final_report, metadata=metadata)
        with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
            tmp_file.write(pdf_bytes)
            tmp_path = tmp_file.name
        logging.info(f"handle_generate_report: PDF report generated at {tmp_path}")
        return "Report generated successfully.", gr.update(value=tmp_path, visible=True)
    except Exception as e:
        logging.error(f"handle_generate_report error: {e}", exc_info=True)
        return f"Error generating report: {str(e)}", None

def extract_summary_from_crumbs(crumbs_list: list) -> str:
    """
    Given a list of crumb records (each with 'url', 'summary', and 'full_content'),
    extract and aggregate only the summary parts.
    """
    aggregated = "\n".join([f"URL: {c['url']}\nSummary: {c['summary']}" for c in crumbs_list])
    logging.info("extract_summary_from_crumbs: Aggregated crumb summary created.")
    return aggregated

def generate_surprise_report(previous_report: str, crumbs_list: list, initial_query: str, reportstyle: str,
                             breadth: int, depth: int, followup_clarifications: str,
                             include_domains: str, exclude_keywords: str, additional_clarifications: str,
                             results_per_query: int, selected_engines, existing_queries: str) -> str:
    crumb_summary = "\n\n".join([f"Title: {c.get('title', 'No Title')}\nURL: {c['url']}\nSummary: {c['summary']}" for c in crumbs_list])
    new_prompt = ("Based on the previous research report:\n"
                  f"{previous_report}\n\n"
                  "And the following summaries of the scraped sources:\n"
                  f"{crumb_summary}\n\n"
                  f"And the initial query: '{initial_query}',\n\n"
                  "please propose a disruptive, experimental, and ambitious new research hypothesis that goes beyond conventional boundaries (Overton-window) related to the topic. "
                  "Formulate this as a new research query that could lead to innovative insights.")
    disruptive_query = llm_call(new_prompt, model="o3-mini", temperature=0, max_tokens_param=2000)
    logging.info(f"generate_surprise_report: Disruptive new query generated: {disruptive_query}")

    # Generate tailored clarification questions for the disruptive query
    clarifications_for_new = generate_tailored_questions(
        os.getenv("OPENAI_API_KEY"),
        disruptive_query + "\n\n IMPORTANT NOTE: in this specific iteration, generate also the responses for the questions asked (simulated)",
        "", "", "", ""
    )
    logging.info(f"generate_surprise_report: Clarification questions for new query: {clarifications_for_new}")

    # Run iterative deep research for the disruptive query
    generator = iterative_deep_research_gen(
        disruptive_query, reportstyle, breadth, depth, followup_clarifications,
        include_domains, exclude_keywords, additional_clarifications,
        extra_context="", selected_engines=selected_engines, results_per_query=results_per_query, existing_queries=existing_queries, go_deeper=1
    )
    extension_report = ""
    for progress, rep, proc_log, new_crumbs, existing_queries in generator:
        if rep is not None:
            extension_report = rep
            break
    logging.info("generate_surprise_report: Extension report generated successfully.")
    appended_report = previous_report + "\n\n<div style='page-break-before: always;'></div>\n<h2>Surprise-Me Extension Report</h2>\n\n" + clarifications_for_new + "\n\n" + extension_report
    return appended_report

def extract_structured_insights(html_text: str) -> str:
    """
    Extract only facts, figures, arguments, and quotes in a concise manner.
    Use BeautifulSoup to parse and remove anything not relevant to these categories.
    This function returns a short text suitable for summarization by the LLM.
    """
    soup = BeautifulSoup(html_text, "html.parser")

    # We can decide to keep paragraphs that contain digits (numbers),
    # or words like "claim", "argument", "quote", etc. This is just an example heuristic.
    paragraphs = soup.find_all('p')
    curated_excerpts = []
    for p in paragraphs:
        text = p.get_text().strip()
        # If it has digits or certain keywords, we keep it
        if re.search(r'\d+', text) or re.search(r'\bargument\b|\bfact\b|\bfigure\b|\bstudy\b|\bquote\b', text, re.IGNORECASE):
            curated_excerpts.append(text)

    # Combine them into a shorter snippet
    snippet = "\n".join(curated_excerpts)
    # If snippet is too short, fallback to the entire cleaned text
    if len(snippet.split()) < 30:
        snippet = clean_content(html_text)[:2000]  # or some fallback length

    return snippet

def generate_reference_table(references_list: list) -> str:
    # Build a simple HTML table with reference number, title, and URL.
    if not references_list:
        return "<p>No references available.</p>"
    table_html = "<table style='width:100%; border-collapse: collapse;'>"
    table_html += "<tr><th style='border: 1px solid #ccc; padding: 8px;'>Ref No.</th><th style='border: 1px solid #ccc; padding: 8px;'>Title/Description</th><th style='border: 1px solid #ccc; padding: 8px;'>URL</th></tr>"
    for ref_num, url in references_list:
        # In a more complete implementation, you might also retrieve a proper title from your scraped data.
        table_html += f"<tr><td style='border: 1px solid #ccc; padding: 8px;'>{ref_num}</td><td style='border: 1px solid #ccc; padding: 8px;'>{url}</td><td style='border: 1px solid #ccc; padding: 8px;'><a href='{url}'>{url}</a></td></tr>"
    table_html += "</table>"
    return table_html
    
def iterative_deep_research_gen(initial_query: str, reportstyle: str, breadth: int, depth: int,
                                followup_clarifications: str,
                                include_domains: str,
                                exclude_keywords: str,
                                additional_clarifications: str,
                                extra_context: str = "",
                                selected_engines=None,
                                results_per_query: int = 10,
                                existing_queries: str = "",
                                go_deeper: int = 8):
    overall_context = extra_context + f"Initial Query: {initial_query}\n"
    if followup_clarifications.strip():
        overall_context += "User Clarifications: " + followup_clarifications.strip() + "\n"
    process_log = "Starting research with context:\n" + overall_context + "\n"
    overall_learnings = []
    visited_urls = set()
    # Parse previously processed queries from existing_queries if provided
    processed_queries = set()
    for q_line in existing_queries.splitlines():
        q_line = q_line.strip()
        if q_line:
            processed_queries.add(q_line)
    crumbs_list = []
    ref_counter = 1
    references_list = []
    followup_suggestions = []
    logging.info("iterative_deep_research_gen: Research started.")
    for iteration in range(1, depth + 1):
        process_log += f"\n--- Iteration {iteration} ---\n"
        logging.info(f"iterative_deep_research_gen: Starting iteration {iteration}.")
        combined_context = overall_context
        if followup_suggestions:
            # Deduplicate follow-up suggestions before adding them to context.
            unique_suggestions = list(set(followup_suggestions))
            combined_context += "\n\nFollow-up suggestions: " + ", ".join(unique_suggestions)
        queries = generate_serp_queries(combined_context, breadth, depth, initial_query, selected_engines, results_per_query)

        # ===================================================================
        # Skip queries already in processed_queries
        filtered_query_tuples = []
        for q_tuple in queries:
            q_text, eng = q_tuple
            if q_text not in processed_queries:
                filtered_query_tuples.append(q_tuple)
                processed_queries.add(q_text)  # remember we've processed it
        # ===================================================================
        process_log += f"\nWill run {len(filtered_query_tuples)} new queries this iteration instead of {len(queries)} total.\n"
        iteration_learnings = []
        followup_suggestions = []  # reset for current iteration
        for query_tuple in filtered_query_tuples:
            query_str, engine = query_tuple
            mod_query = query_str
            if include_domains.strip():
                domains = [d.strip() for d in include_domains.split(",") if d.strip()]
                domain_str = " OR ".join([f"site:{d}" for d in domains])
                mod_query += f" ({domain_str})"
            if exclude_keywords.strip():
                for ex in [ex.strip() for ex in exclude_keywords.split(",") if ex.strip()]:
                    mod_query += f" -{ex}"
            process_log += f"\nPerforming SERPAPI search with query: {mod_query} using engine: {engine}\n"
            results = perform_serpapi_search(mod_query, engine, results_per_query)
            
            # Filter out visited results
            filtered_results = filter_search_results(results, visited_urls, initial_query, followup_clarifications)
            process_log += f"After filtering, {len(filtered_results)} results remain for processing.\n"
            for res in filtered_results:
                url = res.get("link", "")
                title = res.get("title", "No Title")
                if not url:
                    continue
                    
                raw_content = process_url(url)
                if raw_content.startswith("Error"):
                    process_log += f"{raw_content}\n"
                    continue
                    
                # Skip processing if raw_content is empty or too short (< 1000 characters)
                if not raw_content or len(raw_content) < 1000 or "could not be extracted" in raw_content.lower():
                    process_log += f"Content from {url} is too short (<500 characters), skipping.\n"
                    continue
                
                process_log += f"Successfully extracted content from {url}\n"
                
                # 1) Clean and do minimal parse
                cleaned_html = clean_content(raw_content)
                # 2) Extract structured data
                semantically_rich_snippet = extract_structured_insights(cleaned_html)
                # 3) Summarize with LLM
                analysis = analyze_with_gpt4o(initial_query, semantically_rich_snippet, breadth, url, title)
                
                # Analyze the cleaned content with GPT-4o-mini
                cleaned_text = clean_content(raw_content)  # Call the function to get a string.
                analysis = analyze_with_gpt4o(initial_query, cleaned_text, breadth, url, title)
                analysis_summary = analysis.get("summary", "")
                if isinstance(analysis_summary, (dict, list)):
                    analysis_summary = json.dumps(analysis_summary)
                analysis_summary = analysis_summary.strip()
                                
                process_log += f"Summary: {analysis.get('summary')}, Follow-ups: {analysis.get('followups')}\n"
                if not analysis_summary:
                    analysis_summary = (raw_content[:200] + "..." if len(raw_content) > 200 else raw_content)
                
                # Append crumb (even if not relevant)  
                crumbs_list.append({
                    "url": url,
                    "title": title,
                    "summary": analysis_summary,
                    "clean_content": clean_content,
                    "raw_excerpt": raw_content[:1000]
                })
                
                if analysis.get("relevant", "no").lower() == "yes":
                    if url.startswith("http://") or url.startswith("https://"):
                        link_str = f" <a href='{url}'>[{ref_counter}]</a>"
                    else:
                        link_str = f" [{ref_counter}]"
                    summary_with_ref = analysis_summary + link_str
                    iteration_learnings.append(summary_with_ref)
                    references_list.append((ref_counter, url))
                    logging.info(f"Appended crumb: URL: {url}, Title: {title}, Summary snippet: {analysis_summary[:100]}")
                    ref_counter += 1
                    if isinstance(analysis.get("followups"), list):
                        if iteration < depth:
                            followup_suggestions.extend(analysis.get("followups"))
        process_log += f"Iteration {iteration} extracted {len(iteration_learnings)} learnings.\n"
        logging.info(f"iterative_deep_research_gen: Iteration {iteration} extracted {len(iteration_learnings)} learnings.")
        if len(iteration_learnings) == 0:
            process_log += f"Iteration {iteration} extracted no learnings; proceeding to next iteration instead of aborting.\n"
            # Optionally, you could continue here rather than break.
            continue
        overall_learnings.extend(iteration_learnings)
        overall_context += f"\nIteration {iteration} learnings:\n" + "\n".join(iteration_learnings) + "\n"
        if additional_clarifications.strip():
            overall_context += "\nAdditional Clarifications from user: " + additional_clarifications.strip() + "\n"
            process_log += "Appended additional clarifications to the context.\n"
        progress_pct = int((iteration / depth) * 100)
        yield (f"Progress: {progress_pct}%", None, None, None, None)

    if breadth > 3 and depth > 2:
        filtered_crumbs_list = filter_crumbs_in_batches(crumbs_list, initial_query, followup_clarifications)
    else:
        filtered_crumbs_list = crumbs_list

    # Now build aggregated crumb text from filtered_crumbs_list only
    aggregated_crumbs = "\n\n".join([
        f"Title: {c.get('title','No Title')}\nURL: {c['url']}\nSummary: {c['summary']}"
        for c in filtered_crumbs_list
    ])
    final_report = generate_final_report(initial_query, combined_context, reportstyle, overall_learnings, list(visited_urls), aggregated_crumbs, references_list, pages=go_deeper)
    
    # --- NEW STEP: Post-process final_report to replace visual and focus placeholders ---
    final_report = replace_visual_placeholders(final_report, combined_context, initial_query, aggregated_crumbs)
    final_report = replace_focus_placeholders(final_report, combined_context, initial_query, aggregated_crumbs)
    final_report = replace_graph_placeholders(final_report, combined_context, initial_query, aggregated_crumbs)
    
    alignment_assessment = assess_report_alignment(final_report, initial_query, followup_clarifications)
    final_report = final_report.replace(
        "<p>---end---</p>    </div>  </body></html>",
        f"<p>---------</p><p><b>Report alignment assessment:</b> {alignment_assessment}</p>    </div>  </body></html>"
    )
    logging.info("iterative_deep_research_gen: Final report generated.")
    # We convert processed_queries to a string suitable for storing
    all_processed_queries_str = "\n".join(sorted(processed_queries))

    yield ("", final_report, process_log, aggregated_crumbs, all_processed_queries_str)

def filter_crumbs_in_batches(crumbs_list: list, initial_query: str, clarifications: str) -> list:
    accepted = []
    batch_size = 20
    for i in range(0, len(crumbs_list), batch_size):
        batch = crumbs_list[i:i+batch_size]
        # Build a prompt describing each crumb
        prompt = f"""
We have a set of crumbs. For each crumb, decide if it adds new facts, figures, references, or quotes to the topic being investigated.
Don't be too selective, we still need to keep between 10 and 15 in each batch out of {batch_size}.
Mark 'yes' if it is valuable for the final report, otherwise 'no'. Output JSON.
Ensure consistency with the initial query:
{initial_query}

And the provided clarifications:
{clarifications}

Previously selected batch:

Now provide the JSON
"""
        listing = []
        for idx, c in enumerate(batch):
            snippet_for_prompt = c["summary"][:1000]  # short snippet
            listing.append(f"Crumb {idx}: {snippet_for_prompt}")
        prompt += "\n".join(listing)

        prompt += """
Return a JSON object with structure:
{
 "0": "yes" or "no",
 "1": "yes" or "no",
 ...
}

No code fences, no 'json' mentioned before the result, only the json result formatted.
"""
        decision_str = llm_call(prompt, model="o3-mini", temperature=0, max_tokens_param=1500)
        # parse JSON
        try:
            decisions = json.loads(decision_str)
        except:
            decisions = {}
        for idx, c in enumerate(batch):
            d = decisions.get(str(idx), "no").lower()
            if d == "yes":
                accepted.append(c)
    return accepted
    
def assess_report_alignment(report: str, initial_query: str, clarifications: str) -> str:
    prompt = f"""
Please assess the following research report in terms of its alignment with the initial user request and the clarification Q&A provided.
Ensure that the report covers key points of the topic.

Initial Query:
{initial_query}

Clarifications:
{clarifications}

Research Report:
{report}

Provide a short assessment in one paragraph (between 2 html div tags) on how well the report aligns with these requirements, you can put in html tags for bold or italic.
The output will be integrated in a html page for rendering.
"""
    assessment = llm_call(prompt, model="o3-mini", temperature=0, max_tokens_param=400)
    logging.info(f"assess_report_alignment: Assessment result: {assessment}")
    return assessment

def run_deep_research(openai_api_key: str, serpapi_api_key: str, initial_query: str, reportstyle: str, breadth: int, depth: int,
                      followup_clarifications: str, include_domains: str,
                      exclude_keywords: str, additional_clarifications: str,
                      results_per_query: int, selected_engines, existing_crumbs: str, existing_report: str, existing_log: str,
                      existing_queries: str, pages: str, surprise_me: bool):
    if not openai_api_key or not serpapi_api_key:
        logging.error("run_deep_research: Invalid API keys provided.")
        return "Please input valid API keys", "", "", "", ""

    os.environ["OPENAI_API_KEY"] = openai_api_key
    os.environ["SERPAPI_API_KEY"] = serpapi_api_key

    extra_context = ""
    if existing_report:
        extra_context += f"Existing Report:\n{existing_report}\n"
    if existing_log:
        extra_context += f"Existing Log:\n{existing_log}\n"
    if existing_crumbs:
        extra_context += f"Existing Crumbs:\n{existing_crumbs}\n"

    final_progress = ""
    final_report = ""
    final_process_log = ""
    final_crumbs = ""
    logging.info("run_deep_research: Starting deep research process.")
    for progress, rep, proc_log, crumbs, all_processed_queries_str in iterative_deep_research_gen(
            initial_query, reportstyle, breadth, depth, followup_clarifications,
            include_domains, exclude_keywords, additional_clarifications,
            extra_context, selected_engines, results_per_query, existing_queries, go_deeper=int(pages)):
        if rep is None:
            final_progress = progress
            yield final_progress, None, None, None, None, all_processed_queries_str
        else:
            final_report = rep
            final_process_log = proc_log
            final_crumbs = crumbs
            break
    if surprise_me:
        extended_report = generate_surprise_report(
            final_report, final_crumbs, initial_query, reportstyle, breadth, depth,
            followup_clarifications, include_domains, exclude_keywords, additional_clarifications,
            results_per_query, selected_engines, existing_queries
        )
        final_report = extended_report
        final_progress = "Progress: 100% (\"Surprise Me\" extension complete)"
    logging.info("run_deep_research: Deep research process completed.")
    yield (final_progress, final_report, final_report, final_process_log, final_crumbs, all_processed_queries_str)

def load_example(example_choice: str) -> str:
    filename = ""
    if example_choice == "Implications of the release of advanced Deep Research solutions":
        filename = "example1.txt"
    elif example_choice == "AI regulation in finance":
        filename = "example2.txt"
    elif example_choice == "AI top voices":
        filename = "example3.txt"
    try:
        with open(filename, "r", encoding="utf-8") as f:
            content = f.read()
        logging.info(f"load_example: Loaded content from {filename}")
        return content
    except Exception as e:
        logging.error(f"load_example:  Error loading {filename}: {e}")
        return ""

def main():
    custom_css = """
    /* Overall container customization */
    .gradio-container {
        max-width: 2000px;
        margin: auto;
        padding: 15px;
        background: linear-gradient(135deg, #f8f9fa, #e9ecef);
    }
    body {
        background: linear-gradient(135deg, #fefefe, #f4f4f8);
    }
    h1, h2, h3, label {
        color: #343a40;
        font-weight: bold;
    }
    .gr-button {
        background-color: #007bff;
        color: #fff;
        font-weight: bold;
        border-radius: 5px;
        border: none;
        padding: 8px 16px;
    }
    #final-report {
        background-color: #fafaf5;
        box-shadow: 0px 2px 5px rgba(0,0,0,0.1);
        max-width: 1400px;
        margin: auto;
        padding: 20px;
    }
    /* Progress display style */
    #progress-display {
        text-align: center;
        font-size: 1.2em;
        color: #007bff;
        margin-bottom: 10px;
    }
    """

    with gr.Blocks(css=custom_css, title="DeepSearch It – MyWay") as demo:
        with gr.Row():
            with gr.Column():
                gr.Markdown("# DeepSearch It  – MyWay")
                gr.Markdown("### === by T.Sakai / G.Huet ===")
                gr.Markdown("Input your API Keys, choose your search parameters (engine, depth, breadth, results, exclusions...), answer the clarifications questions and let the DeepSearch start.")
            gr.Image(value=display_image, height=300, width=300, interactive=False, show_label=False, show_download_button=False, show_share_button=False, show_fullscreen_button=False, elem_id="image")

        with gr.Accordion("1] API Keys", open=False):
            openai_api_key_input = gr.Textbox(label="OpenAI API Key", placeholder="Enter your OpenAI API Key here...", type="password")
            serpapi_api_key_input = gr.Textbox(label="SERPAPI API Key", placeholder="Enter your SERPAPI API Key here...", type="password")
            gr.Markdown("[Create OpenAI API Key](https://platform.openai.com/account/api-keys) | [Create SERPAPI API Key](https://serpapi.com/manage-api-key)")
            gr.Markdown("You can check the open-source code - None of the user API keys are stored or logged.")

        with gr.Accordion ("2] Research topic", open=False):
            with gr.Row():
                research_query = gr.Textbox(label="Research Query", placeholder="Enter your research query here...", lines=2, elem_id="research-query", scale=4)
                with gr.Column(scale=1):
                    reportstyle = gr.Textbox(label="Report style", placeholder="The report style", lines=1, value="Academic style")
                    refine_query_button = gr.Button("Refine my Query")

        with gr.Accordion("3] Q&A", open=False):
            with gr.Row():
                gen_followups = gr.Button("Generate Tailored Clarification Questions", scale=1)
                clarification_text = gr.Textbox(label="Clarification / Follow-Up Questions", placeholder="Tailored clarifying suggestions will appear here...", lines=6, scale = 4)
                
        with gr.Accordion("4] Search Parameters", open=False):
            with gr.Column():
                with gr.Row():
                    with gr.Column():
                        include_domains = gr.Textbox(label="Include Domains", placeholder="e.g., example.com, anotherdomain.org", lines=1)
                        exclude_keywords = gr.Textbox(label="Exclude Keywords/Mentions", placeholder="e.g., politics, rumor", lines=1)
                    with gr.Column():
                        pages_dropdown = gr.Dropdown(label="Report Length (pages)", choices=[str(i) for i in range(4, 41)], value="8")
                        surprise_me_checkbox = gr.Checkbox(label="Surprise me", value=False)
                with gr.Column():
                    additional_clarifications = gr.Textbox(label="Additional Clarifications", placeholder="Clarifications to be applied between iterations", lines=2, scale=4)
            selected_engines = gr.CheckboxGroup(label="Specific engines to Use (AI to choose by default)", choices=[
                "google",
                "google_jobs_listing",
                "google_trends",
                "google_news",
                "google_scholar",
                "google_ai_overview",
                "bing",
                "bing_news",
                "baidu",
                "baidu_news",
                "yandex",
                "youtube_video",
                "linkedin",
                "linkedin_profile",
                "duckduckgo_news",
                "yelp_reviews"
            ])
            with gr.Row():
                results_per_query = gr.Slider(label="Results per Query", minimum=5, maximum=20, step=1, value=10)
                breadth = gr.Slider(label="Research Breadth (queries per iteration)", minimum=0, maximum=6, step=1, value=2)
                depth = gr.Slider(label="Research Depth (iterations)", minimum=1, maximum=2, step=1, value=2)

        with gr.Accordion("5] Report", open=False, elem_classes="folder"):
            progress_display = gr.Markdown("", elem_id="progress-display")
            run_btn = gr.Button("Generate report")
            with gr.Accordion("Improvement query", open=False, elem_classes="folder"):
                with gr.Row():
                    adjustmentguidelines = gr.Textbox(label="Improvement instructions", placeholder="Input here your directions.\n\nFor example:\n- 'remove the following text:[[ABC]]'\n- 'fix visual after section [[XYZ]]'", lines=5)
                    with gr.Column():
                        suggest_improvements_button = gr.Button("Suggest Improvements")
                        fine_tune_button = gr.Button("Improve the Report")
                    with gr.Column():
                        suggest_expansions_button = gr.Button("Suggest Expansion")
                        expand_button = gr.Button("Expand the Report")
            with gr.Accordion("PDF generation", open=False, elem_classes="folder"):
                with gr.Column():
                    query_name = gr.Textbox(label="Query name", placeholder="Enter query name...", lines=1)
                    user_name = gr.Textbox(label="User name", placeholder="Enter your name...", lines=1)
                    report_status = gr.Textbox(label="Report Status", interactive=False, lines=2, value="Click 'Generate Report' to create your PDF report.")
                    report_file = gr.File(label="Download Report", visible=False, interactive=False, file_types=[".pdf"])
                    generate_button = gr.Button("Create pdf report")
            final_report = gr.HTML(label="Final Report", max_height = 800, min_height = 200, elem_id="final-report")

        
        with gr.Accordion("6] Extra Context (Crumbs, Existing Report & Log, Processed Queries)", open=False):
            existing_report = gr.Textbox(label="Existing Report (if any)", placeholder="Paste previously generated report here...", lines=4)
            existing_log = gr.Textbox(label="Existing Process Log (if any)", placeholder="Paste previously generated log here...", lines=4)
            crumbs_box = gr.Textbox(label="Existing Crumbs (All scraped sources, JSON)", placeholder="Paste existing crumbs JSON here...", lines=4)
            existing_queries_box = gr.Textbox(label="Existing Queries (processed queries)", placeholder="Paste processed queries here...", lines=4)

        with gr.Accordion("7] Chat with search", open=False):
            chatbot = gr.Chatbot(label="Chat with Search")
            chat_input = gr.Textbox(
                label="Your Message", 
                placeholder="Ask a question about the research report or its sources..."
            )
            send_button = gr.Button("Send")
        
        with gr.Accordion("8] Backup / Restore Fields", open=False):
            with gr.Row():
                backup_button = gr.Button("Backup Fields")
                load_button = gr.Button("Load Fields")
            backup_text = gr.Textbox(label="Backup JSON", placeholder="Backup output will appear here. You can also paste JSON here to load fields.", lines=6, interactive=True)

        with gr.Accordion("-- EXAMPLES --", open=False):
            example_dropdown = gr.Dropdown(label="Select an Example", choices=["Implications of the release of advanced Deep Research solutions", "AI regulation in finance", "AI top voices"])
            load_example_button = gr.Button("Load Example")

        # Button actions
        refine_query_button.click(
            fn=refine_query,
            inputs=[research_query, openai_api_key_input, reportstyle],
            outputs=[research_query]
        )
        gen_followups.click(
            fn=generate_tailored_questions,
            inputs=[openai_api_key_input, research_query, clarification_text, existing_report, existing_log, crumbs_box],
            outputs=clarification_text
        )
        run_btn.click(
            fn=run_deep_research,
            inputs=[openai_api_key_input, serpapi_api_key_input, research_query, reportstyle, breadth, depth, clarification_text, include_domains, exclude_keywords,
                    additional_clarifications, results_per_query, selected_engines, existing_report, existing_log, existing_queries_box, crumbs_box,
                    pages_dropdown, surprise_me_checkbox],
            outputs=[progress_display, final_report, existing_report, existing_log, crumbs_box, existing_queries_box],
            show_progress=True,
            api_name="deep_research"
        )
        backup_button.click(
            fn=backup_fields,
            inputs=[research_query, include_domains, exclude_keywords,
                    additional_clarifications, selected_engines, results_per_query, breadth, depth, clarification_text, existing_report, existing_log, crumbs_box, existing_report, existing_queries_box],
            outputs=[backup_text]
        )
        load_button.click(
            fn=load_fields,
            inputs=[backup_text],
            outputs=[openai_api_key_input, serpapi_api_key_input, research_query, include_domains, exclude_keywords,
                     additional_clarifications, selected_engines, results_per_query, breadth, depth, clarification_text, existing_report, existing_log, crumbs_box, final_report, existing_queries_box]
        )
        load_example_button.click(
            fn=load_example,
            inputs=[example_dropdown],
            outputs=[backup_text]
        )
        generate_button.click(
            fn=handle_generate_report,
            inputs=[query_name, user_name, final_report],
            outputs=[report_status, report_file]
        ) 

        fine_tune_button.click(
            fn=fine_tune_report,
            inputs=[adjustmentguidelines, openai_api_key_input, serpapi_api_key_input, final_report, research_query, clarification_text, reportstyle, crumbs_box, additional_clarifications],
            outputs=[final_report, clarification_text]
        )

        expand_button.click(
            fn=expand_report,
            inputs=[adjustmentguidelines, openai_api_key_input, serpapi_api_key_input, final_report, research_query, clarification_text, reportstyle, crumbs_box, additional_clarifications],
            outputs=[final_report, clarification_text]
        )
        
        suggest_expansions_button.click(
            fn=suggest_expansions,
            inputs=[final_report, openai_api_key_input, serpapi_api_key_input, research_query, clarification_text, crumbs_box, additional_clarifications],
            outputs=[adjustmentguidelines]
        )
        
        suggest_improvements_button.click(
            fn=suggest_improvements,
            inputs=[final_report, openai_api_key_input, serpapi_api_key_input, research_query, clarification_text, crumbs_box, additional_clarifications],
            outputs=[adjustmentguidelines]
        )
        
        send_button.click(
            fn=send_chat_message,
            inputs=[chat_input, openai_api_key_input, serpapi_api_key_input, chatbot, final_report, crumbs_box, reportstyle, research_query, clarification_text, additional_clarifications],
            outputs=[chatbot, chat_input, final_report]
        )
        
        demo.launch()

if __name__ == "__main__":
            main()