File size: 74,753 Bytes
63487d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', () => {
    const searchInput = document.getElementById('searchInput');
    const searchBtn = document.getElementById('searchBtn');
    
    // Split layout elements
    const splitLayout = document.getElementById('split-layout');
    const resultsList = document.getElementById('results-list');
    const searchStatsSummary = document.getElementById('search-stats-summary');
    const resultDetailCard = document.getElementById('result-detail-card');
    const emptyDetailState = document.getElementById('empty-detail-state');
    const loadingEl = document.getElementById('loading');
    const noResultsEl = document.getElementById('no-results');
    const exportCsvBtn = document.getElementById('exportCsvBtn');

    // API Key Settings
    const apiSettingsBtn = document.getElementById('apiSettingsBtn');
    if (apiSettingsBtn) {
        apiSettingsBtn.addEventListener('click', () => {
            const currentKey = localStorage.getItem('custom_gemini_api_key') || '';
            const newKey = prompt('系統已內建公用額度。若額度已用完,請填寫您個人的 Google Gemini API Key:\n(若要恢復使用系統預設額度,請清空並按確定)', currentKey);
            if (newKey !== null) {
                localStorage.setItem('custom_gemini_api_key', newKey.trim());
                if (newKey.trim()) {
                    alert('✅ 專屬 API Key 已儲存!接下來的標注將優先使用您的個人額度。');
                } else {
                    alert('🔄 已清除自訂 API Key,將恢復使用系統公用額度。');
                }
            }
        });
    }

    // Tab elements
    const tabBtns = document.querySelectorAll('.tab-btn');
    const tabContents = document.querySelectorAll('.tab-content');

    // Stats elements
    const statsLoading = document.getElementById('stats-loading');
    const statsDashboard = document.getElementById('stats-dashboard');
    let statsLoaded = false;
    let letterChart = null;
    let wordChart = null;
    let prefixChart = null;
    let suffixChart = null;
    let corpusPieChart = null;

    let currentResults = [];
    let currentQuery = '';
    let currentSearchType = '';

    // --- Tab Switching Logic ---
    tabBtns.forEach(btn => {
        btn.addEventListener('click', () => {
            const targetId = btn.getAttribute('data-tab');
            
            tabBtns.forEach(b => b.classList.remove('active'));
            tabContents.forEach(c => c.classList.remove('active'));
            
            btn.classList.add('active');
            document.getElementById(targetId).classList.add('active');

            if (targetId === 'tab-stats' && !statsLoaded) {
                loadStats();
            }
        });
    });

    // --- Search Logic ---
    searchBtn.addEventListener('click', performSearch);
    searchInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            performSearch();
        }
    });

    exportCsvBtn.addEventListener('click', () => {
        if (!currentQuery) return;
        const searchType = document.querySelector('input[name="searchType"]:checked').value;
        const matchMode = document.querySelector('input[name="matchMode"]:checked').value;
        
        const corpusCheckboxes = document.querySelectorAll('input[name="corpus"]:checked');
        let corporaParams = '';
        if (corpusCheckboxes.length > 0) {
            const corpora = Array.from(corpusCheckboxes).map(cb => cb.value);
            corporaParams = '&' + corpora.map(c => `corpora[]=${encodeURIComponent(c)}`).join('&');
        }

        const exportUrl = `/api/export?q=${encodeURIComponent(currentQuery)}&type=${encodeURIComponent(searchType)}&match_mode=${encodeURIComponent(matchMode)}${corporaParams}`;
        window.location.href = exportUrl;
    });

    async function performSearch() {
        const query = searchInput.value.trim();
        if (!query) return;

        // Get options
        const searchType = document.querySelector('input[name="searchType"]:checked').value;
        const matchMode = document.querySelector('input[name="matchMode"]:checked').value;
        
        const corpusCheckboxes = document.querySelectorAll('input[name="corpus"]:checked');
        let corporaParams = '';
        if (corpusCheckboxes.length > 0) {
            const corpora = Array.from(corpusCheckboxes).map(cb => cb.value);
            corporaParams = '&' + corpora.map(c => `corpora[]=${encodeURIComponent(c)}`).join('&');
        }

        // Reset UI
        resultsList.innerHTML = '';
        searchStatsSummary.innerHTML = '';
        resultDetailCard.innerHTML = '';
        
        splitLayout.classList.add('hidden');
        searchStatsSummary.classList.add('hidden');
        resultDetailCard.innerHTML = '';
        emptyDetailState.style.display = 'flex';
        noResultsEl.classList.add('hidden');
        loadingEl.classList.remove('hidden');
        exportCsvBtn.classList.add('hidden');

        try {
            const url = `/api/search?q=${encodeURIComponent(query)}&type=${encodeURIComponent(searchType)}&match_mode=${encodeURIComponent(matchMode)}${corporaParams}`;
            const response = await fetch(url);
            if (!response.ok) throw new Error('Network response was not ok');
            
            const data = await response.json();
            loadingEl.classList.add('hidden');
            
            if (data.results.length === 0) {
                noResultsEl.classList.remove('hidden');
            } else {
                currentResults = data.results;
                currentQuery = query;
                currentSearchType = searchType;
                
                splitLayout.classList.remove('hidden');
                exportCsvBtn.classList.remove('hidden');
                renderSearchSummary(data, query, searchType, matchMode);
                renderResultsList(data.results, query, searchType);
            }
        } catch (error) {
            console.error('Error fetching data:', error);
            loadingEl.classList.add('hidden');
            noResultsEl.classList.remove('hidden');
            noResultsEl.innerHTML = `<p style="color: #ef4444;">發生錯誤,無法取得資料。</p>`;
        }
    }

    function renderSearchSummary(data, query, searchType, matchMode) {
        searchStatsSummary.innerHTML = '';
        searchStatsSummary.classList.remove('hidden');
        
        let collocationsHtml = '';
        if (data.collocations && data.collocations.length > 0) {
            const badgesHtml = data.collocations.map(word => 
                `<span class="collocation-badge" data-word="${word}">${word}</span>`
            ).join('');
            
            collocationsHtml = `
                <div style="margin-top: 1rem;">
                    <span style="color: var(--text-muted); margin-right: 0.5rem;">常用共現關聯詞:</span>
                    <div class="collocations-container">${badgesHtml}</div>
                </div>
            `;
        }
        
        let positionsHtml = '';
        if (data.positions) {
            const total = data.positions.initial + data.positions.medial + data.positions.final;
            if (total > 0) {
                const pInit = Math.round((data.positions.initial / total) * 100);
                const pMed = Math.round((data.positions.medial / total) * 100);
                const pFin = Math.round((data.positions.final / total) * 100);
                
                positionsHtml = `
                    <div class="stats-grid" id="position-filters">
                        <div class="stat-item position-btn" data-pos="initial" style="cursor: pointer; transition: 0.2s;">
                            <div class="label">句首 (Initial)</div>
                            <div class="value">${data.positions.initial} 次</div>
                            <div class="pct">${pInit}%</div>
                        </div>
                        <div class="stat-item position-btn" data-pos="medial" style="cursor: pointer; transition: 0.2s;">
                            <div class="label">句中 (Medial)</div>
                            <div class="value">${data.positions.medial} 次</div>
                            <div class="pct">${pMed}%</div>
                        </div>
                        <div class="stat-item position-btn" data-pos="final" style="cursor: pointer; transition: 0.2s;">
                            <div class="label">句尾 (Final)</div>
                            <div class="value">${data.positions.final} 次</div>
                            <div class="pct">${pFin}%</div>
                        </div>
                    </div>
                `;
            }
        }

        let summaryText = '';
        if (matchMode === 'partial') {
            summaryText = `<div style="line-height: 1.8;">💡 關鍵字 <span class="highlight-count">"${query}"</span> 在所選語料中部分搜尋共出現 <span class="highlight-count">${data.total_occurrences}</span> 次,其中精準搜尋共出現 <span class="highlight-count">${data.exact_occurrences}</span> 次。<br>`;
            if (data.corpus_breakdown) {
                // First show all corpus counts
                for (const [corpus, counts] of Object.entries(data.corpus_breakdown)) {
                    summaryText += `💡 其中在 "${corpus}" 中部分搜尋共出現 <span class="highlight-count">${counts.total}</span> 次。<br>`;
                }
                // Then show subcategories for specific corpora
                for (const [corpus, counts] of Object.entries(data.corpus_breakdown)) {
                    if (['族語E樂園', '語推組織語料'].includes(corpus) && counts.subcategories && Object.keys(counts.subcategories).length > 0) {
                        const subBtnsHtml = Object.entries(counts.subcategories).map(([subcat, count]) => {
                            return `<div class="stat-item subcat-btn" data-subcat="${subcat}" style="cursor: pointer; transition: 0.2s; background: rgba(251, 146, 60, 0.1); border: 1px solid rgba(251, 146, 60, 0.3); padding: 0.5rem; border-radius: 6px; display: inline-block; min-width: 80px; text-align: center;">
                                <div style="color: #fb923c; font-weight: bold; font-size: 0.9rem;">${subcat}</div>
                                <div style="color: #e2e8f0; font-size: 0.85rem;">${count} 筆</div>
                            </div>`;
                        }).join('');
                        summaryText += `<div style="margin-top: 0.5rem; margin-bottom: 1rem; display: flex; flex-wrap: wrap; gap: 0.5rem;" id="subcat-filters">${subBtnsHtml}</div>`;
                    }
                }
            }
            summaryText += `</div>`;
        } else {
            summaryText = `<div style="line-height: 1.8;">💡 關鍵字 <span class="highlight-count">"${query}"</span> 在所選語料中共精準出現了 <span class="highlight-count">${data.total_occurrences}</span> 次。<br>`;
            if (data.corpus_breakdown) {
                // First show all corpus counts
                for (const [corpus, counts] of Object.entries(data.corpus_breakdown)) {
                    summaryText += `💡 其中在 "${corpus}" 中共精準出現了 <span class="highlight-count">${counts.exact}</span> 次。<br>`;
                }
                // Then show subcategories for specific corpora
                for (const [corpus, counts] of Object.entries(data.corpus_breakdown)) {
                    if (['族語E樂園', '語推組織語料'].includes(corpus) && counts.subcategories && Object.keys(counts.subcategories).length > 0) {
                        const subBtnsHtml = Object.entries(counts.subcategories).map(([subcat, count]) => {
                            return `<div class="stat-item subcat-btn" data-subcat="${subcat}" style="cursor: pointer; transition: 0.2s; background: rgba(251, 146, 60, 0.1); border: 1px solid rgba(251, 146, 60, 0.3); padding: 0.5rem; border-radius: 6px; display: inline-block; min-width: 80px; text-align: center;">
                                <div style="color: #fb923c; font-weight: bold; font-size: 0.9rem;">${subcat}</div>
                                <div style="color: #e2e8f0; font-size: 0.85rem;">${count} 筆</div>
                            </div>`;
                        }).join('');
                        summaryText += `<div style="margin-top: 0.5rem; margin-bottom: 1rem; display: flex; flex-wrap: wrap; gap: 0.5rem;" id="subcat-filters">${subBtnsHtml}</div>`;
                    }
                }
            }
            summaryText += `</div>`;
        }
        
        let ngramsHtml = '';
        if (data.ngrams && (data.ngrams.bigrams?.length > 0 || data.ngrams.trigrams?.length > 0)) {
            const renderNgram = (ng) => `<div style="background: rgba(0,0,0,0.3); padding: 0.3rem 0.6rem; border-radius: 4px; display: inline-flex; align-items: center; gap: 0.5rem; margin: 0.2rem;"><span style="color: #e2e8f0;">${ng.ngram}</span><span style="color: #60a5fa; font-size: 0.85em;">${ng.count}次</span></div>`;
            
            const bigramsHtml = (data.ngrams.bigrams || []).map(renderNgram).join('');
            const trigramsHtml = (data.ngrams.trigrams || []).map(renderNgram).join('');
            
            ngramsHtml = `
                <div style="margin-top: 1.5rem; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 1rem;">
                    <div style="color: var(--text-muted); margin-bottom: 0.5rem; font-weight: bold;">連詞分析 (N-grams):</div>
                    ${bigramsHtml ? `<div style="margin-bottom: 0.5rem;"><span style="color: #94a3b8; font-size: 0.9em; width: 60px; display: inline-block;">雙詞:</span> ${bigramsHtml}</div>` : ''}
                    ${trigramsHtml ? `<div><span style="color: #94a3b8; font-size: 0.9em; width: 60px; display: inline-block;">三詞:</span> ${trigramsHtml}</div>` : ''}
                </div>
            `;
        }

        let morphHtml = '';
        if (data.morphology) {
            const familyBtns = data.morphology.family.map(fw => 
                `<button onclick="document.getElementById('searchInput').value='${fw}'; document.getElementById('searchBtn').click();" style="background: rgba(99,102,241,0.2); border: 1px solid rgba(99,102,241,0.5); color: #818cf8; padding: 0.3rem 0.6rem; border-radius: 4px; cursor: pointer; transition: all 0.2s; margin: 0.2rem;" onmouseover="this.style.background='rgba(99,102,241,0.4)'" onmouseout="this.style.background='rgba(99,102,241,0.2)'">${fw}</button>`
            ).join('');
            
            morphHtml = `
                <div style="margin-top: 1.5rem; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 1rem;">
                    <div style="color: #a78bfa; margin-bottom: 0.5rem; font-weight: bold;">✨ 構詞學分析 (Morphology):</div>
                    <div style="margin-bottom: 0.5rem;"><span style="color: #94a3b8; font-size: 0.9em; width: 80px; display: inline-block;">推測詞根:</span> <span style="color: #e2e8f0; font-weight: bold;">${data.morphology.root}</span></div>
                    <div><span style="color: #94a3b8; font-size: 0.9em; width: 80px; display: inline-block; vertical-align: top;">家族詞彙:</span> <div style="display: inline-block; width: calc(100% - 85px);">${familyBtns}</div></div>
                </div>
            `;
        }

        searchStatsSummary.innerHTML = `
            <p>${summaryText}</p>
            ${morphHtml}
            ${collocationsHtml}
            ${ngramsHtml}
            ${positionsHtml}
        `;

        const badges = searchStatsSummary.querySelectorAll('.collocation-badge');
        badges.forEach(badge => {
            badge.addEventListener('click', () => {
                const word = badge.getAttribute('data-word');
                searchInput.value = word;
                document.querySelector('input[name="searchType"][value="truku"]').checked = true;
                performSearch();
            });
        });

        // Position Filtering
        const posBtns = searchStatsSummary.querySelectorAll('.position-btn');
        let activePos = null;
        
        posBtns.forEach(btn => {
            btn.addEventListener('click', () => {
                const pos = btn.getAttribute('data-pos');
                if (activePos === pos) {
                    // Toggle off
                    activePos = null;
                    posBtns.forEach(b => {
                        b.style.background = 'rgba(0,0,0,0.2)';
                        b.style.border = 'none';
                    });
                    renderResultsList(data.results, query, searchType);
                } else {
                    // Toggle on
                    activePos = pos;
                    posBtns.forEach(b => {
                        b.style.background = 'rgba(0,0,0,0.2)';
                        b.style.border = 'none';
                    });
                    btn.style.background = 'rgba(59, 130, 246, 0.2)';
                    btn.style.border = '1px solid rgba(59, 130, 246, 0.5)';
                    
                    const filtered = data.results.filter(r => r.positions && r.positions.includes(pos));
                    renderResultsList(filtered, query, searchType);
                }
            });
        });

        // Subcategory Filtering
        const subcatBtns = searchStatsSummary.querySelectorAll('.subcat-btn');
        let activeSubcat = null;
        
        subcatBtns.forEach(btn => {
            btn.addEventListener('click', () => {
                const subcat = btn.getAttribute('data-subcat');
                if (activeSubcat === subcat) {
                    activeSubcat = null;
                    subcatBtns.forEach(b => {
                        b.style.background = 'rgba(251, 146, 60, 0.1)';
                        b.style.border = '1px solid rgba(251, 146, 60, 0.3)';
                    });
                    renderResultsList(data.results, query, searchType);
                } else {
                    activeSubcat = subcat;
                    subcatBtns.forEach(b => {
                        b.style.background = 'rgba(0,0,0,0.2)';
                        b.style.border = '1px solid transparent';
                    });
                    btn.style.background = 'rgba(251, 146, 60, 0.3)';
                    btn.style.border = '1px solid rgba(251, 146, 60, 0.8)';
                    
                    const filtered = data.results.filter(r => r.detailed_source && r.detailed_source.includes(subcat));
                    renderResultsList(filtered, query, searchType);
                }
            });
        });
    }

    function renderResultsList(results, query, searchType) {
        resultsList.innerHTML = '';
        
        let displayResults = results;
        let isTruncated = false;
        if (results.length > 200) {
            displayResults = results.slice(0, 200);
            isTruncated = true;
        }
        
        if (displayResults.length === 0) {
            resultsList.innerHTML = '<div style="padding: 1rem; color: #94a3b8; text-align: center;">沒有符合此條件的結果。</div>';
            return;
        }
        
        displayResults.forEach((item, index) => {
            const li = document.createElement('div');
            li.className = `list-item ${index === 0 ? 'active' : ''}`;
            
            let displayTruku = item.truku_sentence || '';
            let displayChinese = item.chinese_translation || '';
            const highlightRegex = new RegExp(`(${escapeRegExp(query)})`, 'gi');
            
            if (searchType === 'truku' || searchType === 'source') {
                displayTruku = displayTruku.replace(highlightRegex, '<span style="color: var(--highlight-bg); font-weight: bold;">$1</span>');
            } else if (searchType === 'chinese') {
                displayChinese = displayChinese.replace(highlightRegex, '<span style="color: var(--highlight-bg); font-weight: bold;">$1</span>');
            }

            let badgeHtml = '';
            if (item.is_exact !== undefined) {
                badgeHtml = item.is_exact 
                    ? '<span class="list-item-badge badge-exact">精準</span>'
                    : '<span class="list-item-badge badge-partial">部分</span>';
            }
            
            let corpusBadge = '';
            let subcatBadge = '';
            if (item.corpus_source === '族語辭典') {
                corpusBadge = '<span class="list-item-badge badge-corpus-dict">辭典</span>';
            } else if (item.corpus_source === '族語E樂園') {
                corpusBadge = '<span class="list-item-badge badge-corpus-klokah">E樂園</span>';
                if (item.detailed_source && item.detailed_source.trim() !== '') {
                    subcatBadge = `<span class="list-item-badge badge-corpus-subcat">${item.detailed_source.replace(/^族語E樂園 - /, '')}</span>`;
                }
            } else if (item.corpus_source === '族語文學') {
                corpusBadge = '<span class="list-item-badge badge-corpus-lit">文學</span>';
            } else if (item.corpus_source === '語推組織語料') {
                corpusBadge = '<span class="list-item-badge badge-corpus-org">語推</span>';
                if (item.detailed_source && item.detailed_source.trim() !== '') {
                    subcatBadge = `<span class="list-item-badge badge-corpus-subcat">${item.detailed_source}</span>`;
                }
            } else if (item.corpus_source === '族語資料庫語料') {
                corpusBadge = '<span class="list-item-badge badge-corpus-db">資料庫</span>';
            } else if (item.corpus_source) {
                corpusBadge = `<span class="list-item-badge badge-corpus-dict">${item.corpus_source}</span>`;
            }

            li.innerHTML = `
                <div class="list-item-truku">${corpusBadge} ${subcatBadge} ${displayTruku} ${badgeHtml}</div>
                <div class="list-item-chinese">${displayChinese}</div>
            `;
            
            li.addEventListener('click', () => {
                document.querySelectorAll('.list-item').forEach(el => el.classList.remove('active'));
                li.classList.add('active');
                renderDetailCard(item);
            });
            
            resultsList.appendChild(li);
        });

        if (isTruncated) {
            const truncRow = document.createElement('div');
            truncRow.style.padding = '1rem';
            truncRow.style.textAlign = 'center';
            truncRow.style.color = '#94a3b8';
            truncRow.style.fontSize = '0.9rem';
            truncRow.innerHTML = `已隱藏其餘 ${results.length - 200} 筆結果。請縮小搜尋範圍或點擊「匯出 CSV」檢視全部資料。`;
            resultsList.appendChild(truncRow);
        }

        if (displayResults.length > 0) {
            renderDetailCard(displayResults[0]);
        }
    }

    function renderDetailCard(item) {
        emptyDetailState.style.display = 'none';
        resultDetailCard.classList.remove('hidden');
        
        let displayTruku = item.truku_sentence || '';
        let displayChinese = item.chinese_translation || '';
        let displaySource = item.source_word || '';
        let kwicText = item.kwic_sentence || '';

        const highlightRegex = new RegExp(`(${escapeRegExp(currentQuery)})`, 'gi');
        
        if (currentSearchType === 'truku') {
            kwicText = kwicText.replace(highlightRegex, '<span class="keyword-highlight">$1</span>');
            displayTruku = displayTruku.replace(highlightRegex, '<span style="color: var(--highlight-bg); font-weight: bold;">$1</span>');
        } else if (currentSearchType === 'chinese') {
            displayChinese = displayChinese.replace(highlightRegex, '<span style="color: var(--highlight-bg); font-weight: bold;">$1</span>');
        } else if (currentSearchType === 'source') {
            displaySource = displaySource.replace(highlightRegex, '<span style="color: var(--highlight-bg); font-weight: bold;">$1</span>');
            displayTruku = displayTruku.replace(highlightRegex, '<span style="color: var(--highlight-bg); font-weight: bold;">$1</span>');
        }

        const kwicBlock = currentSearchType === 'truku' 
            ? `<div class="kwic-text">搭配詞: ${kwicText}</div>`
            : `<div class="truku-text">${displayTruku}</div>`;

        resultDetailCard.innerHTML = `
            <div class="card">
                <div class="card-header" style="display: flex; justify-content: space-between; align-items: flex-start;">
                    <div>
                        <h2>${displaySource || '詳細資訊'}</h2>
                        <span class="badge ${item.is_exact ? 'badge-exact' : 'badge-partial'}">
                            ${item.is_exact ? '精準匹配' : '部分匹配'}
                        </span>
                    </div>
                    <a href="#" onclick="openReportModal('sentence_error', ${item.id}, \`${(item.truku_sentence || item.source_word || '').replace(/`/g, '')}\`); return false;" 
                       class="report-btn" style="padding: 0.3rem 0.6rem; font-size: 0.8rem; background: rgba(245, 158, 11, 0.1); color: #f59e0b; border-color: rgba(245, 158, 11, 0.3);">
                        <svg viewBox="0 0 24 24" width="14" height="14" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path><line x1="12" y1="9" x2="12" y2="13"></line><line x1="12" y1="17" x2="12.01" y2="17"></line></svg>
                        回報此句
                    </a>
                </div>
                <div class="card-body">
                    ${kwicBlock}
                    ${currentSearchType === 'truku' ? `<div class="truku-text" style="font-size: 0.9em; color: var(--text-muted);">完整例句: ${displayTruku}</div>` : ''}
                    <div class="chinese-text">${displayChinese}</div>
                    ${item.full_explanation ? `<div class="explanation">${item.full_explanation.replace(/;/g, '<br>')}</div>` : ''}
                    
                    <!-- IGT Annotation Trigger -->
                    <div style="margin-top: 1.2rem; padding-top: 1rem; border-top: 1px solid rgba(255,255,255,0.08); display:flex; gap:0.6rem; flex-wrap:wrap; align-items:center;">
                        <button 
                            onclick="triggerGlosser(this, \`${(item.truku_sentence || '').replace(/`/g, "'")}\`, \`${(item.chinese_translation || '').replace(/`/g, "'")}\`)"
                            style="background: rgba(139,92,246,0.15); color: #a78bfa; border: 1px solid rgba(139,92,246,0.4); padding: 0.35rem 0.9rem; border-radius: 6px; cursor: pointer; font-size: 0.85rem; font-weight: 600; transition: all 0.2s; display: inline-flex; align-items: center; gap: 0.4rem;"
                            onmouseover="this.style.background='rgba(139,92,246,0.3)'"
                            onmouseout="this.style.background='rgba(139,92,246,0.15)'"
                        >
                            🏷️ 快速標注 (IGT)
                        </button>
                        <button
                            onclick="sendToGlosser(\`${(item.truku_sentence || '').replace(/`/g, "'")}\`, \`${(item.chinese_translation || '').replace(/`/g, "'")}\`)"
                            style="background: rgba(52,211,153,0.12); color: #34d399; border: 1px solid rgba(52,211,153,0.3); padding: 0.35rem 0.9rem; border-radius: 6px; cursor: pointer; font-size: 0.85rem; font-weight: 600; transition: all 0.2s; display: inline-flex; align-items: center; gap: 0.4rem;"
                            onmouseover="this.style.background='rgba(52,211,153,0.25)'"
                            onmouseout="this.style.background='rgba(52,211,153,0.12)'"
                        >
                            ↗️ 送往標注頁籤
                        </button>
                    </div>
                    <div class="igt-output-area" style="display:none; margin-top: 1rem;"></div>
                </div>
            </div>
        `;
    }

    function escapeRegExp(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    }

    // --- IGT Glosser Integration ---
    // Calls truku-glosser backend (port 8000) and renders 4-line IGT format
    // --- Stats Logic ---
    async function loadStats() {
        statsLoading.classList.remove('hidden');
        
        try {
            const response = await fetch('/api/stats');
            if (!response.ok) throw new Error('Network response was not ok');
            const data = await response.json();
            
            statsLoading.classList.add('hidden');
            statsDashboard.classList.remove('hidden');
            
            document.getElementById('stat-entries').innerText = data.total_entries.toLocaleString();
            document.getElementById('stat-sentences').innerText = data.total_sentences.toLocaleString();
            document.getElementById('stat-avg-len').innerText = data.avg_sentence_length.toLocaleString();

            initSubTabs();
            renderCharts(data);
            statsLoaded = true;
        } catch (error) {
            console.error('Error fetching stats:', error);
            statsLoading.innerHTML = `<p style="color: #ef4444;">載入統計資料失敗。</p>`;
        }
    }

    function initSubTabs() {
        const subTabBtns = document.querySelectorAll('.sub-tab-btn');
        const subTabContents = document.querySelectorAll('.sub-tab-content');
        
        subTabBtns.forEach(btn => {
            btn.addEventListener('click', () => {
                subTabBtns.forEach(b => b.classList.remove('active'));
                subTabContents.forEach(c => c.classList.add('hidden'));
                
                btn.classList.add('active');
                document.getElementById(btn.dataset.subtab).classList.remove('hidden');
                
                // Force charts to resize when their container becomes visible
                setTimeout(() => {
                    if (wordChart) wordChart.resize();
                    if (letterChart) letterChart.resize();
                    if (prefixChart) prefixChart.resize();
                    if (suffixChart) suffixChart.resize();
                }, 10);
            });
        });
    }

    function renderCharts(data) {
        Chart.defaults.color = '#94a3b8';
        Chart.defaults.borderColor = 'rgba(255, 255, 255, 0.1)';
        
        if (typeof ChartDataLabels !== 'undefined') {
            Chart.register(ChartDataLabels);
        }

        // 1. Corpus Pie Chart
        const pieCtx = document.getElementById('corpusPieChart').getContext('2d');
        const corpusKeys = Object.keys(data.corpus_distribution);
        const corpusValues = corpusKeys.map(k => data.corpus_distribution[k]);
        const corpusColors = corpusKeys.map(k => {
            if (k === '族語辭典') return '#60a5fa';
            if (k === '族語E樂園') return '#a855f7';
            if (k === '族語文學') return '#10b981';
            return '#f59e0b';
        });

        if (corpusPieChart) corpusPieChart.destroy();
        corpusPieChart = new Chart(pieCtx, {
            type: 'doughnut',
            data: {
                labels: corpusKeys,
                datasets: [{
                    data: corpusValues,
                    backgroundColor: corpusColors,
                    borderWidth: 0
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: { position: 'bottom', labels: { color: '#e2e8f0' } },
                    datalabels: {
                        color: '#fff',
                        font: { weight: 'bold' },
                        formatter: (value, ctx) => {
                            let sum = 0;
                            let dataArr = ctx.chart.data.datasets[0].data;
                            dataArr.map(data => { sum += data; });
                            let percentage = (value*100 / sum).toFixed(1)+"%";
                            return percentage;
                        }
                    }
                }
            }
        });

        // 2. Letter Chart
        const letterCtx = document.getElementById('letterChart').getContext('2d');
        const letterKeys = Object.keys(data.first_letters).sort();
        const letterValues = letterKeys.map(k => data.first_letters[k]);
        
        const bgColors = letterKeys.map((_, i) => `hsla(${i * (360/letterKeys.length)}, 70%, 60%, 0.6)`);
        const borderColors = letterKeys.map((_, i) => `hsla(${i * (360/letterKeys.length)}, 70%, 60%, 1)`);

        if (letterChart) letterChart.destroy();
        letterChart = new Chart(letterCtx, {
            type: 'bar',
            data: {
                labels: letterKeys.map(k => k.toUpperCase()),
                datasets: [{
                    label: '詞條數量',
                    data: letterValues,
                    backgroundColor: bgColors,
                    borderColor: borderColors,
                    borderWidth: 1
                }]
            },
            options: { 
                responsive: true, 
                plugins: { 
                    legend: { display: false },
                    datalabels: {
                        color: '#e2e8f0',
                        anchor: 'end',
                        align: 'top',
                        formatter: Math.round,
                        font: { weight: 'bold' }
                    }
                } 
            }
        });

        // Prefix Chart
        if (data.prefixes) {
            const preCtx = document.getElementById('prefixChart').getContext('2d');
            const preKeys = Object.keys(data.prefixes);
            const preVals = preKeys.map(k => data.prefixes[k]);
            if (prefixChart) prefixChart.destroy();
            prefixChart = new Chart(preCtx, {
                type: 'bar',
                data: {
                    labels: preKeys,
                    datasets: [{
                        data: preVals,
                        backgroundColor: 'rgba(236, 72, 153, 0.6)',
                        borderColor: 'rgba(236, 72, 153, 1)',
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    plugins: { legend: { display: false }, datalabels: { color: '#e2e8f0', anchor: 'end', align: 'top', font: { weight: 'bold' } } }
                }
            });
        }

        // Suffix Chart
        if (data.suffixes) {
            const sufCtx = document.getElementById('suffixChart').getContext('2d');
            const sufKeys = Object.keys(data.suffixes);
            const sufVals = sufKeys.map(k => data.suffixes[k]);
            if (suffixChart) suffixChart.destroy();
            suffixChart = new Chart(sufCtx, {
                type: 'bar',
                data: {
                    labels: sufKeys,
                    datasets: [{
                        data: sufVals,
                        backgroundColor: 'rgba(16, 185, 129, 0.6)',
                        borderColor: 'rgba(16, 185, 129, 1)',
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    plugins: { legend: { display: false }, datalabels: { color: '#e2e8f0', anchor: 'end', align: 'top', font: { weight: 'bold' } } }
                }
            });
        }

        // 3. Top Words
        const top50 = data.top_words;
        const topWordsKeys = top50.map(item => item.word);
        const topWordsValues = top50.map(item => item.count);

        if (wordChart) wordChart.destroy();
        const wordCtx = document.getElementById('wordChart').getContext('2d');
        wordChart = new Chart(wordCtx, {
            type: 'bar',
            data: {
                labels: topWordsKeys,
                datasets: [{
                    label: '出現次數',
                    data: topWordsValues,
                    backgroundColor: '#3b82f6',
                    borderRadius: 4
                }]
            },
            options: {
                indexAxis: 'y',
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: { display: false },
                    datalabels: {
                        color: '#e2e8f0',
                        anchor: 'end',
                        align: 'right',
                        formatter: Math.round,
                        font: { weight: 'bold', size: 10 }
                    }
                },
                scales: {
                    x: { grid: { color: 'rgba(255,255,255,0.05)' }, max: Math.max(...topWordsValues) * 1.15 },
                    y: { grid: { display: false } }
                }
            }
        });

        // Populate Left List
        const listContainer = document.getElementById('stats-word-list');
        listContainer.innerHTML = top50.map(item => `
            <div class="stats-list-row">
                <div class="col-word">
                    ${item.word}
                    ${item.translation ? `<span style="color:#94a3b8; font-size:0.85em; margin-left:0.5rem;">(${item.translation})</span>` : ''}
                </div>
                <div class="col-count">${item.count.toLocaleString()}</div>
            </div>
        `).join('');
        
        // Populate N-grams
        const bigramsContainer = document.getElementById('stats-bigrams');
        bigramsContainer.innerHTML = Object.entries(data.top_bigrams).sort((a,b)=>b[1]-a[1]).map(item => `
            <div class="ngram-item">
                <span class="ngram-text">${item[0]}</span>
                <span class="ngram-count">${item[1].toLocaleString()}次</span>
            </div>
        `).join('');
        
        const trigramsContainer = document.getElementById('stats-trigrams');
        trigramsContainer.innerHTML = Object.entries(data.top_trigrams).sort((a,b)=>b[1]-a[1]).map(item => `
            <div class="ngram-item">
                <span class="ngram-text">${item[0]}</span>
                <span class="ngram-count">${item[1].toLocaleString()}次</span>
            </div>
        `).join('');

        // Populate Corpus Compare Table
        if (data.top_words_by_corpus) {
            const tableBody = document.querySelector('#corpus-compare-table tbody');
            const dictTop = data.top_words_by_corpus['族語辭典'] || [];
            const klokahTop = data.top_words_by_corpus['族語E樂園'] || [];
            const litTop = data.top_words_by_corpus['族語文學'] || [];
            
            let tableHtml = '';
            for (let i = 0; i < 50; i++) {
                const dictItem = dictTop[i] || {word: '-', count: 0, translation: ''};
                const klokahItem = klokahTop[i] || {word: '-', count: 0, translation: ''};
                const litItem = litTop[i] || {word: '-', count: 0, translation: ''};
                
                tableHtml += `
                    <tr style="border-bottom: 1px solid rgba(255,255,255,0.05); transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.05)'" onmouseout="this.style.background='transparent'">
                        <td style="padding: 0.75rem 1rem; color: #94a3b8;">${i + 1}</td>
                        <td style="padding: 0.75rem 1rem;">
                            <span style="color: #e2e8f0; font-weight: 500;">${dictItem.word}</span>
                            ${dictItem.word !== '-' ? `<span style="color: #60a5fa; margin-left: 0.5rem; font-size: 0.85em;">(${dictItem.count})</span><span style="color: rgba(255,255,255,0.4); margin-left: 0.3rem; font-size: 0.8em;">${dictItem.percentage}%</span>` : ''}
                            ${dictItem.translation ? `<div style="color: #64748b; font-size: 0.8em; margin-top: 0.2rem;">${dictItem.translation}</div>` : ''}
                        </td>
                        <td style="padding: 0.75rem 1rem;">
                            <span style="color: #e2e8f0; font-weight: 500;">${klokahItem.word}</span>
                            ${klokahItem.word !== '-' ? `<span style="color: #fde047; margin-left: 0.5rem; font-size: 0.85em;">(${klokahItem.count})</span><span style="color: rgba(255,255,255,0.4); margin-left: 0.3rem; font-size: 0.8em;">${klokahItem.percentage}%</span>` : ''}
                            ${klokahItem.translation ? `<div style="color: #64748b; font-size: 0.8em; margin-top: 0.2rem;">${klokahItem.translation}</div>` : ''}
                        </td>
                        <td style="padding: 0.75rem 1rem;">
                            <span style="color: #e2e8f0; font-weight: 500;">${litItem.word}</span>
                            ${litItem.word !== '-' ? `<span style="color: #10b981; margin-left: 0.5rem; font-size: 0.85em;">(${litItem.count})</span><span style="color: rgba(255,255,255,0.4); margin-left: 0.3rem; font-size: 0.8em;">${litItem.percentage}%</span>` : ''}
                            ${litItem.translation ? `<div style="color: #64748b; font-size: 0.8em; margin-top: 0.2rem;">${litItem.translation}</div>` : ''}
                        </td>
                    </tr>
                `;
            }
            if (tableBody) tableBody.innerHTML = tableHtml;
        }
    }
});

window.openReportModal = function(type, id = null, sentence = '') {
    const modal = document.getElementById('reportModal');
    const typeSelect = document.getElementById('reportType');
    const sentenceGroup = document.getElementById('reportSentenceGroup');
    const sentenceArea = document.getElementById('reportSentence');
    const idInput = document.getElementById('reportSentenceId');
    
    typeSelect.value = type;
    document.getElementById('reportDescription').value = '';
    document.getElementById('reportMessage').innerHTML = '';
    
    if (type === 'sentence_error' && id) {
        sentenceGroup.style.display = 'block';
        sentenceArea.value = sentence;
        idInput.value = id;
    } else {
        sentenceGroup.style.display = 'none';
        sentenceArea.value = '';
        idInput.value = '';
    }
    
    modal.classList.remove('hidden');
}

window.closeReportModal = function() {
    document.getElementById('reportModal').classList.add('hidden');
}

window.submitReport = async function() {
    const type = document.getElementById('reportType').value;
    const description = document.getElementById('reportDescription').value;
    const email = document.getElementById('reportEmail').value;
    const sentenceId = document.getElementById('reportSentenceId').value;
    const sentence = document.getElementById('reportSentence').value;
    
    if (!description.trim()) {
        alert('請填寫詳細說明!');
        return;
    }
    
    const btn = document.getElementById('submitReportBtn');
    const msg = document.getElementById('reportMessage');
    btn.disabled = true;
    btn.innerText = '傳送中...';
    msg.innerHTML = '';
    
    try {
        const res = await fetch('/api/report', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ type, description, email, sentenceId, sentence })
        });
        const data = await res.json();
        
        if (res.ok) {
            msg.style.color = '#10b981';
            msg.innerHTML = '✅ 回報已成功送出給 lowking@ilrdf.org.tw!謝謝您!';
            setTimeout(() => closeReportModal(), 2000);
        } else {
            throw new Error(data.error || 'Server error');
        }
    } catch (error) {
        msg.style.color = '#ef4444';
        msg.innerHTML = '❌ 傳送失敗,請稍後再試。';
    } finally {
        btn.disabled = false;
        btn.innerText = '送出回報';
    }
}

