File size: 85,132 Bytes
41f1cb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
console.log("Script execution started.");
// --- Global Variables ---
let generatedCode = [];
let currentCleanedCode = [];

let evolutionTimeline = [];
let activeTimelineIndex = -1;
let selectedVariationGridIndex = -1;
let currentFullscreenHistoryIndex = -1;

let originalUserPromptForCurrentGeneration = '';
let lastPreviewUpdateTime = [];
let previewUpdateInterval = 500;
let numVariationsToGenerate = 4;
let activeApiControllers = [];

let lastGenerationConfig = {
    prompt: '',
    isRefinement: false,
    numVariations: 4,
    refinedTimelineIndex: -1,
    thinkingBudget: 0
};


// --- DOM Element References ---
let apiKeyEl, codeOutputEl, errorMessageEl;
let modelSelEl;
let selectButtons = [];
let fullscreenButtons = [];
let previewItems = [];

let refinementLoadingIndicator;
let mainContentEl, configButtonEl;
let intervalSliderEl, intervalValueDisplayEl;

let fullscreenOverlayEl, fullscreenIframeEl, exitFullscreenBtnEl;
let perspectiveViewportEl, previewGridWrapperEl;

let historyPanelEl, historyPanelPlaceholderEl;
let selectedCodeTitleH3El;
let mainContentTitleH1El, mainContentSubtitleH2El;
let fullscreenHistoryNavEl, historyNavPrevBtnEl, historyNavNextBtnEl;
let promptModalOverlayEl, promptModalContentEl, modalUserPromptEl, modalGenerateBtnEl, modalCancelBtnEl, modalLoadingIndicatorEl;
let modalRefinementCheckboxEl, numVariationsSliderEl, numVariationsValueDisplayEl;
let configModalOverlayEl, configModalContentEl, configModalCloseBtnEl, copyCodeButtonEl;
let historyToggleButtonEl, historyArrowDownEl, historyArrowUpEl;
let exportCodeButtonEl;
let newButtonEl;
let confirmModalOverlayEl, confirmModalMessageEl, confirmModalConfirmBtnEl, confirmModalCancelBtnEl;
let currentConfirmCallback = null;
let historyNavLeftBtnEl, historyNavRightBtnEl;
let modalThinkingBudgetSliderEl, modalThinkingBudgetValueDisplayEl;
let promptDisplayModalOverlayEl, promptDisplayModalContentEl, fullPromptTextEl, promptDisplayModalCloseBtnEl;
let showPromptModalButtonEl; // Added for the new button
let variationNavLeftBtnEl, variationNavRightBtnEl; // Buttons for variation scrolling

// --- Elements for Initial Setup CTA ---
let initialSetupCtaEl;
let initialApiKeyInputEl;
let examplePromptsContainerEl;


// --- Constants ---
const API_BASE_URL = 'https://generativelanguage.googleapis.com/v1beta/models/';


// --- Helper Function to Process Stream for One Variation ---
async function processStreamForVariation(apiKey, prompt, variationIndex, modelName, signal) {
    console.log(`[Variation ${variationIndex + 1}] Starting generation with model ${modelName}...`);
    const previewFrame = document.getElementById(`preview-frame-${variationIndex + 1}`);
    const loader = document.getElementById(`preview-loader-${variationIndex + 1}`);
    const selectBtn = selectButtons[variationIndex];
    const fullscreenBtn = fullscreenButtons[variationIndex];

    const currentThinkingBudget = parseInt(lastGenerationConfig.thinkingBudget, 10);

    if (!previewFrame) { console.error(`[Variation ${variationIndex + 1}] Preview frame missing.`); return false; }
    if (!loader) { console.warn(`[Variation ${variationIndex + 1}] Loader missing.`); }


    if (loader) loader.classList.remove('hidden');
    if (selectBtn) selectBtn.disabled = true;
    if (fullscreenBtn) fullscreenBtn.disabled = true;

    if (previewFrame) updateLivePreviewInGrid(variationIndex, '<div class="flex items-center justify-center h-full"><p class="text-slate-500">Generating...</p></div>', false);
    if(lastPreviewUpdateTime[variationIndex] !== undefined) lastPreviewUpdateTime[variationIndex] = 0;


    const API_ENDPOINT = `${API_BASE_URL}${modelName}:streamGenerateContent?key=${apiKey}&alt=sse`;
    let rawGeneratedCode = '';
    let htmlBlockStarted = false;
    let accumulatedStreamData = '';
    let success = false;

    try {
        const requestBody = {
            contents: [{ parts: [{ text: prompt }] }]
        };

        if (currentThinkingBudget > 0) {
            requestBody.generationConfig = {
                thinkingConfig: {
                    thinkingBudget: currentThinkingBudget
                }
            };
            console.log(`[Variation ${variationIndex + 1}] Thinking Mode ENABLED. Budget: ${currentThinkingBudget}`);
        }

        const response = await fetch(API_ENDPOINT, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(requestBody),
            signal: signal
        });

        if (!response.ok) {
            let errorDetails = `HTTP Error: ${response.status} ${response.statusText}`;
            try { const errorData = await response.json(); errorDetails += ` - ${errorData?.error?.message || 'No msg.'}`; } catch (e) { /* Ignore */ }
            throw new Error(errorDetails);
        }

        const reader = response.body.getReader();
        const decoder = new TextDecoder();
        let sseBuffer = "";

        while (true) {
            const { done, value } = await reader.read();
            if (done) break;
            sseBuffer += decoder.decode(value, { stream: true });
            let eolIndex;
            while ((eolIndex = sseBuffer.indexOf('\n')) >= 0) {
                const line = sseBuffer.substring(0, eolIndex).trim();
                sseBuffer = sseBuffer.substring(eolIndex + 1);

                if (line.startsWith('data: ')) {
                    try {
                        const jsonData = JSON.parse(line.substring(5).trim());
                        if (jsonData.candidates?.[0]?.content?.parts?.[0]?.text) {
                            const textPart = jsonData.candidates[0].content.parts[0].text;

                            if (!htmlBlockStarted) {
                                accumulatedStreamData += textPart;
                                const marker = "```html";
                                const markerIndex = accumulatedStreamData.indexOf(marker);
                                if (markerIndex !== -1) {
                                    htmlBlockStarted = true;
                                    let codeStartIndex = markerIndex + marker.length;
                                    if (accumulatedStreamData[codeStartIndex] === '\n') {
                                        codeStartIndex++;
                                    }
                                    rawGeneratedCode = accumulatedStreamData.substring(codeStartIndex);
                                }
                            } else {
                                rawGeneratedCode += textPart;
                            }

                            if (htmlBlockStarted) {
                                const now = Date.now();
                                if (lastPreviewUpdateTime[variationIndex] !== undefined && (now - lastPreviewUpdateTime[variationIndex] >= previewUpdateInterval)) {
                                    updateLivePreviewInGrid(variationIndex, rawGeneratedCode, true);
                                    lastPreviewUpdateTime[variationIndex] = now;
                                }
                            }
                        }
                    } catch (e) { console.warn(`[Var ${variationIndex + 1}] JSON parse error:`, e, "Problematic Line:", line); }
                }
            }
        }

        console.log(`[Variation ${variationIndex + 1}] Streaming finished. HTML Block Started: ${htmlBlockStarted}. Raw content after marker: ${(rawGeneratedCode || "").substring(0, 200)}`);
        let finalExtractedHtml = null; 
        let processingErrorMessage = null; 

        if (htmlBlockStarted) {
            let tempHtml = rawGeneratedCode.trim();
            if (tempHtml.endsWith("```")) {
                tempHtml = tempHtml.substring(0, tempHtml.length - 3).trim();
            } else if (tempHtml.endsWith("```html")) {
                tempHtml = tempHtml.substring(0, tempHtml.length - 7).trim();
            }

            if (tempHtml.length > 0) {
                finalExtractedHtml = tempHtml;
                const minLength = 20;
                const hasStructuralTag = /<!DOCTYPE html|<html[^>]*>|<head[^>]*>|<body[^>]*>/i.test(finalExtractedHtml);
                const hasAnyTag = /<[a-zA-Z][^>]*>/.test(finalExtractedHtml);

                if (finalExtractedHtml.length >= minLength && (hasStructuralTag || hasAnyTag)) {
                    success = true;
                    console.log(`[Variation ${variationIndex + 1}] HTML validation SUCCEEDED.`);
                } else {
                    success = false; 
                    console.warn("[Variation " + (variationIndex + 1) + "] HTML validation FAILED (length: " + finalExtractedHtml.length + ", structural: " + hasStructuralTag + ", anyTag: " + hasAnyTag + "). Review content manually.");
                }
            } else { 
                processingErrorMessage = "// Error: No meaningful content found after '```html' marker.";
                console.warn(`[Variation ${variationIndex + 1}] ${processingErrorMessage}`);
                success = false;
            }
        } else { 
            processingErrorMessage = `// Error: '\`\`\`html' code block marker not found. Received:\n${(accumulatedStreamData || rawGeneratedCode || "").substring(0, 200)}`;
            console.warn(`[Variation ${variationIndex + 1}] ${processingErrorMessage}`);
            success = false;
        }

        if (success && finalExtractedHtml) {
            let processedHtml = finalExtractedHtml;
            const requiredHeadContent = `
                        <meta charset="UTF-8">
                        <meta name="viewport" content="width=device-width, initial-scale=1.0">
                        <script src="https://cdn.tailwindcss.com"><\\/script>
                        <link rel="preconnect" href="https://rsms.me/">
                        <link rel="stylesheet" href="https://rsms.me/inter/inter.css">
                        <style>
                        html { font-family: 'Inter', sans-serif; }
                        @supports (font-variation-settings: normal) {
                            html { font-family: 'Inter var', sans-serif; }
                        }
                        body {
                            background-color: #f8fafc; /* slate-50 */
                            color: #0f172a; /* slate-900 */
                            padding: 1rem;
                        }
                        </style>`; 

            if (!processedHtml.includes('<head>')) {
                processedHtml = `<head>${requiredHeadContent}</head><body>${processedHtml}</body>`;
            } else { 
                let tempRequired = "";
                if (!processedHtml.includes('cdn.tailwindcss.com')) tempRequired += `    <script src="https://cdn.tailwindcss.com"><\\/script>\\n`;
                if (!processedHtml.includes('inter.css')) tempRequired += `    <link rel="preconnect" href="https://rsms.me/">\\n    <link rel="stylesheet" href="https://rsms.me/inter/inter.css">\\n`;
                if (!processedHtml.includes("html { font-family: 'Inter', sans-serif; }")) { 
                    tempRequired += `    <style>\\n      html { font-family: 'Inter', sans-serif; }\\n      @supports (font-variation-settings: normal) { html { font-family: 'Inter var', sans-serif; } }\\n      body { background-color: #f8fafc; color: #0f172a; padding: 1rem; }\\n    </style>\\n`;
                }
                if (tempRequired) { 
                    processedHtml = processedHtml.replace(/<\/head>/i, `${tempRequired}</head>`);
                }
            }
            if(currentCleanedCode[variationIndex] !== undefined) currentCleanedCode[variationIndex] = processedHtml;

            const interactionScript = `<script>(function() { const VARIATION_INDEX = ${variationIndex}; })(); <\\/script>`;
            const bodyEndIndex = processedHtml.lastIndexOf('</body>');
            if(generatedCode[variationIndex] !== undefined) {
                generatedCode[variationIndex] = (bodyEndIndex !== -1)
                    ? processedHtml.slice(0, bodyEndIndex) + interactionScript + processedHtml.slice(bodyEndIndex)
                    : processedHtml + interactionScript;
            }
            updateLivePreviewInGrid(variationIndex, null, true);
        } else {
            generatedCode[variationIndex] = ''; 
            if (finalExtractedHtml) { 
                if(currentCleanedCode[variationIndex] !== undefined) currentCleanedCode[variationIndex] = finalExtractedHtml; 
                updateLivePreviewInGrid(variationIndex, finalExtractedHtml, false); 
            } else { 
                if(currentCleanedCode[variationIndex] !== undefined) currentCleanedCode[variationIndex] = processingErrorMessage; 
                updateLivePreviewInGrid(variationIndex, `<div class="p-4 text-red-400 font-medium">${processingErrorMessage.replace(/\\n/g, '<br>')}</div>`, false); 
            }
        }

    } catch (error) {
        if (error.name === 'AbortError') {
            console.log(`[Variation ${variationIndex + 1}] Fetch aborted.`);
            updateLivePreviewInGrid(variationIndex, `<div class="p-4 text-yellow-400 font-medium">Generation Cancelled.</div>`, false);
        } else {
            console.error(`[Variation ${variationIndex + 1}] Generation error:`, error);
            updateLivePreviewInGrid(variationIndex, `<div class="p-4 text-red-400 font-medium">Error: ${error.message}</div>`, false);
        }
        if(currentCleanedCode[variationIndex] !== undefined) currentCleanedCode[variationIndex] = `// Error: ${error.message}`;
        if(generatedCode[variationIndex] !== undefined) generatedCode[variationIndex] = '';
    } finally {
        const finalLoader = document.getElementById(`preview-loader-${variationIndex + 1}`);
        if (finalLoader) finalLoader.classList.add('hidden');
        if (selectBtn) selectBtn.disabled = !success; // Disable if not successful
        if (fullscreenBtn) fullscreenBtn.disabled = !success; // Disable if not successful
    }
    return success;
}

