File size: 41,229 Bytes
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
 
177882b
d991e85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177882b
d991e85
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
177882b
 
d991e85
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
 
 
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
177882b
 
 
 
 
 
 
d991e85
177882b
 
 
 
 
 
 
 
 
 
d991e85
 
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d991e85
 
 
177882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Aurora Dub — Premium AI Video Dubbing</title>
<style>
/* ============================================================
   Design tokens
   ============================================================ */
:root{
  --sky:#00a2ff;
  --sky-soft:#7cd1ff;
  --sky-deep:#0077cc;
  --ink:#0b1524;
  --ink-soft:#5a6b82;
  --line:#e7eef7;
  --bg:#ffffff;
  --panel:#ffffff;
  --panel-glass:rgba(255,255,255,0.62);
  --shadow:0 18px 50px -20px rgba(0,110,190,0.28);
  --shadow-sm:0 8px 24px -12px rgba(11,21,36,0.18);
  --radius:20px;
  --radius-sm:12px;
  --term-bg:#0a1220;
  --term-line:#12203a;
  --ok:#2fd07a;
  --warn:#ffcf5c;
  --err:#ff6b6b;
}

/* Rotatable angle for the RGB border */
@property --rgb-angle{
  syntax:'<angle>';
  initial-value:0deg;
  inherits:false;
}

*{box-sizing:border-box;}
html,body{margin:0;padding:0;}
body{
  font-family:'Inter',-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;
  color:var(--ink);
  background:
    radial-gradient(1100px 620px at 12% -8%, rgba(124,209,255,0.20), transparent 60%),
    radial-gradient(1000px 620px at 108% 4%, rgba(0,162,255,0.14), transparent 58%),
    #ffffff;
  min-height:100vh;
  -webkit-font-smoothing:antialiased;
  line-height:1.5;
}

/* ============================================================
   Layout
   ============================================================ */
.shell{max-width:1180px;margin:0 auto;padding:34px 26px 70px;}

.topbar{
  display:flex;align-items:center;justify-content:space-between;
  gap:18px;margin-bottom:34px;
}
.brand{display:flex;align-items:center;gap:14px;}
.brand-mark{
  width:46px;height:46px;border-radius:14px;
  display:grid;place-items:center;
  background:linear-gradient(135deg,var(--sky),var(--sky-soft));
  box-shadow:0 12px 26px -10px rgba(0,162,255,0.65);
  color:#fff;
}
.brand-name{font-size:20px;font-weight:800;letter-spacing:-0.02em;}
.brand-sub{font-size:12.5px;color:var(--ink-soft);font-weight:500;margin-top:-2px;}

.status-pill{
  display:inline-flex;align-items:center;gap:9px;
  padding:9px 15px;border-radius:999px;
  background:var(--panel-glass);
  backdrop-filter:blur(14px);-webkit-backdrop-filter:blur(14px);
  border:1px solid var(--line);
  font-size:13px;font-weight:600;color:var(--ink-soft);
  box-shadow:var(--shadow-sm);
}
.status-dot{width:9px;height:9px;border-radius:50%;background:var(--sky);box-shadow:0 0 0 4px rgba(0,162,255,0.16);}

.hero{margin-bottom:30px;}
.hero h1{
  font-size:clamp(28px,4vw,44px);line-height:1.08;
  letter-spacing:-0.03em;font-weight:850;margin:0 0 10px;
}
.hero h1 .grad{
  background:linear-gradient(120deg,var(--sky-deep),var(--sky) 45%,var(--sky-soft));
  -webkit-background-clip:text;background-clip:text;color:transparent;
}
.hero p{max-width:640px;color:var(--ink-soft);font-size:15.5px;margin:0;}

/* ============================================================
   Cards / grid
   ============================================================ */
.grid{
  display:grid;
  grid-template-columns:1.05fr 0.95fr;
  gap:24px;align-items:start;
}
@media (max-width:940px){.grid{grid-template-columns:1fr;}}

.card{
  background:var(--panel);
  border:1px solid var(--line);
  border-radius:var(--radius);
  box-shadow:var(--shadow);
  padding:24px;
}
.card.glass{
  background:var(--panel-glass);
  backdrop-filter:blur(16px);-webkit-backdrop-filter:blur(16px);
}
.card-title{display:flex;align-items:center;gap:10px;font-weight:750;font-size:15px;margin-bottom:18px;}
.card-title svg{color:var(--sky);}

/* ============================================================
   RGB Glow Dropbox
   ============================================================ */
.drop-wrap{position:relative;border-radius:22px;padding:3px;isolation:isolate;}
.drop-wrap::before{
  content:"";position:absolute;inset:0;border-radius:22px;padding:3px;
  background:conic-gradient(from var(--rgb-angle),
     #ff3d81,#ff9d3d,#ffe14d,#4dff8f,#00a2ff,#7c6bff,#ff3d81);
  -webkit-mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);
  -webkit-mask-composite:xor;mask-composite:exclude;
  animation:spin 6s linear infinite;
  opacity:0.9;transition:opacity .3s ease;
  z-index:-1;
}
@keyframes spin{to{--rgb-angle:360deg;}}

