File size: 53,544 Bytes
0ee7bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>⚡ Keefe-Discere OS — Multimodal Any-to-Any Edge</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&family=Fira+Code:wght@400;500&display=swap" rel="stylesheet">
<script>
  tailwind.config = {
    theme: {
      extend: {
        fontFamily: {
          sans: ['Inter', 'ui-sans-serif', 'system-ui'],
          mono: ['JetBrains Mono', 'Fira Code', 'ui-monospace'],
        },
        colors: {
          keefe: {
            bg: '#090d16',
            panel: '#0d1420',
            border: '#1e293b',
            indigo: '#6366f1',
            cyan: '#06b6d4',
            emerald: '#10b981',
            amber: '#f59e0b',
            rose: '#f43f5e',
          }
        },
        animation: {
          'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite',
          'glow': 'glow 2s ease-in-out infinite alternate',
          'scan': 'scan 3s linear infinite',
          'blink': 'blink 1.2s steps(2, start) infinite',
        },
        keyframes: {
          glow: {
            '0%': { boxShadow: '0 0 10px rgba(99, 102, 241, 0.3)' },
            '100%': { boxShadow: '0 0 20px rgba(6, 182, 212, 0.5)' },
          },
          scan: {
            '0%': { transform: 'translateY(-100%)' },
            '100%': { transform: 'translateY(100%)' },
          },
          blink: {
            'to': { visibility: 'hidden' }
          }
        }
      }
    }
  }
