File size: 51,574 Bytes
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1404692
36dde81
1404692
273ab56
36dde81
273ab56
b2b72aa
36dde81
 
 
 
 
 
 
f697c3b
36dde81
 
ef88983
 
36dde81
 
 
ef88983
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
ef88983
36dde81
 
273ab56
36dde81
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef88983
36dde81
 
 
 
 
 
 
ef88983
36dde81
 
ef88983
36dde81
 
 
 
 
273ab56
36dde81
273ab56
 
36dde81
 
 
 
 
 
 
 
 
 
ef88983
36dde81
 
 
 
 
 
 
ef88983
 
 
 
 
36dde81
 
 
ef88983
36dde81
ef88983
 
 
36dde81
ef88983
 
 
 
36dde81
ef88983
36dde81
 
 
ef88983
36dde81
 
ef88983
36dde81
 
 
 
 
273ab56
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
273ab56
36dde81
 
 
273ab56
36dde81
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f63a8
36dde81
 
 
 
 
 
 
 
07f63a8
36dde81
273ab56
36dde81
 
 
 
 
 
 
273ab56
36dde81
 
 
 
273ab56
36dde81
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
273ab56
dcda553
36dde81
 
dcda553
36dde81
 
 
 
 
 
 
 
dcda553
36dde81
 
 
 
dcda553
36dde81
 
 
 
273ab56
 
36dde81
 
 
273ab56
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
f697c3b
36dde81
 
 
 
 
 
 
 
 
273ab56
1404692
f697c3b
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15fea8d
36dde81
15fea8d
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
15fea8d
36dde81
15fea8d
36dde81
15fea8d
36dde81
 
15fea8d
36dde81
 
 
 
 
 
 
 
 
1404692
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
15fea8d
36dde81
 
 
273ab56
36dde81
 
 
 
 
273ab56
36dde81
 
 
273ab56
36dde81
 
 
 
 
 
 
273ab56
b8fe50f
36dde81
07f63a8
 
36dde81
07f63a8
 
36dde81
 
07f63a8
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f63a8
36dde81
 
 
 
 
 
15fea8d
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcda553
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef88983
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef88983
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273ab56
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15fea8d
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15fea8d
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15fea8d
36dde81
 
 
 
 
 
273ab56
15fea8d
 
 
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
{%- macro rgb(h) -%}{{ h[1:3]|int(base=16) }} {{ h[3:5]|int(base=16) }} {{ h[5:7]|int(base=16) }}{%- endmacro -%}
{%- set path = request.url.path -%}
{%- set nav = [
    ('/', 'Home', 'fa-house', '/'),
    ('/media/manage', 'Media', 'fa-clapperboard', '/media'),
    ('/catalogs', 'Catalogs', 'fa-layer-group', '/catalogs'),
    ('/admin/dashboard', 'Admin', 'fa-chart-simple', '/admin/dashboard'),
    ('/admin/subscriptions', 'Plans', 'fa-credit-card', '/admin/subscriptions'),
    ('/admin/access', 'Tokens', 'fa-key', '/admin/access'),
    ('/admin/requests', 'Requests', 'fa-inbox', '/admin/requests'),
    ('/admin/tools', 'Tools', 'fa-screwdriver-wrench', '/admin/tools'),
    ('/admin/settings', 'Settings', 'fa-gear', '/admin/settings'),
] -%}
{%- set dock_count = 4 -%}
{%- macro is_active(prefix) -%}{{ 'true' if (prefix == '/' and path == '/') or (prefix != '/' and path.startswith(prefix)) else '' }}{%- endmacro -%}
<!DOCTYPE html>
<html lang="en" data-theme="{{ current_theme }}" class="{{ 'dark' if theme.is_dark else 'light' }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>{% block title %}Telegram Stremio{% endblock %}</title>

<link rel="icon" type="image/svg+xml" href="/pwa-icon.svg">
<link rel="apple-touch-icon" href="/pwa-icon.svg">
<link rel="manifest" href="/manifest.webmanifest">
<meta name="theme-color" content="{{ theme.colors.background }}">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="{{ 'black-translucent' if theme.is_dark else 'default' }}">
<meta name="mobile-web-app-capable" content="yes">

<link rel="preconnect" href="https://image.tmdb.org">
<link rel="preconnect" href="https://images.metahub.space">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
<script src="https://cdn.tailwindcss.com/3.4.16"></script>

<style>
{% for key, t in themes.items() %}
:root[data-theme="{{ key }}"] {
    color-scheme: {{ 'dark' if t.is_dark else 'light' }};
    --primary: {{ t.colors.primary }};
    --on-primary: {{ t.colors.on_primary }};
    --bg: {{ t.colors.background }};
    --card: {{ t.colors.card }};
    --card-high: {{ t.colors.card_high }};
    --border: {{ t.colors.border }};
    --text: {{ t.colors.text }};
    --text-sec: {{ t.colors.text_secondary }};
    --danger: {{ t.colors.danger }};
    --ok: {{ t.colors.ok }};
    --warn: {{ '#D9A63A' if key == 'ink' else ('#A15C07' if key == 'paper' else '#D6B370') }};
    --info: {{ '#7FA7D9' if key == 'ink' else ('#2F5F9E' if key == 'paper' else '#8FA9C9') }};
    --primary-rgb: {{ rgb(t.colors.primary) }};
    --on-primary-rgb: {{ rgb(t.colors.on_primary) }};
    --bg-rgb: {{ rgb(t.colors.background) }};
    --card-rgb: {{ rgb(t.colors.card) }};
    --card-high-rgb: {{ rgb(t.colors.card_high) }};
    --border-rgb: {{ rgb(t.colors.border) }};
    --text-rgb: {{ rgb(t.colors.text) }};
    --text-sec-rgb: {{ rgb(t.colors.text_secondary) }};
    --danger-rgb: {{ rgb(t.colors.danger) }};
    --ok-rgb: {{ rgb(t.colors.ok) }};
    --warn-rgb: {{ '217 166 58' if key == 'ink' else ('161 92 7' if key == 'paper' else '214 179 112') }};
    --info-rgb: {{ '127 167 217' if key == 'ink' else ('47 95 158' if key == 'paper' else '143 169 201') }};
    --scrim: {{ 'rgba(0,0,0,.6)' if t.is_dark else 'rgba(29,27,24,.35)' }};
}
{% endfor %}
:root {
    --secondary: var(--primary);
    --accent: var(--primary);
    --surface: color-mix(in srgb, var(--text) 4%, transparent);
    --surface-2: color-mix(in srgb, var(--text) 7%, transparent);
    --surface-3: color-mix(in srgb, var(--text) 11%, transparent);
    --hairline: var(--border);
    --hairline-strong: color-mix(in srgb, var(--text) 18%, var(--border));
    --muted: var(--text-sec);
    --ring: color-mix(in srgb, var(--primary) 28%, transparent);
    --radius: 20px;
    --radius-sm: 14px;
    --ease: cubic-bezier(.2, .8, .2, 1);
    --spring: cubic-bezier(.34, 1.4, .5, 1);
    --rail: 88px;
    --shadow-soft: none;
    --shadow-strong: 0 18px 48px rgba(0,0,0,.28);
}