.dropbox{
  position:relative;border-radius:19px;
  background:#ffffff;
  padding:40px 26px;text-align:center;cursor:pointer;
  transition:background .25s ease,box-shadow .25s ease,transform .12s ease;
  display:flex;flex-direction:column;align-items:center;gap:12px;
}
.dropbox:hover{transform:translateY(-1px);}
.drop-icon{
  width:62px;height:62px;border-radius:50%;
  display:grid;place-items:center;color:var(--sky);
  background:radial-gradient(circle at 50% 40%,rgba(0,162,255,0.14),rgba(124,209,255,0.06));
  transition:transform .25s ease,color .25s ease;
}
.dropbox h3{margin:2px 0 0;font-size:16.5px;font-weight:750;}
.dropbox p{margin:0;color:var(--ink-soft);font-size:13.5px;}
.dropbox .hint{font-size:12px;color:#98a7bd;}

/* Deep sky-blue solid glow while dragging */
.drop-wrap.dragging::before{opacity:0;}
.drop-wrap.dragging .dropbox{
  background:linear-gradient(180deg,#eaf7ff,#dcf0ff);
  box-shadow:0 0 0 3px var(--sky),0 0 46px -4px rgba(0,162,255,0.85),inset 0 0 40px rgba(0,162,255,0.20);
}
.drop-wrap.dragging .drop-icon{transform:scale(1.08);color:var(--sky-deep);}
.drop-wrap.has-file::before{
  background:conic-gradient(from var(--rgb-angle),var(--sky),var(--sky-soft),var(--sky-deep),var(--sky));
}

.file-chip{
  margin-top:16px;display:none;align-items:center;gap:12px;
  padding:12px 14px;border-radius:var(--radius-sm);
  background:#f4fbff;border:1px solid #d5edff;
  text-align:left;
}
.file-chip.show{display:flex;}
.file-chip .fmeta{min-width:0;flex:1;}
.file-chip .fname{font-weight:650;font-size:14px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
.file-chip .fsize{font-size:12px;color:var(--ink-soft);}
.file-chip .fico{color:var(--sky);flex-shrink:0;}
.file-chip .fclear{margin-left:auto;background:none;border:none;cursor:pointer;color:var(--ink-soft);padding:6px;border-radius:8px;display:grid;place-items:center;}
.file-chip .fclear:hover{background:#e3f3ff;color:var(--err);}

/* ============================================================
   Form controls
   ============================================================ */
.field{margin-bottom:16px;}
.field label{display:block;font-size:12.5px;font-weight:650;color:var(--ink-soft);margin-bottom:7px;letter-spacing:.01em;}
.control{
  position:relative;
}
.control select,.control input[type="text"]{
  width:100%;appearance:none;-webkit-appearance:none;
  padding:12px 40px 12px 14px;border-radius:var(--radius-sm);
  border:1px solid var(--line);background:#fbfdff;
  font-size:14.5px;font-family:inherit;color:var(--ink);font-weight:550;
  transition:border-color .2s ease,box-shadow .2s ease,background .2s ease;
}
.control select:focus,.control input[type="text"]:focus{
  outline:none;border-color:var(--sky);background:#fff;
  box-shadow:0 0 0 4px rgba(0,162,255,0.14);
}
.control .chev{position:absolute;right:13px;top:50%;transform:translateY(-50%);color:var(--ink-soft);pointer-events:none;}

.row2{display:grid;grid-template-columns:1fr 1fr;gap:14px;}
@media (max-width:480px){.row2{grid-template-columns:1fr;}}

/* Source language badge */
.src-badge{
  display:inline-flex;align-items:center;gap:9px;
  padding:8px 13px;border-radius:999px;font-size:12.5px;font-weight:650;
  background:#f1f8ff;border:1px solid #d8ecff;color:var(--sky-deep);
}
.src-badge svg{flex-shrink:0;}
.src-badge.detecting{color:var(--ink-soft);background:#f6f8fb;border-color:var(--line);}
.src-badge .spin-i{animation:rot 1.1s linear infinite;transform-origin:center;}
@keyframes rot{to{transform:rotate(360deg);}}
.src-badge.locked{color:#0a8a4a;background:#eefcf3;border-color:#c7f0d7;}

/* Primary button */
.btn{
  display:inline-flex;align-items:center;justify-content:center;gap:10px;
  width:100%;padding:15px 20px;border:none;border-radius:14px;cursor:pointer;
  font-size:15px;font-weight:750;font-family:inherit;color:#fff;
  background:linear-gradient(120deg,var(--sky-deep),var(--sky) 55%,var(--sky-soft));
  box-shadow:0 16px 34px -14px rgba(0,120,204,0.8);
  transition:transform .12s ease,box-shadow .2s ease,filter .2s ease,opacity .2s ease;
}
.btn:hover:not(:disabled){transform:translateY(-2px);filter:brightness(1.03);}
.btn:active:not(:disabled){transform:translateY(0);}
.btn:disabled{opacity:.55;cursor:not-allowed;box-shadow:none;}
.btn.ghost{
  background:#fff;color:var(--sky-deep);border:1px solid #cfe8ff;box-shadow:none;
}
.btn.ghost:hover:not(:disabled){background:#f2f9ff;transform:none;}

.btn-spin{display:none;}
.btn.loading .btn-spin{display:inline-grid;}
.btn.loading .btn-label,.btn.loading .btn-ico{display:none;}
.btn-spin svg{animation:rot .9s linear infinite;}

/* ============================================================
   Dashboard: progress + console
   ============================================================ */
.dash{display:grid;grid-template-columns:1fr;gap:20px;}

.progress-head{display:flex;align-items:center;justify-content:space-between;margin-bottom:12px;}
.progress-stage{font-size:13px;font-weight:650;color:var(--ink-soft);display:flex;align-items:center;gap:9px;min-width:0;}
.progress-stage .st-txt{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
.progress-pct{font-size:22px;font-weight:850;letter-spacing:-.02em;color:var(--sky-deep);font-variant-numeric:tabular-nums;}

.progress-track{
  height:14px;border-radius:999px;background:#eaf1f9;overflow:hidden;position:relative;
  border:1px solid #e0ebf6;
}
.progress-fill{
  height:100%;width:0%;border-radius:999px;
  background:linear-gradient(90deg,var(--sky-deep),var(--sky) 55%,var(--sky-soft));
  transition:width .55s cubic-bezier(.22,.61,.36,1);
  position:relative;overflow:hidden;
}
.progress-fill::after{
  content:"";position:absolute;inset:0;
  background:linear-gradient(90deg,transparent,rgba(255,255,255,0.55),transparent);
  transform:translateX(-100%);
}
.progress-fill.active::after{animation:sheen 1.6s ease-in-out infinite;}
@keyframes sheen{to{transform:translateX(100%);}}
.progress-fill.paused{background:linear-gradient(90deg,#7c6bff,var(--sky));}

.progress-marks{display:flex;justify-content:space-between;margin-top:8px;font-size:10.5px;color:#9fb0c4;font-weight:600;}

/* Console */
.console{
  margin-top:6px;background:var(--term-bg);border-radius:14px;overflow:hidden;
  border:1px solid #0e1a30;box-shadow:inset 0 2px 20px rgba(0,0,0,0.4);
}
.console-bar{
  display:flex;align-items:center;gap:8px;padding:11px 14px;
  background:#0d1728;border-bottom:1px solid var(--term-line);
}
.dotset{display:flex;gap:6px;}
.dotset i{width:11px;height:11px;border-radius:50%;display:block;}
.dotset i:nth-child(1){background:#ff5f57;}
.dotset i:nth-child(2){background:#febc2e;}
.dotset i:nth-child(3){background:#28c840;}
.console-title{color:#7d93b3;font-size:12px;font-weight:600;margin-left:6px;font-family:'SFMono-Regular',ui-monospace,Menlo,Consolas,monospace;}
.console-live{margin-left:auto;display:flex;align-items:center;gap:6px;font-size:11px;color:#4f6party;}
.console-live span{color:#5f7590;font-size:11px;}
.live-dot{width:7px;height:7px;border-radius:50%;background:#3a4a63;}
.live-dot.on{background:var(--ok);box-shadow:0 0 0 3px rgba(47,208,122,0.2);animation:pulse 1.4s ease-in-out infinite;}
@keyframes pulse{50%{opacity:.4;}}

.console-body{
  height:270px;overflow-y:auto;padding:14px 16px;
  font-family:'SFMono-Regular',ui-monospace,Menlo,Consolas,monospace;
  font-size:12.5px;line-height:1.65;color:#cdd9ea;
}
.console-body::-webkit-scrollbar{width:9px;}
.console-body::-webkit-scrollbar-thumb{background:#22314c;border-radius:9px;}
.log{display:flex;gap:10px;padding:1px 0;white-space:pre-wrap;word-break:break-word;}
.log .ts{color:#546a8a;flex-shrink:0;}
.log .msg{flex:1;}
.log.l-info .msg{color:#cdd9ea;}
.log.l-ok .msg{color:var(--ok);}
.log.l-warn .msg{color:var(--warn);}
.log.l-err .msg{color:var(--err);}
.log.l-sys .msg{color:var(--sky-soft);}
.console-empty{color:#3f5474;font-style:italic;}

/* ============================================================
   Result panel
   ============================================================ */
.result{display:none;margin-top:20px;}
.result.show{display:block;animation:fadeUp .5s ease;}
@keyframes fadeUp{from{opacity:0;transform:translateY(10px);}to{opacity:1;transform:translateY(0);}}
.result video{width:100%;border-radius:14px;background:#000;display:block;box-shadow:var(--shadow-sm);}
.result-meta{display:flex;flex-wrap:wrap;gap:10px;margin:14px 0;}
.meta-tag{
  display:inline-flex;align-items:center;gap:7px;font-size:12.5px;font-weight:650;
  padding:7px 12px;border-radius:999px;background:#f2f9ff;border:1px solid #d8ecff;color:var(--sky-deep);
}
.meta-tag svg{color:var(--sky);}

.footer{margin-top:44px;text-align:center;color:#9fb0c4;font-size:12.5px;}
.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0;}
</style>
</head>
<body>
<div class="shell">

  <!-- ===================== TOP BAR ===================== -->
  <header class="topbar">
    <div class="brand">
      <div class="brand-mark" aria-hidden="true">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <path d="M12 3v18"/><path d="M6 8v8"/><path d="M18 8v8"/><path d="M3 11v2"/><path d="M21 11v2"/>
        </svg>
      </div>
      <div>
        <div class="brand-name">Aurora Dub</div>
        <div class="brand-sub">Neural Video Localization Studio</div>
      </div>
    </div>
    <div class="status-pill" id="serverPill" title="Backend connection status">
      <span class="status-dot" id="serverDot"></span>
      <span id="serverStatusText">Checking backend…</span>
    </div>
  </header>

  <!-- ===================== HERO ===================== -->
  <section class="hero">
    <h1>Dub any video into <span class="grad">any language</span>, flawlessly.</h1>
    <p>Pick a video from your gallery — upload starts instantly. Our pipeline transcribes, translates, and re-voices it in one Gemini request, then matches the audio to your video's exact length with real-time progress streaming.</p>
  </section>

  <!-- ===================== MAIN GRID ===================== -->
  <div class="grid">

    <!-- LEFT: Upload + Setup -->
    <section class="card">
      <div class="card-title">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/>
        </svg>
        Source Media
      </div>

      <!-- RGB Glow Dropbox -->
      <div class="drop-wrap" id="dropWrap">
        <div class="dropbox" id="dropbox" role="button" tabindex="0" aria-label="Upload a video file">
          <div class="drop-icon" aria-hidden="true">
            <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round">
              <rect x="2" y="4" width="20" height="16" rx="3"/><path d="m10 9 5 3-5 3z"/>
            </svg>
          </div>
          <h3>Drag &amp; drop your video</h3>
          <p>or click to browse your files</p>
          <span class="hint">MP4 · MOV · MKV · WEBM — up to your server limit</span>
        </div>
      </div>
      <input type="file" id="fileInput" accept="video/*" class="sr-only" />

      <!-- Selected file chip -->
      <div class="file-chip" id="fileChip">
        <span class="fico" aria-hidden="true">
          <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
            <path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z"/><polyline points="14 2 14 8 20 8"/><path d="m10 12 4 2-4 2z"/>
          </svg>
        </span>
        <div class="fmeta">
          <div class="fname" id="fileName">—</div>
          <div class="fsize" id="fileSize"></div>
        </div>
        <button class="fclear" id="fileClear" title="Remove file" aria-label="Remove file">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
        </button>
      </div>

      <hr style="border:none;border-top:1px solid var(--line);margin:22px 0;">

      <div class="card-title">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>
        </svg>
        Dubbing Setup
      </div>

      <div class="row2">
        <div class="field">
          <label for="targetLang">Target Language</label>
          <div class="control">
            <select id="targetLang">
              <option value="English">English</option>
              <option value="Spanish">Spanish</option>
              <option value="French">French</option>
              <option value="German">German</option>
              <option value="Italian">Italian</option>
              <option value="Portuguese">Portuguese</option>
              <option value="Hindi">Hindi</option>
              <option value="Bengali">Bengali</option>
              <option value="Arabic">Arabic</option>
              <option value="Japanese">Japanese</option>
              <option value="Korean">Korean</option>
              <option value="Chinese (Mandarin)">Chinese (Mandarin)</option>
              <option value="Russian">Russian</option>
              <option value="Turkish">Turkish</option>
              <option value="Dutch">Dutch</option>
              <option value="Indonesian">Indonesian</option>
              <option value="Vietnamese">Vietnamese</option>
              <option value="Thai">Thai</option>
              <option value="Polish">Polish</option>
              <option value="Ukrainian">Ukrainian</option>
            </select>
            <span class="chev" aria-hidden="true"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg></span>
          </div>
        </div>

        <div class="field">
          <label for="voiceSelect">Voice Selection</label>
          <div class="control">
            <select id="voiceSelect"></select>
            <span class="chev" aria-hidden="true"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg></span>
          </div>
        </div>
      </div>

      <div class="field">
        <label>Detected Source Language</label>
        <span class="src-badge detecting" id="srcBadge">
          <svg class="spin-i" id="srcSpin" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round">
            <path d="M21 12a9 9 0 1 1-6.219-8.56"/>
          </svg>
          <span id="srcText">Auto-Detecting…</span>
        </span>
      </div>

      <button class="btn" id="generateBtn" disabled>
        <span class="btn-ico" aria-hidden="true">
          <svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="5 3 19 12 5 21 5 3"/></svg>
        </span>
        <span class="btn-label">Generate Dub</span>
        <span class="btn-spin" aria-hidden="true"><svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg></span>
      </button>
    </section>

    <!-- RIGHT: Dashboard -->
    <section class="card glass">
      <div class="card-title">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <path d="M3 3v18h18"/><path d="m19 9-5 5-4-4-3 3"/>
        </svg>
        Live Pipeline Dashboard
      </div>

      <div class="dash">
        <!-- Progress -->
        <div>
          <div class="progress-head">
            <div class="progress-stage">
              <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
              <span class="st-txt" id="stageText">Idle — awaiting media</span>
            </div>
            <div class="progress-pct" id="pctText">0%</div>
          </div>
          <div class="progress-track">
            <div class="progress-fill" id="progressFill"></div>
          </div>
          <div class="progress-marks">
            <span>Upload</span><span>Transcribe</span><span>Voice</span><span>Mux</span><span>Done</span>
          </div>
        </div>

        <!-- Console -->
        <div class="console">
          <div class="console-bar">
            <div class="dotset" aria-hidden="true"><i></i><i></i><i></i></div>
            <div class="console-title">pipeline://stream — SSE</div>
            <div class="console-live">
              <span class="live-dot" id="liveDot"></span>
              <span id="liveText">offline</span>
            </div>
          </div>
          <div class="console-body" id="console">
            <div class="console-empty">// Log stream is idle. Start a dub to see live server events.</div>
          </div>
        </div>
      </div>

      <!-- Result -->
      <div class="result" id="resultPanel">
        <hr style="border:none;border-top:1px solid var(--line);margin:20px 0;">
        <div class="card-title" style="margin-bottom:14px;">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg>
          Dubbed Result
        </div>
        <video id="resultVideo" controls playsinline></video>
        <div class="result-meta" id="resultMeta"></div>
        <a id="downloadBtn" class="btn" download>
          <span class="btn-ico" aria-hidden="true"><svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg></span>
          <span>Download Dubbed Video</span>
        </a>
      </div>
    </section>
  </div>

  <div class="footer">
    Aurora Dub · Neural dubbing pipeline · Streaming over Server-Sent Events
  </div>
</div>

<script>
"use strict";

/* ============================================================
   Config
   ============================================================ */
const API_BASE = "https://video-dubbing-api-85ur.onrender.com";

/* All 30 official Gemini native-TTS prebuilt voices, with Google's own
   one-word style tag for each (ai.google.dev/gemini-api/docs/speech-generation). */
const VOICES = [
  { name: "Zephyr",        style: "Bright"        },
  { name: "Puck",          style: "Upbeat"        },
  { name: "Charon",        style: "Informative"   },
  { name: "Kore",          style: "Firm"          },
  { name: "Fenrir",        style: "Excitable"     },
  { name: "Leda",          style: "Youthful"      },
  { name: "Orus",          style: "Firm"          },
  { name: "Aoede",         style: "Breezy"        },
  { name: "Callirrhoe",    style: "Easy-going"    },
  { name: "Autonoe",       style: "Bright"        },
  { name: "Enceladus",     style: "Breathy"       },
  { name: "Iapetus",       style: "Clear"         },
  { name: "Umbriel",       style: "Easy-going"    },
  { name: "Algieba",       style: "Smooth"        },
  { name: "Despina",       style: "Smooth"        },
  { name: "Erinome",       style: "Clear"         },
  { name: "Algenib",       style: "Gravelly"      },
  { name: "Rasalgethi",    style: "Informative"   },
  { name: "Laomedeia",     style: "Upbeat"        },
  { name: "Achernar",      style: "Soft"          },
  { name: "Alnilam",       style: "Firm"          },
  { name: "Schedar",       style: "Even"          },
  { name: "Gacrux",        style: "Mature"        },
  { name: "Pulcherrima",   style: "Forward"       },
  { name: "Achird",        style: "Friendly"      },
  { name: "Zubenelgenubi", style: "Casual"        },
  { name: "Vindemiatrix",  style: "Gentle"        },
  { name: "Sadachbia",     style: "Lively"        },
  { name: "Sadaltager",    style: "Knowledgeable" },
  { name: "Sulafat",       style: "Warm"          }
];
const DEFAULT_VOICE = "Kore";

/* ============================================================
   Element refs
   ============================================================ */
const $ = (id) => document.getElementById(id);

const dropWrap = $("dropWrap");
const dropbox = $("dropbox");
const fileInput = $("fileInput");
const fileChip = $("fileChip");
const fileNameEl = $("fileName");
const fileSizeEl = $("fileSize");
const fileClear = $("fileClear");

const targetLang = $("targetLang");
const voiceSelect = $("voiceSelect");
const generateBtn = $("generateBtn");

const srcBadge = $("srcBadge");
const srcText = $("srcText");
const srcSpin = $("srcSpin");

const stageText = $("stageText");
const pctText = $("pctText");
const progressFill = $("progressFill");
const consoleEl = $("console");
const liveDot = $("liveDot");
const liveText = $("liveText");

const resultPanel = $("resultPanel");
const resultVideo = $("resultVideo");
const resultMeta = $("resultMeta");
const downloadBtn = $("downloadBtn");

const serverDot = $("serverDot");
const serverStatusText = $("serverStatusText");

/* ============================================================
   App state
   ============================================================ */
const state = {
  file: null,
  running: false,
  sessionId: null,
  abort: null,
  targetLanguage: "English",
  resultObjectUrl: null,
  consoleCleared: false
};

/* ============================================================
   Utilities
   ============================================================ */
function fmtBytes(bytes){
  if(!bytes && bytes !== 0) return "";
  const u = ["B","KB","MB","GB"]; let i = 0; let n = bytes;
  while(n >= 1024 && i < u.length-1){ n/=1024; i++; }
  return n.toFixed(n < 10 && i>0 ? 1 : 0) + " " + u[i];
}
function nowStamp(){
  const d = new Date();
  const p = (x,l=2)=>String(x).padStart(l,"0");
  return `${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}.${p(d.getMilliseconds(),3)}`;
}
function esc(s){
  return String(s).replace(/[&<>"']/g,(c)=>({ "&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;" }[c]));
}

/* Classify a raw log line into a severity for coloring. */
function logClass(msg){
  const m = msg.toUpperCase();
  if(m.includes("[ERROR]") || m.includes("FAIL") ) return "l-err";
  if(m.includes("[WARN]") || m.includes("WARNING")) return "l-warn";
  if(m.includes("[SUCCESS]") || m.includes("COMPLETE")) return "l-ok";
  if(m.includes("[SYS]") || m.includes("//")) return "l-sys";
  return "l-info";
}

function pushLog(msg, forceClass){
  if(!state.consoleCleared){
    consoleEl.innerHTML = "";
    state.consoleCleared = true;
  }
  const row = document.createElement("div");
  row.className = "log " + (forceClass || logClass(msg));
  row.innerHTML = `<span class="ts">${nowStamp()}</span><span class="msg">${esc(msg)}</span>`;
  consoleEl.appendChild(row);
  consoleEl.scrollTop = consoleEl.scrollHeight;
}

function resetConsole(){
  consoleEl.innerHTML = '<div class="console-empty">// Log stream is idle. Start a dub to see live server events.</div>';
  state.consoleCleared = false;
}

function setLive(on){
  liveDot.classList.toggle("on", !!on);
  liveText.textContent = on ? "streaming" : "offline";
}

function setProgress(percent, message){
  const p = Math.max(0, Math.min(100, Math.round(percent)));
  progressFill.style.width = p + "%";
  pctText.textContent = p + "%";
  if(message) stageText.textContent = message;
}

function setStage(txt){ stageText.textContent = txt; }

/* ============================================================
   Source language badge
   ============================================================ */
function lockSourceLanguage(lang){
  if(!lang || lang === "Unknown") return;
  srcBadge.classList.remove("detecting");
  srcBadge.classList.add("locked");
  srcSpin.style.display = "none";
  srcText.textContent = lang;
}
function resetSourceLanguage(){
  srcBadge.classList.remove("locked");
  srcBadge.classList.add("detecting");
  srcSpin.style.display = "";
  srcText.textContent = "Auto-Detecting…";
}

/* ============================================================
   Voice selectors
   ============================================================ */
function buildMainVoiceSelect(){
  voiceSelect.innerHTML = "";
  VOICES.forEach(v=>{
    const o = document.createElement("option");
    o.value = v.name;
    o.textContent = `${v.name} · ${v.style}`;
    voiceSelect.appendChild(o);
  });
  voiceSelect.value = DEFAULT_VOICE;
}

/* ============================================================
   File handling
   ============================================================ */
function acceptFile(file){
  if(!file) return;
  if(!file.type.startsWith("video/") && !/\.(mp4|mov|mkv|webm|avi|m4v)$/i.test(file.name)){
    pushLog("[WARN] Selected file does not look like a video. Proceeding anyway.", "l-warn");
  }
  state.file = file;
  fileNameEl.textContent = file.name;
  fileSizeEl.textContent = fmtBytes(file.size);
  fileChip.classList.add("show");
  dropWrap.classList.add("has-file");
  refreshGenerateState();
  // Upload starts the instant a video is picked (from the gallery, a file
  // browser, or drag-and-drop) — no extra tap needed.
  if(!state.running) startDub();
}
function clearFile(){
  state.file = null;
  fileInput.value = "";
  fileChip.classList.remove("show");
  dropWrap.classList.remove("has-file");
  refreshGenerateState();
}
function refreshGenerateState(){
  generateBtn.disabled = !state.file || state.running;
}

dropbox.addEventListener("click", ()=> fileInput.click());
dropbox.addEventListener("keydown", (e)=>{ if(e.key==="Enter"||e.key===" "){ e.preventDefault(); fileInput.click(); }});
fileInput.addEventListener("change", ()=> acceptFile(fileInput.files[0]));
fileClear.addEventListener("click", clearFile);

["dragenter","dragover"].forEach(ev=>{
  dropWrap.addEventListener(ev,(e)=>{ e.preventDefault(); e.stopPropagation(); dropWrap.classList.add("dragging"); });
});
["dragleave","dragend","drop"].forEach(ev=>{
  dropWrap.addEventListener(ev,(e)=>{ e.preventDefault(); e.stopPropagation(); if(ev!=="drop") dropWrap.classList.remove("dragging"); });
});
dropWrap.addEventListener("drop",(e)=>{
  dropWrap.classList.remove("dragging");
  const f = e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files[0];
  if(f) acceptFile(f);
});
/* Prevent the browser from opening the file when dropped outside the box */
window.addEventListener("dragover",(e)=> e.preventDefault());
window.addEventListener("drop",(e)=> e.preventDefault());

/* ============================================================
   SSE-over-fetch reader
   Parses text/event-stream framing from a POST response body.
   ============================================================ */
async function readSSE(response, onEvent){
  if(!response.ok){
    let detail = "";
    try{ const j = await response.json(); detail = j.detail || JSON.stringify(j); }
    catch(_){ try{ detail = await response.text(); }catch(__){} }
    throw new Error(`HTTP ${response.status}${detail ? " — " + detail : ""}`);
  }
  if(!response.body) throw new Error("Streaming not supported by this browser.");

  const reader = response.body.getReader();
  const decoder = new TextDecoder("utf-8");
  let buffer = "";

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

    // Split complete events on blank line (handle \n\n and \r\n\r\n)
    let sep;
    while((sep = buffer.search(/\r?\n\r?\n/)) !== -1){
      const rawEvent = buffer.slice(0, sep);
      buffer = buffer.slice(sep + (buffer[sep] === "\r" ? 4 : 2));
      const parsed = parseEventBlock(rawEvent);
      if(parsed) onEvent(parsed.event, parsed.data);
    }
  }
  // Flush any trailing event without a terminating blank line
  const tail = buffer.trim();
  if(tail){
    const parsed = parseEventBlock(tail);
    if(parsed) onEvent(parsed.event, parsed.data);
  }
}

function parseEventBlock(block){
  const lines = block.split(/\r?\n/);
  let event = "message";
  const dataLines = [];
  for(const line of lines){
    if(!line || line.startsWith(":")) continue;      // comment / keep-alive
    if(line.startsWith("event:")) event = line.slice(6).trim();
    else if(line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /,""));
  }
  if(dataLines.length === 0 && event === "message") return null;
  return { event, data: dataLines.join("\n") };
}

function safeJSON(str){
  try{ return JSON.parse(str); }catch(_){ return null; }
}

/* ============================================================
   Central SSE event handler for the /dub stream
   ============================================================ */
function handleSSEEvent(event, data){
  switch(event){
    case "log": {
      pushLog(data);
      const m = /source language detected:\s*(.+)/i.exec(data);
      if(m) lockSourceLanguage(m[1].trim());
      break;
    }
    case "progress": {
      const p = safeJSON(data);
      if(p && typeof p.percent === "number"){
        progressFill.classList.add("active");
        progressFill.classList.remove("paused");
        setProgress(p.percent, p.message || undefined);
      }
      break;
    }
    case "data": {
      const d = safeJSON(data);
      if(!d) break;
      if(d.source_language) lockSourceLanguage(d.source_language);
      if(d.session_id) state.sessionId = d.session_id;
      break;
    }
    case "error": {
      pushLog(String(data), "l-err");
      failRun("Backend reported an error.");
      break;
    }
    case "done": {
      const d = safeJSON(data) || {};
      finishRun(d);
      break;
    }
    default: {
      if(data) pushLog(`[${event}] ${data}`);
    }
  }
}

/* ============================================================
   Run lifecycle
   ============================================================ */
function startRunUI(){
  state.running = true;
  state.consoleCleared = false;
  generateBtn.classList.add("loading");
  generateBtn.disabled = true;
  resultPanel.classList.remove("show");
  progressFill.classList.remove("paused");
  progressFill.classList.add("active");
  setProgress(0, "Initializing…");
  resetSourceLanguage();
  setLive(true);
}

function endRunUI(){
  state.running = false;
  generateBtn.classList.remove("loading");
  progressFill.classList.remove("active");
  setLive(false);
  refreshGenerateState();
}

function failRun(reason){
  pushLog("[WARN] " + (reason || "Run stopped.") + " State reset.", "l-warn");
  if(state.abort){ try{ state.abort.abort(); }catch(_){}} 
  endRunUI();
}

function finishRun(d){
  setProgress(100, "Complete");
  pushLog("[SUCCESS] Dubbing finished. Preparing preview…", "l-ok");
  endRunUI();
  const sid = d.session_id || state.sessionId;
  if(sid){ state.sessionId = sid; presentResult(sid, d); }
}

/* ============================================================
   Result presentation + single-shot download
   The backend deletes the session after /download streams, so we fetch
   the file ONCE into an object URL and reuse it for both preview + save.
   ============================================================ */
async function presentResult(sessionId, meta){
  // Build meta tags
  resultMeta.innerHTML = "";
  const tags = [];
  if(meta.source_language) tags.push(["Source", meta.source_language]);
  if(meta.target_language) tags.push(["Target", meta.target_language]);
  if(typeof meta.segments !== "undefined") tags.push(["Segments", meta.segments]);
  tags.forEach(([k,v])=>{
    const el = document.createElement("span");
    el.className = "meta-tag";
    el.innerHTML = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg> ${esc(k)}: ${esc(String(v))}`;
    resultMeta.appendChild(el);
  });

  resultPanel.classList.add("show");
  const url = `${API_BASE}/download/${encodeURIComponent(sessionId)}`;

  try{
    pushLog("[INFO] Fetching dubbed video stream from server…");
    const res = await fetch(url, { method:"GET" });
    if(!res.ok) throw new Error("HTTP " + res.status);
    const blob = await res.blob();

    if(state.resultObjectUrl){ URL.revokeObjectURL(state.resultObjectUrl); }
    state.resultObjectUrl = URL.createObjectURL(blob);

    resultVideo.src = state.resultObjectUrl;
    downloadBtn.href = state.resultObjectUrl;
    downloadBtn.setAttribute("download", `dubbed_${sessionId}.mp4`);
    pushLog("[SUCCESS] Preview ready. Download available.", "l-ok");
  }catch(err){
    pushLog("[ERROR] Could not retrieve the dubbed file: " + err.message, "l-err");
    // Fallback: let the browser hit the endpoint directly on click.
    resultVideo.src = url;
    downloadBtn.href = url;
    downloadBtn.setAttribute("download", `dubbed_${sessionId}.mp4`);
  }
}

/* ============================================================
   Network: /dub
   ============================================================ */
async function startDub(){
  if(!state.file || state.running) return;

  state.targetLanguage = targetLang.value;
  startRunUI();
  pushLog(`[SYS] Uploading "${state.file.name}" (${fmtBytes(state.file.size)})…`, "l-sys");
  pushLog(`[INFO] Target language: ${targetLang.value} · Voice: ${voiceSelect.value}`);

  const fd = new FormData();
  fd.append("file", state.file);
  fd.append("target_language", targetLang.value);
  fd.append("voice_name", voiceSelect.value);

  state.abort = new AbortController();
  try{
    const res = await fetch(`${API_BASE}/dub`, { method:"POST", body:fd, signal:state.abort.signal });
    await readSSE(res, handleSSEEvent);
    // Stream ended. If we were mid-run without a done/error, note it.
    if(state.running){
      pushLog("[WARN] Stream closed unexpectedly before completion.", "l-warn");
      endRunUI();
    }
  }catch(err){
    if(err.name === "AbortError"){ /* user cancelled */ }
    else{
      pushLog("[ERROR] Connection failed: " + err.message, "l-err");
      failRun("Upload/stream error.");
    }
  }
}

generateBtn.addEventListener("click", startDub);

/* ============================================================
   Backend health check
   ============================================================ */
async function checkHealth(){
  try{
    const res = await fetch(`${API_BASE}/health`, { method:"GET" });
    const j = await res.json();
    if(j.status === "ok"){
      const hasKeys = (j.gemini_keys_configured|0) > 0 && (j.groq_keys_configured|0) > 0;
      serverDot.style.background = hasKeys ? "var(--ok)" : "var(--warn)";
      serverStatusText.textContent = hasKeys ? "Backend online" : "Online · API key missing";
    }else{
      serverDot.style.background = "var(--warn)";
      serverStatusText.textContent = "Backend degraded";
    }
  }catch(_){
    serverDot.style.background = "var(--err)";
    serverStatusText.textContent = "Backend unreachable";
  }
}

/* ============================================================
   Init
   ============================================================ */
buildMainVoiceSelect();
resetConsole();
refreshGenerateState();
checkHealth();
</script>
</body>
</html>