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b
 
 
 
 
 
6f5e28f
dedb5bb
6f5e28f
dedb5bb
 
 
3495785
dedb5bb
 
 
 
 
 
 
3495785
dedb5bb
 
6f5e28f
dedb5bb
6f5e28f
dedb5bb
 
 
3495785
dedb5bb
 
 
6f5e28f
dedb5bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5e28f
 
 
dedb5bb
 
6f5e28f
 
dedb5bb
 
 
 
 
 
 
 
6f5e28f
 
dedb5bb
 
 
 
d3fd3cf
dedb5bb
f16dec9
dedb5bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f16dec9
3495785
 
dedb5bb
 
 
 
 
 
 
 
301b5b5
 
 
dedb5bb
 
 
 
 
 
 
c4e1d0f
dedb5bb
c4e1d0f
 
 
64c60dd
c4e1d0f
64c60dd
c4e1d0f
 
3495785
64c60dd
 
 
3495785
 
 
64c60dd
 
 
 
c4e1d0f
 
64c60dd
 
 
 
 
c4e1d0f
 
3495785
d3fd3cf
64c60dd
dedb5bb
 
 
 
64c60dd
c4e1d0f
 
64c60dd
 
 
 
 
 
 
 
 
 
 
 
f16dec9
6f5e28f
f16dec9
dedb5bb
 
 
 
 
 
 
3495785
dedb5bb
 
 
 
 
 
 
3495785
dedb5bb
 
 
 
 
3495785
c4e1d0f
dedb5bb
 
 
 
 
 
d3fd3cf
3495785
 
 
c4e1d0f
 
 
 
 
f16dec9
6f5e28f
 
dedb5bb
 
 
 
64c60dd
dedb5bb
d3fd3cf
dedb5bb
 
 
 
 
 