// --- Main Function to Generate Variations ---
async function generateVariations() {
    console.log("generateVariations called.");
    const userPrompt = modalUserPromptEl.value.trim();

    if (!apiKeyEl || !modalUserPromptEl || !modelSelEl || !previewGridWrapperEl || !modalGenerateBtnEl || !modalLoadingIndicatorEl || !errorMessageEl || !codeOutputEl) {
        console.error("Cannot generate variations: One or more critical elements are missing.");
        if (errorMessageEl) errorMessageEl.textContent = "Initialization error. Cannot generate.";
        return;
    }

    // Read API key: Prefer config modal, then initial input, then localStorage
    let apiKey = apiKeyEl.value.trim();
    if (!apiKey && initialApiKeyInputEl) {
        apiKey = initialApiKeyInputEl.value.trim();
    }
    if (!apiKey) {
        apiKey = localStorage.getItem('geminiApiKey') || '';
    }
    
    // Update input fields with the resolved API key
    if (apiKeyEl && apiKeyEl.value !== apiKey) apiKeyEl.value = apiKey;
    if (initialApiKeyInputEl && initialApiKeyInputEl.value !== apiKey) initialApiKeyInputEl.value = apiKey;


    const selectedModel = modelSelEl.value;
    const currentIsRefinementMode = modalRefinementCheckboxEl.checked;
    const currentNumVariations = parseInt(numVariationsSliderEl.value, 10);
    const currentThinkingBudget = parseInt(modalThinkingBudgetSliderEl.value, 10);

    if (!apiKey || !userPrompt) {
        errorMessageEl.textContent = 'Error: API Key and Prompt (via Alt+P) are required.';
        if (!apiKey && initialSetupCtaEl && initialSetupCtaEl.classList.contains('flex')) {
            if(initialApiKeyInputEl) initialApiKeyInputEl.focus();
        } else if (!userPrompt && !promptModalOverlayEl.classList.contains('visible')) {
            showPromptModal();
        }
        return;
    }
     if (!selectedModel) {
         errorMessageEl.textContent = 'Error: Please select a model.';
         return;
     }

    lastGenerationConfig = {
        prompt: userPrompt,
        isRefinement: currentIsRefinementMode,
        numVariations: currentNumVariations,
        refinedTimelineIndex: currentIsRefinementMode ? activeTimelineIndex : -1,
        thinkingBudget: currentThinkingBudget
    };


    errorMessageEl.textContent = '';
    console.log(`Mode: ${currentIsRefinementMode ? 'Refinement' : 'Initial'}, Model: ${selectedModel}, Variations: ${currentNumVariations}`);

    let baseCodeForRefinement = null;
    let contextPromptForRefinement = '';
    originalUserPromptForCurrentGeneration = userPrompt;

    if (currentIsRefinementMode && activeTimelineIndex !== -1 && evolutionTimeline[activeTimelineIndex]) {
        baseCodeForRefinement = evolutionTimeline[activeTimelineIndex].code;
        contextPromptForRefinement = evolutionTimeline[activeTimelineIndex].originalUserPrompt;
        console.log(`Refining Evolution ${activeTimelineIndex + 1}. Original context: "${contextPromptForRefinement}"`);
    } else if (currentIsRefinementMode) {
         errorMessageEl.textContent = 'Error: No active evolution selected to refine. Uncheck "refine" or select an evolution from history.';
         // Also hide the initial CTA if it's somehow visible
         if (initialSetupCtaEl) initialSetupCtaEl.classList.add('hidden');
         if (initialSetupCtaEl) initialSetupCtaEl.classList.remove('flex');
         if (previewGridWrapperEl) previewGridWrapperEl.classList.remove('hidden');
         return;
    }

    modalGenerateBtnEl.disabled = true;
    modalLoadingIndicatorEl.classList.remove('hidden');
    if (codeOutputEl && selectedCodeTitleH3El) {
        codeOutputEl.innerHTML = '<code class="language-html">// Select a variation to view its code.</code>';
        selectedCodeTitleH3El.textContent = "Selected Code:";
    }
    selectedVariationGridIndex = -1;

    numVariationsToGenerate = currentNumVariations;
    generatedCode = Array(numVariationsToGenerate).fill('');
    currentCleanedCode = Array(numVariationsToGenerate).fill('');
    lastPreviewUpdateTime = Array(numVariationsToGenerate).fill(0);
    selectButtons = Array(numVariationsToGenerate).fill(null);
    fullscreenButtons = Array(numVariationsToGenerate).fill(null);
    previewItems = Array(numVariationsToGenerate).fill(null);

    // Ensure correct layout state for generation
    if (initialSetupCtaEl) {
        initialSetupCtaEl.classList.remove('active-cta'); // Start hide animation
        // Hide example prompt buttons immediately or animate them out too
        if (examplePromptsContainerEl) {
            examplePromptsContainerEl.querySelectorAll('.example-prompt-button').forEach(btn => btn.classList.remove('visible'));
        }
        setTimeout(() => { // Wait for animation to roughly finish
            initialSetupCtaEl.style.display = 'none';
            initialSetupCtaEl.classList.remove('flex', 'flex-grow');
        }, 500); // Corresponds to CSS transition duration
    }
    if (mainContentEl) mainContentEl.classList.remove('justify-center-cta-active'); // Remove centering class from main-content
    if (perspectiveViewportEl) perspectiveViewportEl.classList.remove('hidden');
    // previewGridWrapperEl will be managed by showFourGridPreviewUI

    showFourGridPreviewUI();

    activeApiControllers = Array(numVariationsToGenerate).fill(null).map(() => new AbortController());

    const prompts = Array(numVariationsToGenerate).fill(null).map((_, i) => {
        if (currentIsRefinementMode && baseCodeForRefinement) {
            return `
You are an expert web developer specializing in HTML, Tailwind CSS, and JavaScript, with a focus on MOBILE-FIRST refinement.
Original User Request (for context): "${contextPromptForRefinement}"
Base HTML Code to Refine (assume it's being targeted for mobile):
\`\`\`html
${baseCodeForRefinement}
\`\`\`
User's Refinement Instructions for MOBILE VIEW: "${userPrompt}"
Instructions:
1. Analyze the Base Code and the User's Refinement Instructions, ensuring the result is optimized for a typical mobile viewport (e.g., 375px width).
2. Modify ONLY the necessary parts of the Base Code to implement the refinement.
3. This is Variation ${i + 1} of ${numVariationsToGenerate} attempting this mobile refinement. Try a slightly different approach if possible.
4. Ensure the refined code remains a single, complete, runnable HTML document suitable for mobile.
5. ALWAYS include Tailwind CSS CDN (<script src="https://cdn.tailwindcss.com"><\\/script>) and Inter font (<link rel="stylesheet" href="https://rsms.me/inter/inter.css">) in the <head>.
6. Ensure basic mobile-friendly body styling: <style>html { font-family: 'Inter', sans-serif; } body { background-color: #f8fafc; color: #0f172a; padding: 0.5rem; }</style>
7. Output ONLY the raw, complete, refined HTML code. Do NOT include explanations or markdown. Start your response with \`\`\`html.
Refined Mobile-Optimized Code:`;
        } else {
            return `
You are an expert web developer specializing in clean, modern HTML, CSS (using Tailwind CSS classes), and JavaScript, specifically for MOBILE-FIRST designs.
Generate the complete, runnable HTML code for the following request, optimized for a typical mobile viewport (e.g., 375px width).
Ensure the code is self-contained and responsive.
1. ALWAYS include Tailwind CSS CDN (<script src="https://cdn.tailwindcss.com"><\\/script>) in the <head>.
2. ALWAYS include Inter font (<link rel="stylesheet" href="https://rsms.me/inter/inter.css">) and set html { font-family: 'Inter', sans-serif; } in a <style> tag in the <head>.
3. Add basic body styling for readability: body { background-color: #f8fafc; color: #0f172a; padding: 0.5rem; /* Adjusted padding for mobile */ } in the same <style> tag.
4. Add HTML comments to explain the code.
5. This is Variation ${i + 1} of ${numVariationsToGenerate}. Try to make it distinct while adhering to mobile-first principles.
6. Output ONLY the raw HTML code. Do NOT include explanations or markdown. Start your response with \`\`\`html.
User Request:
"${originalUserPromptForCurrentGeneration}"
Mobile-Optimized Code:`;
        }
    });

    const generationPromises = prompts.map((prompt, index) =>
        processStreamForVariation(apiKey, prompt, index, selectedModel, activeApiControllers[index].signal)
    );

    try {
        const results = await Promise.allSettled(generationPromises);
        console.log("Generation promises settled:", results);
        const successfulGenerations = results.filter(r => r.status === 'fulfilled' && r.value === true).length;
        if (successfulGenerations === 0 && !results.some(r => r.status === 'rejected' && r.reason.name === 'AbortError')) {
            errorMessageElement.textContent = 'Error: All variations failed.';
        }
        else if (successfulGenerations < numVariationsToGenerate && !results.every(r => r.status === 'rejected' && r.reason.name === 'AbortError')) {
            errorMessageElement.textContent = `Warning: ${numVariationsToGenerate - successfulGenerations} var(s) failed or were cancelled.`;
        }

        updateMainContentTitles("Select a Variation", "Click 'Select' on a preview below.");

    } catch (error) {
        console.error("Parallel generation error:", error);
        errorMessageElement.textContent = `Unexpected Error: ${error.message}`;
        showInitialPreviewStateUI();
    } finally {
        modalGenerateBtnEl.disabled = false;
        modalLoadingIndicatorEl.classList.add('hidden');
        updateVariationNavButtons(); // Re-evaluate arrows after generation attempt ends
        console.log("Generation process finished.");
    }
}

