File size: 92,075 Bytes
f34af6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gradio UI entrypoint for the FlowProt Hugging Face Docker Space."""

from __future__ import annotations

import base64
import csv
import html
import json
import logging
import os
import shutil
import traceback
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Tuple

import gradio as gr
import pandas as pd

from inference import (
    ArtifactResolutionError,
    FlowProtInferenceService,
    InferenceError,
    InferenceResult,
    ModelLoadError,
)
from self_consistency import (
    FlowProtSelfConsistencyService,
    SelfConsistencyError,
)
from viewer_alignment import AlignmentError, align_folded_to_backbone_overlay

logging.basicConfig(
    level=os.getenv("FLOWPROT_LOG_LEVEL", "INFO").upper(),
    format="%(asctime)s %(levelname)s %(name)s - %(message)s",
)
LOGGER = logging.getLogger(__name__)

SERVICE = FlowProtInferenceService()
SC_SERVICE = FlowProtSelfConsistencyService()
VIEWER_HEIGHT = 620
SC_SUMMARY_HEADERS = ["Metric", "Value"]
SC_METRICS_HEADERS = ["Sample", "Seq len", "scTM", "scRMSD", "ESMFold mean pLDDT"]
SC_TOP_TM_HEADERS = ["Rank", "Sample", "Seq len", "scTM"]
SC_TOP_RMSD_HEADERS = ["Rank", "Sample", "Seq len", "scRMSD"]
EXAMPLE_CASE_DIR = (
    Path(__file__).resolve().parent / "examples" / "flowprot_space_example"
).resolve()
EXAMPLE_CASE_SAMPLE_DIR = EXAMPLE_CASE_DIR / "sample"
EXAMPLE_CASE_MANIFEST = EXAMPLE_CASE_DIR / "manifest.json"
SPACE_OUTPUTS_DIR = (Path(__file__).resolve().parent / "space_outputs").resolve()
RUN_HISTORY_PATH = SPACE_OUTPUTS_DIR / "run_history.json"
RUN_HISTORY_LIMIT = 25


def _env_flag(name: str, default: bool = False) -> bool:
    raw = os.getenv(name)
    if raw is None:
        return default
    return str(raw).strip().lower() in {"1", "true", "yes", "on"}


VIEW_EXAMPLE_ON_STARTUP_DEFAULT = _env_flag("FLOWPROT_VIEW_EXAMPLE_ON_STARTUP", default=False)
SC_DEFAULT_TOP_N = 3
SC_TM_MIN = 0.0
SC_TM_MAX = 1.0
SC_RMSD_MIN = 0.0
SC_RMSD_MAX = 10.0
SC_PLDDT_MIN = 0.0
SC_PLDDT_MAX = 1.0
UI_CSS = """

.gradio-container {

  max-width: 1400px !important;

  margin: 0 auto !important;

  padding-left: 14px !important;

  padding-right: 14px !important;

}

.fp-card {

  border: 1px solid var(--block-border-color);

  border-radius: 14px;

  padding: 14px;

  margin-bottom: 12px;

  background: var(--block-background-fill);

  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);

}

.fp-tight {

  gap: 12px !important;

}

.fp-status textarea {

  min-height: 100px !important;

}

.fp-debug-note {

  color: var(--body-text-color-subdued);

  font-size: 0.92rem;

}

.fp-subnote {

  color: var(--body-text-color-subdued);

  font-size: 0.9rem;

}

.fp-main-row {

  align-items: stretch;

}

.fp-plot-row {

  align-items: stretch;

}

@media (max-width: 980px) {

  .gradio-container {

    padding-left: 10px !important;

    padding-right: 10px !important;

  }

  .fp-main-row {

    flex-direction: column !important;

  }

  .fp-status textarea {

    min-height: 88px !important;

  }

}

@media (max-width: 1280px) {

  .fp-plot-row {

    flex-direction: column !important;

  }

}