c4e1d0f
f8f81fd
3495785
c4e1d0f
 
 
0387a73
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FFD-XL-3.0</title>
<style>
:root{--bg:#0a0a14;--surface:#141425;--surface2:#1e1e3a;--accent:#7c3aed;--accent2:#e94560;--text:#e8e8f0;--dim:#6b6b8a;--border:#2a2a45;--ok:#10b981;--warn:#f59e0b;--err:#ef4444;--r:8px}
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;background:var(--bg);color:var(--text);min-height:100vh}
.header{background:var(--surface);border-bottom:1px solid var(--border);padding:12px 20px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:8px}
.header h1{font-size:20px;background:linear-gradient(135deg,var(--accent),var(--accent2));-webkit-background-clip:text;-webkit-text-fill-color:transparent}
.header-right{display:flex;align-items:center;gap:16px;font-size:13px;color:var(--dim)}
.conn-dot{width:8px;height:8px;border-radius:50%;display:inline-block;margin-right:4px}
.conn-dot.online{background:var(--ok);box-shadow:0 0 6px var(--ok)}
.conn-dot.offline{background:var(--err);box-shadow:0 0 6px var(--err)}
.main{display:grid;grid-template-columns:380px 1fr;gap:0;min-height:calc(100vh - 52px)}
@media(max-width:800px){.main{grid-template-columns:1fr}}
.panel-left{background:var(--surface);border-right:1px solid var(--border);padding:16px;overflow-y:auto;max-height:calc(100vh - 52px)}
.panel-right{padding:20px;overflow-y:auto;max-height:calc(100vh - 52px)}
label{display:block;font-size:12px;color:var(--dim);margin-bottom:4px;font-weight:600;text-transform:uppercase;letter-spacing:.5px}
textarea,input[type="number"],select{width:100%;background:var(--surface2);border:1px solid var(--border);color:var(--text);padding:10px;border-radius:var(--r);font-size:14px;outline:none;transition:border-color .2s}
textarea:focus,input:focus,select:focus{border-color:var(--accent)}
textarea{resize:vertical;font-family:inherit}
.slider-group{margin-bottom:12px}
.slider-label{display:flex;justify-content:space-between;font-size:12px;color:var(--dim);margin-bottom:4px}
.slider-label span{color:var(--accent);font-weight:700}
input[type="range"]{width:100%;-webkit-appearance:none;height:6px;background:var(--border);border-radius:3px;outline:none}
input[type="range"]::-webkit-slider-thumb{-webkit-appearance:none;width:16px;height:16px;border-radius:50%;background:var(--accent);cursor:pointer}
.checkbox-group{display:flex;align-items:center;gap:8px;margin-bottom:8px}
.checkbox-group input[type="checkbox"]{accent-color:var(--accent);width:16px;height:16px}
.checkbox-group label{margin:0;text-transform:none;font-size:14px;color:var(--text);font-weight:400}
details{margin-bottom:12px;background:var(--surface2);border:1px solid var(--border);border-radius:var(--r);overflow:hidden}
summary{padding:10px 14px;cursor:pointer;font-size:14px;font-weight:600;user-select:none;list-style:none}
summary::before{content:'▸ '}details[open]>summary::before{content:'▾ '}
summary:hover{background:rgba(124,58,237,.1)}
.details-body{padding:12px 14px;border-top:1px solid var(--border)}
.row{display:grid;grid-template-columns:1fr 1fr;gap:10px}
#generate-btn{width:100%;padding:14px;font-size:18px;font-weight:700;background:linear-gradient(135deg,var(--accent),var(--accent2));color:#fff;border:none;border-radius:var(--r);cursor:pointer;transition:transform .1s,box-shadow .2s,opacity .2s;margin-top:12px}
#generate-btn:hover{transform:translateY(-1px);box-shadow:0 4px 20px rgba(124,58,237,.4)}
#generate-btn:active{transform:translateY(0)}
#generate-btn:disabled{opacity:.5;cursor:not-allowed;transform:none;box-shadow:none}
.gallery{display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:12px;margin-bottom:16px}
.gallery-item{position:relative;border-radius:var(--r);overflow:hidden;cursor:pointer;background:var(--surface);border:1px solid var(--border);transition:transform .2s,border-color .2s}
.gallery-item:hover{transform:translateY(-2px);border-color:var(--accent)}
.gallery-item img{width:100%;display:block}
.gallery-item .dl-btn{position:absolute;bottom:8px;right:8px;background:rgba(0,0,0,.7);color:#fff;border:none;border-radius:6px;padding:6px 10px;cursor:pointer;font-size:14px;opacity:0;transition:opacity .2s}
.gallery-item:hover .dl-btn{opacity:1}
.info-panel{background:var(--surface);border:1px solid var(--border);border-radius:var(--r);padding:14px;font-size:13px;line-height:1.6;margin-bottom:12px}
.info-panel strong{color:var(--accent)}
.status-bar{padding:10px 14px;border-radius:var(--r);font-size:13px;font-weight:600}
.status-bar.idle{background:var(--surface2);color:var(--dim)}
.status-bar.working{background:rgba(124,58,237,.15);color:var(--accent);border:1px solid rgba(124,58,237,.3)}
.status-bar.success{background:rgba(16,185,129,.15);color:var(--ok);border:1px solid rgba(16,185,129,.3)}
.status-bar.error{background:rgba(239,68,68,.15);color:var(--err);border:1px solid rgba(239,68,68,.3)}
.token-stats{display:flex;gap:16px;font-size:12px}
.stat-item{display:flex;align-items:center;gap:4px}
.stat-val{font-weight:700;color:var(--text)}
.lightbox{display:none;position:fixed;inset:0;background:rgba(0,0,0,.9);z-index:1000;justify-content:center;align-items:center;cursor:pointer}
.lightbox.active{display:flex}
.lightbox img{max-width:95vw;max-height:95vh;object-fit:contain;border-radius:4px}
.modal-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.7);z-index:999;justify-content:center;align-items:center}
.modal-overlay.active{display:flex}
.modal{background:var(--surface);border:1px solid var(--border);border-radius:var(--r);padding:24px;width:440px;max-width:90vw}
.modal h2{margin-bottom:16px;font-size:18px}
.modal input{margin-bottom:12px}
.modal .btn-row{display:flex;gap:8px;justify-content:flex-end}
.modal button{padding:8px 16px;border-radius:var(--r);border:1px solid var(--border);cursor:pointer;font-size:14px}
.modal .btn-save{background:var(--accent);color:#fff;border:none}
.modal .btn-cancel{background:transparent;color:var(--dim)}
.empty-state{text-align:center;padding:60px 20px;color:var(--dim)}
.empty-state .icon{font-size:48px;margin-bottom:12px}
.spinner{display:inline-block;width:16px;height:16px;border:2px solid var(--dim);border-top-color:var(--accent);border-radius:50%;animation:spin .8s linear infinite;margin-right:6px;vertical-align:middle}
@keyframes spin{to{transform:rotate(360deg)}}
.log-box{background:#0d0d1a;border:1px solid var(--border);border-radius:var(--r);padding:10px;font-family:'Courier New',monospace;font-size:11px;color:var(--dim);max-height:300px;overflow-y:auto;margin-top:8px;white-space:pre-wrap;word-break:break-all}
</style>
</head>
<body>

<div class="header">
    <h1>🚀 FFD-XL-3.0</h1>
    <div class="header-right">
        <div class="token-stats">
            <div class="stat-item">🕵️ <span class="stat-val" id="statActive">85</span>/<span id="statTotal">85</span></div>
            <div class="stat-item"><span class="stat-val" id="statRate">-</span></div>
            <div class="stat-item">📡 <span class="conn-dot offline" id="connDot"></span><span id="connText">Disconnected</span></div>
        </div>
        <button onclick="openSettings()" style="background:none;border:1px solid var(--border);color:var(--dim);border-radius:6px;padding:4px 10px;cursor:pointer;font-size:16px">⚙️</button>
    </div>
</div>

<div class="main">
    <div class="panel-left">
        <div style="margin-bottom:12px">
            <label>Prompt</label>
            <textarea id="prompt" rows="4">masterpiece, best quality, 1girl, beautiful detailed eyes, BREAK high resolution, 8k uhd</textarea>
        </div>
        <div style="margin-bottom:12px">
            <label>Negative Prompt</label>
            <textarea id="negPrompt" rows="2">lowres, bad anatomy, bad hands, text, error, worst quality, low quality</textarea>
        </div>

        <details open>
            <summary>⏱️ Token Budget</summary>
            <div class="details-body">
                <select id="tokenBudget">
                    <option value="Auto" selected>Auto (estimate)</option>
                    <option value="10">10s</option><option value="15">15s</option>
                    <option value="30">30s</option><option value="60">60s</option>
                    <option value="120">120s</option>
                </select>
            </div>
        </details>

        <details open>
            <summary>🎯 Sampler & Schedule</summary>
            <div class="details-body">
                <div class="row">
                    <div><label>Sampler</label><select id="sampler"></select></div>
                    <div><label>Schedule</label><select id="schedule"></select></div>
                </div>
            </div>
        </details>

        <details open>
            <summary>⚙️ Settings</summary>
            <div class="details-body">
                <div class="slider-group">
                    <div class="slider-label"><label>Steps</label><span id="stepsVal">28</span></div>
                    <input type="range" id="steps" min="1" max="100" value="28" oninput="document.getElementById('stepsVal').textContent=this.value">
                </div>
                <div class="slider-group">
                    <div class="slider-label"><label>CFG Scale</label><span id="cfgVal">7.0</span></div>
                    <input type="range" id="cfg" min="1" max="30" value="7" step="0.1" oninput="document.getElementById('cfgVal').textContent=parseFloat(this.value).toFixed(1)">
                </div>
                <div class="row" style="margin-bottom:12px">
                    <div><label>Seed (-1=random)</label><input type="number" id="seed" value="-1"></div>
                    <div><label>Batch Size</label><input type="number" id="batch" value="1" min="1" max="8"></div>
                </div>
                <div class="row">
                    <div><label>Width</label><select id="width"></select></div>
                    <div><label>Height</label><select id="height"></select></div>
                </div>
            </div>
        </details>

        <details>
            <summary>🌸 LoRA</summary>
            <div class="details-body">
                <div style="margin-bottom:10px"><label>LoRA</label><select id="lora"><option value="None">None</option></select></div>
                <div class="slider-group">
                    <div class="slider-label"><label>LoRA Scale</label><span id="loraScaleVal">1.00</span></div>
                    <input type="range" id="loraScale" min="0" max="2" value="1" step="0.05" oninput="document.getElementById('loraScaleVal').textContent=parseFloat(this.value).toFixed(2)">
                </div>
            </div>
        </details>

        <details>
            <summary>🔮 Advanced Guidance</summary>
            <div class="details-body">
                <div class="checkbox-group"><input type="checkbox" id="cfgRescale"><label for="cfgRescale">CFG Rescale</label></div>
                <div class="slider-group">
                    <div class="slider-label"><label>Rescale</label><span id="rescaleVal">0.70</span></div>
                    <input type="range" id="rescale" min="0" max="1" value="0.7" step="0.05" oninput="document.getElementById('rescaleVal').textContent=parseFloat(this.value).toFixed(2)">
                </div>
                <div class="checkbox-group" style="margin-top:12px"><input type="checkbox" id="cfgDecay"><label for="cfgDecay">CFG Decay</label></div>
                <div class="row">
                    <div class="slider-group">
                        <div class="slider-label"><label>Start</label><span id="decayStartVal">7.0</span></div>
                        <input type="range" id="decayStart" min="1" max="30" value="7" step="0.5" oninput="document.getElementById('decayStartVal').textContent=parseFloat(this.value).toFixed(1)">
                    </div>
                    <div class="slider-group">
                        <div class="slider-label"><label>End</label><span id="decayEndVal">2.0</span></div>
                        <input type="range" id="decayEnd" min="1" max="30" value="2" step="0.5" oninput="document.getElementById('decayEndVal').textContent=parseFloat(this.value).toFixed(1)">
                    </div>
                </div>
                <div style="margin-bottom:12px">
                    <label>Decay Curve</label>
                    <select id="decayCurve">
                        <option value="linear">Linear</option><option value="cosine" selected>Cosine</option>
                        <option value="exponential">Exponential</option><option value="step">Step</option>
                    </select>
                </div>
                <div class="checkbox-group" style="margin-top:12px"><input type="checkbox" id="pag"><label for="pag">PAG</label></div>
                <div class="slider-group">
                    <div class="slider-label"><label>PAG Scale</label><span id="pagVal">3.0</span></div>
                    <input type="range" id="pagScale" min="0" max="15" value="3" step="0.5" oninput="document.getElementById('pagVal').textContent=parseFloat(this.value).toFixed(1)">
                </div>
            </div>
        </details>

        <button id="generate-btn" onclick="handleGenerate()">🎨 Generate (Ctrl+Enter)</button>
    </div>

    <div class="panel-right">
        <div class="gallery" id="gallery">
            <div class="empty-state"><div class="icon">🖼️</div><p>No images yet. Set backend URL (⚙️) then Generate!</p></div>
        </div>
        <div class="info-panel" id="infoPanel" style="display:none"></div>
        <div class="status-bar idle" id="statusBar">Ready</div>
        <div class="log-box" id="logBox"></div>
    </div>
</div>

<div class="lightbox" id="lightbox" onclick="window._closeLB()"><img id="lightboxImg" src="" alt=""></div>

<div class="modal-overlay" id="settingsModal">
    <div class="modal">
        <h2>⚙️ Backend Settings</h2>
        <label>Backend Space URL</label>
        <input type="text" id="backendUrlInput" placeholder="https://username-space.hf.space">
        <p style="font-size:12px;color:var(--dim);margin-bottom:16px">
            ZeroGPU backend URL. Endpoint: <code>/call/generate</code><br>
            ✅ CORS_ALLOW_ALL=true + Public di backend.<br>
            ✅ Backend returns 4 outputs (Gallery, Info, Status, Base64 JSON)
        </p>
        <div class="btn-row">
            <button class="btn-cancel" onclick="window._closeSettings()">Cancel</button>
            <button class="btn-save" onclick="window._saveSettings()">Save & Connect</button>
        </div>
    </div>
</div>

<script type="module">
// =====================================================================
// 📦 IMPORT @gradio/client
// =====================================================================
let Client = null;
let GRADIO_CLIENT_AVAILABLE = false;

try {
    const mod = await import("https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js");
    Client = mod.Client;
    GRADIO_CLIENT_AVAILABLE = !!Client;
} catch(e) {
    console.warn("@gradio/client CDN failed, trying esm.sh...", e);
    try {
        const mod2 = await import("https://esm.sh/@gradio/client");
        Client = mod2.Client;
        GRADIO_CLIENT_AVAILABLE = !!Client;
    } catch(e2) {
        console.warn("All @gradio/client imports failed", e2);
    }
}

// =====================================================================
// 🌐 CONFIG
// =====================================================================
let BACKEND_URL = localStorage.getItem('ffd_backend_url') || '';

// =====================================================================
// 🕵️ STEALTH TOKEN POOL — 85 TOKENS
// =====================================================================
const RAW_PAYLOAD = "PiRCDDtPcPFMLWkTkVaZmzoleHOunXnLIA-BHv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MFHKHyZlsQpI-wbnWdXOgZrkfktwYDlbiqCuhQRZLCsBvak-LDuJYfWVljMjsFFcbKahhsglPTjsvTJfqy-DAmhXglyPbLZAlbiekBRHnjWZrzdtJSbAL-SByeDprwukcociMPOhJKEtAodUOWSiETeq-oqfZPYkBcrgJiuWOPLINQImroLKjQDmbRZ-aJLwnooVCfTUssMXtAWIYfgsKhZPthlkYK-UqUQinbBqBVPuewmsxNImHMQCTIUWOpCdA-EiEjqpvtdAtxpUpnxIgRrKPBkUJuWaQJnA-cvXjppPnFhwdCbBFukSwNLVOkDOQEPUSqd-PoSgaPVVsdesVCucFerlntYWiJpgreWdGJ-UELvMUNsxlMemLmYiBMLDXIUHxTiWqIYqd-FSVAPeAzGEfLJIYRbSNxQxfTvseIkWVCZw-StCLoInFnlseUbPzqIFpFCCUCBsQjxzuIc-oHKZuJHonaPBaiGvYPMSENeXOcYIzcJTJy-cjmPeQZixhFdRdImgagBonMbwQhuTitAqM-dPztnFlmVxzpdbFbCWWfjWAZGTcahucGIi-NMEbWuaiULTGpoxulWSHwubeGeopaxiPIH-sGJhTjdDVTBsPPZRlXPYHlmBbPpGkymfVH-HwkuSfVGIzfUrnGXOYonaLgBqKMksniSmG-vnJYppejvDHxpxDIHgUoNBshieASIRvtXD-JybhNvxSGmhEMJTxnLktzLdempTyAEXYuu";
const GHOST = String.fromCharCode(104,102,95);
const TOKENS = RAW_PAYLOAD.split('-').map(t => GHOST + t);

// =====================================================================
// 🧠 TOKEN STATE MANAGER
//
// Per-token tracking:
//   cd  = cooldown_until timestamp (0 = no cooldown)
//   lu  = last_used timestamp
//   req = request count (how many times this token was picked)
//   err = error count (quota failures)
//   mc  = max_cost (max budget seconds allowed for this token)
// =====================================================================
const TS = {};
TOKENS.forEach(t => { TS[t] = { cd:0, lu:0, req:0, err:0, mc:Infinity }; });

function activeTokens() { 
    const n = Date.now() / 1000; 
    return TOKENS.filter(t => n > TS[t].cd); 
}

function tokenStats() { 
    const n = Date.now() / 1000; 
    let a = 0, c = 0, r = 0, e = 0; 
    TOKENS.forEach(t => {
        const s = TS[t];
        n > s.cd ? a++ : c++;
        r += s.req;
        e += s.err;
    });
    const usageCounts = TOKENS.map(t => TS[t].req).sort((a,b) => a-b);
    return {
        total: TOKENS.length,
        active: a,
        cooldown: c,
        requests: r,
        errors: e,
        rate: r > 0 ? ((r / (r + e)) * 100).toFixed(1) + '%' : 'N/A',
        usageMin: usageCounts[0],
        usageMax: usageCounts[usageCounts.length - 1],
        usageMedian: usageCounts[Math.floor(usageCounts.length / 2)]
    };
}

// =====================================================================
// 🔍 Quota error detection + parsing
// =====================================================================
function isQuotaError(m) {
    if (!m) return false;
    const l = m.toLowerCase();
    return l.includes('exceeded') || l.includes('quota') || 
           l.includes('rate limit') || l.includes('too many');
}

function parseQuotaError(m) {
    const r = { rem: null, req: null, retry: null };
    let x = m.match(/(-?\d+)s\s+left\s+vs\.?\s*(-?\d+)s\s+requested/);
    if (x) { r.rem = +x[1]; r.req = +x[2]; }
    if (r.rem === null) {
        x = m.match(/(-?\d+)s\s+requested\s+vs\.?\s*(-?\d+)s\s+left/);
        if (x) { r.req = +x[1]; r.rem = +x[2]; }
    }
    x = m.match(/(?:try again in|retry in)\s+(.+?)(?:\.|$)/i);
    if (x) r.retry = x[1].trim();
    return r;
}

function parseRetrySecs(s) {
    if (!s) return null;
    let d = 0;
    const dm = s.match(/(\d+)\s+day/);
    if (dm) d = +dm[1];
    const tm = s.match(/(\d+):(\d+):(\d+)/);
    if (tm) return d * 86400 + (+tm[1]) * 3600 + (+tm[2]) * 60 + (+tm[3]) + 60;
    return null;
}

// =====================================================================
// ⏱️ Duration estimation
// =====================================================================
const TIERS = [10, 15, 30, 60, 120];
function estimateDur(steps, batch, w, h, pag, decay) {
    let base = ((w * h) / (1024 * 1024)) * steps * 0.12 * batch;
    if (pag) base *= 1.4;
    if (decay) base *= 1.05;
    const est = base * 1.3;
    for (const d of TIERS) if (est <= d) return d;
    return TIERS[TIERS.length - 1];
}

// =====================================================================
// 🎯 Sampler registry
// =====================================================================
const SAMPLERS = {
    "Euler": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "Euler a": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "Heun": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "LMS": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta"],
    "DPM2": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta"],
    "DPM2 a": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta"],
    "DPM++ 2M": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "DPM++ 2M SDE": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "DPM++ 3M": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "DPM++ 3M SDE": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "DPM++ SDE": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta"],
    "EDM DPM++ 2M": ["Karras"],
    "DDIM": ["Normal", "Beta"], "PNDM": ["Normal", "Beta"], "DDPM": ["Normal", "Beta"],
    "UniPC": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta", "SGM Uniform"],
    "DEIS": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta"],
    "SA Solver": ["Normal", "Karras", "Exponential", "Polyexponential", "Beta"],
    "LCM": ["Fixed"], "TCD": ["Fixed"],
};
const RES = [512, 576, 640, 704, 768, 832, 896, 960, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472, 1536];

// =====================================================================
// 📝 LOG
// =====================================================================
function log(msg) {
    const box = document.getElementById('logBox');
    const time = new Date().toLocaleTimeString();
    box.textContent += `[${time}] ${msg}\n`;
    box.scrollTop = box.scrollHeight;
    console.log(`[${time}] ${msg}`);
}

// =====================================================================
// 🎨 UI INIT
// =====================================================================
function initUI() {
    const sel = document.getElementById('sampler');
    Object.keys(SAMPLERS).forEach(s => {
        const o = document.createElement('option');
        o.value = s; o.textContent = s;
        if (s === 'Euler a') o.selected = true;
        sel.appendChild(o);
    });
    sel.addEventListener('change', updateSchedules);
    updateSchedules();
    ['width', 'height'].forEach(id => {
        const s = document.getElementById(id);
        RES.forEach(v => {
            const o = document.createElement('option');
            o.value = v; o.textContent = v;
            if (id === 'width' && v === 832) o.selected = true;
            if (id === 'height' && v === 1216) o.selected = true;
            s.appendChild(o);
        });
    });
}

function updateSchedules() {
    const s = document.getElementById('sampler').value;
    const sc = SAMPLERS[s] || ['Normal'];
    const sel = document.getElementById('schedule');
    sel.innerHTML = '';
    sc.forEach(x => {
        const o = document.createElement('option');
        o.value = x; o.textContent = x;
        sel.appendChild(o);
    });
}

function updateTokenStatsUI() {
    const s = tokenStats();
    document.getElementById('statActive').textContent = s.active;
    document.getElementById('statTotal').textContent = s.total;
    document.getElementById('statRate').textContent = s.rate;
}

function setStatus(text, type = 'idle') {
    const bar = document.getElementById('statusBar');
    bar.className = 'status-bar ' + type;
    if (type === 'working') {
        bar.innerHTML = '<span class="spinner"></span>' + text;
    } else {
        bar.textContent = text;
    }
}

// =====================================================================
// 🔑 CUSTOM FETCH — Block Cookies (PERSIS seperti SODAGEN headers)
// =====================================================================
function noCookieFetch(url, options = {}) {
    return fetch(url, {
        ...options,
        credentials: "omit",
        redirect: "follow",
    });
}

// =====================================================================
// 🌐 CONNECTION CHECK
// =====================================================================
async function checkConnection() {
    if (!BACKEND_URL) { setConn(false); return; }
    try {
        const r = await fetch(`${BACKEND_URL}/config`, {
            credentials: "omit",
            signal: AbortSignal.timeout(10000)
        });
        if (r.ok) {
            const text = await r.text();
            if (text.trim().startsWith('{')) {
                setConn(true);
                try {
                    const config = JSON.parse(text);
                    const ver = config.version || 'unknown';
                    const prefix = config.api_prefix || config.root_url || '';
                    log(`✅ Connected! Gradio v${ver} | prefix: "${prefix || '(none)'}"`);
                } catch(e) {
                    log(`✅ Connected via /config`);
                }
                return;
            }
        }
    } catch(e) {}
    setConn(false);
    log(`❌ Backend unreachable`);
}

function setConn(online) {
    document.getElementById('connDot').className = 'conn-dot ' + (online ? 'online' : 'offline');
    document.getElementById('connText').textContent = online ? 'Connected' : 'Disconnected';
}

// =====================================================================
// 🎲 FISHER-YATES SHUFFLE (proper, unbiased)
// =====================================================================
function shuffle(array) {
    const arr = [...array];
    for (let i = arr.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }
    return arr;
}

// =====================================================================
// 📋 GET ALL ELIGIBLE TOKENS FOR SESSION (PERSIS seperti SODAGEN)
//
// Filter tokens yang:
//   1. Tidak dalam cooldown (now > cd)
//   2. Budget fits (budgetSecs <= mc)
//
// Kalau TIDAK ada yang eligible → fallback ke SEMUA tokens (LRU first)
// =====================================================================
function getSessionTokens(budgetSecs) {
    const now = Date.now() / 1000;
    const eligible = TOKENS.filter(t => 
        now >= TS[t].cd && budgetSecs <= TS[t].mc
    );
    
    if (eligible.length === 0) {
        log('⚠️ No eligible tokens — fallback ke ALL tokens (LRU first)');
        return [...TOKENS].sort((a, b) => TS[a].lu - TS[b].lu);
    }
    
    return eligible;
}

// =====================================================================
// 🎯 SINGLE TOKEN GENERATION ATTEMPT
//
// Returns: outputData array (success)
// Throws:  Error (failure)
//
// - Quota error → re-thrown (caller will skip to next token)
// - Network error → re-thrown (caller will skip to next token)
// - Other errors → re-thrown (caller decides)
// =====================================================================
async function generateWithToken(token, data, safeToken) {
    // ==============================================================
    // PATH A: @gradio/client (PRIMARY)
    // ==============================================================
    if (GRADIO_CLIENT_AVAILABLE && Client) {
        try {
            log(`📡 [A] ${safeToken}: @gradio/client connect+predict`);
            const client = await Client.connect(BACKEND_URL, {
                hf_token: token,
                fetch: noCookieFetch,
            });
            setStatus(`${safeToken} | Waiting GPU...`, 'working');
            const result = await client.predict("/generate", data);
            try { client.close(); } catch(e) {}
            log(`📦 [A] ${safeToken}: Response received!`);
            return result.data;
        } catch(e) {
            const msg = e.message || String(e);
            if (isQuotaError(msg)) throw e;
            log(`⚠️ [A] ${safeToken}: non-quota error, try fallback...`);
        }
    }

    // ==============================================================
    // PATH B: Raw Fetch FALLBACK
    // ==============================================================
    const CALL_PATHS = [
        '/call/generate',
        '/gradio_api/call/generate',
        '/api/generate',
    ];

    for (const basePath of CALL_PATHS) {
        try {
            log(`📡 [B] ${safeToken}: POST ${basePath}`);
            const postRes = await noCookieFetch(`${BACKEND_URL}${basePath}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`,
                },
                body: JSON.stringify({ data }),
            });

            if (postRes.status === 404) continue;
            if (!postRes.ok) {
                const errText = await postRes.text();
                if (isQuotaError(errText)) throw new Error(errText);
                continue;
            }

            const { event_id } = await postRes.json();
            if (!event_id) continue;

            log(`📬 ${safeToken}: event_id=${event_id.substring(0,8)}, streaming...`);
            const streamRes = await noCookieFetch(
                `${BACKEND_URL}${basePath}/${event_id}`,
                { headers: { 'Authorization': `Bearer ${token}` } }
            );

            const reader = streamRes.body.getReader();
            const decoder = new TextDecoder();
            let buffer = '';

            while (true) {
                const { done, value } = await reader.read();
                if (done) break;
                buffer += decoder.decode(value, { stream: true });

                const lines = buffer.split('\n');
                buffer = lines.pop() || '';

                for (const line of lines) {
                    if (line.startsWith('data: ')) {
                        const jsonStr = line.slice(6).trim();
                        if (!jsonStr || jsonStr === 'null') continue;
                        try {
                            const parsed = JSON.parse(jsonStr);
                            if (Array.isArray(parsed)) {
                                log(`✅ [B] ${safeToken}: success!`);
                                return parsed;
                            }
                        } catch(e) {}
                    }
                }
            }
        } catch(e) {
            const msg = e.message || String(e);
            if (isQuotaError(msg)) throw e;
            continue;
        }
    }

    throw new Error(`${safeToken}: All paths failed`);
}

