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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
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>CodeTutor Pro - Intelligent Code Explanation Platform</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <style>
    :root {
      --primary: #7c3aed;
      --secondary: #8b5cf6;
      --accent: #c4b5fd;
      --background: #f8fafc;
      --surface: #ffffff;
      --text: #1e293b;
      --text-secondary: #64748b;
      --border: #e2e8f0;
      --success: #10b981;
      --warning: #f59e0b;
      --error: #ef4444;
      --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
      --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    }

    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
      background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
      color: var(--text);
      line-height: 1.6;
      min-height: 100vh;
      padding: 15px;
      direction: ltr;
      text-align: left;
    }

    /* RTL Support - Comprehensive */
    body.rtl {
      direction: rtl;
      text-align: right;
    }

    body.rtl .form-grid {
      direction: rtl;
    }

    body.rtl .header {
      direction: rtl;
    }

    body.rtl .features-grid {
      direction: rtl;
    }

    body.rtl .settings-bar {
      direction: rtl;
    }

    body.rtl .language-info {
      flex-direction: row-reverse;
    }

    body.rtl .form-group {
      text-align: right;
    }

    body.rtl .landing-form {
      text-align: right;
    }

    body.rtl .landing-form h2 {
      text-align: center;
    }

    body.rtl .api-key-section {
      right: 15px;
      left: auto;
    }

    body.rtl .char-counter {
      text-align: left;
    }

    body.rtl .form-section h3 {
      flex-direction: row-reverse;
    }

    body.rtl .output-section h2 {
      flex-direction: row-reverse;
    }

    body.rtl .generate-btn {
      direction: rtl;
    }

    body.rtl .spinner {
      margin-right: 0;
      margin-left: 8px;
    }

    body.rtl .feature-card .icon {
      direction: ltr; /* Keep emojis in LTR */
    }

    body.rtl .edit-notice {
      direction: rtl;
    }

    body.rtl .footer {
      direction: rtl;
    }

    /* Ensure code areas remain LTR for readability */
    body.rtl #codeInput,
    body.rtl .output-area,
    body.rtl pre,
    body.rtl code {
      direction: ltr;
      text-align: left;
    }

    /* API key inputs should remain LTR */
    body.rtl #apiKey,
    body.rtl #landingApiKey {
      direction: ltr;
      text-align: left;
    }

    /* RTL List Formatting - Fix numbered lists positioning */
    body.rtl ol {
      padding-right: 20px;
      padding-left: 0;
      margin-right: 0;
      margin-left: 0;
    }

    body.rtl ul {
      padding-right: 20px;
      padding-left: 0;
      margin-right: 0;
      margin-left: 0;
    }

    body.rtl li {
      text-align: right;
      direction: rtl;
      margin-right: 0;
      margin-left: 0;
      padding-right: 0;
      padding-left: 0;
    }

    /* Fix numbered list markers positioning in RTL */
    body.rtl ol li {
      list-style-position: inside;
      text-align: right;
    }

    body.rtl ul li {
      list-style-position: inside;
      text-align: right;
    }

    /* Ensure proper spacing for RTL lists */
    body.rtl .output-area ol,
    body.rtl .output-area ul {
      margin: 10px 0;
      padding-right: 25px;
      padding-left: 0;
    }

    body.rtl .output-area li {
      margin-bottom: 5px;
      text-align: right;
      direction: rtl;
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      background: var(--surface);
      border-radius: 16px;
      box-shadow: var(--shadow-lg);
      overflow: hidden;
      position: relative;
    }

    /* Landing Page Styles */
    .landing-page {
      display: block;
      padding: 40px;
      text-align: center;
      min-height: 80vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    .landing-page.hidden {
      display: none;
    }

    .landing-header {
      margin-bottom: 40px;
    }

    .landing-header h1 {
      font-size: 3rem;
      font-weight: 800;
      background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      margin-bottom: 16px;
    }

    .landing-header .subtitle {
      font-size: 1.3rem;
      color: var(--text-secondary);
      font-weight: 500;
      margin-bottom: 12px;
    }

    .landing-header .description {
      font-size: 1.1rem;
      color: var(--text-secondary);
      max-width: 600px;
      margin: 0 auto;
    }

    .landing-form {
      background: var(--surface);
      border: 2px solid var(--border);
      border-radius: 16px;
      padding: 40px;
      max-width: 500px;
      width: 100%;
      box-shadow: var(--shadow-lg);
    }

    .landing-form h2 {
      color: var(--primary);
      font-size: 1.5rem;
      font-weight: 700;
      margin-bottom: 30px;
      text-align: center;
    }

    .form-group {
      margin-bottom: 25px;
      text-align: left;
    }

    .form-group label {
      display: block;
      margin-bottom: 8px;
      font-weight: 600;
      color: var(--text);
      font-size: 1rem;
    }

    .form-group input,
    .form-group select {
      width: 100%;
      padding: 14px 16px;
      border: 2px solid var(--border);
      border-radius: 8px;
      font-size: 1rem;
      font-family: inherit;
      background: var(--surface);
      transition: all 0.2s ease;
    }

    .form-group input:focus,
    .form-group select:focus {
      outline: none;
      border-color: var(--primary);
      box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.1);
    }

    .start-btn {
      background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
      color: white;
      padding: 16px 32px;
      border: none;
      border-radius: 12px;
      font-size: 1.2rem;
      font-weight: 700;
      cursor: pointer;
      transition: all 0.3s ease;
      width: 100%;
      margin-top: 10px;
    }

    .start-btn:hover {
      transform: translateY(-2px);
      box-shadow: 0 12px 24px -8px rgba(124, 58, 237, 0.4);
    }

    .start-btn:disabled {
      opacity: 0.7;
      cursor: not-allowed;
      transform: none;
    }

    /* Main App Styles */
    .main-app {
      display: none;
    }

    .main-app.active {
      display: block;
    }

    /* Language and Settings Bar */
    .settings-bar {
      background: rgba(124, 58, 237, 0.05);
      border-bottom: 1px solid var(--border);
      padding: 15px 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      gap: 15px;
    }

    .language-info {
      display: flex;
      align-items: center;
      gap: 15px;
      font-size: 0.9rem;
      color: var(--text-secondary);
    }

    .change-language-btn,
    .clear-cache-btn {
      background: var(--primary);
      color: white;
      border: none;
      padding: 8px 16px;
      border-radius: 6px;
      font-size: 0.85rem;
      cursor: pointer;
      transition: all 0.2s ease;
    }

    .change-language-btn:hover,
    .clear-cache-btn:hover {
      background: var(--secondary);
      transform: translateY(-1px);
    }

    .clear-cache-btn {
      background: var(--warning);
    }

    .clear-cache-btn:hover {
      background: #d97706;
    }

    /* API Key Section - Static Position Top Left */
    .api-key-section {
      background: rgba(124, 58, 237, 0.05);
      border: 1px solid rgba(124, 58, 237, 0.1);
      border-radius: 8px;
      padding: 12px;
      margin: 15px;
      max-width: 280px;
    }

    .api-key-section label {
      font-size: 12px;
      font-weight: 600;
      color: var(--primary);
      margin-bottom: 6px;
      display: block;
    }

    .api-key-section input {
      width: 100%;
      padding: 8px 12px;
      border: 1px solid var(--border);
      border-radius: 6px;
      font-size: 14px;
      background: var(--surface);
      transition: all 0.2s ease;
      font-family: 'Courier New', monospace;
    }

    .api-key-section input:focus {
      outline: none;
      border-color: var(--primary);
      box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.1);
    }

    /* Main Content */
    .main-content {
      padding: 20px 30px 30px;
    }

    /* Header */
    .header {
      text-align: center;
      margin-bottom: 25px;
      padding-bottom: 20px;
      border-bottom: 2px solid var(--border);
    }

    .header h1 {
      font-size: 2.5rem;
      font-weight: 800;
      background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      margin-bottom: 8px;
    }

    .header .subtitle {
      font-size: 1.1rem;
      color: var(--text-secondary);
      font-weight: 500;
    }

    .header .description {
      font-size: 1rem;
      color: var(--text-secondary);
      margin-top: 12px;
      max-width: 700px;
      margin-left: auto;
      margin-right: auto;
    }

    /* Code Explanation Features Overview */
    .features-overview {
      background: rgba(124, 58, 237, 0.05);
      border: 1px solid rgba(124, 58, 237, 0.1);
      border-radius: 12px;
      padding: 20px;
      margin-bottom: 25px;
    }

    .features-overview h3 {
      color: var(--primary);
      font-size: 1.2rem;
      font-weight: 700;
      margin-bottom: 15px;
      text-align: center;
    }

    .features-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 15px;
    }

    .feature-card {
      background: rgba(124, 58, 237, 0.02);
      border: 1px solid rgba(124, 58, 237, 0.1);
      border-radius: 8px;
      padding: 15px;
      text-align: center;
      transition: all 0.3s ease;
    }

    .feature-card:hover {
      background: rgba(124, 58, 237, 0.05);
      border-color: var(--primary);
      transform: translateY(-2px);
    }

    .feature-card .icon {
      font-size: 1.5rem;
      margin-bottom: 8px;
    }

    .feature-card h4 {
      color: var(--primary);
      font-size: 0.9rem;
      font-weight: 700;
      margin-bottom: 6px;
    }

    .feature-card p {
      font-size: 0.8rem;
      color: var(--text-secondary);
      line-height: 1.4;
    }

    /* Progress Bar */
    .progress-container {
      background: var(--border);
      height: 6px;
      border-radius: 3px;
      margin: 15px 0;
      overflow: hidden;
    }

    .progress-bar {
      width: 0%;
      height: 100%;
      background: linear-gradient(90deg, var(--primary) 0%, var(--secondary) 100%);
      transition: width 0.3s ease;
      border-radius: 3px;
    }

    /* Form Section */
    .form-section {
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 12px;
      padding: 25px;
      margin-bottom: 25px;
      transition: all 0.3s ease;
      box-shadow: var(--shadow);
    }

    .form-section:hover {
      border-color: var(--primary);
      box-shadow: 0 8px 25px -8px rgba(124, 58, 237, 0.3);
    }

    .form-section h3 {
      font-size: 1.2rem;
      font-weight: 700;
      color: var(--primary);
      margin-bottom: 15px;
      display: flex;
      align-items: center;
      gap: 8px;
    }

    /* Form Elements */
    label {
      display: block;
      margin-bottom: 8px;
      font-weight: 600;
      color: var(--text);
      font-size: 0.95rem;
    }

    input[type="text"],
    textarea,
    select {
      width: 100%;
      padding: 12px 16px;
      border: 2px solid var(--border);
      border-radius: 8px;
      font-size: 1rem;
      font-family: inherit;
      background: var(--surface);
      transition: all 0.2s ease;
      resize: vertical;
    }

    input[type="text"]:focus,
    textarea:focus,
    select:focus {
      outline: none;
      border-color: var(--primary);
      box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.1);
    }

    textarea {
      min-height: 200px;
      line-height: 1.6;
      font-family: 'Fira Code', 'Courier New', monospace;
    }

    .char-counter {
      font-size: 0.8rem;
      color: var(--text-secondary);
      text-align: right;
      margin-top: 4px;
    }

    .form-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      gap: 15px;
      margin-top: 15px;
    }

    .form-field {
      display: flex;
      flex-direction: column;
    }

    /* Generate Button */
    .generate-btn {
      background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
      color: white;
      padding: 18px 40px;
      border: none;
      border-radius: 12px;
      font-size: 1.2rem;
      font-weight: 700;
      cursor: pointer;
      transition: all 0.3s ease;
      display: block;
      margin: 30px auto;
      min-width: 320px;
      position: relative;
      overflow: hidden;
    }

    .generate-btn:hover {
      transform: translateY(-2px);
      box-shadow: 0 12px 24px -8px rgba(124, 58, 237, 0.4);
    }

    .generate-btn:active {
      transform: translateY(0);
    }

    .generate-btn:disabled {
      opacity: 0.7;
      cursor: not-allowed;
      transform: none;
    }

    /* Loading Spinner */
    .spinner {
      display: none;
      width: 20px;
      height: 20px;
      border: 2px solid rgba(255, 255, 255, 0.3);
      border-radius: 50%;
      border-top-color: white;
      animation: spin 1s ease-in-out infinite;
      margin-right: 8px;
    }

    @keyframes spin {
      to { transform: rotate(360deg); }
    }

    /* Output Section */
    .output-section {
      margin-top: 30px;
    }

    .output-section h2 {
      font-size: 1.5rem;
      font-weight: 700;
      color: var(--primary);
      margin-bottom: 15px;
      display: flex;
      align-items: center;
      gap: 8px;
      text-align: center;
      justify-content: center;
    }

    .output-area {
      background: var(--surface);
      border: 2px solid var(--border);
      border-radius: 12px;
      padding: 25px;
      min-height: 300px;
      transition: all 0.3s ease;
      position: relative;
      overflow-x: auto;
      font-family: 'Georgia', serif;
      line-height: 1.8;
      white-space: pre-wrap;
      word-wrap: break-word;
    }

    .output-area:empty::before {
      content: "Your detailed code explanation will appear here...";
      color: var(--text-secondary);
      font-style: italic;
      font-family: 'Inter', sans-serif;
    }

    .output-area:focus {
      outline: none;
      border-color: var(--primary);
      box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.1);
    }

    .output-area h2, .output-area h3 {
      color: var(--primary);
      margin: 25px 0 15px 0;
      font-family: 'Inter', sans-serif;
    }

    .output-area h2 {
      border-bottom: 2px solid var(--primary);
      padding-bottom: 8px;
      font-size: 1.4rem;
    }

    .output-area h3 {
      font-size: 1.2rem;
    }

    /* Section Headers with Decorative Underlines - Matching Example Format */
    .output-area .section-header {
      color: #e67e22; /* Orange color like in the example */
      font-size: 1.3rem;
      font-weight: 700;
      margin: 30px 0 20px 0;
      padding-bottom: 10px;
      border-bottom: 3px solid #e67e22;
      text-align: right; /* RTL alignment */
      direction: rtl;
      font-family: 'Inter', sans-serif;
    }

    /* For LTR languages */
    body:not(.rtl) .output-area .section-header {
      text-align: left;
      direction: ltr;
    }

    /* Section content paragraphs */
    .output-area .section-content {
      margin: 15px 0 25px 0;
      line-height: 1.8;
      font-size: 1rem;
      text-align: right;
      direction: rtl;
    }

    /* For LTR languages */
    body:not(.rtl) .output-area .section-content {
      text-align: left;
      direction: ltr;
    }

    /* Better paragraph spacing for the new format */
    .output-area .section-content p {
      margin: 12px 0;
      line-height: 1.8;
    }

    .output-area p {
      margin: 12px 0;
    }

    .output-area strong {
      color: var(--primary);
    }

    .output-area ul, .output-area ol {
      margin: 12px 0 12px 20px;
    }

    .output-area li {
      margin: 6px 0;
    }

    .output-area code {
      background: rgba(124, 58, 237, 0.1);
      padding: 2px 6px;
      border-radius: 4px;
      font-family: 'Fira Code', 'Courier New', monospace;
      color: var(--primary);
      font-size: 0.9em;
    }

    .output-area pre {
      background: rgba(124, 58, 237, 0.05);
      border: 1px solid rgba(124, 58, 237, 0.2);
      border-radius: 8px;
      padding: 16px;
      margin: 16px 0;
      overflow-x: auto;
      font-family: 'Fira Code', 'Courier New', monospace;
      font-size: 0.9em;
      line-height: 1.5;
    }

    .output-area pre code {
      background: none;
      padding: 0;
      border-radius: 0;
      color: var(--text);
      font-size: inherit;
    }

    /* Edit Notice */
    .edit-notice {
      background: rgba(124, 58, 237, 0.1);
      border: 1px solid rgba(124, 58, 237, 0.2);
      color: var(--primary);
      padding: 8px 12px;
      border-radius: 6px;
      margin-bottom: 10px;
      font-size: 0.85rem;
      text-align: center;
      font-weight: 500;
    }

    /* Error Messages */
    .error-message {
      background: rgba(239, 68, 68, 0.1);
      border: 1px solid rgba(239, 68, 68, 0.2);
      color: var(--error);
      padding: 12px 16px;
      border-radius: 8px;
      margin: 10px 0;
      font-size: 0.9rem;
      display: none;
    }

    /* Status Messages */
    .status-message {
      background: rgba(124, 58, 237, 0.1);
      border: 1px solid rgba(124, 58, 237, 0.2);
      color: var(--primary);
      padding: 12px 16px;
      border-radius: 8px;
      margin: 10px 0;
      font-size: 0.9rem;
      text-align: center;
      font-weight: 600;
    }

    /* Footer */
    .footer {
      text-align: center;
      padding: 20px;
      background: rgba(124, 58, 237, 0.05);
      border-top: 1px solid var(--border);
      color: var(--text-secondary);
      font-size: 0.9rem;
    }

    /* Responsive Design */
    @media (max-width: 768px) {
      body {
        padding: 10px;
      }

      .landing-page {
        padding: 20px;
      }

      .landing-header h1 {
        font-size: 2.2rem;
      }

      .landing-form {
        padding: 30px 20px;
      }

      .settings-bar {
        padding: 12px 20px;
        flex-direction: column;
        align-items: stretch;
      }

      .language-info {
        justify-content: center;
        margin-bottom: 10px;
      }

      .api-key-section {
        margin: 10px;
        max-width: none;
      }

      .main-content {
        padding: 15px 20px 20px;
      }

      .header h1 {
        font-size: 2rem;
      }

      .form-section {
        padding: 20px 15px;
      }

      .features-grid {
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
      }

      .form-grid {
        grid-template-columns: 1fr;
      }

      .generate-btn {
        width: 100%;
        margin: 25px 0;
        padding: 16px 32px;
        font-size: 1.1rem;
      }

      .output-area {
        padding: 20px;
      }
    }

    @media (max-width: 480px) {
      .landing-header h1 {
        font-size: 1.8rem;
      }

      .header h1 {
        font-size: 1.8rem;
      }

      .features-grid {
        grid-template-columns: 1fr;
      }

      .feature-card {
        padding: 12px;
      }
    }

    /* Dark mode support */
    @media (prefers-color-scheme: dark) {
      :root {
        --background: #0f172a;
        --surface: #1e293b;
        --text: #f1f5f9;
        --text-secondary: #94a3b8;
        --border: #334155;
      }

      body {
        background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
      }
    }

    /* Translation loading indicator */
    .translation-loading {
      opacity: 0.6;
      pointer-events: none;
    }

    .translation-loading::after {
      content: "🔄";
      position: absolute;
      top: 10px;
      right: 10px;
      animation: spin 1s linear infinite;
    }
  </style>