// Global variables to store current analyzed words for export
let currentAiWord1 = '';
let currentAiWord2 = '';

// =========================================================
// 🏷️  IGT Glosser Integration — triggerGlosser()
// Calls truku-glosser backend on port 8000, renders 4-line IGT
// =========================================================
window.triggerGlosser = async function(btn, trukuSentence, chineseTranslation) {
    const outputArea = btn.closest('.card-body').querySelector('.igt-output-area');
    
    // Toggle: if already showing, hide it
    if (outputArea.style.display !== 'none' && outputArea.innerHTML !== '') {
        outputArea.style.display = 'none';
        btn.innerHTML = '🏷️ 語言學標注 (IGT)';
        return;
    }

    btn.innerHTML = '⏳ 分析中...';
    btn.disabled = true;
    outputArea.style.display = 'block';
    outputArea.innerHTML = `<div style="color:#a78bfa; font-size:0.85rem; padding:0.5rem 0;">正在呼叫語言學標注引擎(Port 8000)...</div>`;

    try {
        const response = await fetch('/api/parse', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ truku_sentence: trukuSentence, chinese_translation: chineseTranslation })
        });

        if (!response.ok) {
            const err = await response.text();
            throw new Error(err);
        }

        const data = await response.json();
        const sentences = data.sentences || [];

        if (sentences.length === 0) {
            outputArea.innerHTML = `<div style="color:#fca5a5;">標注引擎未回傳結果。</div>`;
            return;
        }

        // Build 4-line IGT HTML
        let igtHtml = `<div class="igt-container">`;

        sentences.forEach((sentObj, sIdx) => {
            const words = sentObj.words || [];
            const translation = sentObj.translation || chineseTranslation || '';

            // Build the raw sentence (Line 1) from words
            const originalLine = words.map(w => w.raw).join('  ');

            // Word columns for lines 2 & 3
            const wordColsHtml = words.map((w, i) => {
                const isLow = w.confidence === 'low';
                return `
                    <div class="igt-word-col ${isLow ? 'igt-low-conf' : ''}" data-idx="${sIdx}-${i}" data-raw="${(w.raw||'').replace(/"/g,"'")}">
                        <div class="igt-l2" contenteditable="true" title="詞素切分(可編輯)">${(w.morph||w.raw).replace(/</g,'&lt;').replace(/>/g,'&gt;')}</div>
                        <div class="igt-l3" contenteditable="true" title="語法標記(可編輯)">${(w.gloss||'').replace(/</g,'&lt;').replace(/>/g,'&gt;')}</div>
                    </div>`;
            }).join('');

            igtHtml += `
                <div class="igt-block" data-sidx="${sIdx}">
                    <div class="igt-l1">${originalLine}</div>
                    <div class="igt-words-row">${wordColsHtml}</div>
                    <div class="igt-l4">${translation}</div>
                    <div class="igt-actions">
                        <button class="igt-save-btn" onclick="igtSave(this, ${sIdx})">💾 儲存至記憶庫</button>
                        <button class="igt-csv-btn" onclick="igtDownloadCsv(this)">📥 CSV</button>
                        <button class="igt-word-btn" onclick="igtDownloadWord(this, ${sIdx})">📝 Word</button>
                        <button class="igt-csv-btn" style="background:#0ea5e9;color:white;border-color:#0284c7;" onclick="importToDictSentence(this)">📖 匯入為主辭典例句</button>
                        <span class="igt-save-msg" style="color:#4ade80; font-size:0.8rem; display:none;">✅ 已儲存!</span>
                    </div>
                </div>`;
        });

        igtHtml += `</div>`;
        outputArea.innerHTML = igtHtml;

        // Store raw data on the container for download functions
        outputArea._glosserData = sentences;

        btn.innerHTML = '🏷️ 收起標注';
    } catch (e) {
        outputArea.innerHTML = `<div style="color:#fca5a5; font-size:0.85rem;">❌ 標注失敗:${e.message}<br><small>請確認 truku-glosser 後端(Port 8000)已啟動。</small></div>`;
        btn.innerHTML = '🏷️ 語言學標注 (IGT)';
    } finally {
        btn.disabled = false;
    }
};

