File size: 39,506 Bytes
a9ea92e
f87680f
a9ea92e
f87680f
 
db0d89d
 
c7c58b1
db0d89d
c7c58b1
a9ea92e
c7c58b1
c982b83
c7c58b1
a9ea92e
c7c58b1
c982b83
c7c58b1
a9ea92e
c7c58b1
 
 
db0d89d
c7c58b1
a9ea92e
c7c58b1
c982b83
c7c58b1
 
 
 
 
 
 
 
 
 
 
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db0d89d
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
 
a9ea92e
c7c58b1
a9ea92e
c7c58b1
 
 
 
 
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
 
a9ea92e
c7c58b1
db0d89d
a9ea92e
c7c58b1
 
 
db0d89d
a9ea92e
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
ff4e0c9
a9ea92e
 
 
 
 
 
c7c58b1
a9ea92e
 
 
c982b83
a9ea92e
 
 
 
 
 
 
 
 
c982b83
a9ea92e
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
c982b83
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
a9ea92e
 
 
 
 
c7c58b1
 
a9ea92e
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
a9ea92e
 
 
 
c7c58b1
 
a9ea92e
 
 
 
 
 
 
 
db0d89d
a9ea92e
f87680f
a9ea92e
c7c58b1
a9ea92e
c7c58b1
 
 
 
 
 
c982b83
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db0d89d
 
c7c58b1
a9ea92e
c7c58b1
 
a9ea92e
c7c58b1
 
 
 
26a9bf8
 
 
a9ea92e
f87680f
db0d89d
c982b83
a9ea92e
 
 
 
 
 
 
 
 
 
 
26a9bf8
a9ea92e
 
 
 
 
 
 
 
 
 
 
c7c58b1
a9ea92e
 
c7c58b1
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db0d89d
f87680f
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
25ac863
c7c58b1
a9ea92e
db0d89d
f87680f
c7c58b1
db0d89d
 
 
 
 
 
 
c7c58b1
db0d89d
 
c7c58b1
db0d89d
 
ff4e0c9
c7c58b1
db0d89d
 
67ff1a0
 
c7c58b1
db0d89d
c7c58b1
db0d89d
 
 
 
c7c58b1
 
a9ea92e
c7c58b1
 
a9ea92e
 
 
c7c58b1
a9ea92e
c7c58b1
 
 
 
 
 
a9ea92e
c7c58b1
a9ea92e
 
 
 
 
 
 
c7c58b1
a9ea92e
c7c58b1
 
a9ea92e
 
c7c58b1
26a9bf8
a9ea92e
 
 
 
db0d89d
 
 
a9ea92e
26a9bf8
a9ea92e
 
 
 
 
 
25ac863
a9ea92e
db0d89d
 
a9ea92e
 
db0d89d
 
a9ea92e
db0d89d
 
c7c58b1
a9ea92e
db0d89d
a9ea92e
 
 
 
 
db0d89d
a9ea92e
c7c58b1
a9ea92e
 
26a9bf8
a9ea92e
 
 
 
26a9bf8
a9ea92e
 
 
 
 
 
c7c58b1
 
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
 
 
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a9bf8
 
 
db0d89d
 
a9ea92e
 
 
 
 
 
 
 
c7c58b1
 
a9ea92e
67ff1a0
c7c58b1
67ff1a0
 
 
c7c58b1
 
 
 
 
 
 
 
 
 
 
67ff1a0
a9ea92e
26a9bf8
c7c58b1
67ff1a0
 
a9ea92e
db0d89d
a9ea92e
db0d89d
 
 
26a9bf8
a9ea92e
 
 
 
26a9bf8
db0d89d
a9ea92e
db0d89d
 
a9ea92e
 
 
 
 
db0d89d
a9ea92e
 
db0d89d
a9ea92e
db0d89d
a9ea92e
 
26a9bf8
ed39ad7
a9ea92e
 
db0d89d
a9ea92e
db0d89d
 
a9ea92e
 
 
 
c982b83
a9ea92e
 
db0d89d
 
 
a9ea92e
db0d89d
a9ea92e
c7c58b1
a9ea92e
c7c58b1
26a9bf8
db0d89d
 
 
 
 
a9ea92e
 
 
 
 
c7c58b1
a9ea92e
 
ff4e0c9
 
26a9bf8
c7c58b1
a9ea92e
 
 
 
 
 
26a9bf8
ff4e0c9
26a9bf8
a9ea92e
 
 
 
 
 
 
 
 
 
 
ff4e0c9
db0d89d
 
 
 
 
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
db0d89d
a9ea92e
db0d89d
 
a9ea92e
db0d89d
26a9bf8
db0d89d
 
 
67ff1a0
 
26a9bf8
a9ea92e
db0d89d
26a9bf8
ed39ad7
a9ea92e
c7c58b1
67ff1a0
a9ea92e
67ff1a0
c7c58b1
 
 
a9ea92e
c7c58b1
 
67ff1a0
 
a9ea92e
db0d89d
a9ea92e
 
 
 
26a9bf8
a9ea92e
 
db0d89d
a9ea92e
db0d89d
a9ea92e
 
 
 
 
67ff1a0
 
a9ea92e
26a9bf8
a9ea92e
 
67ff1a0
a9ea92e
 
 
67ff1a0
db0d89d
a9ea92e
 
 
 
 
26a9bf8
a9ea92e
 
 
 
 
 
 
 
 
 