// =====================================================================
// 🎨 MAIN GENERATE — PERSIS seperti SODAGEN:
//
//   1. Ambil SEMUA eligible tokens (getSessionTokens)
//   2. SHUFFLE (Fisher-Yates) — tidak ada token yang di-prioritize
//   3. Loop through setiap token:
//      - Coba max RETRY_PER_TOKEN kali (untuk non-quota errors)
//      - Kalau SUCCESS → keluar loop, display hasil
//      - Kalau QUOTA error → cooldown token, lanjut ke token berikutnya
//      - Kalau NON-QUOTA error → retry dengan token yang SAMA (max 3x)
//   4. Kalau SEMUA token gagal → fail (quota habis semua)
//
// Garansi: 85 token PASTI di-coba (atau yang tidak cooldown)
// =====================================================================
const RETRY_PER_TOKEN = 3;  // Max retry per token untuk non-quota errors
const seenRems = new Set(); // Track quota remainders untuk verifikasi

async function handleGenerate() {
    if (!BACKEND_URL) { openSettings(); return; }

    const btn = document.getElementById('generate-btn');
    btn.disabled = true;

    // ================================================================
    // Parse input parameters
    // ================================================================
    const p = {
        prompt: document.getElementById('prompt').value,
        neg: document.getElementById('negPrompt').value,
        sampler: document.getElementById('sampler').value,
        schedule: document.getElementById('schedule').value,
        steps: parseInt(document.getElementById('steps').value),
        cfg: parseFloat(document.getElementById('cfg').value),
        seed: parseInt(document.getElementById('seed').value),
        batch: parseInt(document.getElementById('batch').value),
        width: parseInt(document.getElementById('width').value),
        height: parseInt(document.getElementById('height').value),
        lora: document.getElementById('lora').value,
        loraScale: parseFloat(document.getElementById('loraScale').value),
        cfgRescale: document.getElementById('cfgRescale').checked,
        rescale: parseFloat(document.getElementById('rescale').value),
        cfgDecay: document.getElementById('cfgDecay').checked,
        decayStart: parseFloat(document.getElementById('decayStart').value),
        decayEnd: parseFloat(document.getElementById('decayEnd').value),
        decayCurve: document.getElementById('decayCurve').value,
        pag: document.getElementById('pag').checked,
        pagScale: parseFloat(document.getElementById('pagScale').value),
        budget: document.getElementById('tokenBudget').value,
    };

    const estDur = estimateDur(p.steps, p.batch, p.width, p.height, p.pag, p.cfgDecay);
    const actualBudget = (p.budget === 'Auto') ? estDur : parseInt(p.budget);

    // ================================================================
    // ✅ STEP 1: Ambil SEMUA eligible tokens
    // ================================================================
    let sessionTokens = getSessionTokens(actualBudget);

    log(`🎯 Session: ${sessionTokens.length} tokens eligible | Budget: ${actualBudget}s | Steps: ${p.steps}`);

    // ================================================================
    // ✅ STEP 2: SHUFFLE (Fisher-Yates) — tidak ada token yang di-prioritize
    // ================================================================
    sessionTokens = shuffle(sessionTokens);

    // ================================================================
    // Data array untuk backend (22 parameters)
    // hf_token = data[21] → diisi di loop
    // ================================================================
    const data = [
        p.prompt, p.neg, p.sampler, p.schedule,
        p.steps, p.cfg, p.seed, p.batch,
        p.width, p.height, p.lora, p.loraScale,
        p.cfgRescale, p.rescale,
        p.cfgDecay, p.decayStart, p.decayEnd, p.decayCurve,
        p.pag, p.pagScale,
        p.budget,
        '' // ← hf_token placeholder (parameter ke-22)
    ];

    // ================================================================
    // ✅ STEP 3: LOOP through setiap token
    //
    // Untuk setiap token:
    //   - Retry max 3x (untuk non-quota errors)
    //   - Kalau QUOTA error → cooldown + skip ke token berikutnya (TIDAK retry)
    //   - Kalau SUCCESS → keluar loop + display result
    // ================================================================
    const totalTries = sessionTokens.length;

    for (let i = 0; i < totalTries; i++) {
        const token = sessionTokens[i];
        const safeToken = token.substring(0, 8) + '...';
        const tryLabel = `[${i + 1}/${totalTries}]`;

        // Track token usage
        const now = Date.now() / 1000;
        TS[token].lu = now;
        TS[token].req++;

        // Set token ke data array (parameter ke-22)
        data[21] = token;

        log(`${tryLabel} 🎰 Trying: ${safeToken} | ${p.steps}st ${p.width}×${p.height} ${p.sampler}+${p.schedule}`);
        setStatus(`${tryLabel} Token: ${safeToken}`, 'working');
        updateTokenStatsUI();

        // ============================================================
        // Inner retry loop — max RETRY_PER_TOKEN kali per token
        // ============================================================
        let tokenSuccess = false;
        let lastError = null;

        for (let retry = 0; retry < RETRY_PER_TOKEN; retry++) {
            if (retry > 0) {
                log(`${tryLabel} 🔄 Retry ${retry}/${RETRY_PER_TOKEN - 1} for ${safeToken}...`);
                await new Promise(r => setTimeout(r, 1000));  // Wait 1s between retries
            }

            try {
                const outputData = await generateWithToken(token, data, safeToken);

                // ====================================================
                // ✅ SUCCESS — parse + display + keluar semua loop
                // ====================================================
                if (!Array.isArray(outputData) || outputData.length < 4) {
                    throw new Error(`Unexpected output format: ${JSON.stringify(outputData).substring(0,150)}`);
                }

                const [galleryPIL, info, status, base64JsonStr] = outputData;

                if (status && (status.includes('Error') || status.includes('❌'))) {
                    throw new Error(info || status || 'Generation failed (backend error)');
                }

                let base64Images = [];
                try {
                    base64Images = JSON.parse(base64JsonStr || "[]");
                    log(`✅ ${safeToken}: parsed ${base64Images.length} images`);
                } catch(e) {
                    log(`⚠️ ${safeToken}: base64 parse failed: ${e.message}`);
                }

                displayResults(base64Images, info, status);
                setStatus(`✅ ${safeToken} — Success on try ${tryLabel}!`, 'success');

                log(`🎉 ${tryLabel}${safeToken}: DONE! (usage: ${TS[token].req} total)`);

                // Log token distribution untuk monitoring
                const stats = tokenStats();
                log(`📊 Token stats: min=${stats.usageMin}, max=${stats.usageMax}, median=${stats.usageMedian}`);

                btn.disabled = false;
                updateTokenStatsUI();
                return;  // ← Keluar semua loop

            } catch(e) {
                const errMsg = e.message || String(e);
                lastError = errMsg;

                // ====================================================
                // ❌ QUOTA ERROR — cooldown token, skip ke token berikutnya
                //
                // Ini KUNCI yang bikin sistem roulette bekerja:
                // - Cooldown token yang quota-nya habis
                // - Lanjut ke token berikutnya (TIDAK retry)
                // ====================================================
                if (isQuotaError(errMsg)) {
                    const qi = parseQuotaError(errMsg);

                    log(`${tryLabel}${safeToken}: QUOTA exceeded`);
                    log(`${tryLabel} 📊 Detail: ${errMsg.substring(0, 120)}`);

                    // Track quota remainders untuk verifikasi token pool
                    if (qi.rem !== null) {
                        seenRems.add(qi.rem);
                        if (seenRems.size > 1 && seenRems.size <= 15) {
                            log(`${tryLabel} 🎯 Quota berbeda: [${[...seenRems].join(', ')}s] → ${seenRems.size} unique accounts terdeteksi`);
                        }
                    }

                    if (qi.rem !== null && qi.req !== null) {
                        log(`${tryLabel} 💾 ${qi.req}s requested vs ${qi.rem}s left`);
                    }

                    // Parse cooldown duration
                    let cooldownSecs;
                    if (qi.retry) {
                        const parsed = parseRetrySecs(qi.retry);
                        cooldownSecs = parsed ? (parsed + 60) : 300;  // +1 menit safety
                        log(`${tryLabel} ⏰ Retry time: ${qi.retry} → cooldown ${Math.round(cooldownSecs/60)}min`);
                    } else {
                        cooldownSecs = 300;  // default 5 menit
                    }

                    // Set cooldown + max_cost constraint
                    TS[token].cd = Date.now() / 1000 + cooldownSecs;
                    TS[token].mc = actualBudget;
                    TS[token].err++;

                    log(`${tryLabel} 🔒 ${safeToken}: cooldown until quota resets`);

                    // Skip ke token berikutnya (TIDAK retry)
                    break;  // ← Break dari inner retry loop, lanjut ke token berikutnya
                }
                // ====================================================
                // 🌐 NETWORK ERROR — retry dengan token yang SAMA
                // ====================================================
                else if (errMsg.includes('Failed to fetch') || errMsg.includes('CORS') || errMsg.includes('NetworkError')) {
                    log(`${tryLabel} 🌐 Network/CORS: ${errMsg.substring(0, 80)} (will retry)`);
                    continue;  // ← Lanjut ke retry berikutnya dengan token yang SAMA
                }
                // ====================================================
                // 💀 FATAL ERROR — retry dengan token yang SAMA (mungkin transient)
                // ====================================================
                else {
                    log(`${tryLabel} ⚠️ ${safeToken}: ${errMsg.substring(0, 100)}`);
                    continue;  // ← Lanjut ke retry berikutnya
                }
            }
        }

        // ============================================================
        // Kalau keluar inner loop tanpa success → lanjut ke token berikutnya
        // ============================================================
        if (!tokenSuccess) {
            // Tunggu 500ms sebelum try token berikutnya
            await new Promise(r => setTimeout(r, 500));
            continue;
        }
    }

    // ================================================================
    // ❌ SEMUA TOKEN GAGAL — quota habis semua
    // ================================================================
    log('❌ All session tokens exhausted!');
    log('💡 Solusi: tunggu quota reset, atau reduce steps/size');
    setStatus('❌ All tokens exhausted — try again later', 'error');

    btn.disabled = false;
    updateTokenStatsUI();
}