window.igtSave = async function(btn, sIdx) {
    if (!confirm("確定要將這些單字校正儲存至記憶庫嗎?\n這將做為未來 AI 自動標注的黃金標準!")) return;
    const block = btn.closest('.igt-block');
    const words = Array.from(block.querySelectorAll('.igt-word-col')).map(col => ({
        raw: col.dataset.raw,
        morph: col.querySelector('.igt-l2').innerText.trim(),
        gloss: col.querySelector('.igt-l3').innerText.trim()
    }));
    const msgEl = btn.nextElementSibling.nextElementSibling;
    try {
        const res = await fetch('/api/save_correct', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ words })
        });
        if (res.ok) {
            msgEl.style.display = 'inline';
            setTimeout(() => { msgEl.style.display = 'none'; }, 3000);
            alert("✅ 儲存成功!");
            
            try {
                const trukuSentence = block.querySelector('.igt-l1').innerText.trim();
                const chineseTranslation = block.querySelector('.igt-l4').innerText.trim();
                
                const checkRes = await fetch('/api/dictionary/check_sentence', {
                    method: 'POST', headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ truku_sentence: trukuSentence })
                });
                if (checkRes.ok) {
                    const checkData = await checkRes.json();
                    if (!checkData.exists) {
                        if (confirm(`發現這整句例句目前不在主辭典中:\n\n${trukuSentence}\n\n請問要順便將它新增到主辭典嗎?(邊標注、邊擴充)`)) {
                            await fetch('/api/dictionary/add_sentence', {
                                method: 'POST', headers: { 'Content-Type': 'application/json' },
                                body: JSON.stringify({ truku_sentence: trukuSentence, chinese_translation: chineseTranslation })
                            });
                            alert("✅ 例句擴充成功!");
                        }
                    }
                }
            } catch(e) { console.error(e); }
        }
    } catch(e) { alert('儲存失敗,請確認標注後端已開啟。'); }
};