db0d89d
a9ea92e
db0d89d
 
26a9bf8
db0d89d
 
 
a9ea92e
db0d89d
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a9bf8
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a9bf8
a9ea92e
26a9bf8
a9ea92e
 
 
26a9bf8
a9ea92e
c7c58b1
 
a9ea92e
c7c58b1
a9ea92e
c7c58b1
a9ea92e
 
 
c7c58b1
a9ea92e
 
 
26a9bf8
db0d89d
a9ea92e
 
 
 
 
 
 
 
 
f87680f
a9ea92e
26a9bf8
a9ea92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c58b1
 
25ac863
db0d89d
a9ea92e
f87680f
a9ea92e
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

<!DOCTYPE html>

<html lang="fr">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Mariam AI</title>

  <!-- Tailwind CSS via CDN -->

  <script src="https://cdn.tailwindcss.com?plugins=forms,typography"></script>

  <!-- Font Awesome pour les icônes -->

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

  <!-- Google Fonts -->

  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">

  <!-- Favicon (Emoji amélioré) -->

  <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>✨</text></svg>">

  <script>
    // Configuration avancée de Tailwind
    tailwind.config = {
      darkMode: 'class',
      theme: {
        extend: {
          fontFamily: {
            sans: ['Inter', 'system-ui', 'sans-serif'],
            mono: ['"JetBrains Mono"', 'monospace'],
          },
          colors: {
            primary: {
              50: '#f0f9ff',
              100: '#e0f2fe',
              200: '#bae6fd',
              300: '#7dd3fc',
              400: '#38bdf8',
              500: '#0ea5e9',
              600: '#0284c7',
              700: '#0369a1',
              800: '#075985',
              900: '#0c4a6e',
            },
            secondary: {
              50: '#f8fafc',
              100: '#f1f5f9',
              200: '#e2e8f0',
              300: '#cbd5e1',
              400: '#94a3b8',
              500: '#64748b',
              600: '#475569',
              700: '#334155',
              800: '#1e293b',
              900: '#0f172a',
            },
            accent: {
              50: '#fdf4ff',
              100: '#fae8ff',
              200: '#f5d0fe',
              300: '#f0abfc',
              400: '#e879f9',
              500: '#d946ef',
              600: '#c026d3',
              700: '#a21caf',
              800: '#86198f',
              900: '#701a75',
            }
          },
          animation: {
            'bounce-slow': 'bounce 2s infinite',
            'pulse-slow': 'pulse 3s infinite',
            'typing': 'typing 1.2s steps(3) infinite',
          },
          keyframes: {
            typing: {
              '0%': { width: '0.15em' },
              '50%': { width: '0.7em' },
              '100%': { width: '0.15em' },
            }
          }
        }
      }
    }
  </script>

  <style>
    /* Base styles */
    html {
      scroll-behavior: smooth;
    }
    
    body {
      font-family: 'Inter', sans-serif;
      transition: background-color 0.3s ease, color 0.3s ease;
    }
    
    /* Layout */
    .chat-layout {
      min-height: calc(100vh - 64px);
      display: grid;
      grid-template-rows: 1fr auto;
    }
    
    /* Scrollbar styling */
    ::-webkit-scrollbar {
      width: 5px;
      height: 5px;
    }
    
    ::-webkit-scrollbar-track {
      background: transparent;
    }
    
    ::-webkit-scrollbar-thumb {
      background: #cbd5e1;
      border-radius: 5px;
    }
    
    .dark ::-webkit-scrollbar-thumb {
      background: #475569;
    }
    
    ::-webkit-scrollbar-thumb:hover {
      background: #94a3b8;
    }
    
    .dark ::-webkit-scrollbar-thumb:hover {
      background: #64748b;
    }
    
    /* Message bubbles */
    .message-bubble {
      position: relative;
      max-width: 85%;
      border-radius: 1rem;
      padding: 0.875rem 1rem;
      line-height: 1.5;
      animation: message-fade-in 0.3s ease-out;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
      transition: transform 0.2s ease, box-shadow 0.2s ease;
    }
    
    .message-bubble:hover {
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.1);
    }
    
    .dark .message-bubble {
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    }
    
    .dark .message-bubble:hover {
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2), 0 1px 3px rgba(0, 0, 0, 0.3);
    }
    
    .user-message {
      border-bottom-right-radius: 0.125rem;
      align-self: flex-end;
      background: linear-gradient(to bottom right, #3b82f6, #2563eb);
      color: white;
    }
    
    .assistant-message {
      border-bottom-left-radius: 0.125rem;
      align-self: flex-start;
    }
    
    .dark .assistant-message {
      background-color: #1e293b;
      color: #e2e8f0;
      border-color: #334155;
    }
    
    /* Message bubble arrow */
    .user-message::after {
      content: '';
      position: absolute;
      bottom: -0.5rem;
      right: 0.125rem;
      width: 0.75rem;
      height: 0.75rem;
      background: #2563eb;
      clip-path: polygon(0 0, 100% 0, 100% 100%);
    }
    
    .assistant-message::after {
      content: '';
      position: absolute;
      bottom: -0.5rem;
      left: 0.125rem;
      width: 0.75rem;
      height: 0.75rem;
      background: #f8fafc;
      clip-path: polygon(0 0, 100% 0, 0 100%);
    }
    
    .dark .assistant-message::after {
      background: #1e293b;
    }
    
    /* Animations */
    @keyframes message-fade-in {
      from {
        opacity: 0;
        transform: translateY(10px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }
    
    @keyframes pulse-fade {
      0%, 100% {
        opacity: 0.5;
      }
      50% {
        opacity: 1;
      }
    }
    
    .typing-indicator {
      display: inline-flex;
      align-items: center;
      margin-left: 0.5rem;
    }
    
    .typing-dot {
      width: 0.5rem;
      height: 0.5rem;
      border-radius: 50%;
      background-color: currentColor;
      opacity: 0.7;
      margin: 0 0.1rem;
    }
    
    .typing-dot:nth-child(1) {
      animation: pulse-fade 1.2s 0s infinite;
    }
    
    .typing-dot:nth-child(2) {
      animation: pulse-fade 1.2s 0.2s infinite;
    }
    
    .typing-dot:nth-child(3) {
      animation: pulse-fade 1.2s 0.4s infinite;
    }
    
    /* Style spécifique pour le mode sombre */
    .dark body {
      background-color: #0f172a;
      color: #e2e8f0;
    }
    
    /* Tooltip custom */
    .tooltip {
      position: relative;
    }
    
    .tooltip .tooltip-text {
      visibility: hidden;
      width: max-content;
      max-width: 200px;
      background-color: #1e293b;
      color: #f8fafc;
      text-align: center;
      border-radius: 6px;
      padding: 0.375rem 0.625rem;
      position: absolute;
      z-index: 1;
      bottom: 125%;
      left: 50%;
      transform: translateX(-50%);
      opacity: 0;
      transition: opacity 0.3s, visibility 0.3s;
      font-size: 0.75rem;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      pointer-events: none;
    }
    
    .dark .tooltip .tooltip-text {
      background-color: #475569;
      color: #f1f5f9;
    }
    
    .tooltip .tooltip-text::after {
      content: "";
      position: absolute;
      top: 100%;
      left: 50%;
      margin-left: -5px;
      border-width: 5px;
      border-style: solid;
      border-color: #1e293b transparent transparent transparent;
    }
    
    .dark .tooltip .tooltip-text::after {
      border-color: #475569 transparent transparent transparent;
    }
    
    .tooltip:hover .tooltip-text {
      visibility: visible;
      opacity: 1;
    }
    
    /* Bouton de copie */
    .copy-btn {
      position: absolute;
      top: 0.5rem;
      right: 0.5rem;
      opacity: 0;
      transition: opacity 0.2s ease, background-color 0.2s ease;
      z-index: 2;
    }
    
    .message-bubble:hover .copy-btn {
      opacity: 1;
    }
    
    /* File preview */
    .file-preview {
      max-width: 300px;
      margin: 0.5rem auto;
      position: relative;
      overflow: hidden;
      border-radius: 0.5rem;
      transition: transform 0.2s ease;
    }
    
    .file-preview:hover {
      transform: scale(1.02);
    }
    
    .file-preview img {
      width: 100%;
      height: auto;
      display: block;
      object-fit: cover;
    }
    
    /* Chip style */
    .chip {
      display: inline-flex;
      align-items: center;
      background: #e0f2fe;
      border-radius: 9999px;
      padding: 0.25rem 0.75rem;
      font-size: 0.75rem;
      font-weight: 500;
      color: #0369a1;
      transition: all 0.2s ease;
    }
    
    .chip .chip-icon {
      margin-right: 0.25rem;
    }
    
    .chip .chip-close {
      margin-left: 0.25rem;
      cursor: pointer;
      opacity: 0.7;
      transition: opacity 0.2s ease;
    }
    
    .chip .chip-close:hover {
      opacity: 1;
    }
    
    .dark .chip {
      background: #0c4a6e;
      color: #7dd3fc;
    }
    
    /* Switch toggle style */
    .toggle-switch {
      position: relative;
      display: inline-block;
      width: 2.5rem;
      height: 1.25rem;
    }
    
    .toggle-switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .toggle-slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #cbd5e1;
      transition: .4s;
      border-radius: 1.25rem;
    }
    
    .toggle-slider:before {
      position: absolute;
      content: "";
      height: 0.875rem;
      width: 0.875rem;
      left: 0.25rem;
      bottom: 0.1875rem;
      background-color: white;
      transition: .4s;
      border-radius: 50%;
    }
    
    input:checked + .toggle-slider {
      background-color: #0ea5e9;
    }
    
    input:focus + .toggle-slider {
      box-shadow: 0 0 1px #0ea5e9;
    }
    
    input:checked + .toggle-slider:before {
      transform: translateX(1.125rem);
    }
    
    .dark .toggle-slider {
      background-color: #475569;
    }
    
    .dark input:checked + .toggle-slider {
      background-color: #38bdf8;
    }
    
    /* Input styling */
    .chat-input {
      transition: all 0.3s ease;
      border-color: #e2e8f0;
    }
    
    .chat-input:focus {
      border-color: #38bdf8;
      box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.2);
    }
    
    .dark .chat-input {
      background-color: #1e293b;
      color: #f1f5f9;
      border-color: #334155;
    }
    
    .dark .chat-input:focus {
      border-color: #38bdf8;
      box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.2);
    }
    
    /* Code block styling */
    pre {
      position: relative;
      background-color: #f8fafc;
      border-radius: 0.5rem;
      margin: 1rem 0;
      padding: 1.25rem 1rem;
      overflow-x: auto;
    }
    
    .dark pre {
      background-color: #1e293b;
      color: #e2e8f0;
    }
    
    code {
      font-family: 'JetBrains Mono', monospace;
      font-size: 0.875rem;
    }
    
    /* Code block copy button */
    .code-copy-btn {
      position: absolute;
      top: 0.5rem;
      right: 0.5rem;
      opacity: 0;
      transition: opacity 0.2s ease;
    }
    
    pre:hover .code-copy-btn {
      opacity: 0.7;
    }
    
    pre:hover .code-copy-btn:hover {
      opacity: 1;
    }
  </style>