</script>
<style>
  * { -webkit-tap-highlight-color: transparent; }
  html, body {
    background-color: #090d16;
    color: #e2e8f0;
    font-family: 'Inter', sans-serif;
    overflow: hidden;
  }
  /* Grid background pattern */
  .cyber-grid {
    background-image:
      linear-gradient(rgba(99, 102, 241, 0.04) 1px, transparent 1px),
      linear-gradient(90deg, rgba(99, 102, 241, 0.04) 1px, transparent 1px);
    background-size: 44px 44px;
    background-position: -1px -1px;
  }
  .cyber-grid::before {
    content: '';
    position: fixed;
    inset: 0;
    background: radial-gradient(ellipse at top, rgba(99, 102, 241, 0.12), transparent 50%),
                radial-gradient(ellipse at bottom, rgba(6, 182, 212, 0.08), transparent 50%);
    pointer-events: none;
    z-index: 0;
  }

  .glass {
    background: rgba(13, 20, 32, 0.75);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid rgba(30, 41, 59, 0.7);
  }

  .glass-strong {
    background: rgba(9, 13, 22, 0.85);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border: 1px solid rgba(30, 41, 59, 0.85);
  }

  /* Scrollbars */
  .thin-scroll::-webkit-scrollbar { width: 8px; height: 8px; }
  .thin-scroll::-webkit-scrollbar-track { background: transparent; }
  .thin-scroll::-webkit-scrollbar-thumb {
    background: rgba(99, 102, 241, 0.2);
    border-radius: 10px;
  }
  .thin-scroll::-webkit-scrollbar-thumb:hover {
    background: rgba(99, 102, 241, 0.4);
  }

  /* XML tag styles */
  .xml-block {
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.08), rgba(245, 158, 11, 0.03));
    border: 1px solid rgba(245, 158, 11, 0.25);
    border-left: 3px solid #f59e0b;
    border-radius: 10px;
    padding: 12px 14px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12.5px;
    color: #fcd34d;
    position: relative;
    overflow: hidden;
  }
  .xml-block::before {
    content: '⚡ TOOL_CALL';
    position: absolute;
    top: -8px;
    right: 10px;
    background: #090d16;
    padding: 0 6px;
    font-size: 9px;
    letter-spacing: 2px;
    color: #f59e0b;
    font-weight: 700;
  }
  .xml-tag { color: #fbbf24; font-weight: 500; }
  .xml-name { color: #fb923c; font-weight: 600; }
  .xml-input { color: #fde68a; }

  /* Message animations */
  @keyframes slideInUp {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
  }
  .msg-enter { animation: slideInUp 0.35s ease-out forwards; }

  /* Drop zone pulse */
  .drop-zone.drag-over {
    background: rgba(6, 182, 212, 0.08) !important;
    border-color: #06b6d4 !important;
    box-shadow: inset 0 0 0 2px rgba(6, 182, 212, 0.4), 0 0 30px rgba(6, 182, 212, 0.2);
  }
  .drop-zone.drag-over .drop-label { color: #06b6d4; }

  /* Blinking cursor */
  .cursor-blink::after {
    content: '▊';
    color: #06b6d4;
    animation: blink 1.2s steps(2, start) infinite;
    margin-left: 2px;
  }

  /* Status dot pulse */
  .status-dot {
    position: relative;
  }
  .status-dot::after {
    content: '';
    position: absolute;
    inset: -2px;
    border-radius: 999px;
    background: #10b981;
    animation: pulse-ring 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
    opacity: 0.6;
  }
  @keyframes pulse-ring {
    0%, 100% { transform: scale(1); opacity: 0.6; }
    50% { transform: scale(1.8); opacity: 0; }
  }

  /* Scan line effect for active tool calls */
  .scan-line {
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 100%;
    background: linear-gradient(180deg, transparent, rgba(6, 182, 212, 0.2), transparent);
    animation: scan 2.5s linear infinite;
    pointer-events: none;
  }

  /* Textarea auto resize */
  textarea { resize: none; }

  /* Button hover effects */
  .btn-primary {
    background: linear-gradient(135deg, #6366f1, #06b6d4);
    transition: all 0.2s ease;
  }
  .btn-primary:hover:not(:disabled) {
    box-shadow: 0 0 25px rgba(99, 102, 241, 0.5), 0 0 45px rgba(6, 182, 212, 0.3);
    transform: translateY(-1px);
  }
  .btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

  /* Modal backdrop */
  .modal-backdrop {
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
  }

  /* Gradient text */
  .gradient-text {
    background: linear-gradient(135deg, #a5b4fc 0%, #67e8f9 50%, #86efac 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  /* Code block for regular code */
  .code-block {
    background: rgba(2, 6, 23, 0.8);
    border: 1px solid rgba(30, 41, 59, 0.8);
    border-radius: 8px;
    padding: 12px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: #94a3b8;
    overflow-x: auto;
  }

  /* Tool result card */
  .tool-result {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.06), rgba(6, 182, 212, 0.03));
    border: 1px solid rgba(16, 185, 129, 0.25);
    border-left: 3px solid #10b981;
    border-radius: 10px;
    padding: 12px 14px;
  }
  .tool-result::before {
    content: '✓ TOOL_RESULT';
    display: block;
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 2px;
    color: #10b981;
    font-weight: 700;
    margin-bottom: 6px;
  }

  /* Error card */
  .error-block {
    background: linear-gradient(135deg, rgba(244, 63, 94, 0.08), rgba(244, 63, 94, 0.03));
    border: 1px solid rgba(244, 63, 94, 0.25);
    border-left: 3px solid #f43f5e;
    border-radius: 10px;
    padding: 12px 14px;
    color: #fca5a5;
  }

  /* Chip styles */
  .chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 500;
    background: rgba(30, 41, 59, 0.7);
    border: 1px solid rgba(51, 65, 85, 0.6);
    color: #cbd5e1;
  }

  /* Prevent text selection on UI */
  .no-select { user-select: none; }

  /* Loading dots */
  .loading-dots span {
    display: inline-block;
    width: 6px;
    height: 6px;
    margin: 0 2px;
    background: #06b6d4;
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
  }
  .loading-dots span:nth-child(1) { animation-delay: -0.32s; }
  .loading-dots span:nth-child(2) { animation-delay: -0.16s; }
  @keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
  }

  /* Focus ring */
  input:focus, textarea:focus {
    outline: none;
  }
  .focus-ring:focus {
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.4);
  }
</style>
</head>
<body class="cyber-grid h-screen w-screen relative">

<!-- ===== MAIN CONTAINER ===== -->
<div id="app" class="relative z-10 h-full w-full flex flex-col">

  <!-- ===== TOP BAR ===== -->
  <header class="glass-strong border-b border-slate-800/80 px-4 sm:px-6 py-3 flex items-center justify-between gap-4 flex-shrink-0">
    <div class="flex items-center gap-3 min-w-0">
      <div class="w-9 h-9 rounded-lg bg-gradient-to-br from-indigo-500 to-cyan-500 flex items-center justify-center flex-shrink-0 shadow-lg shadow-indigo-500/30">
        <i data-lucide="zap" class="w-5 h-5 text-white"></i>
      </div>
      <div class="min-w-0">
        <h1 class="text-sm sm:text-base font-bold gradient-text truncate">Keefe-Discere OS</h1>
        <div class="flex items-center gap-2 mt-0.5">
          <span class="status-dot w-1.5 h-1.5 rounded-full bg-emerald-400 inline-block"></span>
          <span class="text-[10px] text-slate-400 font-mono tracking-wide">Online • Static Edge Router</span>
        </div>
      </div>
    </div>

    <div class="flex items-center gap-2 flex-shrink-0">
      <div id="modelChip" class="chip hidden sm:inline-flex">
        <i data-lucide="cpu" class="w-3 h-3"></i>
        <span class="font-mono">KeefeBuild/Keefe-Discere</span>
      </div>
      <button id="tokenBtn" class="relative px-3 py-1.5 rounded-lg glass hover:bg-slate-800/80 transition-colors flex items-center gap-2 group">
        <i data-lucide="key-round" class="w-4 h-4 text-slate-400 group-hover:text-indigo-400"></i>
        <span class="text-xs font-medium text-slate-300 hidden sm:inline" id="tokenStatus">Set Token</span>
        <span id="tokenSavedBadge" class="hidden absolute -top-1 -right-1 w-2 h-2 rounded-full bg-emerald-400 ring-2 ring-keefe-bg"></span>
      </button>
      <button id="clearBtn" class="p-2 rounded-lg glass hover:bg-slate-800/80 transition-colors" title="Clear chat">
        <i data-lucide="trash-2" class="w-4 h-4 text-slate-400"></i>
      </button>
    </div>
  </header>

  <!-- ===== CHAT AREA ===== -->
  <main class="flex-1 flex flex-col min-h-0 relative">

    <!-- System Prompt Collapsible -->
    <div class="px-4 sm:px-6 pt-3 flex-shrink-0">
      <button id="systemPromptToggle" class="flex items-center gap-2 text-xs text-slate-400 hover:text-slate-200 transition-colors group">
        <i data-lucide="settings-2" class="w-3.5 h-3.5"></i>
        <span class="font-mono tracking-wide">SYSTEM_INSTRUCTIONS</span>
        <i data-lucide="chevron-down" id="sysPromptIcon" class="w-3.5 h-3.5 transition-transform"></i>
      </button>
      <div id="systemPromptPanel" class="hidden mt-2">
        <div class="glass rounded-xl p-3">
          <textarea id="systemPromptInput" rows="4"
            class="w-full bg-slate-950/60 text-slate-200 text-xs font-mono p-3 rounded-lg border border-slate-800 focus-ring"
            placeholder="Enter system prompt...">You are Keefe-Discere, an advanced multimodal AI operating system. You can reason across text, images, and audio inputs. When you need to use an external tool, you MUST emit a tool call in this exact format:

<tool_call><name>tool_name</name><input>input_data</input></tool_call>

Available tools:
- ocr_vision: Extract text from an image URL (input: image URL)
- transcribe_audio: Transcribe audio (input: audio URL or description)
- image_caption: Generate a detailed caption for an image (input: image URL)
- web_search: Search the web for information (input: query)
- image_generation: Generate an image from a text prompt (input: prompt)

Be concise, precise, and always acknowledge multimodal inputs. When routing to a tool, wait for its result before responding.</textarea>
          <div class="flex items-center justify-between mt-2">
            <span class="text-[10px] text-slate-500 font-mono">Tokens auto-injected into every request</span>
            <span class="text-[10px] text-emerald-400 font-mono">● LIVE</span>
          </div>
        </div>
      </div>
    </div>

    <!-- Message Stream -->
    <div id="messageStream" class="flex-1 overflow-y-auto thin-scroll px-4 sm:px-6 py-4 min-h-0">
      <div id="messageContainer" class="max-w-4xl mx-auto space-y-4">

        <!-- Welcome message -->
        <div id="welcomeScreen" class="flex flex-col items-center justify-center py-12 text-center">
          <div class="w-20 h-20 rounded-2xl bg-gradient-to-br from-indigo-500/20 to-cyan-500/20 border border-indigo-500/30 flex items-center justify-center mb-6 animate-glow">
            <i data-lucide="sparkles" class="w-10 h-10 text-indigo-400"></i>
          </div>
          <h2 class="text-2xl sm:text-3xl font-bold gradient-text mb-3">Welcome to Keefe-Discere OS</h2>
          <p class="text-slate-400 max-w-md text-sm sm:text-base leading-relaxed">
            Multimodal any-to-any intelligence router. Drop an image, attach audio, or just type — and watch XML tool calls flow through the edge in real-time.
          </p>
          <div class="flex flex-wrap gap-2 mt-6 justify-center">
            <div class="chip"><i data-lucide="image" class="w-3 h-3"></i>Vision</div>
            <div class="chip"><i data-lucide="mic" class="w-3 h-3"></i>Audio</div>
            <div class="chip"><i data-lucide="text" class="w-3 h-3"></i>Text</div>
            <div class="chip"><i data-lucide="route" class="w-3 h-3"></i>Tool Routing</div>
          </div>
          <div class="grid grid-cols-1 sm:grid-cols-2 gap-2 mt-8 max-w-xl w-full">
            <button class="suggestion-btn glass rounded-xl p-3 text-left hover:bg-slate-800/80 transition-colors group" data-prompt="Analyze this screenshot and tell me what UI patterns are used">
              <div class="flex items-center gap-2 mb-1">
                <i data-lucide="layout" class="w-3.5 h-3.5 text-indigo-400"></i>
                <span class="text-xs font-semibold text-slate-200">UI Analysis</span>
              </div>
              <p class="text-[11px] text-slate-400 line-clamp-2">Analyze a screenshot and identify UI patterns...</p>
            </button>
            <button class="suggestion-btn glass rounded-xl p-3 text-left hover:bg-slate-800/80 transition-colors group" data-prompt="Generate a cyberpunk logo for a startup called 'Nexora'">
              <div class="flex items-center gap-2 mb-1">
                <i data-lucide="palette" class="w-3.5 h-3.5 text-cyan-400"></i>
                <span class="text-xs font-semibold text-slate-200">Logo Generation</span>
              </div>
              <p class="text-[11px] text-slate-400 line-clamp-2">Design a logo with tool routing...</p>
            </button>
            <button class="suggestion-btn glass rounded-xl p-3 text-left hover:bg-slate-800/80 transition-colors group" data-prompt="What are the latest breakthroughs in multimodal AI this month?">
              <div class="flex items-center gap-2 mb-1">
                <i data-lucide="globe" class="w-3.5 h-3.5 text-emerald-400"></i>
                <span class="text-xs font-semibold text-slate-200">Web Research</span>
              </div>
              <p class="text-[11px] text-slate-400 line-clamp-2">Search for recent AI breakthroughs...</p>
            </button>
            <button class="suggestion-btn glass rounded-xl p-3 text-left hover:bg-slate-800/80 transition-colors group" data-prompt="Help me debug this error: TypeError cannot read property of undefined">
              <div class="flex items-center gap-2 mb-1">
                <i data-lucide="bug" class="w-3.5 h-3.5 text-amber-400"></i>
                <span class="text-xs font-semibold text-slate-200">Code Debug</span>
              </div>
              <p class="text-[11px] text-slate-400 line-clamp-2">Diagnose and resolve coding errors...</p>
            </button>
          </div>
        </div>

      </div>
    </div>

    <!-- Input Zone -->
    <div class="px-4 sm:px-6 pb-4 pt-2 flex-shrink-0">
      <div id="dropZone" class="drop-zone max-w-4xl mx-auto glass-strong rounded-2xl p-3 transition-all relative overflow-hidden">
        <!-- Attached files preview -->
        <div id="attachedFiles" class="hidden mb-2 flex flex-wrap gap-2"></div>

        <!-- Drop label (shown when empty & dragging) -->
        <div id="dropLabel" class="hidden text-center py-2">
          <i data-lucide="upload-cloud" class="w-6 h-6 text-cyan-400 mx-auto mb-1"></i>
          <p class="text-sm text-cyan-300 font-medium">Drop files here</p>
          <p class="text-xs text-slate-500">PNG, JPG, MP3, WAV supported</p>
        </div>

        <div class="flex items-end gap-2">
          <!-- Attach button -->
          <button id="attachBtn" class="p-2.5 rounded-xl bg-slate-800/80 hover:bg-slate-700/80 transition-colors flex-shrink-0 group relative">
            <i data-lucide="paperclip" class="w-4 h-4 text-slate-400 group-hover:text-cyan-400"></i>
            <input type="file" id="fileInput" class="absolute inset-0 opacity-0 cursor-pointer" accept="image/png,image/jpeg,image/jpg,image/webp,image/gif,audio/mpeg,audio/wav,audio/mp3,audio/ogg" multiple>
          </button>

          <!-- Textarea -->
          <div class="flex-1 min-w-0">
            <textarea id="userInput" rows="1"
              class="w-full bg-slate-950/60 text-slate-100 text-sm p-3 rounded-xl border border-slate-800 focus-ring transition-all placeholder:text-slate-600"
              placeholder="Message Keefe-Discere... (Enter to send, Shift+Enter for newline)"
              maxlength="8000"></textarea>
          </div>

          <!-- Send button -->
          <button id="sendBtn" class="btn-primary p-2.5 rounded-xl flex-shrink-0 flex items-center justify-center" title="Send (Enter)">
            <i data-lucide="send-horizontal" class="w-4 h-4 text-white"></i>
          </button>
        </div>

        <div class="flex items-center justify-between mt-2 px-1">
          <div class="flex items-center gap-3 text-[10px] text-slate-500 font-mono">
            <span class="flex items-center gap-1"><i data-lucide="command" class="w-2.5 h-2.5"></i>ENTER to send</span>
            <span class="flex items-center gap-1"><i data-lucide="shift" class="w-2.5 h-2.5"></i>+↵ for newline</span>
          </div>
          <div id="charCount" class="text-[10px] text-slate-500 font-mono">0 / 8000</div>
        </div>
      </div>
    </div>

  </main>

</div>

<!-- ===== TOKEN MODAL ===== -->
<div id="tokenModal" class="fixed inset-0 z-50 hidden items-center justify-center p-4 modal-backdrop">
  <div class="glass-strong rounded-2xl p-6 w-full max-w-md shadow-2xl border border-slate-800/80">
    <div class="flex items-center justify-between mb-4">
      <div>
        <h3 class="text-base font-bold gradient-text">HF Access Token</h3>
        <p class="text-xs text-slate-400 mt-0.5">Stored locally in your browser only</p>
      </div>
      <button id="closeTokenModal" class="p-1.5 rounded-lg hover:bg-slate-800/80 transition-colors">
        <i data-lucide="x" class="w-4 h-4 text-slate-400"></i>
      </button>
    </div>

    <div class="space-y-3">
      <div class="relative">
        <input type="password" id="tokenInput"
          class="w-full bg-slate-950/80 text-slate-100 text-sm font-mono p-3 pr-10 rounded-lg border border-slate-800 focus-ring"
          placeholder="hf_xxxxxxxxxxxxxxxxxxxxx" autocomplete="off">
        <button id="toggleTokenVis" class="absolute right-2 top-1/2 -translate-y-1/2 p-1.5 rounded hover:bg-slate-800/80 transition-colors">
          <i data-lucide="eye" id="eyeIcon" class="w-4 h-4 text-slate-400"></i>
        </button>
      </div>

      <div class="flex items-center justify-between text-xs">
        <a href="https://huggingface.co/settings/tokens" target="_blank" rel="noopener"
          class="text-indigo-400 hover:text-indigo-300 underline-offset-2 hover:underline flex items-center gap-1">
          <i data-lucide="external-link" class="w-3 h-3"></i> Get token at huggingface.co
        </a>
        <span id="tokenSavedStatus" class="text-emerald-400 hidden">✓ Saved</span>
      </div>

      <div class="flex gap-2 pt-2">
        <button id="saveTokenBtn" class="btn-primary flex-1 py-2.5 rounded-lg text-sm font-semibold text-white flex items-center justify-center gap-2">
          <i data-lucide="shield-check" class="w-4 h-4"></i> Save & Encrypt Locally
        </button>
        <button id="clearTokenBtn" class="px-4 py-2.5 rounded-lg bg-slate-800/80 hover:bg-slate-700/80 transition-colors text-sm text-slate-300">
          Clear
        </button>
      </div>

      <div class="bg-slate-950/50 rounded-lg p-3 border border-slate-800/80">
        <p class="text-[10px] text-slate-500 leading-relaxed font-mono">
          <i data-lucide="info" class="w-3 h-3 inline mr-1"></i>
          Token never leaves your browser except for direct HTTPS calls to the Hugging Face Inference API (api-inference.huggingface.co). No analytics, no tracking.
        </p>
      </div>
    </div>
  </div>
</div>

<!-- ===== TOAST CONTAINER ===== -->
<div id="toastContainer" class="fixed bottom-24 right-4 z-50 space-y-2 pointer-events-none"></div>

<script type="module">
import { InferenceClient } from "https://cdn.jsdelivr.net/npm/@huggingface/inference@4.13.22/+esm";

// ====================== STATE ======================
const state = {
  token: localStorage.getItem('keefe_hf_token') || '',
  model: 'KeefeBuild/Keefe-Discere',
  messages: [], // {role: 'user'|'assistant'|'system', content: string, files?: []}
  attachedFiles: [], // {file, dataUrl, type, name}
  isStreaming: false,
  toolCallDepth: 0,
  maxToolCallDepth: 3,
};

// ====================== DOM REFS ======================
const $ = (id) => document.getElementById(id);
const messageContainer = $('messageContainer');
const messageStream = $('messageStream');
const userInput = $('userInput');
const sendBtn = $('sendBtn');
const attachBtn = $('attachBtn');
const fileInput = $('fileInput');
const attachedFilesEl = $('attachedFiles');
const dropZone = $('dropZone');
const dropLabel = $('dropLabel');
const charCount = $('charCount');
const tokenModal = $('tokenModal');
const tokenInput = $('tokenInput');
const tokenBtn = $('tokenBtn');
const tokenStatus = $('tokenStatus');
const tokenSavedBadge = $('tokenSavedBadge');
const systemPromptToggle = $('systemPromptToggle');
const systemPromptPanel = $('systemPromptPanel');
const systemPromptInput = $('systemPromptInput');
const sysPromptIcon = $('sysPromptIcon');
const clearBtn = $('clearBtn');
const welcomeScreen = $('welcomeScreen');

// ====================== UTILITIES ======================
function escapeHtml(str) {
  return String(str)
    .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;').replace(/'/g, '&#039;');
}

function showToast(msg, type = 'info', duration = 3000) {
  const colors = {
    info: 'border-indigo-500/50 bg-indigo-500/10 text-indigo-200',
    success: 'border-emerald-500/50 bg-emerald-500/10 text-emerald-200',
    error: 'border-rose-500/50 bg-rose-500/10 text-rose-200',
    warning: 'border-amber-500/50 bg-amber-500/10 text-amber-200',
  };
  const icons = {
    info: 'info', success: 'check-circle-2', error: 'alert-circle', warning: 'alert-triangle'
  };
  const toast = document.createElement('div');
  toast.className = `glass-strong rounded-lg px-4 py-3 border ${colors[type]} shadow-xl pointer-events-auto max-w-sm msg-enter flex items-start gap-3`;
  toast.innerHTML = `
    <i data-lucide="${icons[type]}" class="w-4 h-4 mt-0.5 flex-shrink-0"></i>
    <span class="text-xs leading-relaxed">${escapeHtml(msg)}</span>
  `;
  $('toastContainer').appendChild(toast);
  lucide.createIcons({ nodes: [toast] });
  setTimeout(() => {
    toast.style.transition = 'opacity 0.3s, transform 0.3s';
    toast.style.opacity = '0';
    toast.style.transform = 'translateX(20px)';
    setTimeout(() => toast.remove(), 300);
  }, duration);
}

function scrollToBottom() {
  requestAnimationFrame(() => {
    messageStream.scrollTop = messageStream.scrollHeight;
  });
}

function autoResizeTextarea() {
  userInput.style.height = 'auto';
  userInput.style.height = Math.min(userInput.scrollHeight, 180) + 'px';
}

// ====================== MARKDOWN / XML RENDERER ======================
function renderContent(text) {
  if (!text) return '';
  let html = escapeHtml(text);

  // Code blocks ```lang\ncode```
  html = html.replace(/```(\w+)?\n?([\s\S]*?)```/g, (m, lang, code) => {
    return `<pre class="code-block my-2"><code class="language-${lang || 'text'}">${code.trim()}</code></pre>`;
  });

  // Inline code
  html = html.replace(/`([^`]+)`/g, '<code class="bg-slate-800/80 px-1.5 py-0.5 rounded text-xs text-cyan-300 font-mono">$1</code>');

  // Bold
  html = html.replace(/\*\*(.+?)\*\*/g, '<strong class="font-semibold text-slate-100">$1</strong>');

  // Italic
  html = html.replace(/(?<!\*)\*([^*\n]+)\*(?!\*)/g, '<em>$1</em>');

  // Headings
  html = html.replace(/^### (.+)$/gm, '<h3 class="font-bold text-base mt-3 mb-1 text-slate-100">$1</h3>');
  html = html.replace(/^## (.+)$/gm, '<h2 class="font-bold text-lg mt-3 mb-1 text-slate-100">$1</h2>');
  html = html.replace(/^# (.+)$/gm, '<h1 class="font-bold text-xl mt-3 mb-2 text-slate-100">$1</h1>');

  // Lists
  html = html.replace(/^- (.+)$/gm, '<li class="ml-4 list-disc text-slate-300">$1</li>');
  html = html.replace(/^(\d+)\. (.+)$/gm, '<li class="ml-4 list-decimal text-slate-300">$2</li>');
  html = html.replace(/(<li[^>]*>.*?<\/li>\n?)+/g, (m) => `<ul class="my-2 space-y-0.5">${m}</ul>`);

  // Links
  html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank" rel="noopener" class="text-cyan-400 hover:text-cyan-300 underline underline-offset-2">$1</a>');

  // Bare URLs
  html = html.replace(/(?<!\()https?:\/\/[^\s<)]+/g, (url) => {
    if (url.includes('class="') || url.includes('href="')) return url;
    return `<a href="${url}" target="_blank" rel="noopener" class="text-cyan-400 hover:text-cyan-300 break-all">${url}</a>`;
  });

  // Tool calls: extract <tool_call>...</tool_call> blocks
  html = html.replace(/&lt;tool_call&gt;([\s\S]*?)&lt;\/tool_call&gt;/g, (m, inner) => {
    // Parse name & input
    const nameMatch = inner.match(/&lt;name&gt;([\s\S]*?)&lt;\/name&gt;/);
    const inputMatch = inner.match(/&lt;input&gt;([\s\S]*?)&lt;\/input&gt;/);
    const name = nameMatch ? nameMatch[1] : 'unknown';
    const input = inputMatch ? inputMatch[1] : '';
    const icon = getToolIcon(name);
    return `
      <div class="xml-block my-3">
        <div class="flex items-center gap-2 mb-2">
          <i data-lucide="${icon}" class="w-3.5 h-3.5 text-amber-400"></i>
          <span class="text-[10px] font-mono tracking-wider text-amber-300 uppercase">${escapeHtml(name)}</span>
        </div>
        <div class="flex flex-col gap-1 text-[11.5px]">
          <div><span class="xml-tag">&lt;name&gt;</span><span class="xml-name">${escapeHtml(name)}</span><span class="xml-tag">&lt;/name&gt;</span></div>
          <div class="break-all"><span class="xml-tag">&lt;input&gt;</span><span class="xml-input">${escapeHtml(input)}</span><span class="xml-tag">&lt;/input&gt;</span></div>
        </div>
      </div>
    `;
  });

  // Standalone XML-ish tags that weren't tool_calls
  html = html.replace(/&lt;(\/?[a-zA-Z_][\w\-]*)&gt;/g, '<span class="xml-tag">&lt;$1&gt;</span>');

  // Paragraphs / newlines
  html = html.split('\n\n').map(p => {
    if (p.includes('<pre') || p.includes('<div class="xml-block"') || p.includes('<h') || p.includes('<ul')) return p;
    return `<p class="mb-2 leading-relaxed text-slate-200">${p.replace(/\n/g, '<br>')}</p>`;
  }).join('');

  return html;
}

function getToolIcon(name) {
  const icons = {
    ocr_vision: 'scan-eye',
    transcribe_audio: 'audio-waveform',
    image_caption: 'image-text',
    web_search: 'globe',
    image_generation: 'image-plus',
    code_execute: 'terminal',
    math: 'sigma',
  };
  return icons[name] || 'wrench';
}

// ====================== MESSAGE RENDERING ======================
function addMessage(role, content, files = [], opts = {}) {
  if (welcomeScreen && !welcomeScreen.classList.contains('hidden')) {
    welcomeScreen.classList.add('hidden');
  }

  const msgId = 'msg_' + Date.now() + '_' + Math.random().toString(36).slice(2, 7);
  const msg = { id: msgId, role, content, files, timestamp: new Date() };
  state.messages.push(msg);

  const wrapper = document.createElement('div');
  wrapper.id = msgId;
  wrapper.className = `msg-enter flex gap-3 ${role === 'user' ? 'justify-end' : ''}`;

  if (role === 'user') {
    wrapper.innerHTML = `
      <div class="max-w-[85%] sm:max-w-[75%]">
        ${files.length ? renderUserFiles(files) : ''}
        <div class="bg-gradient-to-br from-indigo-600/90 to-indigo-700/90 rounded-2xl rounded-tr-sm px-4 py-2.5 shadow-lg shadow-indigo-900/30">
          <div class="text-sm text-slate-50 leading-relaxed whitespace-pre-wrap break-words">${escapeHtml(content)}</div>
        </div>
        <div class="text-[10px] text-slate-500 font-mono mt-1 text-right">
          <i data-lucide="user" class="w-2.5 h-2.5 inline"></i> YOU • ${formatTime(msg.timestamp)}
        </div>
      </div>
    `;
  } else if (role === 'system-log') {
    wrapper.className = 'msg-enter flex justify-center';
    wrapper.innerHTML = `
      <div class="inline-flex items-center gap-2 px-3 py-1.5 rounded-full bg-slate-800/60 border border-slate-700/60">
        <i data-lucide="${opts.icon || 'info'}" class="w-3 h-3 text-slate-400"></i>
        <span class="text-[11px] font-mono text-slate-400">${escapeHtml(content)}</span>
      </div>
    `;
  } else if (role === 'tool-call') {
    wrapper.className = 'msg-enter flex justify-center';
    wrapper.innerHTML = `
      <div class="glass rounded-xl px-4 py-2.5 border-amber-500/40 shadow-lg max-w-md relative overflow-hidden">
        <div class="scan-line"></div>
        <div class="relative flex items-center gap-3">
          <div class="w-7 h-7 rounded-lg bg-amber-500/20 border border-amber-500/40 flex items-center justify-center">
            <i data-lucide="${opts.icon || 'route'}" class="w-3.5 h-3.5 text-amber-400"></i>
          </div>
          <div>
            <div class="text-[10px] font-mono tracking-wider text-amber-300 uppercase">Cloud Action Badge</div>
            <div class="text-xs text-slate-200">${escapeHtml(content)}</div>
          </div>
          <div class="loading-dots"><span></span><span></span><span></span></div>
        </div>
      </div>
    `;
  } else if (role === 'tool-result') {
    wrapper.innerHTML = `
      <div class="w-full max-w-3xl">
        <div class="tool-result">
          <div class="flex items-center gap-2 mb-2">
            <i data-lucide="${opts.icon || 'check'}" class="w-3.5 h-3.5 text-emerald-400"></i>
            <span class="text-[10px] font-mono tracking-wider text-emerald-300 uppercase">${escapeHtml(opts.toolName || 'RESULT')}</span>
            <span class="text-[10px] text-slate-500 font-mono ml-auto">${formatTime(msg.timestamp)}</span>
          </div>
          <div class="text-xs text-slate-300 whitespace-pre-wrap break-words leading-relaxed">${opts.html ? opts.html : escapeHtml(content)}</div>
        </div>
      </div>
    `;
  } else {
    // Assistant
    wrapper.innerHTML = `
      <div class="w-full max-w-3xl">
        <div class="flex items-center gap-2 mb-2">
          <div class="w-6 h-6 rounded-lg bg-gradient-to-br from-indigo-500 to-cyan-500 flex items-center justify-center">
            <i data-lucide="zap" class="w-3 h-3 text-white"></i>
          </div>
          <span class="text-xs font-semibold text-slate-300">Keefe-Discere</span>
          <span class="text-[10px] text-slate-500 font-mono">• ${formatTime(msg.timestamp)}</span>
        </div>
        <div class="glass rounded-2xl rounded-tl-sm px-4 py-3 text-sm leading-relaxed break-words assistant-content">
          ${opts.html || renderContent(content)}
        </div>
      </div>
    `;
  }

  messageContainer.appendChild(wrapper);
  lucide.createIcons({ nodes: [wrapper] });
  scrollToBottom();
  return msgId;
}

function renderUserFiles(files) {
  return `<div class="flex flex-wrap gap-2 mb-2 justify-end">
    ${files.map(f => {
      if (f.type.startsWith('image/')) {
        return `<img src="${f.dataUrl}" alt="${escapeHtml(f.name)}" class="h-24 w-24 object-cover rounded-lg border border-slate-700">`;
      } else if (f.type.startsWith('audio/')) {
        return `<div class="flex items-center gap-2 px-3 py-2 rounded-lg bg-slate-800/80 border border-slate-700">
          <i data-lucide="audio-waveform" class="w-4 h-4 text-cyan-400"></i>
          <span class="text-xs text-slate-300">${escapeHtml(f.name)}</span>
        </div>`;
      }
      return '';
    }).join('')}
  </div>`;
}

function updateAssistantMessage(msgId, content) {
  const el = document.getElementById(msgId);
  if (!el) return;
  const contentEl = el.querySelector('.assistant-content');
  if (!contentEl) return;
  contentEl.innerHTML = renderContent(content);
  lucide.createIcons({ nodes: [contentEl] });
  scrollToBottom();
}

function removeMessage(msgId) {
  const el = document.getElementById(msgId);
  if (el) el.remove();
  state.messages = state.messages.filter(m => m.id !== msgId);
}

function formatTime(d) {
  const date = new Date(d);
  return date.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false });
}

// ====================== FILE HANDLING ======================
function readFileAsDataURL(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => resolve(reader.result);
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
}

async function handleFiles(fileList) {
  const files = Array.from(fileList);
  for (const file of files) {
    if (!file.type.startsWith('image/') && !file.type.startsWith('audio/')) {
      showToast(`Unsupported file type: ${file.type}`, 'error');
      continue;
    }
    if (file.size > 20 * 1024 * 1024) {
      showToast(`File too large (max 20MB): ${file.name}`, 'error');
      continue;
    }
    try {
      const dataUrl = await readFileAsDataURL(file);
      state.attachedFiles.push({ file, dataUrl, type: file.type, name: file.name });
    } catch (err) {
      console.error(err);
      showToast(`Failed to read: ${file.name}`, 'error');
    }
  }
  renderAttachedFiles();
}

function renderAttachedFiles() {
  if (state.attachedFiles.length === 0) {
    attachedFilesEl.classList.add('hidden');
    attachedFilesEl.innerHTML = '';
    return;
  }
  attachedFilesEl.classList.remove('hidden');
  attachedFilesEl.innerHTML = state.attachedFiles.map((f, idx) => {
    if (f.type.startsWith('image/')) {
      return `<div class="relative group">
        <img src="${f.dataUrl}" alt="${escapeHtml(f.name)}" class="h-16 w-16 object-cover rounded-lg border border-slate-700">
        <button class="remove-file absolute -top-1.5 -right-1.5 w-5 h-5 rounded-full bg-rose-500 hover:bg-rose-600 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity" data-idx="${idx}">
          <i data-lucide="x" class="w-3 h-3 text-white"></i>
        </button>
        <div class="text-[9px] text-slate-400 mt-0.5 truncate max-w-[64px] font-mono">${escapeHtml(f.name)}</div>
      </div>`;
    } else {
      return `<div class="relative group flex items-center gap-2 px-3 py-2 rounded-lg bg-slate-800/80 border border-slate-700">
        <i data-lucide="audio-waveform" class="w-4 h-4 text-cyan-400"></i>
        <span class="text-xs text-slate-300 max-w-[120px] truncate">${escapeHtml(f.name)}</span>
        <button class="remove-file p-1 rounded hover:bg-slate-700/80" data-idx="${idx}">
          <i data-lucide="x" class="w-3 h-3 text-slate-400"></i>
        </button>
      </div>`;
    }
  }).join('');

  attachedFilesEl.querySelectorAll('.remove-file').forEach(btn => {
    btn.addEventListener('click', (e) => {
      const idx = parseInt(btn.dataset.idx);
      state.attachedFiles.splice(idx, 1);
      renderAttachedFiles();
    });
  });

  lucide.createIcons({ nodes: [attachedFilesEl] });
}

// ====================== XML TOOL PARSER ======================
function parseToolCalls(text) {
  const calls = [];
  const regex = /<tool_call>\s*<name>([\s\S]*?)<\/name>\s*<input>([\s\S]*?)<\/input>\s*<\/tool_call>/g;
  let match;
  while ((match = regex.exec(text)) !== null) {
    calls.push({
      full: match[0],
      name: match[1].trim(),
      input: match[2].trim(),
    });
  }
  return calls;
}

// ====================== TOOL EXECUTION ======================
async function executeTool(name, input) {
  if (!state.token) {
    throw new Error('HF token required to execute tool');
  }
  const client = new InferenceClient(state.token);

  const callMsgId = addMessage('tool-call', `👁️ Routing to HF Inference • ${name}`, [], {
    icon: getToolIcon(name)
  });

  try {
    let result = '';
    let resultHtml = '';

    if (name === 'ocr_vision' || name === 'image_caption') {
      // Image-to-text
      let imageUrl = input.trim();
      // If input is not a URL, try to use first attached image
      if (!imageUrl.match(/^https?:\/\//)) {
        const attachedImage = state.attachedFiles.find(f => f.type.startsWith('image/'));
        if (attachedImage) {
          imageUrl = attachedImage.dataUrl;
        } else {
          throw new Error('No image URL provided and no image attached');
        }
      }

      const caption = await client.imageToText({
        model: name === 'ocr_vision' ? 'Salesforce/blip-image-captioning-large' : 'Salesforce/blip-image-captioning-large',
        data: imageUrl,
      });
      result = Array.isArray(caption) ? caption.map(c => c.generated_text).join('\n') : (caption?.generated_text || String(caption));
      resultHtml = `
        <div class="flex items-start gap-3">
          <img src="${imageUrl.startsWith('data:') ? imageUrl : imageUrl}" alt="input" class="w-20 h-20 rounded-lg object-cover border border-slate-700 flex-shrink-0" onerror="this.style.display='none'">
          <div class="flex-1 min-w-0">
            <div class="text-[10px] font-mono text-emerald-300 mb-1 uppercase tracking-wider">Caption Output</div>
            <div class="text-slate-200">${escapeHtml(result)}</div>
          </div>
        </div>`;
    }
    else if (name === 'transcribe_audio') {
      let audioData = input.trim();
      const attachedAudio = state.attachedFiles.find(f => f.type.startsWith('audio/'));
      if (attachedAudio) {
        // Convert dataUrl to Blob
        const res = await fetch(attachedAudio.dataUrl);
        audioData = await res.blob();
      } else if (audioData.match(/^https?:\/\//)) {
        const res = await fetch(audioData);
        audioData = await res.blob();
      } else {
        throw new Error('No audio URL provided and no audio attached');
      }
      const transcript = await client.automaticSpeechRecognition({
        model: 'openai/whisper-large-v3',
        data: audioData,
      });
      result = transcript?.text || String(transcript);
      resultHtml = `
        <div>
          <div class="text-[10px] font-mono text-emerald-300 mb-1 uppercase tracking-wider">Transcript</div>
          <div class="text-slate-200 leading-relaxed">${escapeHtml(result)}</div>
        </div>`;
    }
    else if (name === 'web_search') {
      // Simulated web search result (no actual external search tool, but we can provide a helpful response)
      result = `Searched for: "${input}"\n\n[Simulated search results would appear here. In production, this would route to a search API or use a web-grounded model.]`;
      resultHtml = `
        <div>
          <div class="text-[10px] font-mono text-emerald-300 mb-1 uppercase tracking-wider">Query: ${escapeHtml(input)}</div>
          <div class="text-slate-200 leading-relaxed text-[11px] font-mono opacity-80">${escapeHtml(result)}</div>
        </div>`;
    }
    else if (name === 'image_generation') {
      const blob = await client.textToImage({
        model: 'black-forest-labs/FLUX.1-schnell',
        inputs: input,
        parameters: { num_inference_steps: 25 }
      });
      const imgUrl = URL.createObjectURL(blob);
      result = `Generated image for: "${input}"`;
      resultHtml = `
        <div>
          <div class="text-[10px] font-mono text-emerald-300 mb-2 uppercase tracking-wider">Generated Image</div>
          <img src="${imgUrl}" alt="${escapeHtml(input)}" class="max-w-full rounded-lg border border-slate-700 shadow-lg">
          <div class="text-[10px] text-slate-400 mt-1 font-mono">Prompt: ${escapeHtml(input)}</div>
        </div>`;
    }
    else {
      throw new Error(`Unknown tool: ${name}`);
    }

    // Remove the "routing" message and add result
    removeMessage(callMsgId);
    addMessage('tool-result', result, [], {
      html: resultHtml,
      toolName: name,
      icon: getToolIcon(name)
    });

    return result;
  } catch (err) {
    removeMessage(callMsgId);
    addMessage('system-log', `Tool error: ${err.message}`, [], { icon: 'alert-triangle' });
    throw err;
  }
}

// ====================== MAIN SEND / STREAM ======================
async function sendMessage() {
  const text = userInput.value.trim();
  if (!text && state.attachedFiles.length === 0) return;
  if (state.isStreaming) return;

  if (!state.token) {
    openTokenModal();
    showToast('Please set your HF access token first', 'warning');
    return;
  }

  state.isStreaming = true;
  sendBtn.disabled = true;
  sendBtn.innerHTML = '<div class="loading-dots"><span></span><span></span><span></span></div>';

  const files = [...state.attachedFiles];
  state.attachedFiles = [];
  renderAttachedFiles();
  userInput.value = '';
  autoResizeTextarea();
  updateCharCount();

  // Add user message
  addMessage('user', text || '(multimodal input)', files);

  try {
    await runAssistantLoop(text, files);
  } catch (err) {
    console.error(err);
    addMessage('system-log', `Error: ${err.message}`, [], { icon: 'alert-circle' });
    showToast(err.message, 'error');
  } finally {
    state.isStreaming = false;
    sendBtn.disabled = false;
    sendBtn.innerHTML = '<i data-lucide="send-horizontal" class="w-4 h-4 text-white"></i>';
    lucide.createIcons({ nodes: [sendBtn] });
  }
}

async function runAssistantLoop(userText, files) {
  // Build messages array for HF API
  const systemPrompt = systemPromptInput.value.trim();
  const apiMessages = [];
  if (systemPrompt) {
    apiMessages.push({ role: 'system', content: systemPrompt });
  }

  // Add recent chat history (last 10 turns)
  const history = state.messages.slice(-10).filter(m => m.role === 'user' || m.role === 'assistant');
  for (const m of history) {
    if (m.role === 'user') {
      if (m.files && m.files.length > 0) {
        const content = [];
        if (m.content && m.content !== '(multimodal input)') {
          content.push({ type: 'text', text: m.content });
        }
        for (const f of m.files) {
          if (f.type.startsWith('image/')) {
            content.push({ type: 'image_url', image_url: { url: f.dataUrl } });
          } else if (f.type.startsWith('audio/')) {
            content.push({ type: 'text', text: `[Audio attachment: ${f.name}]` });
          }
        }
        apiMessages.push({ role: 'user', content });
      } else {
        apiMessages.push({ role: 'user', content: m.content });
      }
    } else {
      apiMessages.push({ role: 'assistant', content: m.content });
    }
  }

  const assistantMsgId = addMessage('assistant', '', [], {
    html: '<div class="text-slate-400 flex items-center gap-2"><div class="loading-dots"><span></span><span></span><span></span></div><span class="text-xs font-mono">Reasoning...</span></div>'
  });

  const client = new InferenceClient(state.token);
  let fullResponse = '';

  try {
    // Try streaming first
    const stream = client.chatCompletionStream({
      model: state.model,
      messages: apiMessages,
      temperature: 0.7,
      max_tokens: 2048,
      stream: true,
    });

    let firstChunk = true;
    for await (const chunk of stream) {
      const delta = chunk.choices?.[0]?.delta?.content || '';
      if (delta) {
        if (firstChunk) {
          fullResponse = delta;
          firstChunk = false;
        } else {
          fullResponse += delta;
        }
        updateAssistantMessage(assistantMsgId, fullResponse);
      }
    }
  } catch (streamErr) {
    console.warn('Streaming failed, falling back to non-streaming:', streamErr.message);
    // Fallback to non-streaming
    try {
      const result = await client.chatCompletion({
        model: state.model,
        messages: apiMessages,
        temperature: 0.7,
        max_tokens: 2048,
      });
      fullResponse = result.choices?.[0]?.message?.content || '';
      updateAssistantMessage(assistantMsgId, fullResponse);
    } catch (fallbackErr) {
      removeMessage(assistantMsgId);
      throw fallbackErr;
    }
  }

  // Parse & execute any tool calls in the response
  const toolCalls = parseToolCalls(fullResponse);
  if (toolCalls.length > 0 && state.toolCallDepth < state.maxToolCallDepth) {
    state.toolCallDepth++;
    const toolResults = [];
    for (const call of toolCalls) {
      try {
        const result = await executeTool(call.name, call.input);
        toolResults.push({ name: call.name, input: call.input, result });
      } catch (err) {
        toolResults.push({ name: call.name, input: call.input, error: err.message });
      }
    }

    // Follow-up turn with tool results
    if (toolResults.length > 0) {
      const followUpMessages = [
        ...apiMessages,
        { role: 'assistant', content: fullResponse },
        {
          role: 'user',
          content: `[SYSTEM] Tool execution complete. Here are the results:\n\n${toolResults.map(r =>
            `--- ${r.name} ---\nInput: ${r.input}\nResult: ${r.error ? `ERROR: ${r.error}` : r.result}\n`
          ).join('\n')}\n\nPlease respond to the user incorporating these tool results. Do NOT emit any new <tool_call> tags unless absolutely necessary.`
        }
      ];

      const followUpMsgId = addMessage('assistant', '', [], {
        html: '<div class="text-slate-400 flex items-center gap-2"><div class="loading-dots"><span></span><span></span><span></span></div><span class="text-xs font-mono">Synthesizing tool results...</span></div>'
      });

      let followUpResponse = '';
      try {
        const followStream = client.chatCompletionStream({
          model: state.model,
          messages: followUpMessages,
          temperature: 0.5,
          max_tokens: 2048,
          stream: true,
        });

        let first = true;
        for await (const chunk of followStream) {
          const delta = chunk.choices?.[0]?.delta?.content || '';
          if (delta) {
            followUpResponse += delta;
            updateAssistantMessage(followUpMsgId, followUpResponse);
          }
        }
      } catch (followErr) {
        console.warn('Follow-up stream failed:', followErr.message);
        try {
          const result = await client.chatCompletion({
            model: state.model,
            messages: followUpMessages,
            temperature: 0.5,
            max_tokens: 2048,
          });
          followUpResponse = result.choices?.[0]?.message?.content || '';
          updateAssistantMessage(followUpMsgId, followUpResponse);
        } catch (e) {
          removeMessage(followUpMsgId);
          addMessage('system-log', `Follow-up failed: ${e.message}`, [], { icon: 'alert-circle' });
        }
      }
    }
    state.toolCallDepth = 0;
  }
}

// ====================== EVENT LISTENERS ======================
userInput.addEventListener('input', () => {
  autoResizeTextarea();
  updateCharCount();
});

userInput.addEventListener('keydown', (e) => {
  if (e.key === 'Enter' && !e.shiftKey) {
    e.preventDefault();
    sendMessage();
  }
});

sendBtn.addEventListener('click', sendMessage);

function updateCharCount() {
  const len = userInput.value.length;
  charCount.textContent = `${len} / 8000`;
  charCount.classList.toggle('text-amber-400', len > 6000);
  charCount.classList.toggle('text-rose-400', len > 7500);
}

fileInput.addEventListener('change', (e) => {
  handleFiles(e.target.files);
  e.target.value = '';
});

// Drag and drop
let dragCounter = 0;
['dragenter', 'dragover'].forEach(evt => {
  dropZone.addEventListener(evt, (e) => {
    e.preventDefault();
    e.stopPropagation();
    dragCounter++;
    dropZone.classList.add('drag-over');
    if (state.attachedFiles.length === 0) {
      dropLabel.classList.remove('hidden');
    }
  });
});
['dragleave', 'drop'].forEach(evt => {
  dropZone.addEventListener(evt, (e) => {
    e.preventDefault();
    e.stopPropagation();
    dragCounter--;
    if (dragCounter <= 0) {
      dragCounter = 0;
      dropZone.classList.remove('drag-over');
      dropLabel.classList.add('hidden');
    }
  });
});
dropZone.addEventListener('drop', (e) => {
  if (e.dataTransfer?.files?.length) {
    handleFiles(e.dataTransfer.files);
  }
});

// Paste images
userInput.addEventListener('paste', (e) => {
  const items = e.clipboardData?.items;
  if (!items) return;
  const files = [];
  for (const item of items) {
    if (item.kind === 'file') {
      const file = item.getAsFile();
      if (file) files.push(file);
    }
  }
  if (files.length > 0) {
    e.preventDefault();
    handleFiles(files);
  }
});

// System prompt toggle
systemPromptToggle.addEventListener('click', () => {
  systemPromptPanel.classList.toggle('hidden');
  sysPromptIcon.classList.toggle('rotate-180');
});

// Clear chat
clearBtn.addEventListener('click', () => {
  if (state.messages.length === 0) return;
  if (confirm('Clear all messages?')) {
    state.messages = [];
    messageContainer.innerHTML = '';
    welcomeScreen.classList.remove('hidden');
    messageContainer.appendChild(welcomeScreen);
    showToast('Chat cleared', 'info');
  }
});

// Suggestion buttons
document.addEventListener('click', (e) => {
  const btn = e.target.closest('.suggestion-btn');
  if (btn) {
    userInput.value = btn.dataset.prompt || '';
    userInput.focus();
    autoResizeTextarea();
    updateCharCount();
  }
});

// ====================== TOKEN MODAL ======================
function openTokenModal() {
  tokenModal.classList.remove('hidden');
  tokenModal.classList.add('flex');
  tokenInput.value = state.token;
  tokenInput.focus();
  lucide.createIcons();
}
function closeTokenModal() {
  tokenModal.classList.add('hidden');
  tokenModal.classList.remove('flex');
}

tokenBtn.addEventListener('click', openTokenModal);
$('closeTokenModal').addEventListener('click', closeTokenModal);
tokenModal.addEventListener('click', (e) => {
  if (e.target === tokenModal) closeTokenModal();
});

$('toggleTokenVis').addEventListener('click', () => {
  const isPw = tokenInput.type === 'password';
  tokenInput.type = isPw ? 'text' : 'password';
  const icon = $('eyeIcon');
  icon.setAttribute('data-lucide', isPw ? 'eye-off' : 'eye');
  lucide.createIcons({ nodes: [icon.parentElement] });
});

$('saveTokenBtn').addEventListener('click', () => {
  const val = tokenInput.value.trim();
  if (!val) {
    showToast('Token cannot be empty', 'error');
    return;
  }
  state.token = val;
  localStorage.setItem('keefe_hf_token', val);
  updateTokenStatus();
  showToast('Token saved locally', 'success');
  closeTokenModal();
});

$('clearTokenBtn').addEventListener('click', () => {
  if (confirm('Remove stored token?')) {
    state.token = '';
    localStorage.removeItem('keefe_hf_token');
    tokenInput.value = '';
    updateTokenStatus();
    showToast('Token cleared', 'info');
  }
});

tokenInput.addEventListener('keydown', (e) => {
  if (e.key === 'Enter') {
    e.preventDefault();
    $('saveTokenBtn').click();
  }
});

function updateTokenStatus() {
  if (state.token) {
    tokenStatus.textContent = 'Token Active';
    tokenStatus.classList.remove('text-slate-300');
    tokenStatus.classList.add('text-emerald-400');
    tokenSavedBadge.classList.remove('hidden');
  } else {
    tokenStatus.textContent = 'Set Token';
    tokenStatus.classList.add('text-slate-300');
    tokenStatus.classList.remove('text-emerald-400');
    tokenSavedBadge.classList.add('hidden');
  }
}

// ====================== INIT ======================
updateTokenStatus();
updateCharCount();
lucide.createIcons();

// Welcome token prompt
if (!state.token) {
  setTimeout(() => {
    showToast('Set your Hugging Face token to begin', 'info', 5000);
  }, 800);
}

// Global keyboard shortcut
document.addEventListener('keydown', (e) => {
  // Cmd/Ctrl+K to focus input
  if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
    e.preventDefault();
    userInput.focus();
  }
  // Escape to close modal
  if (e.key === 'Escape') {
    if (!tokenModal.classList.contains('hidden')) closeTokenModal();
  }
});

// Expose for debugging
window.keefe = { state, sendMessage };

</script>
</body>
</html>