@view-transition { navigation: auto; }
::view-transition-old(root), ::view-transition-new(root) { animation-duration: .18s; }
#rail { view-transition-name: rail; }
#dock { view-transition-name: dock; }

html { scroll-behavior: auto; -webkit-tap-highlight-color: transparent; text-size-adjust: 100%; }
html, body { max-width: 100%; overflow-x: clip; }
body {
    margin: 0;
    font-family: 'Space Grotesk', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    transition: background-color .25s var(--ease), color .25s var(--ease);
}
img { max-width: 100%; }
code, pre, kbd, .font-mono { font-family: 'JetBrains Mono', ui-monospace, monospace; }
h1, h2, h3, h4 { letter-spacing: -0.015em; }
.tabular { font-variant-numeric: tabular-nums; }
::selection { background: color-mix(in srgb, var(--primary) 32%, transparent); color: var(--text); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; border: 3px solid transparent; background-clip: padding-box; }
*::-webkit-scrollbar-thumb:hover { background: var(--text-sec); background-clip: padding-box; }

input, select, textarea, button { font-family: inherit; }
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]), select, textarea { color: var(--text); }
input::placeholder, textarea::placeholder { color: color-mix(in srgb, var(--text-sec) 80%, transparent); opacity: 1; }
input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; appearance: none; }
select:not([multiple]):not([size]) {
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23888' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E") !important;
    background-repeat: no-repeat !important;
    background-position: right .85rem center !important;
    background-size: 14px !important;
    padding-right: 2.4rem !important;
    cursor: pointer;
}
select option, select optgroup { background-color: var(--card); color: var(--text); }
input[type="checkbox"], input[type="radio"], input[type="range"] { accent-color: var(--primary); }
input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, textarea:-webkit-autofill {
    -webkit-text-fill-color: var(--text);
    -webkit-box-shadow: 0 0 0 1000px var(--card) inset;
    caret-color: var(--text);
    transition: background-color 9999s ease-in-out 0s;
}
button, a, [role="button"] { touch-action: manipulation; }
button:disabled { cursor: not-allowed; opacity: .5; }

.glass-panel, .surface-card, .glass-card, .glass-card-soft {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: none;
}
.glass-nav { background: var(--card); }
.glass-pill { border-radius: 9999px; }

.btn-primary, .btn-ghost, .btn-danger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .5rem;
    border-radius: 9999px;
    font-weight: 500;
    white-space: nowrap;
    border: 1px solid transparent;
    transition: background-color .18s var(--ease), border-color .18s var(--ease), color .18s var(--ease), transform .12s var(--ease), filter .18s var(--ease);
}
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover { filter: brightness(1.07); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--card-high); }
.btn-danger { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 20%, transparent); }
.btn-primary:active, .btn-ghost:active, .btn-danger:active { transform: scale(.97); }

.badge {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    font-size: .68rem;
    font-weight: 500;
    letter-spacing: .06em;
    text-transform: uppercase;
    padding: .2rem .6rem;
    border-radius: 9999px;
    background: color-mix(in srgb, var(--primary) 14%, transparent);
    color: var(--primary);
    white-space: nowrap;
}

.field {
    width: 100%;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color .18s var(--ease), box-shadow .18s var(--ease);
}
.field:hover { border-color: var(--hairline-strong); }
.field:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }

.label-caps {
    font-size: .72rem;
    font-weight: 500;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--text-sec);
}

.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 1rem; margin-bottom: 2rem; }
.page-head > div:first-child { min-width: 0; }
.page-title { font-size: 28px; font-weight: 600; line-height: 1.15; letter-spacing: -.02em; }
.page-sub { margin-top: .3rem; color: var(--text-sec); font-size: .92rem; }
.page-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
@media (min-width: 640px) { .page-title { font-size: 30px; } }
.section-label { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin: 0 .25rem .65rem; font-size: .72rem; font-weight: 500; letter-spacing: .1em; text-transform: uppercase; color: var(--text-sec); }

.seg { display: inline-flex; padding: 4px; gap: 2px; border-radius: 9999px; background: var(--card); border: 1px solid var(--border); }
.seg > a, .seg > button { display: inline-flex; align-items: center; gap: .45rem; padding: .45rem .95rem; border-radius: 9999px; font-size: .88rem; color: var(--text-sec); text-decoration: none; white-space: nowrap; transition: background-color .2s var(--ease), color .2s var(--ease); }
.seg > a:hover, .seg > button:hover { color: var(--text); }
.seg > [aria-selected="true"], .seg > .active { background: var(--card-high); color: var(--primary); }
.chip-toggle { display: inline-flex; align-items: center; gap: .45rem; padding: .5rem .95rem; border-radius: 9999px; font-size: .88rem; color: var(--text-sec); border: 1px solid var(--border); cursor: pointer; user-select: none; transition: all .18s var(--ease); }
.chip-toggle:hover { color: var(--text); }
.chip-toggle:has(input:checked) { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 50%, var(--border)); background: color-mix(in srgb, var(--primary) 10%, transparent); }
.pg-btn { min-width: 2.4rem; height: 2.4rem; padding: 0 .85rem; border-radius: 9999px; font-size: .88rem; color: var(--text); border: 1px solid var(--border); background: transparent; transition: background-color .15s var(--ease); }
.pg-btn:hover { background: var(--card-high); }
.pg-btn[aria-current="page"] { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }

.hidden:not([class*=":flex"]):not([class*=":block"]):not([class*=":grid"]):not([class*=":inline"]):not([class*=":table"]) { display: none !important; }

#nav-progress { position: fixed; top: 0; left: 0; z-index: 10000; height: 3px; width: 0; background: var(--primary); box-shadow: 0 0 8px color-mix(in srgb, var(--primary) 60%, transparent); opacity: 0; pointer-events: none; border-radius: 0 3px 3px 0; }
#nav-progress.run { opacity: 1; animation: nav-progress 8s cubic-bezier(.08,.75,.2,1) forwards; }
@keyframes nav-progress { 0% { width: 0; } 6% { width: 35%; } 30% { width: 65%; } 100% { width: 92%; } }