</head>
<body class="bg-gray-50 text-gray-900 antialiased">
  <!-- Header -->
  <header class="bg-gradient-to-r from-primary-600 to-primary-800 text-white py-3 px-4 shadow-md sticky top-0 z-10">
    <div class="max-w-4xl mx-auto flex justify-between items-center">
      <!-- Logo & Title -->
      <div class="flex items-center space-x-2">
        <span class="text-2xl"></span>
        <h1 class="text-xl font-bold">Mariam AI</h1>
      </div>

<!-- Actions -->
  <div class="flex items-center space-x-2 sm:space-x-4">
    <!-- Thème sombre/clair -->
    <button id="theme-toggle" class="p-2 rounded-full hover:bg-primary-700/50 transition-colors duration-200 tooltip" aria-label="Changer de thème">
      <i class="fa-solid fa-moon dark:hidden"></i>
      <i class="fa-solid fa-sun hidden dark:inline"></i>
      <span class="tooltip-text">Mode clair/sombre</span>
    </button>
    
    <!-- Bouton d'effacement -->
    <form action="/clear" method="POST" id="clear-form">
      <button type="submit" class="flex items-center bg-red-500 hover:bg-red-600 text-white text-xs font-semibold py-1.5 px-3 rounded-full transition duration-200 focus:outline-none focus:ring-2 focus:ring-red-400 focus:ring-opacity-75 tooltip">
        <i class="fa-solid fa-trash-can mr-1.5"></i>
        <span class="hidden sm:inline">Effacer</span>
        <span class="tooltip-text">Effacer la conversation</span>
      </button>
    </form>
  </div>