"""


def _viewer_height_css() -> str:
    return f"min(72vh, {VIEWER_HEIGHT}px)"


def _molstar_placeholder_html(message: Optional[str] = None) -> str:
    placeholder_message = message or "Generate a sample and select it to load in Mol*."
    return (
        f"<div style=\"height: {_viewer_height_css()}; border: 1px solid var(--block-border-color, #d9d9d9); border-radius: 8px; "
        "display: flex; align-items: center; justify-content: center; color: var(--body-text-color-subdued, #666666); background: var(--block-background-fill, #ffffff);\">"
        f"{html.escape(placeholder_message)}"
        "</div>"
    )


def _mode_choices() -> List[str]:
    choices = ["unconditional"]
    if SERVICE.classifier_enabled:
        choices.append("classifier")
    if SERVICE.conditional_enabled:
        choices.append("conditional")
    return choices


def _empty_sc_plot_df() -> pd.DataFrame:
    # Keep numeric columns explicitly typed so Gradio/Altair infer quantitative axes.
    return pd.DataFrame(
        {
            "sample": pd.Series(dtype="string"),
            "plot_color": pd.Series(dtype="string"),
            "scTM": pd.Series(dtype="float64"),
            "scRMSD": pd.Series(dtype="float64"),
            "esmfold_mean_plddt": pd.Series(dtype="float64"),
        }
    )


def _empty_scatter_plot_html(title: str, message: str) -> str:
    return (
        "<div style=\"border: 1px solid var(--block-border-color, #d9d9d9); border-radius: 10px; "
        "background: var(--block-background-fill, #ffffff); min-height: 420px; padding: 10px 12px;\">"
        f"<div style=\"font-weight: 600; margin-bottom: 6px;\">{html.escape(title)}</div>"
        "<div style=\"height: 360px; display:flex; align-items:center; justify-content:center; "
        "color: var(--body-text-color-subdued, #666666);\">"
        f"{html.escape(message)}"
        "</div>"
        "</div>"
    )


def _empty_sc_outputs() -> Tuple[
    str,
    List[List[str]],
    List[List[object]],
    str,
    str,
    List[str],
    List[List[object]],
    List[List[object]],
]:
    return (
        "",
        [],
        [],
        _empty_scatter_plot_html(
            title="Self-consistency tradeoff (scTM vs scRMSD)",
            message="Run self-consistency to render plot data.",
        ),
        _empty_scatter_plot_html(
            title="ESMFold confidence vs scTM",
            message="Run self-consistency with folding enabled to render confidence plot.",
        ),
        [],
        [],
        [],
    )


def _empty_sc_state() -> Tuple[str, Dict[str, object], List[Dict[str, object]], List[str]]:
    return "", {}, [], []


def _empty_example_view_outputs() -> Tuple[
    str,
    List[str],
    List[str],
    Dict[str, object],
    str,
    str,
    List[List[str]],
    List[List[object]],
    pd.DataFrame,
    pd.DataFrame,
    List[str],
    List[List[object]],
    List[List[object]],
    str,
    Dict[str, object],
    List[Dict[str, object]],
    List[str],
    str,
]:
    empty_sc = _empty_sc_outputs()
    empty_state = _empty_sc_state()
    return (
        "",
        [],
        [],
        gr.update(choices=[], value=[], interactive=False),
        _molstar_placeholder_html(),
        *empty_sc,
        *empty_state,
        "",
    )


def _resolve_saved_example_sample() -> Optional[str]:
    if EXAMPLE_CASE_MANIFEST.exists():
        try:
            payload = json.loads(EXAMPLE_CASE_MANIFEST.read_text(encoding="utf-8"))
            manifest_sample = payload.get("example_sample_path")
            if manifest_sample:
                path = Path(str(manifest_sample))
                if path.exists():
                    return path.resolve().as_posix()
        except Exception:
            LOGGER.warning("Failed to parse example manifest at %s", EXAMPLE_CASE_MANIFEST)

    fallback_sample = EXAMPLE_CASE_SAMPLE_DIR / "sample.pdb"
    if fallback_sample.exists():
        return fallback_sample.resolve().as_posix()
    return None


def _find_latest_generated_sample() -> Optional[Path]:
    output_root = (Path(__file__).resolve().parent / "space_outputs").resolve()
    if not output_root.exists():
        return None
    candidates = sorted(
        output_root.glob("space_*/length_*/sample_*/sample.pdb"),
        key=lambda path: path.stat().st_mtime,
        reverse=True,
    )
    return candidates[0] if candidates else None


def _normalize_selected_samples(selected_samples: Optional[object]) -> List[str]:
    if selected_samples is None:
        return []
    if isinstance(selected_samples, str):
        value = selected_samples.strip()
        return [value] if value else []
    if isinstance(selected_samples, (list, tuple, set)):
        normalized: List[str] = []
        for raw in selected_samples:
            if raw is None:
                continue
            value = str(raw).strip()
            if value:
                normalized.append(value)
        return normalized
    value = str(selected_samples).strip()
    return [value] if value else []


def _sample_label_from_path(sample_path: str, metric: Optional[Dict[str, object]] = None) -> str:
    path = Path(sample_path)
    sample_dir_name = path.parent.name
    sample_id = sample_dir_name.replace("sample_", "", 1) if sample_dir_name.startswith("sample_") else sample_dir_name
    esmfold_id: object = "x"
    if metric is not None and metric.get("index") is not None:
        esmfold_id = metric.get("index")
    return f"sample_{sample_id}_{esmfold_id}"


def _enrich_metric_sample_fields(metric: Dict[str, object], sample_path: str) -> Dict[str, object]:
    enriched = dict(metric)
    enriched["sample_source_path"] = sample_path
    enriched["sample_display"] = _sample_label_from_path(sample_path, metric=metric)
    # Keep legacy key expected by existing views/tooltips.
    enriched["sample"] = enriched["sample_display"]
    return enriched


def _metric_sample_display(metric: Dict[str, object]) -> str:
    value = metric.get("sample_display") or metric.get("sample")
    return str(value) if value is not None else "unknown"


def _metric_sequence_length(metric: Dict[str, object]) -> Optional[int]:
    sequence = metric.get("sequence")
    if sequence is None:
        return None
    return len(str(sequence))


def _normalize_optional_text(value: Optional[object]) -> str:
    if value is None:
        return ""
    return str(value).strip()


def _parse_optional_seed(seed_override: Optional[object]) -> Optional[int]:
    text = _normalize_optional_text(seed_override)
    if not text:
        return None
    try:
        return int(text)
    except ValueError as exc:
        raise InferenceError("Seed override must be an integer value.") from exc


def _actionable_error_hint(exc: object) -> str:
    text = str(exc).lower()
    if "out of memory" in text or "cuda" in text and "memory" in text:
        return (
            " Hint: the GPU ran out of memory. Try a shorter length, fewer samples, "
            "or fewer sampling timesteps."
        )
    if "no model artifact source" in text or "artifact" in text:
        return (
            " Hint: configure a checkpoint via FLOWPROT_CKPT_PATH, FLOWPROT_CKPT_DIR, "
            "or FLOWPROT_HF_REPO_ID before running inference."
        )
    if "esmfold" in text or "transformers" in text:
        return " Hint: ESMFold could not run. Disable folding or verify the ESMFold model is available."
    return ""


def _short_error_text(message: object, max_chars: int = 180) -> str:
    raw = str(message).strip()
    if not raw:
        return "unknown error"
    first_line = raw.splitlines()[0].strip()
    if len(first_line) <= max_chars:
        return first_line
    return first_line[: max_chars - 3].rstrip() + "..."


def _build_sc_filter_sample_choices(metrics: Sequence[Dict[str, object]]) -> List[str]:
    ordered_unique: List[str] = []
    seen = set()
    for item in metrics:
        label = _metric_sample_display(item)
        if label in seen:
            continue
        seen.add(label)
        ordered_unique.append(label)
    return ordered_unique


def _build_folded_choices(metrics: Sequence[Dict[str, object]]) -> List[Tuple[str, str]]:
    choices: List[Tuple[str, str]] = []
    for item in metrics:
        folded_path = _normalize_optional_text(item.get("folded_sample_path"))
        if not folded_path:
            continue
        sc_tm = item.get("scTM")
        sc_rmsd = item.get("scRMSD")
        sequence_index = item.get("index")
        sample_display = _metric_sample_display(item)
        choice_label = (
            f"{sample_display} | seq#{sequence_index} | "
            f"scTM={float(sc_tm):.4f} | scRMSD={float(sc_rmsd):.4f}"
            if sc_tm is not None and sc_rmsd is not None
            else f"{sample_display} | seq#{sequence_index}"
        )
        choices.append((choice_label, folded_path))
    return choices


def _build_sc_summary(num_selected_samples: int, num_processed_samples: int, metrics: List[Dict[str, object]]) -> Dict[str, object]:
    summary: Dict[str, object] = {
        "num_samples_selected": num_selected_samples,
        "num_samples_processed": num_processed_samples,
        "num_sequences_evaluated": len(metrics),
    }
    if metrics:
        tm_values = [float(item["scTM"]) for item in metrics if item.get("scTM") is not None]
        rmsd_values = [float(item["scRMSD"]) for item in metrics if item.get("scRMSD") is not None]
        plddt_values = [
            float(item["esmfold_mean_plddt"])
            for item in metrics
            if item.get("esmfold_mean_plddt") is not None
        ]
        if tm_values:
            summary["mean_scTM"] = float(sum(tm_values) / len(tm_values))
            summary["max_scTM"] = max(tm_values)
            summary["min_scTM"] = min(tm_values)
        if rmsd_values:
            summary["mean_scRMSD"] = float(sum(rmsd_values) / len(rmsd_values))
            summary["min_scRMSD"] = min(rmsd_values)
            summary["max_scRMSD"] = max(rmsd_values)
        if plddt_values:
            summary["mean_esmfold_plddt"] = float(sum(plddt_values) / len(plddt_values))
    return summary


def _safe_float(value: Optional[str]) -> Optional[float]:
    if value is None:
        return None
    text = str(value).strip()
    if not text:
        return None
    try:
        return float(text)
    except ValueError:
        return None


def _load_sc_metrics_from_csv(csv_path: Path) -> List[Dict[str, object]]:
    metrics: List[Dict[str, object]] = []
    with csv_path.open("r", encoding="utf-8", newline="") as handle:
        reader = csv.DictReader(handle)
        for row in reader:
            index_raw = row.get("index")
            index_value: object = index_raw
            if index_raw not in (None, ""):
                try:
                    index_value = int(str(index_raw))
                except ValueError:
                    index_value = index_raw
            metrics.append(
                {
                    "index": index_value,
                    "header": row.get("header"),
                    "sequence": row.get("sequence"),
                    "scTM": _safe_float(row.get("scTM")),
                    "scRMSD": _safe_float(row.get("scRMSD")),
                    "esmfold_mean_plddt": _safe_float(row.get("esmfold_mean_plddt")),
                    "folded_sample_path": row.get("folded_sample_path"),
                }
            )
    return metrics


def _collect_loaded_sc_artifacts(run_dir: Path, metrics_csv: Path, metrics: List[Dict[str, object]]) -> List[str]:
    artifacts: List[str] = []
    parsed_jsonl = run_dir / "parsed_pdbs.jsonl"
    mpnn_fasta = run_dir / "seqs" / "sample.fa"
    if parsed_jsonl.exists():
        artifacts.append(parsed_jsonl.resolve().as_posix())
    if mpnn_fasta.exists():
        artifacts.append(mpnn_fasta.resolve().as_posix())

    esmf_dir = run_dir / "esmf"
    if esmf_dir.exists():
        folded = sorted(esmf_dir.glob("*.pdb"))
        artifacts.extend(path.resolve().as_posix() for path in folded)
    else:
        for item in metrics:
            folded_path = item.get("folded_sample_path")
            if not folded_path:
                continue
            path = Path(str(folded_path))
            if path.exists():
                artifacts.append(path.resolve().as_posix())

    if metrics_csv.exists():
        artifacts.append(metrics_csv.resolve().as_posix())
    return artifacts


def _hydrate_sc_outputs_for_samples(selected_samples: Optional[object]) -> Tuple[
    str,
    List[List[str]],
    List[List[object]],
    pd.DataFrame,
    pd.DataFrame,
    List[str],
    List[List[object]],
    List[List[object]],
    str,
    Dict[str, object],
    List[Dict[str, object]],
    List[str],
]:
    sample_values = _normalize_selected_samples(selected_samples)
    if not sample_values:
        return (*_empty_sc_outputs(), *_empty_sc_state())

    combined_metrics: List[Dict[str, object]] = []
    combined_artifacts: List[str] = []
    processed_samples = 0

    for sample_value in sample_values:
        sample_path = Path(sample_value)
        sample_dir = sample_path.parent
        sc_root = sample_dir / "self_consistency"
        if not sc_root.exists():
            continue

        run_dirs = sorted(
            [path for path in sc_root.iterdir() if path.is_dir()],
            key=lambda path: path.stat().st_mtime,
            reverse=True,
        )
        for run_dir in run_dirs:
            metrics_csv = run_dir / "sc_results.csv"
            if not metrics_csv.exists():
                continue
            try:
                metrics = _load_sc_metrics_from_csv(metrics_csv)
                if not metrics:
                    continue
                for item in metrics:
                    combined_metrics.append(_enrich_metric_sample_fields(metric=item, sample_path=sample_value))
                combined_artifacts.extend(
                    _collect_loaded_sc_artifacts(run_dir=run_dir, metrics_csv=metrics_csv, metrics=metrics)
                )
                processed_samples += 1
                break
            except Exception as exc:  # pragma: no cover
                LOGGER.warning("Failed to load saved self-consistency from %s: %s", metrics_csv, exc)
                continue

    if not combined_metrics:
        return (*_empty_sc_outputs(), *_empty_sc_state())

    # Keep artifact list stable and duplicate-free.
    artifacts = list(dict.fromkeys(combined_artifacts))
    summary = _build_sc_summary(
        num_selected_samples=len(sample_values),
        num_processed_samples=processed_samples,
        metrics=combined_metrics,
    )
    base_status = (
        f"Loaded saved self-consistency for {processed_samples}/{len(sample_values)} selected sample(s). "
        f"Evaluated={len(combined_metrics)}."
    )
    rendered = _render_sc_outputs(
        base_status=base_status,
        summary=summary,
        metrics=combined_metrics,
        artifacts=artifacts,
        top_n=SC_DEFAULT_TOP_N,
    )
    return (*rendered, base_status, summary, combined_metrics, artifacts)


def save_example_case(selected_samples: Optional[object]) -> str:
    selected_values = _normalize_selected_samples(selected_samples)
    selected_sample = selected_values[0] if selected_values else None
    if not selected_sample:
        latest = _find_latest_generated_sample()
        if latest is None:
            return "No generated sample found. Run inference or select a sample before saving an example case."
        selected_sample = latest.resolve().as_posix()

    sample_path = Path(selected_sample)
    if not sample_path.exists():
        return f"Selected sample does not exist: {sample_path}"

    source_dir = sample_path.parent
    try:
        EXAMPLE_CASE_DIR.mkdir(parents=True, exist_ok=True)
        if EXAMPLE_CASE_SAMPLE_DIR.exists():
            shutil.rmtree(EXAMPLE_CASE_SAMPLE_DIR)
        shutil.copytree(source_dir, EXAMPLE_CASE_SAMPLE_DIR)

        saved_sample_path = EXAMPLE_CASE_SAMPLE_DIR / sample_path.name
        if not saved_sample_path.exists():
            return (
                "Example case copy completed, but sample file was missing in copied directory. "
                "Please verify the source sample."
            )

        manifest = {
            "saved_at_utc": datetime.utcnow().isoformat() + "Z",
            "source_sample_path": sample_path.resolve().as_posix(),
            "example_sample_path": saved_sample_path.resolve().as_posix(),
        }
        EXAMPLE_CASE_MANIFEST.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
        return (
            f"Saved example case to {EXAMPLE_CASE_SAMPLE_DIR}. "
            "Set FLOWPROT_VIEW_EXAMPLE_ON_STARTUP=true to auto-load it at app startup."
        )
    except Exception as exc:  # pragma: no cover
        LOGGER.exception("Failed to save example case.")
        return f"Failed to save example case: {exc}"


def load_saved_example(

    prefix_status: str = "Loaded saved example case.",

    prefix_example_status: str = "Example case loaded.",

) -> Tuple[
    str,
    List[str],
    List[str],
    Dict[str, object],
    str,
    str,
    List[List[str]],
    List[List[object]],
    pd.DataFrame,
    pd.DataFrame,
    List[str],
    List[List[object]],
    List[List[object]],
    str,
    Dict[str, object],
    List[Dict[str, object]],
    List[str],
    str,
]:
    sample_path = _resolve_saved_example_sample()
    if not sample_path:
        empty = _empty_example_view_outputs()
        return (*empty[:-1], "No saved example found in examples/flowprot_space_example. Save one first.")

    selector_update = gr.update(choices=[sample_path], value=[sample_path], interactive=True)
    sc_payload = _hydrate_sc_outputs_for_samples([sample_path])
    loaded_metrics = sc_payload[10]
    sc_note = (
        f" Loaded self-consistency ({len(loaded_metrics)} sequence(s))."
        if loaded_metrics
        else " No saved self-consistency metrics found for this example."
    )
    return (
        f"{prefix_status} Source: {sample_path}",
        [sample_path],
        [],
        selector_update,
        _build_molstar_iframe_html(sample_path),
        *sc_payload,
        f"{prefix_example_status} Source: {sample_path}.{sc_note}",
    )


def maybe_load_saved_example_on_startup(

    view_example_on_startup: bool,

) -> Tuple[
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    object,
    str,
]:
    if not bool(view_example_on_startup):
        return (
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            "",
        )
    return load_saved_example(
        prefix_status="Loaded saved example case on startup.",
        prefix_example_status="Startup example loaded.",
    )


def _format_summary_rows(summary: Dict[str, object]) -> List[List[str]]:
    preferred = [
        ("mean_scTM", "scTM score"),
        ("mean_scRMSD", "scRMSD score"),
        ("mean_esmfold_plddt", "Mean pLDDT"),
    ]
    rows: List[List[str]] = []
    for key, label in preferred:
        if key not in summary:
            continue
        value = summary[key]
        if isinstance(value, float):
            precision = 4 if "plddt" not in key else 4
            rows.append([label, f"{value:.{precision}f}"])
        else:
            rows.append([label, str(value)])
    return rows


def _format_metrics_rows(metrics: List[Dict[str, object]]) -> List[List[object]]:
    rows: List[List[object]] = []
    for item in metrics:
        rows.append(
            [
                _metric_sample_display(item),
                _metric_sequence_length(item),
                round(float(item.get("scTM")), 4) if item.get("scTM") is not None else None,
                round(float(item.get("scRMSD")), 4) if item.get("scRMSD") is not None else None,
                round(float(item.get("esmfold_mean_plddt")), 4)
                if item.get("esmfold_mean_plddt") is not None
                else None,
            ]
        )
    return rows


def _format_metrics_plot_df(metrics: List[Dict[str, object]]) -> pd.DataFrame:
    plot_rows = [
        {
            "sample": _metric_sample_display(item),
            "plot_color": "metric",
            "scTM": item.get("scTM"),
            "scRMSD": item.get("scRMSD"),
            "esmfold_mean_plddt": item.get("esmfold_mean_plddt"),
        }
        for item in metrics
        if item.get("scTM") is not None and item.get("scRMSD") is not None
    ]
    if not plot_rows:
        return _empty_sc_plot_df()
    frame = pd.DataFrame(plot_rows)
    for col in ("scTM", "scRMSD", "esmfold_mean_plddt"):
        frame[col] = pd.to_numeric(frame[col], errors="coerce")
    return frame


def _format_leaderboard_rows(

    metrics: List[Dict[str, object]],

    top_n: int,

) -> Tuple[List[List[object]], List[List[object]]]:
    if not metrics:
        return [], []

    ranked_tm = sorted(
        metrics,
        key=lambda item: float(item["scTM"]),
        reverse=True,
    )[: int(top_n)]
    top_tm_rows: List[List[object]] = []
    for rank, item in enumerate(ranked_tm, start=1):
        top_tm_rows.append(
            [
                rank,
                _metric_sample_display(item),
                _metric_sequence_length(item),
                round(float(item["scTM"]), 4),
            ]
        )

    ranked_rmsd = sorted(
        metrics,
        key=lambda item: float(item["scRMSD"]),
    )[: int(top_n)]
    top_rmsd_rows: List[List[object]] = []
    for rank, item in enumerate(ranked_rmsd, start=1):
        top_rmsd_rows.append(
            [
                rank,
                _metric_sample_display(item),
                _metric_sequence_length(item),
                round(float(item["scRMSD"]), 4),
            ]
        )
    return top_tm_rows, top_rmsd_rows


def _filter_metrics(

    metrics: Sequence[Dict[str, object]],

    sample_labels: Optional[object],

    sample_query: str,

    min_tm: float,

    max_rmsd: float,

    min_plddt: float,

) -> List[Dict[str, object]]:
    selected_sample_labels = set(_normalize_selected_samples(sample_labels))
    query = sample_query.strip().lower()
    filtered: List[Dict[str, object]] = []

    for item in metrics:
        sample_display = _metric_sample_display(item)
        if selected_sample_labels and sample_display not in selected_sample_labels:
            continue
        if query and query not in sample_display.lower():
            continue

        tm_value = item.get("scTM")
        if tm_value is None or float(tm_value) < float(min_tm):
            continue
        rmsd_value = item.get("scRMSD")
        if rmsd_value is None or float(rmsd_value) > float(max_rmsd):
            continue
        plddt_value = item.get("esmfold_mean_plddt")
        if plddt_value is not None and float(plddt_value) < float(min_plddt):
            continue
        filtered.append(dict(item))
    return filtered


def _filters_summary_line(

    sample_labels: Optional[object],

    sample_query: str,

    min_tm: float,

    max_rmsd: float,

    min_plddt: float,

) -> Optional[str]:
    labels = _normalize_selected_samples(sample_labels)
    parts: List[str] = []
    if labels:
        parts.append(f"samples={len(labels)}")
    query = sample_query.strip()
    if query:
        parts.append(f'text="{query}"')
    if float(min_tm) > SC_TM_MIN:
        parts.append(f"scTM>={float(min_tm):.2f}")
    if float(max_rmsd) < SC_RMSD_MAX:
        parts.append(f"scRMSD<={float(max_rmsd):.2f}")
    if float(min_plddt) > SC_PLDDT_MIN:
        parts.append(f"pLDDT>={float(min_plddt):.2f}")
    if not parts:
        return None
    return "Filters active: " + ", ".join(parts)


def _build_scatter_plot_html(

    plot_df: pd.DataFrame,

    x_col: str,

    y_col: str,

    title: str,

    x_title: str,

    y_title: str,

    fallback_x_range: Tuple[float, float],

    fallback_y_range: Tuple[float, float],

) -> str:
    if plot_df.empty:
        return _empty_scatter_plot_html(title=title, message="No points available for this view.")

    keep_cols = [x_col, y_col]
    if "sample" in plot_df.columns:
        keep_cols.append("sample")
    rows = plot_df[keep_cols].copy()
    rows[x_col] = pd.to_numeric(rows[x_col], errors="coerce")
    rows[y_col] = pd.to_numeric(rows[y_col], errors="coerce")
    rows = rows.dropna(subset=[x_col, y_col])
    if rows.empty:
        return _empty_scatter_plot_html(title=title, message="No numeric points available for this view.")

    x_min, x_max = fallback_x_range
    y_min, y_max = fallback_y_range
    rows = rows[(rows[x_col] >= x_min) & (rows[x_col] <= x_max) & (rows[y_col] >= y_min) & (rows[y_col] <= y_max)]
    if rows.empty:
        return _empty_scatter_plot_html(title=title, message="No points within the fixed axis range for this view.")

    x_vals = rows[x_col].astype(float).tolist()
    y_vals = rows[y_col].astype(float).tolist()
    labels = rows["sample"].astype(str).tolist() if "sample" in rows.columns else [""] * len(x_vals)
    x_span = max(x_max - x_min, 1e-9)
    y_span = max(y_max - y_min, 1e-9)

    width = 760
    height = 420
    left = 74
    right = 26
    top = 36
    bottom = 58
    plot_w = width - left - right
    plot_h = height - top - bottom

    def sx(value: float) -> float:
        return left + ((value - x_min) / x_span) * plot_w

    def sy(value: float) -> float:
        return top + (1.0 - ((value - y_min) / y_span)) * plot_h

    points_svg = []
    for xv, yv, label in zip(x_vals, y_vals, labels):
        tooltip = html.escape(
            f"{label} | {x_title.split(' (')[0]}={xv:.3f} | {y_title.split(' (')[0]}={yv:.3f}"
        )
        points_svg.append(
            f"<circle cx=\"{sx(xv):.2f}\" cy=\"{sy(yv):.2f}\" r=\"4.5\" "
            "fill=\"#2563eb\" fill-opacity=\"0.9\" stroke=\"#ffffff\" stroke-width=\"1.1\">"
            f"<title>{tooltip}</title></circle>"
        )

    x_ticks = []
    y_ticks = []
    tick_count = 5
    for idx in range(tick_count + 1):
        ratio = idx / tick_count
        xv = x_min + ratio * x_span
        yv = y_min + ratio * y_span
        tx = left + ratio * plot_w
        ty = top + (1.0 - ratio) * plot_h
        x_ticks.append(
            f"<line x1=\"{tx:.2f}\" y1=\"{top + plot_h:.2f}\" x2=\"{tx:.2f}\" y2=\"{top + plot_h + 6:.2f}\" stroke=\"#94a3b8\" />"
            f"<text x=\"{tx:.2f}\" y=\"{top + plot_h + 20:.2f}\" text-anchor=\"middle\" font-size=\"11\" fill=\"#64748b\">{xv:.2f}</text>"
        )
        y_ticks.append(
            f"<line x1=\"{left - 6:.2f}\" y1=\"{ty:.2f}\" x2=\"{left:.2f}\" y2=\"{ty:.2f}\" stroke=\"#94a3b8\" />"
            f"<text x=\"{left - 10:.2f}\" y=\"{ty + 4:.2f}\" text-anchor=\"end\" font-size=\"11\" fill=\"#64748b\">{yv:.2f}</text>"
        )

    return (
        "<div style=\"border: 1px solid var(--block-border-color, #d9d9d9); border-radius: 10px; "
        "background: var(--block-background-fill, #ffffff); padding: 10px 12px;\">"
        f"<div style=\"font-weight: 600; margin-bottom: 6px;\">{html.escape(title)}</div>"
        f"<svg viewBox=\"0 0 {width} {height}\" width=\"100%\" role=\"img\" aria-label=\"{html.escape(title)}\">"
        f"<rect x=\"{left}\" y=\"{top}\" width=\"{plot_w}\" height=\"{plot_h}\" fill=\"none\" stroke=\"#cbd5e1\" />"
        + "".join(
            f"<line x1=\"{left}\" y1=\"{top + (i / tick_count) * plot_h:.2f}\" x2=\"{left + plot_w}\" y2=\"{top + (i / tick_count) * plot_h:.2f}\" "
            "stroke=\"#eef2f7\" />"
            for i in range(tick_count + 1)
        )
        + "".join(x_ticks)
        + "".join(y_ticks)
        + "".join(points_svg)
        + f"<text x=\"{left + plot_w / 2:.2f}\" y=\"{height - 14}\" text-anchor=\"middle\" font-size=\"12\" fill=\"#475569\">{html.escape(x_title)}</text>"
        + f"<text x=\"18\" y=\"{top + plot_h / 2:.2f}\" text-anchor=\"middle\" font-size=\"12\" fill=\"#475569\" transform=\"rotate(-90 18 {top + plot_h / 2:.2f})\">{html.escape(y_title)}</text>"
        + "</svg>"
        + "</div>"
    )


def _render_sc_outputs(

    base_status: str,

    summary: Dict[str, object],

    metrics: List[Dict[str, object]],

    artifacts: List[str],

    top_n: int,

    extra_status_line: Optional[str] = None,

) -> Tuple[
    str,
    List[List[str]],
    List[List[object]],
    str,
    str,
    List[str],
    List[List[object]],
    List[List[object]],
]:
    status_lines = [line for line in [base_status, extra_status_line] if line]
    if metrics:
        status_lines.append(f"Showing {len(metrics)} evaluated sequence(s).")
    status = "\n".join(status_lines)

    summary_rows = _format_summary_rows(summary)
    metrics_rows = _format_metrics_rows(metrics)
    plot_df = _format_metrics_plot_df(metrics)
    tm_vs_rmsd_html = _build_scatter_plot_html(
        plot_df=plot_df,
        x_col="scTM",
        y_col="scRMSD",
        title="Self-consistency tradeoff (scTM vs scRMSD)",
        x_title="scTM (higher is better)",
        y_title="scRMSD (lower is better)",
        fallback_x_range=(SC_TM_MIN, SC_TM_MAX),
        fallback_y_range=(SC_RMSD_MIN, SC_RMSD_MAX),
    )
    confidence_html = _build_scatter_plot_html(
        plot_df=plot_df,
        x_col="esmfold_mean_plddt",
        y_col="scTM",
        title="ESMFold confidence vs scTM",
        x_title="ESMFold mean pLDDT",
        y_title="scTM",
        fallback_x_range=(SC_PLDDT_MIN, SC_PLDDT_MAX),
        fallback_y_range=(SC_TM_MIN, SC_TM_MAX),
    )
    top_tm_rows, top_rmsd_rows = _format_leaderboard_rows(metrics, int(top_n))
    return status, summary_rows, metrics_rows, tm_vs_rmsd_html, confidence_html, artifacts, top_tm_rows, top_rmsd_rows


def refresh_health() -> Dict[str, object]:
    health = SERVICE.health_check()
    health["self_consistency"] = SC_SERVICE.health_check()
    return health


def _resolve_viewer_value(sample_path: Optional[str]) -> Optional[str]:
    if not sample_path:
        return None
    normalized = Path(sample_path)
    if normalized.exists():
        # Use POSIX-style separators for robust frontend URL serialization.
        return normalized.resolve().as_posix()
    return None


def _build_molstar_iframe_html(

    sample_path: Optional[str],

    viewer_message: Optional[str] = None,

) -> str:
    viewer_value = _resolve_viewer_value(sample_path)
    if not viewer_value:
        return _molstar_placeholder_html(message=viewer_message)

    try:
        pdb_text = Path(viewer_value).read_text(encoding="utf-8", errors="replace")
    except Exception as exc:  # pragma: no cover
        LOGGER.warning("Failed to read PDB for Mol*: %s", exc)
        return (
            f"<div style=\"height: {_viewer_height_css()}; border: 1px solid #d9d9d9; border-radius: 8px; "
            "display: flex; align-items: center; justify-content: center; color: #b42318; background: #ffffff;\">"
            "Failed to load structure for Mol*."
            "</div>"
        )

    pdb_base64 = base64.b64encode(pdb_text.encode("utf-8")).decode("ascii")
    srcdoc = f"""