#cmdk, #keys { padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--card); color: var(--text); box-shadow: var(--shadow-strong); width: min(640px, calc(100vw - 32px)); max-height: min(560px, calc(100dvh - 120px)); margin: 12vh auto auto; overflow: hidden; }
#cmdk[open], #keys[open] { display: flex; flex-direction: column; animation: cmdk-in .18s var(--ease); }
#cmdk::backdrop, #keys::backdrop { background: var(--scrim); }
@keyframes cmdk-in { from { opacity: 0; transform: translateY(-6px) scale(.985); } to { opacity: 1; transform: none; } }
.cmdk-head { display: flex; align-items: center; gap: .75rem; padding: .9rem 1.1rem; border-bottom: 1px solid var(--border); }
.cmdk-head i { color: var(--text-sec); }
.cmdk-head input { flex: 1; min-width: 0; background: none; border: 0; outline: none; font-size: 1rem; color: var(--text); }
.cmdk-list { overflow-y: auto; padding: .4rem; overscroll-behavior: contain; }
.cmdk-group { padding: .65rem .75rem .3rem; font-size: .7rem; letter-spacing: .1em; text-transform: uppercase; color: var(--text-sec); }
.cmdk-item { display: flex; align-items: center; gap: .75rem; width: 100%; padding: .55rem .75rem; border: 0; border-radius: 12px; background: none; color: var(--text); text-align: left; font-size: .92rem; cursor: pointer; }
.cmdk-item > i { width: 1.1rem; text-align: center; color: var(--text-sec); }
.cmdk-item img { width: 28px; height: 42px; border-radius: 5px; object-fit: cover; background: var(--card-high); flex-shrink: 0; }
.cmdk-item .t { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cmdk-item .t small { color: var(--text-sec); margin-left: .4rem; }
.cmdk-item[aria-selected="true"] { background: var(--card-high); }
.cmdk-item[aria-selected="true"] > i { color: var(--primary); }
.cmdk-empty { padding: 1.5rem; text-align: center; color: var(--text-sec); font-size: .9rem; }
.cmdk-foot { display: flex; flex-wrap: wrap; gap: .4rem 1rem; padding: .6rem 1.1rem; border-top: 1px solid var(--border); font-size: .76rem; color: var(--text-sec); }
kbd.k { display: inline-grid; place-items: center; min-width: 1.4rem; height: 1.4rem; padding: 0 .35rem; border-radius: 6px; border: 1px solid var(--border); border-bottom-width: 2px; background: var(--bg); color: var(--text-sec); font: 500 .72rem/1 'Space Grotesk', ui-sans-serif, system-ui, sans-serif; }
.keys-body { overflow-y: auto; padding: .5rem 1.1rem 1.1rem; }
#cmdk:focus-visible, #keys:focus-visible, .keys-body:focus-visible, .cmdk-list:focus-visible { outline: none; }
.keys-body h3 { font-size: .7rem; letter-spacing: .1em; text-transform: uppercase; color: var(--text-sec); margin: .9rem 0 .4rem; font-weight: 500; }
.keys-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .4rem 0; font-size: .9rem; border-bottom: 1px solid var(--border); }
.keys-row:last-child { border-bottom: 0; }
.keys-row span:last-child { display: inline-flex; gap: .25rem; align-items: center; flex-shrink: 0; color: var(--text-sec); font-size: .78rem; }
.rail-search .ico { border: 1px solid var(--border); }
@media (max-width: 767.98px) { #cmdk { margin-top: 10vh; } }

.page-in { animation: page-in .32s var(--ease); }
@keyframes page-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

#rail {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--rail);
    z-index: 40;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .25rem;
    padding: 1rem 0 .9rem;
    background: var(--bg);
    border-right: 1px solid var(--border);
}
.rail-logo { display: grid; place-items: center; width: 44px; height: 44px; margin-bottom: .9rem; border-radius: 14px; background: var(--primary); color: var(--on-primary); transition: transform .2s var(--spring); }
.rail-logo:hover { transform: scale(1.06) rotate(-4deg); }
.rail-nav { display: flex; flex-direction: column; align-items: center; gap: .15rem; overflow-y: auto; scrollbar-width: none; flex: 1; min-height: 0; width: 100%; padding: 0 .5rem; }
.rail-nav::-webkit-scrollbar { display: none; }
.rail-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .3rem;
    width: 100%;
    padding: .35rem 0 .45rem;
    font-size: .72rem;
    color: var(--text-sec);
    text-decoration: none;
    border-radius: 14px;
    transition: color .18s var(--ease);
}
.rail-item .ico { display: grid; place-items: center; width: 56px; height: 32px; border-radius: 9999px; font-size: 1rem; transition: background-color .22s var(--ease), color .18s var(--ease), transform .25s var(--spring); }
.rail-item:hover { color: var(--text); }
.rail-item:hover .ico { background: var(--surface-2); }
.rail-item:active .ico { transform: scale(.9); }
.rail-item[aria-current="page"] { color: var(--text); font-weight: 500; }
.rail-item[aria-current="page"] .ico { background: var(--card-high); color: var(--primary); }

#topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .75rem;
    height: calc(56px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 16px 0;
    background: var(--bg);
    border-bottom: 1px solid transparent;
    transition: border-color .2s var(--ease);
}
#topbar.scrolled { border-color: var(--border); }
.brand { display: flex; align-items: center; gap: .6rem; color: var(--text); text-decoration: none; font-weight: 600; font-size: 1.05rem; letter-spacing: -.01em; min-width: 0; }
.brand-mark { display: grid; place-items: center; flex-shrink: 0; width: 30px; height: 30px; border-radius: 10px; background: var(--primary); color: var(--on-primary); font-size: .7rem; }
.icon-btn { display: grid; place-items: center; width: 40px; height: 40px; flex-shrink: 0; border-radius: 9999px; color: var(--text-sec); background: transparent; border: 0; cursor: pointer; transition: background-color .18s var(--ease), color .18s var(--ease), transform .12s var(--ease); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:active { transform: scale(.92); }

#dock {
    position: fixed;
    left: 50%;
    bottom: calc(12px + env(safe-area-inset-bottom));
    z-index: 40;
    width: min(calc(100% - 32px), 480px);
    transform: translateX(-50%);
    padding: 5px;
    border-radius: 9999px;
    background: var(--card-high);
    border: 1px solid var(--border);
    box-shadow: 0 10px 30px rgba(0,0,0,{{ '.45' if theme.is_dark else '.12' }});
}
.dock-row { position: relative; display: grid; grid-template-columns: repeat({{ dock_count + 1 }}, minmax(0, 1fr)); height: 52px; }
.dock-pill { position: absolute; top: 0; bottom: 0; left: 0; width: calc(100% / {{ dock_count + 1 }}); border-radius: 9999px; background: color-mix(in srgb, var(--primary) 18%, transparent); transition: transform .42s var(--spring); pointer-events: none; }
.dock-item { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-width: 0; color: var(--text-sec); text-decoration: none; font-size: .68rem; letter-spacing: .02em; background: none; border: 0; cursor: pointer; border-radius: 9999px; transition: color .2s var(--ease); }
.dock-item i { font-size: 1.05rem; transition: transform .3s var(--spring); }
.dock-item span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 2px; }
.dock-item[aria-current="page"], .dock-item.on { color: var(--primary); }
.dock-item[aria-current="page"] i { transform: scale(1.08); }
.dock-item:active i { transform: scale(.84); }

.scrim { position: fixed; inset: 0; z-index: 45; background: var(--scrim); opacity: 0; pointer-events: none; transition: opacity .22s var(--ease); }
.scrim.open { opacity: 1; pointer-events: auto; }

#sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 46;
    max-height: 86dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 10px 16px calc(20px + env(safe-area-inset-bottom));
    background: var(--card);
    border-top: 1px solid var(--border);
    border-radius: 24px 24px 0 0;
    transform: translateY(105%);
    visibility: hidden;
    transition: transform .34s var(--ease), visibility 0s linear .34s;
}
#sheet.open { transform: none; visibility: visible; transition: transform .34s var(--ease), visibility 0s; }
.sheet-grip { width: 36px; height: 4px; margin: 0 auto 14px; border-radius: 99px; background: var(--border); }
.sheet-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .5rem; }
.sheet-link { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .45rem; min-height: 76px; padding: .6rem .25rem; border-radius: 16px; border: 1px solid var(--border); background: var(--bg); color: var(--text); text-decoration: none; font-size: .8rem; text-align: center; transition: background-color .18s var(--ease), transform .12s var(--ease); }
.sheet-link i { color: var(--text-sec); font-size: 1.05rem; }
.sheet-link:active { transform: scale(.96); }
.sheet-link[aria-current="page"] { border-color: color-mix(in srgb, var(--primary) 50%, var(--border)); }
.sheet-link[aria-current="page"] i { color: var(--primary); }

#account-pop {
    position: fixed;
    left: calc(var(--rail) + 10px);
    bottom: 14px;
    z-index: 46;
    width: 280px;
    padding: .9rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-strong);
    opacity: 0;
    transform: translateX(-6px) scale(.98);
    transform-origin: bottom left;
    pointer-events: none;
    visibility: hidden;
    transition: opacity .18s var(--ease), transform .22s var(--ease), visibility 0s linear .22s;
}
#account-pop.open { opacity: 1; transform: none; pointer-events: auto; visibility: visible; transition: opacity .18s var(--ease), transform .22s var(--ease), visibility 0s; }

.theme-opts { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .5rem; }
.theme-opt { display: flex; flex-direction: column; align-items: center; gap: .45rem; padding: .6rem .25rem .5rem; border-radius: 14px; border: 1px solid var(--border); background: transparent; color: var(--text-sec); font-size: .78rem; cursor: pointer; transition: border-color .18s var(--ease), color .18s var(--ease), transform .12s var(--ease); }
.theme-opt:hover { color: var(--text); }
.theme-opt:active { transform: scale(.96); }
.theme-opt[aria-pressed="true"] { border-color: var(--primary); color: var(--text); }
.swatch { position: relative; width: 34px; height: 34px; border-radius: 9999px; overflow: hidden; border: 1px solid color-mix(in srgb, var(--text) 16%, transparent); }
.swatch::after { content: ""; position: absolute; right: 6px; bottom: 6px; width: 12px; height: 12px; border-radius: 9999px; background: var(--sw); }

.menu-row { display: flex; align-items: center; gap: .7rem; width: 100%; padding: .65rem .75rem; border-radius: 12px; color: var(--text); text-decoration: none; font-size: .9rem; background: none; border: 0; cursor: pointer; transition: background-color .15s var(--ease); }
.menu-row:hover { background: var(--surface-2); }
.menu-row i { width: 1rem; text-align: center; color: var(--text-sec); }
.menu-row.danger, .menu-row.danger i { color: var(--danger); }

#update-banner { display: flex; align-items: center; gap: .85rem; margin-bottom: 1.5rem; padding: .85rem 1rem; border-radius: var(--radius-sm); border: 1px solid color-mix(in srgb, var(--primary) 40%, var(--border)); background: color-mix(in srgb, var(--primary) 9%, var(--card)); }
#update-banner[hidden] { display: none; }

#toast-container { position: fixed; z-index: 9999; top: 16px; right: 16px; display: flex; flex-direction: column; gap: .5rem; pointer-events: none; }
.toast {
    pointer-events: auto;
    display: flex;
    align-items: flex-start;
    gap: .7rem;
    width: 340px;
    max-width: calc(100vw - 32px);
    padding: .8rem 1rem;
    border-radius: 16px;
    background: var(--card-high);
    border: 1px solid var(--border);
    color: var(--text);
    box-shadow: var(--shadow-strong);
    animation: toast-in .26s var(--ease);
}
.toast > i { margin-top: .2rem; }
.toast.success > i { color: var(--ok); }
.toast.error > i { color: var(--danger); }
.toast.info > i { color: var(--primary); }
.toast-title { font-weight: 600; font-size: .9rem; line-height: 1.25; }
.toast-message { font-size: .84rem; color: var(--text-sec); line-height: 1.4; overflow-wrap: anywhere; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-6px); } }

.shell { min-width: 0; }
@media (min-width: 768px) {
    .shell { padding-left: var(--rail); }
    #topbar, #dock, #sheet { display: none; }
}
@media (max-width: 767.98px) {
    #rail, #account-pop { display: none; }
    #toast-container { top: calc(64px + env(safe-area-inset-top)); left: 16px; right: 16px; align-items: stretch; }
    .toast { width: auto; }
}

@media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
</style>