window.igtDownloadCsv = function(btn) {
    const outputArea = btn.closest('.igt-output-area');
    const sentences = outputArea._glosserData || [];
    let csv = '\uFEFF句子編號,行一(原文),行二(詞素切分),行三(語法標記),行四(華語翻譯)\n';
    sentences.forEach((s, i) => {
        const blocks = btn.closest('.igt-container').querySelectorAll('.igt-block');
        const block = blocks[i];
        const cols = block ? block.querySelectorAll('.igt-word-col') : [];
        const raws = s.words.map(w => w.raw).join('   ');
        const morphs = Array.from(cols).map(c => c.querySelector('.igt-l2').innerText.trim()).join('   ');
        const glosses = Array.from(cols).map(c => c.querySelector('.igt-l3').innerText.trim()).join('   ');
        const trans = s.translation || '';
        csv += `${i+1},"${raws.replace(/"/g,'""')}","${morphs.replace(/"/g,'""')}","${glosses.replace(/"/g,'""')}","${trans.replace(/"/g,'""')}"\n`;
    });
    const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = `igt_annotation_${new Date().toISOString().slice(0,10)}.csv`;
    a.click();
};

window.igtDownloadWord = async function(btn, sIdx) {
    const outputArea = btn.closest('.igt-output-area');
    const sentences = outputArea._glosserData || [];
    const blocks = btn.closest('.igt-container').querySelectorAll('.igt-block');
    
    // Build up-to-date data from editable fields
    const payload = { sentences: [] };
    blocks.forEach((block, i) => {
        const cols = block.querySelectorAll('.igt-word-col');
        const words = Array.from(cols).map(col => ({
            raw: col.dataset.raw,
            morph: col.querySelector('.igt-l2').innerText.trim(),
            gloss: col.querySelector('.igt-l3').innerText.trim(),
            confidence: 'high'
        }));
        const trans = block.querySelector('.igt-l4').innerText.replace(/^'|'$/g, '');
        payload.sentences.push({ words, translation: trans });
    });

    try {
        const res = await fetch('/api/download_word', {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(payload)
        });
        const blob = await res.blob();
        const a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.download = '太魯閣語IGT標注成果.docx';
        a.click();
    } catch(e) { alert('下載失敗,請確認標注後端已開啟。'); }
};

