File size: 78,704 Bytes
5f216b2
 
 
 
1fc5bd0
 
20b03c2
 
5f216b2
 
20b03c2
 
5f216b2
cedb3a9
20b03c2
5f216b2
 
 
 
 
 
20b03c2
5f216b2
 
1fc5bd0
 
 
 
 
 
 
 
d87a207
 
20b03c2
 
 
 
 
 
 
 
 
 
8862a62
20b03c2
1fc5bd0
 
 
8862a62
5f216b2
 
 
 
20b03c2
5f216b2
20b03c2
 
1fc5bd0
20b03c2
 
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
20b03c2
5f216b2
20b03c2
 
d87a207
20b03c2
1fc5bd0
 
 
5f216b2
20b03c2
1fc5bd0
20b03c2
 
 
5f216b2
20b03c2
 
 
 
 
 
 
1fc5bd0
5f216b2
20b03c2
2e1a881
5f216b2
20b03c2
 
8862a62
20b03c2
 
 
 
1fc5bd0
20b03c2
8862a62
1fc5bd0
20b03c2
 
 
 
 
 
1fc5bd0
20b03c2
 
1fc5bd0
20b03c2
1fc5bd0
20b03c2
 
 
1fc5bd0
 
20b03c2
 
 
 
 
 
1fc5bd0
20b03c2
 
 
 
1fc5bd0
 
 
20b03c2
1fc5bd0
 
20b03c2
 
1fc5bd0
20b03c2
 
 
 
 
1fc5bd0
 
 
20b03c2
1fc5bd0
20b03c2
 
 
1fc5bd0
 
20b03c2
 
 
 
1fc5bd0
20b03c2
 
 
 
1fc5bd0
 
 
20b03c2
1fc5bd0
20b03c2
1fc5bd0
20b03c2
1fc5bd0
 
20b03c2
 
 
1fc5bd0
20b03c2
1fc5bd0
20b03c2
 
1fc5bd0
 
20b03c2
 
 
 
 
 
 
 
 
 
 
1fc5bd0
20b03c2
 
 
 
 
 
 
 
1fc5bd0
20b03c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4433e44
20b03c2
 
 
 
 
 
 
 
 
 
4433e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
1fc5bd0
 
20b03c2
 
 
1fc5bd0
20b03c2
 
 
 
1fc5bd0
20b03c2
 
 
1fc5bd0
 
20b03c2
 
 
 
 
 
1fc5bd0
 
20b03c2
1fc5bd0
20b03c2
 
 
1fc5bd0
 
20b03c2
 
 
 
 
 
 
 
 
1fc5bd0
20b03c2
 
 
 
 
 
 
 
 
 
 
 
 
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
20b03c2
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
 
1fc5bd0
 
 
 
 
 
 
20b03c2
5f216b2
 
1fc5bd0
d87a207
20b03c2
 
 
 
 
 
 
d87a207
5f216b2
1fc5bd0
 
 
 
 
 
 
 
20b03c2
 
 
 
 
 
 
1fc5bd0
 
 
 
 
 
 
 
d87a207
20b03c2
1fc5bd0
 
 
 
 
 
 
5f216b2
1fc5bd0
 
 
 
 
4433e44
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4433e44
1fc5bd0
 
 
 
 
20b03c2
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
1fc5bd0
 
 
 
 
 
 
 
 
 
20b03c2
 
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
cedb3a9
5f216b2
1fc5bd0
 
 
 
 
 
 
 
20b03c2
 
1fc5bd0
 
 
20b03c2
 
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b03c2
5f216b2
20b03c2
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
20b03c2
 
1fc5bd0
 
 
 
 
 
20b03c2
 
1fc5bd0
 
 
 
 
 
 
 
20b03c2
 
 
 
1fc5bd0
 
 
 
 
 
 
 
 
 
20b03c2
 
1fc5bd0
 
 
 
 
 
20b03c2
1fc5bd0
 
5f216b2
1fc5bd0
 
5f216b2
1fc5bd0
 
 
 
5f216b2
1fc5bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f216b2
1fc5bd0
 
 
 
5f216b2
cedb3a9
 
5f216b2
20b03c2
5f216b2
8e11847
 
cedb3a9
5f216b2
 
cedb3a9
5f216b2
1fc5bd0
5f216b2
cedb3a9
1fc5bd0
 
 
 
 
 
 
 
4433e44
5f216b2
 
 
 
 
 
 
 
1fc5bd0
 
20b03c2
5f216b2
1fc5bd0
 
5f216b2
 
 
 
4433e44
 
 
5f216b2
4433e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f216b2
4433e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f216b2
1fc5bd0
 
 
 
20b03c2
1fc5bd0
5f216b2
 
 
4433e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f216b2
4433e44
1fc5bd0
 
 
 
20b03c2
 
4433e44
20b03c2
4433e44
 
5f216b2
1fc5bd0
5f216b2
 
 
4433e44
1fc5bd0
20b03c2
5f216b2
4433e44
5f216b2
 
 
 
 
 
1fc5bd0
5f216b2
 
1fc5bd0
5f216b2
 
cedb3a9
4433e44
 
 
5f216b2
4433e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f216b2
 
4433e44
 
5f216b2
 
1fc5bd0
5f216b2
 
cedb3a9
1fc5bd0
cedb3a9
 
1fc5bd0
cedb3a9
 
 
 
 
 
1fc5bd0
cedb3a9
 
1fc5bd0
cedb3a9
 
 
 
 
 
 
1fc5bd0
cedb3a9
 
 
 
20b03c2
cedb3a9
 
1fc5bd0
20b03c2
1fc5bd0
cedb3a9
1fc5bd0
20b03c2
 
cedb3a9
1fc5bd0
20b03c2
 
cedb3a9
1fc5bd0
 
20b03c2
 
 
cedb3a9
1fc5bd0
cedb3a9
1fc5bd0
 
20b03c2
cedb3a9
 
1fc5bd0
 
20b03c2
cedb3a9
 
 
 
1fc5bd0
 
cedb3a9
 
 
 
 
 
 
 
 
 
 
 
1fc5bd0
 
cedb3a9
 
1fc5bd0
 
cedb3a9
 
 
 
1fc5bd0
 
 
 
cedb3a9
1fc5bd0
cedb3a9
 
 
 
 
 
 
1fc5bd0
 
 
cedb3a9
 
 
 
 
 
 
1fc5bd0
 
 
cedb3a9
 
20b03c2
cedb3a9
 
20b03c2
cedb3a9
1fc5bd0
 
 
 
 
cedb3a9
 
 
 
 
 
 
 
 
 
1fc5bd0
cedb3a9
 
 
 
 
 
 
1fc5bd0
 
 
cedb3a9
 
1fc5bd0
 
 
2e1a881
 
 
 
 
1fc5bd0
 
5f216b2
 
 
 
 
 
8e11847
5f216b2
8e11847
5f216b2
 
 
1fc5bd0
5f216b2
 
 
 
8e11847
5f216b2
1fc5bd0
5f216b2
 
 
1fc5bd0
5f216b2
 
 
 
 
1fc5bd0
5f216b2
20b03c2
1fc5bd0
 
 
 
 
 
20b03c2
5f216b2
 
 
 
 
 
 
 
 
 
 
20b03c2
1fc5bd0
 
 
 
 
 
20b03c2
5f216b2
 
 
 
 
 
 
1fc5bd0
5f216b2
 
 
8e11847
5f216b2
 
 
 
1fc5bd0
5f216b2
1fc5bd0
5f216b2
 
 
 
8e11847
5f216b2
 
 
1fc5bd0
5f216b2
20b03c2
 
2e1a881
1fc5bd0
2e1a881
1fc5bd0
2e1a881
cedb3a9
1fc5bd0
2e1a881
 
 
 
 
 
cedb3a9
2e1a881
 
 
cedb3a9
2e1a881
 
 
cedb3a9
2e1a881
 
 
 
 
 
 
 
 
 
cedb3a9
2e1a881
20b03c2
2e1a881
 
cedb3a9
2e1a881
 
 
 
1fc5bd0
2e1a881
 
20b03c2
cedb3a9
2e1a881
1fc5bd0
cedb3a9
2e1a881
 
 
5f216b2
1fc5bd0
5f216b2
 
2e1a881
 
 
1fc5bd0
2e1a881
 
 
cedb3a9
2e1a881
1fc5bd0
2e1a881
 
 
cedb3a9
2e1a881
 
 
 
 
 
 
 
 
 
 
 
 
 
cedb3a9
2e1a881
 
20b03c2
2e1a881
cedb3a9
2e1a881
 
1fc5bd0
 
cedb3a9
2e1a881
1fc5bd0
 
20b03c2
 
 
2e1a881
 
 
 
