File size: 106,137 Bytes
cc02825
 
 
 
 
4ee4b3d
cc02825
af6a5a0
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
cc02825
4ee4b3d
 
cc02825
4ee4b3d
 
 
cc02825
4ee4b3d
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
cc02825
4ee4b3d
 
 
 
cc02825
afa9f64
af6a5a0
 
 
 
 
 
 
 
 
 
 
afa9f64
 
 
 
 
 
 
 
 
af6a5a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
cc02825
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
 
4ee4b3d
 
 
 
 
 
9e4f513
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
9e4f513
4ee4b3d
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
cc02825
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
 
4ee4b3d
 
 
 
cc02825
 
4ee4b3d
 
 
cc02825
 
 
4ee4b3d
 
 
 
 
 
 
8de485b
4ee4b3d
cc02825
9e4f513
4ee4b3d
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
e6a7e66
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
4ee4b3d
 
 
 
 
9e4f513
4ee4b3d
 
9e4f513
4ee4b3d
 
 
9e4f513
 
 
 
 
 
ef12d89
c271e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
ef12d89
 
 
 
 
 
 
 
 
 
 
 
9e4f513
ef12d89
af6a5a0
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
e6a7e66
cc02825
4ee4b3d
 
cc02825
4ee4b3d
 
 
 
cc02825
 
4ee4b3d
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
 
 
9e4f513
4ee4b3d
9e4f513
4ee4b3d
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
e6a7e66
cc02825
4ee4b3d
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
6a52ec8
4ee4b3d
 
 
 
 
9e4f513
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
803064a
4ee4b3d
 
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
c271e98
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
e6a7e66
 
4ee4b3d
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
4ee4b3d
cc02825
4ee4b3d
 
 
 
9e4f513
 
4ee4b3d
 
 
 
9e4f513
4ee4b3d
 
cc02825
4ee4b3d
 
 
e6a7e66
 
9e4f513
4ee4b3d
 
 
 
9e4f513
 
 
 
 
 
 
4ee4b3d
 
 
 
9e4f513
 
4ee4b3d
 
 
 
 
 
 
 
 
e6a7e66
 
4ee4b3d
 
 
 
 
 
e6a7e66
4ee4b3d
 
 
 
 
 
af6a5a0
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
afa9f64
4ee4b3d
 
 
 
 
 
 
8de485b
4ee4b3d
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
4ee4b3d
 
c271e98
4ee4b3d
 
 
 
 
 
 
 
 
e6a7e66
 
4ee4b3d
 
 
 
 
 
 
c271e98
 
 
 
 
 
 
4ee4b3d
c271e98
 
 
 
 
4ee4b3d
 
c271e98
4ee4b3d
9e4f513
c271e98
 
 
4ee4b3d
 
 
9e4f513
 
4ee4b3d
 
 
 
9e4f513
4ee4b3d
 
c271e98
 
 
9e4f513
4ee4b3d
 
c271e98
4ee4b3d
c271e98
8de485b
4ee4b3d
 
 
 
 
 
 
 
 
 
 
9fc50c0
7fffccc
9fc50c0
 
 
 
 
 
4ee4b3d
 
9d55857
9e4f513
 
 
4ee4b3d
 
9fc50c0
8de485b
 
9e4f513
 
 
e6a7e66
9e4f513
 
 
 
 
 
4ee4b3d
 
 
 
e6a7e66
 
9e4f513
c271e98
4ee4b3d
 
 
 
9e4f513
 
 
4ee4b3d
 
9e4f513
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb7057c
4ee4b3d
 
 
9fc50c0
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
9fc50c0
4ee4b3d
 
 
 
9fc50c0
4ee4b3d
9e4f513
 
 
4ee4b3d
 
9e4f513
 
9fc50c0
4ee4b3d
 
cc02825
4ee4b3d
 
 
cc02825
4ee4b3d
9fc50c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
1e47070
4ee4b3d
 
 
 
 
cc02825
4ee4b3d
 
 
e6a7e66
b1c9fe4
4ee4b3d
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af6a5a0
 
4ee4b3d
 
 
 
 
 
 
 
 
 
e6a7e66
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
9d55857
4ee4b3d
 
 
 
 
e6a7e66
4ee4b3d
 
 
 
 
 
 
 
e6a7e66
 
4ee4b3d
 
 
 
 
 
e6a7e66
cc02825
4ee4b3d
 
 
 
c271e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
cc02825
4ee4b3d
 
 
 
9e4f513
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
9e4f513
 
 
 
4ee4b3d
e6a7e66
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
ef12d89
9e4f513
 
 
 
 
 
 
ef12d89
 
9e4f513
 
 
 
 
ef12d89
 
 
 
9e4f513
 
 
ef12d89
 
 
 
 
 
 
 
 
 
 
 
9e4f513
ef12d89
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef12d89
 
9e4f513
 
 
ef12d89
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
9e4f513
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
ef12d89
 
 
 
 
9e4f513
ef12d89
9e4f513
 
 
ef12d89
9e4f513
 
ef12d89
9e4f513
 
 
ef12d89
9e4f513
 
ef12d89
9e4f513
ef12d89
9e4f513
 
 
 
 
 
ef12d89
9e4f513
 
4ee4b3d
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef12d89
9e4f513
 
 
 
 
ef12d89
 
 
9e4f513
 
 
ef12d89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
9e4f513
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fc50c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
cc02825
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4f513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee4b3d
 
 
 
 
eb48c03
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb48c03
4ee4b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb48c03
 
cc02825
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NexusChat - Advanced AI Client</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/github.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#f0f9ff',
                            100: '#e0f2fe',
                            200: '#bae6fd',
                            300: '#7dd3fc',
                            400: '#38bdf8',
                            500: '#0ea5e9',
                            600: '#0284c7',
                            700: '#0369a1',
                            800: '#075985',
                            900: '#0c4a6e',
                        },
                        dark: {
                            800: '#1e293b',
                            900: '#0f172a',
                        }
                    }
                }
            }
        }
    </script>
    <style>
        /* Custom scrollbar */
        ::-webkit-scrollbar {
            width: 6px;
        }
        ::-webkit-scrollbar-track {
            background: #f1f1f1;
        }
        ::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 3px;
        }
        ::-webkit-scrollbar-thumb:hover {
            background: #555;
        }

        /* Dark mode scrollbar */
        .dark ::-webkit-scrollbar-track {
            background: #1e293b;
        }
        .dark ::-webkit-scrollbar-thumb {
            background: #64748b;
        }

        /* Chat message typing animation */
        @keyframes typing {
            from { width: 0 }
            to { width: 100% }
        }

        .typing-animation {
            overflow: hidden;
            white-space: nowrap;
            animation: typing 2s steps(40, end);
        }

        /* Smooth transitions */
        .transition-all {
            transition: all 0.3s ease;
        }

        /* Markdown styling */
        .prose pre {
            background-color: rgba(0,0,0,0.05);
            padding: 1em;
            border-radius: 0.5em;
            overflow-x: auto;
            margin: 1em 0;
        }
        .dark .prose pre {
            background-color: rgba(255,255,255,0.05);
        }
        .prose code {
            background-color: rgba(0,0,0,0.1);
            padding: 0.2em 0.4em;
            border-radius: 0.25em;
            font-family: monospace;
        }
        .dark .prose code {
            background-color: rgba(255,255,255,0.1);
        }
        .prose ul, .prose ol {
            padding-left: 1.5em;
            margin: 0.5em 0;
        }
        .prose li {
            margin: 0.25em 0;
        }
        .prose table {
            border-collapse: collapse;
            width: 100%;
            margin: 1em 0;
        }
        .prose th, .prose td {
            border: 1px solid rgba(0,0,0,0.1);
            padding: 0.5em;
        }
        .dark .prose th, .dark .prose td {
            border-color: rgba(255,255,255,0.1);
        }
    </style>