// AI Analysis Logic
document.addEventListener('DOMContentLoaded', () => {
    const analyzeBtn = document.getElementById('aiAnalyzeBtn');
    if (analyzeBtn) {
        analyzeBtn.addEventListener('click', async () => {
            const w1 = document.getElementById('aiWord1').value.trim();
            const w2 = document.getElementById('aiWord2').value.trim();
            const limit = document.getElementById('aiSentenceLimit').value;
            
            currentAiWord1 = w1;
            currentAiWord2 = w2;
            
            if (!w1) {
                alert('請至少輸入第一個詞彙進行分析!');
                return;
            }
            
            const loading = document.getElementById('ai-loading');
            const errorDiv = document.getElementById('ai-error');
            const resultContainer = document.getElementById('ai-result-container');
            const contentDiv = document.getElementById('ai-markdown-content');
            const statsInfo = document.getElementById('ai-stats-info');
            
            loading.classList.remove('hidden');
            errorDiv.classList.add('hidden');
            resultContainer.classList.add('hidden');
            analyzeBtn.disabled = true;
            
            try {
                const params = new URLSearchParams({ w1, limit });
                if (w2) params.append('w2', w2);
                
                const customApiKey = localStorage.getItem('custom_gemini_api_key') || "";
                if (customApiKey) params.append('custom_api_key', customApiKey);
                
                const response = await fetch(`/api/ai_analysis?${params.toString()}`);
                const data = await response.json();
                
                if (!response.ok) {
                    throw new Error(data.error || '分析發生錯誤');
                }
                
                if (w2) {
                    statsInfo.innerHTML = `基於 <b>${data.w1_count}</b> 句 ${w1} 與 <b>${data.w2_count}</b> 句 ${w2} 例句進行分析`;
                } else {
                    statsInfo.innerHTML = `基於 <b>${data.w1_count}</b> 句 ${w1} 例句進行分析`;
                }
                
                // Parse markdown first
                let htmlContent = marked.parse(data.result);
                
                function escapeRegExp(string) {
                    return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
                }
                
                // Highlight keywords in HTML (only outside tags)
                if (w1) {
                    const regex1 = new RegExp('\\b(' + escapeRegExp(w1) + ')\\b(?![^<]*>)', 'gi');
                    htmlContent = htmlContent.replace(regex1, '<span style="color: #3b82f6; font-weight: bold;">$1</span>');
                }
                if (w2) {
                    const regex2 = new RegExp('\\b(' + escapeRegExp(w2) + ')\\b(?![^<]*>)', 'gi');
                    htmlContent = htmlContent.replace(regex2, '<span style="color: #ef4444; font-weight: bold;">$1</span>');
                }
                
                contentDiv.innerHTML = htmlContent;
                resultContainer.classList.remove('hidden');
                
            } catch (err) {
                errorDiv.innerHTML = err.message;
                errorDiv.classList.remove('hidden');
            } finally {
                loading.classList.add('hidden');
                analyzeBtn.disabled = false;
            }
        });
    }
});