</div>

  </header>

  <!-- Main Container -->

  <main class="max-w-4xl mx-auto chat-layout">
    <!-- Chat Messages Container -->
    <section id="chat-messages" class="flex flex-col space-y-6 p-4 overflow-y-auto">
      <!-- Message de chargement de l'historique -->
      <div id="history-loading" class="text-center py-10">
        <div class="inline-flex items-center px-4 py-2 bg-primary-50 text-primary-700 rounded-lg dark:bg-primary-900/30 dark:text-primary-300">
          <svg class="animate-spin h-5 w-5 mr-3" viewBox="0 0 24 24">
            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
          </svg>
          <span>Chargement de la conversation...</span>
        </div>
      </div>

<!-- Indicateur de chargement pour les réponses -->
  <div id="loading-indicator" class="flex items-start space-x-2 hidden">
    <div class="w-8 h-8 rounded-full bg-primary-100 flex items-center justify-center dark:bg-primary-900/50">
      <span class="text-lg"></span>
    </div>
    <div class="message-bubble assistant-message bg-secondary-50 text-secondary-900 border border-secondary-200 flex items-center">
      <span>Mariam réfléchit</span>
      <div class="typing-indicator">
        <span class="typing-dot"></span>
        <span class="typing-dot"></span>
        <span class="typing-dot"></span>
      </div>
    </div>
  </div>
</section>