<!doctype html>

<html>

  <head>

    <meta charset="utf-8" />

    <script src="https://cdn.jsdelivr.net/npm/@rcsb/rcsb-molstar/build/dist/viewer/rcsb-molstar.js"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@rcsb/rcsb-molstar/build/dist/viewer/rcsb-molstar.css" />

    <style>

      html, body {{

        margin: 0;

        width: 100%;

        height: 100%;

        overflow: hidden;

        background: #ffffff;

      }}

      #protein-viewer {{

        width: 100%;

        height: 100%;

      }}

      #viewer-status {{

        position: absolute;

        top: 10px;

        left: 10px;

        right: 10px;

        z-index: 10;

        display: none;

        font-family: sans-serif;

        border-radius: 6px;

        border: 1px solid #fecaca;

        background: #fff1f2;

        color: #b42318;

        padding: 8px 10px;

        font-size: 13px;

      }}

      #viewer-shell {{

        position: relative;

        width: 100%;

        height: 100%;

      }}

    </style>

  </head>

  <body>

    <div id="viewer-shell">

      <div id="viewer-status"></div>

      <div id="protein-viewer"></div>

    </div>

    <script>

      (async function() {{

        const statusEl = document.getElementById("viewer-status");

        const viewer = new rcsbMolstar.Viewer("protein-viewer", {{

          layoutShowControls: true,

          layoutShowSequence: true,

          viewportShowExpand: false,

          collapseLeftPanel: false

        }});

        const pdbBase64 = {json.dumps(pdb_base64)};

        const blob = new Blob([atob(pdbBase64)], {{ type: "text/plain" }});

        const url = URL.createObjectURL(blob);

        try {{

          await viewer.loadStructureFromUrl(url, "pdb");

          statusEl.style.display = "none";

          statusEl.textContent = "";

        }} catch (error) {{

          const reason = error && error.message ? error.message : String(error);

          statusEl.style.display = "block";

          statusEl.textContent = "Mol* failed to render this structure: " + reason;

          console.error("Error loading structure:", error);

        }} finally {{

          URL.revokeObjectURL(url);

        }}

      }})();

    </script>

  </body>