// AI Analysis Export Functions
function exportToDoc() {
    const contentHtml = document.getElementById('ai-markdown-content').innerHTML;
    const header = `<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>太魯閣語語言學分析報告</title><style>body { color: #000000 !important; line-height: 1.8; font-family: sans-serif; }</style></head><body>`;
    const footer = `</body></html>`;
    const sourceHTML = header + contentHtml + footer;
    
    let filename = '太魯閣語語言學分析報告_' + currentAiWord1;
    if (currentAiWord2) filename += '&' + currentAiWord2;
    filename += '.doc';
    
    const source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
    const fileDownload = document.createElement("a");
    document.body.appendChild(fileDownload);
    fileDownload.href = source;
    fileDownload.download = filename;
    fileDownload.click();
    document.body.removeChild(fileDownload);
}

function exportToPdf() {
    const element = document.getElementById('ai-markdown-content');
    
    // Temporarily set text color to black for PDF rendering
    const originalColor = element.style.color;
    element.style.color = '#000000';
    
    // Add a class to adjust typography for print
    element.classList.add('pdf-exporting');
    
    let filename = '太魯閣語語言學分析報告_' + currentAiWord1;
    if (currentAiWord2) filename += '&' + currentAiWord2;
    filename += '.pdf';
    
    const opt = {
        margin:       0.6,
        filename:     filename,
        image:        { type: 'jpeg', quality: 1.0 },
        html2canvas:  { scale: 4, useCORS: true, logging: false },
        jsPDF:        { unit: 'in', format: 'a4', orientation: 'portrait' },
        pagebreak:    { mode: ['css', 'avoid-all'] }
    };
    
    html2pdf().set(opt).from(element).save().then(() => {
        // Revert text color back to original after PDF is generated
        element.style.color = originalColor;
        element.classList.remove('pdf-exporting');
    });
}