<!-- Bottom Container -->
<div class="border-t border-gray-200 dark:border-gray-700">
  <!-- Error Message -->
  <div id="error-message" class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 dark:bg-red-900/30 dark:text-red-300 dark:border-red-600 hidden" role="alert">
    <div class="flex">
      <div class="flex-shrink-0">
        <i class="fa-solid fa-circle-exclamation"></i>
      </div>
      <div class="ml-3">
        <p class="text-sm font-medium" id="error-text">Le message d'erreur détaillé ira ici.</p>
      </div>
      <button class="ml-auto" id="dismiss-error">
        <i class="fa-solid fa-xmark"></i>
      </button>
    </div>
  </div>
  
  <!-- Preview Area -->
  <div id="preview-area" class="px-4 py-2 bg-gray-50 dark:bg-gray-800/50 hidden">
    <!-- File Preview -->
    <div id="file-preview" class="hidden"></div>
  </div>
  
  <!-- Options Bar -->
  <div class="flex items-center justify-between px-4 py-2 bg-gray-100 dark:bg-gray-800/80 text-sm text-gray-600 dark:text-gray-300">
    <!-- Options du chat -->
    <div class="flex items-center space-x-4">
      <!-- Web Search Toggle -->
      <label class="flex items-center cursor-pointer tooltip">
        <span class="mr-2 text-xs sm:text-sm font-medium">
          <i class="fa-solid fa-globe mr-1.5"></i>
          <span class="hidden sm:inline">Recherche Web</span>
        </span>
        <span class="toggle-switch">
          <input type="checkbox" id="web_search_toggle" name="web_search" value="true">
          <span class="toggle-slider"></span>
        </span>
        <span class="tooltip-text">Activer la recherche web pour Mariam</span>
      </label>
      
      <!-- Advanced Reasoning Toggle -->
      <label class="flex items-center cursor-pointer tooltip">
        <span class="mr-2 text-xs sm:text-sm font-medium text-accent-700 dark:text-accent-300">
          <i class="fa-solid fa-brain mr-1.5"></i>
          <span class="hidden sm:inline">Avancé</span>
          <span id="advanced-cooldown-timer" class="text-xs ml-1 hidden"></span>
        </span>
        <span class="toggle-switch">
          <input type="checkbox" id="advanced_reasoning_toggle" name="advanced_reasoning" value="true">
          <span class="toggle-slider"></span>
        </span>
        <span class="tooltip-text">Activer le raisonnement avancé (1 fois/min)</span>
      </label>
    </div>
    
    <!-- Upload Button -->
    <div>
      <label for="file_upload" class="cursor-pointer flex items-center text-primary-600 hover:text-primary-700 dark:text-primary-400 dark:hover:text-primary-300 tooltip">
        <i class="fa-solid fa-paperclip"></i>
        <span class="ml-1.5 hidden sm:inline">Fichier</span>
        <input type="file" id="file_upload" name="file" class="hidden" accept=".txt,.pdf,.png,.jpg,.jpeg">
        <span class="tooltip-text">Joindre un fichier (txt, pdf, image)</span>
      </label>
      
      <!-- File Chip (apparaît quand un fichier est sélectionné) -->
      <div id="file-chip" class="chip mt-2 hidden">
        <i class="fa-solid fa-file chip-icon"></i>
        <span id="file-name" class="truncate max-w-[120px]"></span>
        <i id="clear-file" class="fa-solid fa-xmark chip-close"></i>
      </div>
    </div>
  </div>
  
  <!-- Chat Input Form -->
  <form id="chat-form" class="p-3 sm:p-4 bg-white dark:bg-gray-900">
    <div class="relative">
      <input 
        type="text" 
        id="prompt" 
        name="prompt" 
        class="chat-input w-full pl-4 pr-12 py-3 rounded-full border focus:outline-none text-sm sm:text-base" 
        placeholder="Posez votre question à Mariam..." 
        autocomplete="off"
      >
      <button 
        type="submit" 
        id="send-button" 
        class="absolute right-2 top-1/2 transform -translate-y-1/2 bg-primary-500 hover:bg-primary-600 disabled:bg-primary-300 text-white rounded-full p-2 transition focus:outline-none focus:ring-2 focus:ring-primary-400"
        title="Envoyer le message"
      >
        <i class="fa-solid fa-paper-plane"></i>
      </button>
    </div>
    <div class="text-xs text-center mt-2 text-gray-400 dark:text-gray-500">
      Appuyez sur <kbd class="px-1.5 py-0.5 bg-gray-100 dark:bg-gray-800 rounded border border-gray-300 dark:border-gray-700">Entrée</kbd> pour envoyer • <kbd class="px-1.5 py-0.5 bg-gray-100 dark:bg-gray-800 rounded border border-gray-300 dark:border-gray-700">Shift+Entrée</kbd> pour une nouvelle ligne
    </div>
  </form>
