File size: 47,903 Bytes
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9402118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2344c18
 
 
 
 
 
 
 
 
 
 
9376474
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9f6ce3
9bb4acf
 
e9f6ce3
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9402118
 
 
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e2d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9376474
 
e4e2d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4298d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2835623
 
 
 
f9357dc
2835623
 
6b7678a
2835623
 
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9402118
 
 
 
 
 
 
 
 
 
 
 
e4e2d9d
9402118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9402118
2344c18
 
 
 
 
 
 
 
 
 
 
 
9402118
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4298d7
2344c18
 
2835623
2344c18
f9357dc
 
 
 
b4298d7
 
 
 
 
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b7678a
2344c18
b4298d7
 
 
 
 
2344c18
 
 
 
 
 
 
 
 
2835623
b4298d7
 
 
 
 
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
9402118
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9402118
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9357dc
 
 
 
 
2344c18
 
e9f6ce3
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FriendlyBot — Dashboard</title>
<link rel="icon" href="/logo.png">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<style>
*, *::before, *::after { margin:0; padding:0; box-sizing:border-box; }

:root {
    --bg: #0a0a1a;
    --surface: #12122a;
    --surface2: #1a1a3a;
    --border: rgba(255,255,255,0.06);
    --accent: #5865F2;
    --accent-glow: rgba(88,101,242,0.3);
    --green: #57F287;
    --red: #ED4245;
    --yellow: #FEE75C;
    --text: #ffffff;
    --text-dim: rgba(255,255,255,0.5);
    --text-mid: rgba(255,255,255,0.7);
    --radius: 16px;
    --radius-sm: 10px;
}

body {
    font-family: 'Inter', -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
}

/* ===== LOGIN ===== */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: radial-gradient(ellipse at 50% 0%, rgba(88,101,242,0.15) 0%, transparent 60%);
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 48px 40px;
    width: 420px;
    max-width: 90vw;
    text-align: center;
    backdrop-filter: blur(20px);
    box-shadow: 0 24px 80px rgba(0,0,0,0.5);
    animation: fadeUp 0.6s ease;
}

@keyframes fadeUp {
    from { opacity:0; transform:translateY(30px); }
    to { opacity:1; transform:translateY(0); }
}

.login-card img { width: 80px; border-radius: 50%; margin-bottom: 20px; box-shadow: 0 0 40px var(--accent-glow); }
.login-card h1 { font-size: 24px; font-weight: 700; margin-bottom: 6px; }
.login-card p { color: var(--text-dim); font-size: 14px; margin-bottom: 28px; }

.login-card input {
    width: 100%;
    padding: 14px 18px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 15px;
    font-family: 'Inter', sans-serif;
    outline: none;
    transition: border 0.2s;
    margin-bottom: 14px;
}
.login-card input:focus { border-color: var(--accent); }

.login-card button {
    width: 100%;
    padding: 14px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 600;
    font-family: 'Inter', sans-serif;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}
.login-card button:hover { background: #4752c4; transform: translateY(-1px); box-shadow: 0 8px 30px var(--accent-glow); }
.login-card button svg { width: 20px; height: 20px; }

.login-error { color: var(--red); font-size: 13px; margin-top: 14px; display: none; }
.login-info { color: var(--text-dim); font-size: 12px; margin-top: 16px; line-height: 1.6; }

.user-pill {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px;
    background: var(--surface2);
    border-radius: 100px;
}
.user-pill img { width: 28px; height: 28px; border-radius: 50%; }
.user-pill span { font-size: 13px; font-weight: 600; }

/* ===== DASHBOARD ===== */
#dashboard { display: none; }

.sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 260px;
    height: 100vh;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 24px 16px;
    z-index: 100;
    display: flex;
    flex-direction: column;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 8px 24px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 20px;
}
.sidebar-brand img { width: 40px; border-radius: 50%; }
.sidebar-brand span { font-weight: 700; font-size: 17px; }

.sidebar-nav { flex: 1; display: flex; flex-direction: column; gap: 4px; }

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    color: var(--text-mid);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
}
.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active { background: var(--accent); color: white; }
.nav-item svg { width: 20px; height: 20px; flex-shrink: 0; }

.sidebar-footer {
    padding: 16px 8px 0;
    border-top: 1px solid var(--border);
}
.sidebar-footer .nav-item { color: var(--red); }
.sidebar-footer .nav-item:hover { background: rgba(237,66,69,0.1); }

.main { margin-left: 260px; padding: 32px 40px; min-height: 100vh; }

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
}
.topbar h2 { font-size: 26px; font-weight: 700; }
.topbar-right { display: flex; align-items: center; gap: 12px; }

