File size: 70,547 Bytes
cc21d29
600e656
 
 
 
 
2e83c3a
03d066e
ffac483
3be9dd8
38f4f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3be9dd8
 
a4ca8d7
 
 
 
 
 
 
600e656
ffac483
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31955f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c79dbf
 
600e656
 
2e83c3a
600e656
ae22812
48ab613
 
31955f4
 
 
0792994
 
ee2e10e
 
0792994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6716a6e
ed17c8d
 
 
 
 
 
 
6716a6e
ed17c8d
 
6716a6e
0792994
 
 
 
 
 
 
6716a6e
0792994
 
6716a6e
 
 
 
ed17c8d
6716a6e
0792994
6716a6e
 
 
 
0792994
ed17c8d
 
 
 
 
 
 
 
6716a6e
 
0792994
 
 
 
 
 
 
 
6716a6e
 
 
0792994
 
6716a6e
 
0792994
 
 
6716a6e
 
 
0792994
6716a6e
0792994
 
 
 
 
 
ee2e10e
 
9daadf2
 
 
 
eb08066
 
9daadf2
eb08066
 
 
 
 
 
 
 
 
9daadf2
eb08066
9daadf2
 
 
2a79127
 
 
9daadf2
 
 
 
 
 
2a79127
 
9daadf2
 
2c79dbf
 
9daadf2
 
 
 
 
 
 
 
 
 
 
 
 
2a79127
 
 
cd7a12c
9daadf2
 
 
 
 
 
 
 
 
ff4b14c
 
 
2c79dbf
 
 
 
 
 
 
 
 
 
 
 
9daadf2
 
2c79dbf
 
9daadf2
 
 
 
ee2e10e
 
9daadf2
ee2e10e
31955f4
a4ca8d7
 
 
 
b8bd46e
31955f4
a4ca8d7
 
 
 
 
ee2e10e
a4ca8d7
ee2e10e
 
9daadf2
a4ca8d7
 
 
 
b8bd46e
 
 
 
 
 
 
 
 
a4ca8d7
 
 
 
ee2e10e
 
0792994
600e656
4430c91
 
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd6293
 
 
 
 
 
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93265a5
 
 
 
 
 
 
3e4465e
 
 
 
 
 
 
 
 
 
 
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf7ca90
 
600e656
bf7ca90
 
 
600e656
 
 
 
bf7ca90
 
 
 
 
 
600e656
 
 
 
 
 
 
 
 
 
 
bf7ca90
 
 
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e4465e
 
 
 
 
93265a5
 
3e4465e
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a44a72
 
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a44a72
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd6293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
600e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c6d58a
 
 
 
 
 
 
 
 
03d066e
 
 
 
 
 
8c6d58a
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
import { ChatView } from './views/chat/chat-view.js?v=13';
import { db } from './db/database.js';
import { Clipboard } from '@capacitor/clipboard';
import { authService } from './services/auth-service.js';
import { syncManager } from './services/sync-service.js';
import { ttsService } from './services/tts-service.js';
import { appShortcutsService } from './services/app-shortcuts-service.js';
import { showVocabularyModal } from './views/vocabulary-modal.js';
import { TextToSpeech } from '@capacitor-community/text-to-speech';

// Handle PWA Service Worker
if ('serviceWorker' in navigator) {
  if (window.Capacitor) {
    // IMPORTANT: If we are in the native Capacitor app, we MUST NOT use a Service Worker.
    // A Service Worker here would aggressively cache local files and prevent app updates!
    navigator.serviceWorker.getRegistrations().then(function(registrations) {
      for(let registration of registrations) {
        registration.unregister();
        console.log('Successfully unregistered old PWA Service Worker in Capacitor');
      }
    });
  } else {
    // We are in a normal browser (e.g. HuggingFace). Register the PWA Service Worker.
    import('virtual:pwa-register').then(({ registerSW }) => {
      registerSW({ immediate: true });
    }).catch(err => console.warn('PWA Registration failed', err));
  }
}

// Handle App Shortcuts and Intents globally
window.addEventListener('appShortcutAction', (e) => {
  if (e.detail && e.detail.shortcutId === 'add_vocabulary') {
    showVocabularyModal();
  }
});

window.ttsService = ttsService;
window.TextToSpeech = TextToSpeech;

window.appPrompt = function(message) {
  return new Promise(resolve => {
    const overlay = document.createElement('div');
    overlay.className = 'custom-modal-overlay';
    overlay.innerHTML = `
      <div class="custom-modal">
        <h3>Eingabe</h3>
        <p>${message}</p>
        <input type="text" style="width:100%; padding:10px; border-radius:8px; border:1px solid rgba(255,255,255,0.2); background:rgba(0,0,0,0.5); color:#fff; margin-top:8px; outline:none;" id="app-prompt-input" autocomplete="off" />
        <div class="modal-actions">
          <button class="modal-btn cancel">Abbrechen</button>
          <button class="modal-btn confirm">Speichern</button>
        </div>
      </div>
    `;
    document.body.appendChild(overlay);
    const input = overlay.querySelector('#app-prompt-input');
    const cancelBtn = overlay.querySelector('.cancel');
    const confirmBtn = overlay.querySelector('.confirm');
    
    input.focus();
    
    const close = (val) => {
      input.blur();
      overlay.classList.add('fade-out');
      setTimeout(() => { overlay.remove(); resolve(val); }, 300);
    };
    
    cancelBtn.onclick = () => close(null);
    confirmBtn.onclick = () => close(input.value.trim() ? input.value.trim() : null);
    input.onkeydown = (e) => {
      if (e.key === 'Enter') close(input.value.trim() ? input.value.trim() : null);
      if (e.key === 'Escape') close(null);
    };
  });
};

window.appConfirm = function(message, confirmText = "Löschen", cancelText = "Abbrechen") {
  return new Promise(resolve => {
    const overlay = document.createElement('div');
    overlay.className = 'custom-modal-overlay';
    overlay.innerHTML = `
      <div class="custom-modal">
        <h3>Bestätigung</h3>
        <p>${message}</p>
        <div class="modal-actions" style="margin-top: 16px;">
          <button class="modal-btn cancel">${cancelText}</button>
          <button class="modal-btn confirm" style="background: var(--destructive); border-color: var(--destructive); color: white;">${confirmText}</button>
        </div>
      </div>
    `;
    document.body.appendChild(overlay);
    const cancelBtn = overlay.querySelector('.cancel');
    const confirmBtn = overlay.querySelector('.confirm');
    
    const close = (val) => {
      overlay.classList.add('fade-out');
      setTimeout(() => { overlay.remove(); resolve(val); }, 300);
    };
    
    cancelBtn.onclick = () => close(false);
    confirmBtn.onclick = () => close(true);
  });
};