</html>

"""
    escaped_srcdoc = html.escape(srcdoc, quote=True)
    return (
        "<iframe "
        "title=\"Mol* Viewer\" "
        f"style=\"width: 100%; height: {_viewer_height_css()}; border: 1px solid #d9d9d9; border-radius: 8px; overflow: hidden; background: #ffffff;\" "
        "srcdoc=\""
        + escaped_srcdoc
        + "\"></iframe>"
    )


def update_selected_molstar(selected_samples: Optional[object]):
    selected_values = _normalize_selected_samples(selected_samples)
    viewer_sample = selected_values[0] if selected_values else None
    return (
        _build_molstar_iframe_html(viewer_sample),
        *_hydrate_sc_outputs_for_samples(selected_values),
    )


def _update_backbone_selector(selected_samples: Optional[object]) -> Dict[str, object]:
    selected_values = _normalize_selected_samples(selected_samples)
    return gr.update(
        choices=selected_values,
        value=selected_values[0] if selected_values else None,
        interactive=bool(selected_values),
    )


def _update_sc_filter_sample_selector(metrics: Optional[List[Dict[str, object]]]) -> Dict[str, object]:
    choices = _build_sc_filter_sample_choices(metrics or [])
    return gr.update(choices=choices, value=[], interactive=bool(choices))


def _update_folded_selector(metrics: Optional[List[Dict[str, object]]]) -> Dict[str, object]:
    choices = _build_folded_choices(metrics or [])
    return gr.update(
        choices=choices,
        value=choices[0][1] if choices else None,
        interactive=bool(choices),
    )


def load_aligned_compare_view(

    backbone_sample_path: Optional[str],

    folded_sample_path: Optional[str],

) -> Tuple[str, str]:
    backbone_value = _normalize_optional_text(backbone_sample_path)
    folded_value = _normalize_optional_text(folded_sample_path)
    if not backbone_value:
        return (
            _molstar_placeholder_html(message="Select a backbone sample to compare."),
            "Select a backbone sample before loading aligned compare view.",
        )
    if not folded_value:
        return (
            _build_molstar_iframe_html(backbone_value),
            "Select a folded structure from self-consistency rows before comparison.",
        )

    try:
        overlay = align_folded_to_backbone_overlay(
            backbone_sample_path=backbone_value,
            folded_sample_path=folded_value,
        )
        viewer = _build_molstar_iframe_html(
            overlay.output_path,
            viewer_message="Aligned backbone + ESMFold comparison.",
        )
        details = (
            "Loaded aligned compare view. "
            f"CA pairs={overlay.num_ca_pairs}, "
            f"RMSD before={overlay.rmsd_before:.4f}, after={overlay.rmsd_after:.4f}. "
            f"Overlay: {overlay.output_path}"
        )
        return viewer, details
    except AlignmentError as exc:
        LOGGER.error("Alignment compare failed: %s", exc)
        return (
            _build_molstar_iframe_html(backbone_value),
            f"Failed to build aligned compare view: {exc}",
        )
    except Exception as exc:  # pragma: no cover
        LOGGER.exception("Unexpected compare-view failure.")
        return (
            _build_molstar_iframe_html(backbone_value),
            f"Unexpected compare-view failure: {exc}",
        )


def preload_model() -> Tuple[str, Dict[str, object]]:
    try:
        SERVICE.preload_model()
        return "Model loaded successfully.", refresh_health()
    except (ArtifactResolutionError, ModelLoadError, InferenceError) as exc:
        LOGGER.error("Model preload failed: %s", exc)
        return f"Model preload failed: {exc}", refresh_health()
    except Exception as exc:  # pragma: no cover
        LOGGER.exception("Unexpected preload failure.")
        return f"Unexpected preload failure: {exc}", refresh_health()


def _collect_trajectory_artifacts(

    include_trajectory_artifacts: bool,

    result: InferenceResult,

) -> List[str]:
    if not include_trajectory_artifacts:
        return []
    combined = [*result.trajectory_files, *result.x0_trajectory_files]
    return list(dict.fromkeys(combined))


def run_generation(

    mode: str,

    length: int,

    num_samples: int,

    seed_override: Optional[object],

    include_trajectory_artifacts: bool,

    guidance_scale: float,

    target_class: int,

    num_timesteps: Optional[object],

    reference_pdb: Optional[object],

    fixed_residues_text: Optional[object],

    use_classifier_guidance: bool,

    progress=gr.Progress(track_tqdm=False),

):
    try:
        progress(0.05, desc="Loading model and preparing inputs...")
        seed = _parse_optional_seed(seed_override)
        timesteps = None
        timesteps_text = _normalize_optional_text(num_timesteps)
        if timesteps_text:
            timesteps = int(float(timesteps_text))
        reference_path = None
        if reference_pdb is not None:
            reference_path = getattr(reference_pdb, "name", None) or str(reference_pdb)
        fixed_residues = _parse_fixed_residues(fixed_residues_text) if mode == "conditional" else None

        progress(0.2, desc=f"Sampling {int(num_samples)} structure(s)...")
        result = SERVICE.generate(
            mode=mode,
            length=int(length),
            num_samples=int(num_samples),
            seed=seed,
            guidance_scale=float(guidance_scale) if mode in ("classifier", "conditional") else None,
            target_class=int(target_class) if mode in ("classifier", "conditional") else None,
            num_timesteps=timesteps,
            reference_pdb_path=reference_path,
            fixed_residues=fixed_residues,
            use_classifier_guidance=bool(use_classifier_guidance),
        )
        progress(0.9, desc="Writing artifacts...")
        _append_run_history(result)
        trajectory_artifacts = _collect_trajectory_artifacts(
            include_trajectory_artifacts=bool(include_trajectory_artifacts),
            result=result,
        )
        trajectory_message = (
            f"Trajectory artifacts ready: {len(trajectory_artifacts)} file(s)."
            if include_trajectory_artifacts
            else "Trajectory artifacts hidden; enable the checkbox to download traj/x0 files."
        )
        classifier_details = ""
        if result.mode == "classifier":
            classifier_details = (
                f" Guidance scale={result.guidance_scale}, "
                f"target_class={result.target_class}."
            )
        elif result.mode == "conditional":
            classifier_details = f" Fixed residues={result.fixed_residue_count}."
        steps_details = f" Timesteps={result.num_timesteps}." if result.num_timesteps else ""
        summary = (
            f"Generated {len(result.sample_files)} sample(s) with mode={result.mode}. "
            f"Seed={result.seed}.{steps_details} Output dir: {result.run_dir}. "
            f"Artifacts source: {result.artifacts_source}.{classifier_details} "
            f"{trajectory_message}"
        )
        default_sample = result.sample_files[0] if result.sample_files else None
        selector_update = gr.update(
            choices=result.sample_files,
            value=[default_sample] if default_sample else [],
            interactive=bool(result.sample_files),
        )
        return (
            summary,
            result.sample_files,
            trajectory_artifacts,
            refresh_health(),
            selector_update,
            _build_molstar_iframe_html(default_sample),
            *_empty_sc_outputs(),
            *_empty_sc_state(),
        )
    except (ArtifactResolutionError, ModelLoadError, InferenceError) as exc:
        LOGGER.error("Inference request failed: %s", exc)
        return (
            f"Inference failed: {exc}{_actionable_error_hint(exc)}",
            [],
            [],
            refresh_health(),
            gr.update(choices=[], value=[], interactive=False),
            _molstar_placeholder_html(),
            *_empty_sc_outputs(),
            *_empty_sc_state(),
        )
    except Exception as exc:  # pragma: no cover
        LOGGER.exception("Unhandled inference failure.")
        trace = traceback.format_exc(limit=1)
        return (
            f"Unexpected error: {exc}{_actionable_error_hint(exc)}\n{trace}",
            [],
            [],
            refresh_health(),
            gr.update(choices=[], value=[], interactive=False),
            _molstar_placeholder_html(),
            *_empty_sc_outputs(),
            *_empty_sc_state(),
        )


def run_self_consistency(

    selected_samples: Optional[object],

    num_seq_per_target: int,

    run_folding: bool,

    top_n: int,

    progress=gr.Progress(track_tqdm=False),

):
    sample_values = _normalize_selected_samples(selected_samples)
    if not sample_values:
        empty_outputs = _empty_sc_outputs()
        return (
            "Select one or more samples before running self-consistency.",
            *empty_outputs[1:],
            *_empty_sc_state(),
        )

    try:
        all_metrics: List[Dict[str, object]] = []
        all_artifacts: List[str] = []
        processed_samples = 0
        failed_samples: List[str] = []
        total_samples = len(sample_values)

        for sample_idx, sample_value in enumerate(sample_values):
            sample_label = _sample_label_from_path(sample_value)
            progress(
                sample_idx / max(total_samples, 1),
                desc=f"ProteinMPNN + folding ({sample_idx + 1}/{total_samples}): {sample_label}",
            )

            def _fold_progress(done: int, total: int, _idx=sample_idx, _label=sample_label) -> None:
                if total <= 0:
                    return
                frac = (_idx + done / total) / max(total_samples, 1)
                progress(frac, desc=f"Folding {_label}: {done}/{total} sequences")

            try:
                result = SC_SERVICE.run(
                    sample_path=sample_value,
                    num_seq_per_target=int(num_seq_per_target),
                    run_folding=bool(run_folding),
                    progress_callback=_fold_progress,
                )
                for item in result.per_sequence_metrics:
                    all_metrics.append(_enrich_metric_sample_fields(metric=item, sample_path=sample_value))

                all_artifacts.extend(
                    [
                        result.parsed_pdbs_jsonl,
                        result.mpnn_fasta_path,
                        *result.folded_pdb_paths,
                    ]
                )
                if result.metrics_csv_path:
                    all_artifacts.append(result.metrics_csv_path)
                processed_samples += 1
            except SelfConsistencyError as exc:
                LOGGER.error("Self-consistency failed for %s: %s", sample_value, exc)
                failed_samples.append(
                    f"{_sample_label_from_path(sample_value)} ({_short_error_text(exc)})"
                )

        if not all_metrics:
            empty_outputs = _empty_sc_outputs()
            fail_preview = "\n".join(f"- {sample}" for sample in failed_samples[:5])
            if failed_samples and len(failed_samples) > 5:
                fail_preview += f"\n... and {len(failed_samples) - 5} more failure(s)."
            fail_context = fail_preview if failed_samples else "No metrics were generated."
            return (
                "Self-consistency failed across selected samples.\n"
                f"Requested sequences/sample={int(num_seq_per_target)}, folding={'on' if run_folding else 'off'}.\n"
                f"{fail_context}",
                *empty_outputs[1:],
                *_empty_sc_state(),
            )

        artifacts = list(dict.fromkeys(all_artifacts))
        summary = _build_sc_summary(
            num_selected_samples=len(sample_values),
            num_processed_samples=processed_samples,
            metrics=all_metrics,
        )
        base_status_lines = [
            f"Self-consistency completed for {processed_samples}/{len(sample_values)} selected sample(s).",
            f"Evaluated={len(all_metrics)} sequences. Requested sequences/sample={int(num_seq_per_target)}.",
            f"Folding={'on' if run_folding else 'off'}. Artifacts={len(artifacts)}.",
        ]
        if failed_samples:
            preview = "; ".join(failed_samples[:3])
            suffix = f"; +{len(failed_samples) - 3} more" if len(failed_samples) > 3 else ""
            base_status_lines.append(f"Failed samples: {len(failed_samples)} ({preview}{suffix})")
        base_status = "\n".join(base_status_lines)

        rendered = _render_sc_outputs(
            base_status=base_status,
            summary=summary,
            metrics=all_metrics,
            artifacts=artifacts,
            top_n=top_n,
        )
        return (*rendered, base_status, summary, all_metrics, artifacts)
    except Exception as exc:  # pragma: no cover
        LOGGER.exception("Unexpected self-consistency failure.")
        trace = traceback.format_exc(limit=1)
        empty_outputs = _empty_sc_outputs()
        return (
            f"Unexpected self-consistency error: {exc}\n{trace}",
            *empty_outputs[1:],
            *_empty_sc_state(),
        )


def apply_sc_view_settings(

    base_status: str,

    summary: Dict[str, object],

    metrics: List[Dict[str, object]],

    artifacts: List[str],

    top_n: int,

    filter_sample_labels: Optional[object],

    filter_sample_query: str,

    filter_tm_min: float,

    filter_rmsd_max: float,

    filter_plddt_min: float,

):
    filtered_metrics = _filter_metrics(
        metrics=metrics,
        sample_labels=filter_sample_labels,
        sample_query=filter_sample_query,
        min_tm=filter_tm_min,
        max_rmsd=filter_rmsd_max,
        min_plddt=filter_plddt_min,
    )
    filters_line = _filters_summary_line(
        sample_labels=filter_sample_labels,
        sample_query=filter_sample_query,
        min_tm=filter_tm_min,
        max_rmsd=filter_rmsd_max,
        min_plddt=filter_plddt_min,
    )
    return _render_sc_outputs(
        base_status=base_status,
        summary=summary,
        metrics=filtered_metrics,
        artifacts=artifacts,
        top_n=top_n,
        extra_status_line=filters_line,
    )


def reset_sc_view_settings(

    base_status: str,

    summary: Dict[str, object],

    metrics: List[Dict[str, object]],

    artifacts: List[str],

):
    rendered = _render_sc_outputs(
        base_status=base_status,
        summary=summary,
        metrics=metrics,
        artifacts=artifacts,
        top_n=SC_DEFAULT_TOP_N,
    )
    return (
        gr.update(value=SC_DEFAULT_TOP_N),
        gr.update(value=[]),
        gr.update(value=""),
        gr.update(value=SC_TM_MIN),
        gr.update(value=SC_RMSD_MAX),
        gr.update(value=SC_PLDDT_MIN),
        *rendered,
    )


def clear_compare_status() -> str:
    return ""


def _status_badge_html() -> str:
    health = SERVICE.health_check()
    model_loaded = bool(health.get("model_loaded"))
    device = str(health.get("device", "auto"))
    sc_health = SC_SERVICE.health_check()
    pmpnn_ok = bool(sc_health.get("pmpnn_available")) and bool(sc_health.get("pmpnn_weights_available"))

    def pill(label: str, ok: bool) -> str:
        color = "#16a34a" if ok else "#b91c1c"
        bg = "#dcfce7" if ok else "#fee2e2"
        dot = "&#9679;"
        return (
            f"<span style=\"display:inline-flex; align-items:center; gap:6px; "
            f"padding:3px 10px; border-radius:999px; background:{bg}; color:{color}; "
            f"font-size:0.82rem; font-weight:600; margin-right:8px;\">{dot} {html.escape(label)}</span>"
        )

    device_pill = (
        f"<span style=\"display:inline-flex; align-items:center; padding:3px 10px; "
        f"border-radius:999px; background:var(--block-background-fill,#f1f5f9); "
        f"border:1px solid var(--block-border-color,#e2e8f0); font-size:0.82rem; "
        f"margin-right:8px;\">Device: {html.escape(device)}</span>"
    )
    return (
        "<div style=\"display:flex; flex-wrap:wrap; align-items:center; gap:4px; padding:4px 0;\">"
        + pill("Model loaded" if model_loaded else "Model not loaded", model_loaded)
        + pill("Self-consistency ready" if pmpnn_ok else "ProteinMPNN missing", pmpnn_ok)
        + device_pill
        + "</div>"
    )


def refresh_status_badge() -> str:
    return _status_badge_html()


def _parse_fixed_residues(text: Optional[object]) -> Optional[List[int]]:
    raw = _normalize_optional_text(text)
    if not raw:
        return None
    residues: List[int] = []
    for token in raw.replace(";", ",").split(","):
        token = token.strip()
        if not token:
            continue
        if ":" in token:
            token = token.split(":", 1)[1].strip()
        if "-" in token:
            start_str, end_str = token.split("-", 1)
            start, end = int(start_str.strip()), int(end_str.strip())
            if end < start:
                start, end = end, start
            residues.extend(range(start, end + 1))
        else:
            residues.append(int(token))
    deduped = sorted(dict.fromkeys(residues))
    return deduped or None


def _resolve_trajectory_path(sample_path: Optional[str], kind: str) -> Optional[str]:
    sample_values = _normalize_selected_samples(sample_path)
    if not sample_values:
        return None
    parent = Path(sample_values[0]).parent
    filename = "bb_traj.pdb" if kind == "Backbone trajectory" else "x0_traj.pdb"
    candidate = parent / filename
    return candidate.resolve().as_posix() if candidate.exists() else None


def _build_trajectory_iframe_html(traj_path: Optional[str], title: str) -> str:
    if not traj_path:
        return _molstar_placeholder_html(
            message="No trajectory found for this sample. Generate a new sample to view its flow trajectory."
        )
    try:
        pdb_text = Path(traj_path).read_text(encoding="utf-8", errors="replace")
    except Exception as exc:  # pragma: no cover
        LOGGER.warning("Failed to read trajectory PDB: %s", exc)
        return _molstar_placeholder_html(message="Failed to read trajectory file.")

    pdb_base64 = base64.b64encode(pdb_text.encode("utf-8")).decode("ascii")
    srcdoc = f"""