1fc5bd0
 
 
2e1a881
1fc5bd0
cedb3a9
2e1a881
 
 
5f216b2
cedb3a9
2e1a881
1fc5bd0
2e1a881
5f216b2
 
 
 
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
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <meta name="theme-color" content="#1a0d07">
    <title>Cafe AI — پنل مدیریت</title>

    <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=Lalezar&family=Vazirmatn:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">

    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>

    <script>
        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        sans: ['Vazirmatn', 'sans-serif'],
                        display: ['Lalezar', 'serif'],
                    },
                    colors: {
                        bazaar: { 950: '#1a0d07', 900: '#2d1810', 800: '#3d2418', 700: '#5c3a2a', 600: '#7a4e3a', 500: '#9c6b4f', 400: '#b8856a', 300: '#d4a88c' },
                        azure: { 950: '#051a2e', 900: '#0a2540', 800: '#123a5e', 700: '#1e4976', 600: '#2d6ca8', 500: '#3d8bc4', 400: '#6ba7d4', 300: '#a3c8e4' },
                        turk: { DEFAULT: '#0d9488', light: '#2dd4bf', glow: '#5eead4', deep: '#134e4a' },
                        rug: { DEFAULT: '#8b1e3f', light: '#c23d5f', dark: '#5a1028', glow: '#e85d7e' },
                        ochre: { DEFAULT: '#c9a961', light: '#e4c97a', dark: '#9c7d3a', pale: '#f0dca3' },
                        ivory: '#faf3e7', cream: '#f5e6d3', saffron: '#d97706',
                        emerald: { DEFAULT: '#10b981', light: '#34d399', dark: '#065f46' },
                        rose: { DEFAULT: '#e11d48', light: '#fb7185' },
                    },
                    boxShadow: {
                        'carpet': '0 10px 40px -10px rgba(139, 30, 63, 0.35), 0 0 0 1px rgba(201, 169, 97, 0.15)',
                        'mosque': '0 10px 40px -10px rgba(13, 148, 136, 0.4), 0 0 0 1px rgba(45, 212, 191, 0.2)',
                        'bazaar-glow': '0 0 60px rgba(201, 169, 97, 0.25), inset 0 1px 0 rgba(244, 228, 163, 0.1)',
                        'inner-soft': 'inset 0 2px 8px rgba(0,0,0,0.35)',
                        'inner-gold': 'inset 0 0 20px rgba(201, 169, 97, 0.15)',
                    },
                    animation: {
                        'float': 'float 6s ease-in-out infinite',
                        'shimmer': 'shimmer 3s ease-in-out infinite',
                        'pulse-glow': 'pulseGlow 2.5s ease-in-out infinite',
                    },
                    keyframes: {
                        float: { '0%, 100%': { transform: 'translateY(0px)' }, '50%': { transform: 'translateY(-12px)' } },
                        shimmer: { '0%, 100%': { opacity: '0.7' }, '50%': { opacity: '1' } },
                        pulseGlow: { '0%, 100%': { boxShadow: '0 0 20px rgba(201, 169, 97, 0.3)' }, '50%': { boxShadow: '0 0 40px rgba(201, 169, 97, 0.6)' } },
                    },
                }
            }
        }
    </script>

    <style>
        :root {
            --pattern-url: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='none' stroke='%23c9a961' stroke-width='0.6' opacity='0.08'%3E%3Cpath d='M40 10 L50 20 L60 10 L70 20 L60 30 L70 40 L60 50 L70 60 L60 70 L50 60 L40 70 L30 60 L20 70 L10 60 L20 50 L10 40 L20 30 L10 20 L20 10 L30 20 Z'/%3E%3Ccircle cx='40' cy='40' r='12'/%3E%3Ccircle cx='40' cy='40' r='6'/%3E%3Cpath d='M40 28 L40 52 M28 40 L52 40'/%3E%3C/g%3E%3C/svg%3E");
            --vh: 1vh;
        }

        * { box-sizing: border-box; }

        html, body {
            height: 100%;
            overflow: hidden;
            overscroll-behavior: none;
            -webkit-overflow-scrolling: touch;
        }

        @supports (height: 100dvh) {
            html, body { height: 100dvh; }
        }

        body {
            background-color: #1a0d07;
            color: #f5e6d3;
            -webkit-tap-highlight-color: transparent;
            font-feature-settings: "ss01", "cv01";
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

        .bg-pattern { background-image: var(--pattern-url); background-size: 80px 80px; }

        .bg-pattern-fine {
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40' viewBox='0 0 40 40'%3E%3Cg fill='none' stroke='%23c9a961' stroke-width='0.4' opacity='0.1'%3E%3Cpath d='M20 0 L20 40 M0 20 L40 20'/%3E%3Ccircle cx='20' cy='20' r='3'/%3E%3Ccircle cx='0' cy='0' r='2'/%3E%3Ccircle cx='40' cy='0' r='2'/%3E%3Ccircle cx='0' cy='40' r='2'/%3E%3Ccircle cx='40' cy='40' r='2'/%3E%3C/g%3E%3C/svg%3E");
        }

        .bg-arch {
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200' viewBox='0 0 200 200'%3E%3Cg fill='none' stroke='%230d9488' stroke-width='0.5' opacity='0.06'%3E%3Cpath d='M100 20 Q 40 40, 40 100 Q 40 180, 100 180 Q 160 180, 160 100 Q 160 40, 100 20 Z'/%3E%3Cpath d='M100 50 Q 65 65, 65 100 Q 65 155, 100 155 Q 135 155, 135 100 Q 135 65, 100 50 Z'/%3E%3Cpath d='M100 80 Q 85 90, 85 100 Q 85 130, 100 130 Q 115 130, 115 100 Q 115 90, 100 80 Z'/%3E%3C/g%3E%3C/svg%3E");
            background-size: 200px 200px;
        }

        ::-webkit-scrollbar { width: 6px; height: 6px; }
        ::-webkit-scrollbar-track { background: transparent; }
        ::-webkit-scrollbar-thumb {
            background: linear-gradient(180deg, #c9a961, #8b1e3f);
            border-radius: 4px;
        }
        ::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, #e4c97a, #c23d5f); }

        .glass-card {
            background: linear-gradient(135deg, rgba(45, 24, 16, 0.82) 0%, rgba(26, 13, 7, 0.78) 100%);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border: 1px solid rgba(201, 169, 97, 0.18);
            box-shadow: 0 20px 50px -20px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(244, 228, 163, 0.08);
            transition: all 0.4s ease;
        }
        .glass-card:hover { border-color: rgba(201, 169, 97, 0.3); }

        .glass-card-azure {
            background: linear-gradient(135deg, rgba(10, 37, 64, 0.78) 0%, rgba(5, 26, 46, 0.82) 100%);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border: 1px solid rgba(45, 212, 191, 0.2);
            box-shadow: 0 20px 50px -20px rgba(13, 148, 136, 0.2), inset 0 1px 0 rgba(94, 234, 212, 0.1);
        }

        .arch-shape { border-radius: 50% 50% 1rem 1rem / 35% 35% 1rem 1rem; }

        .chat-markdown { line-height: 1.85; word-wrap: break-word; overflow-wrap: break-word; }
        .chat-markdown > * + * { margin-top: 0.6em; }
        .chat-markdown h1, .chat-markdown h2, .chat-markdown h3,
        .chat-markdown h4, .chat-markdown h5, .chat-markdown h6 {
            font-family: 'Lalezar', serif; color: #e4c97a;
            margin-top: 1em; margin-bottom: 0.4em; line-height: 1.3;
        }
        .chat-markdown h1 { font-size: 1.4em; }
        .chat-markdown h2 { font-size: 1.25em; }
        .chat-markdown h3 { font-size: 1.15em; color: #5eead4; }
        .chat-markdown h4, .chat-markdown h5, .chat-markdown h6 { font-size: 1.05em; }
        .chat-markdown p { margin-bottom: 0.5em; }
        .chat-markdown ul, .chat-markdown ol { padding-right: 1.4em; margin: 0.4em 0; }
        .chat-markdown ul { list-style-type: '❋  '; }
        .chat-markdown ol { list-style-type: persian; }
        .chat-markdown li { margin-bottom: 0.3em; padding-right: 0.2em; }
        .chat-markdown li::marker { color: #c9a961; }
        .chat-markdown strong { color: #f0dca3; font-weight: 700; text-shadow: 0 0 8px rgba(201, 169, 97, 0.3); }
        .chat-markdown em { color: #5eead4; font-style: italic; }
        .chat-markdown a { color: #2dd4bf; text-decoration: underline; text-underline-offset: 3px; }
        .chat-markdown code {
            background: rgba(13, 148, 136, 0.18); color: #5eead4;
            padding: 0.15em 0.4em; border-radius: 5px; font-size: 0.88em;
            border: 1px solid rgba(45, 212, 191, 0.2);
            font-family: 'Courier New', monospace;
            direction: ltr; display: inline-block;
        }
        .chat-markdown pre {
            background: rgba(5, 26, 46, 0.7);
            border: 1px solid rgba(45, 212, 191, 0.25);
            border-right: 3px solid #0d9488;
            padding: 0.9em 1em; border-radius: 8px;
            overflow-x: auto; margin: 0.6em 0;
            direction: ltr; text-align: left;
        }
        .chat-markdown pre code { background: transparent; border: none; padding: 0; color: #a3c8e4; font-size: 0.85em; }
        .chat-markdown blockquote {
            border-right: 3px solid #c9a961;
            background: linear-gradient(90deg, rgba(201, 169, 97, 0.08), transparent);
            padding: 0.5em 1em; margin: 0.6em 0;
            color: #f0dca3; font-style: italic;
            border-radius: 0 8px 8px 0;
        }
        .chat-markdown blockquote p { margin: 0; }
        .chat-markdown hr {
            border: none; height: 1px;
            background: linear-gradient(90deg, transparent, rgba(201, 169, 97, 0.5), transparent);
            margin: 1em 0;
        }
        .chat-markdown table {
            width: 100%; border-collapse: collapse; margin: 0.6em 0;
            font-size: 0.92em; background: rgba(45, 24, 16, 0.5);
            border-radius: 8px; overflow: hidden;
            border: 1px solid rgba(201, 169, 97, 0.25);
            display: block; overflow-x: auto;
        }
        .chat-markdown thead { background: linear-gradient(90deg, rgba(139, 30, 63, 0.4), rgba(201, 169, 97, 0.2)); }
        .chat-markdown th {
            padding: 0.55em 0.8em; text-align: right;
            color: #e4c97a; font-weight: 700;
            border-bottom: 1px solid rgba(201, 169, 97, 0.3);
            font-family: 'Lalezar', serif;
        }
        .chat-markdown td { padding: 0.5em 0.8em; border-bottom: 1px solid rgba(201, 169, 97, 0.1); }
        .chat-markdown tr:last-child td { border-bottom: none; }
        .chat-markdown img { max-width: 100%; border-radius: 10px; border: 2px solid rgba(201, 169, 97, 0.3); }

        .typing-dots span {
            display: inline-block; width: 6px; height: 6px;
            border-radius: 50%; background: #c9a961;
            animation: typing 1.4s infinite;
        }
        .typing-dots span:nth-child(2) { animation-delay: 0.2s; }
        .typing-dots span:nth-child(3) { animation-delay: 0.4s; }
        @keyframes typing {
            0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
            30% { transform: translateY(-8px); opacity: 1; }
        }

        .btn-primary {
            background: linear-gradient(135deg, #c9a961 0%, #9c7d3a 100%);
            color: #1a0d07; font-weight: 800;
            transition: all 0.3s ease;
            box-shadow: 0 8px 20px -5px rgba(201, 169, 97, 0.4);
        }
        .btn-primary:hover {
            background: linear-gradient(135deg, #e4c97a 0%, #c9a961 100%);
            transform: translateY(-2px);
            box-shadow: 0 12px 28px -5px rgba(201, 169, 97, 0.6);
        }
        .btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

        .btn-ghost {
            background: rgba(201, 169, 97, 0.08);
            color: #f0dca3;
            border: 1px solid rgba(201, 169, 97, 0.2);
            transition: all 0.3s ease;
        }
        .btn-ghost:hover {
            background: rgba(201, 169, 97, 0.18);
            border-color: rgba(201, 169, 97, 0.45);
        }

        .btn-turk {
            background: linear-gradient(135deg, #0d9488 0%, #134e4a 100%);
            color: #f5e6d3;
            border: 1px solid rgba(45, 212, 191, 0.3);
            transition: all 0.3s ease;
            box-shadow: 0 6px 16px -4px rgba(13, 148, 136, 0.4);
        }
        .btn-turk:hover {
            background: linear-gradient(135deg, #2dd4bf 0%, #0d9488 100%);
            border-color: rgba(94, 234, 212, 0.6);
            transform: translateY(-1px);
        }

        .drop-zone {
            position: relative;
            border: 2px dashed rgba(201, 169, 97, 0.3);
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .drop-zone:hover, .drop-zone.drag-over {
            border-color: rgba(201, 169, 97, 0.7);
            background: rgba(201, 169, 97, 0.06);
        }
        .drop-zone::before {
            content: '';
            position: absolute;
            inset: 6px;
            border: 1px dashed rgba(201, 169, 97, 0.15);
            border-radius: inherit;
            pointer-events: none;
        }

        /* کارت سفارش - با افکت ویژه برای سفارشات چندگانه */
        .order-card {
            background: linear-gradient(135deg, rgba(45, 24, 16, 0.6), rgba(26, 13, 7, 0.5));
            border: 1px solid rgba(201, 169, 97, 0.2);
            transition: all 0.3s ease;
        }
        .order-card:hover {
            border-color: rgba(201, 169, 97, 0.5);
            box-shadow: 0 12px 30px -10px rgba(139, 30, 63, 0.3);
            transform: translateY(-2px);
        }
        .order-card.multi-order {
            border: 1px solid rgba(194, 61, 95, 0.5);
            background: linear-gradient(135deg, rgba(90, 16, 40, 0.25), rgba(26, 13, 7, 0.5));
        }
        .order-card.multi-order::before {
            content: '';
            position: absolute;
            top: -1px; left: -1px; right: -1px; bottom: -1px;
            border-radius: inherit;
            background: linear-gradient(135deg, rgba(201, 169, 97, 0.3), transparent);
            z-index: -1;
            pointer-events: none;
        }

        /* Badge برای تعداد سفارش‌ها */
        .orders-count-badge {
            background: linear-gradient(135deg, #8b1e3f, #5a1028);
            color: #f5e6d3;
            border: 1px solid rgba(201, 169, 97, 0.5);
            font-family: 'Lalezar', serif;
            animation: pulseGlow 2s ease-in-out infinite;
        }

        .menu-row { transition: all 0.3s ease; }
        .menu-row:hover { background: rgba(201, 169, 97, 0.05); }

        input:focus, textarea:focus, select:focus {
            outline: none;
            border-color: rgba(201, 169, 97, 0.6) !important;
            box-shadow: 0 0 0 3px rgba(201, 169, 97, 0.15);
        }

        input[type="checkbox"] {
            appearance: none; width: 18px; height: 18px;
            border: 2px solid rgba(201, 169, 97, 0.4);
            border-radius: 4px;
            background: rgba(45, 24, 16, 0.5);
            cursor: pointer; position: relative;
            transition: all 0.2s ease; flex-shrink: 0;
        }
        input[type="checkbox"]:checked {
            background: linear-gradient(135deg, #0d9488, #134e4a);
            border-color: #2dd4bf;
        }
        input[type="checkbox"]:checked::after {
            content: '✓'; position: absolute;
            top: 50%; left: 50%;
            transform: translate(-50%, -50%);
            color: #f5e6d3; font-size: 14px; font-weight: bold;
        }

        .live-dot {
            display: inline-block; width: 8px; height: 8px;
            border-radius: 50%; background: #10b981;
            box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
            animation: live-pulse 2s infinite;
        }
        @keyframes live-pulse {
            0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
            70% { box-shadow: 0 0 0 10px rgba(16, 185, 129, 0); }
            100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
        }

        .section-title { font-family: 'Lalezar', serif; letter-spacing: 0.02em; }

        .badge-live {
            background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(6, 95, 70, 0.2));
            border: 1px solid rgba(52, 211, 153, 0.4);
            color: #34d399;
        }

        .badge-table {
            background: linear-gradient(135deg, rgba(201, 169, 97, 0.15), rgba(139, 30, 63, 0.15));
            border: 1px solid rgba(201, 169, 97, 0.4);
            color: #e4c97a;
        }

        .admin-table thead { background: linear-gradient(90deg, rgba(139, 30, 63, 0.25), rgba(201, 169, 97, 0.15)); }
        .admin-table th { font-family: 'Lalezar', serif; color: #e4c97a; font-size: 11px; }
        .admin-table td, .admin-table th { border-bottom: 1px solid rgba(201, 169, 97, 0.1); }

        .safe-bottom { padding-bottom: env(safe-area-inset-bottom); }
        .safe-top { padding-top: env(safe-area-inset-top); }

        .scroll-area {
            overflow-y: auto;
            overflow-x: hidden;
            overscroll-behavior: contain;
            -webkit-overflow-scrolling: touch;
        }

        button, .no-select { -webkit-user-select: none; user-select: none; }

        .app-wrapper {
            display: flex;
            flex-direction: column;
            height: 100vh;
            height: 100dvh;
            overflow: hidden;
        }

        .chat-column {
            display: flex;
            flex-direction: column;
            min-height: 0;
            overflow: hidden;
        }

        .chat-messages-container {
            flex: 1;
            min-height: 0;
            overflow-y: auto;
            overflow-x: hidden;
            overscroll-behavior: contain;
            -webkit-overflow-scrolling: touch;
        }

        .chat-header-box,
        .chat-loader-box,
        .chat-input-box {
            flex-shrink: 0;
        }

        @media (min-width: 1024px) {
            .main-grid {
                overflow: hidden !important;
                height: 100%;
            }
            .left-column {
                overflow-y: auto;
                min-height: 0;
                height: 100%;
            }
            .chat-column {
                height: 100%;
                max-height: 100%;
            }
        }

        @media (max-width: 1023px) {
            .main-grid {
                overflow-y: auto;
                overflow-x: hidden;
            }
            .chat-column {
                height: 65vh;
                height: calc(var(--vh, 1vh) * 65);
                min-height: 380px;
                max-height: 600px;
                flex-shrink: 0;
            }
        }

        @media (max-width: 480px) {
            .chat-column {
                height: 60vh;
                height: calc(var(--vh, 1vh) * 60);
                min-height: 340px;
                max-height: 500px;
            }
        }

        @media (max-height: 500px) and (orientation: landscape) {
            .chat-column {
                height: 75vh !important;
                min-height: 280px !important;
            }
        }

        @media (min-width: 768px) and (max-width: 1023px) {
            .chat-column {
                height: 70vh;
                height: calc(var(--vh, 1vh) * 70);
                min-height: 420px;
                max-height: 650px;
            }
        }
    </style>
</head>
<body class="font-sans selection:bg-ochre selection:text-bazaar-950 antialiased">

    <!-- Ambient Background -->
    <div class="fixed inset-0 overflow-hidden pointer-events-none z-0">
        <div class="absolute inset-0 bg-gradient-to-br from-bazaar-950 via-bazaar-900 to-azure-950"></div>
        <div class="absolute inset-0 bg-pattern opacity-40"></div>
        <div class="absolute -top-40 -right-40 w-[600px] h-[600px] rounded-full bg-rug/10 blur-[140px] animate-float"></div>
        <div class="absolute -bottom-40 -left-40 w-[600px] h-[600px] rounded-full bg-turk/10 blur-[140px] animate-float" style="animation-delay: -3s;"></div>
        <div class="absolute top-1/3 left-1/4 w-[400px] h-[400px] rounded-full bg-ochre/8 blur-[120px] animate-shimmer"></div>
    </div>

    <div class="app-wrapper relative z-10">

        <!-- Header -->
        <header class="flex-shrink-0 border-b border-ochre/20 bg-bazaar-900/70 backdrop-blur-2xl z-20 px-3 md:px-6 lg:px-8 py-2.5 md:py-3 shadow-lg safe-top">
            <div class="max-w-7xl mx-auto flex items-center justify-between gap-2 md:gap-4">
                <div class="flex items-center gap-2 md:gap-3 min-w-0">
                    <div class="w-9 h-9 md:w-11 md:h-11 arch-shape bg-gradient-to-br from-rug via-rug-dark to-bazaar-900 border-2 border-ochre/50 flex items-center justify-center shadow-carpet flex-shrink-0">
                        <svg class="w-5 h-5 md:w-6 md:h-6 text-ochre-light" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
                            <path d="M17 8h1a4 4 0 1 1 0 8h-1" stroke-linecap="round"/>
                            <path d="M3 8h14v9a4 4 0 0 1-4 4H7a4 4 0 0 1-4-4Z" stroke-linecap="round"/>
                            <line x1="6" y1="2" x2="6" y2="4" stroke-linecap="round"/>
                            <line x1="10" y1="2" x2="10" y2="4" stroke-linecap="round"/>
                            <line x1="14" y1="2" x2="14" y2="4" stroke-linecap="round"/>
                        </svg>
                    </div>
                    <div class="min-w-0">
                        <h1 class="section-title text-sm md:text-base lg:text-lg text-ochre-light tracking-wide flex items-center gap-1.5 md:gap-2 truncate">
                            <span class="text-rug-light flex-shrink-0">Cafe AI</span>
                            <span class="text-cream/40 flex-shrink-0">|</span>
                            <span class="text-cream truncate">پنل مدیریت</span>
                        </h1>
                        <p class="text-[9px] md:text-[10px] text-turk-light/80 font-medium mt-0.5 truncate hidden sm:block">پایش برخط سفارشات و منوی هوشمند</p>
                    </div>
                </div>

                <div class="flex items-center gap-2 flex-shrink-0">
                    <span class="badge-live inline-flex items-center gap-1.5 md:gap-2 px-2 md:px-3 py-1.5 rounded-full text-[9px] md:text-[10px] font-bold">
                        <span class="live-dot"></span>
                        <span class="hidden sm:inline">نظارت زنده</span>
                        <span class="sm:hidden">زنده</span>
                    </span>
                </div>
            </div>
        </header>

        <!-- Main grid -->
        <main class="main-grid flex-1 min-h-0 w-full mx-auto p-3 md:p-5 lg:p-6 grid grid-cols-1 lg:grid-cols-12 gap-3 md:gap-5 lg:gap-6 z-10 relative max-w-7xl">

            <!-- Right column -->
            <section class="left-column lg:col-span-7 flex flex-col gap-3 md:gap-5 lg:gap-6 min-h-0">

                <!-- Live orders -->
                <div class="glass-card rounded-2xl md:rounded-3xl p-4 md:p-5 lg:p-6 shadow-xl relative overflow-hidden flex-shrink-0">
                    <div class="absolute inset-0 bg-pattern-fine opacity-20 pointer-events-none"></div>
                    <div class="relative">
                        <div class="flex items-center justify-between mb-3 border-b border-ochre/20 pb-2.5">
                            <div class="flex items-center gap-2.5 min-w-0">
                                <div class="w-8 h-8 md:w-9 md:h-9 rounded-xl bg-rug/20 border border-rug/40 flex items-center justify-center flex-shrink-0">
                                    <svg class="w-4 h-4 md:w-5 md:h-5 text-rug-light" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"/>
                                    </svg>
                                </div>
                                <div class="min-w-0">
                                    <h2 class="section-title text-sm md:text-base text-ochre-light flex items-center gap-2 truncate">
                                        سفارشات جدید آشپزخانه
                                        <span class="w-2 h-2 rounded-full bg-rug-light animate-pulse flex-shrink-0"></span>
                                    </h2>
                                    <p class="text-[9px] md:text-[10px] text-cream/50 mt-0.5 truncate">گروه‌بندی شده بر اساس میز (ادغام خودکار)</p>
                                </div>
                            </div>
                            <button onclick="fetchActiveOrders()" class="btn-ghost text-[9px] md:text-[10px] flex items-center gap-1 px-2 md:px-3 py-1.5 rounded-lg md:rounded-xl flex-shrink-0">
                                <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M4 4v5h.582m15.356 2A8.001 8.001 0 1121.21 15H15"/>
                                </svg>
                                <span class="hidden sm:inline">بروزرسانی</span>
                            </button>
                        </div>
                        <div id="ordersContainer" class="space-y-2 md:space-y-2.5 max-h-[260px] md:max-h-[320px] scroll-area pr-1">
                            <div class="text-center py-8 text-[11px] text-ochre/60">در حال بارگذاری...</div>
                        </div>
                    </div>
                </div>

                <!-- Current Menu -->
                <div class="glass-card rounded-2xl md:rounded-3xl p-4 md:p-5 lg:p-6 space-y-3 shadow-xl relative overflow-hidden flex-shrink-0">
                    <div class="absolute inset-0 bg-arch opacity-15 pointer-events-none"></div>
                    <div class="relative">
                        <div class="flex items-center justify-between border-b border-ochre/20 pb-2.5 mb-2.5 flex-wrap gap-2">
                            <div class="flex items-center gap-2.5 min-w-0">
                                <div class="w-8 h-8 md:w-9 md:h-9 rounded-xl bg-ochre/20 border border-ochre/40 flex items-center justify-center flex-shrink-0">
                                    <svg class="w-4 h-4 md:w-5 md:h-5 text-ochre-light" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4"/>
                                    </svg>
                                </div>
                                <div class="min-w-0">
                                    <h2 class="section-title text-sm md:text-base text-ochre-light truncate">مدیریت منوی فعال</h2>
                                    <p class="text-[9px] md:text-[10px] text-cream/50 mt-0.5 truncate">کنترل و ویرایش اقلام قابل سفارش</p>
                                </div>
                            </div>
                            <div class="flex gap-1.5 md:gap-2 flex-shrink-0">
                                <button onclick="fetchCurrentMenu()" class="btn-ghost text-[9px] md:text-[10px] flex items-center gap-1 px-2 md:px-3 py-1.5 rounded-lg md:rounded-xl">
                                    <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M4 4v5h.582m15.356 2A8.001 8.001 0 1121.21 15H15"/>
                                    </svg>
                                    <span class="hidden sm:inline">بازنشانی</span>
                                </button>
                                <button onclick="addMenuItemRow()" class="btn-turk px-2.5 md:px-4 py-1.5 rounded-lg md:rounded-xl text-[9px] md:text-[10px] font-extrabold flex items-center gap-1">
                                    <span>+ افزودن</span>
                                </button>
                            </div>
                        </div>
                        <p class="text-[10px] md:text-[11px] text-cream/70 leading-relaxed mb-3">
                            لیست اقلام منو. تغییرات بلافاصله روی سامانه مشتریان اعمال می‌شود.
                        </p>
                        <div class="overflow-x-auto max-h-[280px] md:max-h-[380px] border border-ochre/20 rounded-xl md:rounded-2xl bg-bazaar-950/50 scroll-area">
                            <table class="min-w-full admin-table text-[10px] md:text-[11px]" id="currentMenuTable">
                                <thead class="sticky top-0">
                                    <tr>
                                        <th class="px-2 md:px-3 py-2 md:py-2.5 text-right">نام محصول</th>
                                        <th class="px-2 md:px-3 py-2 md:py-2.5 text-right hidden md:table-cell">توضیحات</th>
                                        <th class="px-2 md:px-3 py-2 md:py-2.5 text-right w-16 md:w-20">قیمت</th>
                                        <th class="px-2 md:px-3 py-2 md:py-2.5 text-center w-12 md:w-16">موجود</th>
                                        <th class="px-2 md:px-3 py-2 md:py-2.5 text-center w-20 md:w-24">عملیات</th>
                                    </tr>
                                </thead>
                                <tbody id="menuTableBody" class="divide-y divide-ochre/10"></tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Menu extraction & upload -->
                <div class="glass-card rounded-2xl md:rounded-3xl p-4 md:p-5 lg:p-6 space-y-3 shadow-xl relative overflow-hidden flex-shrink-0">
                    <div class="absolute inset-0 bg-pattern-fine opacity-20 pointer-events-none"></div>
                    <div class="relative">
                        <div class="flex items-center gap-2.5 border-b border-ochre/20 pb-2.5 mb-2.5">
                            <div class="w-8 h-8 md:w-9 md:h-9 rounded-xl bg-turk/20 border border-turk/40 flex items-center justify-center flex-shrink-0">
                                <svg class="w-4 h-4 md:w-5 md:h-5 text-turk-light" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"/>
                                </svg>
                            </div>
                            <div class="min-w-0">
                                <h2 class="section-title text-sm md:text-base text-ochre-light truncate">استخراج خودکار منو</h2>
                                <p class="text-[9px] md:text-[10px] text-cream/50 mt-0.5 truncate">بارگذاری تصویر یا فایل PDF</p>
                            </div>
                        </div>
                        <p class="text-[10px] md:text-[11px] text-cream/70 leading-relaxed mb-3">
                            تصویر منو را بارگذاری کنید. هوش مصنوعی نیلا متون و قیمت‌ها را استخراج می‌کند.
                        </p>

                        <div id="dropZone"
                             class="drop-zone rounded-xl md:rounded-2xl p-5 md:p-8 text-center cursor-pointer"
                             onclick="document.getElementById('menuFileInput').click()"
                             ondragover="handleDragOver(event)"
                             ondragleave="handleDragLeave(event)"
                             ondrop="handleDrop(event)">
                            <input type="file" id="menuFileInput" class="hidden" accept="image/*,application/pdf" onchange="uploadMenuFile(event)">
                            <div class="flex flex-col items-center gap-2 md:gap-3">
                                <div class="w-12 h-12 md:w-16 md:h-16 rounded-xl md:rounded-2xl bg-gradient-to-br from-ochre/20 to-rug/15 border border-ochre/30 flex items-center justify-center">
                                    <svg id="uploadIcon" class="w-6 h-6 md:w-8 md:h-8 text-ochre-light" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M9 13h6m-3-3v6m-9 1V7a2 2 0 012-2h6l2 2h6a2 2 0 012 2v8a2 2 0 01-2 2H5a2 2 0 01-2-2z"/>
                                    </svg>
                                </div>
                                <span class="text-xs md:text-sm text-cream font-medium">فایل منو را بکشید یا انتخاب کنید</span>
                                <span class="text-[9px] md:text-[10px] text-ochre/60 uppercase tracking-widest">فرمت‌های تصویری یا PDF</span>
                            </div>
                        </div>

                        <div id="uploadLoader" class="hidden text-center py-5 bg-bazaar-950/70 rounded-xl md:rounded-2xl border border-turk/30 mt-3">
                            <div class="inline-block animate-spin rounded-full h-7 w-7 md:h-8 md:w-8 border-t-2 border-b-2 border-ochre mb-2"></div>
                            <p class="text-[9px] md:text-[10px] text-ochre-light font-extrabold">نیلا در حال آنالیز منو...</p>
                        </div>

                        <div id="menuEditorContainer" class="hidden space-y-3 border-t border-ochre/15 pt-3 mt-3">
                            <h3 class="text-[11px] md:text-xs font-bold text-ochre-light flex items-center gap-1.5 section-title">
                                <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                                </svg>
                                بازبینی اقلام منو پیش از انتشار
                            </h3>
                            <div class="overflow-x-auto max-h-[220px] md:max-h-[250px] border border-ochre/20 rounded-xl md:rounded-2xl bg-bazaar-950/50 scroll-area">
                                <table class="min-w-full admin-table text-[10px] md:text-[11px]">
                                    <thead>
                                        <tr>
                                            <th class="px-2 md:px-3 py-2 md:py-2.5 text-right">عنوان محصول</th>
                                            <th class="px-2 md:px-3 py-2 md:py-2.5 text-right">توضیحات</th>
                                            <th class="px-2 md:px-3 py-2 md:py-2.5 text-right w-20 md:w-24">قیمت</th>
                                            <th class="px-2 md:px-3 py-2 md:py-2.5 text-center w-14 md:w-16">عملیات</th>
                                        </tr>
                                    </thead>
                                    <tbody id="menuEditorTableBody" class="divide-y divide-ochre/10"></tbody>
                                </table>
                            </div>
                            <div class="flex justify-end gap-2 flex-wrap">
                                <button onclick="addEmptyMenuRow()" class="btn-ghost px-3 md:px-4 py-1.5 md:py-2 rounded-lg md:rounded-xl text-[9px] md:text-[10px]">
                                    + افزودن محصول
                                </button>
                                <button onclick="saveAndPublishMenu()" class="btn-turk px-4 md:px-5 py-1.5 md:py-2.5 rounded-lg md:rounded-xl text-[10px] md:text-[11px] font-extrabold flex items-center gap-2">
                                    ثبت و همگام‌سازی زنده
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <!-- Left column: Admin chat -->
            <section class="lg:col-span-5 flex flex-col min-h-0">
                <div class="chat-column glass-card-azure rounded-2xl md:rounded-3xl shadow-xl relative">
                    <div class="absolute inset-0 bg-arch opacity-15 pointer-events-none"></div>

                    <div class="chat-header-box relative px-3.5 md:px-5 py-2.5 md:py-3 border-b border-turk/25 flex items-center gap-2.5 bg-gradient-to-l from-azure-950/60 to-azure-900/40">
                        <div class="w-9 h-9 md:w-10 md:h-10 rounded-xl bg-gradient-to-br from-turk/30 to-azure-700/30 border border-turk/40 flex items-center justify-center shadow-mosque flex-shrink-0">
                            <svg class="w-5 h-5 md:w-6 md:h-6 text-turk-glow" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
                                <path d="M12 2a10 10 0 0 1 10 10c0 5.523-4.477 10-10 10S2 17.523 2 12A10 10 0 0 1 12 2z"/>
                                <path d="M12 8v4l3 3" stroke-linecap="round"/>
                            </svg>
                        </div>
                        <div class="min-w-0 flex-1">
                            <h3 class="section-title text-xs md:text-sm text-ochre-light tracking-wide truncate">دستیار هوشمند ادمین</h3>
                            <p class="text-[9px] md:text-[10px] text-turk-light/80 font-medium flex items-center gap-1.5 truncate">
                                <span class="w-1.5 h-1.5 rounded-full bg-turk-light animate-pulse flex-shrink-0"></span>
                                <span class="truncate">کنترل همگام موجودی و منو</span>
                            </p>
                        </div>
                    </div>

                    <div id="chatMessages" class="chat-messages-container p-3 md:p-4 space-y-3 relative">
                        <div class="flex gap-2 md:gap-2.5 max-w-[88%]">
                            <div class="w-7 h-7 md:w-8 md:h-8 rounded-xl bg-gradient-to-br from-turk/20 to-azure-700/20 border border-turk/30 flex items-center justify-center flex-shrink-0">
                                <span class="text-ochre-light font-display text-[10px] md:text-[11px]">ن</span>
                            </div>
                            <div class="bg-bazaar-950/70 border border-ochre/20 text-cream text-xs md:text-sm rounded-2xl rounded-tr-none px-3 py-2 md:px-3.5 md:py-2.5 leading-relaxed shadow-inner-soft chat-markdown break-words" style="flex: 1; max-width: calc(100% - 2.5rem);">
                                سلام همکار گرامی! من <strong>نیلا</strong> هستم، دستیار مدیریت منوی <em>Cafe AI</em>. شما می‌توانید از همین‌جا اقلام منو را با گفتگوی ساده کنترل کنید؛ برای مثال بنویسید: «کروسان ساده تمام شد» یا «چای ماسالا را موجود کن» تا سریعاً تغییرات روی سیستم مشتریان نیز اعمال گردد.
                            </div>
                        </div>
                    </div>

                    <div id="chatLoader" class="chat-loader-box hidden relative px-3 py-2 md:px-4 md:py-2.5 flex items-center justify-between bg-azure-950/70 text-[9px] md:text-[10px] text-turk-light font-semibold border-t border-turk/20">
                        <div class="flex items-center gap-2">
                            <div class="typing-dots flex gap-1">
                                <span></span><span></span><span></span>
                            </div>
                            <span>در حال پردازش...</span>
                        </div>
                        <button onclick="stopAdminChat()" class="px-2.5 py-1 md:px-3 md:py-1.5 bg-rug-dark/40 border border-rug/40 hover:bg-rug/40 rounded-lg text-[9px] md:text-[10px] text-rug-light font-bold transition-all">
                            توقف
                        </button>
                    </div>

                    <form id="chatForm" onsubmit="sendAdminMessage(event)" class="chat-input-box relative p-2 md:p-3 border-t border-turk/20 bg-bazaar-950/60 flex gap-2 safe-bottom">
                        <input type="text" id="chatInput" placeholder="تغییر وضعیت یا ویرایش منو..." autocomplete="off"
                               class="flex-1 min-w-0 bg-azure-950/60 border border-azure-700/40 text-xs md:text-sm text-cream placeholder-ochre/40 rounded-lg md:rounded-xl px-3 md:px-4 py-2 md:py-3 focus:outline-none focus:border-ochre/60 transition-all duration-300">
                        <button type="submit" id="sendBtn" class="btn-primary px-3 md:px-4 py-2 md:py-3 rounded-lg md:rounded-xl text-xs flex items-center justify-center flex-shrink-0">
                            <svg class="w-4 h-4 md:w-5 md:h-5 rotate-180" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M14 5l7 7m0 0l-7 7m7-7H3"/>
                            </svg>
                        </button>
                    </form>
                </div>
            </section>
        </main>

        <footer class="flex-shrink-0 border-t border-ochre/15 py-2.5 md:py-3 text-center text-[9px] md:text-[10px] text-cream/40 bg-bazaar-950/60 backdrop-blur safe-bottom">
            طراحی و توسعه توسط الگوریتم داده نسترن | با الهام از فرهنگ تبریز © ۲۰۲۶
        </footer>
    </div>

    <script>
        // ============ VIEWPORT HEIGHT FIX ============
        function setVh() {
            const vh = window.innerHeight * 0.01;
            document.documentElement.style.setProperty('--vh', `${vh}px`);
        }
        setVh();
        window.addEventListener('resize', setVh);
        window.addEventListener('orientationchange', () => setTimeout(setVh, 100));

        function scrollChatToBottom(smooth = true) {
            const chatScroll = document.getElementById('chatMessages');
            if (chatScroll) {
                if (smooth) {
                    chatScroll.scrollTo({ top: chatScroll.scrollHeight, behavior: 'smooth' });
                } else {
                    chatScroll.scrollTop = chatScroll.scrollHeight;
                }
            }
        }

        window.addEventListener('resize', () => {
            setVh();
            setTimeout(scrollChatToBottom, 150);
        });

        marked.setOptions({ sanitize: true, breaks: true });

        let chatHistory = [
            { role: 'model', content: 'سلام همکار گرامی! من نیلا هستم، دستیار مدیریت منوی Cafe AI. شما می‌توانید از همین‌جا اقلام منو را با گفتگوی ساده کنترل کنید؛ برای مثال بنویسید: «کروسان ساده تمام شد» یا «چای ماسالا را موجود کن» تا سریعاً تغییرات روی سیستم مشتریان نیز اعمال گردد.' }
        ];
        let abortController = null;
        let isGenerating = false;

        window.addEventListener('DOMContentLoaded', () => {
            fetchActiveOrders();
            fetchCurrentMenu();
            setInterval(fetchActiveOrders, 10000);
            setTimeout(scrollChatToBottom, 100);
        });

        const chatInput = document.getElementById('chatInput');
        if (chatInput) {
            chatInput.addEventListener('focus', () => {
                setTimeout(scrollChatToBottom, 300);
                setTimeout(scrollChatToBottom, 600);
            });
        }

        // ============ ORDER FUNCTIONS - با ادغام سفارشات هر میز ============
        async function fetchActiveOrders() {
            const container = document.getElementById('ordersContainer');
            try {
                const response = await fetch('/api/admin/orders');
                const orders = await response.json();
                if (response.ok) {
                    if (orders.length === 0) {
                        container.innerHTML = `
                            <div class="text-center py-8 md:py-12 text-ochre/60 border-2 border-dashed border-ochre/25 rounded-xl md:rounded-2xl bg-bazaar-950/30">
                                <svg class="w-8 h-8 md:w-10 md:h-10 text-ochre/40 mx-auto mb-2 md:mb-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                                <p class="text-xs md:text-sm font-medium">سفارش معلقی در صف نیست</p>
                                <p class="text-[9px] md:text-[10px] text-cream/40 mt-1">به‌روزرسانی خودکار فعال</p>
                            </div>
                        `;
                        return;
                    }

                    // ✅ گروه‌بندی سفارشات بر اساس میز
                    const tableGroups = {};
                    orders.forEach(order => {
                        const tableKey = String(order.table);
                        if (!tableGroups[tableKey]) {
                            tableGroups[tableKey] = {
                                table: order.table,
                                orderIds: [],
                                items: {},   // name -> {name, quantity}
                                timestamps: [],
                                totalItemCount: 0
                            };
                        }
                        const group = tableGroups[tableKey];
                        group.orderIds.push(order.id);
                        group.timestamps.push(order.timestamp);
                        group.totalItemCount += order.items.reduce((sum, it) => sum + it.quantity, 0);

                        // ✅ ادغام آیتم‌های تکراری با جمع تعداد
                        order.items.forEach(item => {
                            if (group.items[item.name]) {
                                group.items[item.name].quantity += item.quantity;
                            } else {
                                group.items[item.name] = { name: item.name, quantity: item.quantity };
                            }
                        });
                    });

                    container.innerHTML = '';
                    // مرتب‌سازی بر اساس جدیدترین timestamp هر گروه
                    const sortedGroups = Object.values(tableGroups).sort((a, b) => {
                        const aMax = Math.max(...a.timestamps);
                        const bMax = Math.max(...b.timestamps);
                        return bMax - aMax;
                    });

                    sortedGroups.forEach(group => {
                        const itemsArray = Object.values(group.items);
                        let itemsHtml = '';
                        itemsArray.forEach(item => {
                            itemsHtml += `
                                <div class="flex items-center justify-between text-[10px] md:text-[11px] bg-bazaar-950/60 px-2.5 py-1.5 md:px-3 md:py-2 rounded-lg border border-ochre/20">
                                    <span class="text-cream font-bold flex items-center gap-2 min-w-0">
                                        <span class="w-1 h-4 md:h-5 bg-gradient-to-b from-rug to-ochre rounded-full flex-shrink-0"></span>
                                        <span class="truncate">${escapeHtml(item.name)}</span>
                                    </span>
                                    <span class="text-ochre-light bg-ochre/10 px-2 py-0.5 rounded-md font-display text-[9px] md:text-[10px] border border-ochre/25 flex-shrink-0 mr-2">×${item.quantity}</span>
                                </div>
                            `;
                        });

                        // محاسبه زمان‌ها
                        const firstTime = new Date(Math.min(...group.timestamps) * 1000).toLocaleTimeString('fa-IR', { hour: '2-digit', minute: '2-digit' });
                        const lastTime = new Date(Math.max(...group.timestamps) * 1000).toLocaleTimeString('fa-IR', { hour: '2-digit', minute: '2-digit' });
                        const orderCount = group.orderIds.length;
                        const isMulti = orderCount > 1;

                        // نمایش زمان
                        let timeDisplay = '';
                        if (isMulti) {
                            timeDisplay = `<span class="text-[9px] md:text-[10px] text-turk-light/70 font-medium flex items-center gap-1 flex-shrink-0">
                                <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                                ${firstTime}${lastTime}
                            </span>`;
                        } else {
                            timeDisplay = `<span class="text-[9px] md:text-[10px] text-turk-light/70 font-medium flex items-center gap-1 flex-shrink-0">
                                <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                                ${lastTime}
                            </span>`;
                        }

                        // Badge برای سفارشات چندگانه
                        const multiBadge = isMulti ? `
                            <span class="orders-count-badge text-[10px] md:text-[11px] px-2 py-0.5 rounded-md flex items-center gap-1 flex-shrink-0">
                                <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/>
                                </svg>
                                ${orderCount} سفارش
                            </span>
                        ` : '';

                        const orderIdsJson = JSON.stringify(group.orderIds).replace(/"/g, '&quot;');

                        const cardHtml = `
                            <div class="order-card ${isMulti ? 'multi-order' : ''} relative rounded-xl md:rounded-2xl p-3 md:p-4 flex flex-col md:flex-row justify-between items-start md:items-center gap-2.5 md:gap-3">
                                <div class="flex-1 space-y-2 w-full min-w-0">
                                    <div class="flex items-center justify-between md:justify-start gap-2 md:gap-3 w-full flex-wrap">
                                        <span class="badge-table text-[10px] md:text-xs font-bold px-2.5 md:px-3 py-1 rounded-lg flex items-center gap-1.5 flex-shrink-0">
                                            <svg class="w-3 h-3 md:w-3.5 md:h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                                <path stroke-linecap="round" stroke-linejoin="round" d="M3 10h18M3 14h18m-9-4v8m-7 0h14a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"/>
                                            </svg>
                                            میز ${group.table}
                                        </span>
                                        ${multiBadge}
                                        ${timeDisplay}
                                    </div>
                                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-1 md:gap-1.5 mt-1.5 md:mt-2">
                                        ${itemsHtml}
                                    </div>
                                </div>
                                <button onclick='completeOrdersForTable(${orderIdsJson})' class="w-full md:w-auto btn-ghost hover:bg-emerald-dark/40 hover:border-emerald-light/50 hover:text-emerald-light px-3 md:px-4 py-2 md:py-2.5 rounded-lg md:rounded-xl text-[9px] md:text-[10px] font-bold flex items-center justify-center gap-1.5 flex-shrink-0 group">
                                    <svg class="w-3.5 h-3.5 md:w-4 md:h-4 transition-transform group-hover:scale-110" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7"/>
                                    </svg>
                                    تحویل نهایی ${isMulti ? `(${orderCount} سفارش)` : 'شد'}
                                </button>
                            </div>
                        `;
                        container.insertAdjacentHTML('beforeend', cardHtml);
                    });
                } else {
                    container.innerHTML = `<div class="text-center py-6 text-rug-light text-xs">خطا در دریافت سفارشات.</div>`;
                }
            } catch (err) {
                container.innerHTML = `<div class="text-center py-6 text-rug-light text-xs">عدم ارتباط با سرور: ${escapeHtml(err.message)}</div>`;
            }
        }

        // ✅ تکمیل همه سفارش‌های یک میز (چندگانه)
        async function completeOrdersForTable(orderIds) {
            if (!Array.isArray(orderIds) || orderIds.length === 0) return;
            try {
                // انجام sequential همه درخواست‌ها
                let successCount = 0;
                let failCount = 0;
                for (const orderId of orderIds) {
                    try {
                        const response = await fetch('/api/admin/complete_order', {
                            method: 'POST',
                            headers: { 'Content-Type': 'application/json' },
                            body: JSON.stringify({ order_id: orderId })
                        });
                        const result = await response.json();
                        if (response.ok && result.success) {
                            successCount++;
                        } else {
                            failCount++;
                        }
                    } catch (e) {
                        failCount++;
                    }
                }
                if (failCount === 0) {
                    fetchActiveOrders();
                } else {
                    alert(`${successCount} سفارش تکمیل شد. ${failCount} خطا رخ داد.`);
                    fetchActiveOrders();
                }
            } catch (err) {
                alert("خطای شبکه: " + err.message);
            }
        }

        // ============ MENU CRUD ============
        async function fetchCurrentMenu() {
            const tbody = document.getElementById('menuTableBody');
            tbody.innerHTML = '<tr><td colspan="5" class="text-center py-6 text-ochre/60 text-xs">در حال بارگذاری...</td></tr>';
            try {
                const res = await fetch('/api/admin/menu');
                const menu = await res.json();
                if (res.ok && Array.isArray(menu)) {
                    renderMenuTable(menu);
                } else {
                    tbody.innerHTML = '<tr><td colspan="5" class="text-center py-6 text-rug-light text-xs">خطا</td></tr>';
                }
            } catch (e) {
                tbody.innerHTML = '<tr><td colspan="5" class="text-center py-6 text-rug-light text-xs">عدم اتصال</td></tr>';
            }
        }

        function renderMenuTable(menu) {
            const tbody = document.getElementById('menuTableBody');
            tbody.innerHTML = '';
            if (menu.length === 0) {
                tbody.innerHTML = '<tr><td colspan="5" class="text-center py-6 text-ochre/60 text-xs">منو خالی است.</td></tr>';
                return;
            }
            menu.forEach(item => {
                const row = document.createElement('tr');
                row.className = 'menu-row';
                row.dataset.id = item.id;
                row.innerHTML = `
                    <td class="px-2 py-1.5 md:py-2">
                        <span class="view-mode font-bold text-cream">${escapeHtml(item.name)}</span>
                        <input type="text" class="edit-mode hidden w-full bg-azure-950/60 border border-azure-700/40 rounded px-2 py-1 text-cream text-[10px] md:text-[11px]" value="${escapeHtml(item.name)}" data-field="name">
                    </td>
                    <td class="px-2 py-1.5 md:py-2 hidden md:table-cell">
                        <span class="view-mode text-cream/60 text-[10px]">${escapeHtml(item.description || '-')}</span>
                        <input type="text" class="edit-mode hidden w-full bg-azure-950/60 border border-azure-700/40 rounded px-2 py-1 text-cream text-[11px]" value="${escapeHtml(item.description || '')}" data-field="description">
                    </td>
                    <td class="px-2 py-1.5 md:py-2">
                        <span class="view-mode text-ochre-light font-medium">${escapeHtml(item.price || '-')}</span>
                        <input type="text" class="edit-mode hidden w-full bg-azure-950/60 border border-azure-700/40 rounded px-2 py-1 text-cream text-[11px]" value="${escapeHtml(item.price || '')}" data-field="price">
                    </td>
                    <td class="px-2 py-1.5 md:py-2 text-center">
                        <span class="view-mode text-[9px] md:text-[10px] ${item.available ? 'text-emerald-light' : 'text-rug-light'} font-bold">${item.available ? '✓' : '✗'}</span>
                        <div class="edit-mode hidden flex items-center justify-center">
                            <input type="checkbox" data-field="available" ${item.available ? 'checked' : ''}>
                        </div>
                    </td>
                    <td class="px-2 py-1.5 md:py-2 text-center">
                        <div class="view-mode flex gap-1 justify-center">
                            <button onclick="editMenuItem(this)" class="p-1 md:p-1.5 text-turk/70 hover:text-ochre-light transition-colors rounded-lg hover:bg-bazaar-800/50" title="ویرایش">
                                <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                                </svg>
                            </button>
                            <button onclick="deleteMenuItem(${item.id})" class="p-1 md:p-1.5 text-cream/30 hover:text-rug-light transition-colors rounded-lg hover:bg-bazaar-800/50" title="حذف">
                                <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                </svg>
                            </button>
                        </div>
                        <div class="edit-mode hidden flex gap-1 justify-center">
                            <button onclick="saveMenuItem(this, ${item.id})" class="px-2 md:px-2.5 py-1 bg-emerald-dark/40 border border-emerald-light/40 text-emerald-light rounded-md text-[9px] md:text-[10px] font-bold">ذخیره</button>
                            <button onclick="cancelEdit(this)" class="px-2 md:px-2.5 py-1 bg-bazaar-800 border border-ochre/20 text-cream/60 rounded-md text-[9px] md:text-[10px]">لغو</button>
                        </div>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }

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

        function toggleRowEdit(row, editing) {
            row.querySelectorAll('.view-mode').forEach(el => el.classList.toggle('hidden', editing));
            row.querySelectorAll('.edit-mode').forEach(el => el.classList.toggle('hidden', !editing));
        }

        function editMenuItem(btn) { toggleRowEdit(btn.closest('tr'), true); }
        function cancelEdit(btn) { fetchCurrentMenu(); }

        async function saveMenuItem(btn, id) {
            const row = btn.closest('tr');
            const data = {
                name: row.querySelector('[data-field="name"]').value.trim(),
                description: row.querySelector('[data-field="description"]').value.trim(),
                price: row.querySelector('[data-field="price"]').value.trim(),
                available: row.querySelector('[data-field="available"]').checked
            };
            if (!data.name) { alert('نام آیتم نمی‌تواند خالی باشد.'); return; }
            try {
                const res = await fetch(`/api/admin/menu/item/${id}`, {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                });
                const result = await res.json();
                if (res.ok && result.success) fetchCurrentMenu();
                else alert(result.error || 'خطا در بروزرسانی');
            } catch (e) { alert('خطای شبکه: ' + e.message); }
        }

        async function deleteMenuItem(id) {
            if (!confirm('آیا از حذف این آیتم اطمینان دارید؟')) return;
            try {
                const res = await fetch(`/api/admin/menu/item/${id}`, { method: 'DELETE' });
                const result = await res.json();
                if (res.ok && result.success) fetchCurrentMenu();
                else alert(result.error || 'خطا در حذف');
            } catch (e) { alert('خطای شبکه: ' + e.message); }
        }

        function addMenuItemRow() {
            const tbody = document.getElementById('menuTableBody');
            const row = document.createElement('tr');
            row.className = 'menu-row bg-bazaar-800/40';
            row.innerHTML = `
                <td class="px-2 py-1.5 md:py-2"><input type="text" class="w-full bg-azure-950/60 border border-turk/40 rounded px-2 py-1 text-cream text-[10px] md:text-[11px]" placeholder="نام" data-field="name"></td>
                <td class="px-2 py-1.5 md:py-2 hidden md:table-cell"><input type="text" class="w-full bg-azure-950/60 border border-azure-700/40 rounded px-2 py-1 text-cream text-[11px]" placeholder="توضیحات" data-field="description"></td>
                <td class="px-2 py-1.5 md:py-2"><input type="text" class="w-full bg-azure-950/60 border border-azure-700/40 rounded px-2 py-1 text-cream text-[11px]" placeholder="قیمت" data-field="price"></td>
                <td class="px-2 py-1.5 md:py-2 text-center"><input type="checkbox" data-field="available" checked></td>
                <td class="px-2 py-1.5 md:py-2 text-center"><button onclick="saveNewItem(this)" class="btn-turk px-2.5 md:px-3 py-1 rounded-md text-[9px] md:text-[10px]">افزودن</button></td>
            `;
            tbody.appendChild(row);
        }

        async function saveNewItem(btn) {
            const row = btn.closest('tr');
            const name = row.querySelector('[data-field="name"]').value.trim();
            const desc = row.querySelector('[data-field="description"]').value.trim();
            const price = row.querySelector('[data-field="price"]').value.trim();
            const avail = row.querySelector('[data-field="available"]').checked;
            if (!name) { alert('نام الزامی است.'); return; }
            try {
                const res = await fetch('/api/admin/menu/item', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ name, description: desc, price, available: avail })
                });
                const result = await res.json();
                if (res.ok && result.success) fetchCurrentMenu();
                else alert(result.error || 'خطا در افزودن');
            } catch (e) { alert('خطای شبکه: ' + e.message); }
        }

        // ============ MENU EXTRACTION ============
        function handleDragOver(e) { e.preventDefault(); document.getElementById('dropZone').classList.add('drag-over'); }
        function handleDragLeave(e) { e.preventDefault(); document.getElementById('dropZone').classList.remove('drag-over'); }
        function handleDrop(e) {
            e.preventDefault();
            handleDragLeave(e);
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                document.getElementById('menuFileInput').files = files;
                uploadMenuFile({ target: { files: files } });
            }
        }
        async function uploadMenuFile(e) {
            const file = e.target.files[0];
            if (!file) return;
            const loader = document.getElementById('uploadLoader');
            const editor = document.getElementById('menuEditorContainer');
            loader.classList.remove('hidden');
            editor.classList.add('hidden');
            const formData = new FormData();
            formData.append('file', file);
            try {
                const response = await fetch('/api/admin/extract_menu', { method: 'POST', body: formData });
                const data = await response.json();
                loader.classList.add('hidden');
                if (response.ok && data.success) {
                    renderMenuEditor(data.extracted_menu);
                    editor.classList.remove('hidden');
                } else {
                    alert(data.error || 'خطا در آنالیز فایل.');
                }
            } catch (err) {
                loader.classList.add('hidden');
                alert('خطا: ' + err.message);
            }
        }
        function renderMenuEditor(menuItems) {
            const tbody = document.getElementById('menuEditorTableBody');
            tbody.innerHTML = '';
            menuItems.forEach(item => {
                const row = `
                    <tr class="menu-row">
                        <td class="px-2 py-1.5 md:py-2"><input type="text" class="menu-name w-full bg-azure-950/60 border border-azure-700/40 rounded-lg px-2.5 py-1.5 text-cream focus:border-ochre/50 text-[10px] md:text-[11px]" value="${escapeHtml(item.name || '')}"></td>
                        <td class="px-2 py-1.5 md:py-2"><input type="text" class="menu-desc w-full bg-azure-950/60 border border-azure-700/40 rounded-lg px-2.5 py-1.5 text-cream focus:border-ochre/50 text-[10px] md:text-[11px]" value="${escapeHtml(item.description || '')}"></td>
                        <td class="px-2 py-1.5 md:py-2"><input type="text" class="menu-price w-full bg-azure-950/60 border border-azure-700/40 rounded-lg px-2.5 py-1.5 text-cream focus:border-ochre/50 text-[10px] md:text-[11px]" value="${escapeHtml(item.price || '')}"></td>
                        <td class="px-2 py-1.5 md:py-2 text-center">
                            <button onclick="removeMenuRow(this)" class="p-1 md:p-1.5 text-cream/30 hover:text-rug-light transition-colors rounded-lg hover:bg-bazaar-800/50">
                                <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                </svg>
                            </button>
                        </td>
                    </tr>
                `;
                tbody.insertAdjacentHTML('beforeend', row);
            });
        }
        function addEmptyMenuRow() {
            const tbody = document.getElementById('menuEditorTableBody');
            const row = `
                <tr class="menu-row">
                    <td class="px-2 py-1.5 md:py-2"><input type="text" class="menu-name w-full bg-azure-950/60 border border-azure-700/40 rounded-lg px-2.5 py-1.5 text-cream text-[10px] md:text-[11px]" placeholder="مثال: لاته"></td>
                    <td class="px-2 py-1.5 md:py-2"><input type="text" class="menu-desc w-full bg-azure-950/60 border border-azure-700/40 rounded-lg px-2.5 py-1.5 text-cream text-[10px] md:text-[11px]" placeholder="توضیحات"></td>
                    <td class="px-2 py-1.5 md:py-2"><input type="text" class="menu-price w-full bg-azure-950/60 border border-azure-700/40 rounded-lg px-2.5 py-1.5 text-cream text-[10px] md:text-[11px]" placeholder="۸۵,۰۰۰"></td>
                    <td class="px-2 py-1.5 md:py-2 text-center">
                        <button onclick="removeMenuRow(this)" class="p-1 md:p-1.5 text-cream/30 hover:text-rug-light transition-colors rounded-lg hover:bg-bazaar-800/50">
                            <svg class="w-3.5 h-3.5 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                            </svg>
                        </button>
                    </td>
                </tr>
            `;
            tbody.insertAdjacentHTML('beforeend', row);
        }
        function removeMenuRow(btn) { btn.closest('tr').remove(); }
        async function saveAndPublishMenu() {
            const tbody = document.getElementById('menuEditorTableBody');
            const rows = tbody.querySelectorAll('tr');
            const menuData = [];
            rows.forEach(row => {
                const name = row.querySelector('.menu-name').value.trim();
                const description = row.querySelector('.menu-desc').value.trim();
                const price = row.querySelector('.menu-price').value.trim();
                if (name) menuData.push({ name, description, price, available: true });
            });
            if (menuData.length === 0) { alert("لیست فاقد محصول است."); return; }
            try {
                const response = await fetch('/api/admin/save_menu', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ menu: menuData })
                });
                const data = await response.json();
                if (response.ok && data.success) {
                    alert("✅ منوی جدید با موفقیت ثبت و همگام‌سازی شد!");
                    document.getElementById('menuEditorContainer').classList.add('hidden');
                    fetchCurrentMenu();
                    appendChatMessage('model', '✅ **منوی زنده همگام‌سازی شد.** سیستم سفارش‌گیری مشتریان اکنون بر مبنای آخرین لیست ویرایش‌شده کالیبره شده است.');
                } else {
                    alert(data.error || 'خطا در همگام‌سازی.');
                }
            } catch (err) { alert('خطا: ' + err.message); }
        }

        // ============ ADMIN CHAT ============
        async function sendAdminMessage(e) {
            e.preventDefault();
            if (isGenerating) return;
            const input = document.getElementById('chatInput');
            const text = input.value.trim();
            if (!text) return;

            appendChatMessage('user', text);
            input.value = '';
            chatHistory.push({ role: 'user', content: text });

            isGenerating = true;
            toggleChatLoading(true);
            abortController = new AbortController();

            try {
                const response = await fetch('/api/admin/chat', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ messages: chatHistory }),
                    signal: abortController.signal
                });
                const data = await response.json();
                toggleChatLoading(false);
                isGenerating = false;

                if (response.ok && data.success) {
                    const botBubbleId = appendChatMessage('model', '');
                    const botBubble = document.getElementById(botBubbleId);
                    const textResponse = data.response;
                    let i = 0;
                    function typeText() {
                        if (i < textResponse.length && !abortController.signal.aborted) {
                            botBubble.textContent += textResponse.charAt(i);
                            i++;
                            scrollChatToBottom(false);
                            setTimeout(typeText, 6);
                        } else {
                            botBubble.textContent = '';
                            botBubble.innerHTML = marked.parse(textResponse);
                            chatHistory.push({ role: 'model', content: textResponse });
                            scrollChatToBottom(true);
                            fetchCurrentMenu();
                        }
                    }
                    typeText();
                } else {
                    appendChatMessage('model', data.error || 'خطا در پاسخگویی.');
                }
            } catch (err) {
                toggleChatLoading(false);
                isGenerating = false;
                if (err.name !== 'AbortError') {
                    appendChatMessage('model', '❌ خطا: ' + escapeHtml(err.message));
                }
            }
        }

        function stopAdminChat() {
            if (abortController) abortController.abort();
            toggleChatLoading(false);
            isGenerating = false;
        }

        function toggleChatLoading(loading) {
            const loader = document.getElementById('chatLoader');
            const sendBtn = document.getElementById('sendBtn');
            const input = document.getElementById('chatInput');
            if (loading) {
                loader.classList.remove('hidden');
                sendBtn.disabled = true;
                input.disabled = true;
            } else {
                loader.classList.add('hidden');
                sendBtn.disabled = false;
                input.disabled = false;
            }
        }

        function appendChatMessage(role, content) {
            const container = document.getElementById('chatMessages');
            const bubbleId = 'bubble-admin-' + Date.now() + '-' + Math.random().toString(36).substr(2, 5);
            let html = '';

            if (role === 'user') {
                html = `
                    <div class="flex justify-end gap-2 md:gap-2.5 max-w-[88%] mr-auto">
                        <div class="bg-gradient-to-br from-rug/20 to-rug-dark/30 border border-rug/30 text-cream text-xs md:text-sm rounded-2xl rounded-tl-none px-3 py-2 md:px-3.5 md:py-2.5 leading-relaxed shadow-inner-soft break-words" style="flex: 1; max-width: calc(100% - 2.5rem);">
                            ${escapeHtml(content)}
                        </div>
                        <div class="w-7 h-7 md:w-8 md:h-8 rounded-xl bg-gradient-to-br from-ochre/30 to-rug/20 border border-ochre/40 flex items-center justify-center flex-shrink-0">
                            <svg class="w-3.5 h-3.5 md:w-4 md:h-4 text-ochre-light" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                            </svg>
                        </div>
                    </div>
                `;
            } else {
                html = `
                    <div class="flex gap-2 md:gap-2.5 max-w-[88%]">
                        <div class="w-7 h-7 md:w-8 md:h-8 rounded-xl bg-gradient-to-br from-turk/20 to-azure-700/20 border border-turk/30 flex items-center justify-center flex-shrink-0">
                            <span class="text-ochre-light font-display text-[10px] md:text-[11px]">ن</span>
                        </div>
                        <div id="${bubbleId}" class="bg-bazaar-950/70 border border-ochre/20 text-cream text-xs md:text-sm rounded-2xl rounded-tr-none px-3 py-2 md:px-3.5 md:py-2.5 leading-relaxed shadow-inner-soft chat-markdown min-w-0 break-words" style="flex: 1; max-width: calc(100% - 2.5rem);">
                            ${content ? marked.parse(content) : ''}
                        </div>
                    </div>
                `;
            }

            container.insertAdjacentHTML('beforeend', html);
            scrollChatToBottom(true);
            return bubbleId;
        }
    </script>
</body>
</html>