// =========================================================
// 🏷️  語料標注頁籤 (Tab Glosser) — Full standalone logic
// =========================================================

let glosserSessionData = [];   // Stores all sentences from last batch run

// -- 1. Status Checker: polls port 8000 every 5s to show green/red dot --
async function checkGlosserStatus() {
    const dot  = document.getElementById('glosser-status-dot');
    const text = document.getElementById('glosser-status-text');
    if (!dot || !text) return;
    try {
        const res = await fetch('/', { signal: AbortSignal.timeout(2000) });
        if (res.ok) {
            dot.style.background  = '#4ade80';
            text.style.color      = '#4ade80';
            text.textContent      = '標注引擎就緒 (Port 8000)';
        } else { throw new Error(); }
    } catch {
        dot.style.background  = '#f87171';
        text.style.color      = '#f87171';
        text.textContent      = '引擎未啟動 (Port 8000)';
    }
}

// -- 2. Render IGT 4-line block into #glosser-results --
function renderGlosserIGT(sentences) {
    const container = document.getElementById('glosser-results');
    container.innerHTML = '';
    glosserSessionData = sentences;

    sentences.forEach((sentObj, sIdx) => {
        const words = sentObj.words || [];
        const translation = sentObj.translation || '';
        const originalLine = words.map(w => w.raw).join('  ');

        let groups = [];
        words.forEach((w, wIdx) => {
            const m = (w.morph || w.raw || '').trim();
            if (m.startsWith('=') && groups.length > 0) {
                groups[groups.length - 1].push({w, wIdx});
            } else {
                groups.push([{w, wIdx}]);
            }
        });

        const wordColsHtml = groups.map(group => {
            const cols = group.map(item => {
                const w = item.w;
                const wIdx = item.wIdx;
                const isLow = w.confidence === 'low';
                return `
                <div class="igt-word-col ${isLow ? 'igt-low-conf' : ''}"
                     data-raw="${(w.raw||'').replace(/"/g,"'")}"
                     data-sidx="${sIdx}" data-widx="${wIdx}">
                    <div class="igt-l2" contenteditable="true" title="詞素切分(可點擊編輯)">${(w.morph||w.raw).replace(/</g,'&lt;').replace(/>/g,'&gt;')}</div>
                    <div class="igt-l3" contenteditable="true" title="語法標記(可點擊編輯)">${(w.gloss||'').replace(/</g,'&lt;').replace(/>/g,'&gt;')}</div>
                </div>`;
            }).join('');
            return `<div class="igt-word-group" style="display:flex; gap:0;">${cols}</div>`;
        }).join('');

        const block = document.createElement('div');
        block.className = 'igt-block';
        block.dataset.sidx = sIdx;
        block.innerHTML = `
            <div style="font-size:0.75rem; color:#64748b; margin-bottom:0.5rem; font-weight:600;">句子成果 #${sIdx + 1}</div>
            <div class="igt-l1">${originalLine}</div>
            <div class="igt-words-row">${wordColsHtml}</div>
            <div class="igt-l4" contenteditable="true" title="華語意譯(可點擊編輯)">${translation}</div>
            <div class="igt-actions">
                <button class="igt-save-btn" onclick="glosserSaveSentence(this, ${sIdx})">💾 儲存至記憶庫</button>
                <button class="igt-csv-btn" style="background:#0ea5e9;color:white;border-color:#0284c7;" onclick="importToDictSentence(this)">📖 匯入為主辭典例句</button>
                <span class="igt-save-msg" style="color:#4ade80;font-size:0.8rem;display:none;">✅ 已儲存!</span>
            </div>
        `;
        container.appendChild(block);
    });
}

// -- 3. Analyze button handler --
document.addEventListener('DOMContentLoaded', () => {
    // Start status polling as soon as page loads
    checkGlosserStatus();
    setInterval(checkGlosserStatus, 6000);

    const btn = document.getElementById('glosserAnalyzeBtn');
    if (!btn) return;

    btn.addEventListener('click', async () => {
        let trukuText   = (document.getElementById('glosser-truku-input').value || '').trim();
        let chineseText = (document.getElementById('glosser-chinese-input').value || '').trim();

        if (!trukuText) { alert('請先輸入太魯閣語語料!'); return; }

        const loading  = document.getElementById('glosser-loading');
        const errorDiv = document.getElementById('glosser-error');
        const timer    = document.getElementById('glosser-timer');
        const results  = document.getElementById('glosser-results');
        const batchBar = document.getElementById('glosser-batch-bar');

        loading.classList.remove('hidden');
        errorDiv.classList.add('hidden');
        timer.classList.add('hidden');
        results.innerHTML = '';
        batchBar.classList.add('hidden');
        btn.disabled = true;

        // Smart sentence splitting (same as original glosser)
        trukuText   = trukuText.replace(/([.?])\s+/g, '$1\n').replace(/\n+/g, '\n');
        chineseText = chineseText.replace(/([。?!])\s*/g, '$1\n').replace(/\n+/g, '\n');

        const startTime = performance.now();
        try {
            const customApiKey = localStorage.getItem('custom_gemini_api_key') || "";
            const res = await fetch('/api/parse', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ truku_sentence: trukuText, chinese_translation: chineseText, custom_api_key: customApiKey })
            });
            if (res.status === 429) throw new Error('QUOTA_EXCEEDED');
            if (!res.ok) throw new Error(await res.text());
            const data = await res.json();
            const elapsed = ((performance.now() - startTime) / 1000).toFixed(2);
            const sentences = data.sentences || [];

            if (sentences.length === 0) throw new Error('AI 未回傳任何標注結果。');

            timer.classList.remove('hidden');
            timer.innerHTML = `⏱️ 批次標注完成!共處理 <b>${sentences.length}</b> 個句子,核心運算耗時 <b>${elapsed} 秒</b>。`;

            renderGlosserIGT(sentences);
            batchBar.classList.remove('hidden');

        } catch(e) {
            errorDiv.classList.remove('hidden');
            if (e.message.includes('QUOTA_EXCEEDED')) {
                errorDiv.innerHTML = `❌ 系統公用額度已用完!<br><small style="color:#f87171;">請通知作者,或點擊右上角「⚙️ API 設定」填寫您個人的免費金鑰後繼續使用。</small>`;
            } else {
                errorDiv.innerHTML = `❌ 標注失敗:${e.message}<br><small>請確認「語言學標注引擎」(Port 8000)已啟動且知識庫已就緒。</small>`;
            }
        } finally {
            loading.classList.add('hidden');
            btn.disabled = false;
        }
    });
});