.status-pill {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(87,242,135,0.1);
    color: var(--green);
    padding: 8px 16px;
    border-radius: 100px;
    font-size: 13px;
    font-weight: 600;
}
.status-dot { width: 8px; height: 8px; background: var(--green); border-radius: 50%; animation: pulse 2s infinite; }
@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:0.4; } }

/* Stats Cards */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    position: relative;
    overflow: hidden;
    transition: all 0.25s;
}
.stat-card:hover { border-color: rgba(88,101,242,0.3); transform: translateY(-2px); box-shadow: 0 12px 40px rgba(0,0,0,0.3); }
.stat-card::after { content:''; position:absolute; top:0; right:0; width:120px; height:120px; background: radial-gradient(circle, var(--accent-glow) 0%, transparent 70%); opacity:0.15; pointer-events: none; }
.stat-card .label { font-size: 13px; color: var(--text-dim); font-weight: 500; text-transform: uppercase; letter-spacing: 1px; }
.stat-card .value { font-size: 36px; font-weight: 800; margin-top: 8px; line-height: 1; }
.stat-card .sub { font-size: 12px; color: var(--text-dim); margin-top: 8px; }

/* Panels */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 24px;
}
.panel-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.panel-header h3 { font-size: 16px; font-weight: 600; }
.panel-body { padding: 0; }

/* Server List */
.server-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
    transition: background 0.15s;
}
.server-row:last-child { border-bottom: none; }
.server-row:hover { background: var(--surface2); }
.server-info { display: flex; align-items: center; gap: 14px; }
.server-icon {
    width: 40px; height: 40px;
    border-radius: 50%;
    background: var(--accent);
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 16px; color: white; flex-shrink: 0;
}
.server-name { font-weight: 600; font-size: 14px; }
.server-members { color: var(--text-dim); font-size: 12px; margin-top: 2px; }
.server-rank { font-size: 13px; color: var(--text-dim); font-weight: 600; min-width: 30px; }
.member-badge {
    background: var(--surface2);
    padding: 6px 14px;
    border-radius: 100px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-mid);
}

