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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a99a91
 
b4c8e0e
 
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
"""
Enhanced Streamlit GUI for Sign Language Detector
Modern, Professional File Processing Interface
"""

import streamlit as st
import cv2
import numpy as np
import os
import sys
import time
import threading
from PIL import Image
import tempfile
from typing import Optional, List, Dict, Any
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import base64
from io import BytesIO
import json

# Add src directory to path
sys.path.append(os.path.dirname(__file__))

from src.file_handler import FileHandler
from src.output_handler import OutputHandler
from src.hand_detector import HandDetector
from src.gesture_extractor import GestureExtractor
from src.openai_classifier import SignLanguageClassifier
from src.visualization_utils import HandLandmarkVisualizer, create_processing_timeline
from src.export_utils import ResultExporter


# Page configuration
st.set_page_config(
    page_title="Sign Language Detector Pro",
    page_icon="🀟",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Comprehensive CSS for optimal text visibility and professional design
st.markdown("""
<style>
    /* Enhanced theme colors with WCAG AA compliant contrast ratios */
    :root {
        --primary-color: #2E86AB;
        --secondary-color: #A23B72;
        --accent-color: #F18F01;
        --background-color: #F8F9FA;
        --text-color: #2C3E50;
        --text-light: #FFFFFF;
        --text-dark: #1A1A1A;
        --text-medium: #495057;
        --text-muted: #6C757D;
        --success-color: #27AE60;
        --warning-color: #F39C12;
        --error-color: #E74C3C;
        --info-color: #17A2B8;
        --border-color: #E1E5E9;
        --card-background: #FFFFFF;
        --sidebar-background: #F8F9FA;
        --hover-background: #E9ECEF;
    }

    /* Hide Streamlit branding */
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    header {visibility: hidden;}

    /* Global text color improvements - Foundation */
    .stApp {
        color: var(--text-dark) !important;
        background-color: var(--background-color) !important;
    }

    /* All headings - Comprehensive coverage */
    h1, h2, h3, h4, h5, h6 {
        color: var(--text-dark) !important;
        font-weight: 600 !important;
    }

    /* All paragraph text */
    p {
        color: var(--text-color) !important;
    }

    /* All span elements */
    span {
        color: var(--text-dark) !important;
    }

    /* All div text content */
    div {
        color: var(--text-dark) !important;
    }

    /* Custom header */
    .main-header {
        background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
        padding: 2rem;
        border-radius: 15px;
        margin-bottom: 2rem;
        color: var(--text-light);
        text-align: center;
        box-shadow: 0 8px 32px rgba(0,0,0,0.1);
    }

    .main-header h1 {
        font-size: 3rem;
        font-weight: 700;
        margin-bottom: 0.5rem;
        color: var(--text-light) !important;
        text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
    }

    .main-header p {
        font-size: 1.2rem;
        opacity: 0.9;
        margin: 0;
        color: var(--text-light) !important;
    }

    /* File upload area with improved text visibility */
    .upload-area {
        border: 3px dashed var(--primary-color);
        border-radius: 15px;
        padding: 3rem;
        text-align: center;
        background: var(--card-background);
        margin: 2rem 0;
        transition: all 0.3s ease;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        color: var(--text-dark) !important;
    }

    .upload-area h3 {
        color: var(--text-dark) !important;
        font-weight: 600;
        margin-bottom: 1rem;
    }

    .upload-area p {
        color: var(--text-color) !important;
        margin: 0.5rem 0;
    }

    .upload-area:hover {
        border-color: var(--accent-color);
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(0,0,0,0.15);
    }

    /* Result cards with improved text contrast */
    .result-card {
        background: var(--card-background);
        border-radius: 15px;
        padding: 1.5rem;
        margin: 1rem 0;
        box-shadow: 0 4px 20px rgba(0,0,0,0.1);
        border-left: 5px solid var(--primary-color);
        transition: all 0.3s ease;
        color: var(--text-dark) !important;
    }

    .result-card h3 {
        color: var(--text-dark) !important;
        font-weight: 600;
        margin-bottom: 1rem;
    }

    .result-card p {
        color: var(--text-color) !important;
    }

    .result-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 8px 30px rgba(0,0,0,0.15);
    }

    /* Metrics styling with improved text visibility */
    .metric-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: var(--text-light) !important;
        padding: 1.5rem;
        border-radius: 15px;
        text-align: center;
        margin: 0.5rem;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    }

    .metric-value {
        font-size: 2.5rem;
        font-weight: bold;
        margin-bottom: 0.5rem;
        color: var(--text-light) !important;
    }

    .metric-label {
        font-size: 1rem;
        opacity: 0.9;
        color: var(--text-light) !important;
    }

    /* Progress bar styling */
    .stProgress > div > div > div > div {
        background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
        border-radius: 10px;
    }

    /* Comprehensive Button styling - All states covered */
    .stButton > button {
        background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
        color: var(--text-light) !important;
        border: none !important;
        border-radius: 10px !important;
        padding: 0.75rem 2rem !important;
        font-weight: 600 !important;
        font-size: 1rem !important;
        transition: all 0.3s ease !important;
        box-shadow: 0 4px 15px rgba(0,0,0,0.2) !important;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.3) !important;
    }

    .stButton > button:hover {
        transform: translateY(-2px) !important;
        box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
        color: var(--text-light) !important;
        background: linear-gradient(135deg, #3A9BC1, #B8457A) !important;
    }

    .stButton > button:focus {
        color: var(--text-light) !important;
        box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
        outline: 2px solid var(--accent-color) !important;
        outline-offset: 2px !important;
    }

    .stButton > button:active {
        color: var(--text-light) !important;
        transform: translateY(0px) !important;
        box-shadow: 0 2px 10px rgba(0,0,0,0.2) !important;
    }

    /* Download button specific styling */
    .stDownloadButton > button {
        background: linear-gradient(135deg, var(--success-color), #2ECC71) !important;
        color: var(--text-light) !important;
        border: none !important;
        border-radius: 10px !important;
        padding: 0.75rem 2rem !important;
        font-weight: 600 !important;
        font-size: 1rem !important;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.3) !important;
    }

    .stDownloadButton > button:hover {
        color: var(--text-light) !important;
        background: linear-gradient(135deg, #2ECC71, #27AE60) !important;
    }

    /* Comprehensive Sidebar styling - All elements covered */
    .css-1d391kg, .css-1lcbmhc, .css-17eq0hr, .css-1y4p8pa {
        background: var(--sidebar-background) !important;
        color: var(--text-dark) !important;
    }

    /* Sidebar text - All variations */
    .css-1d391kg .stMarkdown, .css-1lcbmhc .stMarkdown, .css-17eq0hr .stMarkdown {
        color: var(--text-dark) !important;
    }

    .css-1d391kg h1, .css-1d391kg h2, .css-1d391kg h3, .css-1d391kg h4, .css-1d391kg h5, .css-1d391kg h6 {
        color: var(--text-dark) !important;
        font-weight: 600 !important;
    }

    .css-1lcbmhc h1, .css-1lcbmhc h2, .css-1lcbmhc h3, .css-1lcbmhc h4, .css-1lcbmhc h5, .css-1lcbmhc h6 {
        color: var(--text-dark) !important;
        font-weight: 600 !important;
    }

    /* Sidebar labels and text */
    .css-1d391kg label, .css-1lcbmhc label {
        color: var(--text-dark) !important;
        font-weight: 500 !important;
    }

    .css-1d391kg p, .css-1lcbmhc p {
        color: var(--text-color) !important;
    }

    .css-1d391kg span, .css-1lcbmhc span {
        color: var(--text-dark) !important;
    }

    /* Sidebar widget labels */
    .css-1d391kg .stSelectbox label, .css-1d391kg .stSlider label, .css-1d391kg .stCheckbox label {
        color: var(--text-dark) !important;
        font-weight: 500 !important;
    }

    /* Success/Error messages with proper contrast */
    .success-message {
        background: var(--success-color) !important;
        color: var(--text-light) !important;
        padding: 1rem !important;
        border-radius: 10px !important;
        margin: 1rem 0 !important;
    }

    .error-message {
        background: var(--error-color) !important;
        color: var(--text-light) !important;
        padding: 1rem !important;
        border-radius: 10px !important;
        margin: 1rem 0 !important;
    }

    /* Streamlit native message styling improvements */
    .stAlert {
        color: var(--text-dark) !important;
    }

    .stSuccess {
        background-color: rgba(39, 174, 96, 0.1) !important;
        color: var(--text-dark) !important;
        border: 1px solid var(--success-color) !important;
    }

    .stError {
        background-color: rgba(231, 76, 60, 0.1) !important;
        color: var(--text-dark) !important;
        border: 1px solid var(--error-color) !important;
    }

    .stWarning {
        background-color: rgba(243, 156, 18, 0.1) !important;
        color: var(--text-dark) !important;
        border: 1px solid var(--warning-color) !important;
    }

    .stInfo {
        background-color: rgba(46, 134, 171, 0.1) !important;
        color: var(--text-dark) !important;
        border: 1px solid var(--primary-color) !important;
    }

    /* Loading animation */
    .loading-spinner {
        display: inline-block;
        width: 40px;
        height: 40px;
        border: 4px solid #f3f3f3;
        border-top: 4px solid var(--primary-color);
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }

    /* Comprehensive Form and Input styling - All form elements */
    .stTextInput > div > div > input {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 8px !important;
        padding: 0.75rem !important;
        font-size: 1rem !important;
    }

    .stTextInput > div > div > input::placeholder {
        color: var(--text-muted) !important;
        opacity: 0.7 !important;
    }

    .stTextInput > div > div > input:focus {
        border-color: var(--primary-color) !important;
        box-shadow: 0 0 0 2px rgba(46, 134, 171, 0.2) !important;
        color: var(--text-dark) !important;
    }

    /* Text area styling */
    .stTextArea > div > div > textarea {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 8px !important;
    }

    .stTextArea > div > div > textarea::placeholder {
        color: var(--text-muted) !important;
        opacity: 0.7 !important;
    }

    /* Select box styling */
    .stSelectbox > div > div > div {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 8px !important;
    }

    .stSelectbox > div > div > div > div {
        color: var(--text-dark) !important;
    }

    /* Multi-select styling */
    .stMultiSelect > div > div > div {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
    }

    /* Number input styling */
    .stNumberInput > div > div > input {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
    }

    /* Slider styling */
    .stSlider > div > div > div {
        color: var(--text-dark) !important;
    }

    .stSlider > div > div > div > div {
        color: var(--text-dark) !important;
    }

    /* Checkbox and radio styling */
    .stCheckbox > label {
        color: var(--text-dark) !important;
        font-weight: 500 !important;
    }

    .stRadio > label {
        color: var(--text-dark) !important;
        font-weight: 500 !important;
    }

    /* Form labels - comprehensive coverage */
    label {
        color: var(--text-dark) !important;
        font-weight: 500 !important;
        font-size: 1rem !important;
    }

    /* Comprehensive Tab styling - All states and variations */
    .stTabs [data-baseweb="tab-list"] {
        gap: 8px !important;
        border-bottom: 2px solid var(--border-color) !important;
    }

    .stTabs [data-baseweb="tab"] {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 8px 8px 0 0 !important;
        padding: 12px 20px !important;
        font-weight: 500 !important;
        font-size: 1rem !important;
        transition: all 0.3s ease !important;
        margin-bottom: -2px !important;
    }

    .stTabs [data-baseweb="tab"]:hover {
        background-color: var(--hover-background) !important;
        color: var(--text-dark) !important;
        border-color: var(--primary-color) !important;
    }

    .stTabs [aria-selected="true"] {
        background-color: var(--primary-color) !important;
        color: var(--text-light) !important;
        border-color: var(--primary-color) !important;
        font-weight: 600 !important;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.2) !important;
    }

    /* Tab content styling */
    .stTabs [data-baseweb="tab-panel"] {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        padding: 1.5rem !important;
        border-radius: 0 8px 8px 8px !important;
        border: 1px solid var(--border-color) !important;
        border-top: none !important;
    }

    /* Comprehensive Expander styling */
    .streamlit-expanderHeader {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 8px !important;
        padding: 1rem !important;
        font-weight: 600 !important;
        font-size: 1.1rem !important;
    }

    .streamlit-expanderHeader:hover {
        background-color: var(--hover-background) !important;
        color: var(--text-dark) !important;
    }

    .streamlit-expanderContent {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-top: none !important;
        border-radius: 0 0 8px 8px !important;
        padding: 1.5rem !important;
    }

    /* Comprehensive Metric styling - All metric components */
    .metric-container {
        background-color: var(--card-background) !important;
        color: var(--text-dark) !important;
        padding: 1rem !important;
        border-radius: 8px !important;
        border: 1px solid var(--border-color) !important;
    }

    /* Streamlit native metrics */
    .css-1xarl3l {
        color: var(--text-dark) !important;
    }

    .css-1xarl3l > div {
        color: var(--text-dark) !important;
    }

    /* Metric values and labels */
    [data-testid="metric-container"] {
        background-color: var(--card-background) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 8px !important;
        padding: 1rem !important;
    }

    [data-testid="metric-container"] > div {
        color: var(--text-dark) !important;
    }

    [data-testid="metric-container"] label {
        color: var(--text-medium) !important;
        font-weight: 500 !important;
    }

    /* Progress indicators and loading text */
    .stProgress > div > div > div {
        color: var(--text-dark) !important;
    }

    .stSpinner > div {
        color: var(--text-dark) !important;
    }

    /* File uploader styling */
    .stFileUploader > div > div > div {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
        border: 2px dashed var(--border-color) !important;
        border-radius: 8px !important;
    }

    .stFileUploader > div > div > div:hover {
        border-color: var(--primary-color) !important;
    }

    .stFileUploader label {
        color: var(--text-dark) !important;
        font-weight: 500 !important;
    }

    /* Data frame and table styling */
    .stDataFrame {
        color: var(--text-dark) !important;
    }

    .stDataFrame table {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
    }

    .stDataFrame th {
        color: var(--text-dark) !important;
        background-color: var(--hover-background) !important;
        font-weight: 600 !important;
    }

    .stDataFrame td {
        color: var(--text-dark) !important;
    }

    /* Code blocks and preformatted text */
    .stCode {
        color: var(--text-dark) !important;
        background-color: var(--hover-background) !important;
    }

    code {
        color: var(--text-dark) !important;
        background-color: var(--hover-background) !important;
        padding: 0.2rem 0.4rem !important;
        border-radius: 4px !important;
    }

    pre {
        color: var(--text-dark) !important;
        background-color: var(--hover-background) !important;
    }

    /* JSON and data display */
    .stJson {
        color: var(--text-dark) !important;
        background-color: var(--card-background) !important;
    }

    /* Caption and help text */
    .caption {
        color: var(--text-muted) !important;
        font-size: 0.9rem !important;
    }

    .help {
        color: var(--text-muted) !important;
        font-size: 0.85rem !important;
    }

    /* Tooltip styling */
    .stTooltipIcon {
        color: var(--text-medium) !important;
    }

    /* Link styling */
    a {
        color: var(--primary-color) !important;
        text-decoration: none !important;
    }

    a:hover {
        color: var(--secondary-color) !important;
        text-decoration: underline !important;
    }

    /* Status indicators */
    .status-success {
        color: var(--success-color) !important;
        font-weight: 600 !important;
    }

    .status-error {
        color: var(--error-color) !important;
        font-weight: 600 !important;
    }

    .status-warning {
        color: var(--warning-color) !important;
        font-weight: 600 !important;
    }

    .status-info {
        color: var(--info-color) !important;
        font-weight: 600 !important;
    }

    /* Responsive design */
    @media (max-width: 768px) {
        .main-header h1 {
            font-size: 2rem !important;
            color: var(--text-light) !important;
        }
        .main-header p {
            font-size: 1rem !important;
            color: var(--text-light) !important;
        }
        .upload-area {
            padding: 2rem !important;
        }

        /* Mobile text adjustments */
        h1, h2, h3, h4, h5, h6 {
            font-size: calc(1rem + 0.5vw) !important;
        }

        p, span, div {
            font-size: 0.9rem !important;
        }

        label {
            font-size: 0.9rem !important;
        }
    }

    /* High contrast mode support */
    @media (prefers-contrast: high) {
        :root {
            --text-dark: #000000;
            --text-light: #FFFFFF;
            --border-color: #000000;
        }
    }

    /* Dark mode support (if needed) */
    @media (prefers-color-scheme: dark) {
        .stApp {
            background-color: #1E1E1E !important;
        }

        :root {
            --background-color: #1E1E1E;
            --card-background: #2D2D2D;
            --text-dark: #FFFFFF;
            --text-color: #E0E0E0;
            --border-color: #404040;
            --hover-background: #404040;
        }
    }
</style>
""", unsafe_allow_html=True)

# Initialize session state
if 'file_handler' not in st.session_state:
    st.session_state.file_handler = None
if 'output_handler' not in st.session_state:
    st.session_state.output_handler = None
if 'detections' not in st.session_state:
    st.session_state.detections = []
if 'transcript' not in st.session_state:
    st.session_state.transcript = []
if 'processing_results' not in st.session_state:
    st.session_state.processing_results = []
if 'current_file' not in st.session_state:
    st.session_state.current_file = None
if 'visualizer' not in st.session_state:
    st.session_state.visualizer = HandLandmarkVisualizer()
if 'exporter' not in st.session_state:
    st.session_state.exporter = ResultExporter()


def initialize_components():
    """Initialize the application components."""
    if st.session_state.file_handler is None:
        st.session_state.file_handler = FileHandler()

    if st.session_state.output_handler is None:
        st.session_state.output_handler = OutputHandler(
            enable_speech=False,  # Disable speech in web interface
            save_transcript=False  # Handle transcript in session state
        )

def create_header():
    """Create the main header with modern styling."""
    st.markdown("""
    <div class="main-header">
        <h1>🀟 Sign Language Detector Pro</h1>
        <p>Advanced AI-Powered Gesture Recognition & Analysis</p>
    </div>
    """, unsafe_allow_html=True)

def create_file_upload_area():
    """Create an enhanced file upload area with drag-and-drop styling."""
    st.markdown("""
    <div class="upload-area">
        <h3 style="color: #2C3E50 !important; font-weight: 600; margin-bottom: 1rem;">πŸ“ Upload Your Files</h3>
        <p style="color: #2C3E50 !important; font-size: 1.1rem; margin-bottom: 0.5rem;">Drag and drop your images or videos here, or click to browse</p>
        <p style="color: #666666 !important; font-size: 0.9rem; margin: 0;"><small>Supported formats: JPG, PNG, BMP, MP4, AVI, MOV, MKV</small></p>
    </div>
    """, unsafe_allow_html=True)

def create_metrics_dashboard(results: List[Dict[str, Any]]):
    """Create a metrics dashboard showing processing statistics."""
    if not results:
        return

    # Calculate metrics
    total_files = len(results)
    successful_files = sum(1 for r in results if r.get('success', False))
    total_hands = sum(r.get('hands_detected', 0) for r in results if r.get('success', False))
    avg_confidence = 0

    if successful_files > 0:
        confidences = []
        for result in results:
            if result.get('success') and result.get('detections'):
                for detection in result['detections']:
                    if 'confidence' in detection:
                        confidences.append(detection['confidence'])
        avg_confidence = np.mean(confidences) if confidences else 0

    # Display metrics in columns
    col1, col2, col3, col4 = st.columns(4)

    with col1:
        st.markdown(f"""
        <div class="metric-card">
            <div class="metric-value" style="color: #FFFFFF !important; font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem;">{total_files}</div>
            <div class="metric-label" style="color: #FFFFFF !important; font-size: 1rem; opacity: 0.9;">Files Processed</div>
        </div>
        """, unsafe_allow_html=True)

    with col2:
        st.markdown(f"""
        <div class="metric-card">
            <div class="metric-value" style="color: #FFFFFF !important; font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem;">{successful_files}</div>
            <div class="metric-label" style="color: #FFFFFF !important; font-size: 1rem; opacity: 0.9;">Successful</div>
        </div>
        """, unsafe_allow_html=True)

    with col3:
        st.markdown(f"""
        <div class="metric-card">
            <div class="metric-value" style="color: #FFFFFF !important; font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem;">{total_hands}</div>
            <div class="metric-label" style="color: #FFFFFF !important; font-size: 1rem; opacity: 0.9;">Hands Detected</div>
        </div>
        """, unsafe_allow_html=True)

    with col4:
        st.markdown(f"""
        <div class="metric-card">
            <div class="metric-value" style="color: #FFFFFF !important; font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem;">{avg_confidence:.1%}</div>
            <div class="metric-label" style="color: #FFFFFF !important; font-size: 1rem; opacity: 0.9;">Avg Confidence</div>
        </div>
        """, unsafe_allow_html=True)

def create_confidence_chart(results: List[Dict[str, Any]], chart_key: str = "confidence_chart"):
    """Create a confidence score visualization."""
    confidences = []
    file_names = []

    for result in results:
        if result.get('success') and result.get('detections'):
            for i, detection in enumerate(result['detections']):
                if 'confidence' in detection:
                    confidences.append(detection['confidence'])
                    file_name = os.path.basename(result.get('file_path', 'Unknown'))
                    file_names.append(f"{file_name} - Hand {i+1}")

    if confidences:
        df = pd.DataFrame({
            'File': file_names,
            'Confidence': confidences
        })

        fig = px.bar(df, x='File', y='Confidence',
                    title='Hand Detection Confidence Scores',
                    color='Confidence',
                    color_continuous_scale='Viridis')
        fig.update_layout(
            xaxis_tickangle=-45,
            height=400,
            showlegend=False
        )
        st.plotly_chart(fig, use_container_width=True, key=chart_key)

def create_gesture_analysis_chart(results: List[Dict[str, Any]], chart_key: str = "gesture_analysis_chart"):
    """Create gesture analysis visualization."""
    gesture_data = []

    for result in results:
        if result.get('success') and result.get('detections'):
            for detection in result['detections']:
                if 'classification' in detection and detection['classification'].get('success'):
                    classification = detection['classification']
                    gesture_data.append({
                        'File': os.path.basename(result.get('file_path', 'Unknown')),
                        'Hand': detection.get('hand_label', 'Unknown'),
                        'Letter': classification.get('letter', 'N/A'),
                        'Word': classification.get('word', 'N/A'),
                        'Confidence': classification.get('confidence', 0)
                    })

    if gesture_data:
        df = pd.DataFrame(gesture_data)

        # Create subplots
        fig = make_subplots(
            rows=1, cols=2,
            subplot_titles=('Letters Detected', 'Classification Confidence'),
            specs=[[{"type": "pie"}, {"type": "histogram"}]]
        )

        # Letter distribution pie chart
        letter_counts = df['Letter'].value_counts()
        fig.add_trace(
            go.Pie(labels=letter_counts.index, values=letter_counts.values, name="Letters"),
            row=1, col=1
        )

        # Confidence histogram
        fig.add_trace(
            go.Histogram(x=df['Confidence'], name="Confidence", nbinsx=10),
            row=1, col=2
        )

        fig.update_layout(height=400, showlegend=False)
        st.plotly_chart(fig, use_container_width=True, key=chart_key)


def setup_ai_api():
    """Setup AI API key with automatic Gemini configuration."""
    st.sidebar.markdown("### πŸ”‘ AI API Configuration")

    # Use Gemini by default
    default_gemini_key = "AIzaSyDd2BfvfgnVQFkGufpuD76QOsaPM3hWgxo"

    # AI provider selection
    ai_provider = st.sidebar.selectbox(
        "AI Provider",
        ["Google Gemini (Recommended)", "OpenAI GPT"],
        index=0,
        help="Choose your AI provider for sign language classification"
    )

    use_gemini = "Gemini" in ai_provider

    # Check if user wants to use a custom API key
    use_custom_key = st.sidebar.checkbox("Use Custom API Key", value=False)

    if use_custom_key:
        if use_gemini:
            api_key = st.sidebar.text_input(
                "Custom Gemini API Key",
                type="password",
                help="Enter your custom Google Gemini API key",
                placeholder="AIza..."
            )
            env_key = 'GEMINI_API_KEY'
        else:
            api_key = st.sidebar.text_input(
                "Custom OpenAI API Key",
                type="password",
                help="Enter your custom OpenAI API key",
                placeholder="sk-..."
            )
            env_key = 'OPENAI_API_KEY'

        if api_key:
            os.environ[env_key] = api_key
            st.sidebar.success(f"βœ… Custom {ai_provider.split()[0]} API key configured")
            return api_key, use_gemini
        else:
            st.sidebar.warning("⚠️ Please enter your custom API key")
            return None, use_gemini
    else:
        # Use default keys
        if use_gemini:
            os.environ['GEMINI_API_KEY'] = default_gemini_key
            os.environ['USE_GEMINI'] = 'True'
            st.sidebar.success("βœ… Gemini API configured automatically")
            st.sidebar.info("πŸš€ Using Google Gemini for fast, accurate predictions")
            return default_gemini_key, True
        else:
            # OpenAI fallback (will likely fail due to quota)
            st.sidebar.warning("⚠️ OpenAI quota may be exceeded")
            st.sidebar.info("πŸ’‘ Recommend using Gemini for better reliability")
            return None, False

def create_settings_panel():
    """Create an advanced settings panel."""
    st.sidebar.markdown("### βš™οΈ Processing Settings")

    # Detection confidence threshold
    confidence_threshold = st.sidebar.slider(
        "Detection Confidence Threshold",
        min_value=0.1,
        max_value=1.0,
        value=0.7,
        step=0.1,
        help="Minimum confidence for hand detection"
    )

    # Maximum hands to detect
    max_hands = st.sidebar.selectbox(
        "Maximum Hands to Detect",
        options=[1, 2, 3, 4],
        index=1,
        help="Maximum number of hands to detect per image"
    )

    # Video frame sampling
    frame_skip = st.sidebar.slider(
        "Video Frame Sampling",
        min_value=1,
        max_value=30,
        value=5,
        help="Process every Nth frame in videos (higher = faster processing)"
    )

    # Export options
    st.sidebar.markdown("### πŸ“Š Export Options")
    export_format = st.sidebar.selectbox(
        "Export Format",
        options=["JSON", "CSV", "PDF Report"],
        help="Choose format for exporting results"
    )

    return {
        'confidence_threshold': confidence_threshold,
        'max_hands': max_hands,
        'frame_skip': frame_skip,
        'export_format': export_format
    }

def process_uploaded_files(uploaded_files: List, api_key: str, settings: Dict[str, Any], use_gemini: bool = True):
    """Process multiple uploaded files with progress tracking."""
    if not uploaded_files:
        return []

    results = []
    progress_bar = st.progress(0)
    status_text = st.empty()

    # Initialize file handler with settings
    file_handler = FileHandler(
        frame_skip=settings['frame_skip'],
        max_frames=100
    )

    if api_key:
        file_handler.initialize_classifier(api_key, use_gemini=use_gemini)

    for i, uploaded_file in enumerate(uploaded_files):
        # Update progress
        progress = (i + 1) / len(uploaded_files)
        progress_bar.progress(progress)
        status_text.text(f"Processing {uploaded_file.name}... ({i+1}/{len(uploaded_files)})")

        # Save uploaded file to temporary location
        with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
            tmp_file.write(uploaded_file.getvalue())
            tmp_path = tmp_file.name

        try:
            # Determine file type and process
            file_type = file_handler.get_file_type(tmp_path)

            if file_type == 'image':
                result = file_handler.process_image(tmp_path)
            elif file_type == 'video':
                result = file_handler.process_video(tmp_path)
            else:
                result = {'success': False, 'error': 'Unsupported file format'}

            # Add filename to result
            result['filename'] = uploaded_file.name
            result['file_size'] = len(uploaded_file.getvalue())
            results.append(result)

        except Exception as e:
            results.append({
                'success': False,
                'error': str(e),
                'filename': uploaded_file.name,
                'file_size': len(uploaded_file.getvalue())
            })

        finally:
            # Clean up temporary file
            try:
                os.unlink(tmp_path)
            except:
                pass

    progress_bar.empty()
    status_text.empty()

    return results


def create_image_with_landmarks(image_array: np.ndarray, hand_landmarks: List[Dict[str, Any]]) -> Image.Image:
    """Create an image with hand landmarks overlaid."""
    # Convert to PIL Image for display
    if len(image_array.shape) == 3 and image_array.shape[2] == 3:
        # BGR to RGB conversion
        image_rgb = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
    else:
        image_rgb = image_array

    return Image.fromarray(image_rgb)

def display_image_results(result: Dict[str, Any]):
    """Display results for image processing with enhanced UI."""
    if not result['success']:
        st.error(f"❌ Error processing {result.get('filename', 'file')}: {result.get('error', 'Unknown error')}")
        return

    filename = result.get('filename', 'Unknown')
    file_size = result.get('file_size', 0)

    # Create result card
    st.markdown(f"""
    <div class="result-card">
        <h3 style="color: #2C3E50 !important; font-weight: 600; margin-bottom: 1rem;">πŸ“Έ {filename}</h3>
        <p style="color: #2C3E50 !important;"><strong>File Size:</strong> {file_size / 1024:.1f} KB | <strong>Hands Detected:</strong> {result['hands_detected']}</p>
    </div>
    """, unsafe_allow_html=True)

    if result['hands_detected'] > 0:
        col1, col2 = st.columns([1, 1])

        with col1:
            st.subheader("πŸ–ΌοΈ Processed Images")

            # Create tabs for different views
            img_tab1, img_tab2, img_tab3 = st.tabs(["πŸ” Enhanced", "πŸ“Š Comparison", "🎯 3D View"])

            with img_tab1:
                if 'enhanced_image' in result:
                    enhanced_img = create_image_with_landmarks(result['enhanced_image'], [])
                    st.image(enhanced_img, caption="Enhanced Hand Landmarks", use_container_width=True)
                elif 'annotated_image' in result:
                    annotated_img = create_image_with_landmarks(result['annotated_image'], [])
                    st.image(annotated_img, caption="Hand Landmarks Detected", use_container_width=True)

            with img_tab2:
                if 'comparison_image' in result:
                    comparison_img = create_image_with_landmarks(result['comparison_image'], [])
                    st.image(comparison_img, caption="Before vs After Comparison", use_container_width=True)

            with img_tab3:
                # 3D visualization for first detected hand
                if result['detections'] and 'landmarks_3d' in result['detections'][0]:
                    hand_data = {
                        'label': result['detections'][0]['hand_label'],
                        'landmarks': result['detections'][0]['landmarks_3d']
                    }

                    visualizer = st.session_state.visualizer
                    fig_3d = visualizer.create_3d_hand_plot(hand_data)
                    st.plotly_chart(fig_3d, use_container_width=True, key="3d_hand_plot")
                else:
                    st.info("3D visualization requires hand landmark data")

        with col2:
            st.subheader("πŸ” Detection Details")

            for i, detection in enumerate(result['detections']):
                with st.expander(f"βœ‹ Hand {i+1}: {detection['hand_label']}", expanded=True):
                    # Confidence meter
                    confidence = detection['confidence']
                    st.metric("Detection Confidence", f"{confidence:.1%}")

                    # Progress bar for confidence
                    st.progress(confidence)

                    # Gesture description
                    st.text_area(
                        "Gesture Description",
                        detection['gesture_description'],
                        height=100,
                        disabled=True
                    )

                    # Classification results
                    if 'classification' in detection and detection['classification']['success']:
                        classification = detection['classification']

                        col_a, col_b = st.columns(2)
                        with col_a:
                            if classification.get('letter'):
                                st.success(f"πŸ”€ **Letter:** {classification['letter']}")
                        with col_b:
                            if classification.get('word'):
                                st.success(f"πŸ“ **Word:** {classification['word']}")

                        if classification.get('confidence'):
                            st.info(f"🎯 **AI Confidence:** {classification['confidence']:.1%}")

def display_video_results(result: Dict[str, Any]):
    """Display results for video processing with enhanced UI."""
    if not result['success']:
        st.error(f"❌ Error processing {result.get('filename', 'file')}: {result.get('error', 'Unknown error')}")
        return

    filename = result.get('filename', 'Unknown')
    file_size = result.get('file_size', 0)
    video_props = result['video_properties']

    # Create result card
    st.markdown(f"""
    <div class="result-card">
        <h3 style="color: #2C3E50 !important; font-weight: 600; margin-bottom: 1rem;">πŸŽ₯ {filename}</h3>
        <p style="color: #2C3E50 !important;"><strong>File Size:</strong> {file_size / (1024*1024):.1f} MB |
           <strong>Duration:</strong> {video_props['duration']:.1f}s |
           <strong>Total Hands:</strong> {result['total_hands_detected']}</p>
    </div>
    """, unsafe_allow_html=True)

    # Video metrics
    col1, col2, col3, col4 = st.columns(4)
    with col1:
        st.metric("Total Frames", video_props['total_frames'])
    with col2:
        st.metric("Processed Frames", video_props['processed_frames'])
    with col3:
        st.metric("FPS", f"{video_props['fps']:.1f}")
    with col4:
        st.metric("Hands Found", result['total_hands_detected'])

    # Frame-by-frame analysis
    if result['frame_detections']:
        st.subheader("πŸ“Š Frame-by-Frame Analysis")

        # Enhanced timeline visualization
        timeline_fig = create_processing_timeline(result['frame_detections'])
        st.plotly_chart(timeline_fig, use_container_width=True, key="video_timeline")

        # Additional analysis charts
        col_chart1, col_chart2 = st.columns(2)

        with col_chart1:
            # Confidence over time
            confidence_data = []
            for frame in result['frame_detections']:
                for detection in frame['detections']:
                    if 'confidence' in detection:
                        confidence_data.append({
                            'Timestamp': frame['timestamp'],
                            'Confidence': detection['confidence'],
                            'Hand': detection['hand_label']
                        })

            if confidence_data:
                conf_df = pd.DataFrame(confidence_data)
                fig_conf = px.scatter(conf_df, x='Timestamp', y='Confidence',
                                    color='Hand', title='Detection Confidence Over Time')
                st.plotly_chart(fig_conf, use_container_width=True, key="confidence_over_time")

        with col_chart2:
            # Hand distribution
            hand_counts = {}
            for frame in result['frame_detections']:
                for detection in frame['detections']:
                    hand_label = detection.get('hand_label', 'Unknown')
                    hand_counts[hand_label] = hand_counts.get(hand_label, 0) + 1

            if hand_counts:
                fig_pie = px.pie(values=list(hand_counts.values()),
                               names=list(hand_counts.keys()),
                               title='Hand Distribution')
                st.plotly_chart(fig_pie, use_container_width=True, key="hand_distribution")

        # Detailed frame results
        st.subheader("πŸ” Detailed Frame Results")

        # Show first 10 frames with detections
        frames_to_show = [f for f in result['frame_detections'] if f['hands_detected'] > 0][:10]

        for frame_data in frames_to_show:
            with st.expander(f"⏱️ Frame {frame_data['frame_number']} (t={frame_data['timestamp']:.1f}s)"):
                for i, detection in enumerate(frame_data['detections']):
                    st.write(f"**βœ‹ {detection['hand_label']} Hand {i+1}**")

                    if 'classification' in detection and detection['classification']['success']:
                        classification = detection['classification']

                        col_a, col_b, col_c = st.columns(3)
                        with col_a:
                            if classification.get('letter'):
                                st.info(f"Letter: **{classification['letter']}**")
                        with col_b:
                            if classification.get('word'):
                                st.info(f"Word: **{classification['word']}**")
                        with col_c:
                            if classification.get('confidence'):
                                st.info(f"Confidence: **{classification['confidence']:.1%}**")

    # Sequence analysis
    if result.get('sequence_analysis') and result['sequence_analysis'].get('success'):
        st.subheader("πŸ”— Sequence Analysis")
        sequence = result['sequence_analysis']

        col1, col2 = st.columns(2)
        with col1:
            if sequence.get('word'):
                st.success(f"🎯 **Detected Word:** {sequence['word']}")
            if sequence.get('sentence'):
                st.success(f"πŸ“ **Detected Sentence:** {sequence['sentence']}")

        with col2:
            if sequence.get('individual_letters'):
                letters_str = ' β†’ '.join(sequence['individual_letters'])
                st.info(f"πŸ”€ **Letter Sequence:** {letters_str}")

            if sequence.get('confidence'):
                st.metric("Sequence Confidence", f"{sequence['confidence']:.1%}")

def export_results(results: List[Dict[str, Any]], format_type: str):
    """Enhanced export functionality with multiple formats."""
    if not results:
        st.warning("No results to export")
        return

    exporter = st.session_state.exporter
    timestamp = int(time.time())

    col1, col2, col3 = st.columns(3)

    with col1:
        if st.button("πŸ“„ Export JSON", use_container_width=True):
            with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as tmp_file:
                if exporter.export_to_json(results, tmp_file.name, include_metadata=True):
                    with open(tmp_file.name, 'r') as f:
                        json_data = f.read()

                    st.download_button(
                        label="πŸ“₯ Download JSON",
                        data=json_data,
                        file_name=f"sign_language_results_{timestamp}.json",
                        mime="application/json",
                        use_container_width=True
                    )
                    os.unlink(tmp_file.name)
                else:
                    st.error("Failed to export JSON")

    with col2:
        if st.button("πŸ“Š Export CSV", use_container_width=True):
            with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp_file:
                if exporter.export_to_csv(results, tmp_file.name):
                    with open(tmp_file.name, 'r') as f:
                        csv_data = f.read()

                    st.download_button(
                        label="πŸ“₯ Download CSV",
                        data=csv_data,
                        file_name=f"sign_language_results_{timestamp}.csv",
                        mime="text/csv",
                        use_container_width=True
                    )
                    os.unlink(tmp_file.name)
                else:
                    st.error("Failed to export CSV")

    with col3:
        if st.button("πŸ“‹ Export PDF Report", use_container_width=True):
            with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
                if exporter.export_to_pdf(results, tmp_file.name, include_images=False):
                    with open(tmp_file.name, 'rb') as f:
                        pdf_data = f.read()

                    st.download_button(
                        label="πŸ“₯ Download PDF",
                        data=pdf_data,
                        file_name=f"sign_language_report_{timestamp}.pdf",
                        mime="application/pdf",
                        use_container_width=True
                    )
                    os.unlink(tmp_file.name)
                else:
                    st.error("Failed to export PDF")

    # Summary report
    if st.button("πŸ“ˆ Generate Summary Report", use_container_width=True):
        summary = exporter.create_summary_report(results)

        st.markdown("### πŸ“Š Processing Summary")

        col_a, col_b, col_c, col_d = st.columns(4)
        with col_a:
            st.metric("Total Files", summary['total_files'])
        with col_b:
            st.metric("Successful", summary['successful_files'])
        with col_c:
            st.metric("Failed", summary['failed_files'])
        with col_d:
            st.metric("Hands Detected", summary['total_hands_detected'])

        if summary['detected_letters']:
            st.markdown("#### πŸ”€ Most Common Letters")
            letters_df = pd.DataFrame(list(summary['detected_letters'].items()),
                                    columns=['Letter', 'Count'])
            letters_df = letters_df.sort_values('Count', ascending=False)

            fig = px.bar(letters_df.head(10), x='Letter', y='Count',
                        title='Top 10 Detected Letters')
            st.plotly_chart(fig, use_container_width=True, key="top_letters_chart")

        if summary['detected_words']:
            st.markdown("#### πŸ“ Most Common Words")
            words_df = pd.DataFrame(list(summary['detected_words'].items()),
                                  columns=['Word', 'Count'])
            words_df = words_df.sort_values('Count', ascending=False)

            fig = px.bar(words_df.head(10), x='Word', y='Count',
                        title='Top 10 Detected Words')
            st.plotly_chart(fig, use_container_width=True, key="top_words_chart")


def get_single_prediction(result: Dict[str, Any]) -> str:
    """
    Extract a single, clear prediction from the result.

    Args:
        result: Processing result dictionary

    Returns:
        Single prediction string (letter, word, or "No prediction")
    """
    if not result.get('success') or not result.get('detections'):
        return "No prediction"

    # Collect all predictions from all detected hands
    letters = []
    words = []

    for detection in result['detections']:
        if 'classification' in detection and detection['classification'].get('success'):
            classification = detection['classification']

            # Get letter prediction
            if classification.get('letter') and classification['letter'] != 'N/A':
                letters.append(classification['letter'])

            # Get word prediction
            if classification.get('word') and classification['word'] != 'N/A':
                words.append(classification['word'])

    # Priority: Word > Letter > No prediction
    if words:
        # Return the most confident word or the first word if multiple
        return words[0].upper()
    elif letters:
        # Return the most confident letter or the first letter if multiple
        return letters[0].upper()
    else:
        return "No prediction"

def display_single_prediction_card(result: Dict[str, Any]):
    """Display a single, clear prediction card for the result."""
    filename = os.path.basename(result.get('file_path', 'Unknown'))
    prediction = get_single_prediction(result)

    # Determine card color based on prediction
    if prediction == "No prediction":
        card_color = "#E74C3C"  # Red for no prediction
        icon = "❌"
        confidence_text = ""
    else:
        card_color = "#27AE60"  # Green for successful prediction
        icon = "βœ…"

        # Get confidence if available
        confidence = 0.0
        for detection in result.get('detections', []):
            if 'classification' in detection and detection['classification'].get('success'):
                conf = detection['classification'].get('confidence', 0)
                if conf > confidence:
                    confidence = conf

        confidence_text = f" (Confidence: {confidence:.1%})" if confidence > 0 else ""

    # Display the prediction card
    st.markdown(f"""
    <div style="
        background: linear-gradient(135deg, {card_color}, {card_color}dd);
        color: white;
        padding: 2rem;
        border-radius: 15px;
        text-align: center;
        margin: 1rem 0;
        box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    ">
        <h2 style="color: white !important; margin-bottom: 1rem; font-size: 2.5rem;">
            {icon} {prediction}
        </h2>
        <p style="color: white !important; font-size: 1.2rem; margin: 0;">
            πŸ“ {filename}{confidence_text}
        </p>
    </div>
    """, unsafe_allow_html=True)

def display_results(results: List[Dict[str, Any]]):
    """Display processing results with enhanced UI."""
    if not results:
        st.info("No results to display")
        return

    # Display Single Predictions First (Most Important)
    st.markdown("## 🎯 **SIGN LANGUAGE PREDICTIONS**")

    # Create a summary table of all predictions
    prediction_data = []
    for result in results:
        filename = os.path.basename(result.get('file_path', 'Unknown'))
        prediction = get_single_prediction(result)

        # Get confidence
        confidence = 0.0
        for detection in result.get('detections', []):
            if 'classification' in detection and detection['classification'].get('success'):
                conf = detection['classification'].get('confidence', 0)
                if conf > confidence:
                    confidence = conf

        prediction_data.append({
            'File': filename,
            'Prediction': prediction,
            'Confidence': f"{confidence:.1%}" if confidence > 0 else "N/A"
        })

    if prediction_data:
        # Display as a clean table
        import pandas as pd
        df = pd.DataFrame(prediction_data)
        st.dataframe(df, use_container_width=True, hide_index=True)

    st.markdown("### Individual Prediction Cards")

    # Show single prediction cards for each file
    for result in results:
        display_single_prediction_card(result)

    # Add separator
    st.markdown("---")

    # Create metrics dashboard
    create_metrics_dashboard(results)

    # Create visualizations
    col1, col2 = st.columns(2)
    with col1:
        create_confidence_chart(results, "main_confidence_chart")
    with col2:
        create_gesture_analysis_chart(results, "main_gesture_analysis_chart")

    # Display individual results
    st.markdown("## πŸ“‹ Detailed Analysis")

    for result in results:
        if result.get('file_type') == 'image':
            display_image_results(result)
        elif result.get('file_type') == 'video':
            display_video_results(result)
        else:
            st.error(f"❌ Failed to process {result.get('filename', 'unknown file')}: {result.get('error', 'Unknown error')}")


def display_quick_summary(results: List[Dict[str, Any]]):
    """Display a quick summary of predictions at the top."""
    if not results:
        return

    predictions = []
    for result in results:
        filename = os.path.basename(result.get('file_path', 'Unknown'))
        prediction = get_single_prediction(result)
        if prediction != "No prediction":
            predictions.append(f"**{filename}** β†’ **{prediction}**")

    if predictions:
        st.success("🎯 **Quick Results:** " + " | ".join(predictions))
    else:
        st.warning("⚠️ No clear predictions found in uploaded files")

def main():
    """Enhanced Streamlit application with modern UI."""
    # Create header
    create_header()

    # Show quick summary if results exist
    if st.session_state.processing_results:
        display_quick_summary(st.session_state.processing_results)

    # Initialize components
    initialize_components()

    # Sidebar configuration
    st.sidebar.markdown("# πŸŽ›οΈ Control Panel")

    # AI API setup
    api_key, use_gemini = setup_ai_api()

    # Settings panel
    settings = create_settings_panel()

    # Main content area
    tab1, tab2, tab3 = st.tabs(["πŸ“ File Processing", "πŸ“Š Analytics", "ℹ️ About"])

    with tab1:
        st.markdown("## πŸ“ File Processing")

        # Enhanced file upload area
        create_file_upload_area()

        # Multiple file uploader
        uploaded_files = st.file_uploader(
            "Choose files",
            type=['jpg', 'jpeg', 'png', 'bmp', 'mp4', 'avi', 'mov', 'mkv'],
            accept_multiple_files=True,
            help="Upload multiple images or videos for batch processing"
        )

        if uploaded_files:
            st.success(f"βœ… {len(uploaded_files)} file(s) uploaded successfully")

            # Show file details
            with st.expander("πŸ“‹ File Details", expanded=True):
                for file in uploaded_files:
                    file_size = len(file.getvalue())
                    st.write(f"β€’ **{file.name}** ({file_size / 1024:.1f} KB)")

            # Process button
            col1, col2, col3 = st.columns([1, 2, 1])
            with col2:
                if st.button("πŸš€ Process All Files", type="primary", use_container_width=True):
                    if not api_key:
                        st.error("❌ Please provide an OpenAI API key to analyze gestures")
                    else:
                        with st.spinner("πŸ”„ Processing files..."):
                            results = process_uploaded_files(uploaded_files, api_key, settings, use_gemini)
                            st.session_state.processing_results = results

                            if results:
                                st.success(f"βœ… Processing complete! {len(results)} files processed.")
                                display_results(results)

                                # Export options
                                st.markdown("### πŸ“€ Export Results")
                                col_a, col_b = st.columns(2)
                                with col_a:
                                    export_results(results, settings['export_format'])
                                with col_b:
                                    if st.button("πŸ—‘οΈ Clear Results"):
                                        st.session_state.processing_results = []
                                        st.experimental_rerun()

        # Display previous results if available
        elif st.session_state.processing_results:
            st.markdown("### πŸ“Š Previous Results")
            display_results(st.session_state.processing_results)

            # Export options
            st.markdown("### πŸ“€ Export Results")
            col_a, col_b = st.columns(2)
            with col_a:
                export_results(st.session_state.processing_results, settings['export_format'])
            with col_b:
                if st.button("πŸ—‘οΈ Clear Results"):
                    st.session_state.processing_results = []
                    st.experimental_rerun()

    with tab2:
        st.markdown("## πŸ“Š Analytics Dashboard")

        if st.session_state.processing_results:
            results = st.session_state.processing_results

            # Overall statistics
            st.markdown("### πŸ“ˆ Overall Statistics")
            create_metrics_dashboard(results)

            # Detailed charts
            st.markdown("### πŸ“Š Detailed Analysis")
            col1, col2 = st.columns(2)

            with col1:
                create_confidence_chart(results, "analytics_confidence_chart")

            with col2:
                create_gesture_analysis_chart(results, "analytics_gesture_analysis_chart")

            # File processing timeline
            st.markdown("### ⏱️ Processing Timeline")
            if results:
                timeline_data = []
                for i, result in enumerate(results):
                    timeline_data.append({
                        'File': result.get('filename', f'File {i+1}'),
                        'Success': result.get('success', False),
                        'Hands': result.get('hands_detected', 0) if result.get('success') else 0,
                        'Size (KB)': result.get('file_size', 0) / 1024
                    })

                df = pd.DataFrame(timeline_data)

                fig = px.scatter(df, x='Size (KB)', y='Hands',
                               color='Success', size='Hands',
                               hover_data=['File'],
                               title='File Size vs Hands Detected')
                st.plotly_chart(fig, use_container_width=True, key="file_size_scatter")
        else:
            st.info("πŸ“Š No data available. Process some files to see analytics.")

    with tab3:
        st.markdown("## ℹ️ About Sign Language Detector Pro")

        col1, col2 = st.columns(2)

        with col1:
            st.markdown("""
            ### 🎯 Features
            - **Advanced File Processing**: Batch analysis of images and videos
            - **AI-Powered Classification**: OpenAI API integration for accurate gesture recognition
            - **Interactive Analytics**: Real-time charts and metrics
            - **Multiple Export Formats**: JSON, CSV, and PDF reports
            - **Professional UI**: Modern, responsive design
            - **Comprehensive Analysis**: Hand landmarks, gesture features, and confidence scores

            ### πŸ”§ How It Works
            1. **Upload Files**: Drag and drop or select multiple files
            2. **Hand Detection**: MediaPipe detects 21 hand landmarks
            3. **Feature Extraction**: Advanced gesture analysis
            4. **AI Classification**: OpenAI interprets gestures
            5. **Results Display**: Interactive charts and detailed analysis
            """)

        with col2:
            st.markdown("""
            ### πŸ“‹ Supported Formats
            **Images:**
            - JPG, JPEG, PNG, BMP

            **Videos:**
            - MP4, AVI, MOV, MKV

            ### βš™οΈ System Requirements
            - Python 3.8+
            - OpenAI API key
            - Modern web browser

            ### πŸš€ Performance
            - Batch processing support
            - Optimized video frame sampling
            - Real-time progress tracking
            - Memory-efficient processing
            """)

        # System information
        st.markdown("### πŸ’» System Information")
        info_col1, info_col2 = st.columns(2)

        with info_col1:
            st.info(f"**Python:** {sys.version.split()[0]}")
            st.info(f"**OpenCV:** {cv2.__version__}")

        with info_col2:
            st.info(f"**Streamlit:** {st.__version__}")
            api_status = "βœ… Configured" if api_key else "❌ Not configured"
            st.info(f"**OpenAI API:** {api_status}")

    # Enhanced footer with improved text visibility
    st.markdown("---")
    st.markdown("""
    <div style="text-align: center; padding: 2rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                border-radius: 15px; color: #FFFFFF !important; margin-top: 2rem; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
        <h4 style="color: #FFFFFF !important; margin-bottom: 1rem; font-weight: 600;">🀟 Sign Language Detector Pro</h4>
        <p style="color: #FFFFFF !important; margin-bottom: 0.5rem; font-size: 1.1rem;">Empowering communication through AI-powered gesture recognition</p>
        <p style="color: #FFFFFF !important; margin: 0; opacity: 0.9;"><small>Built with ❀️ using MediaPipe, OpenAI, and Streamlit</small></p>
    </div>
    """, unsafe_allow_html=True)


if __name__ == "__main__":
    main()