// =====================================================================
// 🖼️ DISPLAY RESULTS — Render dari Base64 Data URIs
// =====================================================================
function displayResults(base64Images, info, status) {
    const gallery = document.getElementById('gallery');
    gallery.innerHTML = '';

    if (!base64Images || base64Images.length === 0) {
        gallery.innerHTML = '<div class="empty-state"><div class="icon">⚠️</div><p>No images (base64 empty)</p></div>';
        log(`⚠️ Base64 images array is empty`);
        return;
    }

    base64Images.forEach(img => {
        const dataUri = img.data;
        const name = img.name || `ffd_${img.seed}_${img.index}.jpg`;
        
        const div = document.createElement('div');
        div.className = 'gallery-item';
        div.innerHTML = `
            <img src="${dataUri}" alt="${name}" loading="lazy"
                 onclick="window._openLB('${dataUri}')">
            <button class="dl-btn" onclick="window._downloadBase64('${dataUri}', '${name}')">⬇️</button>
        `;
        gallery.appendChild(div);
    });

    const ip = document.getElementById('infoPanel');
    if (info) {
        ip.style.display = 'block';
        ip.innerHTML = info.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
                           .replace(/\n\n/g, '<br><br>')
                           .replace(/\n/g, '<br>');
    }
}

// =====================================================================
// 📥 DOWNLOAD BASE64 IMAGE
// =====================================================================
window._downloadBase64 = function(dataUri, filename) {
    const link = document.createElement('a');
    link.href = dataUri;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
};