/* Command List */
.cmd-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
}
.cmd-row:last-child { border-bottom: none; }
.cmd-row:hover { background: var(--surface2); }
.cmd-name { font-family: 'Courier New', monospace; font-weight: 600; color: var(--accent); font-size: 14px; }
.cmd-desc { color: var(--text-dim); font-size: 13px; margin-top: 2px; }
.cmd-badge {
    padding: 4px 12px;
    border-radius: 100px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.cmd-badge.admin { background: rgba(237,66,69,0.15); color: var(--red); }
.cmd-badge.public { background: rgba(87,242,135,0.15); color: var(--green); }
.cmd-badge.staff { background: rgba(254,231,92,0.15); color: var(--yellow); }

/* Pages */
.page { display: none; }
.page.active { display: block; }

/* Invite Section */
.invite-box {
    background: linear-gradient(135deg, #5865F2 0%, #4752c4 100%);
    border-radius: var(--radius);
    padding: 40px;
    text-align: center;
    margin-bottom: 24px;
}
.invite-box h3 { font-size: 22px; font-weight: 700; margin-bottom: 10px; }
.invite-box p { color: rgba(255,255,255,0.8); margin-bottom: 24px; font-size: 15px; }
.invite-btn {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 14px 32px;
    background: white;
    color: var(--accent);
    border: none;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 700;
    font-family: 'Inter', sans-serif;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}
.invite-btn:hover { transform: translateY(-2px); box-shadow: 0 12px 40px rgba(0,0,0,0.3); }

/* Quick Actions */
.actions-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; padding: 24px; }
.action-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 20px;
    cursor: pointer;
    transition: all 0.2s;
}
.action-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.action-card h4 { font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.action-card p { font-size: 12px; color: var(--text-dim); }
.action-icon { font-size: 24px; margin-bottom: 12px; }

/* Logs */
.log-area {
    background: var(--bg);
    padding: 20px 24px;
    font-family: 'Courier New', monospace;
    font-size: 13px;
    color: var(--green);
    max-height: 400px;
    overflow-y: auto;
    line-height: 1.8;
}
.log-line { opacity: 0.85; }
.log-line.error { color: var(--red); }
.log-line.warn { color: var(--yellow); }

/* Responsive */
@media (max-width: 768px) {
    .sidebar { display: none; }
    .main { margin-left: 0; padding: 20px; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* Loading shimmer */
.shimmer {
    background: linear-gradient(90deg, var(--surface) 25%, var(--surface2) 50%, var(--surface) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* Switch Toggle */
.switch {
    position: relative;
    display: inline-block;
    width: 48px;
    height: 24px;
}
.switch input { 
    opacity: 0;
    width: 0;
    height: 0;
}
.slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: var(--surface2);
    transition: .4s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 24px;
    border: 1px solid var(--border);
}
.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .4s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
input:checked + .slider {
    background-color: var(--accent);
    border-color: transparent;
    box-shadow: 0 0 15px var(--accent-glow);
}
input:checked + .slider:before {
    transform: translateX(24px);
}

/* Refresh Button */
.refresh-btn {
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text-mid);
    padding: 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 8px;
}
.refresh-btn:hover {
    color: var(--text);
    border-color: var(--accent);
    background: var(--surface);
    transform: translateY(-1px);
}
.refresh-btn svg { width: 18px; height: 18px; }
.refresh-btn.loading svg {
    animation: spin 0.8s linear infinite;
}
@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}
</style>
</head>
<body>

<!-- ===== LOGIN SCREEN ===== -->
<div id="login-screen">
    <div class="login-card">
        <img src="/logo.png" alt="FriendlyBot">
        <h1>FriendlyBot</h1>
        <p>Sign in with your Discord account to access the dashboard</p>
        <button onclick="window.location.href='/auth/login'">
            <svg viewBox="0 0 24 24" fill="currentColor"><path d="M19.73 4.87a18.2 18.2 0 0 0-4.6-1.44c-.21.4-.4.8-.58 1.21-1.69-.25-3.4-.25-5.1 0-.18-.4-.37-.8-.58-1.21-1.64.4-3.2 0.88-4.6 1.44C1.3 9.4 0.2 13.9 0.7 18.3a18.4 18.4 0 0 0 5.5 2.8c.4-.6.8-1.2 1.1-1.8-1.9-.7-3.7-1.6-5.4-2.8.4.3.9.6 1.3.9 3.5 2.1 7.4 2.1 10.9 0 .4-.3.9-.6 1.3-.9-1.7 1.2-3.5 2.1-5.4 2.8.3.6.7 1.2 1.1 1.8a18.4 18.4 0 0 0 5.5-2.8c.5-4.4-.6-8.9-3.7-13.43zM8.02 15.33c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm7.96 0c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2z"/></svg>
            Login with Discord
        </button>
        <div class="login-error" id="login-error"></div>
        <div class="login-info">Server Administrators can access the dashboard to configure bot settings for their respective guilds.</div>
    </div>
</div>

<!-- ===== DASHBOARD ===== -->
<div id="dashboard">
    <!-- Sidebar -->
    <div class="sidebar">
        <div class="sidebar-brand">
            <img src="/logo.png" alt="">
            <span>FriendlyBot</span>
        </div>
        <div class="sidebar-nav">
            <div class="nav-item active" onclick="showPage('overview', this)">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/></svg>
                Overview
            </div>
            <div class="nav-item" onclick="showPage('servers', this)">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 12h-4l-3 9L9 3l-3 9H2"/></svg>
                Servers
            </div>
            <div class="nav-item" onclick="showPage('commands', this)">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="4 17 10 11 4 5"/><line x1="12" y1="19" x2="20" y2="19"/></svg>
                Commands
            </div>
            <div class="nav-item" onclick="showPage('league', this)">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 3l3 6 6 1-4 4 1 6-6-3-6 3 1-6-4-4 6-1z"/></svg>
                League
            </div>
            <div class="nav-item" onclick="showPage('invite', this)">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="8.5" cy="7" r="4"/><line x1="20" y1="8" x2="20" y2="14"/><line x1="23" y1="11" x2="17" y2="11"/></svg>
                Invite Bot
            </div>
        </div>
        <div class="sidebar-footer">
            <div class="nav-item" onclick="logout()">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>
                Sign Out
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="main">
        <!-- ===== OVERVIEW PAGE ===== -->
        <div class="page active" id="page-overview">
            <div class="topbar">
                <h2>Dashboard</h2>
                <div class="topbar-right">
                    <button class="refresh-btn" onclick="refreshAll()" title="Refresh Data">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
                    </button>
                    <div class="user-pill" id="user-pill" style="display:none">
                        <img id="user-avatar" src="" alt="">
                        <span id="user-name"></span>
                    </div>
                    <div class="status-pill"><div class="status-dot"></div> Online</div>
                </div>
            </div>

            <div class="stats-grid">
                <div class="stat-card">
                    <div class="label">Servers</div>
                    <div class="value" id="stat-servers">—</div>
                    <div class="sub">Connected guilds</div>
                </div>
                <div class="stat-card">
                    <div class="label">Total Members</div>
                    <div class="value" id="stat-members">—</div>
                    <div class="sub">Across all servers</div>
                </div>
                <div class="stat-card">
                    <div class="label">Uptime</div>
                    <div class="value" id="stat-uptime">—</div>
                    <div class="sub">Since last restart</div>
                </div>
                <div class="stat-card">
                    <div class="label">Commands</div>
                    <div class="value" id="stat-commands">—</div>
                    <div class="sub">Registered globally</div>
                </div>
            </div>

            <div class="panel">
                <div class="panel-header"><h3>⚡ Quick Actions</h3></div>
                <div class="actions-grid">
                    <div class="action-card" onclick="window.open('/api/invite','_blank')">
                        <div class="action-icon">🤖</div>
                        <h4>Add to Server</h4>
                        <p>Generate a fresh invite link and add the bot to a new server.</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- ===== SERVERS PAGE ===== -->
        <div class="page" id="page-servers">
            <div class="topbar">
                <h2>Server Registry</h2>
                <div class="topbar-right">
                    <div class="status-pill"><div class="status-dot"></div> <span id="server-count-label">0 Servers</span></div>
                </div>
            </div>
            <div class="panel">
                <div class="panel-header"><h3>🌐 All Connected Servers</h3><span style="color:var(--text-dim);font-size:13px">Sorted by members</span></div>
                <div class="panel-body" id="server-list">
                    <div class="server-row shimmer" style="height:72px"></div>
                    <div class="server-row shimmer" style="height:72px"></div>
                    <div class="server-row shimmer" style="height:72px"></div>
                </div>
            </div>
        </div>

        <!-- ===== COMMANDS PAGE ===== -->
        <div class="page" id="page-commands">
            <div class="topbar"><h2>Command Registry</h2></div>
            <div class="panel">
                <div class="panel-header"><h3>📋 All Registered Commands</h3></div>
                <div class="panel-body" id="command-list"></div>
            </div>
        </div>

        <!-- ===== INVITE PAGE ===== -->
        <div class="page" id="page-invite">
            <div class="topbar"><h2>Invite Bot</h2></div>
            <div class="invite-box">
                <h3>Add FriendlyBot to Your Server</h3>
                <p>Give your server the ultimate match management experience with lineup boards, formations, match tracking, and more.</p>
                <a class="invite-btn" id="invite-link" href="#" target="_blank">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="8.5" cy="7" r="4"/><line x1="20" y1="8" x2="20" y2="14"/><line x1="23" y1="11" x2="17" y2="11"/></svg>
                    Invite to Discord
                </a>
            </div>
            <div class="panel">
                <div class="panel-header"><h3>🔒 Required Permissions</h3></div>
                <div class="actions-grid">
                    <div class="action-card" style="cursor:default">
                        <div class="action-icon">💬</div>
                        <h4>Send Messages</h4>
                        <p>Post lineup boards, match polls, and announcements.</p>
                    </div>
                    <div class="action-card" style="cursor:default">
                        <div class="action-icon">👤</div>
                        <h4>Manage Roles</h4>
                        <p>Assign and manage staff whitelist roles.</p>
                    </div>
                    <div class="action-card" style="cursor:default">
                        <div class="action-icon">📌</div>
                        <h4>Manage Messages</h4>
                        <p>Clear messages, pin important updates, bulk delete.</p>
                    </div>
                    <div class="action-card" style="cursor:default">
                        <div class="action-icon">🔒</div>
                        <h4>Manage Channels</h4>
                        <p>Lock/unlock channels during matches or events.</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- ===== LEAGUE PAGE ===== -->
        <div class="page" id="page-league">
            <div class="topbar"><h2>League Center</h2></div>
            <div class="invite-box" style="background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);">
                <h3 style="color: #000;">Join the Official League</h3>
                <p style="color: rgba(0,0,0,0.8);">Compete against the best teams and players. Winner receives exclusive premium features for FriendlyBot!</p>
                <a class="invite-btn" href="https://discord.gg/vpHd4Xfm" target="_blank" style="color: #5865F2;">
                    <svg viewBox="0 0 24 24" fill="currentColor" style="width:20px; height:20px;"><path d="M19.73 4.87a18.2 18.2 0 0 0-4.6-1.44c-.21.4-.4.8-.58 1.21-1.69-.25-3.4-.25-5.1 0-.18-.4-.37-.8-.58-1.21-1.64.4-3.2 0.88-4.6 1.44C1.3 9.4 0.2 13.9 0.7 18.3a18.4 18.4 0 0 0 5.5 2.8c.4-.6.8-1.2 1.1-1.8-1.9-.7-3.7-1.6-5.4-2.8.4.3.9.6 1.3.9 3.5 2.1 7.4 2.1 10.9 0 .4-.3.9-.6 1.3-.9-1.7 1.2-3.5 2.1-5.4 2.8.3.6.7 1.2 1.1 1.8a18.4 18.4 0 0 0 5.5-2.8c.5-4.4-.6-8.9-3.7-13.43zM8.02 15.33c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm7.96 0c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2z"/></svg>
                    Join Discord League
                </a>
            </div>
            <div class="panel">
                <div class="panel-header"><h3>🏆 League Info</h3></div>
                <div class="panel-body" style="padding: 24px;">
                    <p style="margin-bottom: 20px; color: var(--text-mid); line-height: 1.6;">The FriendlyBot League is the premier competition for teams using our bot. Showcase your skills, climb the rankings, and earn special rewards.</p>
                    
                    <div class="actions-grid" style="padding: 0; margin-bottom: 30px;">
                        <div class="action-card" style="cursor:default">
                            <div class="action-icon">💎</div>
                            <h4>Premium Features</h4>
                            <p>Winners gain access to exclusive bot features, custom themes, and advanced analytics.</p>
                        </div>
                        <div class="action-card" style="cursor:default">
                            <div class="action-icon">📅</div>
                            <h4>Weekly Matches</h4>
                            <p>Scheduled matches every weekend with full stat tracking and automated lineups.</p>
                        </div>
                    </div>

                    <div style="text-align: center; padding: 20px; border-top: 1px solid var(--border);">
                        <a href="/league" class="invite-btn" style="background: var(--accent); color: white;">
                            Visit Full League Website
                        </a>
                        <p style="font-size: 12px; color: var(--text-dim); margin-top: 12px;">View teams, rosters, and full standings on the official league page.</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- ===== SERVER SETTINGS PAGE ===== -->
        <div class="page" id="page-server-settings">
            <div class="topbar">
                <h2 id="settings-guild-name">Server Settings</h2>
                <button class="invite-btn" onclick="showPage('servers')" style="padding:8px 16px; font-size:13px">Back to List</button>
            </div>
            
            <div class="panel">
                <div class="panel-header"><h3>⚙️ General Configuration</h3></div>
                <div class="panel-body" style="padding: 24px;">
                    <div style="margin-bottom: 32px;">
                        <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Command Prefix</label>
                        <div style="display:flex; gap:10px">
                            <input type="text" id="config-prefix" placeholder="!" style="width:100px; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; font-size:16px; outline:none">
                        </div>
                        <p style="font-size:12px; color:var(--text-dim); margin-top:10px">The prefix used to trigger message-based commands (e.g. ! or . or ?)</p>
                    </div>

                    <div style="border-top:1px solid var(--border); padding-top:32px; margin-bottom: 32px;">
                        <h4 style="margin-bottom:16px; font-size:15px; color:var(--accent)">🤖 Auto Friendly System</h4>
                        <div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:24px">
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Feature Status</label>
                                <div style="display:flex; align-items:center; gap:12px">
                                    <label class="switch">
                                        <input type="checkbox" id="config-auto-enabled">
                                        <span class="slider"></span>
                                    </label>
                                    <span style="font-size:14px; color:var(--text-mid)">Enabled</span>
                                </div>
                            </div>
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Interval (Minutes)</label>
                                <input type="number" id="config-auto-interval" placeholder="40" style="width:100%; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; outline:none">
                            </div>
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Target Channel</label>
                                <select id="config-auto-channel" style="width:100%; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; outline:none">
                                    <option value="">Select a channel...</option>
                                </select>
                            </div>
                        </div>
                        <p style="font-size:12px; color:var(--text-dim); margin-top:16px">When enabled, the bot will automatically post a friendly match poll in the selected channel at the specified interval.</p>
                    </div>

                    <div style="border-top:1px solid var(--border); padding-top:32px; margin-bottom: 32px;">
                        <h4 style="margin-bottom:16px; font-size:15px; color:var(--accent)">📢 Broadcast & Announcements</h4>
                        <div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:24px">
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Receive Broadcasts</label>
                                <div style="display:flex; align-items:center; gap:12px">
                                    <label class="switch">
                                        <input type="checkbox" id="config-post-enabled">
                                        <span class="slider"></span>
                                    </label>
                                    <span style="font-size:14px; color:var(--text-mid)">Enabled</span>
                                </div>
                            </div>
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Broadcast Channel</label>
                                <select id="config-post-channel" style="width:100%; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; outline:none">
                                    <option value="">Default (System Channel)</option>
                                </select>
                            </div>
                        </div>
                        <p style="font-size:12px; color:var(--text-dim); margin-top:16px">Control where global announcements from the bot owner are posted. You can opt-out by disabling this feature.</p>
                    </div>

                    <div style="border-top:1px solid var(--border); padding-top:32px; margin-bottom: 32px;">
                        <h4 style="margin-bottom:16px; font-size:15px; color:var(--accent)">🧹 Auto Cleanup & Maintenance</h4>
                        <div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:24px">
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Poll Cleanup (Hours)</label>
                                <input type="number" id="config-cleanup-hours" placeholder="2" style="width:100%; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; outline:none">
                            </div>
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Lineup Cleanup (Hours)</label>
                                <input type="number" id="config-lineup-cleanup-hours" placeholder="2" style="width:100%; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; outline:none">
                            </div>
                        </div>
                        <p style="font-size:12px; color:var(--text-dim); margin-top:16px">The bot will automatically delete friendly polls and lineup boards after the specified time to keep channels clean.</p>
                    </div>

                    <div style="border-top:1px solid var(--border); padding-top:32px; margin-bottom: 32px;">
                        <h4 style="margin-bottom:16px; font-size:15px; color:var(--accent)">⏰ Match Reminders</h4>
                        <div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:24px">
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Enable DM Reminders</label>
                                <div style="display:flex; align-items:center; gap:12px">
                                    <label class="switch">
                                        <input type="checkbox" id="config-reminders-enabled">
                                        <span class="slider"></span>
                                    </label>
                                    <span style="font-size:14px; color:var(--text-mid)">Enabled</span>
                                </div>
                            </div>
                            <div>
                                <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Lead Time (Minutes)</label>
                                <input type="number" id="config-reminders-lead" placeholder="30" style="width:100%; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; outline:none">
                            </div>
                        </div>
                        <p style="font-size:12px; color:var(--text-dim); margin-top:16px">When enabled, players on the lineup board will receive a DM reminder before a scheduled league match starts.</p>
                    </div>

                    <div style="border-top:1px solid var(--border); padding-top:32px; margin-bottom: 32px;">
                        <h4 style="margin-bottom:16px; font-size:15px; color:var(--accent)">💬 Command Messages</h4>
                        <div style="margin-bottom: 24px;">
                            <label style="display:block; font-size:13px; color:var(--text-dim); margin-bottom:8px">Friendly Command Text</label>
                            <textarea id="config-friendly-text" oninput="autoResize(this)" style="width:100%; min-height:100px; padding:12px; background:var(--bg); border:1px solid var(--border); border-radius:var(--radius-sm); color:white; font-family:monospace; font-size:13px; outline:none; resize:none; overflow:hidden; transition: height 0.2s ease;"></textarea>
                            <p style="font-size:12px; color:var(--text-dim); margin-top:8px">Use <code>{needed}</code> as a placeholder for the number of players needed.</p>
                        </div>

                    </div>

                    <div style="border-top:1px solid var(--border); padding-top:24px; text-align:right">
                        <button class="invite-btn" onclick="saveConfig()" style="background:var(--accent); color:white; padding:12px 32px">Save All Changes</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// ===== AUTH (Discord OAuth2 via httpOnly cookie) =====
let currentUser = null;

document.addEventListener('DOMContentLoaded', () => {
    // Check URL params for errors
    const params = new URLSearchParams(window.location.search);
    if (params.get('error') === 'not_owner') {
        document.getElementById('login-error').textContent = '❌ Access denied. You must be an Administrator or have Manage Server permissions to access the dashboard.';
        document.getElementById('login-error').style.display = 'block';
    } else if (params.get('error')) {
        document.getElementById('login-error').textContent = '❌ Login failed. Please try again.';
        document.getElementById('login-error').style.display = 'block';
    }
    
    // Check for existing session
    fetch('/api/me')
    .then(r => r.json())
    .then(data => {
        if (data.loggedIn) {
            currentUser = data.user;
            showDashboard();
            // Clean URL
            if (params.has('login') || params.has('error')) {
                window.history.replaceState({}, '', '/');
            }
        }
    })
    .catch(() => {});
});

function showDashboard() {
    document.getElementById('login-screen').style.display = 'none';
    document.getElementById('dashboard').style.display = 'block';
    
    // Show user info
    if (currentUser) {
        document.getElementById('user-avatar').src = currentUser.avatar;
        document.getElementById('user-name').textContent = currentUser.username;
        document.getElementById('user-pill').style.display = 'flex';
    }
    
    loadStats();
    loadServers();
    loadCommands();
    loadLogs();
    
    // Regular intervals
    setInterval(loadStats, 15000);
    setInterval(loadServers, 15000);
    setInterval(loadLogs, 10000);

    // Auto-refresh when tab becomes visible or focused
    // This solves the issue of not seeing a server after adding it
    window.addEventListener('focus', () => {
        console.log("Tab focused, refreshing dashboard data...");
        refreshAll();
    });

    document.addEventListener('visibilitychange', () => {
        if (document.visibilityState === 'visible') {
            refreshAll();
        }
    });
}

function refreshAll() {
    const btn = document.querySelector('.refresh-btn');
    if (btn) btn.classList.add('loading');
    
    return Promise.all([
        loadStats(),
        loadServers(),
        loadLogs()
    ]).finally(() => {
        setTimeout(() => {
            if (btn) btn.classList.remove('loading');
        }, 600);
    });
}

function logout() {
    window.location.href = '/auth/logout';
}

// ===== NAVIGATION =====
function showPage(name, el) {
    document.querySelectorAll('.page').forEach(p => p.classList.remove('active'));
    document.querySelectorAll('.nav-item').forEach(n => n.classList.remove('active'));
    document.getElementById('page-' + name).classList.add('active');
    if (el) el.classList.add('active');
}

// ===== DATA LOADERS =====
function loadStats() {
    return fetch('/api/stats')
    .then(r => r.json())
    .then(data => {
        document.getElementById('stat-servers').textContent = data.servers || 0;
        document.getElementById('stat-members').textContent = (data.members || 0).toLocaleString();
        document.getElementById('stat-uptime').textContent = data.uptime || '—';
        document.getElementById('stat-commands').textContent = data.commands || 0;
        document.getElementById('server-count-label').textContent = (data.servers || 0) + ' Servers';
        if (data.inviteUrl) document.getElementById('invite-link').href = data.inviteUrl;
    }).catch(() => {});
}

function loadServers() {
    return fetch('/api/servers')
    .then(r => r.json())
    .then(data => {
        const servers = Array.isArray(data) ? data : (Array.isArray(data.servers) ? data.servers : []);
        const container = document.getElementById('server-list');
        container.innerHTML = '';
        
        if (data && data.pending) {
            container.innerHTML = '<div class="server-row"><div class="server-info"><div><div class="server-name">Connecting to Discord...</div><div class="server-members">Bot is still starting up on Hugging Face. This list will auto-refresh.</div></div></div></div>';
            return;
        }
        
        if (!servers.length) {
            container.innerHTML = '<div class="server-row"><div class="server-info"><div><div class="server-name">No servers found</div><div class="server-members">Make sure the bot is in your server and your account has Manage Server or Administrator permissions.</div></div></div></div>';
            return;
        }
        
        servers.sort((a, b) => (b.members || 0) - (a.members || 0));
        servers.forEach((s, i) => {
            const iconHtml = s.icon
                ? `<img class="server-icon" src="${s.icon}" style="object-fit:cover">`
                : `<div class="server-icon">${s.name.charAt(0).toUpperCase()}</div>`;
            container.innerHTML += `
                <div class="server-row" onclick="openServerSettings('${s.id}', '${s.name.replace(/'/g, "\\'")}')" style="cursor:pointer">
                    <div class="server-info">
                        <span class="server-rank">#${i + 1}</span>
                        ${iconHtml}
                        <div>
                            <div class="server-name">${escapeHtml(s.name)}</div>
                            <div class="server-members">${s.id}</div>
                        </div>
                    </div>
                    <div style="display:flex; align-items:center; gap:12px">
                        <div class="member-badge">${(s.members || 0).toLocaleString()} members</div>
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="opacity:0.3"><polyline points="9 18 15 12 9 6"/></svg>
                    </div>
                </div>`;
        });
    }).catch(() => {
        const container = document.getElementById('server-list');
        container.innerHTML = '<div class="server-row"><div class="server-info"><div><div class="server-name">Unable to load servers</div><div class="server-members">Please refresh the page and try again.</div></div></div></div>';
    });
}

let activeGuildId = null;
function openServerSettings(id, name) {
    activeGuildId = id;
    document.getElementById('settings-guild-name').textContent = 'Settings: ' + name;
    showPage('server-settings');
    
    // Load current config
    fetch('/api/config/' + id)
    .then(r => r.json())
    .then(data => {
        document.getElementById('config-prefix').value = data.prefix || '!';
        document.getElementById('config-auto-enabled').checked = !!data.autoFriendlyEnabled;
        document.getElementById('config-auto-interval').value = data.autoFriendlyInterval || 40;
        document.getElementById('config-post-enabled').checked = data.postEnabled !== false;
        document.getElementById('config-friendly-text').value = data.friendlyText || "";
        
        // Auto-resize the textarea after setting value
        const friendlyTextarea = document.getElementById('config-friendly-text');
        setTimeout(() => autoResize(friendlyTextarea), 50);
        
        document.getElementById('config-cleanup-hours').value = data.cleanupHours || 2;
        document.getElementById('config-lineup-cleanup-hours').value = data.lineupCleanupHours || 2;
        document.getElementById('config-reminders-enabled').checked = !!data.remindersEnabled;
        document.getElementById('config-reminders-lead').value = data.remindersLead || 30;
        
        // Load channels and set selected
        fetch('/api/channels/' + id)
        .then(cr => cr.json())
        .then(channels => {
            const autoSelect = document.getElementById('config-auto-channel');
            const postSelect = document.getElementById('config-post-channel');
            
            autoSelect.innerHTML = '<option value="">Select a channel...</option>';
            postSelect.innerHTML = '<option value="">Default (System Channel)</option>';
            
            channels.forEach(c => {
                const optA = document.createElement('option');
                optA.value = c.id;
                optA.textContent = '#' + c.name;
                if (c.id === data.autoFriendlyChannel) optA.selected = true;
                autoSelect.appendChild(optA);

                const optP = document.createElement('option');
                optP.value = c.id;
                optP.textContent = '#' + c.name;
                if (c.id === data.postChannelId) optP.selected = true;
                postSelect.appendChild(optP);
            });
        });
    });
}

function saveConfig() {
    if (!activeGuildId) return;
    const prefix = document.getElementById('config-prefix').value;
    const autoFriendlyEnabled = document.getElementById('config-auto-enabled').checked;
    const autoFriendlyInterval = document.getElementById('config-auto-interval').value;
    const autoFriendlyChannel = document.getElementById('config-auto-channel').value;
    const postEnabled = document.getElementById('config-post-enabled').checked;
    const postChannelId = document.getElementById('config-post-channel').value;
    const friendlyText = document.getElementById('config-friendly-text').value;
    
    const cleanupHours = document.getElementById('config-cleanup-hours').value;
    const lineupCleanupHours = document.getElementById('config-lineup-cleanup-hours').value;
    const remindersEnabled = document.getElementById('config-reminders-enabled').checked;
    const remindersLead = document.getElementById('config-reminders-lead').value;
    
    fetch('/api/config/' + activeGuildId, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ 
            prefix,
            autoFriendlyEnabled,
            autoFriendlyInterval,
            autoFriendlyChannel,
            postEnabled,
            postChannelId,
            friendlyText,
            cleanupHours,
            lineupCleanupHours,
            remindersEnabled,
            remindersLead
        })
    })
    .then(r => r.json())
    .then(data => {
        if (data.success) {
            alert('✅ Settings saved successfully!');
        } else {
            alert('❌ Error: ' + data.error);
        }
    }).catch(() => alert('❌ Network error saving settings.'));
}