<script>
(function () {
    var c = function (v) { return 'rgb(var(--' + v + '-rgb) / <alpha-value>)'; };
    var scale = function (v) { var o = {}; [50,100,200,300,400,500,600,700,800,900,950].forEach(function (k) { o[k] = c(v); }); return o; };
    tailwind.config = {
        darkMode: 'class',
        theme: {
            extend: {
                fontFamily: {
                    sans: ['"Space Grotesk"', 'ui-sans-serif', 'system-ui', 'sans-serif'],
                    mono: ['"JetBrains Mono"', 'ui-monospace', 'monospace']
                },
                colors: {
                    primary: c('primary'),
                    secondary: c('primary'),
                    accent: c('primary'),
                    'on-primary': c('on-primary'),
                    background: c('bg'),
                    card: c('card'),
                    'card-high': c('card-high'),
                    border: c('border'),
                    hairline: c('border'),
                    text: c('text'),
                    'text-secondary': c('text-sec'),
                    muted: c('text-sec'),
                    ok: c('ok'),
                    danger: c('danger'),
                    warn: c('warn'),
                    info: c('info'),
                    surface: 'var(--surface)',
                    'surface-2': 'var(--surface-2)',
                    'surface-3': 'var(--surface-3)',
                    'hairline-strong': 'var(--hairline-strong)',
                    red: scale('danger'), rose: scale('danger'),
                    green: scale('ok'), emerald: scale('ok'), lime: scale('ok'), teal: scale('ok'),
                    amber: scale('warn'), yellow: scale('warn'), orange: scale('warn'),
                    blue: scale('info'), sky: scale('info'), cyan: scale('info'), indigo: scale('info'),
                    purple: scale('primary'), violet: scale('primary'), fuchsia: scale('primary'), pink: scale('primary')
                },
                borderRadius: { '3xl': '20px', '4xl': '24px' },
                boxShadow: { soft: 'none', strong: 'var(--shadow-strong)', glow: 'none' }
            }
        }
    };
})();
if (!navigator.clipboard) Object.defineProperty(navigator, 'clipboard', { value: { writeText: function (t) { return new Promise(function (ok, fail) {
    var a = document.createElement('textarea');
    a.value = t; a.style.cssText = 'position:fixed;opacity:0';
    document.body.appendChild(a); a.select();
    var done = document.execCommand('copy'); a.remove();
    done ? ok() : fail(new Error('Copy failed'));
}); } } });
</script>
</head>

<body>
<div id="nav-progress" aria-hidden="true"></div>
{% if current_user %}
<aside id="rail" aria-label="Primary">
    <a href="/" class="rail-logo" aria-label="Home"><i class="fa-solid fa-play text-sm"></i></a>
    <nav class="rail-nav">
        {% for href, label, icon, prefix in nav %}
        <a href="{{ href }}" class="rail-item" {% if is_active(prefix) %}aria-current="page"{% endif %}>
            <span class="ico"><i class="fa-solid {{ icon }}"></i></span>{{ label }}
        </a>
        {% endfor %}
    </nav>
    <button type="button" class="rail-item rail-search" id="cmdk-btn" style="background:none;border:0;cursor:pointer" data-kbd-title="Search">
        <span class="ico"><i class="fa-solid fa-magnifying-glass"></i></span>Search
    </button>
    <button type="button" class="rail-item" id="account-btn" aria-haspopup="true" aria-expanded="false" style="background:none;border:0;cursor:pointer">
        <span class="ico"><i class="fa-solid fa-circle-user"></i></span>Account
    </button>
</aside>

<div id="account-pop" role="menu">
    <div class="flex items-center gap-3 px-1 pb-3 mb-3 border-b border-border">
        <span class="grid place-items-center w-9 h-9 rounded-full bg-card-high text-primary"><i class="fa-solid fa-user text-sm"></i></span>
        <div class="min-w-0">
            <p class="text-sm font-medium truncate">{{ current_user }}</p>
            <p class="text-xs text-ok flex items-center gap-1.5"><span class="w-1.5 h-1.5 rounded-full bg-ok"></span>Signed in</p>
        </div>
    </div>
    <p class="label-caps px-1 mb-2">Theme</p>
    <div class="theme-opts mb-3" data-theme-picker></div>
    <a href="https://donate.weebzonex.workers.dev" target="_blank" rel="noopener" class="menu-row"><i class="fa-solid fa-heart"></i>Support the project</a>
    <a href="/logout" class="menu-row danger"><i class="fa-solid fa-arrow-right-from-bracket"></i>Log out</a>
</div>

<header id="topbar">
    <a href="/" class="brand"><span class="brand-mark"><i class="fa-solid fa-play"></i></span><span class="truncate">TG Stremio</span></a>
    <button type="button" class="icon-btn" id="theme-cycle" aria-label="Change theme"><i class="fa-solid fa-circle-half-stroke"></i></button>
</header>

<nav id="dock" aria-label="Primary">
    <div class="dock-row">
        {%- set ns = namespace(pos=-1, more=false) -%}
        {%- for href, label, icon, prefix in nav[:dock_count] -%}{%- if is_active(prefix) -%}{%- set ns.pos = loop.index0 -%}{%- endif -%}{%- endfor -%}
        {%- set ns.more = false -%}
        {%- for href, label, icon, prefix in nav[dock_count:] -%}{%- if is_active(prefix) -%}{%- set ns.more = true -%}{%- endif -%}{%- endfor -%}
        {%- set more_active = ns.more -%}
        <span class="dock-pill" id="dock-pill" style="transform: translateX({{ (ns.pos if ns.pos >= 0 else dock_count) * 100 }}%);{{ '' if ns.pos >= 0 or more_active else ' opacity: 0;' }}"></span>
        {% for href, label, icon, prefix in nav[:dock_count] %}
        <a href="{{ href }}" class="dock-item" {% if is_active(prefix) %}aria-current="page"{% endif %}><i class="fa-solid {{ icon }}"></i><span>{{ label }}</span></a>
        {% endfor %}
        <button type="button" class="dock-item" id="more-btn" aria-haspopup="dialog" aria-expanded="false"><i class="fa-solid fa-grip"></i><span>More</span></button>
    </div>
</nav>

