File size: 129,459 Bytes
8ab3008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Extraction Studio | PWA</title>
    
    <!-- Unified Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    
    <!-- Unified Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@700;800&family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Icons for Videoflow -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- Shared Libraries -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@vladmandic/face-api@1.7.12/dist/face-api.js"></script>

    <!-- React & Babel for Immager -->
    <script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

    <style>
        /* Base Resets */
        body { margin: 0; padding: 0; overflow-x: hidden; background-color: #000; }

        /* --- Immager Custom CSS --- */
        @keyframes spin { 100% { transform: rotate(360deg); } }
        .animate-spin { animation: spin 1s linear infinite; }
        @keyframes slideIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
        .animate-in { animation: slideIn 0.5s ease-out forwards; }

        /* --- Videoflow Custom CSS --- */
        .glass-panel { background: rgba(255, 255, 255, 0.03); backdrop-filter: blur(12px); border: 1px solid rgba(255, 255, 255, 0.08); }
        .gradient-text { background: linear-gradient(135deg, #818cf8 0%, #c084fc 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
        .status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
        #results-container::-webkit-scrollbar { width: 5px; }
        #results-container::-webkit-scrollbar-track { background: transparent; }
        #results-container::-webkit-scrollbar-thumb { background: #334155; border-radius: 10px; }
        .scan-line { position: absolute; top: 0; left: 0; width: 100%; height: 2px; background: #818cf8; box-shadow: 0 0 15px #818cf8; animation: scan 2s linear infinite; display: none; z-index: 10; }
        @keyframes scan { 0% { top: 0%; } 100% { top: 100%; } }
        .toggle-checkbox:checked { right: 0; border-color: #6366f1; }
        .toggle-checkbox:checked + .toggle-label { background-color: #6366f1; }
        .toggle-checkbox { right: 4px; z-index: 1; border-color: #e2e8f0; transition: all 0.3s; }
        .toggle-label { background-color: #cbd5e1; transition: all 0.3s; }
    </style>
</head>
<body>

    <!-- ========================================== -->
    <!-- GLOBAL UI: APP SWITCHER                    -->
    <!-- ========================================== -->
    <div class="fixed bottom-6 left-1/2 transform -translate-x-1/2 z-[9999] bg-black/60 backdrop-blur-xl border border-white/10 p-1.5 rounded-full flex gap-1 shadow-[0_0_30px_rgba(0,0,0,0.8)]">
        <button onclick="switchApp('immager')" id="tab-immager" class="px-5 py-2.5 rounded-full text-sm font-semibold transition-all duration-300 bg-indigo-600 text-white shadow-lg flex items-center gap-2">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="18" height="18" x="3" y="3" rx="2" ry="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"/></svg>
            Immager (Images)
        </button>
        <button onclick="switchApp('videoflow')" id="tab-videoflow" class="px-5 py-2.5 rounded-full text-sm font-semibold transition-all duration-300 text-white/60 hover:text-white hover:bg-white/10 flex items-center gap-2">
            <i class="fas fa-video"></i>
            HumanFrame (Video)
        </button>
        <button onclick="switchApp('urlfetcher')" id="tab-urlfetcher" class="px-5 py-2.5 rounded-full text-sm font-semibold transition-all duration-300 text-white/60 hover:text-white hover:bg-white/10 flex items-center gap-2">
            <i class="fas fa-link"></i>
            URL Loader
        </button>
    </div>

    <!-- ========================================== -->
    <!-- APP 1: IMMAGER (REACT)                     -->
    <!-- ========================================== -->
    <div id="app-immager" class="w-full min-h-screen bg-neutral-950 text-neutral-100 font-sans selection:bg-indigo-500/30">
        <div id="root"></div>
    </div>

    <!-- ========================================== -->
    <!-- APP 2: VIDEOFLOW (VANILLA JS)              -->
    <!-- ========================================== -->
    <div id="app-videoflow" class="w-full min-h-screen bg-[#0f172a] text-slate-200 selection:bg-indigo-500/30 hidden relative" style="font-family: 'Inter', sans-serif;">
        <!-- Navbar -->
        <nav class="border-b border-white/5 bg-[#0f172a]/80 backdrop-blur-md sticky top-0 z-50">
            <div class="max-w-7xl mx-auto px-6 h-16 flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-indigo-600 rounded-xl flex items-center justify-center shadow-lg shadow-indigo-500/20">
                        <i class="fas fa-user-check text-white text-lg"></i>
                    </div>
                    <h1 class="text-xl font-bold tracking-tight">HumanFrame<span class="gradient-text">AI</span></h1>
                </div>
                
                <div class="flex gap-3">
                    <div id="model-badge" class="flex items-center gap-2 bg-slate-800/50 px-3 py-1.5 rounded-full border border-white/5 text-xs font-medium text-slate-400">
                        <span class="status-dot bg-amber-500 animate-pulse"></span> MediaPipe Loading...
                    </div>
                    <div id="face-badge" class="flex items-center gap-2 bg-slate-800/50 px-3 py-1.5 rounded-full border border-white/5 text-xs font-medium text-slate-400 hidden">
                        <span class="status-dot bg-amber-500 animate-pulse"></span> FaceAPI Loading...
                    </div>
                </div>
            </div>
        </nav>

        <main class="max-w-7xl mx-auto px-6 py-10 pb-24">
            <div class="grid grid-cols-1 lg:grid-cols-12 gap-8">
                <!-- Left Panel: Configuration -->
                <div class="lg:col-span-4 space-y-6">
                    <!-- Upload Zone -->
                    <div id="drop-zone" class="glass-panel rounded-2xl p-8 text-center border-2 border-dashed border-indigo-500/20 hover:border-indigo-500/50 transition-all cursor-pointer group relative overflow-hidden">
                        <input type="file" id="video-input" class="hidden" accept="video/*">
                        <div class="relative z-10">
                            <div class="w-14 h-14 bg-indigo-500/10 rounded-2xl flex items-center justify-center mx-auto mb-3 group-hover:scale-110 transition-transform duration-300">
                                <i class="fas fa-video text-indigo-400 text-xl"></i>
                            </div>
                            <h3 class="text-md font-semibold text-white mb-1">Upload Video</h3>
                            <p class="text-xs text-slate-400">Drag & drop or click to browse</p>
                        </div>
                    </div>

                    <!-- Settings & Advanced -->
                    <div id="settings-panel" class="glass-panel rounded-2xl p-6 space-y-6 opacity-40 pointer-events-none transition-all">
                        <!-- Basic Engine -->
                        <div class="space-y-4">
                            <div class="flex items-center justify-between border-b border-white/5 pb-2">
                                <h4 class="font-bold text-white text-xs uppercase tracking-wider">Engine Settings</h4>
                            </div>
                            <div class="space-y-2">
                                <label class="text-xs font-semibold text-slate-400 flex justify-between">
                                    Scan Interval
                                    <span id="interval-val" class="text-indigo-400">0.5s</span>
                                </label>
                                <input type="range" id="scan-rate" min="0.1" max="2.0" step="0.1" value="0.5" class="w-full h-1.5 bg-slate-700 rounded-lg appearance-none cursor-pointer accent-indigo-500">
                            </div>
                            <div class="space-y-2">
                                <label class="text-xs font-semibold text-slate-400 flex justify-between">
                                    Detection Confidence
                                    <span id="conf-val" class="text-indigo-400">50%</span>
                                </label>
                                <input type="range" id="confidence" min="0.3" max="0.9" step="0.05" value="0.5" class="w-full h-1.5 bg-slate-700 rounded-lg appearance-none cursor-pointer accent-indigo-500">
                            </div>
                        </div>

                        <!-- Advanced Features -->
                        <div class="space-y-4">
                            <div class="flex items-center justify-between border-b border-white/5 pb-2">
                                <h4 class="font-bold text-white text-xs uppercase tracking-wider text-purple-400">Advanced Extraction</h4>
                            </div>
                            
                            <!-- Extract All Frames Toggle -->
                            <div class="flex items-center justify-between mb-4 border-b border-white/5 pb-4">
                                <div class="flex flex-col">
                                    <span class="text-sm font-medium text-slate-200">Extract Full Frames</span>
                                    <span class="text-[10px] text-slate-500">Extracts entire frames, skipping AI filters</span>
                                </div>
                                <div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
                                    <input type="checkbox" name="toggle" id="extract-all-toggle" class="toggle-checkbox absolute block w-5 h-5 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
                                    <label for="extract-all-toggle" class="toggle-label block overflow-hidden h-5 rounded-full bg-slate-600 cursor-pointer"></label>
                                </div>
                            </div>

                            <div id="advanced-filters-container" class="space-y-4 transition-all duration-300">
                                <!-- Smart Body Crop Toggle -->
                                <div class="flex items-center justify-between">
                                    <div class="flex flex-col">
                                        <span class="text-sm font-medium text-slate-200">Smart Body Crop</span>
                                        <span class="text-[10px] text-slate-500">Extracts the person bounding box</span>
                                    </div>
                                    <div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
                                        <input type="checkbox" name="toggle" id="auto-crop-toggle" class="toggle-checkbox absolute block w-5 h-5 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
                                        <label for="auto-crop-toggle" class="toggle-label block overflow-hidden h-5 rounded-full bg-slate-600 cursor-pointer"></label>
                                    </div>
                                </div>
                                <!-- 512x512 Face Crop Toggle -->
                                <div class="flex items-center justify-between">
                                    <div class="flex flex-col">
                                        <span class="text-sm font-medium text-slate-200">Tight Face Crop (512px)</span>
                                        <span class="text-[10px] text-slate-500">Extracts faces specifically in 512x512</span>
                                    </div>
                                    <div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
                                        <input type="checkbox" name="toggle" id="face-crop-toggle" class="toggle-checkbox absolute block w-5 h-5 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
                                        <label for="face-crop-toggle" class="toggle-label block overflow-hidden h-5 rounded-full bg-slate-600 cursor-pointer"></label>
                                    </div>
                                </div>
                                <!-- Require Visible Face Toggle -->
                                <div class="flex items-center justify-between">
                                    <div class="flex flex-col">
                                        <span class="text-sm font-medium text-slate-200">Require Visible Face</span>
                                        <span class="text-[10px] text-slate-500">Skip frames with bodies but no faces</span>
                                    </div>
                                    <div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
                                        <input type="checkbox" name="toggle" id="require-face-toggle" class="toggle-checkbox absolute block w-5 h-5 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
                                        <label for="require-face-toggle" class="toggle-label block overflow-hidden h-5 rounded-full bg-slate-600 cursor-pointer"></label>
                                    </div>
                                </div>
                                <!-- Target Face Match -->
                                <div class="bg-slate-800/40 p-3 rounded-xl border border-white/5">
                                    <div class="flex flex-col mb-2">
                                        <span class="text-sm font-medium text-slate-200">Target Face Match</span>
                                        <span class="text-[10px] text-slate-500">Only extract frames containing these people</span>
                                    </div>
                                    
                                    <div id="target-faces-container" class="flex flex-wrap gap-3 mt-3 empty:hidden"></div>

                                    <div class="flex items-center gap-3 mt-3">
                                        <div id="face-upload-btn" class="flex-grow bg-slate-700 hover:bg-slate-600 text-xs text-center py-2 rounded-lg cursor-pointer transition-colors border border-dashed border-slate-500">
                                            <i class="fas fa-camera mr-1"></i> Upload Target Face(s)
                                        </div>
                                        <input type="file" id="face-input" class="hidden" accept="image/*" multiple>
                                    </div>
                                    <p id="face-status-text" class="text-[10px] text-amber-400 mt-2 hidden text-center"><i class="fas fa-spinner animate-spin"></i> Analyzing face(s)...</p>
                                </div>
                            </div>
                        </div>

                        <button id="start-btn" class="w-full bg-indigo-600 hover:bg-indigo-500 text-white font-bold py-3 rounded-xl shadow-xl shadow-indigo-500/10 transition-all flex items-center justify-center gap-3">
                            <i class="fas fa-microchip"></i> Start AI Extraction
                        </button>
                    </div>

                    <!-- Monitoring Window -->
                    <div class="glass-panel rounded-2xl overflow-hidden relative shadow-2xl group border-2 border-[#0f172a]">
                        <div class="scan-line" id="scanner"></div>
                        <canvas id="preview-canvas" class="w-full aspect-video bg-black object-contain"></canvas>
                        <div class="absolute bottom-0 left-0 right-0 p-3 bg-gradient-to-t from-black/80 to-transparent flex items-center justify-between">
                            <span class="text-[10px] font-bold tracking-widest text-indigo-400 uppercase">Live Monitor</span>
                            <div id="fps-counter" class="text-[10px] font-mono text-slate-400">-- FPS</div>
                        </div>
                    </div>
                </div>

                <!-- Right Panel: Results Gallery -->
                <div class="lg:col-span-8 flex flex-col h-[calc(100vh-160px)] min-h-[600px]">
                    <div class="glass-panel rounded-3xl flex flex-col h-full overflow-hidden border border-white/10 relative">
                        <!-- Toolbar -->
                        <div class="p-6 border-b border-white/5 flex flex-col md:flex-row gap-4 items-start md:items-center justify-between bg-white/[0.02]">
                            <div>
                                <h2 class="text-xl font-bold text-white">Detection Gallery</h2>
                                <p id="stats-text" class="text-sm text-slate-500">System idle. Awaiting video upload.</p>
                            </div>
                            <div class="flex flex-wrap gap-2">
                                <button id="select-all-btn" class="hidden px-4 py-2.5 text-slate-300 hover:text-white text-sm font-medium transition-colors border border-transparent hover:border-white/10 rounded-xl">
                                    Select All
                                </button>
                                <button id="export-immager-btn" class="hidden px-5 py-2.5 bg-purple-600 hover:bg-purple-500 text-white text-sm font-bold rounded-xl transition-all flex items-center gap-2 shadow-lg shadow-purple-500/20">
                                    <i class="fas fa-magic"></i> Export All to Immager
                                </button>
                                <button id="download-btn" class="hidden px-5 py-2.5 bg-emerald-500 hover:bg-emerald-400 text-white text-sm font-bold rounded-xl transition-all flex items-center gap-2 shadow-lg shadow-emerald-500/20">
                                    <i class="fas fa-file-export"></i> Download All
                                </button>
                            </div>
                        </div>

                        <!-- Progress Bar -->
                        <div id="progress-container" class="px-6 py-4 bg-indigo-500/5 hidden border-b border-white/5">
                            <div class="flex justify-between items-center mb-2">
                                <span class="text-xs font-bold text-indigo-300 uppercase tracking-tighter" id="status-label">Analyzing Frames...</span>
                                <span class="text-xs font-mono text-indigo-300" id="progress-percent">0%</span>
                            </div>
                            <div class="w-full bg-white/5 h-1.5 rounded-full overflow-hidden">
                                <div id="progress-bar" class="h-full bg-indigo-500 transition-all duration-300 shadow-[0_0_10px_#6366f1]" style="width: 0%"></div>
                            </div>
                        </div>

                        <!-- Gallery Grid -->
                        <div id="results-container" class="flex-grow overflow-y-auto p-6 relative">
                            <div id="results" class="flex flex-wrap gap-4 content-start"></div>
                            <!-- Placeholder -->
                            <div id="empty-state" class="absolute inset-0 flex flex-col items-center justify-center opacity-20">
                                <i class="fas fa-images text-7xl mb-6"></i>
                                <p class="text-lg font-medium">No frames extracted yet</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <video id="hidden-video" class="hidden" muted></video>

        <!-- Fullscreen Image Preview Modal -->
        <div id="video-preview-modal" class="fixed inset-0 z-[100] bg-black/90 hidden flex items-center justify-center p-4 backdrop-blur-md">
            <button id="close-preview" class="absolute top-6 right-6 text-white text-4xl hover:text-indigo-400 transition-colors z-50">
                <i class="fas fa-times"></i>
            </button>
            <img id="preview-modal-img" src="" class="max-w-full max-h-[90vh] object-contain rounded-lg shadow-2xl border border-white/10">
        </div>
    </div>

    <!-- ========================================== -->
    <!-- APP 3: URL FETCHER (VANILLA JS)            -->
    <!-- ========================================== -->
    <div id="app-urlfetcher" class="w-full min-h-screen bg-[#0f172a] text-slate-200 hidden relative" style="font-family: 'Inter', sans-serif;">
        <!-- Navbar -->
        <nav class="border-b border-white/5 bg-[#0f172a]/80 backdrop-blur-md sticky top-0 z-50">
            <div class="max-w-7xl mx-auto px-6 h-16 flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-teal-600 rounded-xl flex items-center justify-center shadow-lg shadow-teal-500/20">
                        <i class="fas fa-link text-white text-lg"></i>
                    </div>
                    <h1 class="text-xl font-bold tracking-tight">URL<span class="text-teal-400">Fetch</span></h1>
                </div>
            </div>
        </nav>

        <main class="max-w-7xl mx-auto px-6 py-10 pb-24">
            <div class="grid grid-cols-1 lg:grid-cols-12 gap-8">
                <!-- Left Panel -->
                <div class="lg:col-span-4 space-y-6">
                    <div class="glass-panel rounded-2xl p-6 space-y-4 border border-white/10">
                        <h2 class="text-lg font-bold text-white mb-2">Source URLs</h2>
                        
                        <div class="space-y-2">
                            <label class="text-xs font-semibold text-slate-400">Paste links (one per line)</label>
                            <textarea id="url-input-text" rows="6" class="w-full bg-slate-900 border border-slate-700 rounded-xl p-3 text-sm text-slate-300 focus:outline-none focus:border-teal-500 transition-colors resize-none" placeholder="https://example.com/img1.jpg&#10;https://example.com/img2.png"></textarea>
                        </div>

                        <div class="relative flex py-2 items-center">
                            <div class="flex-grow border-t border-slate-700"></div>
                            <span class="flex-shrink-0 mx-4 text-slate-500 text-xs font-medium uppercase">OR</span>
                            <div class="flex-grow border-t border-slate-700"></div>
                        </div>

                        <div id="url-drop-zone" class="bg-slate-900 border-2 border-dashed border-slate-700 hover:border-teal-500 rounded-xl p-6 text-center transition-colors cursor-pointer group">
                            <input type="file" id="url-file-input" class="hidden" accept=".txt">
                            <i class="fas fa-file-alt text-2xl text-slate-500 group-hover:text-teal-400 transition-colors mb-2"></i>
                            <h3 class="text-sm font-medium text-slate-300">Upload .txt File</h3>
                            <p class="text-[10px] text-slate-500 mt-1" id="url-file-name">Drag & drop or click</p>
                        </div>

                        <button id="url-fetch-btn" class="w-full bg-teal-600 hover:bg-teal-500 text-white font-bold py-3 rounded-xl shadow-xl shadow-teal-500/20 transition-all flex items-center justify-center gap-2 mt-4">
                            <i class="fas fa-cloud-download-alt"></i> Fetch Images
                        </button>
                    </div>
                </div>

                <!-- Right Panel -->
                <div class="lg:col-span-8 flex flex-col h-[calc(100vh-160px)] min-h-[600px]">
                    <div class="glass-panel rounded-3xl flex flex-col h-full overflow-hidden border border-white/10 relative">
                        <!-- Toolbar -->
                        <div class="p-6 border-b border-white/5 flex flex-wrap gap-4 items-center justify-between bg-white/[0.02]">
                            <div>
                                <h2 class="text-xl font-bold text-white">Fetched Gallery</h2>
                                <p id="url-stats-text" class="text-sm text-slate-500">No images loaded yet.</p>
                            </div>
                            <div class="flex gap-2">
                                <button id="url-select-all-btn" class="hidden px-4 py-2 text-slate-300 hover:text-white text-sm font-medium transition-colors border border-transparent hover:border-white/10 rounded-xl">
                                    Select All
                                </button>
                                <button id="url-download-btn" class="hidden px-4 py-2 bg-emerald-600 hover:bg-emerald-500 text-white text-sm font-bold rounded-xl transition-all shadow-lg flex items-center gap-2">
                                    <i class="fas fa-file-archive"></i> Download ZIP
                                </button>
                            </div>
                        </div>

                        <!-- Progress Bar -->
                        <div id="url-progress-container" class="px-6 py-4 bg-teal-500/5 hidden border-b border-white/5">
                            <div class="flex justify-between items-center mb-2">
                                <span class="text-xs font-bold text-teal-300 uppercase tracking-tighter" id="url-status-label">Fetching URLs...</span>
                                <span class="text-xs font-mono text-teal-300" id="url-progress-percent">0/0</span>
                            </div>
                            <div class="w-full bg-white/5 h-1.5 rounded-full overflow-hidden">
                                <div id="url-progress-bar" class="h-full bg-teal-500 transition-all duration-300" style="width: 0%"></div>
                            </div>
                        </div>

                        <!-- Gallery Grid -->
                        <div id="url-results-container" class="flex-grow overflow-y-auto p-6 relative">
                            <div id="url-results" class="flex flex-wrap gap-4 content-start"></div>
                            <!-- Placeholder -->
                            <div id="url-empty-state" class="absolute inset-0 flex flex-col items-center justify-center opacity-20">
                                <i class="fas fa-images text-7xl mb-6"></i>
                                <p class="text-lg font-medium">Ready to fetch</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>

        <!-- Popup Modal for URL App (Not full screen) -->
        <div id="url-preview-modal" class="fixed inset-0 z-[100] bg-black/60 hidden flex items-center justify-center p-4 backdrop-blur-sm">
            <div class="relative bg-[#0f172a] p-2 rounded-2xl border border-white/10 shadow-2xl max-w-[80vw] max-h-[80vh] flex flex-col">
                <div class="flex justify-between items-center px-4 py-2 border-b border-white/5 mb-2">
                    <span class="text-sm font-medium text-slate-300">Image Preview</span>
                    <button id="url-close-preview" class="text-slate-400 hover:text-white transition-colors">
                        <i class="fas fa-times text-xl"></i>
                    </button>
                </div>
                <div class="overflow-hidden flex-grow flex items-center justify-center p-2">
                    <img id="url-preview-modal-img" src="" class="max-w-full max-h-full object-contain rounded-lg">
                </div>
            </div>
        </div>
    </div>

    <!-- ========================================== -->
    <!-- LOGIC: APP SWITCHER                        -->
    <!-- ========================================== -->
    <script>
        function switchApp(appName) {
            const immagerApp = document.getElementById('app-immager');
            const videoflowApp = document.getElementById('app-videoflow');
            const urlfetcherApp = document.getElementById('app-urlfetcher');
            
            const tabImmager = document.getElementById('tab-immager');
            const tabVideoflow = document.getElementById('tab-videoflow');
            const tabUrlfetcher = document.getElementById('tab-urlfetcher');

            if (appName === 'immager') {
                immagerApp.classList.remove('hidden');
                videoflowApp.classList.add('hidden');
                urlfetcherApp.classList.add('hidden');
                
                tabImmager.classList.replace('bg-transparent', 'bg-indigo-600');
                tabImmager.classList.replace('text-white/60', 'text-white');
                tabImmager.classList.remove('hover:bg-white/10');
                
                tabVideoflow.classList.replace('bg-indigo-600', 'bg-transparent');
                tabVideoflow.classList.replace('text-white', 'text-white/60');
                tabVideoflow.classList.add('hover:bg-white/10');

                tabUrlfetcher.classList.replace('bg-teal-600', 'bg-transparent');
                tabUrlfetcher.classList.replace('text-white', 'text-white/60');
                tabUrlfetcher.classList.add('hover:bg-white/10');
                window.scrollTo(0,0);
            } else if (appName === 'videoflow') {
                immagerApp.classList.add('hidden');
                videoflowApp.classList.remove('hidden');
                urlfetcherApp.classList.add('hidden');
                
                tabVideoflow.classList.replace('bg-transparent', 'bg-indigo-600');
                tabVideoflow.classList.replace('text-white/60', 'text-white');
                tabVideoflow.classList.remove('hover:bg-white/10');

                tabImmager.classList.replace('bg-indigo-600', 'bg-transparent');
                tabImmager.classList.replace('text-white', 'text-white/60');
                tabImmager.classList.add('hover:bg-white/10');

                tabUrlfetcher.classList.replace('bg-teal-600', 'bg-transparent');
                tabUrlfetcher.classList.replace('text-white', 'text-white/60');
                tabUrlfetcher.classList.add('hover:bg-white/10');
                window.scrollTo(0,0);
            } else {
                immagerApp.classList.add('hidden');
                videoflowApp.classList.add('hidden');
                urlfetcherApp.classList.remove('hidden');
                
                tabUrlfetcher.classList.replace('bg-transparent', 'bg-teal-600');
                tabUrlfetcher.classList.replace('text-white/60', 'text-white');
                tabUrlfetcher.classList.remove('hover:bg-white/10');

                tabImmager.classList.replace('bg-indigo-600', 'bg-transparent');
                tabImmager.classList.replace('text-white', 'text-white/60');
                tabImmager.classList.add('hover:bg-white/10');

                tabVideoflow.classList.replace('bg-indigo-600', 'bg-transparent');
                tabVideoflow.classList.replace('text-white', 'text-white/60');
                tabVideoflow.classList.add('hover:bg-white/10');
                window.scrollTo(0,0);
            }
        }
    </script>

    <!-- ========================================== -->
    <!-- LOGIC: APP 1 (IMMAGER - REACT)             -->
    <!-- ========================================== -->
    <script type="text/babel">
        const { useState, useEffect, useCallback, useRef } = React;

        const LogoIcon = ({size=24, className=""}) => (
          <svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 512 512" className={className}>
            <rect width="512" height="512" rx="120" fill="#12C369"/>
            <circle cx="380" cy="130" r="55" fill="#ffffff"/>
            <path d="M310 280 L440 430 L180 430 Z" fill="#91E6B3" stroke="#91E6B3" strokeWidth="30" strokeLinejoin="round"/>
            <path d="M220 220 L340 430 L100 430 Z" fill="#ffffff" stroke="#ffffff" strokeWidth="40" strokeLinejoin="round"/>
          </svg>
        );
        const UsersIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>);
        const Trash2Icon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/></svg>);
        const UploadCloudIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M12 12v9"/><path d="m16 16-4-4-4 4"/></svg>);
        const Loader2Icon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>);
        const CheckCircleIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><path d="m9 11 3 3L22 4"/></svg>);
        const ImageIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><rect width="18" height="18" x="3" y="3" rx="2" ry="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"/></svg>);
        const CheckSquareIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><polyline points="9 11 12 14 22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg>);
        const SquareIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><rect width="18" height="18" x="3" y="3" rx="2" ry="2"/></svg>);
        const DownloadIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" x2="12" y1="15" y2="3"/></svg>);
        const SettingsIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1-1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/></svg>);
        const EditIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>);
        const XIcon = ({size=24, className=""}) => (<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>);

        function FaceExtractApp() {
          const [isModelLoading, setIsModelLoading] = useState(true);
          const [isProcessing, setIsProcessing] = useState(false);
          const [progress, setProgress] = useState(0);
          const [statusText, setStatusText] = useState('Initializing Engine...');
          const [faceGroups, setFaceGroups] = useState([]);
          const [selectedFaceIds, setSelectedFaceIds] = useState(new Set());
          const fileInputRef = useRef(null);
          
          const [extractedFaces, setExtractedFaces] = useState([]);
          const [matchThreshold, setMatchThreshold] = useState(0.50);

          const [cropSettings, setCropSettings] = useState({ padding: 0.05, topPadding: 0.2, shape: 'square' });
          const [showSettings, setShowSettings] = useState(false);
          const [editingFace, setEditingFace] = useState(null);
          const editorImgRef = useRef(null);
          
          const [dragState, setDragState] = useState({ isDragging: false, startX: 0, startY: 0, initialOffsetX: 0, initialOffsetY: 0 });

          const [dragOverGroupId, setDragOverGroupId] = useState(null);
          const [isDraggingFace, setIsDraggingFace] = useState(false);
          const draggedFaceRef = useRef(null);
          const [pendingTransferFiles, setPendingTransferFiles] = useState(null);

          // Listen for cross-app transfer event
          useEffect(() => {
            const handleTransfer = (e) => {
              setPendingTransferFiles(e.detail);
            };
            window.addEventListener('SEND_TO_IMMAGER', handleTransfer);
            return () => window.removeEventListener('SEND_TO_IMMAGER', handleTransfer);
          }, []);

          // Initialize models
          useEffect(() => {
            const initModels = async () => {
              while(!window.faceapi || !window.JSZip) {
                  await new Promise(r => setTimeout(r, 100));
              }
              try {
                setStatusText("Loading AI Models (~5MB)...");
                const MODEL_URL = 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api@1.7.12/model/';
                
                await window.faceapi.nets.ssdMobilenetv1.loadFromUri(MODEL_URL);
                await window.faceapi.nets.faceLandmark68Net.loadFromUri(MODEL_URL);
                await window.faceapi.nets.faceRecognitionNet.loadFromUri(MODEL_URL);
                
                setIsModelLoading(false);
                setStatusText("Ready");
              } catch (error) {
                console.error(error);
                setStatusText("Error loading AI models.");
              }
            };
            initModels();
          }, []);

          // Trigger processing of transferred files once models load
          useEffect(() => {
            if (pendingTransferFiles && pendingTransferFiles.length > 0 && !isModelLoading && !isProcessing) {
                processImages(pendingTransferFiles);
                setPendingTransferFiles(null);
            }
          }, [pendingTransferFiles, isModelLoading, isProcessing]);

          const generateCrop = (img, box, settings, manualOffsets = { x: 0, y: 0, zoom: 1, resolution: 'auto' }) => {
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');

            let padX = box.width * settings.padding;
            let padY = box.height * settings.padding;

            let tw = box.width + (padX * 2);
            let th = box.height + padY + (box.height * settings.topPadding);

            if (settings.shape === 'square') {
              const size = Math.max(tw, th);
              tw = size;
              th = size;
            }

            tw /= manualOffsets.zoom;
            th /= manualOffsets.zoom;

            let cx = box.x + box.width / 2;
            let cy = box.y + box.height / 2 - (box.height * settings.topPadding / 2) + (padY / 2);

            cx += manualOffsets.x;
            cy += manualOffsets.y;

            const cropX = Math.max(0, cx - tw / 2);
            const cropY = Math.max(0, cy - th / 2);
            const cropW = Math.min(img.width - cropX, tw);
            const cropH = Math.min(img.height - cropY, th);

            let targetW = cropW;
            let targetH = cropH;

            if (manualOffsets.resolution && manualOffsets.resolution !== 'auto') {
              const res = parseInt(manualOffsets.resolution, 10);
              targetW = res;
              targetH = settings.shape === 'original' ? Math.round(res * (cropH / cropW)) : res; 
            }

            canvas.width = targetW;
            canvas.height = targetH;
            ctx.drawImage(img, cropX, cropY, cropW, cropH, 0, 0, targetW, targetH);
            return canvas.toDataURL('image/jpeg', 0.9);
          };

          const openEditor = async (face, groupIndex, faceIndex) => {
            const img = new Image();
            img.src = face.sourceUrl;
            await new Promise(r => img.onload = r);
            editorImgRef.current = img;

            setEditingFace({
              ...face,
              groupIndex,
              faceIndex,
              manualOffsets: face.manualOffsets || { x: 0, y: 0, zoom: 1, resolution: 'auto' },
              previewUrl: face.cropDataUrl
            });
            setDragState({ isDragging: false, startX: 0, startY: 0, initialOffsetX: 0, initialOffsetY: 0 });
          };

          const updateManualCrop = (updates) => {
            if (!editingFace || !editorImgRef.current) return;
            const newOffsets = { ...editingFace.manualOffsets, ...updates };
            const newPreview = generateCrop(editorImgRef.current, editingFace.originalBox, cropSettings, newOffsets);
            setEditingFace(prev => ({ ...prev, manualOffsets: newOffsets, previewUrl: newPreview }));
          };

          const saveManualCrop = () => {
            const newGroups = [...faceGroups];
            newGroups[editingFace.groupIndex].faces[editingFace.faceIndex].cropDataUrl = editingFace.previewUrl;
            newGroups[editingFace.groupIndex].faces[editingFace.faceIndex].manualOffsets = editingFace.manualOffsets;
            setFaceGroups(newGroups);
            setEditingFace(null);
          };

          const updateGroupName = (groupId, newName) => {
            setFaceGroups(prevGroups => prevGroups.map(g => g.id === groupId ? { ...g, name: newName } : g));
          };

          const handleDragStart = (e, faceId, sourceGroupId) => {
            let dragItems = [];
            // If the dragged face is selected, bundle all selected faces
            if (selectedFaceIds.has(faceId)) {
              faceGroups.forEach(g => {
                g.faces.forEach(f => {
                  if (selectedFaceIds.has(f.id)) {
                    dragItems.push({ faceId: f.id, sourceGroupId: g.id });
                  }
                });
              });
            } else {
              // Otherwise, just drag this specific single face
              dragItems = [{ faceId, sourceGroupId }];
            }
            
            // --- Custom Drag Image for Multiple Items ---
            if (dragItems.length > 1) {
              const dragGhost = document.createElement('div');
              dragGhost.id = 'drag-ghost-container';
              dragGhost.style.position = 'absolute';
              dragGhost.style.top = '-1000px';
              dragGhost.style.left = '-1000px';
              dragGhost.style.width = '80px';
              dragGhost.style.height = '80px';
              
              // Get URLs for the first 3 faces to create a stack
              const previewFaces = dragItems.slice(0, 3).map(item => {
                  let url = '';
                  faceGroups.forEach(g => {
                      const f = g.faces.find(face => face.id === item.faceId);
                      if (f) url = f.cropDataUrl;
                  });
                  return url;
              });

              // Reverse to render the bottom-most image first, top-most last
              previewFaces.reverse().forEach((url, i) => { 
                  if (url) {
                      const img = document.createElement('img');
                      img.src = url;
                      img.style.position = 'absolute';
                      img.style.width = '60px';
                      img.style.height = '60px';
                      img.style.borderRadius = '12px';
                      img.style.objectFit = 'cover';
                      img.style.border = '2px solid white';
                      img.style.boxShadow = '0 4px 6px rgba(0,0,0,0.3)';
                      img.style.top = `${(2 - i) * 6}px`;
                      img.style.left = `${(2 - i) * 6}px`;
                      img.style.zIndex = i;
                      dragGhost.appendChild(img);
                  }
              });

              // Add the count badge
              const badge = document.createElement('div');
              badge.style.position = 'absolute';
              badge.style.bottom = '0';
              badge.style.right = '0';
              badge.style.background = '#4f46e5'; 
              badge.style.color = 'white';
              badge.style.borderRadius = '999px';
              badge.style.padding = '2px 8px';
              badge.style.fontSize = '12px';
              badge.style.fontWeight = 'bold';
              badge.style.zIndex = '10';
              badge.style.border = '2px solid white';
              badge.style.boxShadow = '0 2px 4px rgba(0,0,0,0.2)';
              badge.innerText = dragItems.length;
              dragGhost.appendChild(badge);

              document.body.appendChild(dragGhost);
              e.dataTransfer.setDragImage(dragGhost, 40, 40);
              
              setTimeout(() => {
                  if (dragGhost.parentNode) dragGhost.parentNode.removeChild(dragGhost);
              }, 50);
            }
            
            draggedFaceRef.current = dragItems;
            e.dataTransfer.effectAllowed = 'move';
            e.dataTransfer.setData('text/plain', dragItems.map(i => i.faceId).join(','));
            setTimeout(() => { setIsDraggingFace(true); }, 0);
          };

          const handleDragEnd = (e) => {
            setIsDraggingFace(false);
            setDragOverGroupId(null);
            draggedFaceRef.current = null;
          };

          const handleDragOver = (e, targetGroupId) => {
            e.preventDefault();
            e.dataTransfer.dropEffect = 'move';
            if (dragOverGroupId !== targetGroupId) setDragOverGroupId(targetGroupId);
          };

          const handleDragLeave = (e) => { e.preventDefault(); };

          const handleDrop = (e, targetGroupId) => {
            e.preventDefault();
            setIsDraggingFace(false);
            setDragOverGroupId(null);

            const dragItems = draggedFaceRef.current;
            if (!dragItems || dragItems.length === 0) return;
            
            // If dragging a single item to the exact same group, do nothing to prevent flicker
            if (dragItems.length === 1 && dragItems[0].sourceGroupId === targetGroupId) return;

            setTimeout(() => {
              setFaceGroups(prevGroups => {
                let movedFaces = [];
                
                // 1. Remove all bundled faces from their respective source groups
                let updatedGroups = prevGroups.map(group => {
                  const itemsToRemove = new Set(dragItems.filter(item => item.sourceGroupId === group.id).map(i => i.faceId));
                  if (itemsToRemove.size > 0) {
                    const extracted = group.faces.filter(f => itemsToRemove.has(f.id));
                    movedFaces.push(...extracted);
                    return { ...group, faces: group.faces.filter(f => !itemsToRemove.has(f.id)) };
                  }
                  return group;
                });

                if (movedFaces.length === 0) return prevGroups;

                // 2. Add all bundled faces into the target group
                if (targetGroupId === 'new-group') {
                  updatedGroups.unshift({
                    id: `group-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
                    name: '',
                    baseDescriptor: movedFaces[0].descriptor,
                    faces: movedFaces
                  });
                } else {
                  updatedGroups = updatedGroups.map(group => {
                    if (group.id === targetGroupId) return { ...group, faces: [...group.faces, ...movedFaces] };
                    return group;
                  });
                }
                
                // 3. Clean up any empty groups
                return updatedGroups.filter(g => g.faces.length > 0);
              });
              
              // Clear selection after a successful multi-drag
              setSelectedFaceIds(new Set());
            }, 0);
          };

          const applyGlobalCropSettings = async (newSettings) => {
            setCropSettings(newSettings);
            if (extractedFaces.length === 0) return;

            setIsProcessing(true);
            setStatusText("Applying new crop settings...");
            await new Promise(resolve => setTimeout(resolve, 50));

            try {
              const imageCache = {};
              const updatedFaces = [];

              for (let i = 0; i < extractedFaces.length; i++) {
                const face = extractedFaces[i];
                let img = imageCache[face.sourceUrl];
                if (!img) {
                  img = new Image();
                  img.src = face.sourceUrl;
                  await new Promise(r => img.onload = r);
                  imageCache[face.sourceUrl] = img;
                }
                const newCropDataUrl = generateCrop(img, face.originalBox, newSettings, face.manualOffsets || { x: 0, y: 0, zoom: 1, resolution: 'auto' });
                updatedFaces.push({ ...face, cropDataUrl: newCropDataUrl });
              }

              setExtractedFaces(updatedFaces);
              setFaceGroups(prevGroups => prevGroups.map(group => ({
                ...group,
                faces: group.faces.map(gFace => {
                  const updatedFace = updatedFaces.find(uf => uf.id === gFace.id);
                  return updatedFace ? { ...gFace, cropDataUrl: updatedFace.cropDataUrl } : gFace;
                })
              })));
            } catch (error) {
              console.error("Error applying crop settings:", error);
            } finally {
              setIsProcessing(false);
              setStatusText("Done");
            }
          };

          const clusterFaces = useCallback((facesToCluster, threshold) => {
            const groups = [];
            facesToCluster.forEach(face => {
              let foundGroup = false;
              for (let group of groups) {
                const distance = window.faceapi.euclideanDistance(group.baseDescriptor, face.descriptor);
                if (distance < threshold) { 
                  group.faces.push(face);
                  foundGroup = true;
                  break;
                }
              }
              if (!foundGroup) {
                groups.push({
                  id: `group-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
                  name: '', 
                  baseDescriptor: face.descriptor,
                  faces: [face]
                });
              }
            });
            groups.sort((a, b) => b.faces.length - a.faces.length);
            setFaceGroups(groups);
          }, []);

          const processImages = async (files) => {
            if (!files || files.length === 0) return;
            
            setIsProcessing(true);
            // Append rather than replace if adding from VideoFlow while groups exist
            const startingFaces = faceGroups.length > 0 ? extractedFaces : [];
            const allExtractedFaces = [...startingFaces];

            for (let i = 0; i < files.length; i++) {
              const file = files[i];
              if (!file.type.startsWith('image/')) continue;

              setStatusText(`Scanning image ${i + 1} of ${files.length}...`);
              setProgress(((i) / files.length) * 100);

              try {
                const img = await new Promise((resolve, reject) => {
                  const reader = new FileReader();
                  reader.onload = (e) => {
                    const image = new Image();
                    image.src = e.target.result;
                    image.onload = () => resolve(image);
                    image.onerror = reject;
                  };
                  reader.onerror = reject;
                  reader.readAsDataURL(file);
                });

                const detections = await window.faceapi.detectAllFaces(img)
                  .withFaceLandmarks()
                  .withFaceDescriptors();

                const sourceUrl = URL.createObjectURL(file);

                detections.forEach((det, idx) => {
                  const cropDataUrl = generateCrop(img, det.detection.box, cropSettings);
                  allExtractedFaces.push({
                    id: `face-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
                    sourceFile: file.name,
                    sourceUrl: sourceUrl,
                    originalBox: det.detection.box,
                    cropDataUrl,
                    descriptor: det.descriptor
                  });
                });
              } catch (err) { console.error(`Error processing ${file.name}`, err); }
            }

            setStatusText("Organizing faces by person...");
            setExtractedFaces(allExtractedFaces);
            clusterFaces(allExtractedFaces, matchThreshold);
            setIsProcessing(false);
            setProgress(100);
            setStatusText("Done");
          };

          const onDrop = useCallback((e) => {
            e.preventDefault();
            if (isModelLoading || isProcessing) return;
            const files = Array.from(e.dataTransfer.files);
            processImages(files);
          }, [isModelLoading, isProcessing]);

          const onFileChange = (e) => {
            if (e.target.files && e.target.files.length > 0) processImages(Array.from(e.target.files));
          };

          const toggleFace = (id) => {
            const next = new Set(selectedFaceIds);
            next.has(id) ? next.delete(id) : next.add(id);
            setSelectedFaceIds(next);
          };

          const toggleGroup = (group) => {
            const next = new Set(selectedFaceIds);
            const allSelected = group.faces.every(f => next.has(f.id));
            group.faces.forEach(f => allSelected ? next.delete(f.id) : next.add(f.id));
            setSelectedFaceIds(next);
          };

          const clearAll = () => {
            setFaceGroups([]); setExtractedFaces([]); setSelectedFaceIds(new Set()); setProgress(0);
          };

          const downloadSelected = async () => {
            if (selectedFaceIds.size === 0) return;
            setIsProcessing(true);
            setStatusText("Generating ZIP archive...");
            
            try {
              const zip = new window.JSZip();
              let totalExported = 0;

              faceGroups.forEach((group, gIndex) => {
                const hasName = group.name && group.name.trim() !== '';
                const folderName = hasName ? group.name.trim() : `Person_${gIndex + 1}`;
                const filePrefix = hasName ? group.name.replace(/[^a-z0-9]/gi, '').toLowerCase() : 'face';
                const selectedInGroup = group.faces.filter(f => selectedFaceIds.has(f.id));
                
                if (selectedInGroup.length > 0) {
                  const folder = zip.folder(folderName);
                  selectedInGroup.forEach((face, fIndex) => {
                    const base64Data = face.cropDataUrl.split(',')[1];
                    const fileName = hasName ? `${filePrefix}${fIndex + 1}.jpg` : `face_${fIndex + 1}.jpg`;
                    folder.file(fileName, base64Data, {base64: true});
                    totalExported++;
                  });
                }
              });

              const content = await zip.generateAsync({type: "blob"});
              const url = URL.createObjectURL(content);
              const a = document.createElement("a");
              a.href = url;
              a.download = `Extracted_Faces_${totalExported}.zip`;
              document.body.appendChild(a);
              a.click();
              document.body.removeChild(a);
              URL.revokeObjectURL(url);
            } catch (e) {
              console.error("ZIP Generation Failed", e);
              alert("Failed to generate ZIP file.");
            } finally {
              setIsProcessing(false);
              setStatusText("Ready");
            }
          };

          const downloadSingleFace = (e, face, group, fIndex) => {
            e.stopPropagation(); 
            const hasName = group.name && group.name.trim() !== '';
            const filePrefix = hasName ? group.name.replace(/[^a-z0-9]/gi, '').toLowerCase() : 'face';
            const fileName = hasName ? `${filePrefix}${fIndex + 1}.jpg` : `face_${fIndex + 1}.jpg`;

            const a = document.createElement("a");
            a.href = face.cropDataUrl;
            a.download = fileName;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
          };

          return (
            <div className="min-h-screen font-sans">
              <header className="sticky top-0 z-30 bg-neutral-950/80 backdrop-blur-md border-b border-neutral-800 px-6 py-4 flex items-center justify-between">
                <div className="flex items-center gap-4">
                  <LogoIcon size={42} className="shadow-lg drop-shadow-md" />
                  <div className="flex flex-col">
                    <h1 className="text-2xl font-extrabold tracking-[0.2em] text-white uppercase" style={{ fontFamily: "'Montserrat', sans-serif" }}>immager</h1>
                    <p className="text-[10px] sm:text-xs text-neutral-400 tracking-wider uppercase mt-0.5">crop faces easily in bulk</p>
                  </div>
                </div>
                <div className="flex items-center gap-4">
                  <button onClick={() => setShowSettings(true)} className="text-sm flex items-center gap-2 text-neutral-400 hover:text-white transition-colors">
                    <SettingsIcon size={16} /> Crop Settings
                  </button>
                  {faceGroups.length > 0 && (
                    <button onClick={clearAll} className="text-sm flex items-center gap-2 text-neutral-400 hover:text-white transition-colors">
                      <Trash2Icon size={16} /> Start Over
                    </button>
                  )}
                </div>
              </header>

              <main className="max-w-7xl mx-auto p-6 pb-32">
                {faceGroups.length === 0 && (
                  <div className="mt-12">
                    <div 
                      onDragOver={(e) => e.preventDefault()}
                      onDrop={onDrop}
                      onClick={() => !isModelLoading && !isProcessing && fileInputRef.current.click()}
                      className={`
                        relative w-full max-w-2xl mx-auto flex flex-col items-center justify-center p-16
                        border-2 border-dashed rounded-3xl transition-all duration-200
                        ${isModelLoading || isProcessing 
                          ? 'border-neutral-800 bg-neutral-900/30 cursor-not-allowed' 
                          : 'border-neutral-700 bg-neutral-900/50 hover:bg-neutral-800 hover:border-indigo-500 cursor-pointer'}
                      `}
                    >
                      <input type="file" multiple accept="image/*" ref={fileInputRef} onChange={onFileChange} className="hidden" />
                      
                      {(isModelLoading || isProcessing) ? (
                        <div className="flex flex-col items-center text-center">
                          <Loader2Icon size={48} className="text-indigo-500 animate-spin mb-6" />
                          <h3 className="text-xl font-medium text-white mb-2">{statusText}</h3>
                          {isProcessing && progress > 0 && (
                            <div className="w-full max-w-xs bg-neutral-800 rounded-full h-2 mt-4 overflow-hidden">
                              <div className="bg-indigo-500 h-2 rounded-full transition-all duration-300 ease-out" style={{ width: `${progress}%` }} />
                            </div>
                          )}
                        </div>
                      ) : (
                        <div className="flex flex-col items-center text-center">
                          <div className="bg-neutral-800 p-4 rounded-2xl mb-6 shadow-inner">
                            <UploadCloudIcon size={40} className="text-indigo-400" />
                          </div>
                          <h3 className="text-2xl font-medium text-white mb-3">Upload Images</h3>
                          <p className="text-neutral-400 max-w-sm mb-6">Drag and drop your photos here, or click to browse. We'll find and group the faces.</p>
                          <button className="bg-white text-black px-6 py-2.5 rounded-full font-medium hover:bg-neutral-200 transition-colors">Select Images</button>
                        </div>
                      )}
                    </div>

                    {!isModelLoading && !isProcessing && (
                      <div className="grid grid-cols-1 md:grid-cols-3 gap-6 max-w-4xl mx-auto mt-16 text-center">
                        <div className="bg-neutral-900/30 p-6 rounded-2xl border border-neutral-800/50">
                          <UsersIcon size={24} className="mx-auto text-indigo-400 mb-4" />
                          <h4 className="font-medium text-white mb-2">Smart Clustering</h4>
                          <p className="text-sm text-neutral-400">Groups faces of the same person together automatically.</p>
                        </div>
                        <div className="bg-neutral-900/30 p-6 rounded-2xl border border-neutral-800/50">
                          <ImageIcon size={24} className="mx-auto text-emerald-400 mb-4" />
                          <h4 className="font-medium text-white mb-2">Auto-Cropping</h4>
                          <p className="text-sm text-neutral-400">Extracts perfectly framed headshots ready for use.</p>
                        </div>
                        <div className="bg-neutral-900/30 p-6 rounded-2xl border border-neutral-800/50">
                          <CheckCircleIcon size={24} className="mx-auto text-amber-400 mb-4" />
                          <h4 className="font-medium text-white mb-2">100% Private</h4>
                          <p className="text-sm text-neutral-400">Everything runs entirely inside your browser. No server uploads.</p>
                        </div>
                      </div>
                    )}
                  </div>
                )}

                {faceGroups.length > 0 && (
                  <div className="animate-in">
                    {isDraggingFace && (
                      <div 
                        className={`fixed top-24 left-1/2 -translate-x-1/2 z-50 w-full max-w-sm border-2 border-dashed rounded-2xl flex flex-col items-center justify-center p-6 shadow-2xl backdrop-blur-md transition-all ${dragOverGroupId === 'new-group' ? 'border-indigo-400 bg-indigo-500/30 scale-105' : 'border-neutral-500 bg-neutral-900/80 scale-100'}`}
                        onDragOver={(e) => handleDragOver(e, 'new-group')}
                        onDragLeave={handleDragLeave}
                        onDrop={(e) => handleDrop(e, 'new-group')}
                      >
                        <UsersIcon size={32} className="text-white mb-2" />
                        <p className="text-white font-medium text-center">Drop here to create a new person</p>
                      </div>
                    )}

                    <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-8">
                      <div>
                        <h2 className="text-2xl font-semibold tracking-tight">Found {faceGroups.length} People</h2>
                        <div className="text-sm text-neutral-400 mt-1">Total unique faces: {faceGroups.reduce((acc, curr) => acc + curr.faces.length, 0)}</div>
                      </div>
                      <div className="flex flex-col items-end gap-1 bg-neutral-900/80 p-3 rounded-xl border border-neutral-800 shadow-inner">
                        <label className="text-sm text-neutral-300 font-medium flex justify-between w-full">
                          <span>Grouping Tolerance</span>
                          <span className="text-indigo-400 font-bold">{matchThreshold.toFixed(2)}</span>
                        </label>
                        <input type="range" min="0.30" max="0.70" step="0.01" value={matchThreshold} onChange={(e) => { const val = parseFloat(e.target.value); setMatchThreshold(val); clusterFaces(extractedFaces, val); }} className="w-48 md:w-64 accent-indigo-500 cursor-pointer" title="Lower = stricter matching. Higher = looser matching." />
                        <span className="text-xs text-neutral-500">Slide right to merge similar people</span>
                      </div>
                    </div>
                    
                    <div className="columns-1 md:columns-2 lg:columns-3 xl:columns-4 gap-6 space-y-6">
                      {faceGroups.map((group, groupIndex) => {
                        const allSelected = group.faces.every(f => selectedFaceIds.has(f.id));
                        const someSelected = !allSelected && group.faces.some(f => selectedFaceIds.has(f.id));

                        return (
                          <div key={group.id} className={`break-inside-avoid bg-neutral-900 rounded-2xl border transition-colors overflow-hidden group ${dragOverGroupId === group.id ? 'border-indigo-500 shadow-[0_0_15px_rgba(99,102,241,0.3)]' : 'border-neutral-800'}`} onDragOver={(e) => handleDragOver(e, group.id)} onDragLeave={handleDragLeave} onDrop={(e) => handleDrop(e, group.id)}>
                            <div className="px-5 py-4 flex items-center justify-between border-b border-neutral-800/50 bg-neutral-900/80">
                              <div className="flex items-center gap-3">
                                <div className="w-8 h-8 rounded-full overflow-hidden border border-neutral-700">
                                  <img src={group.faces[0].cropDataUrl} className="w-full h-full object-cover pointer-events-none" alt={`Person ${groupIndex + 1}`} />
                                </div>
                                <div className="flex flex-col">
                                  <input type="text" value={group.name} placeholder={`Person ${groupIndex + 1}`} onChange={(e) => updateGroupName(group.id, e.target.value)} className="text-sm font-medium bg-transparent border-b border-transparent hover:border-neutral-600 focus:border-indigo-500 outline-none text-white w-32 placeholder:text-neutral-400 transition-colors" />
                                  <div className="text-xs text-neutral-500">{group.faces.length} shots</div>
                                </div>
                              </div>
                              <button onClick={() => toggleGroup(group)} className="text-neutral-400 hover:text-white transition-colors p-1" title={allSelected ? "Deselect All" : "Select All"}>
                                {allSelected ? <CheckSquareIcon size={20} className="text-indigo-500" /> : someSelected ? <CheckSquareIcon size={20} className="text-indigo-500 opacity-50" /> : <SquareIcon size={20} />}
                              </button>
                            </div>
                            <div className="p-4 grid grid-cols-3 gap-3">
                              {group.faces.map((face, fIndex) => {
                                const isSelected = selectedFaceIds.has(face.id);
                                return (
                                  <div key={face.id} draggable={true} onDragStart={(e) => handleDragStart(e, face.id, group.id)} onDragEnd={handleDragEnd} onClick={() => toggleFace(face.id)} className={`relative aspect-square rounded-xl overflow-hidden cursor-grab active:cursor-grabbing group/item transition-all duration-200 border-2 ${isSelected ? 'border-indigo-500 scale-95' : 'border-transparent hover:border-neutral-600'}`}>
                                    <img src={face.cropDataUrl} alt="Crop" draggable="false" className="w-full h-full object-cover select-none pointer-events-none" loading="lazy" />
                                    <div className={`absolute top-2 right-2 opacity-0 group-hover/item:opacity-100 transition-opacity z-10 flex gap-1.5`}>
                                      <button onClick={(e) => downloadSingleFace(e, face, group, fIndex)} className="p-1.5 bg-neutral-900/80 hover:bg-emerald-500 rounded-lg text-white backdrop-blur border border-neutral-700 transition-colors shadow-sm" title="Download Image"><DownloadIcon size={14} /></button>
                                      <button onClick={(e) => { e.stopPropagation(); openEditor(face, groupIndex, fIndex); }} className="p-1.5 bg-neutral-900/80 hover:bg-indigo-500 rounded-lg text-white backdrop-blur border border-neutral-700 transition-colors shadow-sm" title="Manual Crop"><EditIcon size={14} /></button>
                                    </div>
                                    <div className={`absolute inset-0 pointer-events-none flex items-center justify-center transition-opacity ${isSelected ? 'bg-indigo-500/20 opacity-100' : 'bg-black/40 opacity-0 group-hover/item:opacity-100'}`}>
                                      <div className={`w-6 h-6 rounded-full border-2 flex items-center justify-center ${isSelected ? 'bg-indigo-500 border-indigo-500' : 'border-white/70'}`}>{isSelected && <CheckCircleIcon size={14} className="text-white" />}</div>
                                    </div>
                                  </div>
                                );
                              })}
                            </div>
                          </div>
                        );
                      })}
                    </div>
                  </div>
                )}
              </main>

              {faceGroups.length > 0 && (
                <div className={`fixed bottom-20 left-1/2 -translate-x-1/2 z-40 transition-all duration-300 ${selectedFaceIds.size > 0 ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0 pointer-events-none'}`}>
                  <div className="bg-neutral-900 border border-neutral-800 shadow-2xl rounded-full p-2 pl-6 pr-3 flex items-center gap-6">
                    <div className="font-medium text-sm"><span className="text-indigo-400 font-bold">{selectedFaceIds.size}</span> faces selected</div>
                    <button onClick={downloadSelected} disabled={isProcessing} className={`flex items-center gap-2 bg-indigo-600 text-white px-5 py-2.5 rounded-full text-sm font-medium hover:bg-indigo-500 transition-colors disabled:opacity-50 disabled:cursor-not-allowed`}>
                      {isProcessing ? <Loader2Icon size={18} className="animate-spin" /> : <DownloadIcon size={18} />}
                      {isProcessing ? 'Creating ZIP...' : 'Download'}
                    </button>
                  </div>
                </div>
              )}

              {showSettings && (
                <div className="fixed inset-0 bg-black/80 z-50 flex items-center justify-center p-4">
                  <div className="bg-neutral-900 border border-neutral-800 rounded-2xl p-6 w-full max-w-md shadow-2xl animate-in">
                    <div className="flex items-center justify-between mb-6">
                      <h3 className="text-lg font-semibold text-white">Smart Crop Settings</h3>
                      <button onClick={() => setShowSettings(false)} className="text-neutral-400 hover:text-white"><XIcon size={20}/></button>
                    </div>
                    <div className="space-y-4">
                      <div>
                        <label className="block text-sm text-neutral-400 mb-2">Padding Style</label>
                        <select disabled={isProcessing} value={cropSettings.padding} onChange={(e) => applyGlobalCropSettings({...cropSettings, padding: parseFloat(e.target.value)})} className="w-full bg-neutral-800 border border-neutral-700 rounded-lg p-2.5 text-white outline-none focus:border-indigo-500 disabled:opacity-50">
                          <option value="0.05">Very Tight (Exclude others)</option>
                          <option value="0.15">Tight</option>
                          <option value="0.3">Normal</option>
                          <option value="0.5">Wide</option>
                        </select>
                      </div>
                      <div>
                        <label className="block text-sm text-neutral-400 mb-2">Shape</label>
                        <select disabled={isProcessing} value={cropSettings.shape} onChange={(e) => applyGlobalCropSettings({...cropSettings, shape: e.target.value})} className="w-full bg-neutral-800 border border-neutral-700 rounded-lg p-2.5 text-white outline-none focus:border-indigo-500 disabled:opacity-50">
                          <option value="square">Square</option>
                          <option value="original">Original Aspect</option>
                        </select>
                      </div>
                      <p className="text-xs text-neutral-500 mt-4 leading-relaxed">Changes apply instantly to all currently extracted faces.</p>
                      <button onClick={() => setShowSettings(false)} disabled={isProcessing} className="w-full mt-6 bg-indigo-600 hover:bg-indigo-500 text-white py-2.5 rounded-lg font-medium transition-colors disabled:opacity-50">Done</button>
                    </div>
                  </div>
                </div>
              )}

              {editingFace && (
                <div className="fixed inset-0 bg-black/80 z-50 flex items-center justify-center p-4">
                  <div className="bg-neutral-900 border border-neutral-800 rounded-2xl p-6 w-full max-w-lg flex flex-col items-center shadow-2xl animate-in">
                    <div className="w-full flex items-center justify-between mb-4">
                      <h3 className="text-lg font-semibold text-white">Manual Crop Adjust (Drag to Pan)</h3>
                      <button onClick={() => setEditingFace(null)} className="text-neutral-400 hover:text-white"><XIcon size={20}/></button>
                    </div>
                    <div className={`relative w-64 h-64 bg-neutral-800 rounded-xl overflow-hidden mb-6 border border-neutral-700 flex items-center justify-center shadow-inner ${dragState.isDragging ? 'cursor-grabbing' : 'cursor-grab'}`} onMouseDown={(e) => { setDragState({ isDragging: true, startX: e.clientX, startY: e.clientY, initialOffsetX: editingFace.manualOffsets.x, initialOffsetY: editingFace.manualOffsets.y }); }} onMouseMove={(e) => { if (!dragState.isDragging || !editingFace) return; const dx = e.clientX - dragState.startX; const dy = e.clientY - dragState.startY; const scale = Math.max(1, editingFace.originalBox.width / 128) / editingFace.manualOffsets.zoom; updateManualCrop({ x: dragState.initialOffsetX - (dx * scale), y: dragState.initialOffsetY - (dy * scale) }); }} onMouseUp={() => setDragState(prev => ({ ...prev, isDragging: false }))} onMouseLeave={() => setDragState(prev => ({ ...prev, isDragging: false }))}>
                      <img src={editingFace.previewUrl} style={{ imageRendering: editingFace.manualOffsets.resolution !== 'auto' && parseInt(editingFace.manualOffsets.resolution) < 150 ? 'pixelated' : 'auto' }} className="max-w-full max-h-full object-contain pointer-events-none select-none" alt="Preview" draggable="false" />
                    </div>
                    <div className="w-full space-y-5">
                      <div>
                        <label className="flex justify-between text-sm text-neutral-400 mb-2">
                          <span>Export Resolution</span>
                          <span>{editingFace.manualOffsets.resolution === 'auto' ? 'Auto' : `${editingFace.manualOffsets.resolution}x${editingFace.manualOffsets.resolution}`}</span>
                        </label>
                        <select value={editingFace.manualOffsets.resolution} onChange={(e) => updateManualCrop({ resolution: e.target.value })} className="w-full bg-neutral-800 border border-neutral-700 rounded-lg p-2.5 text-white outline-none focus:border-indigo-500">
                          <option value="auto">Auto (Original Detected Size)</option>
                          <option value="56">56 x 56</option>
                          <option value="128">128 x 128</option>
                          <option value="256">256 x 256</option>
                          <option value="512">512 x 512</option>
                        </select>
                      </div>
                      <div>
                        <label className="flex justify-between text-sm text-neutral-400 mb-2">
                          <span>Zoom</span>
                          <span>{editingFace.manualOffsets.zoom.toFixed(1)}x</span>
                        </label>
                        <input type="range" min="0.5" max="2.5" step="0.1" value={editingFace.manualOffsets.zoom} onChange={(e) => updateManualCrop({ zoom: parseFloat(e.target.value) })} className="w-full accent-indigo-500" />
                      </div>
                    </div>
                    <div className="w-full flex gap-3 mt-8">
                      <button onClick={() => setEditingFace(null)} className="flex-1 py-2.5 rounded-lg border border-neutral-700 text-white hover:bg-neutral-800 transition-colors">Cancel</button>
                      <button onClick={saveManualCrop} className="flex-1 py-2.5 rounded-lg bg-indigo-600 hover:bg-indigo-500 text-white font-medium transition-colors shadow-lg">Apply Crop</button>
                    </div>
                  </div>
                </div>
              )}
            </div>
          );
        }

        const root = ReactDOM.createRoot(document.getElementById('root'));
        root.render(<FaceExtractApp />);
    </script>

    <!-- ========================================== -->
    <!-- LOGIC: APP 2 (VIDEOFLOW - MODULE)          -->
    <!-- ========================================== -->
    <script type="module">
        import { ObjectDetector, FilesetResolver } from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision/vision_bundle.mjs";

        const videoInput = document.getElementById('video-input');
        const dropZone = document.getElementById('drop-zone');
        const videoEl = document.getElementById('hidden-video');
        const previewCanvas = document.getElementById('preview-canvas');
        const resultsEl = document.getElementById('results');
        const progressBar = document.getElementById('progress-bar');
        const progressPercent = document.getElementById('progress-percent');
        const statusLabel = document.getElementById('status-label');
        const startBtn = document.getElementById('start-btn');
        const downloadBtn = document.getElementById('download-btn');
        const exportImmagerBtn = document.getElementById('export-immager-btn');
        const selectAllBtn = document.getElementById('select-all-btn');
        const modelBadge = document.getElementById('model-badge');
        const faceBadge = document.getElementById('face-badge');
        const settingsPanel = document.getElementById('settings-panel');
        const statsText = document.getElementById('stats-text');
        const scannerLine = document.getElementById('scanner');
        const emptyState = document.getElementById('empty-state');
        const extractAllToggle = document.getElementById('extract-all-toggle');
        const advancedFiltersContainer = document.getElementById('advanced-filters-container');
        const autoCropToggle = document.getElementById('auto-crop-toggle');
        const faceCropToggle = document.getElementById('face-crop-toggle');
        const requireFaceToggle = document.getElementById('require-face-toggle');
        
        const faceUploadBtn = document.getElementById('face-upload-btn');
        const faceInput = document.getElementById('face-input');
        const faceStatusText = document.getElementById('face-status-text');
        const targetFacesContainer = document.getElementById('target-faces-container');

        let detector;
        let extractedFrames = [];
        let isProcessing = false;
        window.targetFaces = [];
        let isFaceApiLoaded = false;
        
        // Setup frame selection memory globally for Vanilla JS
        window.selectedFrames = new Set();

        document.getElementById('scan-rate').oninput = (e) => document.getElementById('interval-val').innerText = e.target.value + 's';
        document.getElementById('confidence').oninput = (e) => document.getElementById('conf-val').innerText = Math.round(e.target.value * 100) + '%';

        // Grey out options logic
        extractAllToggle.addEventListener('change', (e) => {
            if(e.target.checked) {
                advancedFiltersContainer.style.opacity = '0.3';
                advancedFiltersContainer.style.pointerEvents = 'none';
            } else {
                advancedFiltersContainer.style.opacity = '1';
                advancedFiltersContainer.style.pointerEvents = 'auto';
            }
        });

        async function initMediaPipe() {
            try {
                const vision = await FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm");
                detector = await ObjectDetector.createFromOptions(vision, {
                    baseOptions: {
                        modelAssetPath: `https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite0/float16/1/efficientdet_lite0.tflite`,
                        delegate: "GPU"
                    },
                    scoreThreshold: 0.5,
                    runningMode: "IMAGE"
                });
                modelBadge.innerHTML = '<span class="status-dot bg-emerald-500"></span> MediaPipe Ready';
                modelBadge.classList.replace('text-slate-400', 'text-emerald-400');
            } catch (err) {
                console.error("MediaPipe Error:", err);
                modelBadge.innerHTML = '<span class="status-dot bg-red-500"></span> Engine Error';
            }
        }

        async function initFaceAPI() {
            faceBadge.classList.remove('hidden');
            try {
                const MODEL_URL = 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api@1.7.12/model/';
                await Promise.all([
                    window.faceapi.nets.ssdMobilenetv1.loadFromUri(MODEL_URL),
                    window.faceapi.nets.faceLandmark68Net.loadFromUri(MODEL_URL),
                    window.faceapi.nets.faceRecognitionNet.loadFromUri(MODEL_URL)
                ]);
                isFaceApiLoaded = true;
                faceBadge.innerHTML = '<span class="status-dot bg-purple-500"></span> FaceAPI Ready';
                faceBadge.classList.replace('text-slate-400', 'text-purple-400');
            } catch(err) {
                console.error("FaceAPI Error:", err);
                faceBadge.innerHTML = '<span class="status-dot bg-red-500"></span> FaceAPI Error';
            }
        }

        initMediaPipe();
        setTimeout(initFaceAPI, 500);

        window.removeTargetFace = function(index) {
            window.targetFaces.splice(index, 1);
            renderTargetFaces();
        };

        function renderTargetFaces() {
            targetFacesContainer.innerHTML = '';
            window.targetFaces.forEach((face, index) => {
                const wrapper = document.createElement('div');
                wrapper.className = 'relative inline-block';
                wrapper.innerHTML = `
                    <img src="${face.preview}" class="w-16 h-16 object-cover rounded-full border-2 border-indigo-500 shadow-[0_0_10px_rgba(99,102,241,0.5)]">
                    <button class="absolute -top-1 -right-1 bg-red-500 text-white rounded-full w-5 h-5 flex items-center justify-center text-xs hover:bg-red-600 shadow transition-colors" onclick="window.removeTargetFace(${index})">
                        <i class="fas fa-times"></i>
                    </button>
                `;
                targetFacesContainer.appendChild(wrapper);
            });
            
            if(window.targetFaces.length > 0) {
                faceUploadBtn.innerHTML = '<i class="fas fa-plus mr-1"></i> Add More Faces';
                faceStatusText.classList.remove('hidden');
                faceStatusText.innerHTML = `<i class="fas fa-check text-emerald-400"></i> ${window.targetFaces.length} Face(s) Locked`;
                faceStatusText.className = "text-[10px] text-emerald-400 mt-2 text-center font-bold";
            } else {
                faceUploadBtn.innerHTML = '<i class="fas fa-camera mr-1"></i> Upload Target Face';
                faceStatusText.classList.add('hidden');
            }
        }

        faceUploadBtn.onclick = () => faceInput.click();
        faceInput.onchange = async (e) => {
            const files = Array.from(e.target.files);
            if (files.length === 0) return;

            if (!isFaceApiLoaded) {
                alert("Please wait for FaceAPI to finish loading...");
                return;
            }

            faceStatusText.classList.remove('hidden');
            faceStatusText.innerHTML = '<i class="fas fa-spinner animate-spin"></i> Analyzing face(s)...';
            faceStatusText.className = "text-[10px] text-amber-400 mt-2 text-center";

            for (let file of files) {
                try {
                    const url = URL.createObjectURL(file);
                    const img = new Image();
                    await new Promise(r => { img.onload = r; img.src = url; });
                    
                    const options = new window.faceapi.SsdMobilenetv1Options({ minConfidence: 0.2 });
                    const detections = await window.faceapi.detectAllFaces(img, options).withFaceLandmarks().withFaceDescriptors();
                    
                    if (detections.length > 0) {
                        detections.forEach(det => {
                            const box = det.detection.box;
                            const canvas = document.createElement('canvas');
                            const pad = Math.max(box.width, box.height) * 0.3;
                            const size = Math.max(box.width, box.height) + pad * 2;
                            canvas.width = size;
                            canvas.height = size;
                            const ctx = canvas.getContext('2d');
                            const sx = box.x - pad;
                            const sy = box.y - pad;
                            ctx.drawImage(img, sx, sy, size, size, 0, 0, size, size);
                            
                            window.targetFaces.push({
                                descriptor: det.descriptor,
                                preview: canvas.toDataURL('image/jpeg', 0.8)
                            });
                        });
                    }
                } catch (err) {
                    console.error("Detection error:", err);
                }
            }

            if (window.targetFaces.length === 0) {
                faceStatusText.innerHTML = '<i class="fas fa-times text-red-500"></i> No faces detected.';
                faceStatusText.className = "text-[10px] text-red-500 mt-2 text-center";
            } else {
                renderTargetFaces();
            }
            faceInput.value = ''; // Reset input to allow re-uploading the same file if needed
        };

        dropZone.onclick = () => videoInput.click();
        videoInput.onchange = (e) => {
            const file = e.target.files[0];
            if (file) {
                settingsPanel.style.opacity = "1";
                settingsPanel.style.pointerEvents = "all";
                dropZone.querySelector('h3').innerText = file.name;
                statsText.innerText = "Video loaded. Ready to scan.";
            }
        };

        startBtn.onclick = async () => {
            if (isProcessing) {
                isProcessing = false;
                return;
            }

            const file = videoInput.files[0];
            if (!file || !detector) return;

            isProcessing = true;
            extractedFrames = [];
            window.selectedFrames.clear(); // Reset selections
            updateActionButtons();
            
            resultsEl.innerHTML = '';
            
            if (emptyState) emptyState.classList.add('hidden');
            document.getElementById('progress-container').classList.remove('hidden');
            if (scannerLine) scannerLine.style.display = "block";
            
            startBtn.innerHTML = '<i class="fas fa-stop"></i> Stop Analysis';
            startBtn.classList.replace('bg-indigo-600', 'bg-red-600');

            const url = URL.createObjectURL(file);
            videoEl.src = url;

            videoEl.onloadedmetadata = async () => {
                const ctx = previewCanvas.getContext('2d');
                previewCanvas.width = videoEl.videoWidth;
                previewCanvas.height = videoEl.videoHeight;
                
                const duration = videoEl.duration;
                const step = parseFloat(document.getElementById('scan-rate').value);
                const confidence = parseFloat(document.getElementById('confidence').value);
                const extractAll = extractAllToggle.checked;
                const doAutoCrop = autoCropToggle.checked;
                const doFaceCrop = faceCropToggle.checked;
                const requireFace = requireFaceToggle.checked;
                const matchFace = window.targetFaces.length > 0;
                
                let lastTime = performance.now();

                for (let time = 0; time < duration; time += step) {
                    if (!isProcessing) break;

                    videoEl.currentTime = time;
                    await new Promise(r => videoEl.onseeked = r);

                    ctx.drawImage(videoEl, 0, 0);
                    
                    if (extractAll) {
                        // Skip detection entirely and just capture the full frame
                        const fullFrameData = previewCanvas.toDataURL('image/jpeg', 0.9);
                        extractedFrames.push({ data: fullFrameData, time: time, type: 'Full' });
                        addFrameToUI(fullFrameData, time, 'Full', extractedFrames.length - 1);
                    } else {
                        detector.setOptions({ scoreThreshold: confidence });
                        const results = detector.detect(previewCanvas);
                        let people = results.detections.filter(d => 
                            d.categories.some(c => c.categoryName === 'person')
                        );

                        let validEntities = [];

                        if (people.length > 0) {
                            if (matchFace || requireFace || doFaceCrop) {
                                const faces = await window.faceapi.detectAllFaces(previewCanvas, new window.faceapi.SsdMobilenetv1Options({minConfidence: 0.3})).withFaceLandmarks().withFaceDescriptors();
                                
                                if (matchFace) {
                                    const matchingFaces = faces.filter(f => window.targetFaces.some(tf => window.faceapi.euclideanDistance(tf.descriptor, f.descriptor) < 0.55));
                                    matchingFaces.forEach(f => {
                                        let linkedPerson = people.find(p => isFaceInBody(f.detection.box, p.boundingBox));
                                        validEntities.push({ personBox: linkedPerson ? linkedPerson.boundingBox : null, faceBox: f.detection.box, isMatch: true });
                                    });
                                } else if (requireFace) {
                                    faces.forEach(f => {
                                        let linkedPerson = people.find(p => isFaceInBody(f.detection.box, p.boundingBox));
                                        validEntities.push({ personBox: linkedPerson ? linkedPerson.boundingBox : null, faceBox: f.detection.box, isMatch: false });
                                    });
                                } else {
                                    people.forEach(p => {
                                        let linkedFace = faces.find(f => isFaceInBody(f.detection.box, p.boundingBox));
                                        validEntities.push({ personBox: p.boundingBox, faceBox: linkedFace ? linkedFace.detection.box : null, isMatch: false });
                                    });
                                }
                            } else {
                                people.forEach(p => validEntities.push({ personBox: p.boundingBox, faceBox: null, isMatch: false }));
                            }
                        }

                        if (validEntities.length > 0) {
                            if (!doAutoCrop && !doFaceCrop) {
                                const fullFrameData = previewCanvas.toDataURL('image/jpeg', 0.9);
                                extractedFrames.push({ data: fullFrameData, time: time, type: 'Full' });
                                addFrameToUI(fullFrameData, time, 'Full', extractedFrames.length - 1);
                            } else {
                                validEntities.forEach(entity => {
                                    if (doAutoCrop && entity.personBox) {
                                        const box = entity.personBox;
                                        const padX = box.width * 0.15;
                                        const padY = box.height * 0.15;
                                        const cX = Math.max(0, box.originX - padX);
                                        const cY = Math.max(0, box.originY - padY);
                                        const cW = Math.min(previewCanvas.width - cX, box.width + padX * 2);
                                        const cH = Math.min(previewCanvas.height - cY, box.height + padY * 2);

                                        const cropCanvas = document.createElement('canvas');
                                        cropCanvas.width = cW;
                                        cropCanvas.height = cH;
                                        cropCanvas.getContext('2d').drawImage(previewCanvas, cX, cY, cW, cH, 0, 0, cW, cH);
                                        
                                        const frameData = cropCanvas.toDataURL('image/jpeg', 0.9);
                                        extractedFrames.push({ data: frameData, time: time, type: 'Body' });
                                        addFrameToUI(frameData, time, 'Body', extractedFrames.length - 1);
                                    }
                                    
                                    if (doFaceCrop && entity.faceBox) {
                                        const fBox = entity.faceBox;
                                        const size = Math.max(fBox.width, fBox.height) * 2.0;
                                        const centerX = fBox.x + fBox.width / 2;
                                        const centerY = fBox.y + fBox.height / 2;
                                        
                                        const sX = Math.max(0, centerX - size / 2);
                                        const sY = Math.max(0, centerY - size / 2);
                                        const sW = Math.min(previewCanvas.width - sX, size);
                                        const sH = Math.min(previewCanvas.height - sY, size);

                                        const faceCanvas = document.createElement('canvas');
                                        faceCanvas.width = 512;
                                        faceCanvas.height = 512;
                                        const fCtx = faceCanvas.getContext('2d');
                                        fCtx.fillStyle = '#000000';
                                        fCtx.fillRect(0, 0, 512, 512);
                                        
                                        const scale = 512 / size;
                                        const dX = (sX - (centerX - size/2)) * scale;
                                        const dY = (sY - (centerY - size/2)) * scale;
                                        const dW = sW * scale;
                                        const dH = sH * scale;

                                        fCtx.drawImage(previewCanvas, sX, sY, sW, sH, dX, dY, dW, dH);
                                        const faceData = faceCanvas.toDataURL('image/jpeg', 0.95);
                                        extractedFrames.push({ data: faceData, time: time, type: 'Face' });
                                        addFrameToUI(faceData, time, 'Face', extractedFrames.length - 1);
                                    }
                                });
                            }

                            validEntities.forEach(entity => {
                                if (entity.personBox) {
                                    ctx.strokeStyle = entity.isMatch ? '#c084fc' : '#818cf8';
                                    ctx.lineWidth = 4;
                                    ctx.strokeRect(entity.personBox.originX, entity.personBox.originY, entity.personBox.width, entity.personBox.height);
                                    if (entity.isMatch) {
                                        ctx.fillStyle = '#c084fc';
                                        ctx.font = '20px Arial';
                                        ctx.fillText("TARGET MATCH", entity.personBox.originX, entity.personBox.originY - 10);
                                    }
                                }
                                if (entity.faceBox) {
                                    ctx.strokeStyle = '#34d399'; 
                                    ctx.lineWidth = 2;
                                    ctx.strokeRect(entity.faceBox.x, entity.faceBox.y, entity.faceBox.width, entity.faceBox.height);
                                }
                            });
                        }
                    } // <-- End of else statement wrapping the detection logic

                    const pct = Math.min(100, Math.round((time / duration) * 100));
                    progressBar.style.width = `${pct}%`;
                    progressPercent.innerText = `${pct}%`;
                    statsText.innerText = `Extracted ${extractedFrames.length} specific instances.`;

                    const now = performance.now();
                    const fps = Math.round(1000 / (now - lastTime));
                    document.getElementById('fps-counter').innerText = `${fps} SEEK/S`;
                    lastTime = now;
                }
                
                cleanup();
            };
        };
        
        function isFaceInBody(faceBox, bodyBox) {
            if (!bodyBox || !faceBox) return false;
            const fCenterX = faceBox.x + faceBox.width / 2;
            const fCenterY = faceBox.y + faceBox.height / 2;
            return fCenterX >= bodyBox.originX && fCenterX <= bodyBox.originX + bodyBox.width &&
                   fCenterY >= bodyBox.originY && fCenterY <= bodyBox.originY + bodyBox.height;
        }

        function addFrameToUI(src, time, type, index) {
            const wrapper = document.createElement('div');
            const sizeClasses = type === 'Face' ? "w-32 h-32" : (type === 'Body' ? "h-40 w-auto min-w-[100px]" : "w-64 h-auto aspect-video");
            const badgeColor = type === 'Face' ? 'bg-emerald-600' : (type === 'Body' ? 'bg-indigo-600' : 'bg-slate-600');
            
            wrapper.className = `frame-wrapper group relative bg-slate-900 rounded-xl overflow-hidden border border-white/5 hover:border-indigo-500/50 transition-all shadow-xl flex-shrink-0 cursor-pointer ${sizeClasses}`;
            wrapper.id = `frame-wrapper-${index}`;
            wrapper.onclick = () => window.toggleFrameSelection(index);

            wrapper.innerHTML = `
                <img src="${src}" class="w-full h-full object-contain bg-black/50 pointer-events-none select-none">
                
                <!-- Overlay Checkmark for Selections -->
                <div class="selection-overlay absolute inset-0 bg-indigo-500/20 opacity-0 transition-opacity flex items-center justify-center pointer-events-none">
                    <div class="bg-indigo-500 rounded-full w-8 h-8 flex items-center justify-center text-white shadow-lg">
                        <i class="fas fa-check"></i>
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="absolute top-2 left-2 flex gap-1 z-10 opacity-0 group-hover:opacity-100 transition-opacity">
                    <!-- Zoom/Preview Button -->
                    <button class="bg-black/70 hover:bg-indigo-600 text-white p-2 rounded-lg text-xs transition-colors shadow" onclick="event.stopPropagation(); window.showPreview('${src}')" title="Preview Frame">
                        <i class="fas fa-search-plus"></i>
                    </button>
                    <!-- Download Button -->
                    <button class="bg-black/70 hover:bg-emerald-600 text-white p-2 rounded-lg text-xs transition-colors shadow" onclick="event.stopPropagation(); window.downloadSingleFrame('${src}', ${time}, '${type}', ${index})" title="Download Frame">
                        <i class="fas fa-download"></i>
                    </button>
                </div>

                <!-- Status Tags -->
                <div class="absolute bottom-2 left-2 bg-black/60 px-2 py-0.5 rounded text-[9px] font-mono text-indigo-300 pointer-events-none">
                    T+ ${time.toFixed(1)}s
                </div>
                <div class="absolute top-2 right-2 ${badgeColor} px-1.5 py-0.5 rounded text-[8px] font-bold text-white uppercase shadow pointer-events-none">
                    ${type}
                </div>
            `;
            
            resultsEl.appendChild(wrapper);
            const container = document.getElementById('results-container');
            if (container) container.scrollTop = container.scrollHeight;
        }

        // --- GLOBAL UI HELPERS FOR VIDEOFLOW SELECTIONS & PREVIEWS ---
        
        window.toggleFrameSelection = function(index) {
            if (window.selectedFrames.has(index)) {
                window.selectedFrames.delete(index);
            } else {
                window.selectedFrames.add(index);
            }
            
            const frameDiv = document.getElementById(`frame-wrapper-${index}`);
            const overlay = frameDiv.querySelector('.selection-overlay');
            if (window.selectedFrames.has(index)) {
                overlay.classList.remove('opacity-0');
                overlay.classList.add('opacity-100');
                frameDiv.classList.add('ring-2', 'ring-indigo-500');
            } else {
                overlay.classList.add('opacity-0');
                overlay.classList.remove('opacity-100');
                frameDiv.classList.remove('ring-2', 'ring-indigo-500');
            }
            
            updateActionButtons();
        };

        window.showPreview = function(src) {
            document.getElementById('preview-modal-img').src = src;
            document.getElementById('video-preview-modal').classList.remove('hidden');
        };

        window.downloadSingleFrame = function(src, time, type, index) {
            const a = document.createElement('a');
            a.href = src;
            a.download = `frame_${time.toFixed(2)}s_${type}_${index}.jpg`;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        };

        document.getElementById('close-preview').onclick = () => document.getElementById('video-preview-modal').classList.add('hidden');
        document.getElementById('video-preview-modal').onclick = (e) => {
            if(e.target === document.getElementById('video-preview-modal')) {
                 document.getElementById('video-preview-modal').classList.add('hidden');
            }
        };

        function updateActionButtons() {
            const count = window.selectedFrames.size;
            const total = extractedFrames.length;
            
            if (count > 0) {
                downloadBtn.innerHTML = `<i class="fas fa-file-export"></i> Download ${count} Selected`;
                exportImmagerBtn.innerHTML = `<i class="fas fa-magic"></i> Export ${count} to Immager`;
                selectAllBtn.innerHTML = "Deselect All";
            } else {
                downloadBtn.innerHTML = `<i class="fas fa-file-export"></i> Download All`;
                exportImmagerBtn.innerHTML = `<i class="fas fa-magic"></i> Export All to Immager`;
                selectAllBtn.innerHTML = "Select All";
            }
            
            if (total > 0) {
                downloadBtn.classList.remove('hidden');
                exportImmagerBtn.classList.remove('hidden');
                selectAllBtn.classList.remove('hidden');
            } else {
                downloadBtn.classList.add('hidden');
                exportImmagerBtn.classList.add('hidden');
                selectAllBtn.classList.add('hidden');
            }
        }

        selectAllBtn.onclick = () => {
            if (window.selectedFrames.size > 0) {
                window.selectedFrames.clear();
            } else {
                extractedFrames.forEach((_, i) => window.selectedFrames.add(i));
            }
            
            extractedFrames.forEach((_, i) => {
               const frameDiv = document.getElementById(`frame-wrapper-${i}`);
               if(!frameDiv) return;
               const overlay = frameDiv.querySelector('.selection-overlay');
               if (window.selectedFrames.has(i)) {
                   overlay.classList.remove('opacity-0');
                   overlay.classList.add('opacity-100');
                   frameDiv.classList.add('ring-2', 'ring-indigo-500');
               } else {
                   overlay.classList.add('opacity-0');
                   overlay.classList.remove('opacity-100');
                   frameDiv.classList.remove('ring-2', 'ring-indigo-500');
               }
            });
            updateActionButtons();
        };

        function dataURLtoFile(dataurl, filename) {
            let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
            while(n--){
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new File([u8arr], filename, {type:mime});
        }

        exportImmagerBtn.onclick = () => {
            const framesToExport = window.selectedFrames.size > 0 
                ? Array.from(window.selectedFrames).map(i => extractedFrames[i])
                : extractedFrames;
                
            if (framesToExport.length === 0) return;
            
            const originalText = exportImmagerBtn.innerHTML;
            exportImmagerBtn.innerHTML = '<i class="fas fa-spinner animate-spin"></i> Transferring...';
            exportImmagerBtn.disabled = true;
            
            setTimeout(() => {
                const files = framesToExport.map((f, i) => {
                    const filename = `videoflow_${f.time.toFixed(2)}s_${f.type}_${i}.jpg`;
                    return dataURLtoFile(f.data, filename);
                });
                
                // Dispatch event to React Immager
                window.dispatchEvent(new CustomEvent('SEND_TO_IMMAGER', { detail: files }));
                
                // Navigate to Immager View automatically
                switchApp('immager');
                
                exportImmagerBtn.innerHTML = originalText;
                exportImmagerBtn.disabled = false;
            }, 100);
        };

        function cleanup() {
            isProcessing = false;
            startBtn.innerHTML = '<i class="fas fa-microchip"></i> Start AI Extraction';
            startBtn.classList.replace('bg-red-600', 'bg-indigo-600');
            if (scannerLine) scannerLine.style.display = "none";
            statusLabel.innerText = "Process Complete";
            
            window.selectedFrames.clear(); // Reset Selection
            
            if(extractedFrames.length > 0) {
                updateActionButtons();
            } else {
                if (emptyState) emptyState.classList.remove('hidden');
                statsText.innerText = "Scan complete. No matching frames found.";
                updateActionButtons();
            }
        }

        downloadBtn.onclick = async () => {
            const framesToExport = window.selectedFrames.size > 0 
                ? Array.from(window.selectedFrames).map(i => extractedFrames[i])
                : extractedFrames;
                
            if (framesToExport.length === 0) return;

            const originalText = downloadBtn.innerHTML;
            downloadBtn.innerHTML = '<i class="fas fa-spinner animate-spin"></i> Zipping...';
            downloadBtn.disabled = true;

            const zip = new window.JSZip();
            framesToExport.forEach((f, index) => {
                const base64Data = f.data.replace(/^data:image\/(png|jpg|jpeg);base64,/, "");
                zip.file(`frame_${f.time.toFixed(2)}s_${f.type}_${index}.jpg`, base64Data, {base64: true});
            });
            
            try {
                const content = await zip.generateAsync({type: "blob"});
                const url = URL.createObjectURL(content);
                const a = document.createElement('a');
                a.href = url;
                a.download = `HumanFrames_Extracted.zip`;
                a.click();
            } catch (err) {
                console.error("Zipping failed", err);
            } finally {
                downloadBtn.innerHTML = originalText;
                downloadBtn.disabled = false;
            }
        };
    </script>

    <!-- ========================================== -->
    <!-- LOGIC: APP 3 (URL FETCHER)                 -->
    <!-- ========================================== -->
    <script>
        (function() {
            const fetchBtn = document.getElementById('url-fetch-btn');
            const textArea = document.getElementById('url-input-text');
            const fileInput = document.getElementById('url-file-input');
            const dropZone = document.getElementById('url-drop-zone');
            const fileNameDisplay = document.getElementById('url-file-name');
            const resultsContainer = document.getElementById('url-results');
            const emptyState = document.getElementById('url-empty-state');
            const statsText = document.getElementById('url-stats-text');
            
            const progressContainer = document.getElementById('url-progress-container');
            const progressBar = document.getElementById('url-progress-bar');
            const progressPercent = document.getElementById('url-progress-percent');
            
            const selectAllBtn = document.getElementById('url-select-all-btn');
            const downloadBtn = document.getElementById('url-download-btn');
            
            const previewModal = document.getElementById('url-preview-modal');
            const previewImg = document.getElementById('url-preview-modal-img');
            const closePreviewBtn = document.getElementById('url-close-preview');

            let fetchedImages = []; // { id, url, blobUrl, isSelected, status }
            let isFetching = false;

            // Handle File Upload
            dropZone.onclick = () => fileInput.click();
            fileInput.onchange = (e) => {
                const file = e.target.files[0];
                if (file) {
                    fileNameDisplay.innerText = file.name;
                    const reader = new FileReader();
                    reader.onload = (event) => {
                        const content = event.target.result;
                        const current = textArea.value.trim();
                        textArea.value = current ? current + '\n' + content : content;
                    };
                    reader.readAsText(file);
                }
            };

            // Global UI selection logic
            window.toggleUrlSelection = function(index) {
                const item = fetchedImages[index];
                if(!item || item.status !== 'success') return;
                
                item.isSelected = !item.isSelected;
                const card = document.getElementById(`url-card-${index}`);
                const overlay = card.querySelector('.url-selection-overlay');
                
                if (item.isSelected) {
                    overlay.classList.remove('opacity-0');
                    card.classList.add('ring-2', 'ring-teal-500');
                } else {
                    overlay.classList.add('opacity-0');
                    card.classList.remove('ring-2', 'ring-teal-500');
                }
                updateUrlActionButtons();
            };

            window.showUrlPreview = function(url) {
                previewImg.src = url;
                previewModal.classList.remove('hidden');
            };

            window.downloadSingleUrlImage = function(blobUrl, originalUrl) {
                const a = document.createElement('a');
                a.href = blobUrl;
                let filename = "image.jpg";
                try {
                    const parsed = new URL(originalUrl);
                    const parts = parsed.pathname.split('/');
                    const lastPart = parts[parts.length - 1];
                    if(lastPart && lastPart.includes('.')) filename = lastPart;
                } catch(e) {}
                
                a.download = filename;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            };

            closePreviewBtn.onclick = () => previewModal.classList.add('hidden');
            previewModal.onclick = (e) => {
                if(e.target === previewModal) previewModal.classList.add('hidden');
            };

            function updateUrlActionButtons() {
                const successful = fetchedImages.filter(img => img.status === 'success');
                const selected = successful.filter(img => img.isSelected);
                
                if (successful.length > 0) {
                    selectAllBtn.classList.remove('hidden');
                    downloadBtn.classList.remove('hidden');
                    
                    if (selected.length > 0) {
                        downloadBtn.innerHTML = `<i class="fas fa-file-archive"></i> ZIP ${selected.length} Selected`;
                        selectAllBtn.innerText = "Deselect All";
                    } else {
                        downloadBtn.innerHTML = `<i class="fas fa-file-archive"></i> ZIP All (${successful.length})`;
                        selectAllBtn.innerText = "Select All";
                    }
                } else {
                    selectAllBtn.classList.add('hidden');
                    downloadBtn.classList.add('hidden');
                }
            }

            selectAllBtn.onclick = () => {
                const successful = fetchedImages.filter(img => img.status === 'success');
                const anyUnselected = successful.some(img => !img.isSelected);
                
                fetchedImages.forEach((img, idx) => {
                    if (img.status === 'success') {
                        img.isSelected = anyUnselected;
                        const card = document.getElementById(`url-card-${idx}`);
                        const overlay = card.querySelector('.url-selection-overlay');
                        if(card && overlay) {
                            if (img.isSelected) {
                                overlay.classList.remove('opacity-0');
                                card.classList.add('ring-2', 'ring-teal-500');
                            } else {
                                overlay.classList.add('opacity-0');
                                card.classList.remove('ring-2', 'ring-teal-500');
                            }
                        }
                    }
                });
                updateUrlActionButtons();
            };

            function renderUrlImage(item, index) {
                const wrapper = document.createElement('div');
                wrapper.id = `url-card-${index}`;
                wrapper.className = `group relative bg-slate-900 rounded-xl overflow-hidden border border-white/5 hover:border-teal-500/50 transition-all shadow-xl w-40 h-40 flex-shrink-0 cursor-pointer ${item.isSelected ? 'ring-2 ring-teal-500' : ''}`;
                wrapper.onclick = () => window.toggleUrlSelection(index);

                if (item.status === 'success') {
                    wrapper.innerHTML = `
                        <img src="${item.blobUrl}" class="w-full h-full object-cover pointer-events-none select-none">
                        
                        <!-- Overlay Checkmark -->
                        <div class="url-selection-overlay absolute inset-0 bg-teal-500/20 ${item.isSelected ? '' : 'opacity-0'} transition-opacity flex items-center justify-center pointer-events-none">
                            <div class="bg-teal-500 rounded-full w-8 h-8 flex items-center justify-center text-white shadow-lg">
                                <i class="fas fa-check"></i>
                            </div>
                        </div>

                        <!-- Action Buttons -->
                        <div class="absolute top-2 left-2 flex gap-1 z-10 opacity-0 group-hover:opacity-100 transition-opacity">
                            <button class="bg-black/70 hover:bg-teal-600 text-white p-2 rounded-lg text-xs transition-colors shadow" onclick="event.stopPropagation(); window.showUrlPreview('${item.blobUrl}')" title="Preview">
                                <i class="fas fa-search-plus"></i>
                            </button>
                            <button class="bg-black/70 hover:bg-emerald-600 text-white p-2 rounded-lg text-xs transition-colors shadow" onclick="event.stopPropagation(); window.downloadSingleUrlImage('${item.blobUrl}', '${item.url}')" title="Download">
                                <i class="fas fa-download"></i>
                            </button>
                        </div>
                    `;
                } else {
                    wrapper.className = `bg-slate-800 rounded-xl overflow-hidden border border-red-500/20 w-40 h-40 flex flex-col items-center justify-center text-red-400 p-2`;
                    wrapper.innerHTML = `
                        <i class="fas fa-exclamation-circle text-2xl mb-2"></i>
                        <span class="text-[10px] text-center break-all text-slate-400 overflow-hidden line-clamp-2">${item.url}</span>
                        <span class="text-xs font-bold mt-1 text-center">Failed to load</span>
                    `;
                }
                resultsContainer.appendChild(wrapper);
            }

            fetchBtn.onclick = async () => {
                if (isFetching) return;
                
                const text = textArea.value;
                const regex = /(https?:\/\/[^\s]+)/g;
                let urls = text.match(regex) || [];
                urls = [...new Set(urls)]; // Remove duplicates

                if (urls.length === 0) {
                    alert("No valid URLs found. Please enter valid http/https links.");
                    return;
                }

                isFetching = true;
                fetchBtn.innerHTML = '<i class="fas fa-spinner animate-spin"></i> Fetching...';
                
                fetchedImages = [];
                resultsContainer.innerHTML = '';
                emptyState.classList.add('hidden');
                progressContainer.classList.remove('hidden');
                
                let successCount = 0;
                let failCount = 0;

                for (let i = 0; i < urls.length; i++) {
                    const currentUrl = urls[i];
                    
                    progressBar.style.width = `${((i) / urls.length) * 100}%`;
                    progressPercent.innerText = `${i}/${urls.length}`;
                    
                    const imageObj = { id: i, url: currentUrl, blobUrl: null, isSelected: false, status: 'pending' };
                    
                    try {
                        // Fetch the image as a blob to allow direct ZIP download later
                        const response = await fetch(currentUrl, { mode: 'cors' });
                        if (!response.ok) throw new Error("Network response was not ok");
                        const blob = await response.blob();
                        const blobUrl = URL.createObjectURL(blob);
                        
                        if (!blob.type.startsWith('image/')) throw new Error("Not an image");

                        imageObj.blobUrl = blobUrl;
                        imageObj.status = 'success';
                        successCount++;
                    } catch (e) {
                        imageObj.status = 'error';
                        failCount++;
                    }

                    fetchedImages.push(imageObj);
                    renderUrlImage(imageObj, i);
                }

                progressBar.style.width = '100%';
                progressPercent.innerText = `${urls.length}/${urls.length}`;
                setTimeout(() => progressContainer.classList.add('hidden'), 1000);
                
                statsText.innerText = `Fetched ${successCount} images (${failCount} failed).`;
                isFetching = false;
                fetchBtn.innerHTML = '<i class="fas fa-cloud-download-alt"></i> Fetch Images';
                
                updateUrlActionButtons();
            };

            downloadBtn.onclick = async () => {
                const successful = fetchedImages.filter(img => img.status === 'success');
                let toZip = successful.filter(img => img.isSelected);
                if (toZip.length === 0) toZip = successful;

                if (toZip.length === 0) return;

                const originalText = downloadBtn.innerHTML;
                downloadBtn.innerHTML = '<i class="fas fa-spinner animate-spin"></i> Zipping...';
                downloadBtn.disabled = true;

                try {
                    const zip = new window.JSZip();
                    
                    for (let i = 0; i < toZip.length; i++) {
                        const img = toZip[i];
                        const response = await fetch(img.blobUrl);
                        const blob = await response.blob();
                        
                        let ext = "jpg";
                        if(blob.type === "image/png") ext = "png";
                        else if(blob.type === "image/gif") ext = "gif";
                        else if(blob.type === "image/webp") ext = "webp";

                        zip.file(`image_${i+1}.${ext}`, blob);
                    }
                    
                    const content = await zip.generateAsync({type: "blob"});
                    const zipUrl = URL.createObjectURL(content);
                    const a = document.createElement('a');
                    a.href = zipUrl;
                    a.download = `Fetched_Images_${toZip.length}.zip`;
                    a.click();
                } catch (err) {
                    console.error("Zipping failed", err);
                    alert("Failed to create ZIP.");
                } finally {
                    downloadBtn.innerHTML = originalText;
                    downloadBtn.disabled = false;
                }
            };
        })();
    </script>

    <!-- ========================================== -->
    <!-- DYNAMIC PWA REGISTRATION (SINGLE FILE)     -->
    <!-- ========================================== -->
    <script>
        (function() {
            // 1. Generate & Inject Manifest dynamically
            const manifestObj = {
                "name": "AI Extraction Studio",
                "short_name": "AI Studio",
                "start_url": ".",
                "display": "standalone",
                "background_color": "#000000",
                "theme_color": "#4f46e5",
                "description": "Image cropping and video extraction AI tools running locally in your browser.",
                "icons": [{
                    "src": "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 512 512'><rect width='512' height='512' rx='120' fill='%234f46e5'/><path d='M150 350 L250 150 L350 350 Z' fill='white'/></svg>",
                    "sizes": "512x512",
                    "type": "image/svg+xml",
                    "purpose": "any maskable"
                }]
            };
            const manifestBlob = new Blob([JSON.stringify(manifestObj)], {type: 'application/json'});
            const manifestUrl = URL.createObjectURL(manifestBlob);
            const link = document.createElement('link');
            link.rel = 'manifest';
            link.href = manifestUrl;
            document.head.appendChild(link);

            /* Note: Dynamic Service Worker registration via Blob URL has been removed.
              Browsers block 'blob:' protocols for Service Workers for security reasons.
              To make this a fully offline-installable PWA in the future, you will need to 
              host it on a local or live web server and link a separate 'sw.js' file.
            */
        })();
    </script>
</body>
</html>