function loadCommands() {
    return fetch('/api/commands')
    .then(r => r.json())
    .then(data => {
        const container = document.getElementById('command-list');
        container.innerHTML = '';
        data.forEach(c => {
            const badge = c.admin ? '<span class="cmd-badge admin">Admin</span>'
                        : c.staff ? '<span class="cmd-badge staff">Staff</span>'
                        : '<span class="cmd-badge public">Public</span>';
            container.innerHTML += `
                <div class="cmd-row">
                    <div>
                        <div class="cmd-name">/${c.name}</div>
                        <div class="cmd-desc">${escapeHtml(c.description)}</div>
                    </div>
                    ${badge}
                </div>`;
        });
    }).catch(() => {});
}

function loadLogs() {
    return fetch('/api/logs')
    .then(r => r.json())
    .then(data => {
        const area = document.getElementById('log-area');
        area.innerHTML = '';
        (data.logs || []).forEach(line => {
            const cls = line.includes('Error') || line.includes('CRITICAL') ? 'error'
                      : line.includes('Warning') || line.includes('Deprecation') ? 'warn' : '';
            area.innerHTML += `<div class="log-line ${cls}">${escapeHtml(line)}</div>`;
        });
        area.scrollTop = area.scrollHeight;
    }).catch(() => {});
}

function triggerAction(action) {
    fetch('/api/action/' + action, { method: 'POST' })
    .then(r => r.json())
    .then(data => {
        alert(data.message || 'Action triggered!');
        loadStats();
    }).catch(() => alert('Failed to trigger action.'));
}

function escapeHtml(str) {
    const d = document.createElement('div');
    d.textContent = str;
    return d.innerHTML;
}

function autoResize(el) {
    el.style.height = 'auto';
    el.style.height = el.scrollHeight + 'px';
}
</script>
</body>
</html>