</div>
IGNORE_WHEN_COPYING_START
content_copy
download
Use code with caution.
IGNORE_WHEN_COPYING_END
  </main>

  <!-- Scripts -->

  <script>
    document.addEventListener('DOMContentLoaded', () => {
      // Elements
      const chatForm = document.getElementById('chat-form');
      const promptInput = document.getElementById('prompt');
      const chatMessages = document.getElementById('chat-messages');
      const loadingIndicator = document.getElementById('loading-indicator');
      const historyLoadingIndicator = document.getElementById('history-loading');
      const errorMessageDiv = document.getElementById('error-message');
      const errorTextP = document.getElementById('error-text');
      const dismissErrorBtn = document.getElementById('dismiss-error');
      const webSearchToggle = document.getElementById('web_search_toggle');
      const fileUpload = document.getElementById('file_upload');
      const fileChip = document.getElementById('file-chip');
      const fileNameSpan = document.getElementById('file-name');
      const clearFileButton = document.getElementById('clear-file');
      const filePreview = document.getElementById('file-preview');
      const previewArea = document.getElementById('preview-area');
      const sendButton = document.getElementById('send-button');
      const clearForm = document.getElementById('clear-form');
      const advancedToggle = document.getElementById('advanced_reasoning_toggle');
      const advancedCooldownTimerSpan = document.getElementById('advanced-cooldown-timer');
      const themeToggleBtn = document.getElementById('theme-toggle');

      // API endpoints
      const API_CHAT_ENDPOINT = '/api/chat';
      const API_HISTORY_ENDPOINT = '/api/history';
      const CLEAR_ENDPOINT = '/clear';

      // Constantes
      const COOLDOWN_DURATION = 60 * 1000; // 60 secondes
      
      // Variables
      let advancedToggleCooldownEndTime = 0;
      let isComposing = false; // Pour la composition (pour les langues qui utilisent IME)
      
      // Thème
      function initializeTheme() {
        // Vérifier les préférences locales ou systèmes
        if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
          document.documentElement.classList.add('dark');
        } else {
          document.documentElement.classList.remove('dark');
        }
      }
      
      function toggleTheme() {
        if (document.documentElement.classList.contains('dark')) {
          document.documentElement.classList.remove('dark');
          localStorage.theme = 'light';
        } else {
          document.documentElement.classList.add('dark');
          localStorage.theme = 'dark';
        }
      }
      
      themeToggleBtn.addEventListener('click', toggleTheme);
      initializeTheme();
      
      // Fonction de défilement vers le bas
      function scrollToBottom(smooth = true) {
        setTimeout(() => {
          chatMessages.scrollTo({
            top: chatMessages.scrollHeight,
            behavior: smooth ? 'smooth' : 'auto'
          });
        }, 50);
      }

      // Gestion du chargement
      function showLoading(show) {
        if (show) {
          loadingIndicator.classList.remove('hidden');
          chatMessages.appendChild(loadingIndicator);
          scrollToBottom();
        } else {
          loadingIndicator.classList.add('hidden');
        }
        
        sendButton.disabled = show;
        promptInput.disabled = show;
        fileUpload.disabled = show;
        clearFileButton.disabled = show;
      }

      // Gestion des erreurs
      function displayError(message) {
        errorTextP.textContent = message || "Une erreur inconnue est survenue.";
        errorMessageDiv.classList.remove('hidden');
        errorMessageDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
      }
      
      // Fermer le message d'erreur
      dismissErrorBtn.addEventListener('click', () => {
        errorMessageDiv.classList.add('hidden');
      });

      // Ajouter un message au chat
      function addMessageToChat(role, content, isHtml = false) {
        errorMessageDiv.classList.add('hidden');
        
        const messageWrapper = document.createElement('div');
        messageWrapper.classList.add('flex', role === 'user' ? 'justify-end' : 'justify-start');
        
        let html = '';
        
        if (role === 'user') {
          // Message utilisateur
          html = `
            <div class="flex items-start space-x-2 max-w-[85%]">
              <div class="message-bubble user-message">
                <p class="text-sm sm:text-base whitespace-pre-wrap break-words">${escapeHtml(content)}</p>
              </div>
            </div>
          `;
        } else {
          // Message assistant
          html = `
            <div class="flex items-start space-x-2 max-w-[85%]">
              <div class="w-8 h-8 rounded-full bg-primary-100 flex items-center justify-center flex-shrink-0 dark:bg-primary-900/50">
                <span class="text-lg">✨</span>
              </div>
              <div class="message-bubble assistant-message bg-secondary-50 text-secondary-900 border border-secondary-200 relative">
                <div class="prose prose-sm sm:prose-base max-w-none dark:prose-invert">
                  ${isHtml ? content : escapeHtml(content)}
                </div>
                <button class="copy-btn text-xs bg-white/90 dark:bg-gray-800/90 hover:bg-gray-100 dark:hover:bg-gray-700 py-1 px-2 rounded text-gray-600 dark:text-gray-300 flex items-center">
                  <i class="fa-regular fa-copy mr-1"></i> Copier
                </button>
              </div>
            </div>
          `;
        }
        
        messageWrapper.innerHTML = html;
        chatMessages.insertBefore(messageWrapper, loadingIndicator);
        
        // Activer les boutons de copie pour les blocs de code
        const preTags = messageWrapper.querySelectorAll('pre');
        preTags.forEach(pre => {
          if (!pre.querySelector('.code-copy-btn')) {
            const codeBtn = document.createElement('button');
            codeBtn.className = 'code-copy-btn bg-white/90 dark:bg-gray-800/90 text-xs p-1 rounded text-gray-600 dark:text-gray-300';
            codeBtn.innerHTML = '<i class="fa-regular fa-copy"></i>';
            codeBtn.title = 'Copier le code';
            codeBtn.addEventListener('click', () => {
              const code = pre.querySelector('code')?.innerText || pre.innerText;
              navigator.clipboard.writeText(code)
                .then(() => {
                  codeBtn.innerHTML = '<i class="fa-solid fa-check"></i>';
                  setTimeout(() => {
                    codeBtn.innerHTML = '<i class="fa-regular fa-copy"></i>';
                  }, 2000);
                });
            });
            pre.appendChild(codeBtn);
          }
        });
        
        // Activer les boutons de copie pour les messages
        const copyBtn = messageWrapper.querySelector('.copy-btn');
        if (copyBtn) {
          copyBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            e.preventDefault();
            
            const textContent = messageWrapper.querySelector('.prose').innerText;
            navigator.clipboard.writeText(textContent)
              .then(() => {
                copyBtn.innerHTML = '<i class="fa-solid fa-check mr-1"></i> Copié';
                setTimeout(() => {
                  copyBtn.innerHTML = '<i class="fa-regular fa-copy mr-1"></i> Copier';
                }, 2000);
              });
          });
        }
        
        if (historyLoadingIndicator.parentNode !== chatMessages) {
          scrollToBottom();
        }
      }

      // Échappement HTML pour éviter les injections XSS
      function escapeHtml(unsafe) {
        return unsafe
          .replace(/&/g, "&amp;")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/"/g, "&quot;")
          .replace(/'/g, "&#039;");
      }

      // Gestion du cooldown du mode avancé
      function startAdvancedCooldownTimer() {
        advancedToggle.disabled = true;
        advancedToggleCooldownEndTime = Date.now() + COOLDOWN_DURATION;

        const updateTimer = () => {
          const now = Date.now();
          if (now >= advancedToggleCooldownEndTime) {
            clearInterval(intervalId);
            advancedCooldownTimerSpan.classList.add('hidden');
            advancedToggle.disabled = false;
            advancedToggleCooldownEndTime = 0;
          } else {
            const remainingSeconds = Math.ceil((advancedToggleCooldownEndTime - now) / 1000);
            advancedCooldownTimerSpan.textContent = `(${remainingSeconds}s)`;
            advancedCooldownTimerSpan.classList.remove('hidden');
          }
        };

        const intervalId = setInterval(updateTimer, 1000);
        updateTimer();
      }

      // Chargement de l'historique de chat
      async function loadChatHistory() {
        historyLoadingIndicator.style.display = 'block';
        try {
          const response = await fetch(API_HISTORY_ENDPOINT);
          if (!response.ok) {
            let errorMsg = `Erreur serveur (${response.status})`;
            try {
              const errData = await response.json();
              errorMsg = errData.error || errorMsg;
            } catch (e) {}
            throw new Error(errorMsg);
          }
          
          const data = await response.json();
          if (data.success && Array.isArray(data.history)) {
            // Vider le conteneur et préparer les nouvelles entrées
            chatMessages.innerHTML = '';
            chatMessages.appendChild(loadingIndicator);
            loadingIndicator.classList.add('hidden');
            
            if (data.history.length === 0) {
              // Message d'accueil
              addMessageToChat('assistant', "Bonjour ! Je suis Mariam, votre assistant IA. Comment puis-je vous aider aujourd'hui ?", true);
            } else {
              // Afficher l'historique
              data.history.forEach(message => {
                const isAssistantHtml = message.role === 'assistant';
                addMessageToChat(message.role, message.text, isAssistantHtml);
              });
            }
            
            scrollToBottom(false);
          } else {
            throw new Error(data.error || "Format de réponse de l'historique invalide.");
          }
        } catch (error) {
          chatMessages.innerHTML = '';
          chatMessages.appendChild(loadingIndicator);
          loadingIndicator.classList.add('hidden');
          displayError(`Impossible de charger l'historique: ${error.message}`);
        } finally {
          historyLoadingIndicator.remove();
          promptInput.focus();
        }
      }

      // Gestion des fichiers
      function clearFileInput() {
        fileUpload.value = '';
        fileChip.classList.add('hidden');
        filePreview.classList.add('hidden');
        previewArea.classList.add('hidden');
        filePreview.innerHTML = '';
      }

      fileUpload.addEventListener('change', () => {
        if (fileUpload.files.length > 0) {
          const file = fileUpload.files[0];
          const name = file.name;
          
          // Mettre à jour le chip de fichier
          fileNameSpan.textContent = name;
          fileNameSpan.title = name;
          fileChip.classList.remove('hidden');
          
          // Si c'est une image, créer une prévisualisation
          if (file.type.startsWith('image/')) {
            const reader = new FileReader();
            reader.onload = (e) => {
              filePreview.innerHTML = `
                <div class="file-preview">
                  <img src="${e.target.result}" alt="Prévisualisation de l'image">
                </div>
              `;
              filePreview.classList.remove('hidden');
              previewArea.classList.remove('hidden');
            };
            reader.readAsDataURL(file);
          } else {
            // Afficher une icône pour les autres types de fichiers
            filePreview.innerHTML = `
              <div class="flex items-center justify-center py-4">
                <div class="bg-gray-100 dark:bg-gray-800 p-4 rounded-lg text-center">
                  <i class="fa-solid ${getFileIcon(file.type)} text-3xl text-gray-600 dark:text-gray-300 mb-2"></i>
                  <p class="text-sm text-gray-500 dark:text-gray-400">${formatFileSize(file.size)}</p>
                </div>
              </div>
            `;
            filePreview.classList.remove('hidden');
            previewArea.classList.remove('hidden');
          }
        } else {
          clearFileInput();
        }
      });

      // Obtenir l'icône en fonction du type de fichier
      function getFileIcon(fileType) {
        if (fileType.includes('pdf')) return 'fa-file-pdf';
        if (fileType.includes('text')) return 'fa-file-lines';
        return 'fa-file';
      }
      
      // Formater la taille du fichier
      function formatFileSize(bytes) {
        if (bytes < 1024) return bytes + ' octets';
        if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' Ko';
        return (bytes / 1048576).toFixed(1) + ' Mo';
      }

      clearFileButton.addEventListener('click', () => {
        clearFileInput();
      });

      // Soumission du formulaire
      chatForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        const prompt = promptInput.value.trim();
        const file = fileUpload.files[0];
        const useWebSearch = webSearchToggle.checked;
        const useAdvanced = advancedToggle.checked;

        if (!prompt && !file) {
          displayError("Veuillez entrer un message ou sélectionner un fichier.");
          promptInput.focus();
          return;
        }
        
        errorMessageDiv.classList.add('hidden');

        // Vérification du cooldown pour le mode avancé
        if (useAdvanced) {
          const now = Date.now();
          if (now < advancedToggleCooldownEndTime) {
            const remainingSeconds = Math.ceil((advancedToggleCooldownEndTime - now) / 1000);
            displayError(`Le raisonnement avancé est disponible dans ${remainingSeconds} seconde(s).`);
            return;
          }
        }

        // Construire le message utilisateur
        let userMessageText = prompt;
        if (file && file.name) {
          userMessageText = prompt ? `${prompt}` : `[Fichier joint: ${file.name}]`;
        }
        
        addMessageToChat('user', userMessageText);
        
        // Préparer les données
        const formData = new FormData();
        formData.append('prompt', prompt);
        formData.append('web_search', useWebSearch);
        
        if (file) {
          formData.append('file', file);
        }
        
        formData.append('advanced_reasoning', useAdvanced);

        // Afficher le chargement et nettoyer le formulaire
        showLoading(true);
        promptInput.value = '';
        clearFileInput();

        // Réinitialiser les options
        advancedToggle.checked = false;
        if (useAdvanced) startAdvancedCooldownTimer();

        try {
          const response = await fetch(API_CHAT_ENDPOINT, {
            method: 'POST',
            body: formData,
          });
          
          const data = await response.json();
          
          if (!response.ok) {
            throw new Error(data.error || `Erreur serveur: ${response.status}`);
          }
          
          if (data.success && data.message) {
            addMessageToChat('assistant', data.message, true);
          } else {
            throw new Error(data.error || "Réponse invalide ou vide du serveur.");
          }
        } catch (error) {
          displayError(error.message);
        } finally {
          showLoading(false);
          promptInput.focus();
        }
      });

      // Effacement de la conversation
      clearForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        
        // Demander confirmation
        const confirmDialog = document.createElement('div');
        confirmDialog.className = 'fixed inset-0 bg-black/50 flex items-center justify-center z-50';
        confirmDialog.innerHTML = `
          <div class="bg-white dark:bg-gray-800 p-6 rounded-lg shadow-xl max-w-sm mx-4 animate-[message-fade-in_0.2s_ease-out]">
            <h3 class="text-lg font-semibold mb-3 text-gray-900 dark:text-gray-100">Confirmer l'effacement</h3>
            <p class="text-gray-600 dark:text-gray-300 mb-4">Êtes-vous sûr de vouloir effacer toute la conversation ?</p>
            <div class="flex justify-end space-x-3">
              <button id="cancel-clear" class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors">
                Annuler
              </button>
              <button id="confirm-clear" class="px-4 py-2 text-sm font-medium text-white bg-red-500 rounded hover:bg-red-600 transition-colors">
                Effacer
              </button>
            </div>
          </div>
        `;
        
        document.body.appendChild(confirmDialog);
        document.body.classList.add('overflow-hidden');
        
        // Gérer les boutons
        document.getElementById('cancel-clear').addEventListener('click', () => {
          document.body.removeChild(confirmDialog);
          document.body.classList.remove('overflow-hidden');
        });
        
        document.getElementById('confirm-clear').addEventListener('click', async () => {
          document.body.removeChild(confirmDialog);
          document.body.classList.remove('overflow-hidden');
          
          const originalButtonText = e.target.querySelector('button').innerHTML;
          e.target.querySelector('button').innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i>';
          e.target.querySelector('button').disabled = true;
          
          try {
            const response = await fetch(CLEAR_ENDPOINT, {
              method: 'POST',
              headers: {
                'X-Requested-With': 'XMLHttpRequest'
              }
            });
            
            const data = await response.json();
            
            if (response.ok && data.success) {
              chatMessages.innerHTML = '';
              chatMessages.appendChild(loadingIndicator);
              loadingIndicator.classList.add('hidden');
              
              addMessageToChat('assistant', "Conversation effacée. Comment puis-je vous aider ?", true);
              errorMessageDiv.classList.add('hidden');
            } else {
              throw new Error(data.error || "Impossible d'effacer côté serveur.");
            }
          } catch (error) {
            displayError(`Erreur lors de l'effacement du chat: ${error.message}`);
          } finally {
            e.target.querySelector('button').innerHTML = originalButtonText;
            e.target.querySelector('button').disabled = false;
            promptInput.focus();
          }
        });
      });

      // Raccourcis clavier
      promptInput.addEventListener('keydown', (e) => {
        // Ignorer pendant la composition IME
        if (isComposing) return;
        
        // Shift+Enter pour nouvelle ligne, Enter pour envoyer
        if (e.key === 'Enter' && !e.shiftKey) {
          e.preventDefault();
          if (!sendButton.disabled) {
            chatForm.dispatchEvent(new Event('submit'));
          }
        }
      });
      
      // Support IME pour les langues asiatiques
      promptInput.addEventListener('compositionstart', () => {
        isComposing = true;
      });
      
      promptInput.addEventListener('compositionend', () => {
        isComposing = false;
      });

      // Démarrer le chargement
      loadChatHistory();
      
      // Activer l'ajustement automatique de la hauteur du textarea
      const resizeInput = () => {
        // Pour une implémentation future si on change l'input en textarea
      };
      
      // Vérifier les préréglages du navigateur pour le thème sombre
      const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
      prefersDarkScheme.addEventListener('change', (e) => {
        if (!localStorage.theme) { // Seulement si l'utilisateur n'a pas explicitement choisi
          if (e.matches) {
            document.documentElement.classList.add('dark');
          } else {
            document.documentElement.classList.remove('dark');
          }
        }
      });
    });
  </script>

</body>
</html>