<div id="sheet" role="dialog" aria-modal="true" aria-label="More">
    <div class="sheet-grip"></div>
    <div class="sheet-grid mb-5">
        {% for href, label, icon, prefix in nav[dock_count:] %}
        <a href="{{ href }}" class="sheet-link" {% if is_active(prefix) %}aria-current="page"{% endif %}><i class="fa-solid {{ icon }}"></i>{{ label }}</a>
        {% endfor %}
        <a href="https://donate.weebzonex.workers.dev" target="_blank" rel="noopener" class="sheet-link"><i class="fa-solid fa-heart"></i>Support</a>
    </div>
    <p class="label-caps mb-2">Theme</p>
    <div class="theme-opts mb-4" data-theme-picker></div>
    <div class="flex items-center justify-between gap-3 pt-3 border-t border-border">
        <div class="min-w-0">
            <p class="text-sm font-medium truncate">{{ current_user }}</p>
            <p class="text-xs text-text-secondary">Signed in</p>
        </div>
        <a href="/logout" class="btn-danger px-4 py-2 text-sm"><i class="fa-solid fa-arrow-right-from-bracket"></i>Log out</a>
    </div>
</div>

<div class="scrim" id="scrim"></div>
{% endif %}

<div class="shell">
    <main class="w-full max-w-6xl mx-auto px-4 sm:px-6 lg:px-10 {{ 'pt-5 md:pt-10 pb-32 md:pb-14' if current_user else 'py-8' }} page-in">
        {% if current_user %}
        <div id="update-banner" hidden>
            <span class="grid place-items-center w-9 h-9 shrink-0 rounded-full bg-primary text-on-primary"><i class="fa-solid fa-arrow-up text-sm"></i></span>
            <div class="min-w-0 text-sm leading-snug">
                <p class="font-medium">Version <span id="update-latest"></span> is available</p>
                <p class="text-text-secondary">You are on <span id="update-current"></span>. Set the upstream repo and branch, then restart to update.</p>
            </div>
        </div>
        {% endif %}
        {% block content %}{% endblock %}
    </main>
</div>

<div id="toast-container" aria-live="polite"></div>

{% if current_user %}
<dialog id="cmdk" aria-label="Command palette">
    <div class="cmdk-head">
        <i class="fa-solid fa-magnifying-glass"></i>
        <input id="cmdk-input" type="text" placeholder="Search pages, titles and actions" autocomplete="off" spellcheck="false" role="combobox" aria-expanded="true" aria-controls="cmdk-list">
        <kbd class="k">Esc</kbd>
    </div>
    <div class="cmdk-list" id="cmdk-list" role="listbox"></div>
    <div class="cmdk-foot"><span><kbd class="k">↑</kbd> <kbd class="k">↓</kbd> move</span><span><kbd class="k">↵</kbd> open</span><span><kbd class="k">?</kbd> all shortcuts</span></div>
</dialog>

<dialog id="keys" aria-label="Keyboard shortcuts">
    <div class="cmdk-head"><i class="fa-solid fa-keyboard"></i><b class="flex-1 font-medium">Keyboard shortcuts</b><kbd class="k">Esc</kbd></div>
    <div class="keys-body" id="keys-body"></div>
</dialog>
{% endif %}

<script>
const THEMES = {{ themes | tojson }};
const THEME_KEYS = Object.keys(THEMES);

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

function showToast(message, type = 'info', title = '', duration = 2600) {
    const container = document.getElementById('toast-container');
    if (!container) return;
    const icon = type === 'success' ? 'fa-circle-check' : type === 'error' ? 'fa-circle-exclamation' : 'fa-circle-info';
    const toast = document.createElement('div');
    toast.className = `toast ${type}`;
    toast.setAttribute('role', type === 'error' ? 'alert' : 'status');
    toast.innerHTML = `<i class="fa-solid ${icon}"></i><div class="min-w-0">${title ? `<div class="toast-title">${escapeHtml(title)}</div>` : ''}<div class="toast-message">${escapeHtml(message)}</div></div>`;
    container.appendChild(toast);
    setTimeout(() => {
        toast.style.animation = 'toast-out .18s ease-in forwards';
        setTimeout(() => toast.remove(), 190);
    }, duration);
}

function applyTheme(key) {
    const t = THEMES[key];
    if (!t) return;
    const root = document.documentElement;
    root.dataset.theme = key;
    root.classList.toggle('dark', t.is_dark);
    root.classList.toggle('light', !t.is_dark);
    document.querySelector('meta[name="theme-color"]')?.setAttribute('content', t.colors.background);
    document.querySelectorAll('.theme-opt').forEach(b => b.setAttribute('aria-pressed', String(b.dataset.key === key)));
    document.cookie = `theme=${key}; path=/; max-age=31536000; samesite=lax`;
}

document.querySelectorAll('[data-theme-picker]').forEach(box => {
    box.innerHTML = THEME_KEYS.map(k => {
        const c = THEMES[k].colors;
        return `<button type="button" class="theme-opt" data-key="${k}" aria-pressed="${k === document.documentElement.dataset.theme}">
            <span class="swatch" style="background:${c.background};--sw:${c.primary}"></span>${escapeHtml(THEMES[k].name)}</button>`;
    }).join('');
    box.addEventListener('click', e => {
        const b = e.target.closest('.theme-opt');
        if (b) applyTheme(b.dataset.key);
    });
});

document.getElementById('theme-cycle')?.addEventListener('click', () => {
    const cur = THEME_KEYS.indexOf(document.documentElement.dataset.theme);
    const next = THEME_KEYS[(cur + 1) % THEME_KEYS.length];
    applyTheme(next);
    showToast(`${THEMES[next].name} theme`, 'info', '', 1400);
});