// -- 4. Save one sentence to memory DB --
window.glosserSaveSentence = async function(btn, sIdx) {
    if (!confirm("確定要將這些單字校正儲存至記憶庫嗎?\n這將做為未來 AI 自動標注的黃金標準!")) return;
    const block = document.querySelector(`.igt-block[data-sidx="${sIdx}"]`);
    const words = Array.from(block.querySelectorAll('.igt-word-col')).map(col => ({
        raw:  col.dataset.raw,
        morph: col.querySelector('.igt-l2').innerText.trim(),
        gloss: col.querySelector('.igt-l3').innerText.trim()
    }));
    const msgEl = btn.nextElementSibling;
    try {
        const res = await fetch('/api/save_correct', {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ words })
        });
        if (res.ok) {
            msgEl.style.display = 'inline';
            setTimeout(() => { msgEl.style.display = 'none'; }, 3000);
            alert("✅ 儲存成功!");
            
            try {
                const trukuSentence = block.querySelector('.igt-l1').innerText.trim();
                const chineseTranslation = block.querySelector('.igt-l4').innerText.trim();
                
                const checkRes = await fetch('/api/dictionary/check_sentence', {
                    method: 'POST', headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ truku_sentence: trukuSentence })
                });
                if (checkRes.ok) {
                    const checkData = await checkRes.json();
                    if (!checkData.exists) {
                        if (confirm(`發現這整句例句目前不在主辭典中:\n\n${trukuSentence}\n\n請問要順便將它新增到主辭典嗎?(邊標注、邊擴充)`)) {
                            await fetch('/api/dictionary/add_sentence', {
                                method: 'POST', headers: { 'Content-Type': 'application/json' },
                                body: JSON.stringify({ truku_sentence: trukuSentence, chinese_translation: chineseTranslation })
                            });
                            alert("✅ 例句擴充成功!");
                        }
                    }
                }
            } catch(e) { console.error(e); }
        } else { throw new Error(); }
    } catch { alert('儲存失敗,請確認標注後端已開啟。'); }
};

window.importToDictSentence = async function(btn) {
    if (!confirm("確定要將這句話與意譯「匯入主辭典」作為標準例句嗎?")) return;
    const block = btn.closest('.igt-block');
    const trukuSentence = block.querySelector('.igt-l1').innerText.trim();
    const chineseTranslation = block.querySelector('.igt-l4').innerText.trim();
    const oldText = btn.innerHTML;
    btn.innerHTML = '匯入中...';
    btn.disabled = true;
    try {
        const res = await fetch('/api/dictionary/add_sentence', {
            method: 'POST', headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({ truku_sentence: trukuSentence, chinese_translation: chineseTranslation })
        });
        if (res.ok) {
            btn.innerHTML = '✅ 已匯入主辭典';
            alert("✅ 匯入成功!此句已新增至主系統辭典庫中。");
            setTimeout(() => { btn.innerHTML = oldText; btn.disabled = false; }, 3000);
        } else { throw new Error(); }
    } catch(e) {
        btn.innerHTML = '❌ 匯入失敗';
        setTimeout(() => { btn.innerHTML = oldText; btn.disabled = false; }, 3000);
    }
};

// -- 5. Batch CSV export from the glosser tab --
window.glosserBatchCsv = function() {
    const blocks = document.querySelectorAll('#glosser-results .igt-block');
    if (!blocks.length) return;
    let csv = '\uFEFF句子編號,行一(原文),行二(詞素切分),行三(語法標記),行四(華語翻譯)\n';
    blocks.forEach((block, i) => {
        const l1 = block.querySelector('.igt-l1').innerText.trim();
        const cols = block.querySelectorAll('.igt-word-col');
        const morphs  = Array.from(cols).map(c => c.querySelector('.igt-l2').innerText.trim()).join('   ');
        const glosses = Array.from(cols).map(c => c.querySelector('.igt-l3').innerText.trim()).join('   ');
        const l4 = block.querySelector('.igt-l4').innerText.replace(/^'|'$/g,'').trim();
        csv += `${i+1},"${l1.replace(/"/g,'""')}","${morphs.replace(/"/g,'""')}","${glosses.replace(/"/g,'""')}","${l4.replace(/"/g,'""')}"\n`;
    });
    const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = `太魯閣語IGT標注_${new Date().toISOString().slice(0,10)}.csv`;
    a.click();
};

// -- 6. Batch Word export from the glosser tab --
window.glosserBatchWord = async function() {
    const blocks = document.querySelectorAll('#glosser-results .igt-block');
    if (!blocks.length) return;
    const payload = { sentences: [] };
    blocks.forEach(block => {
        const cols = block.querySelectorAll('.igt-word-col');
        const words = Array.from(cols).map(col => ({
            raw:   col.dataset.raw,
            morph: col.querySelector('.igt-l2').innerText.trim(),
            gloss: col.querySelector('.igt-l3').innerText.trim(),
            confidence: 'high'
        }));
        const trans = block.querySelector('.igt-l4').innerText.replace(/^'|'$/g,'').trim();
        payload.sentences.push({ words, translation: trans });
    });
    try {
        const res = await fetch('/api/download_word', {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(payload)
        });
        const blob = await res.blob();
        const a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.download = `太魯閣語IGT標注成果_${new Date().toISOString().slice(0,10)}.docx`;
        a.click();
    } catch(e) { alert('下載失敗,請確認標注後端已開啟。'); }
};

// -- 7. Bridge: fill glosser tab from a search result sentence --
window.sendToGlosser = function(trukuSentence, chineseTranslation) {
    // Switch to glosser tab
    document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
    document.querySelectorAll('.tab-content').forEach(c => c.classList.add('hidden'));
    document.querySelector('[data-tab="tab-glosser"]').classList.add('active');
    document.getElementById('tab-glosser').classList.remove('hidden');

    // Pre-fill input fields
    document.getElementById('glosser-truku-input').value   = trukuSentence;
    document.getElementById('glosser-chinese-input').value = chineseTranslation || '';

    // Scroll up and auto-click analyze
    document.getElementById('glosser-truku-input').scrollIntoView({ behavior: 'smooth' });
    setTimeout(() => { document.getElementById('glosserAnalyzeBtn').click(); }, 400);
};

// =========================================================
// ☀️  Theme Toggle Logic (Light / Dark Mode)
// =========================================================
document.addEventListener('DOMContentLoaded', () => {
    const themeToggleBtn = document.getElementById('themeToggleBtn');
    if (!themeToggleBtn) return;

    // Default to dark mode unless 'light' is saved
    const savedTheme = localStorage.getItem('truku-dict-theme');
    if (savedTheme === 'light') {
        document.documentElement.setAttribute('data-theme', 'light');
        themeToggleBtn.innerHTML = '🌙 暗色模式';
    }

    themeToggleBtn.addEventListener('click', () => {
        const currentTheme = document.documentElement.getAttribute('data-theme');
        if (currentTheme === 'light') {
            document.documentElement.removeAttribute('data-theme'); // reverts to dark (default)
            localStorage.setItem('truku-dict-theme', 'dark');
            themeToggleBtn.innerHTML = '☀️ 亮色模式';
        } else {
            document.documentElement.setAttribute('data-theme', 'light');
            localStorage.setItem('truku-dict-theme', 'light');
            themeToggleBtn.innerHTML = '🌙 暗色模式';
        }
    });
});