// --- UI Update Functions ---
function createPreviewItemDOM(index, isGridItem = true) {
    const item = document.createElement('div');
    item.id = `preview-item-${index + 1}`;
    item.className = isGridItem ? 'preview-item-perspective' : 'single-preview-item';
    if (isGridItem) item.dataset.variationGridIndex = index;

    const header = document.createElement('div'); header.className = 'preview-header';
    const title = document.createElement('span'); title.className = 'preview-header-title';
    title.textContent = isGridItem ? `Variation ${index + 1}` : (evolutionTimeline[activeTimelineIndex]?.prompt.substring(0,30) + '...' || `Evolution ${activeTimelineIndex + 1}`);
    header.appendChild(title);

    const btns = document.createElement('div'); btns.className = 'preview-header-buttons';

    // Only add the iframe-specific fullscreen button if it's NOT a grid item (i.e., it's the single large preview)
    // or if we decide to keep it for individual grid items based on a different condition.
    // For now, removing it from grid items as per user request.
    if (!isGridItem) { 
        const fsBtn = document.createElement('button');
        fsBtn.className = 'fullscreen-btn p-1 focus:outline-none focus:ring-1 focus:ring-cyan-500 rounded disabled:opacity-50';
        // fsBtn.dataset.idx = index; // This index might be ambiguous if not a grid item
        fsBtn.title = 'Full Screen'; 
        fsBtn.disabled = !(evolutionTimeline[activeTimelineIndex]?.code); // Disable if no code for current history
        fsBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></svg>`;
        fsBtn.addEventListener('click', (e) => {
            // When it's the single large preview, it's always from history (activeTimelineIndex)
            if (activeTimelineIndex !== -1 && evolutionTimeline[activeTimelineIndex]?.code) {
                enterFullscreen(activeTimelineIndex, true);
            }
        });
        btns.appendChild(fsBtn);
    }

    if (isGridItem) {
        const selBtn = document.createElement('button');
        selBtn.className = 'select-variation-btn futuristic-button px-3 py-1 text-xs';
        selBtn.dataset.variationGridIndex = index; selBtn.disabled = true; selBtn.textContent = 'Select';
        selBtn.addEventListener('click', handleSelectVariationFromGrid);
        selectButtons[index] = selBtn;
        btns.appendChild(selBtn);
    }

    header.appendChild(btns);
    item.appendChild(header);

    const bodyEl = document.createElement('div'); bodyEl.className = 'preview-body';
    const loaderDiv = document.createElement('div'); loaderDiv.id = `preview-loader-${index + 1}`;
    loaderDiv.className = 'preview-loader ' + (isGridItem ? 'hidden' : '');
    loaderDiv.innerHTML = '<div class="spinner"></div>';
    bodyEl.appendChild(loaderDiv);

    const iframeEl = document.createElement('iframe');
    iframeEl.id = isGridItem ? `preview-frame-${index + 1}` : 'single-large-preview-frame';
    iframeEl.title = `Preview ${index + 1}`; iframeEl.className = 'preview-frame';
    iframeEl.srcdoc = '<div class="flex items-center justify-center h-full"><p class="text-slate-400">Preparing...</p></div>';
    bodyEl.appendChild(iframeEl);
    item.appendChild(bodyEl);

    if (isGridItem) {
        previewItems[index] = item;
        // fullscreenButtons[index] = fsBtn; // fsBtn is no longer created for grid items here
    } else {
        // If it's the single large preview, we might want to store its fullscreen button if needed elsewhere
        // For now, this is not strictly necessary as it's created and added directly.
    }
    return item;
}


function showFourGridPreviewUI() {
    previewGridWrapperEl.innerHTML = '';
    previewGridWrapperEl.className = 'mobile-preview-layout'; // Use this class for the flex row
    if (numVariationsToGenerate === 1) {
        previewGridWrapperEl.style.justifyContent = 'center';
    } else {
        previewGridWrapperEl.style.justifyContent = 'flex-start';
    }
    if(perspectiveViewportEl) perspectiveViewportEl.style.perspective = 'none'; // Remove perspective

    for (let i = 0; i < numVariationsToGenerate; i++) {
        // createPreviewItemDOM's second arg 'isGridItem = true' will assign 'preview-item-perspective'
        // which will be styled as a mobile phone.
        const item = createPreviewItemDOM(i, true);
        previewGridWrapperEl.appendChild(item);
    }
    updateSelectedGridItemUI(); // This should still work to highlight.
    updateVariationNavButtons(); // Explicitly update nav buttons after rendering previews
}

function showSingleLargePreviewUI(htmlContent, titleText, fullPromptText) {
    previewGridWrapperEl.innerHTML = '';
    previewGridWrapperEl.className = 'single-mode';
     if(perspectiveViewportEl) perspectiveViewportEl.style.perspective = 'none';

    // Ensure correct layout state for single preview
    if (initialSetupCtaEl) {
        initialSetupCtaEl.classList.remove('active-cta');
        if (examplePromptsContainerEl) {
            examplePromptsContainerEl.querySelectorAll('.example-prompt-button').forEach(btn => btn.classList.remove('visible'));
        }
        setTimeout(() => {
            initialSetupCtaEl.style.display = 'none';
            initialSetupCtaEl.classList.remove('flex', 'flex-grow');
        }, 500);
    }
    if (mainContentEl) mainContentEl.classList.remove('justify-center-cta-active'); // Remove centering class from main-content
    if (perspectiveViewportEl) perspectiveViewportEl.classList.remove('hidden');
    if (previewGridWrapperEl) previewGridWrapperEl.classList.remove('hidden');

    const item = document.createElement('div');
    item.className = 'single-preview-item';

    const bodyEl = document.createElement('div');
    bodyEl.className = 'preview-body';

    const iframeEl = document.createElement('iframe');
    iframeEl.id = `single-large-preview-frame`;
    iframeEl.title = titleText;
    iframeEl.className = 'preview-frame';
    iframeEl.srcdoc = htmlContent;

    bodyEl.appendChild(iframeEl);
    item.appendChild(bodyEl);
    previewGridWrapperEl.appendChild(item);

    // Handle subtitle truncation and clickability
    const maxPromptLength = 50;
    let displaySubtitle = fullPromptText;
    mainContentSubtitleH2El.classList.remove('prompt-truncated');
    delete mainContentSubtitleH2El.dataset.fullPrompt;

    if (fullPromptText && fullPromptText.length > maxPromptLength) {
        displaySubtitle = fullPromptText.substring(0, maxPromptLength) + "... (click to view full)";
        mainContentSubtitleH2El.classList.add('prompt-truncated');
        mainContentSubtitleH2El.dataset.fullPrompt = fullPromptText; // Store full prompt
    }

    updateMainContentTitles(titleText, displaySubtitle); // Use potentially truncated text
    updateVariationNavButtons(); // Ensure nav buttons are updated for single view
}

function showInitialPreviewStateUI() {
    // Hide the main preview grid and show the initial CTA
    if (previewGridWrapperEl) previewGridWrapperEl.classList.add('hidden');
    if (perspectiveViewportEl) perspectiveViewportEl.classList.add('hidden'); // Hide the whole viewport
    if (mainContentEl) mainContentEl.classList.add('justify-center-cta-active'); // Add centering class to main-content
    
    if (initialSetupCtaEl) {
        initialSetupCtaEl.style.display = 'flex'; // Set display before animation
        initialSetupCtaEl.classList.add('flex-grow');
        // Use requestAnimationFrame to ensure display:flex is applied before adding class for transition
        requestAnimationFrame(() => {
            initialSetupCtaEl.classList.add('active-cta');
        });
    }

    updateMainContentTitles("Welcome to Live Previews", "Set up your API Key or try an example below.");
    if (codeOutputEl) codeOutputEl.innerHTML = '<code class="language-html">// Select a variation or history item to view its code.</code>';
    if (selectedCodeTitleH3El) selectedCodeTitleH3El.textContent = "Selected Code:";
    selectedVariationGridIndex = -1;
    if (mainContentSubtitleH2El) {
        mainContentSubtitleH2El.classList.remove('prompt-truncated');
        delete mainContentSubtitleH2El.dataset.fullPrompt;
    }

    // Populate example prompts
    if (examplePromptsContainerEl) {
        examplePromptsContainerEl.innerHTML = ''; // Clear existing
        const prompts = [
            "A mobile app login screen with social media login options.",
            "A product detail page for a mobile e-commerce app, showing image, price, and add to cart button.",
            "A user profile screen for a mobile app with an avatar, name, and a list of user settings.",
            "A chat interface for a mobile messaging app with message bubbles and an input field."
        ];
        prompts.forEach((promptText, index) => {
            const button = document.createElement('button');
            button.className = 'example-prompt-button';
            button.textContent = promptText;
            button.addEventListener('click', () => {
                if (modalUserPromptEl) modalUserPromptEl.value = promptText;
                showPromptModal();
            });
            examplePromptsContainerEl.appendChild(button);
            // Staggered animation for buttons
            setTimeout(() => {
                button.classList.add('visible');
            }, 300 + index * 120); // Delay after panel animation starts, then stagger
        });
    }
}

function updateMainContentTitles(title, subtitle) {
    if (mainContentTitleH1El) mainContentTitleH1El.textContent = title;
    if (mainContentSubtitleH2El) mainContentSubtitleH2El.textContent = subtitle;
}

function updateSelectedGridItemUI() {
    previewItems.forEach((item, index) => {
        if (!item) return;
        item.classList.toggle('selected', index === selectedVariationGridIndex);
    });
    selectButtons.forEach((button, index) => {
        if (!button) return;
        // Button enabled/disabled state is handled by procStream's success.
        // const hasCode = currentCleanedCode[index]?.length > 0 && !currentCleanedCode[index].startsWith("// Error");
        // button.disabled = !hasCode;
        // if (fullscreenButtons[index]) { fullscreenButtons[index].disabled = !hasCode; }

        button.classList.remove('selected-state');
        if (index === selectedVariationGridIndex) {
            button.textContent = 'Selected';
            button.classList.add('selected-state');
        } else {
            button.textContent = 'Select';
        }
    });
}

function updateLivePreviewInGrid(index, codeToRender = null, applyZoom = false) {
    let baseHtml = codeToRender !== null ? codeToRender : generatedCode[index];
    const frame = document.getElementById(`preview-frame-${index + 1}`);
    if (!frame) { console.warn(`[updateLivePreviewInGrid][Var ${index + 1}] Frame not found.`); return; }

    if (typeof baseHtml !== 'string') {
        baseHtml = '<div class="p-4 text-orange-500">Invalid content received</div>';
    }
    let finalHtml = baseHtml;
    try {
        // REMOVED the injected scaling style for mobile previews
        // The generated mobile code should be responsive within the iframe.
        frame.srcdoc = finalHtml;
    } catch (e) {
        console.error(`[Var ${index + 1}] Error setting srcdoc for grid:`, e);
        try {
            frame.srcdoc = `<div class="p-4 text-red-500 font-semibold">Preview Render Error</div>`;
        } catch (finalError) { console.error("Failed to display error in grid iframe:", finalError); }
    }
}

// --- Event Handlers ---
function handleSelectVariationFromGrid(event) {
    const idx = parseInt(event.target.dataset.variationGridIndex, 10);
     // Check if code for this variation is valid (not an error message)
    if (isNaN(idx) || !currentCleanedCode[idx] || currentCleanedCode[idx].startsWith("// Error")) {
        console.warn(`Cannot select variation ${idx + 1}, code is invalid or generation failed.`);
        return;
    }

    activeApiControllers.forEach((controller, controllerIndex) => {
        if (controllerIndex !== idx && controller) {
            console.log(`Aborting request for variation ${controllerIndex + 1}`);
            controller.abort();
        }
    });
    activeApiControllers = [];


    selectedVariationGridIndex = idx;
    const displayCode = generatedCode[idx];
    const storeCode = currentCleanedCode[idx];

    let originalPromptForThisEvolution;
    let parentTimelineIdx = null;

    const wasThisGenerationARefinement = lastGenerationConfig.isRefinement;
    const refinedIndexForThisGen = lastGenerationConfig.refinedTimelineIndex;

    if (wasThisGenerationARefinement && refinedIndexForThisGen !== -1 && evolutionTimeline[refinedIndexForThisGen]) {
        originalPromptForThisEvolution = evolutionTimeline[refinedIndexForThisGen].originalUserPrompt;
        parentTimelineIdx = refinedIndexForThisGen;
    } else {
        originalPromptForThisEvolution = originalUserPromptForCurrentGeneration;
    }

    const newHistoryEntry = {
        id: 'evo-' + Date.now() + '-' + Math.random().toString(36).substr(2, 5),
        prompt: originalUserPromptForCurrentGeneration,
        originalUserPrompt: originalPromptForThisEvolution,
        code: storeCode,
        timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit'}),
        parentIndex: parentTimelineIdx
    };
    evolutionTimeline.push(newHistoryEntry);
    activeTimelineIndex = evolutionTimeline.length - 1;

    console.log(`Variation ${idx + 1} selected from grid. Added to timeline as Evolution ${activeTimelineIndex + 1}.`);

    renderHistoryPanel();
    showSingleLargePreviewUI(displayCode, `Evolution ${activeTimelineIndex + 1}: Active`, `Prompt: "${newHistoryEntry.prompt}"`);

    if (codeOutputEl) {
        codeOutputEl.textContent = storeCode;
        if (selectedCodeTitleH3El) selectedCodeTitleH3El.textContent = `Code (Evolution ${activeTimelineIndex + 1}):`;
        codeOutputEl.classList.remove('text-slate-200');
        codeOutputEl.classList.add('text-slate-400');
        if (codeOutputEl?.parentElement) { codeOutputEl.parentElement.scrollTop = 0; }
    }


    updateSelectedGridItemUI();
    updateHistoryNavigationButtons(); // Update nav buttons

    if(modalUserPromptEl) modalUserPromptEl.value = '';
    if(modalUserPromptEl) modalUserPromptEl.placeholder = `Refine Evolution ${activeTimelineIndex + 1}...`;
    if(modalRefinementCheckboxEl) modalRefinementCheckboxEl.checked = true;
}

function handleHistoryItemClick(timelineIdxToView) {
    if (timelineIdxToView < 0 || timelineIdxToView >= evolutionTimeline.length) {
        console.warn("Invalid history index clicked:", timelineIdxToView);
        return;
    }
    activeTimelineIndex = timelineIdxToView;
    const historyEntry = evolutionTimeline[activeTimelineIndex];

    console.log(`History item ${activeTimelineIndex + 1} selected.`);

    const displayCodeForHistory = historyEntry.code;

    showSingleLargePreviewUI(displayCodeForHistory, `Evolution ${activeTimelineIndex + 1}: Active (Historical)`, `Prompt: "${historyEntry.prompt}"`);

    if (codeOutputEl) {
        codeOutputEl.textContent = historyEntry.code;
        if (selectedCodeTitleH3El) selectedCodeTitleH3El.textContent = `Code (Evolution ${activeTimelineIndex + 1} - Historical):`;
        codeOutputEl.classList.remove('text-slate-200');
        codeOutputEl.classList.add('text-slate-400');
        if (codeOutputEl?.parentElement) { codeOutputEl.parentElement.scrollTop = 0; }
    }

    renderHistoryPanel();
    updateHistoryNavigationButtons(); // Update nav buttons

    if(modalUserPromptEl) modalUserPromptEl.value = '';
    if(modalUserPromptEl) modalUserPromptEl.placeholder = `Refine Evolution ${activeTimelineIndex + 1}...`;
    if(modalRefinementCheckboxEl) modalRefinementCheckboxEl.checked = true;
    selectedVariationGridIndex = -1;
    updateSelectedGridItemUI();
}


// --- Render History Panel ---
function createHistoryThumbnailDOM(entry, index) {
    const thumbItem = document.createElement('div');
    thumbItem.className = 'history-thumbnail-item group';
    thumbItem.dataset.timelineIndex = index;
    thumbItem.setAttribute('aria-label', `Evolution Step ${index + 1}: ${entry.prompt.substring(0, 30)}...`);

    // Note: Active class and dynamic transforms/z-index are applied in renderHistoryPanel

    const previewContainer = document.createElement('div');
    previewContainer.className = 'history-thumbnail-preview-container';
    previewContainer.title = `Click to view Evolution ${index + 1}`;
    previewContainer.addEventListener('click', () => handleHistoryItemClick(index));

    const iframe = document.createElement('iframe');
    iframe.className = 'history-thumbnail-preview';
    iframe.title = `Preview of Evolution ${index + 1}`;
    const scaledContent = `
                <style>
                    html { transform: scale(0.25); transform-origin: 0 0; width: 400%; height: 400%; overflow: hidden !important; background-color: #fff; }
                    body { width: 100%; height: 100%; overflow: hidden !important; padding: 0 !important; margin: 0 !important; }
                </style>
                ${entry.code}
            `;
    iframe.srcdoc = scaledContent;
    previewContainer.appendChild(iframe);

    const titleEl = document.createElement('div');
    titleEl.className = 'history-thumbnail-title';
    titleEl.textContent = `Evo ${index + 1}: ${entry.prompt.substring(0, 20)}${entry.prompt.length > 20 ? '...' : ''}`;
    titleEl.title = `Prompt: ${entry.prompt}\\nClick to view Evolution ${index + 1}`;
    titleEl.addEventListener('click', () => handleHistoryItemClick(index));

    const fsBtn = document.createElement('button');
    fsBtn.className = 'history-thumbnail-fullscreen-btn';
    fsBtn.title = 'View Fullscreen';
    fsBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></svg>`;
    fsBtn.addEventListener('click', (e) => {
        e.stopPropagation();
        enterFullscreen(index, true);
    });

    thumbItem.appendChild(previewContainer);
    thumbItem.appendChild(titleEl);
    thumbItem.appendChild(fsBtn);
    return thumbItem;
}

function renderHistoryPanel() {
    if (!historyPanelEl || !historyPanelPlaceholderEl) return;
    historyPanelEl.innerHTML = ''; // Clear previous items

    if (evolutionTimeline.length === 0) {
        historyPanelEl.appendChild(historyPanelPlaceholderEl);
        historyPanelPlaceholderEl.classList.remove('hidden');
        return;
    }

    historyPanelPlaceholderEl.classList.add('hidden');

    const totalItems = evolutionTimeline.length;
    const middleIndex = Math.floor(totalItems / 2);
    // const maxRotation = 15; // Max rotation in degrees for edge items (rotation is now 0)
    const yOffsetFactor = 4; // How much non-active items lift towards the edges
    const zOffsetFactor = -15; // How much non-active items move back towards the edges
    const baseZIndex = 10; // Base z-index for non-active items

    // Define specific offsets for the active item to make it stand out
    const activeYOffset = -15; // Moves active item further up
    const activeZOffset = 25;  // Moves active item further forward

    evolutionTimeline.forEach((entry, index) => {
        const thumbItem = createHistoryThumbnailDOM(entry, index);
        let finalTransform;
        let finalZIndex;

        if (index === activeTimelineIndex) {
            thumbItem.classList.add('active-history-item');
            // Transform for the active item: more prominent
            finalTransform = `translateY(${activeYOffset}px) translateZ(${activeZOffset}px) rotate(0deg)`;
            // The .active-history-item class in CSS sets z-index: 50.
            // We can set it here too for consistency or rely on CSS.
            finalZIndex = 50; 
        } else {
            // Calculate transformations relative to the middle for non-active items
            const deltaFromMiddle = index - middleIndex;
            const rotation = 0; // Rotation is kept at 0
            const translateY = Math.abs(deltaFromMiddle) * yOffsetFactor;
            const translateZ = Math.abs(deltaFromMiddle) * zOffsetFactor;

            finalTransform = `translateY(${translateY}px) translateZ(${translateZ}px) rotate(${rotation}deg)`;
            finalZIndex = baseZIndex - Math.abs(deltaFromMiddle);
        }

        thumbItem.style.zIndex = `${finalZIndex}`;
        thumbItem.style.transform = finalTransform;

        historyPanelEl.appendChild(thumbItem);
    });
    const activeThumb = historyPanelEl.querySelector('.active-history-item');
    if (activeThumb) {
        // Keep scrollIntoView, maybe adjust behavior if needed
        activeThumb.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' });
    }
}

// --- Full Screen Logic ---
function enterFullscreen(index, isFromHistory = false) {
    if (!fullscreenIframeEl || !fullscreenOverlayEl || !document.body || !fullscreenHistoryNavEl || !historyNavPrevBtnEl || !historyNavNextBtnEl) {
        console.error("Cannot enter fullscreen: Overlay or nav elements missing.");
        return;
    }

    let codeToDisplay;
    currentFullscreenHistoryIndex = -1;
    fullscreenHistoryNavEl.classList.remove('visible');

    if (isFromHistory) {
        if (index < 0 || index >= evolutionTimeline.length || !evolutionTimeline[index]) {
             console.warn("Cannot enter fullscreen for history item", index); return;
        }
        codeToDisplay = evolutionTimeline[index].code;
        currentFullscreenHistoryIndex = index;

        fullscreenHistoryNavEl.classList.add('visible');
        historyNavPrevBtnEl.disabled = (currentFullscreenHistoryIndex <= 0);
        historyNavNextBtnEl.disabled = (currentFullscreenHistoryIndex >= evolutionTimeline.length - 1);

    } else {
         if (index < 0 || index >= numVariationsToGenerate || !generatedCode[index]) {
            console.warn("Cannot enter fullscreen for variation grid item", index); return;
         }
         codeToDisplay = generatedCode[index];
    }

    fullscreenIframeEl.srcdoc = codeToDisplay;
    fullscreenOverlayEl.classList.add('visible');
    document.body.classList.add('fullscreen-active');
    document.documentElement.style.overflow = 'hidden';
}
function exitFullscreen() {
    if (!fullscreenOverlayEl || !document.body || !fullscreenIframeEl || !fullscreenHistoryNavEl) {
        console.error("Cannot exit fullscreen: Overlay or nav elements missing.");
        return;
    }
    fullscreenOverlayEl.classList.remove('visible');
    document.body.classList.remove('fullscreen-active');
    document.documentElement.style.overflow = '';
    currentFullscreenHistoryIndex = -1;
    fullscreenHistoryNavEl.classList.remove('visible');
    setTimeout(() => { if (fullscreenIframeEl) fullscreenIframeEl.srcdoc = 'about:blank'; }, 300);
}

function showPreviousHistoryInFullscreen() {
    if (currentFullscreenHistoryIndex > 0) {
        currentFullscreenHistoryIndex--;
        activeTimelineIndex = currentFullscreenHistoryIndex;
        enterFullscreen(activeTimelineIndex, true);
        renderHistoryPanel();
        const historyEntry = evolutionTimeline[activeTimelineIndex];
        if (historyEntry && codeOutputEl) {
            codeOutputEl.textContent = historyEntry.code;
            if(selectedCodeTitleH3El) selectedCodeTitleH3El.textContent = `Code (Evolution ${activeTimelineIndex + 1} - Historical):`;
        }
    }
}

function showNextHistoryInFullscreen() {
    if (currentFullscreenHistoryIndex < evolutionTimeline.length - 1) {
        currentFullscreenHistoryIndex++;
        activeTimelineIndex = currentFullscreenHistoryIndex;
        enterFullscreen(activeTimelineIndex, true);
        renderHistoryPanel();
        const historyEntry = evolutionTimeline[activeTimelineIndex];
         if (historyEntry && codeOutputEl) {
            codeOutputEl.textContent = historyEntry.code;
            if(selectedCodeTitleH3El) selectedCodeTitleH3El.textContent = `Code (Evolution ${activeTimelineIndex + 1} - Historical):`;
        }
    }
}

// --- Prompt Modal Logic ---
function showPromptModal() {
    if (!promptModalOverlayEl || !modalUserPromptEl || !modalRefinementCheckboxEl || !numVariationsSliderEl || !modalThinkingBudgetSliderEl || !modalThinkingBudgetValueDisplayEl) return;

    modalRefinementCheckboxEl.checked = (activeTimelineIndex !== -1);
    numVariationsSliderEl.value = lastGenerationConfig.numVariations;
    if(numVariationsValueDisplayEl) numVariationsValueDisplayEl.textContent = numVariationsSliderEl.value;
    
    modalThinkingBudgetSliderEl.value = lastGenerationConfig.thinkingBudget;
    if(modalThinkingBudgetValueDisplayEl) modalThinkingBudgetValueDisplayEl.textContent = lastGenerationConfig.thinkingBudget;

    // Animation handling
    promptModalOverlayEl.classList.remove('modal-anim-fade-out');
    promptModalOverlayEl.style.display = 'flex'; // Or 'block' if that was its original display type
    promptModalOverlayEl.classList.add('modal-anim-fade-in');
    // promptModalOverlayEl.classList.add('visible'); // Keep this if it controls other things besides opacity/visibility

    modalUserPromptEl.focus();
}

function hidePromptModal() {
    if (!promptModalOverlayEl) return;

    promptModalOverlayEl.classList.remove('modal-anim-fade-in');
    promptModalOverlayEl.classList.add('modal-anim-fade-out');

    // Wait for animation to finish before hiding
    const handleAnimationEnd = () => {
        promptModalOverlayEl.style.display = 'none';
        // promptModalOverlayEl.classList.remove('visible'); // Keep this if it controls other things
        promptModalOverlayEl.removeEventListener('animationend', handleAnimationEnd);
    };
    promptModalOverlayEl.addEventListener('animationend', handleAnimationEnd);
}

function handleModalGenerate() {
    if (!modalUserPromptEl || !modalGenerateBtnEl) return;
    const modalPrompt = modalUserPromptEl.value.trim();
    if (modalPrompt) {
        hidePromptModal();
        if (!modalGenerateBtnEl.disabled) {
            generateVariations();
        }
    } else {
        console.warn("Modal prompt is empty. Not generating.");
    }
}

// --- Config Modal Logic ---
function showConfigModal() {
    if (!configModalOverlayEl) return;

    // Animation handling
    configModalOverlayEl.classList.remove('modal-anim-fade-out');
    configModalOverlayEl.style.display = 'flex'; // Or 'block'
    configModalOverlayEl.classList.add('modal-anim-fade-in');
    // configModalOverlayEl.classList.add('visible');
}
function hideConfigModal() {
    if (!configModalOverlayEl) return;

    configModalOverlayEl.classList.remove('modal-anim-fade-in');
    configModalOverlayEl.classList.add('modal-anim-fade-out');

    const handleAnimationEnd = () => {
        configModalOverlayEl.style.display = 'none';
        // configModalOverlayEl.classList.remove('visible');
        configModalOverlayEl.removeEventListener('animationend', handleAnimationEnd);
    };
    configModalOverlayEl.addEventListener('animationend', handleAnimationEnd);
}

// --- Confirmation Modal Logic ---
function showConfirmModal(message, onConfirmCallback) {
    if (!confirmModalOverlayEl || !confirmModalMessageEl || !confirmModalConfirmBtnEl || !confirmModalCancelBtnEl) {
        console.error("Confirmation modal elements not found!");
        // Fallback to default confirm if modal elements are missing
        if (confirm(message)) { 
            onConfirmCallback(); 
        }
        return;
    }

    confirmModalMessageEl.textContent = message;
    currentConfirmCallback = onConfirmCallback; // Store the callback

    // Clear previous listeners (important!)
    confirmModalConfirmBtnEl.replaceWith(confirmModalConfirmBtnEl.cloneNode(true));
    confirmModalCancelBtnEl.replaceWith(confirmModalCancelBtnEl.cloneNode(true));
    // Re-find buttons after cloning
    confirmModalConfirmBtnEl = document.getElementById('confirm-modal-confirm-button');
    confirmModalCancelBtnEl = document.getElementById('confirm-modal-cancel-button');

    // Add new listeners
    confirmModalConfirmBtnEl.addEventListener('click', handleConfirm); 
    confirmModalCancelBtnEl.addEventListener('click', hideConfirmModal);

    // Show modal with animation
    confirmModalOverlayEl.classList.remove('modal-anim-fade-out');
    confirmModalOverlayEl.style.display = 'flex'; 
    confirmModalOverlayEl.classList.add('modal-anim-fade-in');
}

function hideConfirmModal() {
    if (!confirmModalOverlayEl) return;

    confirmModalOverlayEl.classList.remove('modal-anim-fade-in');
    confirmModalOverlayEl.classList.add('modal-anim-fade-out');

    const handleAnimationEnd = () => {
        confirmModalOverlayEl.style.display = 'none';
        currentConfirmCallback = null; // Clear callback when hidden
        confirmModalOverlayEl.removeEventListener('animationend', handleAnimationEnd);
    };
    confirmModalOverlayEl.addEventListener('animationend', handleAnimationEnd);
}

function handleConfirm() {
    hideConfirmModal();
    if (typeof currentConfirmCallback === 'function') {
        currentConfirmCallback(); // Execute the stored callback
    }
    currentConfirmCallback = null; // Clear callback after execution
}

// --- History Navigation Logic ---
function navigateToPreviousHistory() {
    if (activeTimelineIndex > 0) {
        handleHistoryItemClick(activeTimelineIndex - 1);
    }
}

function navigateToNextHistory() {
    if (activeTimelineIndex < evolutionTimeline.length - 1) {
        handleHistoryItemClick(activeTimelineIndex + 1);
    }
}

function updateHistoryNavigationButtons() {
    if (!historyNavLeftBtnEl || !historyNavRightBtnEl) return;
    historyNavLeftBtnEl.disabled = activeTimelineIndex <= 0;
    historyNavRightBtnEl.disabled = activeTimelineIndex >= evolutionTimeline.length - 1;
}

// --- Variation Scroll Navigation Logic ---
function updateVariationNavButtons() {
    if (!perspectiveViewportEl || !variationNavLeftBtnEl || !variationNavRightBtnEl) return;

    const كان_الزر_الأيسر_مخفيًا_سابقًا = variationNavLeftBtnEl.classList.contains('hidden');
    const canScroll = perspectiveViewportEl.scrollWidth > perspectiveViewportEl.clientWidth;

    if (canScroll) {
        perspectiveViewportEl.style.justifyContent = 'flex-start'; // Align to start when scrollable
        variationNavLeftBtnEl.classList.remove('hidden');
        variationNavRightBtnEl.classList.remove('hidden');

        // If the arrows just appeared (i.e., content became scrollable), reset scroll to the beginning.
        if (كان_الزر_الأيسر_مخفيًا_سابقًا) {
            perspectiveViewportEl.scrollLeft = 0;
        }

        // Update disabled state based on current (potentially reset) scrollLeft
        variationNavLeftBtnEl.disabled = perspectiveViewportEl.scrollLeft <= 0;
        variationNavRightBtnEl.disabled = perspectiveViewportEl.scrollLeft >= (perspectiveViewportEl.scrollWidth - perspectiveViewportEl.clientWidth - 1); // -1 for potential subpixel issues
    } else {
        perspectiveViewportEl.style.justifyContent = 'center'; // Center when not scrollable
        variationNavLeftBtnEl.classList.add('hidden');
        variationNavRightBtnEl.classList.add('hidden');
    }
}

function scrollVariations(direction) {
    if (!perspectiveViewportEl || !previewItems || previewItems.length === 0) return;

    let scrollAmount;
    const firstPreviewItem = perspectiveViewportEl.querySelector('.preview-item-perspective');
    
    if (firstPreviewItem) {
        const itemStyle = window.getComputedStyle(firstPreviewItem);
        const itemWidth = parseFloat(itemStyle.width) || 0;
        // Ensure previewGridWrapperEl is used for gap calculation if it exists, otherwise fallback.
        const previewGridWrapper = document.getElementById('preview-grid-wrapper'); 
        const gapStyle = previewGridWrapper ? window.getComputedStyle(previewGridWrapper).gap : '24px'; // Default gap if wrapper not found for some reason
        const gap = parseFloat(gapStyle) || 24;

        if (itemWidth > 0) {
            scrollAmount = itemWidth + gap;
        } else {
            // If itemWidth is 0 (e.g., item not found or has no width), use a larger default scroll.
            scrollAmount = perspectiveViewportEl.clientWidth / 2 || 300; // Scroll by half viewport or 300px
        }
    } else {
        // Fallback if no preview items are found at all for calculation.
        scrollAmount = perspectiveViewportEl.clientWidth / 2 || 300;
    }

    // Ensure scrollAmount is always positive and reasonable
    if (scrollAmount <= 24) { // If calculated amount is still too small (e.g. only gap)
        scrollAmount = Math.max(perspectiveViewportEl.clientWidth / 3, 200); // Min 200px or 1/3 of viewport
    }

    const currentScrollLeft = perspectiveViewportEl.scrollLeft;
    const newScrollLeft = direction === 'left' 
        ? currentScrollLeft - scrollAmount 
        : currentScrollLeft + scrollAmount;

    perspectiveViewportEl.scrollTo({
        left: newScrollLeft,
        behavior: 'smooth'
    });
}

// --- Helper function to call Gemini for code splitting ---
async function fetchCodeSplitFromGemini(apiKey, fullHtmlContent) {
    const exportModelName = "gemini-2.5-flash-preview-04-17"; // Hardcoded model for export
    const API_ENDPOINT = `${API_BASE_URL}${exportModelName}:generateContent?key=${apiKey}`;
    const prompt = `
You are an expert web developer. You are given a single HTML document that might contain inline CSS within <style> tags and inline JavaScript within <script> tags. Your task is to separate this document into three distinct components: HTML structure, CSS styles, and JavaScript code.

Follow these instructions carefully:
1.  **HTML Output**: This should be the main HTML structure. 
    *   If there were inline <style> tags, remove them. Add a <link rel="stylesheet" href="style.css"> in the <head> instead.
    *   If there were inline <script> tags (especially those not setting up initial variables or configurations that need to be in the head), try to move their content to what will become an external script.js file. Add <script src="script.js" defer><\\/script> before the closing </body> tag. For simple, short scripts that are clearly for page setup and are in the head, they can sometimes remain, but prefer externalizing functional code.
    *   Ensure the HTML output is clean and well-formed.
2.  **CSS Output**: This should contain ALL CSS rules extracted from any <style>...</style> blocks in the original HTML. If no <style> blocks were present, this should be an empty string or a comment indicating no CSS.
3.  **JavaScript Output**: This should contain ALL JavaScript code extracted from any <script>...</script> blocks (that are not JSON-LD or other non-executable script types). If no functional <script> blocks were present, this should be an empty string or a comment indicating no JavaScript.

Provide the output STRICTLY as a JSON object with the following keys: "html_code", "css_code", "js_code".

Example of desired JSON output format:
{
  "html_code": "<!DOCTYPE html>...<link rel=\\\"stylesheet\\\" href=\\\"style.css\\\"><script src=\\\"script.js\\\" defer><\\/script></body></html>",
  "css_code": "body { font-family: sans-serif; } ...",
  "js_code": "console.log(\\\'Hello World!\\\'); ...\"
}

Original HTML content:
\`\`\`html
${fullHtmlContent}\n\`\`\`

Return ONLY the JSON object. Do not include any other explanatory text or markdown formatting outside the JSON structure itself.
`;

    try {
        const response = await fetch(API_ENDPOINT, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ contents: [{ parts: [{ text: prompt }] }] })
        });

        if (!response.ok) {
            const errorData = await response.json().catch(() => null);
            const errorMsg = errorData?.error?.message || `HTTP Error: ${response.status}`;
            console.error('Gemini API Error:', errorMsg);
            throw new Error(`API Error: ${errorMsg}`);
        }

        const responseData = await response.json();
        const candidate = responseData.candidates?.[0];
        if (candidate?.content?.parts?.[0]?.text) {
            let rawText = candidate.content.parts[0].text;
            console.log("Raw response from Gemini for splitting:", rawText); // Log the raw response

            let jsonString = null;

            // Attempt 1: Look for JSON within markdown code blocks
            const markdownJsonMatch = rawText.match(/```json\n(\{[\s\S]*\})\n```/s) || rawText.match(/```\n(\{[\s\S]*\})\n```/s);
            if (markdownJsonMatch && markdownJsonMatch[1]) {
                jsonString = markdownJsonMatch[1];
            } else {
                // Attempt 2: Fallback to existing regex if no markdown block found or it's malformed
                const directJsonMatch = rawText.match(/\{.*\}/s);
                if (directJsonMatch) {
                    jsonString = directJsonMatch[0];
                }
            }

            if (jsonString) {
                try {
                    return JSON.parse(jsonString);
                } catch (parseError) {
                    console.error("Failed to parse JSON string from model:", jsonString, parseError);
                    throw new Error("JSON parsing failed after attempting to clean model response."); // Simplified error
                }
            }
            throw new Error("Clean JSON object not found in model's response after attempting to extract.");
        }
        throw new Error("No valid content found in model's response.");

    } catch (error) {
        console.error('Error fetching or parsing split code:', error);
        throw error; // Re-throw to be caught by caller
    }
}