(function shell() {
    const scrim = document.getElementById('scrim');
    const sheet = document.getElementById('sheet');
    const pop = document.getElementById('account-pop');
    const moreBtn = document.getElementById('more-btn');
    const accBtn = document.getElementById('account-btn');
    if (!scrim) return;

    const setOpen = (el, btn, open) => {
        el.classList.toggle('open', open);
        btn?.setAttribute('aria-expanded', String(open));
        btn?.classList.toggle('on', open);
    };
    const closeAll = () => {
        setOpen(sheet, moreBtn, false);
        setOpen(pop, accBtn, false);
        scrim.classList.remove('open');
    };
    moreBtn?.addEventListener('click', () => {
        const open = !sheet.classList.contains('open');
        closeAll();
        if (open) { setOpen(sheet, moreBtn, true); scrim.classList.add('open'); }
    });
    accBtn?.addEventListener('click', e => {
        e.stopPropagation();
        const open = !pop.classList.contains('open');
        setOpen(pop, accBtn, open);
    });
    document.addEventListener('click', e => {
        if (pop.classList.contains('open') && !pop.contains(e.target)) setOpen(pop, accBtn, false);
    });
    scrim.addEventListener('click', closeAll);
    document.addEventListener('keydown', e => { if (e.key === 'Escape') closeAll(); });

    const items = [...document.querySelectorAll('#dock .dock-item')];
    const pill = document.getElementById('dock-pill');
    items.forEach((a, i) => a.addEventListener('click', e => {
        if (a.id === 'more-btn' || e.metaKey || e.ctrlKey || e.shiftKey) return;
        pill.style.opacity = '1';
        pill.style.transform = `translateX(${i * 100}%)`;
    }));
    const pillStyle = pill.getAttribute('style');
    const navCurrent = [...document.querySelectorAll('[aria-current="page"]')];
    window.addEventListener('pageshow', e => {
        if (!e.persisted) return;
        pill.setAttribute('style', pillStyle);
        document.querySelectorAll('[aria-current="page"]').forEach(x => x.removeAttribute('aria-current'));
        navCurrent.forEach(x => x.setAttribute('aria-current', 'page'));
    });

    const topbar = document.getElementById('topbar');
    let ticking = false;
    window.addEventListener('scroll', () => {
        if (ticking) return;
        ticking = true;
        requestAnimationFrame(() => { topbar.classList.toggle('scrolled', window.scrollY > 4); ticking = false; });
    }, { passive: true });
})();

{% if current_user %}
(function manualSessionReminder() {
    if (window.location.pathname === '/admin/tools') return;
    async function check() {
        try {
            const res = await fetch('/api/admin/tools/manual-session');
            if (!res.ok) return;
            const s = (await res.json()).session;
            if (s && s.tmdb_id !== undefined && s.tmdb_id !== null) {
                const label = s.title ? `"${s.title}"` : 'a title';
                showToast(`Uploads are being added to ${label}. Open Tools to stop it when you're done.`, 'error', 'Upload session active', 5000);
            }
        } catch (e) {}
    }
    check();
    setInterval(check, 60000);
})();

(function versionBanner() {
    const banner = document.getElementById('update-banner');
    async function check() {
        try {
            const res = await fetch('/api/admin/version');
            if (!res.ok) return;
            const d = (await res.json()).data || {};
            if (d.update_available && d.latest) {
                document.getElementById('update-latest').textContent = d.latest;
                document.getElementById('update-current').textContent = d.current || '';
                banner.hidden = false;
            } else banner.hidden = true;
        } catch (e) {}
    }
    check();
    setInterval(check, 3600000);
})();
{% endif %}