</head>
<body class="bg-gray-50 dark:bg-dark-900 text-gray-800 dark:text-gray-200 min-h-screen">
    <div class="flex h-screen overflow-hidden">
        <!-- Sidebar -->
        <div class="w-64 bg-white dark:bg-dark-800 border-r border-gray-200 dark:border-gray-700 flex flex-col">
            <div class="p-4 border-b border-gray-200 dark:border-gray-700">
                <button id="newChatBtn" class="w-full flex items-center justify-center gap-2 bg-primary-500 hover:bg-primary-600 text-white py-2 px-4 rounded-lg transition-all">
                    <i class="fas fa-plus"></i>
                    <span>New Chat</span>
                </button>
            </div>
            
            <div class="flex-1 overflow-y-auto p-2" id="chatList">
                <!-- Chat items will be added here dynamically -->
            </div>
            
            <div class="p-4 border-t border-gray-200 dark:border-gray-700">
                <div class="flex items-center gap-3 mb-4 cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 p-2 rounded-lg transition-all">
                    <div class="w-8 h-8 rounded-full bg-primary-500 flex items-center justify-center text-white">
                        <i class="fas fa-user"></i>
                    </div>
                    <span class="font-medium">User Account</span>
                </div>
                
                <div id="settingsBtn" class="flex items-center gap-3 cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 p-2 rounded-lg transition-all">
                    <div class="w-8 h-8 rounded-full bg-gray-200 dark:bg-gray-600 flex items-center justify-center">
                        <i class="fas fa-cog"></i>
                    </div>
                    <span>Settings</span>
                </div>
            </div>
        </div>
        
        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden">
            <!-- Header -->
            <header class="bg-white dark:bg-dark-800 border-b border-gray-200 dark:border-gray-700 p-4 flex items-center justify-between">
                <div class="flex items-center gap-2">
                    <button id="sidebarToggle" class="md:hidden text-gray-500 dark:text-gray-400">
                        <i class="fas fa-bars text-xl"></i>
                    </button>
                    <h1 class="text-xl font-bold">NexusChat</h1>
                </div>
                
                <div class="flex items-center gap-4">
                    <div class="relative">
                        <button id="modelDropdownBtn" class="flex items-center gap-2 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 py-2 px-3 rounded-lg transition-all">
                            <span id="currentModel">GPT-4</span>
                            <i class="fas fa-chevron-down text-xs"></i>
                        </button>
                    </div>
                    <div class="relative">
                        <button id="chatSettingsBtn" class="flex items-center gap-2 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 py-2 px-3 rounded-lg transition-all">
                            <i class="fas fa-cog"></i>
                        </button>
                        
                        <div id="modelDropdown" class="hidden absolute right-0 mt-2 w-48 bg-white dark:bg-dark-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-10">
                            <div class="p-2">
                                <div class="text-xs uppercase text-gray-500 dark:text-gray-400 px-2 py-1">Default Models</div>
                                <div class="model-option cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded" data-model="gpt-3.5">GPT-3.5</div>
                                <div class="model-option cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded" data-model="gpt-4">GPT-4</div>
                                
                                <div class="text-xs uppercase text-gray-500 dark:text-gray-400 px-2 py-1 mt-2">Custom Models</div>
                                <div id="customModelsList">
                                    <!-- Custom models will be added here -->
                                </div>
                                
                                <div class="border-t border-gray-200 dark:border-gray-700 mt-2 pt-2">
                                    <div id="addModelBtn" class="cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded flex items-center gap-2 text-primary-500">
                                        <i class="fas fa-plus"></i>
                                        <span>Add Custom Model</span>
                                    </div>
                                </div>

                                <div class="text-xs uppercase text-gray-500 dark:text-gray-400 px-2 py-1 mt-2">Custom Providers</div>
                                <div id="customProvidersList">
                                    <!-- Custom providers will be added here -->
                                </div>
                                
                                <div class="border-t border-gray-200 dark:border-gray-700 mt-2 pt-2">
                                    <div id="addProviderBtn" class="cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded flex items-center gap-2 text-primary-500">
                                        <i class="fas fa-plus"></i>
                                        <span>Add Custom Provider</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <button id="darkModeToggle" class="text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200">
                        <i class="fas fa-moon dark:hidden"></i>
                        <i class="fas fa-sun hidden dark:block"></i>
                    </button>
                </div>
            </header>
            
            <!-- Chat Area -->
            <div class="flex-1 overflow-y-auto p-4 bg-gray-50 dark:bg-dark-900" id="chatArea">
                <div class="max-w-3xl mx-auto">
                    <!-- Welcome message - shown when no chat or empty chat -->
                    <div id="welcomeMessage" class="text-center py-10">
                        <div class="w-16 h-16 mx-auto mb-4 bg-primary-500 rounded-full flex items-center justify-center text-white">
                            <i class="fas fa-robot text-2xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold mb-2">Welcome to NexusChat</h2>
                        <p class="text-gray-600 dark:text-gray-400 mb-6">Start a new conversation or select one from your history</p>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4 max-w-2xl mx-auto mb-8">
                            <div class="p-4 bg-white dark:bg-gray-800 rounded-lg shadow-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700 transition-all example-prompt">
                                <h3 class="font-medium mb-1">Explain quantum computing</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400">In simple terms</p>
                            </div>
                            <div class="p-4 bg-white dark:bg-gray-800 rounded-lg shadow-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700 transition-all example-prompt">
                                <h3 class="font-medium mb-1">Write a poem</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400">About artificial intelligence</p>
                            </div>
                            <div class="p-4 bg-white dark:bg-gray-800 rounded-lg shadow-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700 transition-all example-prompt">
                                <h3 class="font-medium mb-1">Plan a trip</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400">To Japan for 2 weeks</p>
                            </div>
                            <div class="p-4 bg-white dark:bg-gray-800 rounded-lg shadow-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700 transition-all example-prompt">
                                <h3 class="font-medium mb-1">Help me debug</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400">This JavaScript code</p>
                            </div>
                        </div>
                        
                        <button id="quickStartBtn" class="bg-primary-500 hover:bg-primary-600 text-white py-2 px-6 rounded-lg transition-all">
                            Quick Start
                        </button>
                    </div>
                    
                    <!-- Chat messages will be added here dynamically -->
                </div>
            </div>
            
            <!-- Input Area -->
            <div class="p-4 border-t border-gray-200 dark:border-gray-700 bg-white dark:bg-dark-800">
                <div class="max-w-3xl mx-auto">
                    <form id="messageForm" class="relative">
                        <textarea id="messageInput" rows="1" class="w-full p-4 pr-16 bg-gray-100 dark:bg-gray-700 rounded-lg resize-none focus:outline-none focus:ring-2 focus:ring-primary-500" placeholder="Type your message here..."></textarea>
                        <button type="submit" class="absolute right-4 bottom-4 w-8 h-8 bg-primary-500 hover:bg-primary-600 text-white rounded-full flex items-center justify-center transition-all">
                            <i class="fas fa-paper-plane"></i>
                        </button>
                    </form>
                    
                    <div class="text-xs text-gray-500 dark:text-gray-400 mt-2 text-center">
                        NexusChat may produce inaccurate information about people, places, or facts.
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Add Model Modal -->
    <div id="addModelModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white dark:bg-dark-800 rounded-lg shadow-xl w-full max-w-md p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-bold">Add Custom Model</h3>
                <button id="closeModelModal" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form id="addModelForm">
                <div class="mb-4">
                    <label for="modelName" class="block text-sm font-medium mb-1">Model Name</label>
                    <input type="text" id="modelName" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="e.g. My Custom Model" required>
                </div>
                
                <div class="mb-4">
                    <label for="modelEndpoint" class="block text-sm font-medium mb-1">API Endpoint</label>
                    <input type="text" id="modelEndpoint" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="https://api.example.com/v1/chat" required>
                </div>
                
                <div class="mb-4">
                    <label for="modelKey" class="block text-sm font-medium mb-1">API Key (optional)</label>
                    <input type="password" id="modelKey" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="sk-...">
                </div>

                <div class="mb-4 flex items-center">
                    <input type="checkbox" id="modelStreaming" class="w-4 h-4 text-primary-600 rounded border-gray-300 focus:ring-primary-500 dark:border-gray-600 dark:bg-gray-700">
                    <label for="modelStreaming" class="ml-2 text-sm font-medium text-gray-700 dark:text-gray-300">Supports Streaming</label>
                </div>
                <input type="hidden" id="editModelId" value="">
                
                <input type="hidden" id="editProviderId" value="">
                <div class="flex justify-end gap-2">
                    <button type="button" id="cancelAddModel" class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-all">
                        Cancel
                    </button>
                    <button type="submit" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded transition-all">
                        Add Model
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    <!-- API Key Modal -->
    <div id="apiKeyModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white dark:bg-dark-800 rounded-lg shadow-xl w-full max-w-md p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-bold">OpenAI API Key Required</h3>
                <button id="closeApiKeyModal" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <p class="mb-4">To use OpenAI models, please enter your API key:</p>
            
            <form id="apiKeyForm">
                <div class="mb-4">
                    <input type="password" id="apiKeyInput" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="sk-..." required>
                </div>
                
                <div class="flex justify-end gap-2">
                    <button type="button" id="cancelApiKey" class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-all">
                        Cancel
                    </button>
                    <button type="submit" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded transition-all">
                        Save Key
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Settings Page -->
    <div id="settingsPage" class="hidden fixed inset-0 bg-gray-50 dark:bg-dark-900 z-40 flex flex-col">
        <div class="max-w-3xl mx-auto w-full p-6 overflow-y-auto">
            <div class="sticky top-0 bg-gray-50 dark:bg-dark-900 pt-6 pb-4 z-10">
                <div class="flex items-center gap-4 mb-2">
                    <button id="backToChatBtn" class="text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200">
                        <i class="fas fa-arrow-left"></i>
                    </button>
                    <h2 class="text-2xl font-bold">Settings</h2>
                </div>
            </div>

            <div class="bg-white dark:bg-dark-800 rounded-lg shadow-sm p-6 mb-6">
                <h3 class="text-lg font-semibold mb-4">API Configuration</h3>
                
                <form id="apiKeySettingsForm">
                    <div class="mb-4">
                        <label for="providerUrl" class="block text-sm font-medium mb-1">Custom Provider URL</label>
                        <input type="text" id="providerUrl" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="https://example.com/v1" value="">
                        <p class="text-xs text-gray-500 dark:text-gray-400 mt-1">Base URL for your custom provider</p>
                    </div>
                    <div class="mb-4">
                        <label for="settingsApiKey" class="block text-sm font-medium mb-1">OpenAI API Key</label>
                        <div class="relative">
                            <input type="password" id="settingsApiKey" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="sk-..." value="">
                            <button type="button" id="toggleApiKeyVisibility" class="absolute right-2 top-2 text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                                <i class="fas fa-eye"></i>
                            </button>
                        </div>
                        <p class="text-xs text-gray-500 dark:text-gray-400 mt-1">Your API key is stored locally in your browser</p>
                    </div>
                    
                    <button type="submit" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded transition-all">
                        Save API Key
                    </button>
                </form>
            </div>

            <div class="bg-white dark:bg-dark-800 rounded-lg shadow-sm p-6 mb-6">
                <h3 class="text-lg font-semibold mb-4">Appearance</h3>
                
                <div class="flex items-center justify-between mb-2">
                    <span>Dark Mode</span>
                    <label class="relative inline-flex items-center cursor-pointer">
                        <input type="checkbox" id="darkModeToggleSetting" class="sr-only peer">
                        <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary-300 dark:peer-focus:ring-primary-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary-500"></div>
                    </label>
                </div>
            </div>

            <div class="bg-white dark:bg-dark-800 rounded-lg shadow-sm p-6 mb-6">
                <h3 class="text-lg font-semibold mb-4">Chat Defaults</h3>
                
                <form id="defaultChatSettingsForm">
                    <div class="mb-4">
                        <label for="defaultTemperature" class="block text-sm font-medium mb-1">Default Temperature (0-1)</label>
                        <input type="number" id="defaultTemperature" min="0" max="1" step="0.1" 
                               class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" 
                               placeholder="0.7" required>
                    </div>
                    
                    <div class="mb-4">
                        <label for="defaultHistoryCount" class="block text-sm font-medium mb-1">Default Message History Count</label>
                        <input type="number" id="defaultHistoryCount" min="1" max="20" 
                               class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" 
                               placeholder="5" required>
                    </div>
                    
                    <div class="mb-4">
                        <label for="defaultSystemMessage" class="block text-sm font-medium mb-1">Default System Message</label>
                        <textarea id="defaultSystemMessage" rows="3" 
                                  class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" 
                                  placeholder="You are a helpful assistant..."></textarea>
                    </div>
                    
                    <button type="submit" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded transition-all">
                        Save Defaults
                    </button>
                </form>
            </div>

            <div class="bg-white dark:bg-dark-800 rounded-lg shadow-sm p-6">
                <h3 class="text-lg font-semibold mb-4">About</h3>
                <p class="text-sm text-gray-600 dark:text-gray-300 mb-2">NexusChat v1.0.0</p>
                <p class="text-sm text-gray-600 dark:text-gray-300">An advanced AI client for modern workflows</p>
            </div>
        </div>
    </div>

    <!-- Add Provider Modal -->
    <div id="addProviderModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white dark:bg-dark-800 rounded-lg shadow-xl w-full max-w-md p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-bold">Add Custom Provider</h3>
                <button id="closeProviderModal" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form id="addProviderForm">
                <div class="mb-4">
                    <label for="providerName" class="block text-sm font-medium mb-1">Provider Name</label>
                    <input type="text" id="providerName" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="e.g. My AI Provider" required>
                </div>
                
                <div class="mb-4">
                    <label for="providerBaseUrl" class="block text-sm font-medium mb-1">Base URL</label>
                    <input type="text" id="providerBaseUrl" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="https://api.example.com/v1" required>
                </div>

                <div class="mb-4">
                    <label for="providerApiKey" class="block text-sm font-medium mb-1">API Key (optional)</label>
                    <input type="password" id="providerApiKey" class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" placeholder="sk-...">
                </div>

                <div class="mb-4 flex items-center">
                    <input type="checkbox" id="providerStreaming" class="w-4 h-4 text-primary-600 rounded border-gray-300 focus:ring-primary-500 dark:border-gray-600 dark:bg-gray-700">
                    <label for="providerStreaming" class="ml-2 text-sm font-medium text-gray-700 dark:text-gray-300">Supports Streaming</label>
                </div>
                
                <div class="flex justify-end gap-2">
                    <button type="button" id="cancelAddProvider" class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-all">
                        Cancel
                    </button>
                    <button type="submit" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded transition-all">
                        Add Provider
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Provider Models Modal -->
    <div id="providerModelsModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white dark:bg-dark-800 rounded-lg shadow-xl w-full max-w-md p-6 max-h-[80vh] overflow-y-auto">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-bold" id="providerModelsTitle">Provider Models</h3>
                <button id="closeProviderModelsModal" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <div id="providerModelsList" class="space-y-2">
                <!-- Models will be loaded here -->
            </div>
            
            <div class="flex justify-end mt-4">
                <button type="button" id="cancelProviderModels" class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-all">
                    Close
                </button>
            </div>
        </div>
    </div>

    <!-- Chat Settings Modal -->
    <div id="chatSettingsModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white dark:bg-dark-800 rounded-lg shadow-xl w-full max-w-md p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-bold">Chat Settings</h3>
                <button id="closeChatSettingsModal" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form id="chatSettingsForm">
                <div class="mb-4">
                    <label for="chatTemperature" class="block text-sm font-medium mb-1">Temperature (0-1)</label>
                    <input type="number" id="chatTemperature" min="0" max="1" step="0.1" 
                           class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" 
                           placeholder="0.7" required>
                </div>
                
                <div class="mb-4">
                    <label for="chatHistoryCount" class="block text-sm font-medium mb-1">Message History Count</label>
                    <input type="number" id="chatHistoryCount" min="1" max="20" 
                           class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" 
                           placeholder="5" required>
                </div>
                
                <div class="mb-4">
                    <label for="chatSystemMessage" class="block text-sm font-medium mb-1">System Message</label>
                    <textarea id="chatSystemMessage" rows="3" 
                              class="w-full p-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:ring-primary-500 focus:border-primary-500" 
                              placeholder="You are a helpful assistant..."></textarea>
                </div>
                
                <div class="flex justify-end gap-2">
                    <button type="button" id="cancelChatSettings" class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-all">
                        Cancel
                    </button>
                    <button type="submit" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded transition-all">
                        Save
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Delete Chat Confirmation Modal -->
    <div id="deleteChatModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white dark:bg-dark-800 rounded-lg shadow-xl w-full max-w-md p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-bold">Delete Chat</h3>
                <button id="closeDeleteModal" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <p class="mb-6">Are you sure you want to delete this chat? This action cannot be undone.</p>
            
            <div class="flex justify-end gap-2">
                <button type="button" id="cancelDelete" class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-all">
                    Cancel
                </button>
                <button type="button" id="confirmDelete" class="px-4 py-2 bg-red-500 hover:bg-red-600 text-white rounded transition-all">
                    Delete
                </button>
            </div>
        </div>
    </div>

    <script>
        // DOM Elements
        const apiKeyModal = document.getElementById('apiKeyModal');
        const closeApiKeyModal = document.getElementById('closeApiKeyModal');
        const cancelApiKey = document.getElementById('cancelApiKey');
        const apiKeyForm = document.getElementById('apiKeyForm');
        const apiKeyInput = document.getElementById('apiKeyInput');
        const sidebarToggle = document.getElementById('sidebarToggle');
        const darkModeToggle = document.getElementById('darkModeToggle');
        const newChatBtn = document.getElementById('newChatBtn');
        const chatList = document.getElementById('chatList');
        const chatArea = document.getElementById('chatArea');
        const welcomeMessage = document.getElementById('welcomeMessage');
        const messageForm = document.getElementById('messageForm');
        const messageInput = document.getElementById('messageInput');
        const quickStartBtn = document.getElementById('quickStartBtn');
        const modelDropdownBtn = document.getElementById('modelDropdownBtn');
        const modelDropdown = document.getElementById('modelDropdown');
        const currentModel = document.getElementById('currentModel');
        const customModelsList = document.getElementById('customModelsList');
        const addModelBtn = document.getElementById('addModelBtn');
        const addModelModal = document.getElementById('addModelModal');
        const closeModelModal = document.getElementById('closeModelModal');
        const cancelAddModel = document.getElementById('cancelAddModel');
        const addModelForm = document.getElementById('addModelForm');
        const deleteChatModal = document.getElementById('deleteChatModal');
        const closeDeleteModal = document.getElementById('closeDeleteModal');
        const cancelDelete = document.getElementById('cancelDelete');
        const confirmDelete = document.getElementById('confirmDelete');

        // State
        let chats = JSON.parse(localStorage.getItem('chats')) || [];
        let currentChatId = null;
        let defaultChatSettings = {
            temperature: 0.7,
            historyCount: 5,
            systemMessage: 'You are a helpful assistant.'
        };
        let models = [
            { name: 'GPT-3.5', id: 'gpt-3.5', type: 'default' },
            { name: 'GPT-4', id: 'gpt-4', type: 'default' }
        ];
        let customModels = JSON.parse(localStorage.getItem('customModels')) || [];
        let customProviders = JSON.parse(localStorage.getItem('customProviders')) || [];
        let selectedModel = 'gpt-4';
        let chatToDelete = null;
        let currentProvider = null;

        // Initialize
        function init() {
            // Load default chat settings
            const savedDefaults = localStorage.getItem('defaultChatSettings');
            if (savedDefaults) {
                defaultChatSettings = JSON.parse(savedDefaults);
            }

            // Load custom models and providers from localStorage
            const savedCustomModels = localStorage.getItem('customModels');
            if (savedCustomModels) {
                customModels = JSON.parse(savedCustomModels);
                customModels.forEach(model => {
                    models.push({
                        id: model.id,
                        name: model.name,
                        endpoint: model.endpoint,
                        key: model.key,
                        streaming: model.streaming || false,
                        type: 'custom'
                    });
                });
            }

            // Load custom providers
            const savedCustomProviders = localStorage.getItem('customProviders');
            if (savedCustomProviders) {
                customProviders = JSON.parse(savedCustomProviders);
                customProviders.forEach(provider => {
                    models.push({
                        id: provider.id,
                        name: provider.name,
                        type: 'provider',
                        baseUrl: provider.baseUrl,
                        apiKey: provider.apiKey,
                        streaming: provider.streaming || false
                    });
                });
            }
            // Configure marked.js
            marked.setOptions({
                breaks: true,
                gfm: true,
                highlight: function(code, lang) {
                    const language = hljs.getLanguage(lang) ? lang : 'plaintext';
                    return hljs.highlight(code, { language }).value;
                }
            });

            // Load custom models
            customModels.forEach(model => {
                models.push({ ...model, type: 'custom' });
            });
            
            // Render chat list
            renderChatList();
            
            // Set dark mode if preferred
            if (localStorage.getItem('darkMode') === 'true' || 
                (!localStorage.getItem('darkMode') && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
                document.documentElement.classList.add('dark');
            }
            
            // Set current model
            const savedModel = localStorage.getItem('selectedModel');
            if (savedModel) {
                try {
                    const modelData = JSON.parse(savedModel);
                    const model = models.find(m => m.id === modelData.id);
                    if (model) {
                        selectedModel = modelData.id;
                        currentModel.textContent = modelData.name;
                        
                        // For provider models, ensure the provider is still available
                        if (modelData.type === 'provider-model') {
                            const providerId = modelData.id.split('-')[0];
                            const providerExists = customProviders.some(p => p.id === providerId);
                            if (!providerExists) {
                                // Fall back to default if provider is gone
                                selectModel('gpt-4');
                            }
                        }
                    }
                } catch (e) {
                    // Fallback for old string format
                    const model = models.find(m => m.id === savedModel);
                    if (model) {
                        selectedModel = savedModel;
                        currentModel.textContent = model.name;
                        
                        // Migrate old chats to new modelData format
                        chats.forEach(chat => {
                            if (!chat.modelData && chat.model) {
                                chat.modelData = {
                                    id: chat.model,
                                    name: models.find(m => m.id === chat.model)?.name || 'GPT-4'
                                };
                            }
                        });
                        saveChats();
                    }
                }
            }
            
            // Render custom models and providers in dropdown
            renderCustomModels();
            renderCustomProviders();
        }

        // Render chat list
        function renderChatList() {
            chatList.innerHTML = '';
            
            if (chats.length === 0) {
                chatList.innerHTML = `
                    <div class="p-4 text-center text-gray-500 dark:text-gray-400">
                        No chats yet
                    </div>
                `;
                return;
            }
            
            chats.forEach(chat => {
                // Function to handle edit mode
                const setupEditMode = (chatElement, chat) => {
                    const titleElement = chatElement.querySelector('.chat-title');
                    const originalTitle = titleElement.textContent;
                    
                    const input = document.createElement('input');
                    input.type = 'text';
                    input.value = originalTitle;
                    input.className = 'w-full bg-transparent border-b border-blue-500 focus:outline-none';
                    
                    titleElement.replaceWith(input);
                    input.focus();
                    
                    const handleKeyDown = (e) => {
                        if (e.key === 'Enter') {
                            finishEdit();
                        } else if (e.key === 'Escape') {
                            input.value = originalTitle;
                            finishEdit();
                        }
                    };
                    
                    const finishEdit = () => {
                        const newTitle = input.value.trim() || 'New Chat';
                        chat.title = newTitle;
                        saveChats();
                        
                        titleElement.textContent = newTitle;
                        input.replaceWith(titleElement);
                        
                        input.removeEventListener('blur', finishEdit);
                        input.removeEventListener('keydown', handleKeyDown);
                    };
                    
                    input.addEventListener('blur', finishEdit);
                    input.addEventListener('keydown', handleKeyDown);
                };
                const chatElement = document.createElement('div');
                chatElement.className = `group relative flex items-center gap-3 p-2 rounded-lg cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 transition-all ${currentChatId === chat.id ? 'bg-gray-100 dark:bg-gray-700' : ''}`;
                chatElement.dataset.id = chat.id;
                
                chatElement.innerHTML = `
                    <div class="w-8 h-8 rounded-full bg-primary-100 dark:bg-primary-900 text-primary-500 dark:text-primary-300 flex items-center justify-center">
                        <i class="fas fa-comment"></i>
                    </div>
                    <div class="flex-1 min-w-0">
                        <div class="flex items-center gap-1">
                            <p class="truncate font-medium chat-title">${chat.title || 'New Chat'}</p>
                            <button class="edit-chat opacity-0 group-hover:opacity-100 text-gray-400 hover:text-blue-500 transition-all p-1">
                                <i class="fas fa-edit text-xs"></i>
                            </button>
                        </div>
                        <p class="text-xs text-gray-500 dark:text-gray-400 truncate">${chat.lastMessage || ''}</p>
                    </div>
                    <button class="delete-chat opacity-0 group-hover:opacity-100 text-gray-400 hover:text-red-500 transition-all p-1">
                        <i class="fas fa-trash text-sm"></i>
                    </button>
                `;
                
                chatElement.addEventListener('click', () => loadChat(chat.id));
                
                const deleteBtn = chatElement.querySelector('.delete-chat');
                deleteBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    showDeleteModal(chat.id);
                });
                
                chatList.appendChild(chatElement);
                
                // Add edit button event listener
                const editBtn = chatElement.querySelector('.edit-chat');
                editBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    setupEditMode(chatElement, chat);
                });
            });
        }

        // Render custom providers in dropdown
        function renderCustomProviders() {
            customProvidersList.innerHTML = '';
            
            if (customProviders.length === 0) {
                customProvidersList.innerHTML = `
                    <div class="text-center text-gray-500 dark:text-gray-400 py-2 text-sm">
                        No custom providers added
                    </div>
                `;
                return;
            }
            
            customProviders.forEach(provider => {
                const providerElement = document.createElement('div');
                providerElement.className = 'provider-option cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded flex justify-between items-center';
                providerElement.dataset.provider = provider.id;
                
                providerElement.innerHTML = `
                    <span>${provider.name}</span>
                    <div class="flex gap-1">
                        <button class="edit-provider text-gray-400 hover:text-blue-500 transition-all p-1" title="Edit">
                            <i class="fas fa-edit text-xs"></i>
                        </button>
                        <button class="view-provider-models text-gray-400 hover:text-blue-500 transition-all p-1" title="View Models">
                            <i class="fas fa-list text-xs"></i>
                        </button>
                        <button class="delete-provider text-gray-400 hover:text-red-500 transition-all p-1">
                            <i class="fas fa-trash text-xs"></i>
                        </button>
                    </div>
                `;
                
                const deleteBtn = providerElement.querySelector('.delete-provider');
                deleteBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    deleteCustomProvider(provider.id);
                });

                const viewModelsBtn = providerElement.querySelector('.view-provider-models');
                viewModelsBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    showProviderModels(provider);
                });

                const editBtn = providerElement.querySelector('.edit-provider');
                editBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    showAddProviderModal(provider);
                });
                
                customProvidersList.appendChild(providerElement);
            });
        }

        // Render custom models in dropdown
        function renderCustomModels() {
            customModelsList.innerHTML = '';
            
            // Reload custom models from localStorage to ensure we have latest
            const savedCustomModels = localStorage.getItem('customModels');
            if (savedCustomModels) {
                customModels = JSON.parse(savedCustomModels);
            }
            
            if (customModels.length === 0) {
                customModelsList.innerHTML = `
                    <div class="text-center text-gray-500 dark:text-gray-400 py-2 text-sm">
                        No custom models added
                    </div>
                `;
                return;
            }
            
            customModels.forEach(model => {
                const modelElement = document.createElement('div');
                modelElement.className = 'model-option cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded flex justify-between items-center';
                modelElement.dataset.model = model.id;
                
                modelElement.innerHTML = `
                    <span>${model.name}</span>
                    <div class="flex gap-1">
                        <button class="edit-model text-gray-400 hover:text-blue-500 transition-all p-1">
                            <i class="fas fa-edit text-xs"></i>
                        </button>
                        <button class="delete-model text-gray-400 hover:text-red-500 transition-all p-1">
                            <i class="fas fa-trash text-xs"></i>
                        </button>
                    </div>
                `;
                
                modelElement.addEventListener('click', (e) => {
                    if (!e.target.classList.contains('delete-model')) {
                        selectModel(model.id);
                    }
                });
                
                const deleteBtn = modelElement.querySelector('.delete-model');
                deleteBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    deleteCustomModel(model.id);
                });

                const editBtn = modelElement.querySelector('.edit-model');
                editBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    showEditModelModal(model);
                });
                
                customModelsList.appendChild(modelElement);
            });
        }

        // Show chat settings modal
        function showChatSettingsModal() {
            if (!currentChatId) return;
            
            const chat = chats.find(c => c.id === currentChatId);
            if (!chat) return;
            
            // Initialize settings if they don't exist
            if (!chat.settings) {
                chat.settings = { ...defaultChatSettings };
            }
            
            document.getElementById('chatTemperature').value = chat.settings.temperature;
            document.getElementById('chatHistoryCount').value = chat.settings.historyCount;
            document.getElementById('chatSystemMessage').value = chat.settings.systemMessage;
            
            chatSettingsModal.classList.remove('hidden');
        }

        // Hide chat settings modal
        function hideChatSettingsModal() {
            chatSettingsModal.classList.add('hidden');
        }

        // Save chat settings
        function saveChatSettings() {
            if (!currentChatId) return;
            
            const chat = chats.find(c => c.id === currentChatId);
            if (!chat) return;
            
            chat.settings = {
                temperature: parseFloat(document.getElementById('chatTemperature').value),
                historyCount: parseInt(document.getElementById('chatHistoryCount').value),
                systemMessage: document.getElementById('chatSystemMessage').value
            };
            
            saveChats();
            hideChatSettingsModal();
        }

        // Save default chat settings
        function saveDefaultChatSettings() {
            defaultChatSettings = {
                temperature: parseFloat(document.getElementById('defaultTemperature').value),
                historyCount: parseInt(document.getElementById('defaultHistoryCount').value),
                systemMessage: document.getElementById('defaultSystemMessage').value
            };
            
            localStorage.setItem('defaultChatSettings', JSON.stringify(defaultChatSettings));
            
            // Show success feedback
            const submitBtn = document.querySelector('#defaultChatSettingsForm button[type="submit"]');
            const originalText = submitBtn.textContent;
            submitBtn.textContent = 'Saved!';
            submitBtn.classList.remove('bg-primary-500', 'hover:bg-primary-600');
            submitBtn.classList.add('bg-green-500', 'hover:bg-green-600');
            
            setTimeout(() => {
                submitBtn.textContent = originalText;
                submitBtn.classList.remove('bg-green-500', 'hover:bg-green-600');
                submitBtn.classList.add('bg-primary-500', 'hover:bg-primary-600');
            }, 2000);
        }

        // Create new chat
        function createNewChat() {
            const newChat = {
                id: Date.now().toString(),
                title: 'New Chat',
                model: selectedModel,
                messages: [],
                lastMessage: '',
                createdAt: new Date().toISOString(),
                modelData: {
                    id: selectedModel,
                    name: models.find(m => m.id === selectedModel)?.name || 'GPT-4'
                },
                settings: { ...defaultChatSettings }
            };
            
            chats.unshift(newChat);
            saveChats();
            loadChat(newChat.id);
            renderChatList();
            
            // Welcome message will be shown automatically since messages array is empty
        }

        // Load chat
        function loadChat(chatId) {
            hideSettingsPage();
            currentChatId = chatId;
            const chat = chats.find(c => c.id === chatId);
            
            if (!chat) {
                createNewChat();
                return;
            }
            
            // Update UI and model selection
            document.querySelectorAll('[data-id]').forEach(el => {
                el.classList.toggle('bg-gray-100', el.dataset.id === chatId);
                el.classList.toggle('dark:bg-gray-700', el.dataset.id === chatId);
            });

            // Update model selection to match this chat's model
            if (chat.modelData) {
                selectedModel = chat.modelData.id;
                currentModel.textContent = chat.modelData.name;
                localStorage.setItem('selectedModel', JSON.stringify(chat.modelData));
            }
            
            // Render messages
            renderMessages(chat.messages);
            
            // Show welcome message if chat is empty
            welcomeMessage.classList.toggle('hidden', chat.messages.length > 0);
        }

        // Render messages
        function renderMessages(messages) {
            chatArea.innerHTML = '';
            
            if (messages.length === 0) {
                welcomeMessage.classList.remove('hidden');
                return;
            }
            
            const messagesContainer = document.createElement('div');
            messagesContainer.className = 'space-y-6';
            
            messages.forEach((msg, index) => {
                const messageElement = document.createElement('div');
                messageElement.className = `flex gap-4 ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`;
                
                if (msg.role === 'assistant') {
                    messageElement.innerHTML = `
                        <div class="flex-shrink-0 w-8 h-8 rounded-full bg-primary-500 text-white flex items-center justify-center">
                            <i class="fas fa-robot"></i>
                        </div>
                        <div class="max-w-[80%] bg-white dark:bg-gray-800 rounded-lg p-4 shadow-sm relative group">
                            <div class="prose dark:prose-invert max-w-none break-words">${marked.parse(msg.content)}</div>
                            <div class="absolute right-2 top-2 opacity-0 group-hover:opacity-100 transition-opacity flex gap-1">
                                <button class="message-action bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 p-1 rounded text-gray-700 dark:text-gray-300" data-action="regenerate" title="Regenerate">
                                    <i class="fas fa-sync-alt text-xs"></i>
                                </button>
                                <button class="message-action bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 p-1 rounded text-gray-700 dark:text-gray-300" data-action="edit" title="Edit">
                                    <i class="fas fa-edit text-xs"></i>
                                </button>
                            </div>
                        </div>
                    `;
                } else {
                    messageElement.innerHTML = `
                        <div class="max-w-[80%] bg-primary-500 text-white rounded-lg p-4 shadow-sm relative group">
                            <div class="prose prose-white whitespace-pre-wrap">${msg.content.replace(/\n/g, '<br>')}</div>
                            <div class="absolute right-2 top-2 opacity-0 group-hover:opacity-100 transition-opacity">
                                <button class="message-action bg-primary-600 hover:bg-primary-700 p-1 rounded text-white" data-action="edit" title="Edit">
                                    <i class="fas fa-edit text-xs"></i>
                                </button>
                            </div>
                        </div>
                    `;
                }
                
                messagesContainer.appendChild(messageElement);
            });
            
            chatArea.appendChild(messagesContainer);
            chatArea.scrollTop = chatArea.scrollHeight;
        }

        // Send message
        async function sendMessage() {
            const message = messageInput.value.trim();
            if (!message) return;
            
            // If no chat is selected, create a new one
            if (!currentChatId) {
                createNewChat();
                // Wait a moment for the chat to be created
                await new Promise(resolve => setTimeout(resolve, 50));
            }
            
            // Get current chat
            let chatIndex = chats.findIndex(c => c.id === currentChatId);
            if (chatIndex === -1) return;
            
            const chat = chats[chatIndex];
            const model = models.find(m => m.id === chat.model);
            if (!model) return;
            
            if (model.type === 'default' && !localStorage.getItem('openaiApiKey')) {
                showApiKeyModal();
                return;
            }
            
            // Add user message to chat
            const userMessage = {
                role: 'user',
                content: message,
                timestamp: new Date().toISOString()
            };
            
            // Ensure we have the latest chat reference
            chatIndex = chats.findIndex(c => c.id === currentChatId);
            if (chatIndex === -1) return;
            let currentChat = chats[chatIndex];
            
            currentChat.messages.push(userMessage);
            currentChat.lastMessage = message.length > 30 ? message.substring(0, 30) + '...' : message;
            
            // Save chat immediately after adding user message
            chats[chatIndex] = currentChat;
            saveChats();
            
            // Render all messages including the new user message
            renderMessages(chat.messages);
            messageInput.value = '';
            chatArea.scrollTop = chatArea.scrollHeight;
            
            // Create assistant message element for streaming/response
            const assistantMessageElement = document.createElement('div');
            assistantMessageElement.className = 'flex gap-4 justify-start';
            assistantMessageElement.innerHTML = `
                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-primary-500 text-white flex items-center justify-center">
                    <i class="fas fa-robot"></i>
                </div>
                <div class="max-w-[80%] bg-white dark:bg-gray-800 rounded-lg p-4 shadow-sm relative group">
                    <div class="flex gap-2" id="loading-indicator">
                        <div class="w-2 h-2 rounded-full bg-gray-300 animate-pulse"></div>
                        <div class="w-2 h-2 rounded-full bg-gray-300 animate-pulse delay-75"></div>
                        <div class="w-2 h-2 rounded-full bg-gray-300 animate-pulse delay-150"></div>
                    </div>
                    <div class="prose dark:prose-invert hidden" id="streaming-content"></div>
                </div>
            `;
            chatArea.appendChild(assistantMessageElement);
            const contentElement = assistantMessageElement.querySelector('#streaming-content');
            const loadingIndicator = assistantMessageElement.querySelector('#loading-indicator');

            try {
                // Ensure we have the latest chat reference first
                chatIndex = chats.findIndex(c => c.id === currentChatId);
                if (chatIndex === -1) throw new Error('Chat not found');
                currentChat = chats[chatIndex];
                
                // Get the current model
                const model = models.find(m => m.id === currentChat.model);
                if (!model) throw new Error('Model not found');
                
                // Prepare messages for API
                const messages = currentChat.messages.map(msg => ({
                    role: msg.role,
                    content: msg.content
                }));
                
                let response;
                
                response = '';
                const onStream = (content) => {
                    response = content;
                    contentElement.innerHTML = marked.parse(response);
                    chatArea.scrollTop = chatArea.scrollHeight;
                };

                if (model.type === 'default') {
                    // Call OpenAI API
                    response = await callOpenAIAPI(messages, model.name, null, onStream);
                } else if (model.type === 'provider-model') {
                    // Call provider model API
                    response = await callProviderModelAPI(messages, model, onStream);
                } else {
                    // Call custom model API
                    response = await callCustomModelAPI(messages, model, onStream);
                }
                
                // Hide loading indicator and show content
                loadingIndicator.classList.add('hidden');
                contentElement.classList.remove('hidden');
                
                // Ensure we have the latest chat reference
                chatIndex = chats.findIndex(c => c.id === currentChatId);
                if (chatIndex === -1) return;
                currentChat = chats[chatIndex];
                
                // Update the assistant message in the chat
                const assistantMessage = {
                    role: 'assistant',
                    content: response,
                    timestamp: new Date().toISOString()
                };
                
                // Replace the last message (which was the loading one) with the actual response
                currentChat.messages.push(assistantMessage);
                saveChats();
                
                // Update chat list
                renderChatList();
                
                // Force re-render of messages to ensure UI is in sync
                renderMessages(currentChat.messages);
            } catch (error) {
                console.error('API Error:', error);
                // Remove the message element if there was an error
                messageElement.remove();
                
                // Show error message
                const errorElement = document.createElement('div');
                errorElement.className = 'flex gap-4 justify-start';
                errorElement.innerHTML = `
                    <div class="flex-shrink-0 w-8 h-8 rounded-full bg-red-500 text-white flex items-center justify-center">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                    <div class="max-w-[80%] bg-white dark:bg-gray-800 rounded-lg p-4 shadow-sm">
                        <div class="text-red-500">Error: ${error.message}</div>
                    </div>
                `;
                
                chatArea.appendChild(errorElement);
                chatArea.scrollTop = chatArea.scrollHeight;
            }
        }

        // Call OpenAI API
        async function callOpenAIAPI(messages, model, apiKey, onStream) {
            const isStreaming = model.streaming || false;
            // Get current chat settings
                const chat = chats.find(c => c.id === currentChatId);
                const settings = chat?.settings || defaultChatSettings;
                
                // Limit messages to history count
                const limitedMessages = messages.slice(-settings.historyCount);

            // Add system message if provided
                if (settings.systemMessage) {
                    limitedMessages.unshift({
                        role: 'system',
                        content: settings.systemMessage
                    });
                }
            
            const response = await fetch('https://api.openai.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKey || localStorage.getItem('openaiApiKey')}`
                },
                


                body: JSON.stringify({
                    model: model === 'gpt-3.5' ? 'gpt-3.5-turbo' : 'gpt-4',
                    messages: limitedMessages,
                    temperature: settings.temperature,
                    stream: isStreaming
                })
            });
            
            if (!response.ok) {
                const errorData = await response.json();
                throw new Error(errorData.error?.message || 'Failed to call OpenAI API');
            }
            
            if (isStreaming && onStream) {
                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let result = '';
                
                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;
                    
                    const chunk = decoder.decode(value);
                    const lines = chunk.split('\n').filter(line => line.trim() !== '');
                    
                    for (const line of lines) {
                        if (line.startsWith('data: ')) {
                            const data = line.substring(6);
                            if (data === '[DONE]') continue;
                            
                            try {
                                const parsed = JSON.parse(data);
                                const content = parsed.choices?.[0]?.delta?.content || '';
                                result += content;
                                onStream(result);
                            } catch (e) {
                                console.error('Error parsing stream data:', e);
                            }
                        }
                    }
                }
                return result;
            } else {
                const data = await response.json();
                return data.choices[0]?.message?.content || 'No response from model';
            }
        }
        
        // Call provider model API
        async function callProviderModelAPI(messages, model, onStream) {
            const endpoint = `${model.baseUrl}/chat/completions`;
            
            if (model.streaming) {
                const response = await fetch(endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        ...(model.apiKey ? { 'Authorization': `Bearer ${model.apiKey}` } : {})
                    },
                    body: JSON.stringify({
                        model: model.modelName,
                        messages,
                        temperature: 0.7,
                        stream: true
                    })
                });

                if (!response.ok) {
                    const errorText = await response.text();
                    throw new Error(errorText || 'Failed to call provider model API');
                }

                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let result = '';

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;
                    
                    const chunk = decoder.decode(value);
                    const lines = chunk.split('\n').filter(line => line.trim() !== '');
                    
                    for (const line of lines) {
                        if (line.startsWith('data: ')) {
                            const data = line.substring(6);
                            if (data === '[DONE]') continue;
                            
                            try {
                                const parsed = JSON.parse(data);
                                const content = parsed.choices?.[0]?.delta?.content || '';
                                result += content;
                                if (onStream) onStream(result);
                            } catch (e) {
                                console.error('Error parsing stream data:', e);
                            }
                        }
                    }
                }
                return result;
            } else {
                const response = await fetch(endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        ...(model.apiKey ? { 'Authorization': `Bearer ${model.apiKey}` } : {})
                    },
                    body: JSON.stringify({
                        model: model.modelName,
                        messages,
                        temperature: 0.7
                    })
                });
                
                if (!response.ok) {
                    const errorText = await response.text();
                    throw new Error(errorText || 'Failed to call provider model API');
                }
                
                const data = await response.json();
                return data.choices?.[0]?.message?.content || data.response || 'No response from model';
            }
        }

        // Call custom model API
        async function callCustomModelAPI(messages, model) {
            if (model.streaming) {
                // Handle streaming response
                const response = await fetch(model.endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        ...(model.key ? { 'Authorization': `Bearer ${model.key}` } : {})
                    },
                    body: JSON.stringify({
                        model: model.name,
                        messages,
                        temperature: 0.7,
                        stream: true
                    })
                });

                if (!response.ok) {
                    const errorText = await response.text();
                    throw new Error(errorText || 'Failed to call custom model API');
                }

                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let result = '';

                // Create message element for streaming
                const messageElement = document.createElement('div');
                messageElement.className = 'flex gap-4 justify-start';
                messageElement.innerHTML = `
                    <div class="flex-shrink-0 w-8 h-8 rounded-full bg-primary-500 text-white flex items-center justify-center">
                        <i class="fas fa-robot"></i>
                    </div>
                    <div class="max-w-[80%] bg-white dark:bg-gray-800 rounded-lg p-4 shadow-sm relative group">
                        <div class="prose dark:prose-invert" id="streaming-content"></div>
                    </div>
                `;
                chatArea.appendChild(messageElement);
                const contentElement = messageElement.querySelector('#streaming-content');

                try {
                    while (true) {
                        const { done, value } = await reader.read();
                        if (done) break;
                        
                        const chunk = decoder.decode(value);
                        const lines = chunk.split('\n').filter(line => line.trim() !== '');
                        
                        for (const line of lines) {
                            if (line.startsWith('data: ')) {
                                const data = line.substring(6);
                                if (data === '[DONE]') continue;
                                
                                try {
                                    const parsed = JSON.parse(data);
                                    const content = parsed.choices?.[0]?.delta?.content || '';
                                    result += content;
                                    // Process markdown in streaming response
                                    contentElement.innerHTML = marked.parse(result);
                                    chatArea.scrollTop = chatArea.scrollHeight;
                                } catch (e) {
                                    console.error('Error parsing stream data:', e);
                                }
                            }
                        }
                    }
                } catch (e) {
                    console.error('Stream error:', e);
                    throw e;
                }

                return result;
            } else {
                // Handle regular response
                const response = await fetch(model.endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        ...(model.key ? { 'Authorization': `Bearer ${model.key}` } : {})
                    },
                    body: JSON.stringify({
                        model: model.name,
                        messages,
                        temperature: 0.7,
                        stream: false
                    })
                });
                
                if (!response.ok) {
                    try {
                        const errorData = await response.json();
                        throw new Error(errorData.error?.message || 'Failed to call custom model API');
                    } catch (e) {
                        const errorText = await response.text();
                        throw new Error(errorText || 'Failed to call custom model API');
                    }
                }
                
                try {
                    const data = await response.json();
                    return data.choices[0]?.message?.content || data.response || 'No response from model';
                } catch (e) {
                    const text = await response.text();
                    return text || 'No response from model';
                }
            }
        }

        // Select model
        function selectModel(modelId) {
            // Ensure models array is up to date
            const savedCustomModels = localStorage.getItem('customModels');
            if (savedCustomModels) {
                customModels = JSON.parse(savedCustomModels);
                // Update models array with latest custom models
                models = models.filter(m => m.type !== 'custom');
                customModels.forEach(model => {
                    models.push({
                        id: model.id,
                        name: model.name,
                        endpoint: model.endpoint,
                        key: model.key,
                        streaming: model.streaming || false,
                        type: 'custom'
                    });
                });
            }
            
            const model = models.find(m => m.id === modelId);
            if (!model) return;
            
            selectedModel = modelId;
            currentModel.textContent = model.name;
            modelDropdown.classList.add('hidden');
            
            // Save to localStorage - include both model ID and name for provider models
            const modelData = {
                id: modelId,
                name: model.name,
                type: model.type
            };
            localStorage.setItem('selectedModel', JSON.stringify(modelData));
            
            // Update current chat's model if one is active
            if (currentChatId) {
                const chat = chats.find(c => c.id === currentChatId);
                if (chat) {
                    chat.model = modelId;
                    chat.modelData = {
                        id: modelId,
                        name: model.name
                    };
                    saveChats();
                }
            }
        }

        // Add custom model
        function addCustomModel(name, endpoint, key, streaming) {
            const newModel = {
                id: `custom-${Date.now()}`,
                name,
                endpoint,
                key,
                streaming: streaming || false,
                type: 'custom'
            };
            
            customModels.push(newModel);
            models.push(newModel);
            
            // Save to localStorage
            localStorage.setItem('customModels', JSON.stringify(customModels));
            
            // Update UI
            renderCustomModels();
            hideAddModelModal();
        }

        // Edit custom model
        function editCustomModel(modelId, name, endpoint, key, streaming) {
            const modelIndex = customModels.findIndex(m => m.id === modelId);
            if (modelIndex === -1) return;
            
            customModels[modelIndex] = {
                ...customModels[modelIndex],
                name,
                endpoint,
                key,
                streaming: streaming || false
            };
            
            // Update in models array
            const globalModelIndex = models.findIndex(m => m.id === modelId);
            if (globalModelIndex !== -1) {
                models[globalModelIndex] = {
                    ...models[globalModelIndex],
                    name,
                    endpoint,
                    key
                };
            }
            
            // Save to localStorage
            localStorage.setItem('customModels', JSON.stringify(customModels));
            
            // Update UI
            renderCustomModels();
            hideAddModelModal();
        }

        // Edit custom provider
        function editCustomProvider(providerId, name, baseUrl, apiKey, streaming) {
            const providerIndex = customProviders.findIndex(p => p.id === providerId);
            if (providerIndex === -1) return;
            
            const updatedProvider = {
                ...customProviders[providerIndex],
                name,
                baseUrl,
                apiKey,
                streaming: streaming || false
            };
            
            customProviders[providerIndex] = updatedProvider;
            
            // Update in models array
            const globalModelIndex = models.findIndex(m => m.id === providerId);
            if (globalModelIndex !== -1) {
                models[globalModelIndex] = {
                    ...models[globalModelIndex],
                    name: updatedProvider.name,
                    baseUrl: updatedProvider.baseUrl,
                    apiKey: updatedProvider.apiKey,
                    streaming: updatedProvider.streaming
                };
            }
            
            // Also update any provider models that use this provider
            models.forEach((model, index) => {
                if (model.type === 'provider-model' && model.id.startsWith(providerId)) {
                    models[index] = {
                        ...model,
                        baseUrl: updatedProvider.baseUrl,
                        apiKey: updatedProvider.apiKey,
                        streaming: updatedProvider.streaming
                    };
                }
            });
            
            localStorage.setItem('customProviders', JSON.stringify(customProviders));
            localStorage.setItem('models', JSON.stringify(models));
            renderCustomProviders();
            hideAddProviderModal();
        }

        // Add custom provider
        function addCustomProvider(name, baseUrl, apiKey, streaming) {
            const newProvider = {
                id: `provider-${Date.now()}`,
                name,
                baseUrl,
                apiKey,
                streaming: streaming || false
            };
            
            customProviders.push(newProvider);
            models.push({
                id: newProvider.id,
                name: newProvider.name,
                type: 'provider',
                baseUrl: newProvider.baseUrl,
                apiKey: newProvider.apiKey,
                streaming: newProvider.streaming
            });
            
            localStorage.setItem('customProviders', JSON.stringify(customProviders));
            localStorage.setItem('models', JSON.stringify(models));
            renderCustomProviders();
        }

        // Delete custom provider
        function deleteCustomProvider(providerId) {
            customProviders = customProviders.filter(p => p.id !== providerId);
            models = models.filter(m => m.id !== providerId);
            
            localStorage.setItem('customProviders', JSON.stringify(customProviders));
            renderCustomProviders();
            
            if (selectedModel === providerId) {
                selectModel('gpt-4');
            }
        }

        // Show provider models
        async function showProviderModels(provider) {
            currentProvider = provider;
            document.getElementById('providerModelsTitle').textContent = `${provider.name} Models`;
            document.getElementById('providerModelsList').innerHTML = '<div class="text-center py-4">Loading models...</div>';

            providerModelsModal.classList.remove('hidden');

            try {
                const response = await fetch(`${provider.baseUrl}/models`, {
                    headers: {
                        'Authorization': `Bearer ${provider.apiKey || ''}`
                    }
                });

                if (!response.ok) {
                    throw new Error('Failed to fetch models');
                }

                const data = await response.json();
                const modelsList = document.getElementById('providerModelsList');
                modelsList.innerHTML = '';

                // OpenAI models endpoint returns { data: [ ... ] }
                if (data.data && data.data.length > 0) {
                    data.data.forEach(modelObj => {
                        const modelElement = document.createElement('div');
                        modelElement.className = 'model-option cursor-pointer px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 rounded';
                        modelElement.textContent = modelObj.id;

                        modelElement.addEventListener('click', () => {
                            // Create a model entry for this provider model
                            const providerModel = {
                                id: `${currentProvider.id}-${modelObj.id}`,
                                name: `${currentProvider.name} - ${modelObj.id}`,
                                type: 'provider-model',
                                baseUrl: currentProvider.baseUrl,
                                apiKey: currentProvider.apiKey,
                                modelName: modelObj.id,
                                streaming: currentProvider.streaming || false
                            };
                            
                            // Add or update in models array
                            const existingIndex = models.findIndex(m => m.id === providerModel.id);
                            if (existingIndex !== -1) {
                                models[existingIndex] = providerModel;
                            } else {
                                models.push(providerModel);
                            }
                            
                            selectModel(providerModel.id);
                            providerModelsModal.classList.add('hidden');
                        });

                        modelsList.appendChild(modelElement);
                    });
                } else {
                    modelsList.innerHTML = '<div class="text-center py-4 text-gray-500">No models available</div>';
                }
            } catch (error) {
                document.getElementById('providerModelsList').innerHTML = `
                    <div class="text-center py-4 text-red-500">
                        Error loading models: ${error.message}
                    </div>
                `;
            }
        }


        // Delete custom model
        function deleteCustomModel(modelId) {
            customModels = customModels.filter(m => m.id !== modelId);
            models = models.filter(m => m.id !== modelId);
            
            // Save to localStorage
            localStorage.setItem('customModels', JSON.stringify(customModels));
            
            // Update UI
            renderCustomModels();
            
            // If the deleted model was selected, switch to default
            if (selectedModel === modelId) {
                selectModel('gpt-4');
            }
        }

        // Delete chat
        function deleteChat(chatId) {
            chats = chats.filter(c => c.id !== chatId);
            saveChats();
            
            if (currentChatId === chatId) {
                currentChatId = null;
                chatArea.innerHTML = '';
                welcomeMessage.classList.remove('hidden');
            }
            
            renderChatList();
            hideDeleteModal();
        }

        // Show delete modal
        function showDeleteModal(chatId) {
            chatToDelete = chatId;
            deleteChatModal.classList.remove('hidden');
        }

        // Hide delete modal
        function hideDeleteModal() {
            deleteChatModal.classList.add('hidden');
            chatToDelete = null;
        }

        // Show add model modal
        function showAddModelModal() {
            addModelModal.classList.remove('hidden');
        }

        // Show edit model modal
        function showEditModelModal(model) {
            document.getElementById('modelName').value = model.name;
            document.getElementById('modelEndpoint').value = model.endpoint;
            document.getElementById('modelKey').value = model.key || '';
            document.getElementById('modelStreaming').checked = model.streaming || false;
            document.getElementById('editModelId').value = model.id;
            
            // Change modal title and submit button text
            document.querySelector('#addModelModal h3').textContent = 'Edit Custom Model';
            document.querySelector('#addModelForm button[type="submit"]').textContent = 'Save Changes';
            
            showAddModelModal();
        }

        // Hide add model modal
        function hideAddModelModal() {
            addModelModal.classList.add('hidden');
            addModelForm.reset();
            
            // Reset modal title and submit button text
            document.querySelector('#addModelModal h3').textContent = 'Add Custom Model';
            document.querySelector('#addModelForm button[type="submit"]').textContent = 'Add Model';
        }

        // Save chats to localStorage
        function saveChats() {
            localStorage.setItem('chats', JSON.stringify(chats));
        }

        // Toggle dark mode
        function toggleDarkMode() {
            const isDark = document.documentElement.classList.toggle('dark');
            localStorage.setItem('darkMode', isDark);
            
            // Sync settings toggle if it exists
            const darkModeToggle = document.getElementById('darkModeToggleSetting');
            if (darkModeToggle) {
                darkModeToggle.checked = isDark;
            }
        }

        // Toggle sidebar on mobile
        function toggleSidebar() {
            document.querySelector('.w-64').classList.toggle('hidden');
            document.querySelector('.w-64').classList.toggle('block');
        }

        // Show API key modal
        function showApiKeyModal() {
            apiKeyModal.classList.remove('hidden');
        }

        // Hide API key modal
        function hideApiKeyModal() {
            apiKeyModal.classList.add('hidden');
            apiKeyInput.value = '';
        }

        // Save API key
        function saveApiKey(key) {
            localStorage.setItem('openaiApiKey', key);
            hideApiKeyModal();
        }

        // Show add provider modal
        function showAddProviderModal(provider = null) {
            // Reset form first
            const form = document.getElementById('addProviderForm');
            form.reset();
            document.getElementById('editProviderId').value = '';
            
            if (provider) {
                // Editing existing provider
                document.getElementById('providerName').value = provider.name;
                document.getElementById('providerBaseUrl').value = provider.baseUrl;
                document.getElementById('providerApiKey').value = provider.apiKey || '';
                document.getElementById('providerStreaming').checked = provider.streaming || false;
                document.getElementById('editProviderId').value = provider.id;
                
                // Update UI for edit mode
                document.querySelector('#addProviderModal h3').textContent = 'Edit Provider';
                document.querySelector('#addProviderForm button[type="submit"]').textContent = 'Save Changes';
            } else {
                // Creating new provider
                document.querySelector('#addProviderModal h3').textContent = 'Add Custom Provider';
                document.querySelector('#addProviderForm button[type="submit"]').textContent = 'Add Provider';
                document.getElementById('editProviderId').value = '';
            }
            
            addProviderModal.classList.remove('hidden');
        }

        // Hide add provider modal
        function hideAddProviderModal() {
            addProviderModal.classList.add('hidden');
            // Don't reset form here - let the submit handler or showAddProviderModal handle it
        }

        // Event Listeners
        closeApiKeyModal.addEventListener('click', hideApiKeyModal);
        
        // Chat settings events
        document.getElementById('chatSettingsBtn').addEventListener('click', showChatSettingsModal);
        document.getElementById('closeChatSettingsModal').addEventListener('click', hideChatSettingsModal);
        document.getElementById('cancelChatSettings').addEventListener('click', hideChatSettingsModal);
        document.getElementById('chatSettingsForm').addEventListener('submit', (e) => {
            e.preventDefault();
            saveChatSettings();
        });
        
        // Default chat settings events
        document.getElementById('defaultChatSettingsForm').addEventListener('submit', (e) => {
            e.preventDefault();
            saveDefaultChatSettings();
        });
        
        // Initialize default settings form
        document.getElementById('defaultTemperature').value = defaultChatSettings.temperature;
        document.getElementById('defaultHistoryCount').value = defaultChatSettings.historyCount;
        document.getElementById('defaultSystemMessage').value = defaultChatSettings.systemMessage;
        
        // Provider modal events
        document.getElementById('addProviderBtn').addEventListener('click', () => showAddProviderModal(null));
        document.getElementById('closeProviderModal').addEventListener('click', hideAddProviderModal);
        document.getElementById('cancelAddProvider').addEventListener('click', hideAddProviderModal);
        
        document.getElementById('addProviderForm').addEventListener('submit', (e) => {
            e.preventDefault();
            const name = document.getElementById('providerName').value.trim();
            const baseUrl = document.getElementById('providerBaseUrl').value.trim();
            const apiKey = document.getElementById('providerApiKey').value.trim();
            const streaming = document.getElementById('providerStreaming').checked;
            const editProviderId = document.getElementById('editProviderId').value;
            
            if (!name || !baseUrl) {
                alert('Provider name and base URL are required');
                return;
            }

            // Clear any previous error state
            const errorElements = document.querySelectorAll('.provider-error');
            errorElements.forEach(el => el.remove());

            // Validate base URL format
            try {
                new URL(baseUrl);
            } catch (e) {
                const baseUrlInput = document.getElementById('providerBaseUrl');
                const errorElement = document.createElement('p');
                errorElement.className = 'text-red-500 text-xs mt-1 provider-error';
                errorElement.textContent = 'Please enter a valid URL (e.g. https://api.example.com)';
                baseUrlInput.parentNode.appendChild(errorElement);
                return;
            }

            if (editProviderId) {
                editCustomProvider(editProviderId, name, baseUrl, apiKey, streaming);
            } else {
                addCustomProvider(name, baseUrl, apiKey, streaming);
            }
        });

        // Provider models modal events
        document.getElementById('closeProviderModelsModal').addEventListener('click', () => {
            providerModelsModal.classList.add('hidden');
        });
        document.getElementById('cancelProviderModels').addEventListener('click', () => {
            providerModelsModal.classList.add('hidden');
        });
        cancelApiKey.addEventListener('click', hideApiKeyModal);
        
        apiKeyForm.addEventListener('submit', (e) => {
            e.preventDefault();
            const key = apiKeyInput.value.trim();
            if (key) {
                saveApiKey(key);
                // Retry sending the message
                setTimeout(() => {
                    sendMessage();
                }, 100);
            }
        });
        sidebarToggle.addEventListener('click', toggleSidebar);
        darkModeToggle.addEventListener('click', (e) => {
            e.stopPropagation();
            toggleDarkMode();
        });
        newChatBtn.addEventListener('click', (e) => {
            e.preventDefault();
            e.stopPropagation();
            hideSettingsPage();
            createNewChat();
            
            // Hide welcome message when new chat is created
            welcomeMessage.classList.add('hidden');
            
            // Close sidebar on mobile after creating new chat
            if (window.innerWidth < 768) {
                document.querySelector('.w-64').classList.add('hidden');
            }
        });
        
        quickStartBtn.addEventListener('click', (e) => {
            e.preventDefault();
            e.stopPropagation();
            hideSettingsPage();
            createNewChat();
            
            // Hide welcome message when quick start is clicked
            welcomeMessage.classList.add('hidden');
            
            // Close sidebar on mobile after quick start
            if (window.innerWidth < 768) {
                document.querySelector('.w-64').classList.add('hidden');
            }
        });
        
        messageForm.addEventListener('submit', (e) => {
            e.preventDefault();
            sendMessage();
        });

        messageInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter') {
                // Check for Ctrl+Enter or Command+Enter (Mac)
                if (e.ctrlKey || e.metaKey) {
                    // Insert new line
                    const start = messageInput.selectionStart;
                    const end = messageInput.selectionEnd;
                    messageInput.value = messageInput.value.substring(0, start) + '\n' + messageInput.value.substring(end);
                    messageInput.selectionStart = messageInput.selectionEnd = start + 1;
                    // Prevent default to avoid submitting
                    e.preventDefault();
                } else if (!e.shiftKey) {
                    // Regular Enter - submit form
                    e.preventDefault();
                    sendMessage();
                }
            }
        });
        
        // Auto-resize textarea
        messageInput.addEventListener('input', () => {
            messageInput.style.height = 'auto';
            messageInput.style.height = `${messageInput.scrollHeight}px`;
        });
        
        // Model dropdown
        modelDropdownBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            modelDropdown.classList.toggle('hidden');
        });

        // Close dropdown when clicking outside
        document.addEventListener('click', (e) => {
            if (!modelDropdown.contains(e.target) && e.target !== modelDropdownBtn && !e.target.closest('.model-option')) {
                modelDropdown.classList.add('hidden');
            }
        });

        // Handle model selection - delegated event listener
        document.addEventListener('click', (e) => {
            const modelOption = e.target.closest('.model-option');
            if (modelOption && !e.target.classList.contains('delete-model')) {
                const modelId = modelOption.dataset.model;
                selectModel(modelId);
            }
        });
        
        // Add model
        addModelBtn.addEventListener('click', showAddModelModal);
        closeModelModal.addEventListener('click', hideAddModelModal);
        cancelAddModel.addEventListener('click', hideAddModelModal);
        
        addModelForm.addEventListener('submit', (e) => {
            e.preventDefault();
            const name = document.getElementById('modelName').value;
            const endpoint = document.getElementById('modelEndpoint').value;
            const key = document.getElementById('modelKey').value;
            const streaming = document.getElementById('modelStreaming').checked;
            const editModelId = document.getElementById('editModelId').value;
            
            if (editModelId) {
                editCustomModel(editModelId, name, endpoint, key, streaming);
            } else {
                addCustomModel(name, endpoint, key);
            }
        });
        
        // Delete chat
        closeDeleteModal.addEventListener('click', hideDeleteModal);
        cancelDelete.addEventListener('click', hideDeleteModal);
        confirmDelete.addEventListener('click', () => {
            if (chatToDelete) {
                deleteChat(chatToDelete);
            }
        });

        // Settings button
        document.getElementById('settingsBtn').addEventListener('click', (e) => {
            e.stopPropagation();
            showSettingsPage();
        });

        // Back to chat button
        document.getElementById('backToChatBtn').addEventListener('click', (e) => {
            e.stopPropagation();
            hideSettingsPage();
        });

        // Show settings page
        function showSettingsPage() {
            document.getElementById('settingsPage').classList.remove('hidden');
            document.body.classList.add('overflow-hidden');
            
            // Load current settings
            const apiKey = localStorage.getItem('openaiApiKey') || '';
            document.getElementById('settingsApiKey').value = apiKey;
            
            // Set dark mode toggle state
            const isDark = document.documentElement.classList.contains('dark');
            document.getElementById('darkModeToggleSetting').checked = isDark;
            
            // Update state to remember we're in settings
            localStorage.setItem('inSettings', 'true');
        }

        // Hide settings page
        function hideSettingsPage() {
            document.getElementById('settingsPage').classList.add('hidden');
            document.body.classList.remove('overflow-hidden');
            localStorage.removeItem('inSettings');
        }

        // Check if we should show settings on load
        if (localStorage.getItem('inSettings') === 'true') {
            showSettingsPage();
        }

        // Toggle API key visibility
        document.getElementById('toggleApiKeyVisibility').addEventListener('click', function() {
            const input = document.getElementById('settingsApiKey');
            const icon = this.querySelector('i');
            
            if (input.type === 'password') {
                input.type = 'text';
                icon.classList.replace('fa-eye', 'fa-eye-slash');
            } else {
                input.type = 'password';
                icon.classList.replace('fa-eye-slash', 'fa-eye');
            }
        });

        // Save API key from settings
        document.getElementById('apiKeySettingsForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const key = document.getElementById('settingsApiKey').value.trim();
            saveApiKey(key);
            
            // Show success feedback
            const submitBtn = this.querySelector('button[type="submit"]');
            const originalText = submitBtn.textContent;
            submitBtn.textContent = 'Saved!';
            submitBtn.classList.remove('bg-primary-500', 'hover:bg-primary-600');
            submitBtn.classList.add('bg-green-500', 'hover:bg-green-600');
            
            setTimeout(() => {
                submitBtn.textContent = originalText;
                submitBtn.classList.remove('bg-green-500', 'hover:bg-green-600');
                submitBtn.classList.add('bg-primary-500', 'hover:bg-primary-600');
            }, 2000);
        });

        // Dark mode toggle in settings
        document.getElementById('darkModeToggleSetting').addEventListener('change', function() {
            toggleDarkMode();
        });

        // Handle message actions
        function handleMessageAction(action, messageIndex) {
            if (!currentChatId) return;
            
            const chat = chats.find(c => c.id === currentChatId);
            if (!chat || !chat.messages[messageIndex]) return;
            
            if (action === 'regenerate') {
                // Only regenerate if this is the last assistant message
                if (messageIndex === chat.messages.length - 1 && 
                    chat.messages[messageIndex].role === 'assistant') {
                    chat.messages.splice(messageIndex, 1);
                    renderMessages(chat.messages);
                    
                    // Resend last user message
                    const lastUserMessage = chat.messages[chat.messages.length - 1];
                    if (lastUserMessage && lastUserMessage.role === 'user') {
                        sendMessage(lastUserMessage.content);
                    }
                }
            } else if (action === 'edit') {
                // TODO: Implement edit functionality
                alert('Edit functionality will be implemented here');
            }
        }

        // Initialize the app
        init();

        // Add delegated event listener for message actions
        document.addEventListener('click', (e) => {
            const actionBtn = e.target.closest('.message-action');
            if (actionBtn) {
                e.preventDefault();
                e.stopPropagation();
                
                const messageElement = actionBtn.closest('.flex.gap-4');
                const messageIndex = Array.from(messageElement.parentNode.children).indexOf(messageElement);
                const action = actionBtn.dataset.action;
                
                handleMessageAction(action, messageIndex);
            }
        });
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=kokofixcomputers/nexuschat" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>