// --- Full Prompt Display Modal Logic ---
function showFullPromptModal(fullPrompt) {
    if (!promptDisplayModalOverlayEl || !fullPromptTextEl) return;

    fullPromptTextEl.textContent = fullPrompt;

    // Animation handling
    promptDisplayModalOverlayEl.classList.remove('modal-anim-fade-out');
    promptDisplayModalOverlayEl.style.display = 'flex';
    promptDisplayModalOverlayEl.classList.add('modal-anim-fade-in');
}

function hideFullPromptModal() {
    if (!promptDisplayModalOverlayEl) return;

    promptDisplayModalOverlayEl.classList.remove('modal-anim-fade-in');
    promptDisplayModalOverlayEl.classList.add('modal-anim-fade-out');

    const handleAnimationEnd = () => {
        promptDisplayModalOverlayEl.style.display = 'none';
        promptDisplayModalOverlayEl.removeEventListener('animationend', handleAnimationEnd);
    };
    promptDisplayModalOverlayEl.addEventListener('animationend', handleAnimationEnd);
}

// --- Initialization ---
document.addEventListener('DOMContentLoaded', () => {
    console.log("DOMContentLoaded event fired.");

    apiKeyEl = document.getElementById('api-key');
    modelSelEl = document.getElementById('model-select');
    const codeOutputPre = document.getElementById('code-output');
    if (codeOutputPre) { codeOutputEl = codeOutputPre.querySelector('code'); } else { console.error("Code output <pre> not found");}
    errorMessageEl = document.getElementById('error-message');
    refinementLoadingIndicator = document.getElementById('refinement-loading-indicator');
    mainContentEl = document.getElementById('main-content');
    configButtonEl = document.getElementById('config-button');
    intervalSliderEl = document.getElementById('preview-interval-slider');
    intervalValueDisplayEl = document.getElementById('interval-value');
    fullscreenOverlayEl = document.getElementById('fullscreen-overlay');
    fullscreenIframeEl = document.getElementById('fullscreen-iframe');
    exitFullscreenBtnEl = document.getElementById('exit-fullscreen-btn');
    perspectiveViewportEl = document.getElementById('perspective-viewport');
    previewGridWrapperEl = document.getElementById('preview-grid-wrapper');
    historyPanelEl = document.getElementById('history-panel');
    historyPanelPlaceholderEl = document.getElementById('history-panel-placeholder');
    selectedCodeTitleH3El = document.getElementById('selected-code-title');
    mainContentTitleH1El = document.getElementById('main-content-title');
    mainContentSubtitleH2El = document.getElementById('main-content-subtitle');
    fullscreenHistoryNavEl = document.getElementById('fullscreen-history-nav');
    historyNavPrevBtnEl = document.getElementById('history-nav-prev');
    historyNavNextBtnEl = document.getElementById('history-nav-next');
    promptModalOverlayEl = document.getElementById('prompt-modal-overlay');
    promptModalContentEl = document.getElementById('prompt-modal-content');
    modalUserPromptEl = document.getElementById('modal-user-prompt');
    modalGenerateBtnEl = document.getElementById('modal-generate-button');
    modalCancelBtnEl = document.getElementById('modal-cancel-button');
    modalLoadingIndicatorEl = document.getElementById('modal-loading-indicator');
    modalRefinementCheckboxEl = document.getElementById('modal-refinement-checkbox');
    numVariationsSliderEl = document.getElementById('num-variations-slider');
    numVariationsValueDisplayEl = document.getElementById('num-variations-value');
    configModalOverlayEl = document.getElementById('config-modal-overlay');
    configModalContentEl = document.getElementById('config-modal-content');
    configModalCloseBtnEl = document.getElementById('config-modal-close-button');
    copyCodeButtonEl = document.getElementById('copy-code-button');
    exportCodeButtonEl = document.getElementById('export-code-button');
    historyToggleButtonEl = document.getElementById('history-toggle-button');
    historyArrowDownEl = document.getElementById('history-arrow-down');
    historyArrowUpEl = document.getElementById('history-arrow-up');
    newButtonEl = document.getElementById('new-button');
    confirmModalOverlayEl = document.getElementById('confirm-modal-overlay');
    confirmModalMessageEl = document.getElementById('confirm-modal-message');
    confirmModalConfirmBtnEl = document.getElementById('confirm-modal-confirm-button');
    confirmModalCancelBtnEl = document.getElementById('confirm-modal-cancel-button');
    historyNavLeftBtnEl = document.getElementById('history-nav-left-button');
    historyNavRightBtnEl = document.getElementById('history-nav-right-button');
    modalThinkingBudgetSliderEl = document.getElementById('modal-thinking-budget-slider');
    modalThinkingBudgetValueDisplayEl = document.getElementById('modal-thinking-budget-value');
    promptDisplayModalOverlayEl = document.getElementById('prompt-display-modal-overlay');
    promptDisplayModalContentEl = document.getElementById('prompt-display-modal-content');
    fullPromptTextEl = document.getElementById('full-prompt-text');
    promptDisplayModalCloseBtnEl = document.getElementById('prompt-display-modal-close-button');
    showPromptModalButtonEl = document.getElementById('show-prompt-modal-button'); // Added
    variationNavLeftBtnEl = document.getElementById('variation-nav-left');
    variationNavRightBtnEl = document.getElementById('variation-nav-right');

    // --- Elements for Initial Setup CTA ---
    initialSetupCtaEl = document.getElementById('initial-setup-cta');
    initialApiKeyInputEl = document.getElementById('initial-api-key-input');
    examplePromptsContainerEl = document.getElementById('example-prompts-container');


    // --- Check if all required elements exist ---
    let missingElements = [];
    const requiredElements = { apiKeyEl, modelSelEl, codeOutputEl, errorMessageEl, refinementLoadingIndicator, mainContentEl, configButtonEl, intervalSliderEl, intervalValueDisplayEl, fullscreenOverlayEl, fullscreenIframeEl, exitFullscreenBtnEl, perspectiveViewportEl, previewGridWrapperEl, historyPanelEl, historyPanelPlaceholderEl, selectedCodeTitleH3El, mainContentTitleH1El, mainContentSubtitleH2El, fullscreenHistoryNavEl, historyNavPrevBtnEl, historyNavNextBtnEl, promptModalOverlayEl, promptModalContentEl, modalUserPromptEl, modalGenerateBtnEl, modalCancelBtnEl, modalLoadingIndicatorEl, modalRefinementCheckboxEl, numVariationsSliderEl, numVariationsValueDisplayEl, configModalOverlayEl, configModalContentEl, configModalCloseBtnEl, copyCodeButtonEl, exportCodeButtonEl, historyToggleButtonEl, historyArrowDownEl, historyArrowUpEl, newButtonEl, confirmModalOverlayEl, confirmModalMessageEl, confirmModalConfirmBtnEl, confirmModalCancelBtnEl, historyNavLeftBtnEl, historyNavRightBtnEl, modalThinkingBudgetSliderEl, modalThinkingBudgetValueDisplayEl, promptDisplayModalOverlayEl, promptDisplayModalContentEl, fullPromptTextEl, promptDisplayModalCloseBtnEl, showPromptModalButtonEl, initialSetupCtaEl, initialApiKeyInputEl, examplePromptsContainerEl, variationNavLeftBtnEl, variationNavRightBtnEl }; // Added showPromptModalButtonEl
    for (const key in requiredElements) { if (!requiredElements[key]) { missingElements.push(key); } }

    if (missingElements.length > 0) {
        console.error("Initialization Error: Critical elements missing!", missingElements);
        if (document.body) { document.body.innerHTML = `<div class="fixed inset-0 bg-red-900 text-red-200 p-8 flex flex-col items-center justify-center text-center"><h2 class="text-2xl font-bold mb-4">Application Initialization Error</h2><p class="mb-2">Could not find element(s): ${missingElements.join(', ')}.</p><p>Please ensure the HTML structure is correct.</p></div>`; }
        else { alert(`Initialization Error: Critical elements missing: ${missingElements.join(', ')}.`); }
        return;
    }

    // --- Initial UI Setup ---
    showInitialPreviewStateUI();
    renderHistoryPanel();
    updateHistoryNavigationButtons(); // Initial button state
    updateVariationNavButtons(); // Initial state for variation nav buttons

    // --- Event Listeners ---
    if (newButtonEl) {
        newButtonEl.addEventListener('click', () => {
            showConfirmModal(
                'Start a new session? This will clear the current state.', 
                () => { location.reload(); } // Pass the reload logic as the callback
            );
        });
    }

    if (configButtonEl) configButtonEl.addEventListener('click', showConfigModal);
    if (showPromptModalButtonEl) showPromptModalButtonEl.addEventListener('click', showPromptModal); // Added listener for new button
    if (configModalCloseBtnEl) configModalCloseBtnEl.addEventListener('click', hideConfigModal);
    if (configModalOverlayEl) configModalOverlayEl.addEventListener('click', (e) => {
        if (e.target === configModalOverlayEl) { hideConfigModal(); }
    });

    if (exitFullscreenBtnEl) exitFullscreenBtnEl.addEventListener('click', exitFullscreen);
    if (historyNavPrevBtnEl) historyNavPrevBtnEl.addEventListener('click', showPreviousHistoryInFullscreen);
    if (historyNavNextBtnEl) historyNavNextBtnEl.addEventListener('click', showNextHistoryInFullscreen);

    if (modalGenerateBtnEl) modalGenerateBtnEl.addEventListener('click', handleModalGenerate);
    if (modalCancelBtnEl) modalCancelBtnEl.addEventListener('click', hidePromptModal);
    if (promptModalOverlayEl) promptModalOverlayEl.addEventListener('click', (e) => {
        if (e.target === promptModalOverlayEl) { hidePromptModal(); }
    });
    if (confirmModalOverlayEl) { // Add overlay click listener for confirm modal
        confirmModalOverlayEl.addEventListener('click', (e) => {
            if (e.target === confirmModalOverlayEl) { hideConfirmModal(); }
        });
    }
    if (promptDisplayModalOverlayEl) { // Listener for full prompt display modal overlay click
        promptDisplayModalOverlayEl.addEventListener('click', (e) => {
            if (e.target === promptDisplayModalOverlayEl) { hideFullPromptModal(); }
        });
    }
    if (promptDisplayModalCloseBtnEl) { // Listener for full prompt display modal close button
        promptDisplayModalCloseBtnEl.addEventListener('click', hideFullPromptModal);
    }
    if (mainContentSubtitleH2El) { // Listener for clicking the subtitle
        mainContentSubtitleH2El.addEventListener('click', (e) => {
            const fullPrompt = e.target.dataset.fullPrompt;
            if (fullPrompt) { // Check if the data attribute exists (meaning it was truncated)
                showFullPromptModal(fullPrompt);
            }
        });
    }
     if (modalUserPromptEl) modalUserPromptEl.addEventListener('keydown', (event) => {
         if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {
             event.preventDefault(); handleModalGenerate();
         }
     });
    if (numVariationsSliderEl) {
        numVariationsSliderEl.addEventListener('input', (event) => {
            if(numVariationsValueDisplayEl) numVariationsValueDisplayEl.textContent = event.target.value;
        });
        if(numVariationsValueDisplayEl) numVariationsValueDisplayEl.textContent = numVariationsSliderEl.value;
    }

    if (modalThinkingBudgetSliderEl) {
        modalThinkingBudgetSliderEl.addEventListener('input', (event) => {
            if(modalThinkingBudgetValueDisplayEl) modalThinkingBudgetValueDisplayEl.textContent = event.target.value;
        });
        if(modalThinkingBudgetValueDisplayEl) modalThinkingBudgetValueDisplayEl.textContent = modalThinkingBudgetSliderEl.value;
    }

    if (intervalSliderEl) {
        intervalSliderEl.addEventListener('input', (event) => {
            previewUpdateInterval = parseInt(event.target.value, 10);
            if(intervalValueDisplayEl) intervalValueDisplayEl.textContent = previewUpdateInterval;
        });
        previewUpdateInterval = parseInt(intervalSliderEl.value, 10);
        if(intervalValueDisplayEl) intervalValueDisplayEl.textContent = previewUpdateInterval;
    }

    if (copyCodeButtonEl) {
        copyCodeButtonEl.addEventListener('click', () => {
            if (codeOutputEl && codeOutputEl.textContent && codeOutputEl.textContent !== '// Select a variation or history item to view its code.') {
                const textToCopy = codeOutputEl.textContent;
                const textArea = document.createElement("textarea");
                textArea.value = textToCopy;
                textArea.style.position = "fixed"; // Prevent scrolling to bottom
                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();
                try {
                    const successful = document.execCommand('copy');
                    if (successful) {
                        const originalText = copyCodeButtonEl.innerHTML;
                        copyCodeButtonEl.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check inline-block mr-1"><polyline points="20 6 9 17 4 12"></polyline></svg> Copied!`;
                        copyCodeButtonEl.classList.add('copied');
                        setTimeout(() => {
                            copyCodeButtonEl.innerHTML = originalText;
                            copyCodeButtonEl.classList.remove('copied');
                        }, 2000);
                    } else {
                        console.error('Fallback: Failed to copy code using execCommand.');
                    }
                } catch (err) {
                    console.error('Fallback: Error copying code using execCommand: ', err);
                }
                document.body.removeChild(textArea);
            }
        });
    }

    if (exportCodeButtonEl) {
        exportCodeButtonEl.addEventListener('click', async () => {
            const currentCode = codeOutputEl?.textContent;
            const apiKey = apiKeyEl?.value;

            if (!currentCode || currentCode.startsWith('//') || currentCode.trim() === '') {
                alert('No code selected or available to export.');
                return;
            }
            if (!apiKey) {
                alert('API Key is missing. Please configure it in settings.');
                showConfigModal(); // Show config modal if API key is missing
                return;
            }
            if (typeof JSZip === 'undefined') {
                alert('JSZip library is not loaded. Export cannot proceed.');
                console.error("JSZip is not defined!");
                return;
            }

            const originalButtonContent = exportCodeButtonEl.innerHTML;
            exportCodeButtonEl.disabled = true;
            exportCodeButtonEl.innerHTML = '<svg class="animate-spin h-4 w-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg> Exporting...';

            try {
                const splitCode = await fetchCodeSplitFromGemini(apiKey, currentCode);
                
                if (!splitCode || typeof splitCode.html_code === 'undefined' || typeof splitCode.css_code === 'undefined' || typeof splitCode.js_code === 'undefined') {
                    throw new Error('Invalid response structure from code splitting model.');
                }

                const zip = new JSZip();
                zip.file("index.html", splitCode.html_code);
                zip.file("style.css", splitCode.css_code);
                zip.file("script.js", splitCode.js_code);

                const zipBlob = await zip.generateAsync({ type: "blob" });
                
                const downloadLink = document.createElement('a');
                downloadLink.href = URL.createObjectURL(zipBlob);
                downloadLink.download = "exported_code.zip";
                document.body.appendChild(downloadLink);
                downloadLink.click();
                document.body.removeChild(downloadLink);
                URL.revokeObjectURL(downloadLink.href); 

                exportCodeButtonEl.innerHTML = originalButtonContent;

            } catch (error) {
                console.error("Export failed:", error);
                alert(`Export failed: ${error.message}`);
                exportCodeButtonEl.innerHTML = originalButtonContent;
            } finally {
                exportCodeButtonEl.disabled = false;
            }
        });
    }

    if (historyToggleButtonEl && historyPanelEl && historyArrowDownEl && historyArrowUpEl) {
        historyToggleButtonEl.addEventListener('click', () => {
            const isCollapsed = historyPanelEl.classList.toggle('history-collapsed');
            const rootStyles = getComputedStyle(document.documentElement);
            const expandedHeight = rootStyles.getPropertyValue('--history-panel-expanded-height').trim();
            const collapsedHeight = rootStyles.getPropertyValue('--history-panel-collapsed-height').trim();

            if (isCollapsed) {
                document.documentElement.style.setProperty('--history-panel-current-height', collapsedHeight);
                historyArrowDownEl.classList.add('hidden');
                historyArrowUpEl.classList.remove('hidden');
            } else {
                document.documentElement.style.setProperty('--history-panel-current-height', expandedHeight);
                historyArrowDownEl.classList.remove('hidden');
                historyArrowUpEl.classList.add('hidden');
                // Ensure content is visible if re-expanding and it was set to display:none directly
                // This is now handled by the .history-collapsed CSS rule for children.
                 renderHistoryPanel(); // Re-render to ensure items are correctly displayed if they were hidden
            }
        });
    }

    if (historyNavLeftBtnEl) {
        historyNavLeftBtnEl.addEventListener('click', navigateToPreviousHistory);
    }
    if (historyNavRightBtnEl) {
        historyNavRightBtnEl.addEventListener('click', navigateToNextHistory);
    }

    // Variation navigation listeners
    if (variationNavLeftBtnEl) {
        variationNavLeftBtnEl.addEventListener('click', () => scrollVariations('left'));
    }
    if (variationNavRightBtnEl) {
        variationNavRightBtnEl.addEventListener('click', () => scrollVariations('right'));
    }
    if (perspectiveViewportEl) {
        perspectiveViewportEl.addEventListener('scroll', updateVariationNavButtons);
    }
    window.addEventListener('resize', updateVariationNavButtons);

    // Keyboard Shortcuts Listener
    document.addEventListener('keydown', (event) => {
        if (event.key === 'Escape') {
            // Check style.display instead of .visible class for animated modals
            if (configModalOverlayEl.style.display !== 'none' && !configModalOverlayEl.classList.contains('modal-anim-fade-out')) {
                hideConfigModal();
            } else if (promptModalOverlayEl.style.display !== 'none' && !promptModalOverlayEl.classList.contains('modal-anim-fade-out')) {
                 hidePromptModal();
            } else if (confirmModalOverlayEl.style.display !== 'none' && !confirmModalOverlayEl.classList.contains('modal-anim-fade-out')) { // Add Escape handler for confirm modal
                 hideConfirmModal();
            } else if (promptDisplayModalOverlayEl.style.display !== 'none' && !promptDisplayModalOverlayEl.classList.contains('modal-anim-fade-out')) { // Add Escape handler for prompt display modal
                 hideFullPromptModal();
            } else if (document.body.classList.contains('fullscreen-active')) {
                exitFullscreen();
            }
        }
        const targetTagName = event.target ? event.target.tagName.toLowerCase() : null;
        const isTypingInInputOrTextarea = targetTagName === 'input' || targetTagName === 'textarea';

        if (document.body.classList.contains('fullscreen-active') && currentFullscreenHistoryIndex !== -1 && !isTypingInInputOrTextarea) {
            if (event.key.toLowerCase() === 'w') {
                event.preventDefault();
                showPreviousHistoryInFullscreen();
            } else if (event.key.toLowerCase() === 'd') {
                event.preventDefault();
                showNextHistoryInFullscreen();
            }
        }
        if (event.altKey && !isTypingInInputOrTextarea) {
            if (event.key.toLowerCase() === 'p' || event.code === 'KeyP') {
                event.preventDefault();
                if (!promptModalOverlayEl.classList.contains('visible') && !configModalOverlayEl.classList.contains('visible')) {
                     showPromptModal();
                }
            }
            else if (event.key.toLowerCase() === 'j' || event.code === 'KeyJ') {
                event.preventDefault();
                if (lastGenerationConfig.prompt) {
                    console.log("Alt+R: Regenerating with last settings:", lastGenerationConfig);
                    modalUserPromptEl.value = lastGenerationConfig.prompt;
                    modalRefinementCheckboxEl.checked = lastGenerationConfig.isRefinement;
                    numVariationsSliderEl.value = lastGenerationConfig.numVariations;
                    if(numVariationsValueDisplayEl) numVariationsValueDisplayEl.textContent = numVariationsSliderEl.value;
                    modalThinkingBudgetSliderEl.value = lastGenerationConfig.thinkingBudget;
                    if(modalThinkingBudgetValueDisplayEl) modalThinkingBudgetValueDisplayEl.textContent = lastGenerationConfig.thinkingBudget;

                    if (lastGenerationConfig.isRefinement) {
                        activeTimelineIndex = lastGenerationConfig.refinedTimelineIndex;
                    }
                    handleModalGenerate();
                } else {
                    console.log("Alt+R: No last generation settings found. Opening prompt modal.");
                    showPromptModal();
                }
            }
            else if (event.key.toLowerCase() === 'o' || event.code === 'KeyO') {
                event.preventDefault();
                 if (!configModalOverlayEl.classList.contains('visible') && !promptModalOverlayEl.classList.contains('visible')) {
                    showConfigModal();
                }
            }
            // History navigation shortcuts (non-fullscreen)
            if (event.key === 'PageUp') {
                event.preventDefault();
                navigateToPreviousHistory();
            } else if (event.key === 'PageDown') {
                event.preventDefault();
                navigateToNextHistory();
            }
        }
    });
    console.log("Initialization setup complete.");
});