window.copyToClipboard = async (text, btnElement) => {
  const showCheckmark = () => {
    if (!btnElement) return;
    const svg = btnElement.querySelector('svg');
    if (!svg) return;
    const oldHtml = svg.outerHTML;
    svg.outerHTML = '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
    setTimeout(() => {
      btnElement.innerHTML = oldHtml;
    }, 2000);
  };

  const formatForNotes = (raw) => {
    if (!raw) return raw;
    
    // 1. Strip inline formatting and block markers
    let text = raw
      .replace(/\*\*(.*?)\*\*/g, '$1')
      .replace(/__(.*?)__/g, '$1')
      .replace(/\*(.*?)\*/g, '$1')
      .replace(/_(.*?)_/g, '$1')
      .replace(/~~(.*?)~~/g, '$1')
      .replace(/`(.*?)`/g, '$1')
      .replace(/\[(.*?)\]\(.*?\)/g, '$1')
      .replace(/^```[a-z]*\n/gm, '')
      .replace(/^```\s*$/gm, '')
      .replace(/^>\s+/gm, '');

    const lines = text.split('\n');
    const processedLines = [];
    let previousRealLineWasList = false;
    let emptyLinesSeen = 0;

    for (let i = 0; i < lines.length; i++) {
      let line = lines[i].trim();
      
      if (line === '') {
        emptyLinesSeen++;
        continue;
      }

      const headerMatch = line.match(/^#+\s+(.*)$/);
      const listMatch = line.match(/^([\*\-]|\d+\.)\s+(.*)$/);

      if (headerMatch) {
        // Empty line before header
        if (processedLines.length > 0 && processedLines[processedLines.length - 1] !== '') {
          processedLines.push('');
        }
        processedLines.push(headerMatch[1]);
        // Empty line after header
        processedLines.push('');
        previousRealLineWasList = false;
        emptyLinesSeen = 0;
      } else if (listMatch) {
        if (!previousRealLineWasList) {
          // Empty line before first list item
          if (processedLines.length > 0 && processedLines[processedLines.length - 1] !== '') {
            processedLines.push('');
          }
        }
        const bullet = (listMatch[1] === '*' || listMatch[1] === '-') ? '• ' : `${listMatch[1]} `;
        processedLines.push(`${bullet}${listMatch[2]}`);
        previousRealLineWasList = true;
        emptyLinesSeen = 0;
      } else {
        // Normal text
        if (previousRealLineWasList) {
          // Ensure empty line after list
          if (processedLines.length > 0 && processedLines[processedLines.length - 1] !== '') {
            processedLines.push('');
          }
        } else if (emptyLinesSeen > 0 && processedLines.length > 0) {
          // Maintain paragraph breaks for text
          if (processedLines[processedLines.length - 1] !== '') {
            processedLines.push('');
          }
        }
        processedLines.push(line);
        previousRealLineWasList = false;
        emptyLinesSeen = 0;
      }
    }

    return processedLines.join('\n').trim();
  };

  const formattedText = formatForNotes(text);

  try {
    await Clipboard.write({ string: formattedText });
    showCheckmark();
  } catch (e) {
    console.log('Capacitor Clipboard fallback:', e);
    // Legacy fallback for non-secure HTTP contexts
    try {
      if (navigator.clipboard && navigator.clipboard.writeText) {
        await navigator.clipboard.writeText(formattedText);
      } else {
        const textArea = document.createElement("textarea");
        textArea.value = formattedText;
        textArea.style.position = "fixed";
        textArea.style.left = "-999999px";
        textArea.style.top = "-999999px";
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
        document.execCommand('copy');
        textArea.remove();
      }
      showCheckmark();
    } catch (err) {
      console.error('Copy failed:', err);
    }
  }
};

const initApp = async () => {
  // FAST PATH: Check for overlay mode IMMEDIATELY, before any heavy initialization.
  // This cuts ~1-2 seconds off overlay startup by skipping auth, sync, etc.
  const isNative = window.Capacitor && typeof window.Capacitor.isNativePlatform === 'function' && window.Capacitor.isNativePlatform();
  
  if (isNative) {
    const { QuickSettings } = window.Capacitor.Plugins;
    if (QuickSettings) {
      const intentResult = await QuickSettings.getPendingIntentAction();
      
      if (intentResult && intentResult.overlay_mode === true) {
        // Force transparent on EVERY layer via cssText (highest inline priority)
        document.documentElement.style.cssText = 'background: transparent !important; background-color: transparent !important;';
        document.body.style.cssText = 'background: transparent !important; background-color: transparent !important; margin: 0; padding: 0; visibility: visible !important;';
        
        // Hide ALL non-modal elements
        const hideSelectors = ['#app', '.aurora-background', '#ambient-light', '#live-version-badge', 'svg[style*="pointer-events: none"]'];
        hideSelectors.forEach(sel => {
          const el = document.querySelector(sel);
          if (el) el.style.display = 'none';
        });
        
        window.isOverlayMode = true;
        
        await db.initPromise;
        showVocabularyModal();
        
        // Signal native side to fade in the window (prevents black flash)
        QuickSettings.overlayReady();
        
        return; // Early return — skip ALL heavy initialization
      }
    }
  }

  // NORMAL APP PATH: Full initialization
  await db.initPromise;
  
  await authService.initialize();
  await syncManager.initialize();
  await appShortcutsService.initialize();

  // Load background style
  const bgStyle = db.getSetting('background_style') || 'standard';
  document.body.setAttribute('data-bg-style', bgStyle);
  
  // Make body visible for normal app
  document.body.style.visibility = 'visible';

  // Process any pending vocabulary from the Quick Settings native overlay
  const processOfflineVocabularyQueue = async () => {
    if (!isNative) return;
    try {
      const { QuickSettings } = window.Capacitor.Plugins;
      if (QuickSettings) {
        const { queue } = await QuickSettings.getPendingVocab();
        if (queue && queue !== '[]') {
          const words = JSON.parse(queue);
          if (words.length > 0) {
            console.log("Processing offline vocabulary queue:", words);
            
            // Start background translation process
            setTimeout(async () => {
              const { aiRouter } = await import('./ai/router.js');
              let addedCount = 0;
              
              for (const word of words) {
                try {
                  const prompt = `Übersetze das folgende Wort oder den Satz. Wenn es Deutsch ist, übersetze es ins Englische. Wenn es Englisch ist, übersetze es ins Deutsche.
Falls das Wort extrem falsch geschrieben ist (z.B. phonetisch nach Gehör geschrieben), erkenne das korrekte Wort automatisch und übersetze es.
Das korrekte Wort soll in "front" stehen und die Übersetzung in "back".
Zusätzlich generiere:
- "explanation": 1-2 kurze Sätze in der Sprache der Übersetzung ('back'), die genau dieses Wort erklären. WICHTIG: Markiere das Lernwort (und seine Abwandlungen) IMMER fett mit HTML-Tags (z.B. <b>Wort</b>).
- "example": Ein prägnanter Beispielsatz mit der Übersetzung ('back'). Das Lernwort MUSS auch hier mit <b>...</b> fett markiert sein.
- "imageQuery": Ein präziser englischer Suchbegriff für die Bing-Bildersuche. Das Bild muss EINDEUTIG zur Übersetzung passen. Füge IMMER 'single photo, clear focus, high quality' am Ende hinzu. Keine abstrakten Cartoons.

Antworte AUSSCHLIESSLICH mit einem validen JSON-Objekt. Keine Markdown-Formatierung.
Beispiel für "abanden": 
{"front": "abandon", "back": "verlassen", "explanation": "Jemanden oder etwas <b>verlassen</b> oder aufgeben.", "example": "Er musste das alte Auto <b>verlassen</b>.", "imageQuery": "abandoned old car single photo clear focus"}
Zu übersetzen: "${word}"`;
                  
                  const response = await aiRouter.sendMessage(prompt, { 
                    model: db.getSetting('model') || 'gemini-1.5-flash'
                  });

                  let jsonStr = response.trim();
                  if (jsonStr.startsWith('```json')) jsonStr = jsonStr.replace(/```json/g, '').replace(/```/g, '').trim();
                  else if (jsonStr.startsWith('```')) jsonStr = jsonStr.replace(/```/g, '').trim();
                  
                  let parsed = null;
                  try { 
                    parsed = JSON.parse(jsonStr); 
                  } catch (e) { 
                    console.warn("JSON parse failed, trying fallback...", jsonStr);
                    parsed = { front: word, back: response.replace(/\{|\}/g, '').replace(/"/g, '').replace(/back:|front:|\\n/gi, '').trim(), explanation: "", example: "", imageQuery: encodeURIComponent(word) }; 
                  }

                  if (parsed && parsed.back) {
                    let decks = db.getDecks();
                    let schnelldeck = decks.find(d => d.title === 'Schnelleingabe' || d.title === 'Inbox');
                    if (!schnelldeck) schnelldeck = db.createDeck('Schnelleingabe', 'Automatisch gespeicherte Vokabeln aus der Schnelleingabe', '#8a53e6', 'Englisch', 'Deutsch');

                    let finalBack = parsed.back;
                    if (parsed.explanation && parsed.example) {
                      finalBack = `${parsed.back}\n\n**Erklärung:** ${parsed.explanation}\n\n**Beispiel:** ${parsed.example}`;
                    }
                    const searchTerms = encodeURIComponent(parsed.imageQuery || parsed.front);
                    const imageUrl = `https://tse1.mm.bing.net/th?q=${searchTerms}&w=800&h=1000&c=7&rs=1`;

                    db.addFlashcard(schnelldeck.id, parsed.front, finalBack, imageUrl);
                    addedCount++;
                  }
                } catch (e) {
                  console.error("Failed to translate offline word:", word, e);
                }
              }
              
              await QuickSettings.clearPendingVocab();
              
              if (addedCount > 0) {
                // Dispatch event so UI can refresh if user is on flashcards page
                window.dispatchEvent(new CustomEvent('flashcards-updated'));
                
                // Show a small toast notification
                const toast = document.createElement('div');
                toast.style = "position: fixed; top: 20px; left: 50%; transform: translateX(-50%); background: rgba(52, 199, 89, 0.95); color: white; padding: 12px 24px; border-radius: 30px; font-weight: 600; font-size: 15px; z-index: 99999; box-shadow: 0 8px 24px rgba(0,0,0,0.3); animation: slide-down 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);";
                toast.innerHTML = `✨ ${addedCount} Vokabel(n) hinzugefügt!`;
                document.body.appendChild(toast);
                setTimeout(() => {
                  toast.style.opacity = '0';
                  toast.style.transition = 'opacity 0.4s';
                  setTimeout(() => toast.remove(), 400);
                }, 4000);
              }
            }, 1000); // Wait 1s instead of 2s
          }
        }
      }
    } catch (e) {
      console.warn("Error reading pending vocab queue:", e);
    }
  };

  const processEnrichmentQueue = async () => {
    // Only run if we're not in the tiny transparent overlay mode
    if (window.isOverlayMode) return;
    
    let pendingCards = db.getPendingCards();
    if (!pendingCards || pendingCards.length === 0) return;

    if (!window.isEnriching) {
      window.isEnriching = true;
      try {
        const { aiRouter } = await import('./ai/router.js');
        
        // Process sequentially
        for (const card of pendingCards) {
          const word = card.front;
          const pureTranslation = card.back.replace('<!-- PENDING_CORRECTION -->', '').replace('<!-- NEEDS_ENRICHMENT -->', '').trim();
          
          const prompt = `Erstelle eine kurze Erklärung und ein passendes Beispiel für diese Vokabelkarte, sowie einen Suchbegriff für ein Bild.
Wort: "${word}"
Übersetzung: "${pureTranslation}"

WICHTIG: Prüfe, ob das "Wort" offensichtlich falsch geschrieben ist (z.B. phonetisch nach Gehör geschrieben). 
Falls es einen Tippfehler enthält, gib das richtig geschriebene Wort im Feld "correctedWord" zurück. Wenn es korrekt geschrieben ist, lass das Feld weg oder setze es auf null.

Generiere:
- "explanation": 1-2 kurze Sätze in der Sprache der Übersetzung, die genau dieses Wort erklären. WICHTIG: Markiere das Lernwort (und seine Abwandlungen) IMMER fett mit HTML-Tags (z.B. <b>Wort</b>).
- "example": Ein prägnanter Beispielsatz mit der Übersetzung. Das Lernwort MUSS auch hier mit <b>...</b> fett markiert sein.
- "imageQuery": Ein präziser englischer Suchbegriff für die Bing-Bildersuche. Das Bild muss EINDEUTIG zur Übersetzung passen. Füge IMMER 'single photo, clear focus, high quality' am Ende hinzu. Keine abstrakten Cartoons.

Antworte AUSSCHLIESSLICH mit einem validen JSON-Objekt. Keine Markdown-Formatierung.
Beispiel für Tippfehler bei "abanden": 
{"correctedWord": "abandon", "explanation": "Jemanden oder etwas <b>verlassen</b> oder aufgeben.", "example": "Er musste das alte Auto <b>verlassen</b>.", "imageQuery": "abandoned old car single photo clear focus"}`;
            
            const response = await aiRouter.sendMessage(prompt, { 
              model: db.getSetting('model') || 'gemini-1.5-flash', // Use standard model to ensure it doesn't fail
              disableTools: true // VERY IMPORTANT: Prevent AI from calling create_flashcards and duplicating the deck
            });

            let jsonStr = response.trim();
            if (jsonStr.startsWith('```json')) jsonStr = jsonStr.replace(/```json/g, '').replace(/```/g, '').trim();
            else if (jsonStr.startsWith('```')) jsonStr = jsonStr.replace(/```/g, '').trim();
            
            try { 
              const parsed = JSON.parse(jsonStr); 
              if (parsed.explanation && parsed.example) {
                const finalBack = `${pureTranslation}\n\n**Erklärung:** ${parsed.explanation}\n\n**Beispiel:** ${parsed.example}`;
                const searchTerms = encodeURIComponent(parsed.imageQuery || word);
                const imageUrl = `https://tse1.mm.bing.net/th?q=${searchTerms}&w=800&h=1000&c=7&rs=1`;
                
                // If the AI corrected a typo, use it. Otherwise use the original front.
                const finalFront = parsed.correctedWord ? parsed.correctedWord : card.front;
                
                db.editFlashcard(card.id, finalFront, finalBack, imageUrl);
                console.log(`Enriched card: ${word}`);
              }
            } catch (e) { 
              console.warn("JSON parse failed for enrichment", jsonStr);
            }
            
            // Small pause between requests to avoid rate limits
            await new Promise(r => setTimeout(r, 500));
          }
          
          // Dispatch event ONCE after all retroactive enrichment is done, to prevent UI scrolling spam
          window.dispatchEvent(new CustomEvent('flashcards-updated'));
          
          if (document.visibilityState === 'visible') {
            const toast = document.createElement('div');
            toast.style = "position: fixed; top: 20px; left: 50%; transform: translateX(-50%); background: rgba(52, 199, 89, 0.95); color: white; padding: 12px 24px; border-radius: 30px; font-weight: 600; font-size: 15px; z-index: 99999; box-shadow: 0 8px 24px rgba(0,0,0,0.3); animation: slide-down 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);";
            toast.innerHTML = `✨ Vokabel im Hintergrund geprüft und vervollständigt!`;
            document.body.appendChild(toast);
            setTimeout(() => {
              toast.style.opacity = '0';
              toast.style.transition = 'opacity 0.4s ease';
              setTimeout(() => toast.remove(), 400);
            }, 3000);
          }
        } catch (e) {
          console.error("Enrichment queue failed", e);
        } finally {
          window.isEnriching = false;
        }
    }
  };

  // Run on startup
  processOfflineVocabularyQueue();
  processEnrichmentQueue();

  // Check if we were launched with a native Intent (non-overlay)
  if (isNative) {
    const { QuickSettings } = window.Capacitor.Plugins;
    if (QuickSettings) {
      const intentResult = await QuickSettings.getPendingIntentAction();
      
      if (intentResult && intentResult.action === 'add_vocabulary') {
        showVocabularyModal();
      }
    }
  }

  // Run whenever the app resumes (comes to foreground)
  document.addEventListener("visibilitychange", async () => {
    if (document.visibilityState === 'visible') {
      processOfflineVocabularyQueue();
      processEnrichmentQueue();
      if (isNative) {
        const { QuickSettings } = window.Capacitor.Plugins;
        if (QuickSettings) {
          const intentResult = await QuickSettings.getPendingIntentAction();
          
          if (intentResult && intentResult.overlay_mode === true) {
            document.body.style.background = 'transparent';
            document.body.style.backgroundColor = 'transparent';
            const appElement = document.getElementById('app');
            if (appElement) appElement.style.display = 'none';
            window.isOverlayMode = true;
            showVocabularyModal();
          } else if (intentResult && intentResult.action === 'add_vocabulary') {
            showVocabularyModal();
          }
        }
      }
    }
  });

  // Scale up UI on web (HuggingFace) to match Capacitor native scale
  const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
  if (!isNative && isMobile) {
    document.documentElement.style.zoom = '1.15';
  }
  console.log('SchoolMind AI initialized');
  
  const appContainer = document.getElementById('app');
  
  // Basic skeleton for the layout
  appContainer.innerHTML = `
    <!-- Sidebar Drawer -->
    <div id="sidebar-overlay" class="sidebar-overlay"></div>
    <div id="history-sidebar" class="sidebar-drawer">
      <div class="sidebar-inner">
        <div class="sidebar-header">
          <h2>Verlauf</h2>
          <div style="display: flex; gap: 8px;">
            <button id="fullscreen-sidebar-btn" class="icon-btn" style="font-size: 16px; width: 32px; height: 32px;">⛶</button>
            <button id="close-sidebar-btn" class="icon-btn" style="font-size: 24px; width: 32px; height: 32px;">×</button>
          </div>
        </div>
        <div style="padding: 0 16px 12px 16px;">
          <input type="text" id="history-search-input" placeholder="Chats durchsuchen..." style="width: 100%; padding: 10px 14px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.1); background: rgba(0,0,0,0.3); color: white; outline: none; font-size: 14px; box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);">
        </div>
        <div id="history-list" class="history-list">
          <!-- History items will be injected here -->
        </div>
      </div>
    </div>

    <div class="view-container" id="router-view">
      <!-- Active view will be mounted here -->
    </div>
    
    <!-- TTS Floating Player (Apple Music Style) -->
    <div id="tts-player-container" class="tts-player-container collapsed">
      <div class="tts-player-inner liquid-glass-heavy" style="padding: 10px 16px;">
        <div class="tts-player-drag-handle" style="margin-bottom: 8px;"></div>
        <div class="tts-player-content" style="justify-content: space-between; width: 100%; display: flex; align-items: center;">
          <div style="flex: 1; display: flex; align-items: center;">
            <button id="tts-cc-btn" class="icon-btn tts-btn active" title="Live-Untertitel" style="margin-left: 0; color: var(--primary);">
              <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="7" width="20" height="10" rx="2" ry="2"></rect><path d="M7 11h2"></path><path d="M11 11h6"></path><path d="M7 13h6"></path><path d="M15 13h2"></path></svg>
            </button>
          </div>
          <div class="tts-player-controls" style="gap: 12px; flex: 0 0 auto; display: flex; justify-content: center; align-items: center;">
            <button id="tts-prev-btn" class="icon-btn tts-btn">
              <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"></path><path d="M3 3v5h5"></path><text x="12" y="16" font-size="8" text-anchor="middle" font-weight="bold" fill="currentColor" stroke="none">5</text></svg>
            </button>
            <button id="tts-play-btn" class="icon-btn tts-btn tts-play-btn" style="width: 36px; height: 36px;">
              <svg id="tts-play-icon" viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><polygon points="7 3 21 12 7 21 7 3"></polygon></svg>
            </button>
            <button id="tts-next-btn" class="icon-btn tts-btn">
              <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"></path><path d="M21 3v5h-5"></path><text x="12" y="16" font-size="8" text-anchor="middle" font-weight="bold" fill="currentColor" stroke="none">5</text></svg>
            </button>
          </div>
          <div style="flex: 1; display: flex; justify-content: flex-end;">
            <button id="tts-rate-btn" class="tts-rate-btn" style="margin-left: 0;">1x</button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- Input Capsule (Apple Style) -->
    <div class="input-capsule-container">
      
      <!-- Scroll to bottom button -->
      <button id="scroll-to-bottom-btn" class="liquid-glass" style="display: none; position: absolute; top: -46px; left: 50%; transform: translateX(-50%); background: rgba(30, 30, 35, 0.85); color: white; border: 1px solid rgba(255,255,255,0.15); border-radius: 50%; width: 36px; height: 36px; padding: 0; justify-content: center; align-items: center; z-index: 100; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.3); transition: opacity 0.2s, transform 0.2s, top 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);">
        <svg id="scroll-arrow-icon" viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" style="margin-top: 2px;"><line x1="12" y1="4" x2="12" y2="20"></line><polyline points="18 14 12 20 6 14"></polyline></svg>
        <div id="scroll-typing-indicator" style="display: none; gap: 4px; align-items: center;">
          <div style="width: 5px; height: 5px; background: white; border-radius: 50%; animation: bounce 1.4s infinite ease-in-out both;"></div>
          <div style="width: 5px; height: 5px; background: white; border-radius: 50%; animation: bounce 1.4s infinite ease-in-out both; animation-delay: 0.16s;"></div>
          <div style="width: 5px; height: 5px; background: white; border-radius: 50%; animation: bounce 1.4s infinite ease-in-out both; animation-delay: 0.32s;"></div>
        </div>
      </button>

      <!-- File Permission Popup -->
      <div id="file-permission-popup" style="display: none; position: absolute; top: -100px; left: 16px; right: 16px; background: rgba(30, 30, 35, 0.95); border-radius: 16px; border: 1px solid rgba(182, 120, 242, 0.3); padding: 12px 16px; box-shadow: 0 8px 32px rgba(0,0,0,0.4); z-index: 101; backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); align-items: center; justify-content: space-between; gap: 12px; animation: slide-up 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);">
        <div style="display: flex; flex-direction: column; flex: 1; overflow: hidden;">
          <div style="font-size: 12px; font-weight: 600; color: #b678f2; margin-bottom: 2px;">KI Dateizugriff</div>
          <div id="file-permission-text" style="font-size: 13px; color: white; line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;">Gemini möchte Datei X lesen.</div>
        </div>
        <div style="display: flex; flex-direction: column; gap: 6px;">
          <button id="file-permission-allow" style="background: #b678f2; color: white; border: none; padding: 6px 12px; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer;">Erlauben</button>
          <button id="file-permission-deny" style="background: rgba(255,255,255,0.1); color: white; border: none; padding: 6px 12px; border-radius: 8px; font-size: 13px; font-weight: 500; cursor: pointer;">Ablehnen</button>
        </div>
      </div>

      <div id="image-preview-container" style="display: none; padding: 12px 16px 16px 16px; z-index: 0; position: relative; gap: 12px; overflow-x: auto; white-space: nowrap; align-items: center;">
      </div>
      <div class="input-capsule siri-capsule" style="z-index: 1; position: relative;">
        <input type="file" id="image-upload-input" accept="image/*,application/pdf" multiple style="display: none;" />
        <button class="icon-btn" id="plus-menu-btn" title="Menü">
          <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="opacity: 0.7;"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
        </button>
        <textarea placeholder="Nachricht an Gemini..." rows="1" id="chat-input"></textarea>
        <button class="icon-btn primary" id="send-btn">
          <svg viewBox="0 0 24 24" width="26" height="26" stroke="currentColor" stroke-width="2" fill="none" style="margin-left: -2px; margin-top: 1px;"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>
      </div>
      
      <!-- Plus Popover Menu -->
      <div id="plus-popover-menu" class="liquid-glass-heavy" style="display: none; position: absolute; bottom: calc(100% + 16px); left: 16px; border-radius: 16px; padding: 12px; min-width: 260px; z-index: 10; box-shadow: 0 16px 40px rgba(0,0,0,0.4); border: 1px solid rgba(255,255,255,0.08); flex-direction: column; gap: 4px; transform-origin: bottom left; animation: popover-in 0.2s cubic-bezier(0.16, 1, 0.3, 1);">
        <div style="font-size: 11px; font-weight: 600; color: rgba(255,255,255,0.5); text-transform: uppercase; letter-spacing: 0.5px; padding: 4px 8px; margin-bottom: 4px;">Aktionen</div>
        <button id="menu-attach-image-btn" style="display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: transparent; border: none; color: white; border-radius: 10px; cursor: pointer; text-align: left; font-size: 14px; transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='transparent'">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="opacity: 0.7;"><rect width="18" height="18" x="3" y="3" rx="2" ry="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"/></svg>
          Bild/Dokument anhängen
        </button>
        
        <div style="height: 1px; background: rgba(255,255,255,0.1); margin: 6px 0;"></div>
        
        <div style="font-size: 11px; font-weight: 600; color: rgba(255,255,255,0.5); text-transform: uppercase; letter-spacing: 0.5px; padding: 4px 8px; margin-bottom: 4px;">Features</div>
        <div style="display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-radius: 10px; cursor: pointer; transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='transparent'" onclick="document.getElementById('deep-research-toggle').click()">
          <div style="display: flex; align-items: center; gap: 12px; font-size: 14px;">
            <span style="font-size: 16px;">✨</span> Vertiefte Recherche
          </div>
          <input type="checkbox" id="deep-research-toggle" class="ios-toggle" style="transform: scale(0.7); margin: 0; pointer-events: none;">
        </div>
        
        <div id="detective-board-container" style="display: none; align-items: center; justify-content: space-between; padding: 10px 12px; padding-left: 38px; border-radius: 10px; cursor: pointer; transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='transparent'" onclick="document.getElementById('detective-board-toggle').click()">
          <div style="display: flex; align-items: center; gap: 12px; font-size: 14px; color: rgba(255,255,255,0.8);">
            <span style="font-size: 16px;">🕵️</span> Detective Board
          </div>
          <input type="checkbox" id="detective-board-toggle" class="ios-toggle" style="transform: scale(0.7); margin: 0; pointer-events: none;" checked>
        </div>
        <!-- NEUER SLIDER / SELECT -->
        <div id="deep-research-time-container" style="display: none; padding: 0 12px 10px 38px; font-size: 12px; color: rgba(255,255,255,0.6);">
          <div style="display: flex; justify-content: space-between; align-items: center; gap: 8px;">
            <span>Dauer (Tiefe):</span>
            <select id="deep-research-time-select" style="background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.2); color: white; padding: 2px 6px; border-radius: 4px; outline: none; font-size: 12px;" onclick="event.stopPropagation()">
              <option value="1">1 Minute (Schnell)</option>
              <option value="3" selected>3 Minuten (Standard)</option>
              <option value="5">5 Minuten (Tief)</option>
              <option value="10">10 Minuten (Extrem)</option>
            </select>
          </div>
        </div>

        <div style="display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-radius: 10px; cursor: pointer; transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='transparent'" onclick="document.getElementById('extended-thinking-toggle').click()">
          <div style="display: flex; align-items: center; gap: 12px; font-size: 14px;">
            <span style="font-size: 16px;">🧠</span> Erweitertes Denken
          </div>
          <input type="checkbox" id="extended-thinking-toggle" class="ios-toggle" style="transform: scale(0.7); margin: 0; pointer-events: none;">
        </div>

        <div style="height: 1px; background: rgba(255,255,255,0.1); margin: 6px 0;"></div>

        <div style="font-size: 11px; font-weight: 600; color: rgba(255,255,255,0.5); text-transform: uppercase; letter-spacing: 0.5px; padding: 4px 8px; margin-bottom: 4px;">Lokales Wissen</div>
        <div style="display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-radius: 10px; cursor: pointer; transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='transparent'" onclick="document.getElementById('vault-search-toggle').click()">
          <div style="display: flex; align-items: center; gap: 12px; font-size: 14px;">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="opacity: 0.7;"><path d="M4 20h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.93a2 2 0 0 1-1.66-.9l-.82-1.2A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13c0 1.1.9 2 2 2Z"/></svg>
            Vault durchsuchen
          </div>
          <input type="checkbox" id="vault-search-toggle" class="ios-toggle" style="transform: scale(0.7); margin: 0; pointer-events: none;">
        </div>
        
        <div style="height: 1px; background: rgba(255,255,255,0.1); margin: 6px 0;"></div>
        
        <div style="font-size: 11px; font-weight: 600; color: rgba(255,255,255,0.5); text-transform: uppercase; letter-spacing: 0.5px; padding: 4px 8px; margin-bottom: 4px;">Stimme & Stil</div>
        <div style="display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-radius: 10px; cursor: pointer; transition: background 0.2s;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='transparent'" onclick="document.getElementById('plus-profile-toggle').click()">
          <div style="display: flex; align-items: center; gap: 12px; font-size: 14px;">
            <span style="font-size: 16px;">✍️</span> Persönlicher Style
          </div>
          <input type="checkbox" id="plus-profile-toggle" class="chat-profile-toggle ios-toggle" style="transform: scale(0.7); margin: 0; pointer-events: none;">
        </div>
      </div>
      </div>
    </div>

    <!-- Bottom Tab Bar -->
    <nav class="bottom-tab-bar">
      <svg width="0" height="0" style="position: absolute;">
        <defs>
          <linearGradient id="active-icon-gradient" x1="0%" y1="0%" x2="100%" y2="100%">
            <stop offset="0%" stop-color="#c183f0" />
            <stop offset="100%" stop-color="#8a53e6" />
          </linearGradient>
        </defs>
      </svg>
      <div class="tab-item active" data-tab="Chat">
        <svg viewBox="0 0 24 24"><path d="M12 2C6.477 2 2 6.03 2 11c0 2.84 1.503 5.37 3.856 7.02L4 22l4.28-1.564A10.61 10.61 0 0012 20c5.523 0 10-4.03 10-9s-4.477-9-10-9z"/></svg>
      </div>
      <div class="tab-item" data-tab="News">
        <svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>
      </div>
      <div class="tab-item" data-tab="Hub">
        <svg viewBox="0 0 24 24"><path d="M4 4h6v6H4zm0 10h6v6H4zm10 0h6v6h-6zm0-10h6v6h-6z"/></svg>
      </div>
      <div class="tab-item" data-tab="Vault">
        <!-- Changed to a solid folder icon for consistency with others -->
        <svg viewBox="0 0 24 24"><path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"/></svg>
      </div>
      <div class="tab-item" data-tab="Settings">
        <svg viewBox="0 0 24 24"><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.06-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.73,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.06,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.43-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.49-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></svg>
      </div>
    </nav>
  `;

  // View Instances
  const chatView = new ChatView('router-view');
  try {
    await chatView.init();
  } catch (err) {
    console.error('Failed to initialize ChatView:', err);
    // Continue execution so tabs still work even if chat fails
  }
  window.chatView = chatView; // Export globally for IServ View to trigger PDF modal
  window.openPdfModal = chatView.openPdfModal ? chatView.openPdfModal.bind(chatView) : null;
  window.openArticleModal = chatView.openArticleModal ? chatView.openArticleModal.bind(chatView) : null;
  // We lazily import or instantiate SettingsView when needed, or just import it at top
  // To keep it simple, we'll instantiate it directly here but handle rendering in tab clicks.

  // --- TAB NAVIGATION LOGIC ---
  const tabItems = document.querySelectorAll('.tab-item');
  const inputCapsule = document.querySelector('.input-capsule-container');
  const routerView = document.getElementById('router-view');
  
  let activeView = null; // Track the currently active view to destroy it when switching

  tabItems.forEach(tab => {
    tab.addEventListener('click', async () => {
      const tabName = tab.getAttribute('data-tab');
      // Remove active class from all
      tabItems.forEach(t => t.classList.remove('active'));
      // Add active class to clicked
      tab.classList.add('active');
      
      await window.openView(tabName);
    });
  });

  const handleImportError = (e) => {
    if (e.message && e.message.includes('Failed to fetch dynamically imported module')) {
      // The app was updated and the old chunks are gone from the server.
      // Force a reload to fetch the new index.html and its new chunk map.
      routerView.innerHTML = `<div style="padding: 20px; color: white; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; text-align: center;">
        <div style="font-size: 40px; margin-bottom: 20px;">🔄</div>
        <div style="font-size: 18px; font-weight: 600; margin-bottom: 8px;">App wurde aktualisiert</div>
        <div style="font-size: 14px; opacity: 0.7;">Die neuste Version wird geladen...</div>
      </div>`;
      setTimeout(() => {
        window.location.reload(true);
      }, 1000);
    } else {
      routerView.innerHTML = `<div style="padding: 20px; color: red;">Error: ${e.message}</div>`;
    }
  };

  let currentOpenViewCounter = 0;

  window.openView = async (viewName, isSubView = false) => {
    currentOpenViewCounter++;
    const myCounter = currentOpenViewCounter;
    
    // Cleanup the previous view
    if (activeView && typeof activeView.destroy === 'function') {
      activeView.destroy();
    }
    
    // Safety fallback: always ensure chat is destroyed if we navigate away
    if (viewName !== 'Chat' && window.chatView) {
      window.chatView.destroy();
    }
    
    activeView = null;
    
    document.body.classList.toggle('in-chat-view', viewName === 'Chat');
    document.body.classList.toggle('in-hub-view', viewName === 'Hub');
    document.body.classList.toggle('in-news-view', viewName === 'News');
    document.body.classList.toggle('in-vault-view', viewName === 'Vault');
    
    routerView.classList.remove('view-enter-anim');
    routerView.classList.add('view-exit-anim');
    
    await new Promise(resolve => setTimeout(resolve, 150));
    
    // Abort if another view was opened while we were waiting
    if (currentOpenViewCounter !== myCounter) return;
    
    routerView.innerHTML = '';
    
    // Reset scroll position when switching main tabs
    if (!isSubView) {
      routerView.scrollTop = 0;
    }
    
    if (viewName === 'Chat') {
      inputCapsule.style.display = 'block';
      try {
        chatView.render();
        chatView.loadMessages();
      } catch(e) {
        handleImportError(e);
      }
      activeView = chatView;
    } 
    else if (viewName === 'Settings') {
      inputCapsule.style.display = 'none';
      try {
        const { SettingsView } = await import('./views/settings/settings-view.js');
        const settingsView = new SettingsView('router-view');
        settingsView.init();
        activeView = settingsView;
      } catch (e) {
        handleImportError(e);
      }
    }
    else if (viewName === 'Vault') {
      inputCapsule.style.display = 'none';
      try {
        const { DocumentsView } = await import('./views/documents/documents-view.js');
        const docsView = new DocumentsView('router-view');
        await docsView.init();
        activeView = docsView;
      } catch (e) {
        handleImportError(e);
      }
    }
    else if (viewName === 'Hub') {
      inputCapsule.style.display = 'none';
      try {
        const { HubView } = await import('./views/hub/hub-view.js');
        const hubView = new HubView('router-view');
        await hubView.init();
        activeView = hubView;
      } catch (e) {
        handleImportError(e);
      }
    }
    else if (viewName === 'News') {
      inputCapsule.style.display = 'none';
      try {
        const { NewsView } = await import('./views/news/news-view.js');
        const newsView = new NewsView('router-view');
        await newsView.init();
        activeView = newsView;
      } catch (e) {
        handleImportError(e);
      }
    }
    // Sub-views that can be opened from the Hub
    else if (viewName === 'Lernen') {
      inputCapsule.style.display = 'none';
      try {
        const { LearnView } = await import('./views/learn/learn-view.js');
        const learnView = new LearnView('router-view');
        await learnView.init();
        learnView.render();
        window.learnView = learnView; 
        activeView = learnView;
      } catch (e) {
        handleImportError(e);
      }
    }
    else if (viewName === 'Scan') {
      inputCapsule.style.display = 'none';
      try {
        const { ScanView } = await import('./views/scan/scan-view.js');
        const scanView = new ScanView('router-view');
        await scanView.init();
        scanView.render();
        activeView = scanView;
      } catch (e) {
        handleImportError(e);
      }
    }
    else if (viewName === 'Kalender') {
      inputCapsule.style.display = 'none';
      try {
        const { CalendarView } = await import('./views/calendar/calendar-view.js');
        const calView = new CalendarView('router-view');
        await calView.init();
        activeView = calView;
      } catch (e) {
        handleImportError(e);
      }
    }
    else if (viewName === 'IServ') {
      inputCapsule.style.display = 'none';
      try {
        const { IServView } = await import('./views/iserv/iserv-view.js');
        const iservView = new IServView('router-view');
        await iservView.init();
        activeView = iservView;
      } catch (e) {
        handleImportError(e);
      }
    }
    
    routerView.classList.remove('view-exit-anim');
    routerView.classList.add('view-enter-anim');
  };

  // Helper for Hub to open subviews without changing the active tab highlight
  window.openHubSubView = (viewName) => {
    window.openView(viewName, true);
  };

  // --- CHAT LOGIC ---
  const textarea = document.getElementById('chat-input');
  textarea.addEventListener('input', function() {
    this.style.height = 'auto';
    this.style.height = (this.scrollHeight < 120 ? this.scrollHeight : 120) + 'px';
  });

  // --- PLUS MENU & IMAGE UPLOAD LOGIC ---
  const plusMenuBtn = document.getElementById('plus-menu-btn');
  const plusPopoverMenu = document.getElementById('plus-popover-menu');
  const menuAttachBtn = document.getElementById('menu-attach-image-btn');
  const fileInput = document.getElementById('image-upload-input');
  const previewContainer = document.getElementById('image-preview-container');
  
  window.currentAttachments = []; // Max 20

  function renderAttachments() {
    previewContainer.innerHTML = '';
    if (window.currentAttachments.length === 0) {
      previewContainer.style.display = 'none';
      return;
    }
    
    previewContainer.style.display = 'flex';
    
    window.currentAttachments.forEach((att, index) => {
      const itemWrapper = document.createElement('div');
      itemWrapper.className = 'gpu-accelerated';
      itemWrapper.style = 'position: relative; display: inline-flex; align-items: center; justify-content: center; padding: 6px; background: rgba(255,255,255,0.03); border-radius: 14px; border: 1px solid rgba(255,255,255,0.08); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); box-shadow: 0 8px 32px rgba(0,0,0,0.2); width: 80px; height: 80px; flex-shrink: 0;';
      
      if (att.mimeType === 'application/pdf') {
        itemWrapper.innerHTML = `
          <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; width: 100%; height: 100%; background: #eb4d4b; border-radius: 8px;">
            <svg viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>
            <div style="font-size: 9px; color: white; margin-top: 4px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 90%;">${att.name}</div>
          </div>
        `;
      } else {
        itemWrapper.innerHTML = `
          <img src="${att.data}" style="width: 100%; height: 100%; object-fit: cover; border-radius: 8px; display: block;" />
        `;
      }
      
      const delBtn = document.createElement('button');
      delBtn.style = 'position: absolute; top: -6px; right: -6px; background: rgba(40,40,45,0.9); color: #fff; border: 1px solid rgba(255,255,255,0.15); border-radius: 50%; width: 22px; height: 22px; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 10px; padding: 0; box-shadow: 0 4px 12px rgba(0,0,0,0.4); z-index: 2;';
      delBtn.innerHTML = '✕';
      delBtn.onclick = () => {
        window.currentAttachments.splice(index, 1);
        renderAttachments();
      };
      
      itemWrapper.appendChild(delBtn);
      previewContainer.appendChild(itemWrapper);
    });
  }

  // Toggle plus menu
  if (plusMenuBtn && plusPopoverMenu) {
    plusMenuBtn.addEventListener('click', (e) => {
      e.stopPropagation();
      const isVisible = plusPopoverMenu.style.display === 'flex';
      plusPopoverMenu.style.display = isVisible ? 'none' : 'flex';
    });

    // Close when clicking outside
    document.addEventListener('click', (e) => {
      if (!plusPopoverMenu.contains(e.target) && !plusMenuBtn.contains(e.target)) {
        plusPopoverMenu.style.display = 'none';
      }
    });

    document.body.addEventListener('change', (e) => {
      if (e.target.id === 'deep-research-toggle') {
        const container = document.getElementById('deep-research-time-container');
        if(container) container.style.display = e.target.checked ? 'block' : 'none';
        const detectiveContainer = document.getElementById('detective-board-container');
        if(detectiveContainer) detectiveContainer.style.display = e.target.checked ? 'flex' : 'none';
      }
    });
  }

  if (menuAttachBtn) {
    menuAttachBtn.addEventListener('click', () => {
      if (plusPopoverMenu) plusPopoverMenu.style.display = 'none';
      fileInput.click();
    });
  }

  function handleImageFile(file) {
    if (!file || !file.type.startsWith('image/')) return;
    if (window.currentAttachments.length >= 20) {
      alert("Maximal 20 Anhänge erlaubt.");
      return;
    }
    
    const reader = new FileReader();
    reader.onload = (event) => {
      const img = new Image();
      img.onload = () => {
        const canvas = document.createElement('canvas');
        const MAX_DIM = 800;
        let width = img.width;
        let height = img.height;

        if (width > height && width > MAX_DIM) {
          height *= MAX_DIM / width;
          width = MAX_DIM;
        } else if (height > MAX_DIM) {
          width *= MAX_DIM / height;
          height = MAX_DIM;
        }

        canvas.width = width;
        canvas.height = height;
        const ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, width, height);

        const dataUrl = canvas.toDataURL('image/jpeg', 0.7);
        window.currentAttachments.push({ mimeType: 'image/jpeg', data: dataUrl, name: file.name });
        renderAttachments();
      };
      img.src = event.target.result;
    };
    reader.readAsDataURL(file);
  }

  async function handlePdfFile(file) {
    if (window.currentAttachments.length >= 20) {
      alert("Maximal 20 Anhänge erlaubt.");
      return;
    }
    
    const reader = new FileReader();
    reader.onload = (event) => {
      window.currentAttachments.push({ mimeType: 'application/pdf', data: event.target.result, name: file.name });
      renderAttachments();
    };
    reader.readAsDataURL(file);
  }

  function clearImage() {
    window.currentAttachments = [];
    renderAttachments();
    fileInput.value = '';
    window.updateChatPlaceholder();
  }
  
  // Handle global errors gracefully
  window.addEventListener('unhandledrejection', event => {
    console.error('Unhandled promise rejection:', event.reason);
  });

  // --- File Permission UI Logic ---
  let activePermissionResolve = null;
  const permPopup = document.getElementById('file-permission-popup');
  const permText = document.getElementById('file-permission-text');
  const permAllow = document.getElementById('file-permission-allow');
  const permDeny = document.getElementById('file-permission-deny');

  window.addEventListener('request-file-permission', (e) => {
    const detail = e.detail;
    activePermissionResolve = detail.resolve;
    
    let actionText = 'lesen';
    if (detail.action === 'write') actionText = 'erstellen/überschreiben';
    if (detail.action === 'delete') actionText = 'löschen';
    
    permText.innerText = `Die KI möchte die Datei "${detail.path}" ${actionText}.`;
    permPopup.style.display = 'flex';
    
    // Haptic feedback to alert user
    if (window.navigator && window.navigator.vibrate) {
      window.navigator.vibrate(50);
    }
  });

  const closePermissionPopup = (allowed) => {
    permPopup.style.display = 'none';
    if (activePermissionResolve) {
      activePermissionResolve(allowed);
      activePermissionResolve = null;
    }
  };

  permAllow.addEventListener('click', () => closePermissionPopup(true));
  permDeny.addEventListener('click', () => closePermissionPopup(false));
  
  fileInput.addEventListener('change', (e) => {
    const files = Array.from(e.target.files);
    if (!files.length) return;
    
    files.forEach(file => {
      if (file.type === 'application/pdf') {
        handlePdfFile(file);
      } else {
        handleImageFile(file);
      }
    });
    
    // Clear the input so selecting the same files again works
    fileInput.value = '';
  });

  // Global Paste Event Listener for Images
  document.addEventListener('paste', (e) => {
    const items = (e.clipboardData || e.originalEvent.clipboardData).items;
    for (let index in items) {
      const item = items[index];
      if (item.kind === 'file') {
        const file = item.getAsFile();
        if (file) {
          if (file.type === 'application/pdf') {
             e.preventDefault();
             handlePdfFile(file);
             break;
          } else if (file.type.includes('image')) {
             e.preventDefault();
             handleImageFile(file);
             break;
          }
        }
      }
    }
  });

  // Handle Sending
  const sendBtn = document.getElementById('send-btn');
  
  const handleSend = async () => {
    if (chatView && chatView.isGenerating) {
      if (chatView.currentAbortController) {
        chatView.currentAbortController.abort();
      }
      return;
    }

    const text = textarea.value;
    const hasAttachments = window.currentAttachments && window.currentAttachments.length > 0;
    
    if (text.trim() || hasAttachments) {
      // Send button spin animation
      sendBtn.classList.add('sending');
      setTimeout(() => sendBtn.classList.remove('sending'), 600);
      
      textarea.value = '';
      textarea.style.height = 'auto';
      
      let finalMessage = text.trim();
      
      if (hasAttachments) {
        let attachStr = '';
        window.currentAttachments.forEach(att => {
          attachStr += `![${att.name}](${att.data})\n`;
        });
        finalMessage = `${attachStr}\n${finalMessage}`;
        clearImage();
      }
      
      // Check for API Key if using cloud models
      const currentModel = db.getSetting('model') || 'gemini-1.5-flash';
      
      if (currentModel.startsWith('gemini') && !db.getSetting('gemini_api_key')) {
        const key = await window.appPrompt("Bitte gib deinen Gemini API Key ein (wird lokal gespeichert):");
        if (key) db.setSetting('gemini_api_key', key.trim()); else return;
      } else if (currentModel.startsWith('groq') && !db.getSetting('groq_api_key')) {
        const key = await window.appPrompt("Bitte gib deinen Groq API Key ein (wird lokal gespeichert):");
        if (key) db.setSetting('groq_api_key', key.trim()); else return;
      } else if (currentModel.startsWith('openrouter') && !db.getSetting('openrouter_api_key')) {
        const key = await window.appPrompt("Bitte gib deinen OpenRouter API Key ein (wird lokal gespeichert):");
        if (key) db.setSetting('openrouter_api_key', key.trim()); else return;
      } else if (currentModel.startsWith('deepseek') && !db.getSetting('deepseek_api_key')) {
        const key = await window.appPrompt("Bitte gib deinen DeepSeek API Key ein (wird lokal gespeichert):");
        if (key) db.setSetting('deepseek_api_key', key.trim()); else return;
      }
      
      chatView.handleSend(finalMessage);
      
      // Auto-collapse in PDF mode after sending
      if (document.body.classList.contains('pdf-reading-mode')) {
        document.body.classList.add('pdf-input-collapsed');
      }
    }
  };

  sendBtn.addEventListener('click', handleSend);
  textarea.addEventListener('keydown', (e) => {
    if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
      e.preventDefault();
      handleSend();
    }
  });

  window.updateChatPlaceholder = function() {
    try {
      const model = db.getSetting('model') || 'gemini-1.5-flash';
      let aiName = "SchoolMind AI";
      let isLocal = false;
      
      if (model.includes('gemini') || model.includes('Gemini')) aiName = "Gemini";
      else if (model.includes('claude') || model.includes('Claude')) aiName = "Claude";
      else if (model.includes('qwen') || model.includes('Qwen')) aiName = "Qwen";
      else if (model.includes('llama') || model.includes('Llama')) aiName = "Llama";
      else if (model.includes('lyria') || model.includes('Lyria')) aiName = "Lyria";
      else if (model.includes('gemma') || model.includes('Gemma')) aiName = "Gemma";
      else if (model.includes('deepseek') || model.includes('DeepSeek')) aiName = "DeepSeek";
      else if (model.includes('mistral') || model.includes('codestral')) aiName = "Mistral";
      
      if (model.startsWith('ollama-') || model.includes('nano-')) isLocal = true;
      
      let userName = "";
      const useProfileStr = db.getSetting('use_profile');
      const useProfile = useProfileStr === null ? true : useProfileStr === 'true';
      
      if (useProfile) {
        try {
          const activeProfileRes = db.getActiveProfile();
          if (activeProfileRes && activeProfileRes.length > 0 && activeProfileRes[0].values.length > 0) {
            const row = activeProfileRes[0].values[0];
            const nameIndex = activeProfileRes[0].columns.indexOf('name');
            if (nameIndex !== -1 && row[nameIndex]) {
              userName = row[nameIndex];
            }
          }
        } catch (profileErr) {
          console.warn("Profile fetch error:", profileErr);
        }
      }
      
      let placeholder = `Nachricht an ${aiName}...`;
      if (userName) {
        placeholder = `Als ${userName} an ${aiName}...`;
      }
      if (isLocal) {
        placeholder = `(Lokal) ` + placeholder;
      }
      
      const chatInput = document.getElementById('chat-input');
      if (chatInput) {
        chatInput.placeholder = placeholder;
      }
    } catch (e) {
      const chatInput = document.getElementById('chat-input');
      if (chatInput) {
        if (e.message.includes('not ready')) {
          chatInput.placeholder = "Warte auf Datenbank...";
        } else {
          chatInput.placeholder = "ERROR: " + e.message;
        }
      }
    }
  };
  
  // Wait for DB to be ready before first update
  db.initPromise.then(() => window.updateChatPlaceholder());

  // --- Scroll to Bottom Button Logic ---
  const routerViewEl = document.getElementById('router-view');
  const scrollBtn = document.getElementById('scroll-to-bottom-btn');
  
  routerViewEl.addEventListener('scroll', (e) => {
    // Only show in chat view
    if (document.body.classList.contains('in-chat-view')) {
      const scrollContainer = document.getElementById('chat-scroll-area') || routerViewEl;
      if (e.target !== scrollContainer && e.target !== routerViewEl) return;
      
      const scrollHeight = scrollContainer.scrollHeight;
      const scrollTop = scrollContainer.scrollTop;
      const clientHeight = scrollContainer.clientHeight;
      
      // If user scrolled up by more than 80 lines (ca. 1000px)
      if (scrollHeight - scrollTop - clientHeight > 1000) {
        scrollBtn.style.display = 'flex';
        setTimeout(() => scrollBtn.style.opacity = '1', 10);
      } else {
        scrollBtn.style.opacity = '0';
        setTimeout(() => {
          if (scrollBtn.style.opacity === '0') scrollBtn.style.display = 'none';
        }, 200);
      }
    } else {
      scrollBtn.style.display = 'none';
    }
  }, true);

  scrollBtn.addEventListener('click', () => {
    const scrollContainer = document.getElementById('chat-scroll-area') || routerViewEl;
    scrollContainer.scrollTo({
      top: scrollContainer.scrollHeight,
      behavior: 'smooth'
    });
  });

  window.setGeneratingState = (isGen) => {
    const icon = document.getElementById('scroll-arrow-icon');
    const ind = document.getElementById('scroll-typing-indicator');
    if (icon && ind) {
      if (isGen) {
        icon.style.display = 'none';
        ind.style.display = 'flex';
      } else {
        icon.style.display = 'block';
        ind.style.display = 'none';
      }
    }
  };


   // Extract and Wrap Words Helper for TTS
  window.extractAndWrapText = (element) => {
    // If already wrapped, just return the text
    if (element.hasAttribute('data-words-wrapped')) {
      return element.getAttribute('data-words-wrapped-text');
    }
    
    const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
    const nodes = [];
    while (walker.nextNode()) nodes.push(walker.currentNode);
    
    let wordIndex = 0;
    let fullTextParts = [];
    
    nodes.forEach(node => {
      if (node.nodeValue.trim() === '') {
        fullTextParts.push(node.nodeValue);
        return;
      }
      const parts = node.nodeValue.split(/(\s+)/);
      const fragment = document.createDocumentFragment();
      parts.forEach(part => {
        if (part.trim().length > 0) {
          const span = document.createElement('span');
          span.className = 'tts-word';
          span.dataset.ttsIndex = wordIndex++;
          span.textContent = part;
          fragment.appendChild(span);
          fullTextParts.push(part);
        } else {
          fragment.appendChild(document.createTextNode(part));
          fullTextParts.push(part);
        }
      });
      node.parentNode.replaceChild(fragment, node);
    });
    
    const finalString = fullTextParts.join('');
    element.setAttribute('data-words-wrapped', wordIndex);
    element.setAttribute('data-words-wrapped-text', finalString);
    return finalString;
  };

  // Play TTS Message from a DOM Element
  window.playTTSMessage = (btn, rawText) => {
    // The button is inside .ai-msg-actions, which is a sibling of .chat-bubble-ai
    const actionsDiv = btn.closest('.ai-msg-actions');
    const wrapper = actionsDiv ? actionsDiv.parentElement : null;
    const container = wrapper ? wrapper.querySelector('.chat-bubble-ai') : null;
    
    if (container) {
      const textToPlay = window.extractAndWrapText(container);
      window.ttsService.play(textToPlay, true);
    } else {
      window.ttsService.play(rawText, false);
    }
  };

  // --- TTS Player Logic ---
  const ttsPlayer = document.getElementById('tts-player-container');
  const ttsPlayBtn = document.getElementById('tts-play-btn');
  const ttsPlayIcon = document.getElementById('tts-play-icon');
  const ttsPrevBtn = document.getElementById('tts-prev-btn');
  const ttsNextBtn = document.getElementById('tts-next-btn');
  const ttsRateBtn = document.getElementById('tts-rate-btn');
  const ttsCcBtn = document.getElementById('tts-cc-btn');
  let ttsCcEnabled = true;

  if (ttsPlayer) {
    // CC Button logic
    ttsCcBtn.addEventListener('click', () => {
      ttsCcEnabled = !ttsCcEnabled;
      if (ttsCcEnabled) {
        ttsCcBtn.classList.add('active');
        ttsCcBtn.style.color = 'var(--primary)';
      } else {
        ttsCcBtn.classList.remove('active');
        ttsCcBtn.style.color = 'var(--text-secondary)';
        // Remove active highlights
        document.querySelectorAll('.tts-word.active').forEach(el => {
          el.classList.remove('active');
          el.style.background = 'transparent';
          el.style.color = 'inherit';
        });
      }
    });

    ttsService.onStateChange = (state) => {
      const scrollBtn = document.getElementById('scroll-to-bottom-btn');
      
      // Update word highlights based on progress
      if (ttsCcEnabled && state.currentWordIndex !== undefined) {
        document.querySelectorAll('.tts-word.active').forEach(el => el.classList.remove('active'));
        const activeWord = document.querySelector(`.tts-word[data-tts-index="${state.currentWordIndex}"]`);
        if (activeWord) {
          activeWord.classList.add('active');
          
          if (!window.ttsScrollTimeout) {
            window.ttsScrollTimeout = setTimeout(() => {
              const scrollContainer = activeWord.closest('#chat-scroll-area') || activeWord.closest('.view-container');
              if (scrollContainer) {
                const containerRect = scrollContainer.getBoundingClientRect();
                const wordRect = activeWord.getBoundingClientRect();
                
                const isOutsideMiddle = wordRect.top < containerRect.top + containerRect.height * 0.3 || 
                                        wordRect.bottom > containerRect.bottom - containerRect.height * 0.3;
                
                if (isOutsideMiddle) {
                  const scrollOffset = wordRect.top - containerRect.top + scrollContainer.scrollTop - (containerRect.height / 2);
                  scrollContainer.scrollTo({ top: scrollOffset, behavior: 'smooth' });
                }
              }
              window.ttsScrollTimeout = null;
            }, 300); // Small debounce to prevent jitter
          }
        }
      }

      if (state.hasActiveSession) {
        ttsPlayer.classList.remove('collapsed');
        if (scrollBtn) scrollBtn.style.top = '-130px';
      } else {
        ttsPlayer.classList.add('collapsed');
        if (scrollBtn) scrollBtn.style.top = '-46px';
      }
      
      if (state.isLoading) {
        ttsPlayer.classList.add('playing');
        ttsPlayIcon.innerHTML = '<circle cx="12" cy="12" r="8" stroke="currentColor" stroke-width="3" fill="none" stroke-dasharray="25 25" stroke-linecap="round"><animateTransform attributeName="transform" type="rotate" from="0 12 12" to="360 12 12" dur="1s" repeatCount="indefinite"/></circle>';
        ttsPlayBtn.style.opacity = '0.5';
      } else if (state.isPlaying) {
        ttsPlayer.classList.add('playing');
        ttsPlayIcon.innerHTML = '<rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect>';
        ttsPlayBtn.style.opacity = '1';
      } else {
        ttsPlayer.classList.remove('playing');
        ttsPlayIcon.innerHTML = '<polygon points="7 3 21 12 7 21 7 3"></polygon>';
        ttsPlayBtn.style.opacity = '1';
      }
      
      ttsRateBtn.innerText = state.rate + 'x';
    };
    
    ttsPlayBtn.addEventListener('click', () => ttsService.togglePlayPause());
    ttsPrevBtn.addEventListener('click', () => ttsService.prev());
    ttsNextBtn.addEventListener('click', () => ttsService.next());
    ttsRateBtn.addEventListener('click', () => ttsService.cycleRate());
    
    // Swipe down to close
    let touchStartY = 0;
    let touchEndY = 0;
    ttsPlayer.addEventListener('touchstart', (e) => {
      touchStartY = e.touches[0].clientY;
      touchEndY = touchStartY;
      ttsPlayer.style.transition = 'none'; // Instant drag
    });
    ttsPlayer.addEventListener('touchmove', (e) => {
      touchEndY = e.touches[0].clientY;
      const deltaY = touchEndY - touchStartY;
      if (deltaY > 0) {
        ttsPlayer.style.transform = `translateY(${deltaY}px)`;
        // Smoothly fade out as it reaches the 70px threshold
        const dragOpacity = Math.max(0, 1 - (deltaY / 70));
        ttsPlayer.style.opacity = dragOpacity;
      }
    });
    ttsPlayer.addEventListener('touchend', () => {
      ttsPlayer.style.transition = ''; // Restore CSS transition
      ttsPlayer.style.transform = '';  // Restore CSS transform
      ttsPlayer.style.opacity = '';    // Restore CSS opacity
      if (touchEndY > touchStartY + 70) {
        // Swiped down deliberately
        ttsService.stop();
      }
    });
  }
};

document.addEventListener('DOMContentLoaded', () => {
  initApp().catch(err => {
    console.error("Critical app initialization error:", err);
    if (err && err.message && err.message.includes('no such table')) {
      alert("Die lokale Datenbank ist beschädigt. Sie wird nun zurückgesetzt, um die App wiederherzustellen.");
      localStorage.removeItem('schoolmind_db');
      window.location.reload();
    }
  });

  // Also catch if it was launched directly and stored in window
  if (window.pendingAppShortcut === 'add_vocabulary') {
    showVocabularyModal();
    window.pendingAppShortcut = null;
  }
});