<!doctype html>

<html>

  <head>

    <meta charset="utf-8" />

    <script src="https://cdn.jsdelivr.net/npm/@rcsb/rcsb-molstar/build/dist/viewer/rcsb-molstar.js"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@rcsb/rcsb-molstar/build/dist/viewer/rcsb-molstar.css" />

    <style>

      html, body {{ margin: 0; width: 100%; height: 100%; overflow: hidden; background: #ffffff; font-family: sans-serif; }}

      #protein-viewer {{ width: 100%; height: calc(100% - 52px); }}

      #controls {{ height: 52px; display: flex; align-items: center; gap: 10px; padding: 6px 10px; box-sizing: border-box; }}

      #controls button {{ cursor: pointer; border: 1px solid #cbd5e1; background: #f8fafc; border-radius: 6px; padding: 4px 12px; font-size: 13px; }}

      #frame-slider {{ flex: 1; }}

      #frame-label {{ font-size: 12px; color: #475569; min-width: 96px; text-align: right; }}

    </style>

  </head>

  <body>

    <div id="protein-viewer"></div>

    <div id="controls">

      <button id="play-btn">Play</button>

      <input id="frame-slider" type="range" min="0" max="0" value="0" />

      <span id="frame-label">Frame 1 / 1</span>

    </div>

    <script>

      (async function() {{

        const viewer = new rcsbMolstar.Viewer("protein-viewer", {{

          layoutShowControls: false,

          layoutShowSequence: false,

          viewportShowExpand: false

        }});

        const pdbText = atob({json.dumps(pdb_base64)});

        const lines = pdbText.split(/\\r?\\n/);

        let frames = [];

        let current = [];

        let hasModels = false;

        for (const line of lines) {{

          if (line.startsWith("MODEL")) {{ hasModels = true; current = []; continue; }}

          if (line.startsWith("ENDMDL")) {{ frames.push(current.join("\\n")); current = []; continue; }}

          current.push(line);

        }}

        if (!hasModels) {{ frames = [pdbText]; }}

        if (frames.length === 0 && current.length) {{ frames = [current.join("\\n")]; }}



        const slider = document.getElementById("frame-slider");

        const label = document.getElementById("frame-label");

        const playBtn = document.getElementById("play-btn");

        slider.max = String(Math.max(frames.length - 1, 0));



        async function loadFrame(idx) {{

          const body = "MODEL        1\\n" + frames[idx] + "\\nENDMDL\\nEND\\n";

          const blob = new Blob([body], {{ type: "text/plain" }});

          const url = URL.createObjectURL(blob);

          try {{

            await viewer.clear();

            await viewer.loadStructureFromUrl(url, "pdb");

          }} catch (e) {{ console.error(e); }} finally {{ URL.revokeObjectURL(url); }}

          label.textContent = "Frame " + (idx + 1) + " / " + frames.length;

        }}



        let timer = null;

        slider.addEventListener("input", () => loadFrame(parseInt(slider.value, 10)));

        playBtn.addEventListener("click", () => {{

          if (timer) {{ clearInterval(timer); timer = null; playBtn.textContent = "Play"; return; }}

          playBtn.textContent = "Pause";

          timer = setInterval(() => {{

            let next = (parseInt(slider.value, 10) + 1) % frames.length;

            slider.value = String(next);

            loadFrame(next);

            if (next === frames.length - 1) {{ clearInterval(timer); timer = null; playBtn.textContent = "Play"; }}

          }}, 350);

        }});

        await loadFrame(0);

      }})();

    </script>

  </body>

</html>

"""
    escaped_srcdoc = html.escape(srcdoc, quote=True)
    return (
        f"<div style=\"font-weight:600; margin-bottom:6px;\">{html.escape(title)}</div>"
        "<iframe title=\"Mol* Trajectory\" "
        f"style=\"width: 100%; height: {_viewer_height_css()}; border: 1px solid #d9d9d9; border-radius: 8px; overflow: hidden; background: #ffffff;\" "
        "srcdoc=\""
        + escaped_srcdoc
        + "\"></iframe>"
    )


def load_trajectory_view(selected_samples: Optional[object], kind: str) -> str:
    traj_path = _resolve_trajectory_path(selected_samples, kind)
    return _build_trajectory_iframe_html(traj_path, title=f"{kind} playback")


def update_selection_label(selected_samples: Optional[object]) -> str:
    values = _normalize_selected_samples(selected_samples)
    if not values:
        return "_No sample selected. Generate or load a sample to begin._"
    viewing = _sample_label_from_path(values[0])
    return (
        f"**Viewing in Mol*:** `{viewing}` &nbsp;|&nbsp; "
        f"**Self-consistency targets:** {len(values)} sample(s)"
    )


def _append_run_history(result: InferenceResult) -> None:
    try:
        SPACE_OUTPUTS_DIR.mkdir(parents=True, exist_ok=True)
        history: List[Dict[str, object]] = []
        if RUN_HISTORY_PATH.exists():
            try:
                history = json.loads(RUN_HISTORY_PATH.read_text(encoding="utf-8"))
            except Exception:
                history = []
        entry = {
            "run_id": Path(result.run_dir).name,
            "mode": result.mode,
            "seed": result.seed,
            "num_samples": len(result.sample_files),
            "num_timesteps": result.num_timesteps,
            "sample_files": result.sample_files,
            "created_utc": datetime.utcnow().isoformat() + "Z",
        }
        history = [entry] + [h for h in history if h.get("run_id") != entry["run_id"]]
        history = history[:RUN_HISTORY_LIMIT]
        RUN_HISTORY_PATH.write_text(json.dumps(history, indent=2), encoding="utf-8")
    except Exception:  # pragma: no cover - history is best-effort.
        LOGGER.warning("Failed to append run history.", exc_info=True)


def _run_history_choices() -> List[Tuple[str, str]]:
    if not RUN_HISTORY_PATH.exists():
        return []
    try:
        history = json.loads(RUN_HISTORY_PATH.read_text(encoding="utf-8"))
    except Exception:
        return []
    choices: List[Tuple[str, str]] = []
    for entry in history:
        run_id = str(entry.get("run_id", "run"))
        mode = str(entry.get("mode", "?"))
        n = entry.get("num_samples", 0)
        seed = entry.get("seed", "?")
        label = f"{run_id} | {mode} | {n} sample(s) | seed={seed}"
        choices.append((label, run_id))
    return choices


def refresh_run_history() -> Dict[str, object]:
    choices = _run_history_choices()
    return gr.update(choices=choices, value=None, interactive=bool(choices))


def _history_samples_for_run(run_id: str) -> List[str]:
    if not RUN_HISTORY_PATH.exists():
        return []
    try:
        history = json.loads(RUN_HISTORY_PATH.read_text(encoding="utf-8"))
    except Exception:
        return []
    for entry in history:
        if str(entry.get("run_id")) == str(run_id):
            return [p for p in entry.get("sample_files", []) if Path(p).exists()]
    return []


def load_run_from_history(run_id: Optional[str]):
    sample_files = _history_samples_for_run(run_id) if run_id else []
    if not sample_files:
        empty = _empty_sc_outputs()
        return (
            "Selected run is unavailable (artifacts may have been cleared).",
            gr.update(),
            _molstar_placeholder_html(message="Run artifacts not found."),
            *empty,
            *_empty_sc_state(),
        )
    default_sample = sample_files[0]
    selector_update = gr.update(choices=sample_files, value=[default_sample], interactive=True)
    return (
        f"Loaded run {run_id} with {len(sample_files)} sample(s).",
        selector_update,
        _build_molstar_iframe_html(default_sample),
        *_hydrate_sc_outputs_for_samples([default_sample]),
    )


def export_sc_metrics_csv(

    metrics: List[Dict[str, object]],

    filter_sample_labels: Optional[object],

    filter_sample_query: str,

    filter_tm_min: float,

    filter_rmsd_max: float,

    filter_plddt_min: float,

) -> Optional[str]:
    if not metrics:
        return None
    filtered = _filter_metrics(
        metrics=metrics,
        sample_labels=filter_sample_labels,
        sample_query=filter_sample_query,
        min_tm=filter_tm_min,
        max_rmsd=filter_rmsd_max,
        min_plddt=filter_plddt_min,
    )
    if not filtered:
        return None
    SPACE_OUTPUTS_DIR.mkdir(parents=True, exist_ok=True)
    stamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
    out_path = SPACE_OUTPUTS_DIR / f"sc_metrics_export_{stamp}.csv"
    with out_path.open("w", encoding="utf-8", newline="") as handle:
        writer = csv.writer(handle)
        writer.writerow(["sample", "seq_len", "scTM", "scRMSD", "esmfold_mean_plddt"])
        for item in filtered:
            writer.writerow(
                [
                    _metric_sample_display(item),
                    _metric_sequence_length(item),
                    item.get("scTM"),
                    item.get("scRMSD"),
                    item.get("esmfold_mean_plddt"),
                ]
            )
    return out_path.resolve().as_posix()


def build_run_zip(selected_samples: Optional[object]) -> Optional[str]:
    values = _normalize_selected_samples(selected_samples)
    if not values:
        return None
    # The run directory is space_outputs/<run>/length_*/sample_*/sample.pdb -> 3 levels up.
    sample_dir = Path(values[0]).parent
    run_dir = sample_dir.parent.parent
    if not run_dir.exists():
        return None
    SPACE_OUTPUTS_DIR.mkdir(parents=True, exist_ok=True)
    stamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
    archive_base = SPACE_OUTPUTS_DIR / f"{run_dir.name}_bundle_{stamp}"
    archive_path = shutil.make_archive(str(archive_base), "zip", root_dir=str(run_dir))
    return Path(archive_path).resolve().as_posix()


def on_sc_metrics_select(

    metrics: List[Dict[str, object]],

    backbone_choices_value: Optional[str],

    select_data: gr.SelectData,

):
    if not metrics or select_data is None:
        return gr.update(), gr.update(), gr.update(), gr.update()
    row_index = select_data.index[0] if isinstance(select_data.index, (list, tuple)) else select_data.index
    if row_index is None or row_index >= len(metrics):
        return gr.update(), gr.update(), gr.update(), gr.update()
    metric = metrics[row_index]
    folded_path = _normalize_optional_text(metric.get("folded_sample_path"))
    backbone_path = _normalize_optional_text(metric.get("sample_source_path"))
    if not folded_path or not backbone_path:
        return gr.update(), gr.update(), gr.update(), gr.update()
    viewer_html, status = load_aligned_compare_view(backbone_path, folded_path)
    return (
        gr.update(value=backbone_path),
        gr.update(value=folded_path),
        viewer_html,
        status,
    )


with gr.Blocks(
    title="FlowProt Protein Designer",
    theme=gr.themes.Soft(),
    css=UI_CSS,
) as demo:
    gr.Markdown(
        """

        # FlowProt Protein Designer

        Generate novel protein backbones with flow matching, explore them in 3D, and validate

        their designability with ProteinMPNN and ESMFold self-consistency.

        """
    )
    status_badge = gr.HTML(value=_status_badge_html())
    with gr.Accordion("New here? Three steps to your first design", open=True, elem_classes=["fp-card"]):
        gr.Markdown(
            "1. **Generate** a backbone in the Generate tab (or click *Load demo example* below).\n"
            "2. **View** it in interactive 3D and watch the generation trajectory in the View tab.\n"
            "3. **Analyze** designability (scTM / scRMSD) in the Analyze tab."
        )
        demo_cta_button = gr.Button("Load demo example", variant="primary")

    sc_base_status_state = gr.State("")
    sc_summary_state = gr.State({})
    sc_metrics_state = gr.State([])
    sc_artifacts_state = gr.State([])

    with gr.Tabs():
        with gr.Tab("Generate"):
            with gr.Group(elem_classes=["fp-card"]):
                gr.Markdown("### Inference settings")
                with gr.Row(elem_classes=["fp-tight"]):
                    mode = gr.Dropdown(
                        label="Inference mode",
                        choices=_mode_choices(),
                        value=SERVICE.mvp_mode if SERVICE.mvp_mode in _mode_choices() else "unconditional",
                        info="Unconditional designs from scratch; classifier-guided steers toward a target class.",
                    )
                    length = gr.Slider(label="Protein length (residues)", minimum=32, maximum=1024, value=128, step=1)
                    num_samples = gr.Slider(label="Samples per request", minimum=1, maximum=4, value=1, step=1)
                with gr.Row(elem_classes=["fp-tight"]):
                    seed_override = gr.Textbox(
                        label="Seed override (optional)",
                        value="",
                        placeholder="Leave empty to use config inference.seed",
                    )
                    num_timesteps = gr.Slider(
                        label="Sampling timesteps",
                        minimum=10,
                        maximum=500,
                        value=100,
                        step=10,
                        info="More steps can improve quality at the cost of speed.",
                    )
                    include_trajectory_artifacts = gr.Checkbox(
                        label="Expose trajectory downloads",
                        value=False,
                    )

                with gr.Group(visible=False) as classifier_group:
                    gr.Markdown("**Classifier guidance**", elem_classes=["fp-subnote"])
                    with gr.Row(elem_classes=["fp-tight"]):
                        guidance_scale = gr.Slider(
                            label="Classifier guidance scale",
                            minimum=0.0,
                            maximum=1.0,
                            value=0.2,
                            step=0.05,
                            info="Higher values steer more strongly toward the target class.",
                        )
                        target_class = gr.Dropdown(
                            label="Classifier target class",
                            choices=[0, 1],
                            value=1,
                            info="Binary classifier label to steer toward (0 or 1).",
                        )

                with gr.Group(visible=False) as conditional_group:
                    gr.Markdown(
                        "**Conditional design** keeps the selected residues of a reference structure fixed "
                        "while generating the rest. Protein length is taken from the uploaded PDB.",
                        elem_classes=["fp-subnote"],
                    )
                    with gr.Row(elem_classes=["fp-tight"]):
                        reference_pdb = gr.File(label="Reference PDB (chain A)", file_types=[".pdb"])
                        fixed_residues_text = gr.Textbox(
                            label="Fixed residues",
                            value="",
                            placeholder="e.g. 10-40,55,60-62 (empty = fix all)",
                        )
                        use_classifier_guidance = gr.Checkbox(
                            label="Apply classifier guidance",
                            value=False,
                        )

                run_button = gr.Button("Run inference", variant="primary")

            with gr.Group(elem_classes=["fp-card"]):
                gr.Markdown("### Outputs")
                output_files = gr.Files(label="Generated sample.pdb files")
                trajectory_output_files = gr.Files(label="Optional trajectory artifacts (traj + x0)")
                with gr.Row(elem_classes=["fp-tight"]):
                    zip_button = gr.Button("Bundle current run as zip", variant="secondary")
                    zip_file = gr.File(label="Run bundle (.zip)")

        with gr.Tab("View"):
            with gr.Group(elem_classes=["fp-card"]):
                sample_selector = gr.Dropdown(
                    label="Select generated sample(s)",
                    choices=[],
                    value=[],
                    multiselect=True,
                    interactive=False,
                    info="The first selected sample is shown in 3D; all selected samples feed self-consistency.",
                )
                selection_label = gr.Markdown(update_selection_label(None))
                molstar_viewer = gr.HTML(value=_molstar_placeholder_html())

            with gr.Accordion("Generation trajectory playback", open=False, elem_classes=["fp-card"]):
                gr.Markdown(
                    "Step through the flow-matching trajectory of the selected sample. "
                    "Backbone trajectory shows the integrated path; x0 trajectory shows the model's denoised prediction.",
                    elem_classes=["fp-subnote"],
                )
                with gr.Row(elem_classes=["fp-tight"]):
                    traj_source = gr.Dropdown(
                        label="Trajectory",
                        choices=["Backbone trajectory", "x0 trajectory"],
                        value="Backbone trajectory",
                    )
                    load_traj_button = gr.Button("Load trajectory", variant="secondary")
                trajectory_viewer = gr.HTML(
                    value=_molstar_placeholder_html(message="Select a sample and load a trajectory to play it back.")
                )

            with gr.Accordion("Aligned compare (backbone vs ESMFold)", open=False, elem_classes=["fp-card"]):
                gr.Markdown(
                    "Overlay a generated backbone with an ESMFold-predicted structure from self-consistency. "
                    "Tip: clicking a row in the Analyze metrics table loads the overlay automatically.",
                    elem_classes=["fp-subnote"],
                )
                with gr.Row(elem_classes=["fp-tight"]):
                    backbone_compare_selector = gr.Dropdown(
                        label="Backbone sample for aligned compare",
                        choices=[],
                        value=None,
                        interactive=False,
                    )
                    folded_compare_selector = gr.Dropdown(
                        label="Folded structure for compare",
                        choices=[],
                        value=None,
                        interactive=False,
                    )
                    load_compare_button = gr.Button("Load aligned compare view", variant="secondary")
                molstar_compare_status = gr.Textbox(
                    label="3D compare status",
                    lines=2,
                    interactive=False,
                )

        with gr.Tab("Analyze"):
            with gr.Group(elem_classes=["fp-card"]):
                gr.Markdown(
                    "### Self-consistency (ProteinMPNN + ESMFold)\n"
                    "Runs across the samples selected in the View tab.",
                )
                with gr.Row(elem_classes=["fp-tight"]):
                    sc_num_sequences = gr.Slider(
                        label="ProteinMPNN sequences per sample",
                        minimum=1,
                        maximum=16,
                        value=4,
                        step=1,
                    )
                    sc_run_folding = gr.Checkbox(
                        label="Run ESMFold and compute scTM/scRMSD",
                        value=True,
                    )
                    sc_run_button = gr.Button("Run self-consistency", variant="primary")

                with gr.Accordion("View settings", open=False):
                    with gr.Row(elem_classes=["fp-tight"]):
                        sc_top_n = gr.Slider(
                            label="Leaderboard top-N",
                            minimum=1,
                            maximum=10,
                            value=SC_DEFAULT_TOP_N,
                            step=1,
                        )
                        sc_filter_samples = gr.Dropdown(
                            label="Filter by sample label",
                            choices=[],
                            value=[],
                            multiselect=True,
                            interactive=False,
                        )
                        sc_filter_sample_query = gr.Textbox(
                            label="Sample text contains",
                            value="",
                            placeholder="Optional text search on sample labels",
                        )
                    with gr.Row(elem_classes=["fp-tight"]):
                        sc_filter_tm_min = gr.Slider(
                            label="Minimum scTM",
                            minimum=SC_TM_MIN,
                            maximum=SC_TM_MAX,
                            value=SC_TM_MIN,
                            step=0.01,
                        )
                        sc_filter_rmsd_max = gr.Slider(
                            label="Maximum scRMSD",
                            minimum=SC_RMSD_MIN,
                            maximum=SC_RMSD_MAX,
                            value=SC_RMSD_MAX,
                            step=0.1,
                        )
                        sc_filter_plddt_min = gr.Slider(
                            label="Minimum ESMFold mean pLDDT",
                            minimum=SC_PLDDT_MIN,
                            maximum=SC_PLDDT_MAX,
                            value=SC_PLDDT_MIN,
                            step=0.01,
                        )
                    with gr.Row(elem_classes=["fp-tight"]):
                        sc_apply_view_settings_button = gr.Button("Apply view settings", variant="secondary")
                        sc_reset_view_settings_button = gr.Button("Reset view settings")

                sc_status = gr.Textbox(
                    label="Self-consistency status",
                    lines=4,
                    elem_classes=["fp-status"],
                )
                sc_summary = gr.Dataframe(
                    headers=SC_SUMMARY_HEADERS,
                    datatype=["str", "str"],
                    value=[],
                    label="Self-consistency summary",
                    interactive=False,
                    wrap=True,
                )
                sc_metrics = gr.Dataframe(
                    headers=SC_METRICS_HEADERS,
                    value=[],
                    label="Per-sequence metrics table (click a row to compare in 3D)",
                    interactive=False,
                    wrap=True,
                )
                with gr.Row(elem_classes=["fp-tight"]):
                    sc_export_button = gr.Button("Export current metrics as CSV", variant="secondary")
                    sc_export_file = gr.File(label="Filtered metrics (.csv)")
                with gr.Row(elem_classes=["fp-tight", "fp-plot-row"]):
                    with gr.Column(scale=1, min_width=560):
                        sc_tm_rmsd_plot = gr.HTML(
                            value=_empty_scatter_plot_html(
                                title="Self-consistency tradeoff (scTM vs scRMSD)",
                                message="Run self-consistency to render plot data.",
                            )
                        )
                    with gr.Column(scale=1, min_width=560):
                        sc_confidence_plot = gr.HTML(
                            value=_empty_scatter_plot_html(
                                title="ESMFold confidence vs scTM",
                                message="Run self-consistency with folding enabled to render confidence plot.",
                            )
                        )
                with gr.Row(elem_classes=["fp-tight"]):
                    sc_top_tm = gr.Dataframe(
                        headers=SC_TOP_TM_HEADERS,
                        value=[],
                        label="Top by highest scTM",
                        interactive=False,
                        wrap=True,
                    )
                    sc_top_rmsd = gr.Dataframe(
                        headers=SC_TOP_RMSD_HEADERS,
                        value=[],
                        label="Top by lowest scRMSD",
                        interactive=False,
                        wrap=True,
                    )
                sc_artifacts = gr.Files(label="Self-consistency artifacts")

        with gr.Tab("Advanced"):
            with gr.Group(elem_classes=["fp-card"]):
                gr.Markdown("### Recent runs")
                with gr.Row(elem_classes=["fp-tight"]):
                    history_dropdown = gr.Dropdown(
                        label="Run history",
                        choices=_run_history_choices(),
                        value=None,
                        interactive=True,
                    )
                    history_refresh_button = gr.Button("Refresh history")
                    history_load_button = gr.Button("Load selected run", variant="secondary")

            with gr.Group(elem_classes=["fp-card"]):
                gr.Markdown("### Saved example case")
                with gr.Row(elem_classes=["fp-tight"]):
                    save_example_button = gr.Button("Save selected sample as example", variant="secondary")
                    load_example_button = gr.Button("Load saved example", variant="secondary")
                    view_example_on_startup = gr.Checkbox(
                        label="View saved example on startup",
                        value=VIEW_EXAMPLE_ON_STARTUP_DEFAULT,
                    )

            with gr.Group(elem_classes=["fp-card"]):
                gr.Markdown("### Diagnostics")
                with gr.Row(elem_classes=["fp-tight"]):
                    preload_button = gr.Button("Preload model")
                    health_button = gr.Button("Refresh health")
                with gr.Accordion("Status and debug details", open=False):
                    status = gr.Textbox(
                        label="Run status",
                        lines=4,
                        elem_classes=["fp-status"],
                    )
                    example_status = gr.Textbox(
                        label="Example case status",
                        lines=3,
                        interactive=False,
                    )
                    health = gr.JSON(label="Health check")

    generation_outputs = [
        status,
        output_files,
        trajectory_output_files,
        health,
        sample_selector,
        molstar_viewer,
        sc_status,
        sc_summary,
        sc_metrics,
        sc_tm_rmsd_plot,
        sc_confidence_plot,
        sc_artifacts,
        sc_top_tm,
        sc_top_rmsd,
        sc_base_status_state,
        sc_summary_state,
        sc_metrics_state,
        sc_artifacts_state,
    ]
    example_load_outputs = [
        status,
        output_files,
        trajectory_output_files,
        sample_selector,
        molstar_viewer,
        sc_status,
        sc_summary,
        sc_metrics,
        sc_tm_rmsd_plot,
        sc_confidence_plot,
        sc_artifacts,
        sc_top_tm,
        sc_top_rmsd,
        sc_base_status_state,
        sc_summary_state,
        sc_metrics_state,
        sc_artifacts_state,
        example_status,
    ]

    mode.change(
        fn=lambda selected: (
            gr.update(visible=selected == "classifier"),
            gr.update(visible=selected == "conditional"),
        ),
        inputs=[mode],
        outputs=[classifier_group, conditional_group],
    )

    run_event = run_button.click(
        fn=run_generation,
        inputs=[
            mode,
            length,
            num_samples,
            seed_override,
            include_trajectory_artifacts,
            guidance_scale,
            target_class,
            num_timesteps,
            reference_pdb,
            fixed_residues_text,
            use_classifier_guidance,
        ],
        outputs=generation_outputs,
    )
    run_event.then(fn=update_selection_label, inputs=[sample_selector], outputs=[selection_label])
    run_event.then(fn=refresh_status_badge, inputs=None, outputs=[status_badge])
    run_event.then(fn=refresh_run_history, inputs=None, outputs=[history_dropdown])

    sample_selector.change(
        fn=update_selected_molstar,
        inputs=[sample_selector],
        outputs=[
            molstar_viewer,
            sc_status,
            sc_summary,
            sc_metrics,
            sc_tm_rmsd_plot,
            sc_confidence_plot,
            sc_artifacts,
            sc_top_tm,
            sc_top_rmsd,
            sc_base_status_state,
            sc_summary_state,
            sc_metrics_state,
            sc_artifacts_state,
        ],
    )
    sample_selector.change(fn=update_selection_label, inputs=[sample_selector], outputs=[selection_label])
    sample_selector.change(fn=_update_backbone_selector, inputs=[sample_selector], outputs=[backbone_compare_selector])
    sample_selector.change(fn=clear_compare_status, inputs=None, outputs=[molstar_compare_status])

    load_traj_button.click(
        fn=load_trajectory_view,
        inputs=[sample_selector, traj_source],
        outputs=[trajectory_viewer],
    )
    zip_button.click(fn=build_run_zip, inputs=[sample_selector], outputs=[zip_file])

    save_example_button.click(fn=save_example_case, inputs=[sample_selector], outputs=[example_status])
    load_example_button.click(fn=load_saved_example, inputs=None, outputs=example_load_outputs)
    demo_cta_button.click(fn=load_saved_example, inputs=None, outputs=example_load_outputs)
    load_example_button.click(fn=update_selection_label, inputs=[sample_selector], outputs=[selection_label])
    demo_cta_button.click(fn=update_selection_label, inputs=[sample_selector], outputs=[selection_label])

    sc_run_button.click(
        fn=run_self_consistency,
        inputs=[
            sample_selector,
            sc_num_sequences,
            sc_run_folding,
            sc_top_n,
        ],
        outputs=[
            sc_status,
            sc_summary,
            sc_metrics,
            sc_tm_rmsd_plot,
            sc_confidence_plot,
            sc_artifacts,
            sc_top_tm,
            sc_top_rmsd,
            sc_base_status_state,
            sc_summary_state,
            sc_metrics_state,
            sc_artifacts_state,
        ],
    )
    sc_apply_view_settings_button.click(
        fn=apply_sc_view_settings,
        inputs=[
            sc_base_status_state,
            sc_summary_state,
            sc_metrics_state,
            sc_artifacts_state,
            sc_top_n,
            sc_filter_samples,
            sc_filter_sample_query,
            sc_filter_tm_min,
            sc_filter_rmsd_max,
            sc_filter_plddt_min,
        ],
        outputs=[
            sc_status,
            sc_summary,
            sc_metrics,
            sc_tm_rmsd_plot,
            sc_confidence_plot,
            sc_artifacts,
            sc_top_tm,
            sc_top_rmsd,
        ],
    )
    sc_reset_view_settings_button.click(
        fn=reset_sc_view_settings,
        inputs=[
            sc_base_status_state,
            sc_summary_state,
            sc_metrics_state,
            sc_artifacts_state,
        ],
        outputs=[
            sc_top_n,
            sc_filter_samples,
            sc_filter_sample_query,
            sc_filter_tm_min,
            sc_filter_rmsd_max,
            sc_filter_plddt_min,
            sc_status,
            sc_summary,
            sc_metrics,
            sc_tm_rmsd_plot,
            sc_confidence_plot,
            sc_artifacts,
            sc_top_tm,
            sc_top_rmsd,
        ],
    )
    sc_export_button.click(
        fn=export_sc_metrics_csv,
        inputs=[
            sc_metrics_state,
            sc_filter_samples,
            sc_filter_sample_query,
            sc_filter_tm_min,
            sc_filter_rmsd_max,
            sc_filter_plddt_min,
        ],
        outputs=[sc_export_file],
    )
    sc_metrics_state.change(
        fn=_update_sc_filter_sample_selector,
        inputs=[sc_metrics_state],
        outputs=[sc_filter_samples],
    )
    sc_metrics_state.change(
        fn=_update_folded_selector,
        inputs=[sc_metrics_state],
        outputs=[folded_compare_selector],
    )
    sc_metrics.select(
        fn=on_sc_metrics_select,
        inputs=[sc_metrics_state, backbone_compare_selector],
        outputs=[backbone_compare_selector, folded_compare_selector, molstar_viewer, molstar_compare_status],
    )
    load_compare_button.click(
        fn=load_aligned_compare_view,
        inputs=[backbone_compare_selector, folded_compare_selector],
        outputs=[molstar_viewer, molstar_compare_status],
    )

    history_refresh_button.click(fn=refresh_run_history, inputs=None, outputs=[history_dropdown])
    history_event = history_load_button.click(
        fn=load_run_from_history,
        inputs=[history_dropdown],
        outputs=[
            status,
            sample_selector,
            molstar_viewer,
            sc_status,
            sc_summary,
            sc_metrics,
            sc_tm_rmsd_plot,
            sc_confidence_plot,
            sc_artifacts,
            sc_top_tm,
            sc_top_rmsd,
            sc_base_status_state,
            sc_summary_state,
            sc_metrics_state,
            sc_artifacts_state,
        ],
    )
    history_event.then(fn=update_selection_label, inputs=[sample_selector], outputs=[selection_label])

    preload_button.click(fn=preload_model, inputs=None, outputs=[status, health]).then(
        fn=refresh_status_badge, inputs=None, outputs=[status_badge]
    )
    health_button.click(fn=refresh_health, inputs=None, outputs=health).then(
        fn=refresh_status_badge, inputs=None, outputs=[status_badge]
    )

    demo.load(fn=refresh_health, inputs=None, outputs=health)
    demo.load(fn=refresh_status_badge, inputs=None, outputs=[status_badge])
    demo.load(fn=refresh_run_history, inputs=None, outputs=[history_dropdown])
    demo.load(
        fn=maybe_load_saved_example_on_startup,
        inputs=[view_example_on_startup],
        outputs=example_load_outputs,
    )


if __name__ == "__main__":
    demo.queue(default_concurrency_limit=1, max_size=16)
    demo.launch(
        server_name="0.0.0.0",
        server_port=int(os.getenv("PORT", "7860")),
        show_error=True,
    )