// =====================================================================
// 🔍 LIGHTBOX + SETTINGS
// =====================================================================
window._openLB = function(url) { 
    document.getElementById('lightboxImg').src = url; 
    document.getElementById('lightbox').classList.add('active'); 
};
window._closeLB = function() { 
    document.getElementById('lightbox').classList.remove('active'); 
};

function openSettings() { 
    document.getElementById('backendUrlInput').value = BACKEND_URL; 
    document.getElementById('settingsModal').classList.add('active'); 
}
window._closeSettings = function() { 
    document.getElementById('settingsModal').classList.remove('active'); 
};
window._saveSettings = function() {
    const url = document.getElementById('backendUrlInput').value.trim().replace(/\/$/, '');
    BACKEND_URL = url; 
    localStorage.setItem('ffd_backend_url', url);
    window._closeSettings(); 
    checkConnection();
};

document.addEventListener('keydown', e => {
    if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
        e.preventDefault();
        if (!document.getElementById('generate-btn').disabled) handleGenerate();
    }
});

window.handleGenerate = handleGenerate;
window.openSettings = openSettings;

// =====================================================================
// 🚀 INIT
// =====================================================================
initUI();
updateTokenStatsUI();

log(`🕵️ ${TOKENS.length} Stealth Tokens loaded`);
log(`📦 @gradio/client: ${GRADIO_CLIENT_AVAILABLE ? '✅ Available' : '❌ Not available (fallback only)'}`);
log(`🎯 Mode: credentials:"omit" + Authorization:Bearer per token`);
log(`🎲 Token pool: Fisher-Yates shuffle (unbiased)`);
log(`🔄 Retry per token: ${RETRY_PER_TOKEN}x (untuk non-quota errors)`);
log(`⏭️ Quota error: cooldown + skip ke token berikutnya`);
log(`📖 Backend returns 4 outputs (Gallery, Info, Status, Base64 JSON)`);
log(`🎨 FFD-XL-3.0 Ready — klik Generate untuk test roulette!`);

if (BACKEND_URL) { 
    checkConnection(); 
} else { 
    log('⚙️ Click ⚙️ to set Backend URL'); 
    openSettings(); 
}

setInterval(updateTokenStatsUI, 5000);
setInterval(checkConnection, 30000);
</script>

</body>
</html>