</head>
<body>
  <!-- Landing Page -->
  <div class="landing-page" id="landingPage">
    <div class="landing-header">
      <h1 data-translate="app_title">CodeTutor Pro</h1>
      <p class="subtitle" data-translate="app_subtitle">Intelligent Code Explanation Platform</p>
      <p class="description" data-translate="app_description">Transform complex code into clear, beginner-friendly explanations with AI-powered analysis</p>
    </div>

    <div class="landing-form">
      <h2 data-translate="setup_title">Get Started</h2>
      
      <div class="form-group">
        <label for="languageSelect" data-translate="language_label">Select Language:</label>
        <select id="languageSelect" required>
          <option value="">Choose your language...</option>
          <!-- Languages will be populated by JavaScript -->
        </select>
      </div>

      <div class="form-group">
        <label for="landingApiKey" data-translate="api_key_label">OpenAI API Key:</label>
        <input type="password" id="landingApiKey" data-translate-placeholder="api_key_placeholder" placeholder="Enter your OpenAI API key" autocomplete="off" required>
      </div>

      <button class="start-btn" id="startBtn" data-translate="start_button">Start Using CodeTutor Pro</button>
    </div>
  </div>

  <!-- Main Application -->
  <div class="main-app" id="mainApp">
    <!-- Settings Bar -->
    <div class="settings-bar">
      <div class="language-info">
        <span data-translate="current_language">Language:</span>
        <strong id="currentLanguageName">English</strong>
        <button class="change-language-btn" id="changeLanguageBtn" data-translate="change_language">Change Language</button>
      </div>
      <div>
        <button class="clear-cache-btn" id="clearCacheBtn" data-translate="clear_cache">Clear Translation Cache</button>
      </div>
    </div>

    <div class="container">
      <!-- API Key Section - Static Position Top Left -->
      <div class="api-key-section">
        <label for="apiKey" data-translate="api_key_short">🔑 API Key</label>
        <input type="password" id="apiKey" data-translate-placeholder="api_key_placeholder" placeholder="Enter your OpenAI API key" autocomplete="off">
      </div>

      <div class="main-content">
        <!-- Header -->
        <div class="header">
          <h1 data-translate="app_title">CodeTutor Pro</h1>
          <p class="subtitle" data-translate="app_subtitle">Intelligent Code Explanation Platform</p>
          <p class="description" data-translate="app_description">Transform complex code into clear, beginner-friendly explanations with AI-powered analysis</p>
        </div>

        <!-- Code Explanation Features Overview -->
        <div class="features-overview">
          <h3 data-translate="features_title">💡 Advanced Code Explanation Features</h3>
          <div class="features-grid">
            <div class="feature-card">
              <div class="icon">🎯</div>
              <h4 data-translate="feature_beginner_title">Beginner-Friendly</h4>
              <p data-translate="feature_beginner_desc">Plain language explanations without technical jargon</p>
            </div>
            <div class="feature-card">
              <div class="icon">🔍</div>
              <h4 data-translate="feature_analysis_title">Line-by-Line Analysis</h4>
              <p data-translate="feature_analysis_desc">Detailed breakdown of each code component</p>
            </div>
            <div class="feature-card">
              <div class="icon">📚</div>
              <h4 data-translate="feature_concept_title">Concept Explanation</h4>
              <p data-translate="feature_concept_desc">Clear examples and programming concepts</p>
            </div>
            <div class="feature-card">
              <div class="icon">✏️</div>
              <h4 data-translate="feature_editable_title">Editable Output</h4>
              <p data-translate="feature_editable_desc">Customize and refine explanations as needed</p>
            </div>
            <div class="feature-card">
              <div class="icon">🌐</div>
              <h4 data-translate="feature_multilang_title">Multi-Language Support</h4>
              <p data-translate="feature_multilang_desc">Works with all major programming languages</p>
            </div>
          </div>
        </div>

        <!-- Progress Bar -->
        <div class="progress-container">
          <div class="progress-bar" id="progressBar"></div>
        </div>

        <!-- Form Section -->
        <div class="form-section">
          <h3 data-translate="code_input_title">💻 Code Input & Analysis</h3>
          
          <label for="codeInput" data-translate="code_input_label">Enter Your Code:</label>
          <textarea 
            id="codeInput" 
            data-translate-placeholder="code_input_placeholder"
            placeholder="Paste your code here... (supports all programming languages: Python, JavaScript, Java, C++, HTML, CSS, SQL, etc.)"
            maxlength="1000000"
            required
          ></textarea>
          <div class="char-counter">
            <span id="charCount">0</span>/1000000 <span data-translate="characters">characters</span>
          </div>

          <div class="form-grid">
            <div class="form-field">
              <label for="programmingLanguage" data-translate="programming_language_label">Programming Language (Optional):</label>
              <select id="programmingLanguage">
                <option value="" data-translate="auto_detect">Auto-detect language</option>
                <option value="Python">Python</option>
                <option value="JavaScript">JavaScript</option>
                <option value="Java">Java</option>
                <option value="C++">C++</option>
                <option value="C#">C#</option>
                <option value="HTML">HTML</option>
                <option value="CSS">CSS</option>
                <option value="SQL">SQL</option>
                <option value="PHP">PHP</option>
                <option value="Ruby">Ruby</option>
                <option value="Go">Go</option>
                <option value="Rust">Rust</option>
                <option value="Swift">Swift</option>
                <option value="Kotlin">Kotlin</option>
                <option value="TypeScript">TypeScript</option>
                <option value="R">R</option>
                <option value="MATLAB">MATLAB</option>
                <option value="Scala">Scala</option>
                <option value="Perl">Perl</option>
                <option value="Shell/Bash">Shell/Bash</option>
              </select>
            </div>
            
            <div class="form-field">
              <label for="explanationLevel" data-translate="explanation_level_label">Explanation Level:</label>
              <select id="explanationLevel" required>
                <option value="" data-translate="select_explanation_level">Select explanation level</option>
                <option value="Complete Beginner" data-translate="level_complete_beginner">Complete Beginner (No programming experience)</option>
                <option value="Beginner" data-translate="level_beginner">Beginner (Basic programming knowledge)</option>
                <option value="Intermediate" data-translate="level_intermediate">Intermediate (Some programming experience)</option>
                <option value="Advanced" data-translate="level_advanced">Advanced (Experienced programmer)</option>
              </select>
            </div>
          </div>
        </div>

        <!-- Error Message -->
        <div class="error-message" id="errorMessage"></div>

        <!-- Status Message -->
        <div class="status-message" id="statusMessage" style="display: none;"></div>

        <!-- Generate Button -->
        <button class="generate-btn" id="generateBtn">
          <span class="spinner" id="spinner"></span>
          <span id="buttonText" data-translate="generate_button">🧠 Generate Explanation</span>
        </button>

        <!-- Output Section -->
        <div class="output-section">
          <h2 data-translate="output_title">📖 Code Explanation</h2>
          <div class="edit-notice" data-translate="edit_notice">
            ✏️ This explanation is editable - click anywhere to customize and refine the content
          </div>
          <div class="output-area" id="explanationOutput" contenteditable="true"></div>
        </div>
      </div>

      <!-- Footer -->
      <div class="footer" data-translate="footer">
        Created by Shift Mind AI Labs
      </div>
    </div>
  </div>

  <script>
    // Global variables
    let currentLanguage = 'en';
    let translationCache = {};
    let isTranslating = false;

    // 92 Languages with their codes and RTL information
    const languages = [
      { code: 'en', name: 'English', nativeName: 'English', rtl: false },
      { code: 'es', name: 'Spanish', nativeName: 'Español', rtl: false },
      { code: 'fr', name: 'French', nativeName: 'Français', rtl: false },
      { code: 'de', name: 'German', nativeName: 'Deutsch', rtl: false },
      { code: 'it', name: 'Italian', nativeName: 'Italiano', rtl: false },
      { code: 'pt', name: 'Portuguese', nativeName: 'Português', rtl: false },
      { code: 'ru', name: 'Russian', nativeName: 'Русский', rtl: false },
      { code: 'ja', name: 'Japanese', nativeName: '日本語', rtl: false },
      { code: 'ko', name: 'Korean', nativeName: '한국어', rtl: false },
      { code: 'zh', name: 'Chinese (Simplified)', nativeName: '中文 (简体)', rtl: false },
      { code: 'zh-TW', name: 'Chinese (Traditional)', nativeName: '中文 (繁體)', rtl: false },
      { code: 'ar', name: 'Arabic', nativeName: 'العربية', rtl: true },
      { code: 'he', name: 'Hebrew', nativeName: 'עברית', rtl: true },
      { code: 'hi', name: 'Hindi', nativeName: 'हिन्दी', rtl: false },
      { code: 'th', name: 'Thai', nativeName: 'ไทย', rtl: false },
      { code: 'vi', name: 'Vietnamese', nativeName: 'Tiếng Việt', rtl: false },
      { code: 'tr', name: 'Turkish', nativeName: 'Türkçe', rtl: false },
      { code: 'pl', name: 'Polish', nativeName: 'Polski', rtl: false },
      { code: 'nl', name: 'Dutch', nativeName: 'Nederlands', rtl: false },
      { code: 'sv', name: 'Swedish', nativeName: 'Svenska', rtl: false },
      { code: 'da', name: 'Danish', nativeName: 'Dansk', rtl: false },
      { code: 'no', name: 'Norwegian', nativeName: 'Norsk', rtl: false },
      { code: 'fi', name: 'Finnish', nativeName: 'Suomi', rtl: false },
      { code: 'cs', name: 'Czech', nativeName: 'Čeština', rtl: false },
      { code: 'sk', name: 'Slovak', nativeName: 'Slovenčina', rtl: false },
      { code: 'hu', name: 'Hungarian', nativeName: 'Magyar', rtl: false },
      { code: 'ro', name: 'Romanian', nativeName: 'Română', rtl: false },
      { code: 'bg', name: 'Bulgarian', nativeName: 'Български', rtl: false },
      { code: 'hr', name: 'Croatian', nativeName: 'Hrvatski', rtl: false },
      { code: 'sr', name: 'Serbian', nativeName: 'Српски', rtl: false },
      { code: 'sl', name: 'Slovenian', nativeName: 'Slovenščina', rtl: false },
      { code: 'et', name: 'Estonian', nativeName: 'Eesti', rtl: false },
      { code: 'lv', name: 'Latvian', nativeName: 'Latviešu', rtl: false },
      { code: 'lt', name: 'Lithuanian', nativeName: 'Lietuvių', rtl: false },
      { code: 'uk', name: 'Ukrainian', nativeName: 'Українська', rtl: false },
      { code: 'be', name: 'Belarusian', nativeName: 'Беларуская', rtl: false },
      { code: 'mk', name: 'Macedonian', nativeName: 'Македонски', rtl: false },
      { code: 'sq', name: 'Albanian', nativeName: 'Shqip', rtl: false },
      { code: 'el', name: 'Greek', nativeName: 'Ελληνικά', rtl: false },
      { code: 'mt', name: 'Maltese', nativeName: 'Malti', rtl: false },
      { code: 'ga', name: 'Irish', nativeName: 'Gaeilge', rtl: false },
      { code: 'cy', name: 'Welsh', nativeName: 'Cymraeg', rtl: false },
      { code: 'is', name: 'Icelandic', nativeName: 'Íslenska', rtl: false },
      { code: 'fo', name: 'Faroese', nativeName: 'Føroyskt', rtl: false },
      { code: 'eu', name: 'Basque', nativeName: 'Euskera', rtl: false },
      { code: 'ca', name: 'Catalan', nativeName: 'Català', rtl: false },
      { code: 'gl', name: 'Galician', nativeName: 'Galego', rtl: false },
      { code: 'oc', name: 'Occitan', nativeName: 'Occitan', rtl: false },
      { code: 'br', name: 'Breton', nativeName: 'Brezhoneg', rtl: false },
      { code: 'co', name: 'Corsican', nativeName: 'Corsu', rtl: false },
      { code: 'sc', name: 'Sardinian', nativeName: 'Sardu', rtl: false },
      { code: 'rm', name: 'Romansh', nativeName: 'Rumantsch', rtl: false },
      { code: 'lb', name: 'Luxembourgish', nativeName: 'Lëtzebuergesch', rtl: false },
      { code: 'fy', name: 'Frisian', nativeName: 'Frysk', rtl: false },
      { code: 'af', name: 'Afrikaans', nativeName: 'Afrikaans', rtl: false },
      { code: 'zu', name: 'Zulu', nativeName: 'isiZulu', rtl: false },
      { code: 'xh', name: 'Xhosa', nativeName: 'isiXhosa', rtl: false },
      { code: 'st', name: 'Sesotho', nativeName: 'Sesotho', rtl: false },
      { code: 'tn', name: 'Setswana', nativeName: 'Setswana', rtl: false },
      { code: 'ss', name: 'Swati', nativeName: 'siSwati', rtl: false },
      { code: 've', name: 'Venda', nativeName: 'Tshivenḓa', rtl: false },
      { code: 'ts', name: 'Tsonga', nativeName: 'Xitsonga', rtl: false },
      { code: 'nr', name: 'Ndebele', nativeName: 'isiNdebele', rtl: false },
      { code: 'sw', name: 'Swahili', nativeName: 'Kiswahili', rtl: false },
      { code: 'am', name: 'Amharic', nativeName: 'አማርኛ', rtl: false },
      { code: 'ti', name: 'Tigrinya', nativeName: 'ትግርኛ', rtl: false },
      { code: 'om', name: 'Oromo', nativeName: 'Afaan Oromoo', rtl: false },
      { code: 'so', name: 'Somali', nativeName: 'Soomaali', rtl: false },
      { code: 'ha', name: 'Hausa', nativeName: 'Hausa', rtl: false },
      { code: 'yo', name: 'Yoruba', nativeName: 'Yorùbá', rtl: false },
      { code: 'ig', name: 'Igbo', nativeName: 'Igbo', rtl: false },
      { code: 'ff', name: 'Fulah', nativeName: 'Fulfulde', rtl: false },
      { code: 'wo', name: 'Wolof', nativeName: 'Wolof', rtl: false },
      { code: 'bm', name: 'Bambara', nativeName: 'Bamanankan', rtl: false },
      { code: 'rw', name: 'Kinyarwanda', nativeName: 'Ikinyarwanda', rtl: false },
      { code: 'rn', name: 'Kirundi', nativeName: 'Ikirundi', rtl: false },
      { code: 'ny', name: 'Chichewa', nativeName: 'Chichewa', rtl: false },
      { code: 'sn', name: 'Shona', nativeName: 'chiShona', rtl: false },
      { code: 'mg', name: 'Malagasy', nativeName: 'Malagasy', rtl: false },
      { code: 'fa', name: 'Persian', nativeName: 'فارسی', rtl: true },
      { code: 'ur', name: 'Urdu', nativeName: 'اردو', rtl: true },
      { code: 'ps', name: 'Pashto', nativeName: 'پښتو', rtl: true },
      { code: 'ku', name: 'Kurdish', nativeName: 'Kurdî', rtl: true },
      { code: 'ckb', name: 'Sorani Kurdish', nativeName: 'کوردی', rtl: true },
      { code: 'sd', name: 'Sindhi', nativeName: 'سنڌي', rtl: true },
      { code: 'ug', name: 'Uyghur', nativeName: 'ئۇيغۇرچە', rtl: true },
      { code: 'bn', name: 'Bengali', nativeName: 'বাংলা', rtl: false },
      { code: 'gu', name: 'Gujarati', nativeName: 'ગુજરાતી', rtl: false },
      { code: 'pa', name: 'Punjabi', nativeName: 'ਪੰਜਾਬੀ', rtl: false },
      { code: 'ta', name: 'Tamil', nativeName: 'தமிழ்', rtl: false },
      { code: 'te', name: 'Telugu', nativeName: 'తెలుగు', rtl: false },
      { code: 'kn', name: 'Kannada', nativeName: 'ಕನ್ನಡ', rtl: false },
      { code: 'ml', name: 'Malayalam', nativeName: 'മലയാളം', rtl: false },
      { code: 'si', name: 'Sinhala', nativeName: 'සිංහල', rtl: false },
      { code: 'my', name: 'Myanmar', nativeName: 'မြန်မာ', rtl: false },
      { code: 'km', name: 'Khmer', nativeName: 'ខ្មែរ', rtl: false },
      { code: 'lo', name: 'Lao', nativeName: 'ລາວ', rtl: false },
      { code: 'ka', name: 'Georgian', nativeName: 'ქართული', rtl: false },
      { code: 'hy', name: 'Armenian', nativeName: 'Հայերեն', rtl: false },
      { code: 'az', name: 'Azerbaijani', nativeName: 'Azərbaycan', rtl: false },
      { code: 'kk', name: 'Kazakh', nativeName: 'Қазақ', rtl: false },
      { code: 'ky', name: 'Kyrgyz', nativeName: 'Кыргыз', rtl: false },
      { code: 'uz', name: 'Uzbek', nativeName: 'Oʻzbek', rtl: false },
      { code: 'tk', name: 'Turkmen', nativeName: 'Türkmen', rtl: false },
      { code: 'tg', name: 'Tajik', nativeName: 'Тоҷикӣ', rtl: false },
      { code: 'mn', name: 'Mongolian', nativeName: 'Монгол', rtl: false }
    ];

    // Default translations for English
    const defaultTranslations = {
      app_title: "CodeTutor Pro",
      app_subtitle: "Intelligent Code Explanation Platform",
      app_description: "Transform complex code into clear, beginner-friendly explanations with AI-powered analysis",
      setup_title: "Get Started",
      language_label: "Select Language:",
      api_key_label: "OpenAI API Key:",
      api_key_placeholder: "Enter your OpenAI API key",
      start_button: "Start Using CodeTutor Pro",
      current_language: "Language:",
      change_language: "Change Language",
      clear_cache: "Clear Translation Cache",
      api_key_short: "🔑 API Key",
      features_title: "💡 Advanced Code Explanation Features",
      feature_beginner_title: "Beginner-Friendly",
      feature_beginner_desc: "Plain language explanations without technical jargon",
      feature_analysis_title: "Line-by-Line Analysis",
      feature_analysis_desc: "Detailed breakdown of each code component",
      feature_concept_title: "Concept Explanation",
      feature_concept_desc: "Clear examples and programming concepts",
      feature_editable_title: "Editable Output",
      feature_editable_desc: "Customize and refine explanations as needed",
      feature_multilang_title: "Multi-Language Support",
      feature_multilang_desc: "Works with all major programming languages",
      code_input_title: "💻 Code Input & Analysis",
      code_input_label: "Enter Your Code:",
      code_input_placeholder: "Paste your code here... (supports all programming languages: Python, JavaScript, Java, C++, HTML, CSS, SQL, etc.)",
      characters: "characters",
      programming_language_label: "Programming Language (Optional):",
      auto_detect: "Auto-detect language",
      explanation_level_label: "Explanation Level:",
      select_explanation_level: "Select explanation level",
      level_complete_beginner: "Complete Beginner (No programming experience)",
      level_beginner: "Beginner (Basic programming knowledge)",
      level_intermediate: "Intermediate (Some programming experience)",
      level_advanced: "Advanced (Experienced programmer)",
      generate_button: "🧠 Generate Explanation",
      output_title: "📖 Code Explanation",
      edit_notice: "✏️ This explanation is editable - click anywhere to customize and refine the content",
      footer: "Created by Shift Mind AI Labs"
    };

    // Load cached translations and settings with improved cache handling
    function loadSettings() {
      const savedLanguage = localStorage.getItem('codetutor_language');
      const savedCache = localStorage.getItem('codetutor_translation_cache');
      const savedApiKey = localStorage.getItem('codetutor_api_key');

      if (savedLanguage) {
        currentLanguage = savedLanguage;
      }

      if (savedCache) {
        try {
          const parsedCache = JSON.parse(savedCache);
          // Handle both old and new cache formats
          translationCache = {};
          Object.keys(parsedCache).forEach(key => {
            const value = parsedCache[key];
            if (typeof value === 'string') {
              // Old format - convert to new format
              translationCache[key] = {
                text: value,
                timestamp: Date.now() - (7 * 24 * 60 * 60 * 1000) // Mark as 7 days old
              };
            } else if (typeof value === 'object' && value.text) {
              // New format
              translationCache[key] = value;
            }
          });
        } catch (e) {
          console.error('Error loading translation cache:', e);
          translationCache = {};
        }
      }

      if (savedApiKey) {
        document.getElementById('landingApiKey').value = savedApiKey;
        document.getElementById('apiKey').value = savedApiKey;
      }
    }

    // Save settings with cache size management
    function saveSettings() {
      localStorage.setItem('codetutor_language', currentLanguage);
      
      // Limit cache size to prevent localStorage overflow
      const cacheKeys = Object.keys(translationCache);
      if (cacheKeys.length > 1000) {
        // Keep only the 800 most recent entries
        const sortedEntries = cacheKeys
          .map(key => ({ key, timestamp: translationCache[key].timestamp || 0 }))
          .sort((a, b) => b.timestamp - a.timestamp)
          .slice(0, 800);
        
        const newCache = {};
        sortedEntries.forEach(entry => {
          newCache[entry.key] = translationCache[entry.key];
        });
        translationCache = newCache;
      }
      
      localStorage.setItem('codetutor_translation_cache', JSON.stringify(translationCache));
    }

    // Populate language dropdown
    function populateLanguageDropdown() {
      const select = document.getElementById('languageSelect');
      select.innerHTML = '<option value="">Choose your language...</option>';
      
      languages.forEach(lang => {
        const option = document.createElement('option');
        option.value = lang.code;
        option.textContent = `${lang.nativeName} (${lang.name})`;
        if (lang.code === currentLanguage) {
          option.selected = true;
        }
        select.appendChild(option);
      });
    }

    // Set RTL/LTR direction
    function setTextDirection(langCode) {
      const language = languages.find(lang => lang.code === langCode);
      const body = document.body;
      
      if (language && language.rtl) {
        body.classList.add('rtl');
        body.setAttribute('dir', 'rtl');
      } else {
        body.classList.remove('rtl');
        body.setAttribute('dir', 'ltr');
      }
    }

    // Translation function using OpenAI API with improved caching and batch processing
    async function translateText(text, targetLanguage) {
      if (targetLanguage === 'en' || !text || text.trim() === '') {
        return text;
      }

      const cacheKey = `${text.trim()}_${targetLanguage}`;
      if (translationCache[cacheKey]) {
        const cached = translationCache[cacheKey];
        return typeof cached === 'string' ? cached : cached.text;
      }

      const apiKey = document.getElementById('landingApiKey').value || document.getElementById('apiKey').value;
      if (!apiKey) {
        console.warn('No API key available for translation');
        return text;
      }

      try {
        const language = languages.find(lang => lang.code === targetLanguage);
        const languageName = language ? language.name : targetLanguage;

        const prompt = `Translate the following text to ${languageName}. Maintain any HTML tags, emojis, and formatting. Only return the translated text, nothing else:\n\n${text}`;

        const response = await fetch("https://api.openai.com/v1/chat/completions", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${apiKey}`
          },
          body: JSON.stringify({
            model: "gpt-4o-mini",
            messages: [{ role: "user", content: prompt }],
            max_tokens: 800,
            temperature: 0.2
          })
        });

        if (!response.ok) {
          const errorData = await response.json();
          throw new Error(errorData.error?.message || 'Translation API request failed');
        }

        const data = await response.json();
        const translatedText = data.choices[0].message.content.trim();
        
        // Cache the translation with timestamp
        translationCache[cacheKey] = {
          text: translatedText,
          timestamp: Date.now()
        };
        saveSettings();
        
        return translatedText;
      } catch (error) {
        console.error('Translation error:', error);
        // Try to find a similar cached translation as fallback
        const similarKey = Object.keys(translationCache).find(key => 
          key.includes(text.substring(0, 20)) && key.endsWith(`_${targetLanguage}`)
        );
        if (similarKey && translationCache[similarKey]) {
          return translationCache[similarKey].text || translationCache[similarKey];
        }
        return text; // Return original text if translation fails
      }
    }

    // Batch translation for better performance
    async function translateBatch(textArray, targetLanguage) {
      if (targetLanguage === 'en') {
        return textArray;
      }

      const results = [];
      const uncachedTexts = [];
      const uncachedIndices = [];

      // Check cache first
      textArray.forEach((text, index) => {
        const cacheKey = `${text.trim()}_${targetLanguage}`;
        if (translationCache[cacheKey]) {
          const cached = translationCache[cacheKey];
          results[index] = typeof cached === 'string' ? cached : cached.text;
        } else {
          uncachedTexts.push(text);
          uncachedIndices.push(index);
        }
      });

      // Translate uncached texts
      if (uncachedTexts.length > 0) {
        const apiKey = document.getElementById('landingApiKey').value || document.getElementById('apiKey').value;
        if (!apiKey) {
          // Fill remaining with original texts
          uncachedIndices.forEach((index, i) => {
            results[index] = uncachedTexts[i];
          });
          return results;
        }

        try {
          const language = languages.find(lang => lang.code === targetLanguage);
          const languageName = language ? language.name : targetLanguage;

          const batchText = uncachedTexts.map((text, i) => `${i + 1}. ${text}`).join('\n');
          const prompt = `Translate the following numbered list to ${languageName}. Maintain the numbering, HTML tags, emojis, and formatting. Return only the translated numbered list:\n\n${batchText}`;

          const response = await fetch("https://api.openai.com/v1/chat/completions", {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              "Authorization": `Bearer ${apiKey}`
            },
            body: JSON.stringify({
              model: "gpt-4o-mini",
              messages: [{ role: "user", content: prompt }],
              max_tokens: 2000,
              temperature: 0.2
            })
          });

          if (response.ok) {
            const data = await response.json();
            const translatedBatch = data.choices[0].message.content.trim();
            const translatedLines = translatedBatch.split('\n').filter(line => line.trim());

            translatedLines.forEach((line, i) => {
              if (i < uncachedTexts.length) {
                const translatedText = line.replace(/^\d+\.\s*/, '').trim();
                const originalIndex = uncachedIndices[i];
                results[originalIndex] = translatedText;

                // Cache the translation
                const cacheKey = `${uncachedTexts[i].trim()}_${targetLanguage}`;
                translationCache[cacheKey] = {
                  text: translatedText,
                  timestamp: Date.now()
                };
              }
            });

            saveSettings();
          } else {
            // Fallback to individual translations
            for (let i = 0; i < uncachedTexts.length; i++) {
              const translatedText = await translateText(uncachedTexts[i], targetLanguage);
              results[uncachedIndices[i]] = translatedText;
            }
          }
        } catch (error) {
          console.error('Batch translation error:', error);
          // Fallback to original texts
          uncachedIndices.forEach((index, i) => {
            results[index] = uncachedTexts[i];
          });
        }
      }

      return results;
    }

    // Clean old cache entries (older than 30 days)
    function cleanOldCache() {
      const thirtyDaysAgo = Date.now() - (30 * 24 * 60 * 60 * 1000);
      let cleaned = false;

      Object.keys(translationCache).forEach(key => {
        const cached = translationCache[key];
        if (typeof cached === 'object' && cached.timestamp && cached.timestamp < thirtyDaysAgo) {
          delete translationCache[key];
          cleaned = true;
        }
      });

      if (cleaned) {
        saveSettings();
      }
    }

    // Apply translations to the page with improved batch processing
    async function applyTranslations(langCode) {
      if (isTranslating) return;
      isTranslating = true;

      const elements = document.querySelectorAll('[data-translate]');
      const placeholderElements = document.querySelectorAll('[data-translate-placeholder]');
      
      // Add loading indicator
      document.body.classList.add('translation-loading');

      // Show translation progress
      const progressElement = document.getElementById('progressBar');
      if (progressElement) {
        progressElement.style.width = '10%';
      }

      try {
        // Clean old cache entries
        cleanOldCache();

        if (langCode === 'en') {
          // Apply English (default) translations
          elements.forEach(element => {
            const key = element.getAttribute('data-translate');
            const originalText = defaultTranslations[key] || element.textContent;
            element.textContent = originalText;
          });

          placeholderElements.forEach(element => {
            const key = element.getAttribute('data-translate-placeholder');
            if (key === 'api_key_placeholder') {
              element.setAttribute('placeholder', 'Enter your OpenAI API key');
            } else if (key === 'code_input_placeholder') {
              element.setAttribute('placeholder', 'Paste your code here... (supports all programming languages: Python, JavaScript, Java, C++, HTML, CSS, SQL, etc.)');
            }
          });
        } else {
          // Collect all texts to translate
          const textsToTranslate = [];
          const elementMap = [];

          elements.forEach(element => {
            const key = element.getAttribute('data-translate');
            const originalText = defaultTranslations[key] || element.textContent;
            textsToTranslate.push(originalText);
            elementMap.push({ element, type: 'text' });
          });

          placeholderElements.forEach(element => {
            const originalPlaceholder = element.getAttribute('placeholder');
            if (originalPlaceholder) {
              textsToTranslate.push(originalPlaceholder);
              elementMap.push({ element, type: 'placeholder' });
            }
          });

          if (progressElement) {
            progressElement.style.width = '30%';
          }

          // Batch translate all texts
          const translatedTexts = await translateBatch(textsToTranslate, langCode);

          if (progressElement) {
            progressElement.style.width = '70%';
          }

          // Apply translations
          translatedTexts.forEach((translatedText, index) => {
            const mapping = elementMap[index];
            if (mapping.type === 'text') {
              mapping.element.textContent = translatedText;
            } else if (mapping.type === 'placeholder') {
              mapping.element.setAttribute('placeholder', translatedText);
            }
          });
        }

        if (progressElement) {
          progressElement.style.width = '90%';
        }

        // Update current language display
        const language = languages.find(lang => lang.code === langCode);
        if (language) {
          const currentLangElement = document.getElementById('currentLanguageName');
          if (currentLangElement) {
            currentLangElement.textContent = language.nativeName;
          }
        }

        if (progressElement) {
          progressElement.style.width = '100%';
          setTimeout(() => {
            progressElement.style.width = '0%';
          }, 1000);
        }

      } catch (error) {
        console.error('Error applying translations:', error);
        // Show error message to user
        const errorDiv = document.getElementById('errorMessage');
        if (errorDiv) {
          errorDiv.textContent = `Translation error: ${error.message}`;
          errorDiv.style.display = 'block';
          setTimeout(() => {
            errorDiv.style.display = 'none';
          }, 5000);
        }
      } finally {
        document.body.classList.remove('translation-loading');
        isTranslating = false;
      }
    }

    // Initialize the application with improved loading states
    function initializeApp() {
      loadSettings();
      populateLanguageDropdown();
      setTextDirection(currentLanguage);

      // If language is already selected, show main app
      if (currentLanguage !== 'en' || localStorage.getItem('codetutor_language')) {
        showMainApp();
        
        // Show loading state while translating
        const statusDiv = document.getElementById('statusMessage');
        if (statusDiv && currentLanguage !== 'en') {
          statusDiv.textContent = 'Loading translations...';
          statusDiv.style.display = 'block';
        }
        
        applyTranslations(currentLanguage).then(() => {
          if (statusDiv) {
            statusDiv.style.display = 'none';
          }
        });
      }
    }

    // Show main application with smooth transition
    function showMainApp() {
      const landingPage = document.getElementById('landingPage');
      const mainApp = document.getElementById('mainApp');
      
      landingPage.classList.add('hidden');
      mainApp.classList.add('active');
      
      // Copy API key from landing to main app
      const landingApiKey = document.getElementById('landingApiKey').value;
      if (landingApiKey) {
        document.getElementById('apiKey').value = landingApiKey;
        localStorage.setItem('codetutor_api_key', landingApiKey);
      }
      
      // Focus on code input for better UX
      setTimeout(() => {
        const codeInput = document.getElementById('codeInput');
        if (codeInput) {
          codeInput.focus();
        }
      }, 500);
    }

    // Show landing page with reset
    function showLandingPage() {
      const landingPage = document.getElementById('landingPage');
      const mainApp = document.getElementById('mainApp');
      
      landingPage.classList.remove('hidden');
      mainApp.classList.remove('active');
      
      // Reset language selection to current language
      const languageSelect = document.getElementById('languageSelect');
      if (languageSelect) {
        languageSelect.value = currentLanguage;
      }
    }

    // Event Listeners
    document.getElementById('startBtn').addEventListener('click', async function() {
      const selectedLanguage = document.getElementById('languageSelect').value;
      const apiKey = document.getElementById('landingApiKey').value.trim();

      if (!selectedLanguage) {
        alert('Please select a language');
        return;
      }

      if (!apiKey) {
        alert('Please enter your OpenAI API key');
        return;
      }

      // Save settings
      currentLanguage = selectedLanguage;
      localStorage.setItem('codetutor_api_key', apiKey);
      saveSettings();

      // Set text direction
      setTextDirection(currentLanguage);

      // Show main app
      showMainApp();

      // Apply translations
      await applyTranslations(currentLanguage);
    });

    document.getElementById('changeLanguageBtn').addEventListener('click', function() {
      showLandingPage();
    });

    document.getElementById('clearCacheBtn').addEventListener('click', function() {
      const cacheSize = Object.keys(translationCache).length;
      const cacheMemory = JSON.stringify(translationCache).length;
      const cacheMemoryKB = Math.round(cacheMemory / 1024);
      
      const confirmMessage = `Clear translation cache?\n\nCurrent cache contains:\n• ${cacheSize} translations\n• ~${cacheMemoryKB} KB of data\n\nThis will remove all cached translations and may require re-translating content.`;
      
      if (confirm(confirmMessage)) {
        translationCache = {};
        localStorage.removeItem('codetutor_translation_cache');
        
        // Show success message
        const statusDiv = document.getElementById('statusMessage');
        if (statusDiv) {
          statusDiv.textContent = `Translation cache cleared! Removed ${cacheSize} cached translations.`;
          statusDiv.style.display = 'block';
          setTimeout(() => {
            statusDiv.style.display = 'none';
          }, 3000);
        } else {
          alert(`Translation cache cleared successfully! Removed ${cacheSize} cached translations.`);
        }
      }
    });

    // Store API key in localStorage
    const apiKeyInput = document.getElementById('apiKey');
    const landingApiKeyInput = document.getElementById('landingApiKey');

    [apiKeyInput, landingApiKeyInput].forEach(input => {
      input.addEventListener('input', function() {
        localStorage.setItem('codetutor_api_key', this.value);
        // Sync both inputs
        if (this.id === 'apiKey') {
          landingApiKeyInput.value = this.value;
        } else {
          apiKeyInput.value = this.value;
        }
      });
    });

    // Initialize when page loads
    document.addEventListener('DOMContentLoaded', initializeApp);

    // Character counter
    const codeInputTextarea = document.getElementById('codeInput');
    const charCountSpan = document.getElementById('charCount');

    codeInputTextarea.addEventListener('input', function() {
      const currentLength = this.value.length;
      charCountSpan.textContent = currentLength;
      
      if (currentLength > 9000) {
        charCountSpan.style.color = 'var(--warning)';
      } else if (currentLength > 9500) {
        charCountSpan.style.color = 'var(--error)';
      } else {
        charCountSpan.style.color = 'var(--text-secondary)';
      }
    });

    // Update progress bar
    function updateProgress(percent) {
      document.getElementById('progressBar').style.width = percent + '%';
    }

    // Show status message
    function showStatus(message) {
      const statusDiv = document.getElementById('statusMessage');
      statusDiv.textContent = message;
      statusDiv.style.display = 'block';
    }

    // Hide status message
    function hideStatus() {
      document.getElementById('statusMessage').style.display = 'none';
    }

    // Error handling
    function showError(message) {
      const errorDiv = document.getElementById('errorMessage');
      errorDiv.textContent = message;
      errorDiv.style.display = 'block';
      setTimeout(() => {
        errorDiv.style.display = 'none';
      }, 5000);
    }

    // Build the comprehensive code explanation prompt
    function buildCodeExplanationPrompt() {
      const codeInput = document.getElementById('codeInput').value.trim();
      const programmingLanguage = document.getElementById('programmingLanguage').value;
      const explanationLevel = document.getElementById('explanationLevel').value;

      const languageContext = programmingLanguage ? `Programming Language: ${programmingLanguage}` : 'Programming Language: Auto-detect from code';

      // Get the target language for the explanation
      const language = languages.find(lang => lang.code === currentLanguage);
      const languageName = language ? language.name : 'English';

      return `You are CodeTutor Pro, an expert programming instructor and code explainer. Your mission is to make complex code understandable for learners at different levels. A user has provided code and wants a comprehensive explanation.

IMPORTANT: Provide the entire response in ${languageName}. All explanations, headings, and content should be in ${languageName}.

CRITICAL INSTRUCTION: DO NOT include any code snippets, code blocks, or actual code in your response. Provide ONLY descriptive explanations in natural language. Instead of showing code, describe what the code does in detail using words only.

USER DETAILS:
${languageContext}
Explanation Level: ${explanationLevel}
Response Language: ${languageName}

CODE TO EXPLAIN:
<<<BEGIN CODE>>>
${codeInput}
<<<END CODE>>>

Provide a comprehensive, well-structured explanation using section headers with underlines (not numbered lists). Format your response with clear section headers followed by detailed paragraphs. Use this structure:

**Section Header Format**: Use descriptive section titles that explain what will be discussed, followed by detailed paragraphs.

Structure your explanation with these types of sections:

**Code Overview Section**
- What this code does in simple terms
- The main purpose and functionality  
- What problem it solves or what task it accomplishes

**Detailed Analysis Section**
- Describe each section and component in detail using natural language
- Explain what each part does and why it's needed without showing the actual code
- Describe how different parts work together

**Key Concepts Section**
- Important programming concepts demonstrated in this code
- Explain any algorithms, data structures, or design patterns used
- Define technical terms in simple language

**Execution Flow Section**
- Step-by-step execution flow described in words
- What happens when the code runs
- Input and output explanation

**Learning Insights Section**
- What beginners can learn from this code
- Best practices demonstrated
- Common patterns or techniques shown

**Practical Applications Section**
- Where this type of code might be used
- Real-world scenarios and examples
- How it could be modified or extended

FORMATTING INSTRUCTIONS:
- Use descriptive section headers (not numbered lists like 1., 2., 3.)
- Write each section as flowing paragraphs of text
- Ensure proper text direction for RTL languages
- Integrate English technical terms naturally within the text flow
- Use clear, professional formatting with proper spacing

Tailor your explanation to the ${explanationLevel} level:
- **Complete Beginner**: Use very simple language, explain basic concepts, avoid jargon
- **Beginner**: Use clear language, explain intermediate concepts, minimal jargon with definitions
- **Intermediate**: Use standard programming terminology, focus on logic and structure
- **Advanced**: Use technical language, focus on optimization, patterns, and best practices

Make the explanation engaging, educational, and easy to follow. Use examples and analogies when helpful. Format with clear headings and structure for easy reading.

REMEMBER: Write everything in ${languageName}. DO NOT include any code snippets or code blocks - provide ONLY descriptive text explanations.`;
    }

    // API call function
    async function callAPI(prompt) {
      const apiKey = document.getElementById('apiKey').value.trim();
      
      if (!apiKey) {
        throw new Error('Please enter your OpenAI API key');
      }

      const payload = {
        model: "gpt-4o-mini",
        messages: [{ role: "system", content: prompt }],
        max_tokens: 16000,
        temperature: 0.5
      };

      const response = await fetch("https://api.openai.com/v1/chat/completions", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "Authorization": `Bearer ${apiKey}`
        },
        body: JSON.stringify(payload)
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.error?.message || "API request failed");
      }

      const data = await response.json();
      
      // Debug: Log the full response to check for truncation
      console.log('Full API response:', data);
      
      // Check if the response was truncated
      if (data.choices[0].finish_reason === 'length') {
        console.warn('Response was truncated due to max_tokens limit');
      }
      
      return data.choices[0].message.content;
    }

    // Validation function
    function validateInputs() {
      const codeInput = document.getElementById('codeInput').value.trim();
      const explanationLevel = document.getElementById('explanationLevel').value;

      if (!codeInput) {
        document.getElementById('codeInput').focus();
        throw new Error('Please enter the code you want explained');
      }

      if (codeInput.length < 10) {
        document.getElementById('codeInput').focus();
        throw new Error('Please provide more code to analyze (at least 10 characters)');
      }

      if (!explanationLevel) {
        document.getElementById('explanationLevel').focus();
        throw new Error('Please select your explanation level');
      }
    }

    // Handle form submission
    document.getElementById('generateBtn').addEventListener('click', async function() {
      const button = this;
      const buttonText = document.getElementById('buttonText');
      const spinner = document.getElementById('spinner');
      const explanationOutput = document.getElementById('explanationOutput');

      try {
        // Validate inputs
        validateInputs();

        // Update button state
        button.disabled = true;
        spinner.style.display = 'inline-block';
        buttonText.textContent = 'Analyzing Code...';
        
        // Clear previous output
        explanationOutput.innerHTML = '';
        hideStatus();
        
        // Show progress and status
        updateProgress(20);
        showStatus("Analyzing code structure and syntax...");
        
        // Build prompt and call API
        const prompt = buildCodeExplanationPrompt();
        
        updateProgress(50);
        showStatus("Generating comprehensive explanation...");
        
        const result = await callAPI(prompt);
        
        updateProgress(100);
        hideStatus();
        
        // Display result with section headers and underlines format
        let formattedResult = result;
        
        // Debug: Log the raw result to see what we're getting
        console.log('Raw API result:', result);
        
        // Remove any code block markers that might accidentally be included
        formattedResult = formattedResult.replace(/```[\s\S]*?```/g, '');
        formattedResult = formattedResult.replace(/`([^`\n]+)`/g, '$1');
        
        // Handle section headers with underlines (convert markdown headers to section headers)
        formattedResult = formattedResult.replace(/^## (.*?)$/gm, '<div class="section-header">$1</div>');
        formattedResult = formattedResult.replace(/^### (.*?)$/gm, '<div class="section-header">$1</div>');
        formattedResult = formattedResult.replace(/^#### (.*?)$/gm, '<div class="section-header">$1</div>');
        
        // Handle bold text
        formattedResult = formattedResult.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
        
        // Convert line breaks to <br> tags BEFORE processing sections
        formattedResult = formattedResult.replace(/\n/g, "<br>");
        
        // Group content after section headers into section-content divs
        formattedResult = formattedResult.replace(/(<div class="section-header">.*?<\/div>)<br>(.*?)(?=<div class="section-header">|$)/g, 
          '$1<br><div class="section-content">$2</div>');
        
        // Clean up extra <br> tags around section headers
        formattedResult = formattedResult.replace(/<br><div class="section-header">/g, '<div class="section-header">');
        formattedResult = formattedResult.replace(/<\/div><br><div class="section-content">/g, '</div><div class="section-content">');
        
        // Handle any remaining content that's not in a section (like intro text)
        if (!formattedResult.includes('<div class="section-header">')) {
          // If no section headers, wrap everything in section-content
          formattedResult = `<div class="section-content">${formattedResult}</div>`;
        } else {
          // Handle content before the first section header
          formattedResult = formattedResult.replace(/^(.*?)(<div class="section-header">)/s, 
            '<div class="section-content">$1</div>$2');
        }
        
        explanationOutput.innerHTML = formattedResult;
        
      } catch (error) {
        console.error('Error:', error);
        showError(error.message);
        explanationOutput.innerHTML = '';
        hideStatus();
        updateProgress(0);
      } finally {
        // Reset button state
        button.disabled = false;
        spinner.style.display = 'none';
        buttonText.textContent = '🧠 Generate Explanation';
      }
    });

    // Handle input changes
    document.getElementById('codeInput').addEventListener('input', function() {
      updateProgress(0);
      hideStatus();
    });

    document.getElementById('programmingLanguage').addEventListener('change', function() {
      updateProgress(0);
      hideStatus();
    });

    document.getElementById('explanationLevel').addEventListener('change', function() {
      updateProgress(0);
      hideStatus();
    });

    // Keyboard shortcut for generation
    document.addEventListener('keydown', function(e) {
      if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
        e.preventDefault();
        if (document.getElementById('mainApp').classList.contains('active')) {
          document.getElementById('generateBtn').click();
        }
      }
    });

    // Handle contenteditable placeholder
    const outputArea = document.getElementById('explanationOutput');
    outputArea.addEventListener('focus', function() {
      if (this.innerHTML === '') {
        this.innerHTML = '';
      }
    });

    outputArea.addEventListener('blur', function() {
      if (this.innerHTML.trim() === '') {
        this.innerHTML = '';
      }
    });
  </script>
</body>
</html>