(function keyboard() {
    const cmdk = document.getElementById('cmdk');
    if (!cmdk) return;
    const isMac = /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);
    const MOD = isMac ? '⌘' : 'Ctrl';
    const kbd = keys => keys.map(k => `<kbd class="k">${k}</kbd>`).join('');
    const GO = { h: '/', m: '/media/manage', c: '/catalogs', a: '/admin/dashboard', p: '/admin/subscriptions', t: '/admin/access', r: '/admin/requests', o: '/admin/tools', s: '/admin/settings' };
    const PAGES = [...document.querySelectorAll('#rail .rail-nav .rail-item')].map(a => ({
        label: a.textContent.trim(), href: a.getAttribute('href'), icon: a.querySelector('i').className,
        key: Object.keys(GO).find(k => GO[k] === a.getAttribute('href'))
    }));
    const go = href => { document.getElementById('nav-progress')?.classList.add('run'); location.href = href; };

    const input = document.getElementById('cmdk-input');
    const list = document.getElementById('cmdk-list');
    let items = [], sel = 0, media = [], seq = 0, timer, searching = false;

    function actions(q) {
        const m = s => s.toLowerCase().includes(q);
        const out = [];
        const pages = PAGES.filter(p => !q || m(p.label)).map(p => ({ group: 'Go to', label: p.label, icon: p.icon, hint: p.key ? kbd(['G', p.key.toUpperCase()]) : '', run: () => go(p.href) }));
        const themes = THEME_KEYS.filter(k => !q || m(THEMES[k].name) || m('theme')).map(k => ({ group: 'Theme', label: `${THEMES[k].name} theme`, icon: 'fa-solid fa-circle-half-stroke', hint: document.documentElement.dataset.theme === k ? '<span class="text-xs">Current</span>' : '', run: () => applyTheme(k) }));
        const misc = [
            { label: 'Add a title', icon: 'fa-solid fa-plus', run: () => go('/media/manage?add=1') },
            { label: 'Keyboard shortcuts', icon: 'fa-solid fa-keyboard', hint: kbd(['?']), run: () => openKeys() },
            { label: 'Log out', icon: 'fa-solid fa-arrow-right-from-bracket', run: () => go('/logout') },
        ].filter(x => !q || m(x.label)).map(x => ({ group: 'Actions', ...x }));
        const titles = media.map(x => ({ group: 'Titles', label: x.title, sub: [x.release_year, x.media_type === 'tv' ? 'Series' : 'Movie'].filter(Boolean).join(' · '), img: x.poster, run: () => go(`/media/edit?tmdb_id=${encodeURIComponent(x.tmdb_id)}&db_index=${encodeURIComponent(x.db_index)}&media_type=${x.media_type === 'tv' ? 'tv' : 'movie'}`) }));
        return [...titles, ...pages, ...themes, ...misc];
    }

    function render() {
        const q = input.value.trim().toLowerCase();
        items = actions(q);
        sel = Math.min(sel, Math.max(items.length - 1, 0));
        if (searching && q.length > 1) items.unshift({ group: 'Titles', label: 'Searching titles…', icon: 'fa-solid fa-circle-notch fa-spin', run: () => {} });
        if (!items.length) { list.innerHTML = `<div class="cmdk-empty">${q.length > 1 ? 'Nothing matches “' + escapeHtml(input.value) + '”' : 'Type to search'}</div>`; return; }
        let last = '';
        list.innerHTML = items.map((it, i) => {
            const head = it.group !== last ? `<div class="cmdk-group">${it.group}</div>` : '';
            last = it.group;
            const lead = it.img !== undefined ? (it.img ? `<img src="${escapeHtml(it.img)}" alt="" loading="lazy" onerror="this.style.visibility='hidden'">` : '<img alt="">') : `<i class="${it.icon}"></i>`;
            return `${head}<button type="button" class="cmdk-item" role="option" data-i="${i}" aria-selected="${i === sel}">${lead}<span class="t">${escapeHtml(it.label)}${it.sub ? `<small>${escapeHtml(it.sub)}</small>` : ''}</span>${it.hint || ''}</button>`;
        }).join('');
        list.querySelector('[aria-selected="true"]')?.scrollIntoView({ block: 'nearest' });
    }

    function searchMedia() {
        clearTimeout(timer);
        const q = input.value.trim();
        const id = ++seq;
        if (q.length < 2) { media = []; searching = false; render(); return; }
        searching = true;
        timer = setTimeout(async () => {
            try {
                const res = await Promise.all(['movie', 'tv'].map(t => fetch(`/api/media/list?media_type=${t}&page_size=5&search=${encodeURIComponent(q)}`).then(r => r.ok ? r.json() : {})));
                if (id !== seq) return;
                media = [...(res[0].movies || []).map(x => ({ ...x, media_type: 'movie' })), ...(res[1].tv_shows || []).map(x => ({ ...x, media_type: 'tv' }))].slice(0, 8);
            } catch (e) {
                if (id !== seq) return;
                media = [];
            }
            searching = false;
            render();
        }, 180);
    }

    function open() {
        if (cmdk.open) return;
        document.getElementById('keys').close();
        input.value = ''; media = []; sel = 0; render();
        cmdk.showModal();
        input.focus();
    }
    function run(i) { const it = items[i]; if (!it || it.label === 'Searching titles…') return; cmdk.close(); it.run(); }

    input.addEventListener('input', () => { sel = 0; searchMedia(); render(); });
    input.addEventListener('keydown', e => {
        if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
            e.preventDefault();
            sel = (sel + (e.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length;
            render();
        } else if (e.key === 'Enter') { e.preventDefault(); run(sel); }
    });
    list.addEventListener('click', e => { const b = e.target.closest('[data-i]'); if (b) run(+b.dataset.i); });
    list.addEventListener('mousemove', e => {
        const b = e.target.closest('[data-i]');
        if (b && +b.dataset.i !== sel) { list.querySelector('[aria-selected="true"]')?.setAttribute('aria-selected', 'false'); sel = +b.dataset.i; b.setAttribute('aria-selected', 'true'); }
    });
    [cmdk, document.getElementById('keys')].forEach(d => d.addEventListener('click', e => { if (e.target === d) d.close(); }));
    document.getElementById('cmdk-btn')?.addEventListener('click', open);
    const cb = document.getElementById('cmdk-btn');
    if (cb) cb.title = `Search (${MOD}+K)`;

    const pageKeys = [];
    if (typeof window.switchEditTab === 'function') {
        [...document.querySelectorAll('.edit-tab')].forEach((t, i) => pageKeys.push([String(i + 1), `${t.textContent.trim()} tab`, () => switchEditTab(t.dataset.tab)]));
    }

    function openKeys() {
        cmdk.close();
        const row = (label, keys) => `<div class="keys-row"><span>${label}</span><span>${keys}</span></div>`;
        const or = '<span class="mx-1">or</span>';
        document.getElementById('keys-body').innerHTML =
            `<h3>General</h3>` +
            row('Open search and commands', kbd([MOD, 'K'])) +
            row('Search on this page', kbd(['/'])) +
            row('Save', kbd([MOD, 'S'])) +
            row('Next theme', kbd(['Shift', 'T'])) +
            row('Close dialog or menu', kbd(['Esc'])) +
            row('Show shortcuts', kbd(['?'])) +
            `<h3>Go to</h3>` + PAGES.filter(p => p.key).map(p => row(p.label, kbd(['G', p.key.toUpperCase()]))).join('') +
            (pageKeys.length ? `<h3>This page</h3>` + pageKeys.map(([k, l]) => row(l, kbd([k]))).join('') : '');
        document.getElementById('keys').showModal();
    }

    const typing = el => el && (el.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName));
    let gAt = 0;
    document.addEventListener('keydown', e => {
        const mod = isMac ? e.metaKey : e.ctrlKey;
        const k = e.key.toLowerCase();
        if (mod && !e.shiftKey && !e.altKey && k === 'k') { e.preventDefault(); cmdk.open ? cmdk.close() : open(); return; }
        if (mod && !e.shiftKey && !e.altKey && k === 's') {
            const btn = [...document.querySelectorAll('[data-shortcut-save]')].find(b => b.offsetParent !== null);
            if (btn) { e.preventDefault(); btn.click(); }
            return;
        }
        if (e.defaultPrevented || mod || e.altKey || typing(e.target) || document.querySelector('dialog[open], [aria-modal="true"]:not(.hidden):not([hidden]):not(#sheet)')) return;
        if (e.key === '?') { e.preventDefault(); openKeys(); return; }
        if (e.key === '/') {
            const f = [...document.querySelectorAll('[data-shortcut-search], main input[type="search"]')].find(x => x.offsetParent !== null);
            if (f) { e.preventDefault(); f.focus(); f.select?.(); }
            return;
        }
        if (e.key === 'T' && e.shiftKey) { document.getElementById('theme-cycle')?.click(); return; }
        if (k === 'g' && !e.shiftKey) { gAt = Date.now(); return; }
        if (gAt && Date.now() - gAt < 1200 && GO[k]) { gAt = 0; go(GO[k]); return; }
        gAt = 0;
        const pk = pageKeys.find(([key]) => key === e.key);
        if (pk) { e.preventDefault(); pk[2](); }
    });
})();

(function navProgress() {
    const bar = document.getElementById('nav-progress');
    const start = () => { bar.classList.remove('run'); void bar.offsetWidth; bar.classList.add('run'); };
    document.addEventListener('click', e => {
        const a = e.target.closest('a[href]');
        if (!a || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
        if (a.target && a.target !== '_self') return;
        if (a.hasAttribute('download')) return;
        const url = new URL(a.href, location.href);
        if (url.origin !== location.origin || url.pathname.startsWith('/api/')) return;
        if (url.pathname === location.pathname && url.search === location.search && url.hash) return;
        start();
        const item = a.closest('.rail-item, .dock-item, .sheet-link');
        if (item) {
            document.querySelectorAll('.rail-item[aria-current], .dock-item[aria-current], .sheet-link[aria-current]').forEach(x => x.removeAttribute('aria-current'));
            item.setAttribute('aria-current', 'page');
        }
    });
    document.addEventListener('submit', e => { if (!e.defaultPrevented && !e.target.closest('[data-no-progress]')) setTimeout(() => { if (!e.defaultPrevented) start(); }); });
    window.addEventListener('pageshow', () => bar.classList.remove('run'));
})();

if ('serviceWorker' in navigator) {
    window.addEventListener('load', () => navigator.serviceWorker.register('/sw.js').catch(() => {}));
}
</script>
</body>
</html>