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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{% extends "base.html" %}

{% block title %}Material Detection Head - MV+{% endblock %}

{% block content %}
<style>
    html, body {
        height: 100%;
        overflow: auto;
        margin: 0;
        padding: 0;
    }

    .spatiotemporal-container {
        max-width: 1400px;
        margin: 0 auto;
        padding: 20px;
        min-height: 100vh;
        box-sizing: border-box;
    }

    .spatiotemporal-container .main-content {
        display: grid !important;
        grid-template-columns: 1fr 1fr 1fr !important;
        gap: 20px !important;
        margin-bottom: 30px !important;
        margin-top: 0 !important;
        min-height: auto !important;
        padding: 0 !important;
        overflow-y: visible !important;
        flex-direction: row !important;
    }

    .card {
        background: rgba(0, 0, 0, 0.8);
        border-radius: 20px;
        padding: 20px;
        box-shadow: 0 10px 20px rgba(0,0,0,0.3);
        border: 1px solid rgba(185, 29, 48, 0.3);
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        min-height: 700px;
        max-height: calc(100vh - 40px);
        overflow-y: auto;
        box-sizing: border-box;
    }

    .card:hover {
        transform: translateY(-2px);
        box-shadow: 0 15px 30px rgba(0,0,0,0.4);
        border-color: #B91D30;
    }

    .card h2 {
        color: #ffffff;
        margin-bottom: 15px;
        font-size: 1.5rem;
        font-weight: normal;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .upload-section {
        margin-bottom: 20px;
    }

    .upload-options {
        display: flex;
        gap: 15px;
        margin-bottom: 20px;
    }

    .upload-btn {
        flex: 1;
        padding: 15px;
        border: 2px dashed #555555;
        border-radius: 15px;
        text-align: center;
        cursor: pointer;
        transition: all 0.3s ease;
        background: rgba(0, 0, 0, 0.3);
        color: #ffffff;
        font-size: 1rem;
    }

    .upload-btn:hover {
        background: rgba(185, 29, 48, 0.1);
        color: white;
        border-color: #ffffff;
    }

    .upload-btn.active {
        background: rgba(185, 29, 48, 0.2);
        border-color: #ffffff;
    }

    .upload-btn i {
        margin-right: 8px;
        font-size: 1rem;
    }

    .file-input {
        display: none;
    }

    /* File Browser Modal */
    .file-browser-modal {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.8);
        z-index: 10000;
        justify-content: center;
        align-items: center;
    }

    .file-browser-modal.active {
        display: flex;
    }

    .file-browser-content {
        background: rgba(0, 0, 0, 0.95);
        border: 2px solid #B91D30;
        border-radius: 20px;
        padding: 30px;
        max-width: 800px;
        max-height: 80vh;
        width: 90%;
        overflow-y: auto;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    }

    .file-browser-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid #333;
    }

    .file-browser-header h3 {
        color: #ffffff;
        margin: 0;
        font-size: 1.5rem;
    }

    .file-browser-close {
        background: none;
        border: none;
        color: #ffffff;
        font-size: 1.5rem;
        cursor: pointer;
        padding: 5px 10px;
        border-radius: 5px;
        transition: background 0.3s ease;
    }

    .file-browser-close:hover {
        background: rgba(185, 29, 48, 0.3);
    }

    .file-browser-list {
        display: flex;
        flex-direction: column;
        gap: 8px;
        margin-bottom: 20px;
        max-height: 400px;
        overflow-y: auto;
    }

    .file-browser-item {
        background: rgba(0, 0, 0, 0.6);
        border: 2px solid #333;
        border-radius: 8px;
        padding: 12px 15px;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: space-between;
        text-align: left;
    }

    .file-browser-item:hover {
        border-color: #B91D30;
        background: rgba(185, 29, 48, 0.1);
        transform: translateX(5px);
    }

    .file-browser-item.selected {
        border-color: #B91D30;
        background: rgba(185, 29, 48, 0.2);
    }

    .file-browser-item img {
        display: none;
    }

    .file-browser-item .file-name {
        color: #ffffff;
        font-size: 0.9rem;
        word-break: break-word;
        flex: 1;
        margin-right: 15px;
    }

    .file-browser-item .file-size {
        color: #999;
        font-size: 0.8rem;
        white-space: nowrap;
    }

    .file-browser-item.directory {
        border-color: #555;
    }

    .file-browser-item.directory:hover {
        border-color: #B91D30;
    }

    .file-browser-item.directory .file-name {
        color: #B91D30;
        font-weight: bold;
    }

    .file-browser-item.directory .directory-icon {
        font-size: 2rem;
        color: #B91D30;
        margin-bottom: 10px;
    }


    .file-browser-actions {
        display: flex;
        gap: 10px;
        justify-content: flex-end;
        margin-top: 20px;
        padding-top: 20px;
        border-top: 1px solid #333;
    }

    .file-browser-btn {
        padding: 10px 20px;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        font-size: 1rem;
        transition: all 0.3s ease;
    }

    .file-browser-btn-primary {
        background: #B91D30;
        color: white;
    }

    .file-browser-btn-primary:hover {
        background: #8a1523;
    }

    .file-browser-btn-secondary {
        background: #333;
        color: white;
    }

    .file-browser-btn-secondary:hover {
        background: #555;
    }

    .file-browser-loading {
        text-align: center;
        color: #ffffff;
        padding: 40px;
    }

    .file-browser-empty {
        text-align: center;
        color: #999;
        padding: 40px;
    }

    .image-controls {
        margin-bottom: 25px;
        display: none !important;
    }

    .control-group {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 15px;
        margin-bottom: 15px;
    }

    .control-group.full-width {
        grid-template-columns: 1fr;
    }

    .control-label {
        color: #ffffff;
        font-size: 0.9rem;
        margin-bottom: 5px;
        font-weight: 500;
    }

    .model-select {
        width: 100%;
        padding: 12px;
        border: 1px solid #3C3435;
        border-radius: 8px;
        background: rgba(0, 0, 0, 0.5);
        color: #ffffff;
        font-size: 0.9rem;
    }

    .model-select:focus {
        outline: none;
        border-color: #ffffff;
        box-shadow: 0 0 0 2px rgba(185, 29, 48, 0.3);
    }

    .model-select option {
        background: #000000;
        color: #ffffff;
    }

    .image-preview-wrapper {
        position: relative;
        width: 100%;
        height: 300px;
        border: 2px dashed #B91D30;
        border-radius: 15px;
        overflow: hidden;
        background: rgba(0, 0, 0, 0.3);
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 20px;
    }

    .image-preview-wrapper.has-image {
        border-style: solid;
        background: transparent;
    }

    .image-preview-wrapper img {
        max-width: 100%;
        max-height: 100%;
        object-fit: contain;
        border-radius: 10px;
    }

    .image-preview-wrapper .placeholder {
        color: #666;
        text-align: center;
        font-size: 1rem;
    }

    .image-preview-wrapper .placeholder i {
        font-size: 3rem;
        margin-bottom: 10px;
        display: block;
        color: #B91D30;
    }

    .transform-buttons-compact {
        display: flex;
        gap: 8px;
        margin-bottom: 15px;
        flex-wrap: wrap;
    }

    .transform-btn {
        padding: 8px 12px;
        border: 1px solid #B91D30;
        border-radius: 6px;
        background: rgba(0, 0, 0, 0.3);
        color: #ffffff;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 0.8rem;
        display: flex;
        align-items: center;
        gap: 4px;
    }

    .transform-btn:hover {
        background: rgba(185, 29, 48, 0.2);
        border-color: #ffffff;
    }

    .transform-btn.active {
        background: rgba(185, 29, 48, 0.3);
        border-color: #ffffff;
    }

    .detect-btn {
        width: 100%;
        padding: 15px;
        background: linear-gradient(135deg, #2F1113, #2f0e11);
        border: none;
        border-radius: 10px;
        color: white;
        font-size: 1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        margin-top: 20px;
    }

    .detect-btn:hover:not(:disabled) {
        background: linear-gradient(135deg, #2f0e11, #2F1113);
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(47, 17, 19, 0.4);
    }

    .detect-btn:disabled {
        background: #666;
        cursor: not-allowed;
        transform: none;
        box-shadow: none;
    }

    .performance-section {
        background: rgba(0, 0, 0, 0.6);
        border-radius: 15px;
        padding: 15px;
        border: 1px solid rgba(185, 29, 48, 0.2);
    }

    .performance-title {
        color: #ffffff;
        font-size: 1.1rem;
        margin-bottom: 15px;
        font-weight: 600;
    }

    .performance-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 15px;
    }

    .performance-item {
        background: rgba(0, 0, 0, 0.3);
        padding: 12px;
        border-radius: 8px;
        border: 1px solid rgba(185, 29, 48, 0.1);
    }

    .performance-label {
        color: #ccc;
        font-size: 0.8rem;
        margin-bottom: 5px;
    }

    .performance-value {
        color: #ffffff;
        font-size: 1rem;
        font-weight: 600;
    }

    .metrics-section {
        background: rgba(0, 0, 0, 0.6);
        border-radius: 15px;
        padding: 15px;
        margin-top: 15px;
        border: 1px solid rgba(185, 29, 48, 0.2);
    }

    .metrics-grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .metric-item {
        background: rgba(0, 0, 0, 0.3);
        padding: 10px;
        border-radius: 8px;
        border: 1px solid rgba(185, 29, 48, 0.1);
    }

    .metric-label {
        color: #ccc;
        font-size: 0.8rem;
        margin-bottom: 5px;
    }

    .metric-value {
        color: #ffffff;
        font-size: 1rem;
        font-weight: 600;
    }

    .detection-results {
        background: rgba(0, 0, 0, 0.6);
        border-radius: 15px;
        padding: 20px;
        margin-top: 20px;
        border: 1px solid rgba(185, 29, 48, 0.2);
    }

    .detection-title {
        color: #ffffff;
        font-size: 1.1rem;
        margin-bottom: 15px;
        font-weight: 600;
    }

    .prediction-item {
        background: rgba(0, 0, 0, 0.3);
        padding: 12px;
        border-radius: 8px;
        margin-bottom: 10px;
        border: 1px solid rgba(185, 29, 48, 0.1);
        color: #ffffff;
        font-size: 0.9rem;
    }

    .detection-results-dual {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 15px;
        margin-bottom: 10px;
    }

    .detection-results-column {
        display: flex;
        flex-direction: column;
    }

    .detection-subtitle {
        font-size: 1rem;
        font-weight: 600;
        color: #cccccc;
        margin-bottom: 10px;
        text-align: center;
    }

    .detection-results-column .prediction-item {
        margin-bottom: 5px;
        flex: 1;
    }

    .prediction-item:last-child {
        margin-bottom: 0;
    }

    .result-image {
        width: 100%;
        max-height: 200px;
        object-fit: contain;
        border-radius: 8px;
        margin-top: 15px;
        border: 1px solid rgba(185, 29, 48, 0.2);
    }

    .batch-navigation {
        display: flex;
        gap: 10px;
        margin-top: 15px;
    }

    .nav-btn {
        flex: 1;
        padding: 10px;
        border: 1px solid #B91D30;
        border-radius: 6px;
        background: rgba(0, 0, 0, 0.3);
        color: #ffffff;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 0.9rem;
    }

    .nav-btn:hover:not(:disabled) {
        background: rgba(185, 29, 48, 0.2);
        border-color: #ffffff;
    }

    .nav-btn:disabled {
        background: #333;
        cursor: not-allowed;
        border-color: #666;
    }

    .top-sections {
        display: flex;
        flex-direction: column;
        gap: 15px;
        margin-bottom: 20px;
    }

    .filename-display {
        padding: 10px;
        background: rgba(255, 255, 255, 0.05);
        border-radius: 8px;
        border: 1px solid rgba(255, 255, 255, 0.1);
    }

    .filename-label {
        font-size: 0.8rem;
        color: #888;
        margin-bottom: 5px;
    }

    .filename-value {
        font-size: 0.9rem;
        color: #fff;
        font-weight: 500;
        word-break: break-all;
    }


    @media (max-width: 768px) {
        .spatiotemporal-container .main-content {
            grid-template-columns: 1fr !important;
        }
        
        .control-group {
            grid-template-columns: 1fr !important;
        }
        
        .performance-grid {
            grid-template-columns: 1fr !important;
        }
    }
</style>

<div class="spatiotemporal-container">
    <!-- Simple Status Message -->
    <div id="statusMessage" style="color: #ccc; font-size: 0.9rem; margin-bottom: 15px; text-align: center;">Please select image(s)</div>
    
    <div class="main-content">
        <div class="card">
            <h2>
                Material Detection
            </h2>
            
            <div class="upload-section">
                <div class="upload-options">
                    <div class="upload-btn" id="singleUploadBtn">
                        Select Image
                    </div>
                    <div class="upload-btn" id="batchUploadBtn">
                        Batch Upload
                    </div>
                </div>
                
                <input type="file" id="singleFileInput" class="file-input" accept="image/png,.sto">
                <input type="file" id="batchFileInput" class="file-input" accept="image/png,.sto" multiple>
            </div>

            <!-- File Browser Modal -->
            <div class="file-browser-modal" id="fileBrowserModal">
                <div class="file-browser-content">
                    <div class="file-browser-header">
                        <h3>Select Test Image</h3>
                        <button class="file-browser-close" id="fileBrowserClose">&times;</button>
                    </div>
                    <div id="fileBrowserList" class="file-browser-list">
                        <div class="file-browser-loading">Loading images...</div>
                    </div>
                    <div class="file-browser-actions">
                        <button class="file-browser-btn file-browser-btn-secondary" id="fileBrowserCancel">Cancel</button>
                        <button class="file-browser-btn file-browser-btn-primary" id="fileBrowserSelect" disabled>Select</button>
                    </div>
                </div>
            </div>

            <div class="image-controls" id="imageControls" style="display: none;">
                <div class="control-group">
                    <div>
                        <div class="control-label">Select Model Weight:</div>
                        <select id="modelSelect" class="model-select">
                            <option value="">Loading weights...</option>
                        </select>
                    </div>
                </div>



                <button class="detect-btn" id="detectBtn" disabled>
                    Detect Material
                </button>

                <div class="batch-navigation" id="batchNavigation" style="display: none;">
                    <button class="nav-btn" id="prevImageBtn" disabled>
                        Previous
                    </button>
                    <button class="nav-btn" id="nextImageBtn" disabled>
                        Next
                    </button>
                </div>
            </div>
        </div>

        <!-- Right Card: Results -->
        <div class="card">
            <h2>
                Detection Results
            </h2>
            
            <div class="top-sections">
                <div class="filename-display">
                    <div class="filename-label">Selected Image:</div>
                    <div class="filename-value" id="filenameValue">No image selected</div>
                </div>

            </div>

            <div class="detection-results">
                <div class="detection-subtitle">Detected material(s)</div>
                <div id="prediction1" class="prediction-item">1. -</div>
                <div id="prediction2" class="prediction-item">2. -</div>
                <div id="prediction3" class="prediction-item">3. -</div>
                <div id="detectionCount" class="prediction-item">Found no material(s)</div>
            </div>
        </div>

        <!-- Performance and Architecture Card -->
        <div class="card">
            <h2>
                Performance and Architecture
            </h2>
            
            <div class="metrics-section">
                <div class="metrics-grid">
                    <div class="metric-item">
                        <div class="metric-label">Model Architecture</div>
                        <div class="metric-value" id="architectureValue">-</div>
                    </div>
                    <div class="metric-item">
                        <div class="metric-label">Inference Time</div>
                        <div class="metric-value" id="inferenceTimeValue">-</div>
                    </div>
                    <div class="metric-item">
                        <div class="metric-label">Classes</div>
                        <div class="metric-value" id="classesValue" style="font-size: 0.85rem; word-break: break-word;">-</div>
                    </div>
                    <div class="metric-item">
                        <div class="metric-label">File Size</div>
                        <div class="metric-value" id="fileSizeValue">-</div>
                    </div>
                    <div class="metric-item">
                        <div class="metric-label">transient image Dimensions</div>
                        <div class="metric-value" id="transientImageDimensions">-</div>
                    </div>
                    <div class="metric-item">
                        <div class="metric-label">Upload Time</div>
                        <div class="metric-value" id="uploadTimeValue">-</div>
                    </div>
                    <div class="metric-item">
                        <div class="metric-label">Processing Status</div>
                        <div class="metric-value" id="processingStatusValue"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    let currentImage = null;
    let currentRotation = 0;
    let currentFlip = { horizontal: false, vertical: false };
    let batchFiles = [];
    let currentBatchIndex = 0;
    let selectedFile = null;
    
    // Cache for detection results
    let detectionCache = {};
    let lastImageHash = null;
    let lastWeightPath = null;

    // Initialize the page
    document.addEventListener('DOMContentLoaded', function() {
        setupEventListeners();
        loadModelWeights();
    });

    function setupEventListeners() {
        // File Browser Modal
        const fileBrowserModal = document.getElementById('fileBrowserModal');
        const fileBrowserClose = document.getElementById('fileBrowserClose');
        const fileBrowserCancel = document.getElementById('fileBrowserCancel');
        const fileBrowserSelect = document.getElementById('fileBrowserSelect');
        const fileBrowserList = document.getElementById('fileBrowserList');
        const singleFileInput = document.getElementById('singleFileInput');
        const batchFileInput = document.getElementById('batchFileInput');
        let selectedFiles = [];
        let isBatchMode = false;

        // Upload options - open file browser modal when clicked
        const uploadOptions = document.querySelector('.upload-options');
        if (uploadOptions) {
            uploadOptions.addEventListener('click', (e) => {
                // Don't trigger if clicking directly on the buttons
                if (e.target.closest('.upload-btn')) {
                    return;
                }
                // Default to single mode when clicking on upload-options
                isBatchMode = false;
                selectedFiles = [];
                openFileBrowser();
            });
        }

        // Upload buttons - open file browser modal
        document.getElementById('singleUploadBtn').addEventListener('click', () => {
            isBatchMode = false;
            selectedFiles = [];
            openFileBrowser();
        });

        document.getElementById('batchUploadBtn').addEventListener('click', () => {
            isBatchMode = true;
            selectedFiles = [];
            openFileBrowser();
        });

        // Close modal handlers
        fileBrowserClose.addEventListener('click', closeFileBrowser);
        fileBrowserCancel.addEventListener('click', closeFileBrowser);
        fileBrowserModal.addEventListener('click', (e) => {
            if (e.target === fileBrowserModal) {
                closeFileBrowser();
            }
        });

        // Select button handler
        fileBrowserSelect.addEventListener('click', () => {
            if (selectedFiles.length === 0) return;
            
            if (isBatchMode) {
                loadFilesFromBrowser(selectedFiles);
            } else {
                loadFilesFromBrowser([selectedFiles[0]]);
            }
            closeFileBrowser();
        });

        // File browser functions
        let currentPath = '';
        
        async function openFileBrowser(subpath = '') {
            fileBrowserModal.classList.add('active');
            fileBrowserList.innerHTML = '<div class="file-browser-loading">Loading images...</div>';
            fileBrowserSelect.disabled = true;
            selectedFiles = [];
            currentPath = subpath;
            
            try {
                let url = '/api/list_testimages/material_detection_head';
                if (subpath) {
                    url += '/' + encodeURIComponent(subpath);
                }
                const response = await fetch(url);
                const data = await response.json();
                
                if (data.success && (data.directories && data.directories.length > 0 || data.files && data.files.length > 0)) {
                    fileBrowserList.innerHTML = '';
                    
                    // Display directories first
                    if (data.directories && data.directories.length > 0) {
                        data.directories.forEach(dir => {
                            const dirItem = document.createElement('div');
                            dirItem.className = 'file-browser-item directory';
                            dirItem.dataset.filename = dir.name;
                            dirItem.dataset.type = 'directory';
                            
                            dirItem.innerHTML = `
                                <div class="file-name">📁 ${dir.name}</div>
                                <div class="file-size">Directory</div>
                            `;
                            
                            // Click to navigate into directory
                            dirItem.addEventListener('click', () => {
                                const newPath = currentPath ? `${currentPath}/${dir.name}` : dir.name;
                                openFileBrowser(newPath);
                            });
                            
                            fileBrowserList.appendChild(dirItem);
                        });
                    }
                    
                    // Display files
                    if (data.files && data.files.length > 0) {
                        data.files.forEach(file => {
                            const fileItem = document.createElement('div');
                            fileItem.className = 'file-browser-item';
                            fileItem.dataset.filename = file.name;
                            fileItem.dataset.type = 'file';
                            
                            const fileSize = formatFileSize(file.size);
                            
                            fileItem.innerHTML = `
                                <div class="file-name">${file.name}</div>
                                <div class="file-size">${fileSize}</div>
                            `;
                            
                            // Single click - select in batch mode, import directly in single mode
                            fileItem.addEventListener('click', () => {
                                if (isBatchMode) {
                                    if (fileItem.classList.contains('selected')) {
                                        fileItem.classList.remove('selected');
                                        const filePath = currentPath ? `${currentPath}/${file.name}` : file.name;
                                        selectedFiles = selectedFiles.filter(f => f !== filePath);
                                    } else {
                                        fileItem.classList.add('selected');
                                        const filePath = currentPath ? `${currentPath}/${file.name}` : file.name;
                                        selectedFiles.push(filePath);
                                    }
                                    fileBrowserSelect.disabled = selectedFiles.length === 0;
                                } else {
                                    document.querySelectorAll('.file-browser-item').forEach(item => {
                                        item.classList.remove('selected');
                                    });
                                    fileItem.classList.add('selected');
                                    const filePath = currentPath ? `${currentPath}/${file.name}` : file.name;
                                    selectedFiles = [filePath];
                                    fileBrowserSelect.disabled = false;
                                    loadFilesFromBrowser([filePath]);
                                    closeFileBrowser();
                                }
                            });
                            
                            // Double click - always import directly
                            fileItem.addEventListener('dblclick', () => {
                                const filePath = currentPath ? `${currentPath}/${file.name}` : file.name;
                                loadFilesFromBrowser([filePath]);
                                closeFileBrowser();
                            });
                            
                            fileBrowserList.appendChild(fileItem);
                        });
                    }
                } else {
                    fileBrowserList.innerHTML = '<div class="file-browser-empty">No files or directories found</div>';
                }
            } catch (error) {
                console.error('Error loading test images:', error);
                fileBrowserList.innerHTML = '<div class="file-browser-empty">Error loading images. Please try uploading from your device.</div>';
            }
        }

        function closeFileBrowser() {
            fileBrowserModal.classList.remove('active');
            selectedFiles = [];
            currentPath = '';
            fileBrowserSelect.disabled = true;
        }

        async function loadFilesFromBrowser(filenames) {
            try {
                if (isBatchMode) {
                    const files = [];
                    for (const filename of filenames) {
                        const response = await fetch(`/api/get_testimage/material_detection_head/${encodeURIComponent(filename)}`);
                        if (!response.ok) {
                            throw new Error(`Failed to load ${filename}: ${response.statusText}`);
                        }
                        const blob = await response.blob();
                        const ext = filename.split('.').pop();
                        const mimeTypes = {
                            'jpg': 'image/jpeg', 'JPG': 'image/jpeg',
                            'jpeg': 'image/jpeg', 'JPEG': 'image/jpeg',
                            'png': 'image/png', 'PNG': 'image/png',
                            'gif': 'image/gif', 'GIF': 'image/gif',
                            'webp': 'image/webp', 'WEBP': 'image/webp',
                            'bmp': 'image/bmp', 'BMP': 'image/bmp',
                            'tiff': 'image/tiff', 'TIFF': 'image/tiff',
                            'tif': 'image/tiff', 'TIF': 'image/tiff',
                            'heic': 'image/heic', 'HEIC': 'image/heic',
                            'sto': 'application/octet-stream', 'STO': 'application/octet-stream'
                        };
                        const mimeType = mimeTypes[ext] || mimeTypes[ext.toLowerCase()] || blob.type || 'image/jpeg';
                        const file = new File([blob], filename, { type: mimeType, lastModified: Date.now() });
                        files.push(file);
                    }
                    const dataTransfer = new DataTransfer();
                    files.forEach(file => dataTransfer.items.add(file));
                    // Clear the input first, then set files
                    batchFileInput.value = '';
                    batchFileInput.files = dataTransfer.files;
                    // Trigger change event to process the files and display preview
                    const changeEvent = new Event('change', { bubbles: true });
                    batchFileInput.dispatchEvent(changeEvent);
                } else {
                    const filename = filenames[0];
                    const response = await fetch(`/api/get_testimage/material_detection_head/${encodeURIComponent(filename)}`);
                    if (!response.ok) {
                        throw new Error(`Failed to load ${filename}: ${response.statusText}`);
                    }
                    const blob = await response.blob();
                    const ext = filename.split('.').pop().toLowerCase();
                    const mimeTypes = {
                        'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'png': 'image/png',
                        'gif': 'image/gif', 'webp': 'image/webp', 'heic': 'image/heic', 'HEIC': 'image/heic'
                    };
                    const mimeType = mimeTypes[ext] || blob.type || 'image/jpeg';
                    const file = new File([blob], filename, { type: mimeType, lastModified: Date.now() });
                    const dataTransfer = new DataTransfer();
                    dataTransfer.items.add(file);
                    // Clear the input first, then set files
                    singleFileInput.value = '';
                    singleFileInput.files = dataTransfer.files;
                    // Trigger change event to process the file and display preview
                    const changeEvent = new Event('change', { bubbles: true });
                    singleFileInput.dispatchEvent(changeEvent);
                }
            } catch (error) {
                console.error('Error loading file from browser:', error);
                alert(`Error loading file: ${error.message}. Please try again.`);
            }
        }
        
        function enableControlsAfterUpload() {
            // Update filename display
            const filenameElement = document.getElementById('filenameValue');
            if (selectedFile && filenameElement) {
                filenameElement.textContent = selectedFile.name;
            }
            
            // Show image controls
            const imageControls = document.getElementById('imageControls');
            if (imageControls) {
                // Image controls are hidden via CSS
            }
            
            // Enable weight selection dropdown
            const modelSelect = document.getElementById('modelSelect');
            if (modelSelect) {
                modelSelect.disabled = false;
                // Ensure weights are loaded if not already
                if (modelSelect.options.length <= 1) {
                    loadModelWeights();
                }
            }
            
            // Update detect button state based on both image and weight selection
            updateDetectButtonState();
        }

        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
        }

        // File inputs
        document.getElementById('singleFileInput').addEventListener('change', handleSingleFileSelect);
        document.getElementById('batchFileInput').addEventListener('change', handleBatchFileSelect);

        // Detect button
        document.getElementById('detectBtn').addEventListener('click', detectObjects);

        // Clear status when a model weight is selected
        const modelSelect = document.getElementById('modelSelect');
        modelSelect.addEventListener('change', () => {
            const statusDiv = document.getElementById('statusMessage');
            if (statusDiv) statusDiv.textContent = '';
            const ps = document.getElementById('processingStatusValue');
            if (ps) ps.textContent = 'Ready';
            
            // Update detect button state based on both image and weight selection
            updateDetectButtonState();
        });

        // Batch navigation
        document.getElementById('prevImageBtn').addEventListener('click', () => navigateBatch(-1));
        document.getElementById('nextImageBtn').addEventListener('click', () => navigateBatch(1));
    }

    function loadModelWeights() {
        console.log('Loading model weights from /api/material_detection_head_weights');
        fetch('/api/material_detection_head_weights')
            .then(response => {
                console.log('Response status:', response.status);
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }
                return response.json();
            })
            .then(data => {
                console.log('Weights data received:', data);
                const select = document.getElementById('modelSelect');
                if (!select) {
                    console.error('modelSelect element not found');
                    return;
                }
                
                select.innerHTML = '<option value="">Select a model weight...</option>';
                
                // Display repo link if available
                if (data.repo_url) {
                    const repoLink = document.createElement('div');
                    repoLink.className = 'repo-link';
                    repoLink.style.cssText = 'margin-top: 5px; font-size: 0.85em; color: #00CED1;';
                    repoLink.innerHTML = `<a href="${data.repo_url}" target="_blank" style="color: #00CED1; text-decoration: none;">📦 View on Hugging Face: ${data.repo_id || 'Model Repository'}</a>`;
                    select.parentElement.appendChild(repoLink);
                }
                
                if (data.success && data.weights && data.weights.length > 0) {
                    console.log(`Found ${data.weights.length} weights`);
                    data.weights.forEach(weight => {
                        const option = document.createElement('option');
                        option.value = weight.path;
                        option.textContent = weight.display_name || weight.filename;
                        select.appendChild(option);
                    });
                } else {
                    console.warn('No weights found or empty response:', data);
                    select.innerHTML = '<option value="">No weights available</option>';
                }
            })
            .catch(error => {
                console.error('Error loading weights:', error);
                const select = document.getElementById('modelSelect');
                if (select) {
                    select.innerHTML = '<option value="">Error loading weights</option>';
                }
            });
    }

    function handleSingleFileSelect(event) {
        const file = event.target.files[0];
        if (!file) return;
        // Immediately show controls
        try {
            const ic0 = document.getElementById('imageControls');
            if (ic0) ic0.style.display = 'block';
        } catch (e) {}
        const lower = file.name.toLowerCase();
        if (lower.endsWith('.png')) {
            selectedFile = file;
            batchFiles = [file];
            currentBatchIndex = 0;
            displayImage(file);
            showImageControls();
            // Image controls are hidden via CSS
            // Update detect button state (will be enabled only if weight is also selected)
            updateDetectButtonState();
            hideBatchNavigation();
        } else if (lower.endsWith('.sto')) {
            const formData = new FormData();
            formData.append('file', file);
            fetch('/api/extract_sto_index0', { method: 'POST', body: formData })
                .then(async r => {
                    const raw = await r.text();
                    try { return JSON.parse(raw); } catch(e) { console.error('Non-JSON response:', raw); throw e; }
                })
                .then(data => {
                    if (data.success && data.image) {
                        const img = new Image();
                        img.onload = function() {
                            selectedFile = file;
                            batchFiles = [file];
                            currentBatchIndex = 0;
                            currentImage = img;
                            resetImage();
                            
                            // Call displayImage to handle filename update and prediction reset
                            displayImage(file);
                            
                            updateImageMetrics(img);
                            const psEl = document.getElementById('processingStatusValue');
                            const hasWeight = !!document.getElementById('modelSelect').value;
                            if (psEl) psEl.textContent = hasWeight ? 'Ready' : 'Not ready';
                        };
                        img.src = data.image;
                        showImageControls();
                        // Image controls are hidden via CSS
                        // Update detect button state (will be enabled only if weight is also selected)
                        updateDetectButtonState();
                        hideBatchNavigation();
                    } else {
                        updateStatus(data.error || 'Invalid STO file: missing PNG at index 0', true);
                    }
                })
                .catch(err => {
                    console.error('STO extract failed', err);
                    updateStatus('Invalid STO file: missing PNG at index 0', true);
                });
        } else {
            updateStatus('Unsupported file type. Please select PNG or STO.', true);
        }
    }

    function handleBatchFileSelect(event) {
        const files = Array.from(event.target.files);
        if (!files.length) return;
        // Immediately show controls
        try {
            // Image controls are hidden via CSS
        } catch (e) {}
        batchFiles = files;
        currentBatchIndex = 0;
        const first = files[0];
        const lower = first.name.toLowerCase();
        if (lower.endsWith('.png')) {
            selectedFile = first;
            displayImage(first);
            showImageControls();
            // Image controls are hidden via CSS
            // Update detect button state (will be enabled only if weight is also selected)
            updateDetectButtonState();
            showBatchNavigation();
        } else if (lower.endsWith('.sto')) {
            const formData = new FormData();
            formData.append('file', first);
            fetch('/api/extract_sto_index0', { method: 'POST', body: formData })
                .then(async r => {
                    const raw = await r.text();
                    try { return JSON.parse(raw); } catch(e) { console.error('Non-JSON response:', raw); throw e; }
                })
                .then(data => {
                    if (data.success && data.image) {
                        const img = new Image();
                        img.onload = function() {
                            selectedFile = first;
                            currentImage = img;
                            resetImage();
                            
                            // Call displayImage to handle filename update and prediction reset
                            displayImage(first);
                            
                            updateImageMetrics(img);
                            const psEl2 = document.getElementById('processingStatusValue');
                            const hasWeight2 = !!document.getElementById('modelSelect').value;
                            if (psEl2) psEl2.textContent = hasWeight2 ? 'Ready' : 'Not ready';
                        };
                        img.src = data.image;
                        showImageControls();
                        // Image controls are hidden via CSS
                        // Update detect button state (will be enabled only if weight is also selected)
                        updateDetectButtonState();
                        showBatchNavigation();
                    } else {
                        updateStatus(data.error || 'Invalid STO file: missing PNG at index 0', true);
                    }
                })
                .catch(err => {
                    console.error('STO extract failed', err);
                    updateStatus('Invalid STO file: missing PNG at index 0', true);
                });
        } else {
            updateStatus('Unsupported file type. Please select PNG or STO.', true);
        }
    }

    function displayImage(file) {
        // Update filename display
        const filenameElement = document.getElementById('filenameValue');
        if (filenameElement) {
            filenameElement.textContent = file.name;
            console.log('Updated filename to:', file.name);
        } else {
            console.error('filenameValue element not found');
        }
        // Clear status message after image selection
        const statusDiv = document.getElementById('statusMessage');
        if (statusDiv) statusDiv.textContent = '';
        // Reset detection results until next detect
        try {
            document.getElementById('prediction1').textContent = '1. -';
            document.getElementById('prediction2').textContent = '2. -';
            document.getElementById('prediction3').textContent = '3. -';
            document.getElementById('detectionCount').textContent = 'Found no material(s)';
        } catch (e) { /* ignore */ }
        
        // Update file metrics
        updateFileMetrics(file);
        
        const reader = new FileReader();
        reader.onload = function(e) {
            // Load the image for transformations
            const img = new Image();
            img.onload = function() {
                currentImage = img;
                resetImage();
                // Update image-specific metrics
                updateImageMetrics(img);
            };
            img.src = e.target.result;
        };
        reader.readAsDataURL(file);
    }

    function updateFileMetrics(file) {
        // File size
        const fileSizeKB = (file.size / 1024).toFixed(2);
        document.getElementById('fileSizeValue').textContent = `${fileSizeKB} KB`;
        
        // File format removed
        
        // Upload time
        const uploadTime = new Date().toLocaleTimeString();
        document.getElementById('uploadTimeValue').textContent = uploadTime;
        
        // Processing status: show when image selected
        const psElUp = document.getElementById('processingStatusValue');
        const hasWeightUp = !!document.getElementById('modelSelect').value;
        if (psElUp) psElUp.textContent = hasWeightUp ? 'Ready' : 'Not ready';
    }

    function updateImageMetrics(img) {
        // Image dimensions → write to transientImageDimensions (we removed imageDimensionsValue)
        const transientEl = document.getElementById('transientImageDimensions');
        if (transientEl) {
            transientEl.textContent = `${img.width} × ${img.height}`;
        }
        
        // Processing status: clear after image metrics updated
        const ps = document.getElementById('processingStatusValue');
        if (ps) ps.textContent = '';
    }

    function showImageControls() {
        // Image controls are hidden via CSS (display: none !important)
        // No need to show them, but still ensure weights are loaded
        const select = document.getElementById('modelSelect');
        if (select && select.options.length <= 1) {
            // Only reload if dropdown is empty or only has placeholder
            loadModelWeights();
        }
        // Update detect button state based on both image and weight selection
        updateDetectButtonState();
    }
    
    // Helper function to update detect button state based on image and weight selection
    function updateDetectButtonState() {
        const detectBtn = document.getElementById('detectBtn');
        if (!detectBtn) return;
        
        const hasImage = selectedFile !== null && selectedFile !== undefined;
        const hasWeight = document.getElementById('modelSelect').value !== '';
        
        // Enable detect button only when both image is uploaded and weight is selected
        detectBtn.disabled = !(hasImage && hasWeight);
    }

    function hideImageControls() {
        document.getElementById('imageControls').style.display = 'none';
        document.getElementById('detectBtn').disabled = true;
    }

    function showBatchNavigation() {
        document.getElementById('batchNavigation').style.display = 'flex';
        updateBatchNavigation();
    }

    function hideBatchNavigation() {
        document.getElementById('batchNavigation').style.display = 'none';
    }

    function updateBatchNavigation() {
        document.getElementById('prevImageBtn').disabled = currentBatchIndex === 0;
        document.getElementById('nextImageBtn').disabled = currentBatchIndex === batchFiles.length - 1;
    }

    function navigateBatch(direction) {
        const newIndex = currentBatchIndex + direction;
        if (newIndex >= 0 && newIndex < batchFiles.length) {
            currentBatchIndex = newIndex;
            selectedFile = batchFiles[currentBatchIndex];
            displayImage(selectedFile);
            updateBatchNavigation();
            // Update detect button state after navigating to new image
            updateDetectButtonState();
        }
    }

    function rotateImage(degrees) {
        if (!currentImage) return;
        
        currentRotation = (currentRotation + degrees) % 360;
        if (currentRotation < 0) currentRotation += 360;
        
    }

    function flipImage(direction) {
        if (!currentImage) return;
        
        if (direction === 'horizontal') {
            currentFlip.horizontal = !currentFlip.horizontal;
        } else if (direction === 'vertical') {
            currentFlip.vertical = !currentFlip.vertical;
        }
        
    }

    function resetImage() {
        if (!currentImage) return;
        
        currentRotation = 0;
        currentFlip = { horizontal: false, vertical: false };
        
    }



    // Function to create a hash of current image state
    function createImageHash() {
        if (!currentImage || !selectedFile) return null;
        
        // Create a simple hash based on filename, rotation, and flip state
        const hashString = `${selectedFile.name}_${currentRotation}_${currentFlip.horizontal}_${currentFlip.vertical}`;
        return btoa(hashString).substring(0, 16); // Simple hash
    }

    async function detectObjects() {
        // Validate that image is uploaded
        if (!selectedFile) {
            updateStatus('Please upload an image first', true);
            return;
        }
        
        // Validate that model weight is selected
        const weightPath = document.getElementById('modelSelect').value;
        if (!weightPath) {
            updateStatus('Please select a model weight first', true);
            return;
        }

        // Get filename from the displayed value
        const filenameElement = document.getElementById('filenameValue');
        const displayedFilename = filenameElement ? filenameElement.textContent : selectedFile.name;
        
        // Clear any prior status message before starting detection
        const statusDivDetect = document.getElementById('statusMessage');
        if (statusDivDetect) statusDivDetect.textContent = '';

        // Repopulate Input File Metrics at detection time
        try {
            if (selectedFile) {
                updateFileMetrics(selectedFile);
            }
            if (currentImage) {
                updateImageMetrics(currentImage);
            }
        } catch (e) {
            // Safe guard - ignore metric update errors
            console.warn('Metrics update failed:', e);
        }
        
        // Always perform fresh inference on each Detect click (no frontend caching)

        try {
            showLoading();
            const psStart = document.getElementById('processingStatusValue');
            if (psStart) psStart.textContent = 'Processing...';
            
            // Prepare form data
            const formData = new FormData();
            
            // Use the uploaded image file (selectedFile) for inference
            console.log('Detecting with uploaded image:', displayedFilename);
            console.log('File object:', selectedFile.name);
            formData.append('file', selectedFile);
            formData.append('weight_path', weightPath);
            
            // Send to API
            const response = await fetch('/api/detect_material_head', {
                method: 'POST',
                body: formData
            });

            const data = await response.json();
            
            if (data.success) {
                const psDone = document.getElementById('processingStatusValue');
                if (psDone) psDone.textContent = 'Inference Complete';
                // Ensure status is cleared on success
                const statusDivSuccess = document.getElementById('statusMessage');
                if (statusDivSuccess) statusDivSuccess.textContent = '';
                displayResults(data);
            } else {
                updateStatus(data.error || 'Detection failed', true);
            }
        } catch (error) {
            console.error('Detection error:', error);
            updateStatus('Detection failed: ' + error.message, true);
        } finally {
            hideLoading();
        }
    }

    function displayResults(data) {
        // Ensure Performance & Architecture section is visible
        const perfArchSection = document.querySelector('.performance-stats, .architecture-details, .performance-metrics');
        if (perfArchSection) {
            perfArchSection.style.display = 'block';
            perfArchSection.style.visibility = 'visible';
        }
        
        // Update performance metrics - ensure all fields are updated when detect is clicked
        const architectureEl = document.getElementById('architectureValue');
        const inferenceTimeEl = document.getElementById('inferenceTimeValue');
        
        if (architectureEl) {
            architectureEl.textContent = data.architecture || '-';
        }
        if (inferenceTimeEl && data.inference_time !== undefined) {
            inferenceTimeEl.textContent = `${data.inference_time.toFixed(1)}ms`;
        } else if (inferenceTimeEl) {
            inferenceTimeEl.textContent = '-';
        }
        
        // Update classes display
        console.log('DEBUG: Classes data from API:', data.classes);
        console.log('DEBUG: Classes display from API:', data.classes_display);
        
        const classesElement = document.getElementById('classesValue');
        if (!classesElement) {
            console.error('DEBUG: classesValue element not found!');
            return;
        }
        
        if (data.classes_display) {
            // Use the formatted display string if available
            console.log('DEBUG: Using classes_display:', data.classes_display);
            classesElement.textContent = data.classes_display;
        } else if (data.classes && data.classes.length > 0) {
            // Otherwise format from array
            const classesStr = Array.isArray(data.classes) 
                ? data.classes.join(', ') 
                : (typeof data.classes === 'string' ? data.classes : '-');
            console.log('DEBUG: Using formatted classes array:', classesStr);
            classesElement.textContent = classesStr;
        } else {
            console.log('DEBUG: No classes data found, setting to -');
            classesElement.textContent = '-';
        }
        
        console.log('DEBUG: Final classesValue textContent:', classesElement.textContent);

        // Update predictions - always show 3 with percentages (pad with 0%)
        const preds = (data.top3_predictions || []).slice(0, 3);
        while (preds.length < 3) {
            preds.push({ class: '-', probability: 0 });
        }
        for (let i = 0; i < 3; i++) {
            const element = document.getElementById(`prediction${i + 1}`);
            const pred = preds[i];
            element.textContent = `${i + 1}. ${pred.class} - ${(pred.probability * 100).toFixed(2)}%`;
        }

        // Update detection count - only count objects with >90% confidence
        let count = 0;
        if (data.top3_predictions && data.top3_predictions.length > 0) {
            count = data.top3_predictions.filter(pred => pred.probability > 0.9).length;
        }
        document.getElementById('detectionCount').textContent = `Found ${count} object(s)`;

    }

    function showLoading() {
        document.getElementById('detectBtn').disabled = true;
        document.getElementById('detectBtn').innerHTML = 'Detecting...';
    }

    function hideLoading() {
        document.getElementById('detectBtn').disabled = false;
        document.getElementById('detectBtn').innerHTML = 'Detect Material';
    }

    function updateStatus(message, isError = false) {
        const statusDiv = document.getElementById('statusMessage');
        statusDiv.textContent = message;
        statusDiv.style.color = isError ? '#ff6666' : '#ccc';
        
        if (isError) {
            setTimeout(() => {
                statusDiv.textContent = '';
                statusDiv.style.color = '#ccc';
            }, 3000);
        }
    }
</script>
{% endblock %}