Spaces:
				
			
			
	
			
			
					
		Running
		
			on 
			
			CPU Upgrade
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
			on 
			
			CPU Upgrade
	File size: 77,035 Bytes
			
			| a3b454b 79389ec a3b454b 79389ec a3b454b 69028b4 a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec e4169b9 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e e4169b9 79389ec a3b454b 79389ec a3b454b 06ec34e 79389ec 06ec34e e4169b9 a3b454b 79389ec a3b454b 9eaf22a a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec a3b454b 3d0c02a 06ec34e 69028b4 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 35e5813 889687b 35e5813 889687b 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 21ee9d9 06ec34e 79389ec 06ec34e 3d0c02a 06ec34e 79389ec 06ec34e 79389ec 06ec34e 21ee9d9 06ec34e 889687b 06ec34e 69028b4 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e 79389ec 06ec34e a3b454b 79389ec a3b454b 79389ec a3b454b 79389ec 06ec34e 79389ec a3b454b 79389ec 750d70f a7724cf 750d70f a7724cf 750d70f a7724cf 750d70f 79389ec 06ec34e 79389ec a3b454b 889687b a3b454b cec65a4 79389ec cec65a4 79389ec cec65a4 a3b454b 79389ec cec65a4 9eaf22a 79389ec cec65a4 889687b cec65a4 35e5813 cec65a4 06ec34e 35e5813 06ec34e 79389ec cec65a4 35e5813 cec65a4 21ee9d9 cec65a4 06ec34e cec65a4 2e51155 cec65a4 fd346ae cec65a4 06ec34e 79389ec 3d0c02a cec65a4 06ec34e cec65a4 79389ec cec65a4 b967741 cec65a4 06ec34e cec65a4 06ec34e cec65a4 fd346ae cec65a4 2e51155 cec65a4 79389ec 144b33b cec65a4 79389ec cec65a4 79389ec 06ec34e cec65a4 9eaf22a 750d70f b6daf71 750d70f 9eaf22a a3b454b 06ec34e a3b454b cec65a4 750d70f a7724cf 750d70f cec65a4 b967741 750d70f cec65a4 750d70f 844ddfc cec65a4 79389ec cec65a4 b4ebdaf cec65a4 2ec4a9a cec65a4 750d70f cec65a4 06ec34e a3b454b 06ec34e a3b454b 06ec34e cec65a4 06ec34e 79389ec 06ec34e 2e51155 06ec34e 21ee9d9 06ec34e e886e32 335c836 e886e32 21ee9d9 e3f7f9a 06ec34e 79389ec 06ec34e 79389ec 35e5813 79389ec 06ec34e e886e32 06ec34e 79389ec 06ec34e e886e32 06ec34e 79389ec 06ec34e 79389ec 06ec34e e886e32 06ec34e 79389ec 335c836 e886e32 06ec34e 35e5813 06ec34e 79389ec 35e5813 79389ec 35e5813 79389ec 35e5813 79389ec 35e5813 e886e32 35e5813 e886e32 35e5813 e886e32 35e5813 e886e32 79389ec e886e32 79389ec e886e32 79389ec e886e32 06ec34e 335c836 cec65a4 750d70f 335c836 79389ec 335c836 79389ec 335c836 79389ec 335c836 79389ec 335c836 21ee9d9 335c836 21ee9d9 335c836 79389ec 3d0c02a 21ee9d9 3d0c02a 21ee9d9 335c836 79389ec 335c836 750d70f 79389ec 335c836 79389ec 335c836 e3f7f9a 335c836 79389ec 335c836 889687b 335c836 79389ec 335c836 79389ec 335c836 34f845e 335c836 34f845e 79389ec 34f845e 335c836 e65c775 79389ec 34f845e cec65a4 34f845e 79389ec 21ee9d9 34f845e 79389ec 34f845e 335c836 79389ec 335c836 e886e32 335c836 e886e32 79389ec 335c836 79389ec 335c836 79389ec 335c836 79389ec 335c836 e886e32 335c836 79389ec 335c836 21ee9d9 335c836 79389ec e3f7f9a 335c836 21ee9d9 335c836 e886e32 79389ec 335c836 35e5813 21ee9d9 3d0c02a 21ee9d9 144b33b 35e5813 335c836 e886e32 335c836 79389ec 335c836 79389ec 335c836 79389ec 335c836 79389ec 335c836 79389ec 335c836 e886e32 79389ec e886e32 35e5813 e886e32 35e5813 e886e32 35e5813 e886e32 79389ec 35e5813 e886e32 35e5813 79389ec e886e32 35e5813 e886e32 35e5813 e886e32 79389ec 335c836 79389ec e886e32 79389ec e886e32 79389ec e886e32 335c836 e886e32 79389ec 335c836 79389ec e886e32 79389ec e886e32 79389ec e886e32 79389ec e886e32 79389ec e886e32 335c836 e886e32 79389ec e886e32 79389ec e886e32 79389ec e886e32 79389ec 335c836 e886e32 79389ec e886e32 79389ec e886e32 335c836 06ec34e 335c836 06ec34e e886e32 335c836 06ec34e e886e32 335c836 e886e32 335c836 e886e32 79389ec e886e32 79389ec e886e32 79389ec e886e32 79389ec e886e32 e3f7f9a 06ec34e e3f7f9a 06ec34e 335c836 06ec34e 21ee9d9 06ec34e 21ee9d9 06ec34e 335c836 79389ec 335c836 06ec34e 79389ec e65c775 79389ec e65c775 06ec34e 79389ec 06ec34e 79389ec 06ec34e cec65a4 06ec34e cec65a4 06ec34e a3b454b 79389ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 | <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KREA Realtime Video</title>
    <link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;500;700&family=Roboto+Mono&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Ubuntu', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #fafaf9;
            color: #1c1917;
            min-height: 100vh;
        }
        .container {
            max-width: 1270px;
            margin: 0 auto;
            padding: 20px;
        }
        /* Login Strip */
        .login-strip {
            background: #fef3c7;
            border-bottom: 2px solid #f59e0b;
            padding: 15px 20px;
            box-shadow: 0 3px 0 0 #d97706;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .login-strip-content {
            max-width: 1600px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .login-message {
            color: #78350f;
            font-weight: 500;
        }
        .app-grayed {
            opacity: 0.4;
            pointer-events: none;
            filter: grayscale(50%);
        }
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.1s;
            text-decoration: none;
            display: inline-block;
            font-family: 'Ubuntu', sans-serif;
            border-width: 0px;
        }
        .btn-primary {
            background: #f59e0b;
            color: #78350f;
            box-shadow: 0px 3px 0px 0px #d97706;
        }
        .btn-primary:hover {
            box-shadow: 0px 5px 0px 0px #d97706;
            transform: translateY(-2px);
        }
        .btn-primary:active {
            box-shadow: 0px 2px 0px 0px #d97706;
            transform: translateY(1px);
        }
        .btn-primary:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        .btn-secondary {
            background: #fbbf24;
            color: #78350f;
            box-shadow: 0px 3px 0px 0px #f59e0b;
        }
        .btn-secondary:hover {
            box-shadow: 0px 5px 0px 0px #f59e0b;
            transform: translateY(-2px);
        }
        .btn-secondary:active {
            box-shadow: 0px 2px 0px 0px #f59e0b;
            transform: translateY(1px);
        }
        .btn-danger {
            background: #ef4444;
            color: white;
            box-shadow: 0px 3px 0px 0px #dc2626;
        }
        .btn-danger:hover {
            box-shadow: 0px 5px 0px 0px #dc2626;
            transform: translateY(-2px);
        }
        .btn-danger:active {
            box-shadow: 0px 2px 0px 0px #dc2626;
            transform: translateY(1px);
        }
        .info-box, .error-box, .warning-box {
            border-radius: 6px;
            padding: 12px;
            margin-top: 15px;
            font-size: 0.85rem;
            border-width: 1px;
            border-style: solid;
        }
        .info-box {
            background: #fef3c7;
            border-color: #fbbf24;
            color: #78350f;
        }
        .error-box {
            background: #fee2e2;
            border-color: #fca5a5;
            color: #7f1d1d;
        }
        .warning-box {
            background: #fef3c7;
            border-color: #fbbf24;
            color: #78350f;
        }
        /* User Bar */
        .user-bar {
            background: #fafaf9;
            border-radius: 8px;
            border: 1px solid #d6d3d1;
            padding: 15px 20px;
            margin-top: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0px 3px 0px 0px #d6d3d1;
        }
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            border: 2px solid #f59e0b;
        }
        .user-badge {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 0.7rem;
            font-weight: 600;
            text-transform: uppercase;
        }
        .badge-pro {
            background: #f59e0b;
            color: white;
        }
        .badge-free {
            background: #78716c;
            color: #e7e5e4;
        }
        .btn-logout {
            padding: 8px 16px;
            background: #78716c;
            color: #fafaf9;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.85rem;
        }
        .btn-logout:hover {
            background: #57534e;
        }
        #spinner, #placeholder {
            position: absolute;
        }
        /* Main App Layout */
        header {
            text-align: center;
            margin-bottom: 20px;
        }
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            color: #1c1917;
            font-weight: 700;
        }
        .header-links {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 10px;
        }
        .header-link {
            color: #f59e0b;
            text-decoration: none;
            font-size: 0.9rem;
            font-weight: 500;
        }
        .header-link:hover {
            text-decoration: underline;
        }
        .subtitle {
            color: #78716c;
            font-size: 0.9rem;
            margin-top: 5px;
        }
        /* Video Grid */
        .video-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }
        .video-grid.text-mode {
            grid-template-columns: 1fr;
        }
        .video-grid.text-mode .output-panel {
            max-width: 65%;
            margin: 0 auto;
        }
        @media (max-width: 900px) {
            .video-grid.text-mode .output-panel {
                max-width: 100%;
            }
        }
        .panel {
            background: #fafaf9;
            border-radius: 8px;
            border: 1px solid #d6d3d1;
            padding: 20px;
            box-shadow: 0px 3px 0px 0px #d6d3d1;
        }
        .panel h2 {
            font-size: 1.1rem;
            margin-bottom: 15px;
            color: #1c1917;
        }
        .video-container {
            background: #000;
            border-radius: 8px;
            aspect-ratio: 832/480;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
            box-shadow: 0px -1px 0px 0px #d6d3d1;
        }
        #outputCanvas, #webcamVideo {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
            border-radius: 6px;
        }
        .placeholder {
            text-align: center;
            color: #78716c;
        }
        .spinner {
            width: 50px;
            height: 50px;
            border: 4px solid #e7e5e4;
            border-top: 4px solid #f59e0b;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .status-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid #d6d3d1;
            font-size: 0.85rem;
        }
        .status-pill {
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 0.75rem;
            font-weight: 500;
            display: none;
        }
        .status-connected {
            background: #dcfce7;
            color: #15803d;
        }
        .status-disconnected {
            background: #fee2e2;
            color: #991b1b;
        }
        .status-finished {
            background: #dbeafe;
            color: #1e40af;
        }
        .mode-toggle {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }
        .mode-btn {
            flex: 1;
            padding: 5px;
            border: 1px solid #d6d3d1;
            border-radius: 6px;
            background: #fafaf9;
            color: #57534e;
            cursor: pointer;
            transition: all 0.2s;
            font-size: 0.85rem;
            font-weight: 500;
        }
        .mode-btn:hover:not(:disabled) {
            background: #fef3c7;
        }
        .mode-btn.active {
            background: #fbbf24;
            color: #78350f;
            box-shadow: 0px 3px 0px 0px #f59e0b;
            border-color: #f59e0b;
        }
        .mode-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            font-size: 0.85rem;
            color: #57534e;
            margin-bottom: 5px;
            font-weight: 500;
        }
        input[type="text"],
        input[type="number"],
        input[type="password"],
        textarea,
        select {
            width: 100%;
            padding: 8px 12px;
            background: #fafaf9;
            border: 1px solid #d6d3d1;
            border-radius: 6px;
            color: #1c1917;
            font-size: 0.9rem;
            box-shadow: 0px -1px 0px 0px #d6d3d1;
            font-family: 'Ubuntu', sans-serif;
        }
        input:focus, textarea:focus, select:focus {
            outline: none;
            border-color: #f59e0b;
            box-shadow: 0px -1px 0px 0px #f59e0b;
            background: #fef3c7;
        }
        textarea {
            min-height: 80px;
            resize: vertical;
            font-family: 'Ubuntu', sans-serif;
        }
        input[type="range"] {
            width: 100%;
            height: 4px;
            background: #d6d3d1;
            border-radius: 2px;
            outline: none;
            -webkit-appearance: none;
        }
        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 16px;
            height: 16px;
            background: #f59e0b;
            cursor: pointer;
            border-radius: 50%;
        }
        input[type="range"]::-moz-range-thumb {
            width: 16px;
            height: 16px;
            background: #f59e0b;
            cursor: pointer;
            border-radius: 50%;
            border: none;
        }
        .range-value {
            display: inline-block;
            margin-left: 10px;
            font-weight: 500;
            color: #f59e0b;
        }
        .hint {
            font-size: 0.75rem;
            color: #78716c;
            margin-top: 4px;
        }
        .progress-bar {
            width: 100%;
            height: 6px;
            background: #e7e5e4;
            border-radius: 3px;
            margin-top: 10px;
            overflow: hidden;
        }
        .progress-fill {
            height: 100%;
            background: #f59e0b;
            transition: width 0.3s ease;
            width: 0%;
        }
        .action-buttons {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        .hidden {
            display: none;
        }
        .limit-warning {
            background: #fee2e2;
            border: 1px solid #fca5a5;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            text-align: center;
        }
        .upgrade-link {
            color: #f59e0b;
            text-decoration: none;
            font-weight: 500;
        }
        .upgrade-link:hover {
            text-decoration: underline;
        }
        /* Accordion */
        .accordion {
            margin-top: 15px;
        }
        .accordion-header {
            background: #e7e5e4;
            padding: 12px 15px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 500;
            display: flex;
            justify-content: space-between;
            align-items: center;
            user-select: none;
            color: #1c1917;
            border: 1px solid #d6d3d1;
        }
        .accordion-header:hover {
            background: #d6d3d1;
        }
        .accordion-icon {
            transition: transform 0.2s;
        }
        .accordion-icon.open {
            transform: rotate(180deg);
        }
        .accordion-content {
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease;
        }
        .accordion-content.open {
            max-height: 1000px;
        }
        .accordion-body {
            padding: 15px 0;
        }
        /* Modal */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 1000;
        }
        .modal-overlay:not(.hidden) {
            display: flex;
        }
        .modal {
            background: #fafaf9;
            border-radius: 12px;
            border: 1px solid #d6d3d1;
            padding: 30px;
            max-width: 500px;
            width: 90%;
            box-shadow: 0px 10px 40px rgba(0, 0, 0, 0.3);
        }
        .modal h2 {
            color: #1c1917;
            margin-bottom: 15px;
            font-size: 1.5rem;
        }
        .modal p {
            color: #57534e;
            margin-bottom: 20px;
            line-height: 1.5;
        }
        .modal-options {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }
        .modal-option {
            padding: 15px;
            border: 2px solid #d6d3d1;
            border-radius: 8px;
            text-align: left;
            background: #fafaf9;
            cursor: pointer;
            transition: all 0.2s;
            text-decoration: none;
        }
        .modal-option:hover {
            border-color: #f59e0b;
            background: #fef3c7;
        }
        .modal-option h3 {
            color: #1c1917;
            font-size: 1rem;
            margin-bottom: 5px;
        }
        .modal-option p {
            color: #78716c;
            font-size: 0.85rem;
            margin: 0;
        }
        .modal-fal-key {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #d6d3d1;
        }
        .modal-fal-key h3 {
            color: #1c1917;
            font-size: 1rem;
            margin-bottom: 10px;
        }
        .modal-fal-key .form-group {
            margin-bottom: 10px;
        }
        @media (max-width: 1024px) {
            .video-grid {
                grid-template-columns: 1fr;
            }
            .user-bar {
                flex-direction: column;
                gap: 15px;
            }
            .mode-toggle {
                max-width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Login Strip (Hidden when authenticated) -->
        <div id="loginStrip" class="login-strip hidden">
            <div class="login-strip-content">
                <div class="login-message">
                    Sign in with Hugging Face to start generating videos
                </div>
                <button class="btn btn-primary" onclick="auth.loginWithOAuth()">
                    Sign in with Hugging Face
                </button>
            </div>
        </div>
        <!-- App Container (Grayed out when not authenticated) -->
        <div id="appContainer">
            <header>
                <h1>KREA Realtime Video</h1>
                <div class="header-links">
                    <a href="https://huggingface.co/krea/krea-realtime-video" target="_blank" class="header-link">Model on Hugging Face</a>
                    <span style="color: #d6d3d1;">•</span>
                    <a href="https://github.com/krea-ai/realtime-video" target="_blank" class="header-link">GitHub Repository</a>
                    <span style="color: #d6d3d1;">•</span>
                    <a href="https://fal.ai/models/fal-ai/krea/realtime-video" target="_blank" class="header-link">FAL API</a>
                </div>
            </header>
            <!-- Mode Selection -->
            <div class="mode-toggle">
                <button id="webcamModeBtn" class="mode-btn active">Webcam-to-Video</button>
                <button id="videoModeBtn" class="mode-btn">Video-to-Video</button>
                <button id="textModeBtn" class="mode-btn">Text-to-Video</button>
            </div>
            <!-- Video Grid: Webcam Left, Output Right -->
            <div id="videoGrid" class="video-grid">
                <!-- Input Panel (Webcam or Video) -->
                <div id="inputPanel" class="panel">
                    <h2 id="inputPanelTitle">Webcam Input</h2>
                    <!-- Webcam Video -->
                    <div id="webcamContainer" class="video-container">
                        <video id="webcamVideo" autoplay playsinline muted></video>
                        <div id="webcamPlaceholder" class="placeholder">
                            <p>Webcam will appear here</p>
                        </div>
                    </div>
                    <!-- Uploaded Video Preview -->
                    <div id="uploadedVideoContainer" class="video-container hidden">
                        <video id="uploadedVideoPreview" loop muted playsinline></video>
                        <div id="uploadedVideoPlaceholder" class="placeholder">
                            <p>Upload a video to see preview</p>
                        </div>
                    </div>
                    <!-- Video Upload UI (for video mode) -->
                    <div id="videoUploadUI" class="hidden" style="margin-top: 15px;">
                        <input type="file" id="videoFileInput" accept="video/*" class="hidden">
                        <button class="btn btn-secondary" style="width: 100%;" onclick="document.getElementById('videoFileInput').click()">
                            Upload Video
                        </button>
                        <div id="videoInfoDisplay" style="margin-top: 10px; font-size: 0.85rem; color: #78716c;"></div>
                    </div>
                </div>
                <!-- Video Output Panel -->
                <div class="panel output-panel">
                    <h2>Generated Video</h2>
                    <div class="video-container">
                        <canvas id="outputCanvas"></canvas>
                        <div id="spinner" class="spinner hidden"></div>
                        <div id="placeholder" class="placeholder">
                            <p>Configure settings and click Start</p>
                        </div>
                    </div>
                    <div class="status-bar">
                        <div>
                            <span id="statusPill" class="status-pill status-disconnected">Disconnected</span>
                            <span style="margin-left: 15px;">Frames: <strong id="frameCount">0</strong> / <strong id="maxFrames">474</strong></span>
                        </div>
                        <div>
                            <label style="display: inline; margin: 0;">Playback:</label>
                            <input type="range" id="playbackFps" min="8" max="20" value="8" style="width: 100px;">
                            <span class="range-value" id="playbackFpsValue">8 fps</span>
                        </div>
                    </div>
                    <!-- Video Controls (Replay & Download) -->
                    <div id="videoControls" class="hidden" style="display: flex; gap: 10px; margin-top: 10px;">
                        <button id="replayBtn" class="btn btn-secondary" onclick="app.replayVideo()" style="flex: 1;">
                            Replay
                        </button>
                        <button id="downloadBtn" class="btn btn-secondary" onclick="app.downloadVideo()" style="flex: 1;">
                            Download
                        </button>
                    </div>
                    <div class="progress-bar">
                        <div id="progressFill" class="progress-fill"></div>
                    </div>
                </div>
            </div>
            <!-- Controls Panel -->
            <div class="panel">
                <!-- Prompt (Main Setting) -->
                <div class="form-group">
                    <label>Prompt <span style="color: #15803d; font-size: 0.75rem;">(updates sent every 2 seconds)</span></label>
                    <textarea id="prompt">A small, fluffy cat. Soft morning light filters through sheer curtains, casting warm, golden patterns across its fur. The cat’s coat is silky and well-groomed, a mix of cream and light orange tones that shimmer gently as it breathes. Dust motes drift lazily in the air, glowing in the sunlight. The room is peaceful — a few plants rest by the window, and a faint breeze causes the curtains to sway. The cat’s eyes are half-closed, reflecting a soft green hue, and its tiny ears twitch at distant sounds outside. Everything feels calm, intimate, and comforting — a portrait of stillness and warmth.</textarea>
                    <div class="hint">Detailed prompts work better</div>
                </div>
                <!-- Action Buttons (Outside Accordion) -->
                <div class="action-buttons">
                    <button id="startStopBtn" class="btn btn-primary" onclick="app.toggleGeneration()" style="flex: 1;">
                        Start Generation
                    </button>
                </div>
                <!-- Advanced Settings Accordion -->
                <div class="accordion">
                    <div class="accordion-header" onclick="app.toggleAccordion()">
                        <span>Advanced Settings</span>
                        <span class="accordion-icon" id="accordionIcon">▼</span>
                    </div>
                    <div class="accordion-content" id="accordionContent">
                        <div class="accordion-body">
                            <!-- Input Controls (V2V and Webcam modes) -->
                            <div id="inputControls">
                                <div class="form-group">
                                    <label>Input Frame Rate: <span class="range-value" id="inputFpsValue">10</span> fps</label>
                                    <input type="range" id="inputFps" min="8" max="14" value="10">
                                    <div class="hint">Recommended: 10-12 fps</div>
                                </div>
                                <div class="form-group">
                                    <label>Transformation Strength: <span class="range-value" id="strengthValue">0.45</span></label>
                                    <input type="range" id="strength" min="0.05" max="1" step="0.05" value="0.45">
                                    <div class="hint">Lower values preserve input better, higher values allow more creative freedom</div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Max Frames (Blocks): <span class="range-value" id="numBlocksValue">25</span> → ~<span id="estimatedFrames">294</span> frames</label>
                                <input type="range" id="numBlocks" min="10" max="50" step="5" value="25">
                                <div class="hint">Each block generates ~12 frames</div>
                            </div>
                            <div class="form-group">
                                <label>Seed (optional)</label>
                                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                                    <input type="number" id="seed" placeholder="random">
                                    <button class="btn btn-secondary" onclick="app.randomizeSeed()">Random</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="errorBox" class="error-box hidden"></div>
                <div id="infoBox" class="info-box hidden"></div>
            </div>
            <!-- Limit Reached Modal -->
            <div id="limitModal" class="modal-overlay hidden">
                <div class="modal">
                    <h2>Daily Limit Reached</h2>
                    <p>You've used all your generations for today. Here are your options:</p>
                    <div class="modal-options">
                        <!-- Subscribe to PRO (Free users only) -->
                        <a href="https://huggingface.co/subscribe/pro" target="_blank" class="modal-option" id="proOption">
                            <h3>Subscribe to PRO</h3>
                            <p>Get more generations per day with Hugging Face PRO</p>
                        </a>
                        <!-- Run Locally -->
                        <a href="https://huggingface.co/krea/krea-realtime-video" target="_blank" class="modal-option">
                            <h3>Run Locally</h3>
                            <p>Download and run the model on your own hardware</p>
                        </a>
                        <!-- Use API -->
                        <a href="https://fal.ai" target="_blank" class="modal-option">
                            <h3>Use with API</h3>
                            <p>Integrate into your own applications with FAL API</p>
                        </a>
                    </div>
                    <!-- FAL Key Input -->
                    <div class="modal-fal-key">
                        <h3>Or use your own FAL API Key</h3>
                        <div class="form-group">
                            <label>FAL API Key <a href="https://fal.ai/dashboard/keys" target="_blank" style="color: #f59e0b; font-size: 0.75rem;">(Get your key)</a></label>
                            <input type="password" id="falKeyInput" placeholder="Enter your FAL API key">
                        </div>
                        <button class="btn btn-primary" style="width: 100%;" onclick="limitModal.saveFalKey()">
                            Save & Continue
                        </button>
                        <p style="font-size: 0.75rem; color: #78716c; margin-top: 10px;">
                            Your key is stored locally and never sent to our servers
                        </p>
                    </div>
                    <button onclick="limitModal.close()" style="margin-top: 20px; background: transparent; border: none; color: #78716c; cursor: pointer; width: 100%;">
                        Close
                    </button>
                </div>
            </div>
            <!-- User Bar (Hidden when not authenticated) -->
            <div id="userBar" class="user-bar hidden">
                <div class="user-info">
                    <img id="userAvatar" src="" alt="Avatar" class="user-avatar">
                    <div class="user-details">
                        <h3 id="userFullname" style="font-size: 1rem; margin-bottom: 3px;"></h3>
                        <span id="userBadge" class="user-badge"></span>
                    </div>
                </div>
                <div style="text-align: right;">
                    <button class="btn-logout" onclick="auth.logout()">Logout</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Hidden elements for video processing -->
    <video id="hiddenVideo" style="display: none;" muted playsinline></video>
    <canvas id="extractionCanvas" style="display: none;"></canvas>
    <script>
        const OAUTH_CLIENT_ID = "{{ oauth_client_id }}";
        const AUTH_STORAGE_KEY = 'HF_AUTH_STATE';
        const FAL_KEY_STORAGE = 'FAL_API_KEY';
        // Limit Modal Manager
        const limitModal = {
            show(isPro) {
                // Show/hide PRO option based on user tier
                const proOption = document.getElementById('proOption');
                if (isPro) {
                    proOption.style.display = 'none';
                } else {
                    proOption.style.display = 'block';
                }
                // Check if FAL key already saved
                const savedKey = localStorage.getItem(FAL_KEY_STORAGE);
                if (savedKey) {
                    document.getElementById('falKeyInput').value = '••••••••••••';
                }
                document.getElementById('limitModal').classList.remove('hidden');
            },
            close() {
                const modal = document.getElementById('limitModal');
                if (modal) {
                    modal.classList.add('hidden');
                }
            },
            saveFalKey() {
                const key = document.getElementById('falKeyInput').value.trim();
                if (!key || key === '••••••••••••') {
                    app.showError('Please enter a valid FAL API key');
                    return;
                }
                localStorage.setItem(FAL_KEY_STORAGE, key);
                app.showInfo('FAL API key saved! You can now use unlimited generations.');
                auth.hasFalKey = true;
                this.close();
            },
            hasFalKey() {
                return !!localStorage.getItem(FAL_KEY_STORAGE);
            },
            getFalKey() {
                return localStorage.getItem(FAL_KEY_STORAGE);
            }
        };
        // Authentication Manager
        const auth = {
            token: null,
            user: null,
            canStart: false,
            sessionsUsed: 0,
            sessionsLimit: 2,
            hasFalKey: false,
            async init() {
                // Check if user has FAL key
                this.hasFalKey = limitModal.hasFalKey();
                // Try to restore saved auth from localStorage
                const saved = localStorage.getItem(AUTH_STORAGE_KEY);
                if (saved) {
                    try {
                        const authState = JSON.parse(saved);
                        await this.validateAndSetAuth(authState.token);
                    } catch (e) {
                        console.error('Failed to restore auth:', e);
                        this.clearAuth();
                    }
                } else {
                    this.showLoginStrip();
                }
            },
            async validateAndSetAuth(token) {
                try {
                    const response = await fetch('/api/whoami', {
                        headers: { 'Authorization': `Bearer ${token}` }
                    });
                    if (!response.ok) {
                        throw new Error('Token validation failed with status: ' + response.status);
                    }
                    const data = await response.json();
                    this.token = token;
                    this.user = data;
                    this.canStart = data.can_start;
                    this.sessionsUsed = data.sessions_used;
                    this.sessionsLimit = data.sessions_limit;
                    // Save to localStorage
                    localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify({ token, user: data }));
                    this.updateUI();
                    app.init();
                } catch (error) {
                    console.error('Auth validation failed:', error);
                    this.clearAuth();
                    throw error;
                }
            },
            loginWithOAuth() {
                window.location.href = '/api/auth/login';
            },
            logout() {
                this.clearAuth();
                window.location.reload();
            },
            clearAuth() {
                this.token = null;
                this.user = null;
                this.canStart = false;
                localStorage.removeItem(AUTH_STORAGE_KEY);
                this.showLoginStrip();
            },
            showLoginStrip() {
                document.getElementById('loginStrip').classList.remove('hidden');
                document.getElementById('appContainer').classList.add('app-grayed');
                document.getElementById('userBar').classList.add('hidden');
            },
            updateUI() {
                // Hide login strip, show app
                document.getElementById('loginStrip').classList.add('hidden');
                document.getElementById('appContainer').classList.remove('app-grayed');
                // Show user bar
                const userBar = document.getElementById('userBar');
                userBar.classList.remove('hidden');
                // Update user info
                if (this.user.avatar) {
                    document.getElementById('userAvatar').src = this.user.avatar;
                }
                document.getElementById('userFullname').textContent = this.user.fullname;
                const badge = document.getElementById('userBadge');
                badge.textContent = this.user.is_pro ? 'PRO' : 'FREE';
                badge.className = 'user-badge ' + (this.user.is_pro ? 'badge-pro' : 'badge-free');
                // No longer show banner - modal will appear when user tries to generate
            },
            getAuthHeaders() {
                return this.token ? { 'Authorization': `Bearer ${this.token}` } : {};
            }
        };
        // MsgPack Encoder
        const createMsgpackEncoder = (() => {
            const textEncoder = new TextEncoder();
            const ensureUint8Array = (value) => (value instanceof Uint8Array ? value : new Uint8Array(value));
            const writeUInt8 = (buffer, value) => buffer.push(value & 0xff);
            const writeUInt16 = (buffer, value) => {
                buffer.push((value >>> 8) & 0xff, value & 0xff);
            };
            const writeUInt32 = (buffer, value) => {
                buffer.push((value >>> 24) & 0xff, (value >>> 16) & 0xff, (value >>> 8) & 0xff, value & 0xff);
            };
            const writeFloat64 = (buffer, value) => {
                const array = new ArrayBuffer(8);
                new DataView(array).setFloat64(0, value);
                buffer.push(...new Uint8Array(array));
            };
            const encodeString = (buffer, value) => {
                const utf8 = ensureUint8Array(textEncoder.encode(value));
                const length = utf8.length;
                if (length <= 31) {
                    writeUInt8(buffer, 0xa0 | length);
                } else if (length <= 0xff) {
                    writeUInt8(buffer, 0xd9);
                    writeUInt8(buffer, length);
                } else if (length <= 0xffff) {
                    writeUInt8(buffer, 0xda);
                    writeUInt16(buffer, length);
                } else {
                    writeUInt8(buffer, 0xdb);
                    writeUInt32(buffer, length);
                }
                buffer.push(...utf8);
            };
            const encodeNumber = (buffer, value) => {
                if (Number.isInteger(value)) {
                    if (value >= 0 && value <= 0x7f) {
                        writeUInt8(buffer, value);
                    } else if (value < 0 && value >= -32) {
                        buffer.push(value & 0xff);
                    } else if (value >= 0 && value <= 0xff) {
                        writeUInt8(buffer, 0xcc);
                        writeUInt8(buffer, value);
                    } else if (value >= 0 && value <= 0xffff) {
                        writeUInt8(buffer, 0xcd);
                        writeUInt16(buffer, value);
                    } else if (value >= 0 && value <= 0xffffffff) {
                        writeUInt8(buffer, 0xce);
                        writeUInt32(buffer, value);
                    } else {
                        writeUInt8(buffer, 0xcb);
                        writeFloat64(buffer, value);
                    }
                } else {
                    writeUInt8(buffer, 0xcb);
                    writeFloat64(buffer, value);
                }
            };
            const encodeArray = (buffer, value) => {
                const length = value.length;
                if (length < 16) {
                    writeUInt8(buffer, 0x90 | length);
                } else if (length <= 0xffff) {
                    writeUInt8(buffer, 0xdc);
                    writeUInt16(buffer, length);
                } else {
                    writeUInt8(buffer, 0xdd);
                    writeUInt32(buffer, length);
                }
                value.forEach((item) => encodeValue(buffer, item));
            };
            const encodeObject = (buffer, value) => {
                const entries = Object.entries(value).filter(([, v]) => v !== undefined);
                const length = entries.length;
                if (length < 16) {
                    writeUInt8(buffer, 0x80 | length);
                } else if (length <= 0xffff) {
                    writeUInt8(buffer, 0xde);
                    writeUInt16(buffer, length);
                } else {
                    writeUInt8(buffer, 0xdf);
                    writeUInt32(buffer, length);
                }
                for (const [key, entryValue] of entries) {
                    encodeString(buffer, key);
                    encodeValue(buffer, entryValue);
                }
            };
            function encodeValue(buffer, value) {
                if (value === null) {
                    writeUInt8(buffer, 0xc0);
                } else if (value === false) {
                    writeUInt8(buffer, 0xc2);
                } else if (value === true) {
                    writeUInt8(buffer, 0xc3);
                } else if (typeof value === "number") {
                    encodeNumber(buffer, value);
                } else if (typeof value === "string") {
                    encodeString(buffer, value);
                } else if (Array.isArray(value)) {
                    encodeArray(buffer, value);
                } else if (value instanceof Uint8Array) {
                    const length = value.length;
                    if (length <= 0xff) {
                        writeUInt8(buffer, 0xc4);
                        writeUInt8(buffer, length);
                    } else if (length <= 0xffff) {
                        writeUInt8(buffer, 0xc5);
                        writeUInt16(buffer, length);
                    } else {
                        writeUInt8(buffer, 0xc6);
                        writeUInt32(buffer, length);
                    }
                    buffer.push(...value);
                } else if (value && typeof value === "object") {
                    encodeObject(buffer, value);
                } else {
                    throw new Error("Unsupported type for MsgPack encoding");
                }
            }
            return (input) => {
                const buffer = [];
                encodeValue(buffer, input);
                return new Uint8Array(buffer);
            };
        })();
        // Main App
        const app = {
            mode: 'webcam',
            isGenerating: false,
            frameCount: 0,
            maxFrames: 474,
            ws: null,
            frameBuffer: [],
            allFrames: [], // Store all frames for replay
            playbackInterval: null,
            frameExtractionInterval: null,
            webcamStream: null,
            currentVideoFile: null,
            videoMetadata: null,
            mediaRecorder: null,
            recordedChunks: [],
            promptUpdateTimer: null,
            pendingPromptUpdate: null,
            generationFinished: false,
            idleTimeout: null,
            lastFrameTime: null,
            init() {
                this.setupEventListeners();
                this.updateEstimatedFrames();
                // Auto-start webcam since it's the default mode
                this.setMode('webcam');
            },
            toggleAccordion() {
                const content = document.getElementById('accordionContent');
                const icon = document.getElementById('accordionIcon');
                content.classList.toggle('open');
                icon.classList.toggle('open');
            },
            setupEventListeners() {
                document.getElementById('textModeBtn').addEventListener('click', () => this.setMode('text'));
                document.getElementById('videoModeBtn').addEventListener('click', () => this.setMode('video'));
                document.getElementById('webcamModeBtn').addEventListener('click', () => this.setMode('webcam'));
                // Video file upload (new location in left panel)
                document.getElementById('videoFileInput').addEventListener('change', (e) => this.handleVideoUpload(e));
                document.getElementById('playbackFps').addEventListener('input', (e) => {
                    document.getElementById('playbackFpsValue').textContent = e.target.value + ' fps';
                    if (this.playbackInterval) {
                        this.startPlaybackLoop();
                    }
                });
                document.getElementById('inputFps').addEventListener('input', (e) => {
                    document.getElementById('inputFpsValue').textContent = e.target.value;
                    if (this.isGenerating && (this.mode === 'video' || this.mode === 'webcam')) {
                        this.stopFrameExtraction();
                        this.startFrameExtraction();
                    }
                });
                document.getElementById('strength').addEventListener('input', (e) => {
                    document.getElementById('strengthValue').textContent = parseFloat(e.target.value).toFixed(2);
                });
                document.getElementById('numBlocks').addEventListener('input', (e) => {
                    document.getElementById('numBlocksValue').textContent = e.target.value;
                    this.updateEstimatedFrames();
                    if (this.isGenerating) {
                        this.queuePromptUpdate();
                    }
                });
                // Prompt changes for live updates
                document.getElementById('prompt').addEventListener('input', () => {
                    if (this.isGenerating && (this.mode === 'text' || this.mode === 'webcam')) {
                        this.queuePromptUpdate();
                    }
                });
            },
            setMode(mode) {
                if (this.isGenerating) return;
                this.mode = mode;
                // Update mode buttons
                document.getElementById('textModeBtn').classList.toggle('active', mode === 'text');
                document.getElementById('videoModeBtn').classList.toggle('active', mode === 'video');
                document.getElementById('webcamModeBtn').classList.toggle('active', mode === 'webcam');
                // Update grid layout for text mode (center output, hide input)
                const videoGrid = document.getElementById('videoGrid');
                const inputPanel = document.getElementById('inputPanel');
                if (mode === 'text') {
                    videoGrid.classList.add('text-mode');
                    inputPanel.classList.add('hidden');
                } else {
                    videoGrid.classList.remove('text-mode');
                    inputPanel.classList.remove('hidden');
                }
                // Update input panel title and content
                const inputPanelTitle = document.getElementById('inputPanelTitle');
                const webcamContainer = document.getElementById('webcamContainer');
                const uploadedVideoContainer = document.getElementById('uploadedVideoContainer');
                const videoUploadUI = document.getElementById('videoUploadUI');
                if (mode === 'webcam') {
                    inputPanelTitle.textContent = 'Webcam Input';
                    webcamContainer.classList.remove('hidden');
                    uploadedVideoContainer.classList.add('hidden');
                    videoUploadUI.classList.add('hidden');
                    this.startWebcam();
                } else if (mode === 'video') {
                    inputPanelTitle.textContent = 'Video Input';
                    webcamContainer.classList.add('hidden');
                    uploadedVideoContainer.classList.remove('hidden');
                    videoUploadUI.classList.remove('hidden');
                    this.stopWebcam();
                } else {
                    this.stopWebcam();
                }
                // Input controls visible in accordion for webcam/video modes
                const inputControls = document.getElementById('inputControls');
                if (mode === 'text') {
                    inputControls.style.display = 'none';
                } else {
                    inputControls.style.display = 'block';
                }
            },
            async startWebcam() {
                try {
                    this.webcamStream = await navigator.mediaDevices.getUserMedia({
                        video: {
                            width: { ideal: 1280 },
                            height: { ideal: 720 }
                        },
                        audio: false
                    });
                    const video = document.getElementById('webcamVideo');
                    video.srcObject = this.webcamStream;
                    document.getElementById('webcamPlaceholder').style.display = 'none';
                    this.showInfo('Webcam started successfully');
                } catch (error) {
                    this.showError(`Failed to access webcam: ${error.message}`);
                    // Switch back to text mode if webcam fails
                    this.setMode('text');
                }
            },
            stopWebcam() {
                if (this.webcamStream) {
                    this.webcamStream.getTracks().forEach(track => track.stop());
                    this.webcamStream = null;
                    const video = document.getElementById('webcamVideo');
                    video.srcObject = null;
                    document.getElementById('webcamPlaceholder').style.display = 'flex';
                }
            },
            updateEstimatedFrames() {
                const numBlocks = parseInt(document.getElementById('numBlocks').value);
                const estimatedFrames = (12 * numBlocks) - 6;
                this.maxFrames = estimatedFrames;
                document.getElementById('estimatedFrames').textContent = estimatedFrames;
                document.getElementById('maxFrames').textContent = estimatedFrames;
            },
            randomizeSeed() {
                document.getElementById('seed').value = Math.floor(Math.random() * (1 << 24));
            },
            async toggleGeneration() {
                if (this.isGenerating) {
                    this.disconnect();
                } else {
                    if (!auth.token) {
                        this.showError('Please sign in first');
                        return;
                    }
                    // Check if user has FAL key - if so, skip HF generation limits
                    if (!auth.hasFalKey) {
                        // Record session and check limits
                        try {
                            const response = await fetch('/api/start-session', {
                                method: 'POST',
                                headers: auth.getAuthHeaders()
                            });
                            if (!response.ok) {
                                const data = await response.json();
                                // If limit reached, show modal
                                if (response.status === 429) {
                                    limitModal.show(auth.user.is_pro);
                                    return;
                                }
                                this.showError(data.detail || 'Failed to start session');
                                return;
                            }
                            // Update session count
                            const sessionData = await response.json();
                            auth.sessionsUsed = sessionData.sessions_used;
                            auth.sessionsLimit = sessionData.sessions_limit;
                            auth.canStart = sessionData.sessions_used < sessionData.sessions_limit;
                        } catch (error) {
                            this.showError('Failed to start session');
                            return;
                        }
                    }
                    const prompt = document.getElementById('prompt').value.trim();
                    if (!prompt) {
                        this.showError('Please enter a prompt');
                        return;
                    }
                    if (this.mode === 'video' && !this.currentVideoFile) {
                        this.showError('Please upload a video file');
                        return;
                    }
                    if (this.mode === 'webcam' && !this.webcamStream) {
                        this.showError('Webcam not started');
                        return;
                    }
                    this.isGenerating = true;
                    this.frameCount = 0;
                    this.generationFinished = false;
                    document.getElementById('frameCount').textContent = '0';
                    this.frameBuffer = [];
                    this.allFrames = [];
                    this.updateUI();
                    // Show spinner, hide placeholder and video controls
                    document.getElementById('spinner').classList.remove('hidden');
                    document.getElementById('placeholder').style.display = 'none';
                    document.getElementById('videoControls').classList.add('hidden');
                    // Start recording
                    this.startRecording();
                    // Connect to backend WebSocket proxy
                    const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
                    let wsUrl = `${protocol}//${window.location.host}/ws/video-gen`;
                    // If user has their own FAL key, pass it as query param
                    const userFalKey = limitModal.getFalKey();
                    if (userFalKey) {
                        wsUrl += `?user_fal_key=${encodeURIComponent(userFalKey)}`;
                    }
                    try {
                        this.ws = new WebSocket(wsUrl);
                        this.ws.binaryType = 'arraybuffer';
                        this.ws.onopen = () => {
                            this.showInfo('Connected! Waiting for ready signal...');
                            this.startIdleTimeout();
                        };
                        this.ws.onmessage = async (event) => {
                            if (typeof event.data === 'string') {
                                try {
                                    const data = JSON.parse(event.data);
                                    if (data.status === 'ready') {
                                        this.showInfo('Ready - sending parameters...');
                                        await this.sendInitialParams();
                                    } else if (data.error) {
                                        this.showError(`Server error: ${JSON.stringify(data.error)}`);
                                        this.disconnect();
                                    }
                                } catch (e) {
                                    console.error('WebSocket message parse error:', e);
                                }
                            } else if (event.data instanceof ArrayBuffer) {
                                await this.displayFrame(event.data);
                            }
                        };
                        this.ws.onerror = (error) => {
                            this.showError('WebSocket connection error');
                            console.error('WebSocket error:', error);
                        };
                        this.ws.onclose = (event) => {
                            // Force state reset
                            this.isGenerating = false;
                            this.ws = null;
                            // Stop all processes
                            this.stopFrameExtraction();
                            this.stopRecording();
                            this.stopPlaybackLoop();
                            // Update UI immediately
                            const btn = document.getElementById('startStopBtn');
                            if (btn) {
                                btn.textContent = 'Start Generation';
                                btn.className = 'btn btn-primary';
                                btn.disabled = !auth.canStart;
                            }
                            // Update status - only if not already finished
                            if (!this.generationFinished) {
                                const statusPill = document.getElementById('statusPill');
                                if (statusPill) {
                                    statusPill.className = 'status-pill status-disconnected';
                                    statusPill.textContent = 'Disconnected';
                                }
                                this.showInfo(`Disconnected: ${event.reason || 'Connection closed'}`);
                            }
                            // Re-enable mode buttons
                            ['textModeBtn', 'videoModeBtn', 'webcamModeBtn'].forEach(id => {
                                const btn = document.getElementById(id);
                                if (btn) btn.disabled = false;
                            });
                        };
                    } catch (error) {
                        this.showError('Failed to connect: ' + error.message);
                        this.isGenerating = false;
                        this.updateUI();
                    }
                }
            },
            async sendInitialParams() {
                const payload = {
                    prompt: document.getElementById('prompt').value,
                    num_blocks: parseInt(document.getElementById('numBlocks').value),
                    num_denoising_steps: 4,
                    strength: parseFloat(document.getElementById('strength').value) || 0.45,
                    width: 832,
                    height: 480
                };
                // Add start_frame for video and webcam modes
                if (this.mode === 'video' && this.currentVideoFile) {
                    try {
                        const video = document.getElementById('hiddenVideo');
                        video.currentTime = 0;
                        await new Promise((resolve, reject) => {
                            const timeout = setTimeout(() => reject(new Error('Video seek timeout')), 5000);
                            video.onseeked = () => {
                                clearTimeout(timeout);
                                resolve();
                            };
                        });
                        const startFrame = await this.extractVideoFrameBytes();
                        if (!startFrame) throw new Error('Failed to extract start frame');
                        payload.start_frame = startFrame;
                    } catch (error) {
                        this.showError(`Failed to extract start frame: ${error.message}`);
                        this.disconnect();
                        return;
                    }
                } else if (this.mode === 'webcam') {
                    try {
                        const startFrame = await this.extractWebcamBytes();
                        if (!startFrame) throw new Error('Failed to extract webcam frame');
                        payload.start_frame = startFrame;
                    } catch (error) {
                        this.showError(`Failed to extract webcam frame: ${error.message}`);
                        this.disconnect();
                        return;
                    }
                }
                const seedValue = document.getElementById('seed').value;
                if (seedValue && seedValue.trim() !== '') {
                    payload.seed = parseInt(seedValue);
                } else {
                    payload.seed = Math.floor(Math.random() * (1 << 24));
                }
                try {
                    const encoded = createMsgpackEncoder(payload);
                    this.ws.send(encoded);
                    this.showInfo('Generation started!');
                    // Start frame extraction for v2v and webcam modes
                    if (this.mode === 'video' || this.mode === 'webcam') {
                        setTimeout(() => this.startFrameExtraction(), 500);
                    }
                } catch (error) {
                    this.showError('Failed to send parameters: ' + error.message);
                }
            },
            async displayFrame(imageData) {
                const blob = new Blob([imageData], { type: 'image/jpeg' });
                const bitmap = await createImageBitmap(blob);
                this.frameBuffer.push(bitmap);
                this.allFrames.push(imageData); // Store raw data for replay
                this.frameCount++;
                document.getElementById('frameCount').textContent = this.frameCount;
                // Reset idle timeout on every frame
                this.startIdleTimeout();
                if (this.frameCount === 1) {
                    document.getElementById('spinner').classList.add('hidden');
                    this.startPlaybackLoop();
                }
                // Update progress
                const progress = Math.min(100, (this.frameCount / this.maxFrames) * 100);
                document.getElementById('progressFill').style.width = progress + '%';
                // Check if generation is complete
                if (this.frameCount >= this.maxFrames) {
                    this.generationFinished = true;
                    // Update status to Finished
                    const statusPill = document.getElementById('statusPill');
                    statusPill.className = 'status-pill status-finished';
                    statusPill.textContent = 'Finished';
                    // Show video controls (replay and download)
                    document.getElementById('videoControls').classList.remove('hidden');
                    // Disconnect to clean up (will trigger onclose which resets UI)
                    setTimeout(() => {
                        this.disconnect();
                        this.showInfo('Generation complete!');
                    }, 500);
                }
            },
            drawNextFrame() {
                if (this.frameBuffer.length === 0) return;
                const canvas = document.getElementById('outputCanvas');
                const bitmap = this.frameBuffer.shift();
                canvas.width = bitmap.width;
                canvas.height = bitmap.height;
                const ctx = canvas.getContext('2d');
                ctx.drawImage(bitmap, 0, 0);
                if (typeof bitmap.close === 'function') {
                    bitmap.close();
                }
            },
            startPlaybackLoop() {
                if (this.playbackInterval) {
                    clearInterval(this.playbackInterval);
                }
                const fps = parseInt(document.getElementById('playbackFps').value);
                const intervalMs = Math.max(10, Math.floor(1000 / fps));
                this.playbackInterval = setInterval(() => this.drawNextFrame(), intervalMs);
            },
            stopPlaybackLoop() {
                if (this.playbackInterval) {
                    clearInterval(this.playbackInterval);
                    this.playbackInterval = null;
                }
            },
            async handleVideoUpload(event) {
                const file = event.target.files?.[0];
                if (!file) return;
                this.currentVideoFile = file;
                const hiddenVideo = document.getElementById('hiddenVideo');
                const previewVideo = document.getElementById('uploadedVideoPreview');
                const url = URL.createObjectURL(file);
                return new Promise((resolve) => {
                    hiddenVideo.onloadedmetadata = () => {
                        this.videoMetadata = {
                            duration: hiddenVideo.duration,
                            width: hiddenVideo.videoWidth,
                            height: hiddenVideo.videoHeight
                        };
                        // Update info display
                        const infoHTML = `
                            ${file.name}<br>
                            ${this.videoMetadata.width}×${this.videoMetadata.height} • ${this.videoMetadata.duration.toFixed(1)}s
                        `;
                        document.getElementById('videoInfoDisplay').innerHTML = infoHTML;
                        // Show preview video
                        previewVideo.src = url;
                        previewVideo.play();
                        document.getElementById('uploadedVideoPlaceholder').style.display = 'none';
                        this.showInfo('Video loaded successfully');
                        resolve();
                    };
                    hiddenVideo.onerror = () => {
                        this.showError('Failed to load video');
                        this.currentVideoFile = null;
                        this.videoMetadata = null;
                    };
                    hiddenVideo.src = url;
                });
            },
            async extractVideoFrameBytes() {
                const video = document.getElementById('hiddenVideo');
                const canvas = document.getElementById('extractionCanvas');
                if (!video || !canvas) return null;
                const ctx = canvas.getContext('2d');
                canvas.width = 832;
                canvas.height = 480;
                // Scale and crop to 832x480
                const scale = Math.max(832 / video.videoWidth, 480 / video.videoHeight);
                const scaledWidth = video.videoWidth * scale;
                const scaledHeight = video.videoHeight * scale;
                const offsetX = (832 - scaledWidth) / 2;
                const offsetY = (480 - scaledHeight) / 2;
                ctx.clearRect(0, 0, 832, 480);
                ctx.drawImage(video, offsetX, offsetY, scaledWidth, scaledHeight);
                return new Promise((resolve) => {
                    canvas.toBlob(async (blob) => {
                        if (!blob) {
                            resolve(null);
                            return;
                        }
                        try {
                            const arrayBuffer = await blob.arrayBuffer();
                            const uint8Array = new Uint8Array(arrayBuffer);
                            resolve(uint8Array);
                        } catch (error) {
                            console.error('Failed to convert blob:', error);
                            resolve(null);
                        }
                    }, 'image/jpeg', 0.9);
                });
            },
            async extractWebcamBytes() {
                const video = document.getElementById('webcamVideo');
                const canvas = document.getElementById('extractionCanvas');
                if (!video || !canvas || !video.videoWidth) return null;
                const ctx = canvas.getContext('2d');
                const targetWidth = 832;
                const targetHeight = 480;
                canvas.width = targetWidth;
                canvas.height = targetHeight;
                const videoWidth = video.videoWidth;
                const videoHeight = video.videoHeight;
                const sourceAspect = videoWidth / videoHeight;
                const targetAspect = targetWidth / targetHeight;
                let sx, sy, sWidth, sHeight;
                if (sourceAspect > targetAspect) {
                    sHeight = videoHeight;
                    sWidth = videoHeight * targetAspect;
                    sx = (videoWidth - sWidth) / 2;
                    sy = 0;
                } else {
                    sWidth = videoWidth;
                    sHeight = videoWidth / targetAspect;
                    sx = 0;
                    sy = (videoHeight - sHeight) / 2;
                }
                ctx.drawImage(video, sx, sy, sWidth, sHeight, 0, 0, targetWidth, targetHeight);
                return new Promise((resolve) => {
                    canvas.toBlob(async (blob) => {
                        if (!blob) {
                            resolve(null);
                            return;
                        }
                        try {
                            const arrayBuffer = await blob.arrayBuffer();
                            const uint8Array = new Uint8Array(arrayBuffer);
                            resolve(uint8Array);
                        } catch (error) {
                            console.error('Failed to convert blob:', error);
                            resolve(null);
                        }
                    }, 'image/jpeg', 0.9);
                });
            },
            startFrameExtraction() {
                const inputFps = parseInt(document.getElementById('inputFps').value);
                const intervalMs = Math.floor(1000 / inputFps);
                const video = document.getElementById('hiddenVideo');
                if (this.mode === 'video') {
                    video.play();
                }
                this.frameExtractionInterval = setInterval(async () => {
                    if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
                        this.stopFrameExtraction();
                        return;
                    }
                    let frameBytes = null;
                    if (this.mode === 'video') {
                        if (video.ended) {
                            video.currentTime = 0;
                            video.play();
                        }
                        frameBytes = await this.extractVideoFrameBytes();
                    } else if (this.mode === 'webcam') {
                        frameBytes = await this.extractWebcamBytes();
                    }
                    if (!frameBytes || frameBytes.length === 0) {
                        console.warn('Empty frame, skipping');
                        return;
                    }
                    const strengthValue = parseFloat(document.getElementById('strength').value);
                    const message = {
                        image: frameBytes,
                        strength: strengthValue,
                        timestamp: Date.now()
                    };
                    // For webcam mode, include prompt and num_blocks
                    if (this.mode === 'webcam') {
                        message.prompt = document.getElementById('prompt').value;
                        message.num_blocks = parseInt(document.getElementById('numBlocks').value);
                    }
                    const encoded = createMsgpackEncoder(message);
                    this.ws.send(encoded);
                }, intervalMs);
            },
            stopFrameExtraction() {
                if (this.frameExtractionInterval) {
                    clearInterval(this.frameExtractionInterval);
                    this.frameExtractionInterval = null;
                }
                const video = document.getElementById('hiddenVideo');
                video.pause();
                video.currentTime = 0;
            },
            queuePromptUpdate() {
                this.pendingPromptUpdate = {
                    prompt: document.getElementById('prompt').value,
                    num_blocks: parseInt(document.getElementById('numBlocks').value)
                };
                if (this.promptUpdateTimer) {
                    clearTimeout(this.promptUpdateTimer);
                }
                this.promptUpdateTimer = setTimeout(() => {
                    if (this.pendingPromptUpdate && this.ws?.readyState === WebSocket.OPEN) {
                        const encoded = createMsgpackEncoder(this.pendingPromptUpdate);
                        this.ws.send(encoded);
                        this.maxFrames = (12 * this.pendingPromptUpdate.num_blocks) - 6;
                        document.getElementById('maxFrames').textContent = this.maxFrames;
                        this.pendingPromptUpdate = null;
                    }
                }, 2000);
            },
            startRecording() {
                const canvas = document.getElementById('outputCanvas');
                const fps = parseInt(document.getElementById('playbackFps').value);
                const stream = canvas.captureStream(fps);
                this.recordedChunks = [];
                this.mediaRecorder = new MediaRecorder(stream, {
                    mimeType: MediaRecorder.isTypeSupported('video/webm;codecs=vp9')
                        ? 'video/webm;codecs=vp9'
                        : 'video/webm'
                });
                this.mediaRecorder.ondataavailable = (event) => {
                    if (event.data && event.data.size > 0) {
                        this.recordedChunks.push(event.data);
                    }
                };
                this.mediaRecorder.start();
            },
            stopRecording() {
                if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
                    this.mediaRecorder.stop();
                }
            },
            startIdleTimeout() {
                // Clear existing timeout
                if (this.idleTimeout) {
                    clearTimeout(this.idleTimeout);
                }
                // Set new timeout - disconnect after 15 seconds of no frames
                this.idleTimeout = setTimeout(() => {
                    if (this.isGenerating) {
                        this.showError('The FAL API server is too busy, try again soon!');
                        this.disconnect();
                    }
                }, 15000);
            },
            clearIdleTimeout() {
                if (this.idleTimeout) {
                    clearTimeout(this.idleTimeout);
                    this.idleTimeout = null;
                }
            },
            disconnect() {
                this.clearIdleTimeout();
                if (this.ws) {
                    this.ws.close();
                    this.ws = null;
                }
                if (this.playbackInterval) {
                    clearInterval(this.playbackInterval);
                    this.playbackInterval = null;
                }
                if (this.promptUpdateTimer) {
                    clearTimeout(this.promptUpdateTimer);
                    this.promptUpdateTimer = null;
                }
                this.pendingPromptUpdate = null;
                this.stopFrameExtraction();
                this.stopRecording();
                this.isGenerating = false;
                // Hide spinner, show placeholder if no frames
                document.getElementById('spinner').classList.add('hidden');
                if (this.frameCount === 0) {
                    document.getElementById('placeholder').style.display = 'flex';
                }
                // Update status pill - only show "Disconnected" if manually stopped or error
                if (!this.generationFinished) {
                    const statusPill = document.getElementById('statusPill');
                    if (statusPill) {
                        statusPill.className = 'status-pill status-disconnected';
                        statusPill.textContent = 'Disconnected';
                    }
                }
                this.updateUI();
            },
            async replayVideo() {
                if (this.allFrames.length === 0) {
                    this.showError('No frames to replay');
                    return;
                }
                // Stop current playback
                this.stopPlaybackLoop();
                // Recreate frameBuffer from stored frames
                this.frameBuffer = [];
                for (const imageData of this.allFrames) {
                    const blob = new Blob([imageData], { type: 'image/jpeg' });
                    const bitmap = await createImageBitmap(blob);
                    this.frameBuffer.push(bitmap);
                }
                // Restart playback
                this.startPlaybackLoop();
                this.showInfo('Replaying video');
            },
            downloadVideo() {
                if (this.recordedChunks.length === 0) {
                    this.showError('No video data to download');
                    return;
                }
                const blob = new Blob(this.recordedChunks, { type: 'video/webm' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `generated-video-${Date.now()}.webm`;
                a.click();
                URL.revokeObjectURL(url);
                this.showInfo('Video downloaded successfully');
            },
            updateUI() {
                const btn = document.getElementById('startStopBtn');
                btn.textContent = this.isGenerating ? 'Stop Generation' : 'Start Generation';
                btn.className = this.isGenerating ? 'btn btn-danger' : 'btn btn-primary';
                // Update mode buttons
                const textBtn = document.getElementById('textModeBtn');
                const videoBtn = document.getElementById('videoModeBtn');
                const webcamBtn = document.getElementById('webcamModeBtn');
                if (textBtn && videoBtn && webcamBtn) {
                    textBtn.disabled = this.isGenerating;
                    videoBtn.disabled = this.isGenerating;
                    webcamBtn.disabled = this.isGenerating;
                }
            },
            showError(message) {
                const box = document.getElementById('errorBox');
                box.textContent = message;
                box.classList.remove('hidden');
                setTimeout(() => box.classList.add('hidden'), 5000);
            },
            showInfo(message) {
                const box = document.getElementById('infoBox');
                box.textContent = message;
                box.classList.remove('hidden');
                setTimeout(() => box.classList.add('hidden'), 3000);
            }
        };
        // Initialize on page load
        document.addEventListener('DOMContentLoaded', () => {
            auth.init();
        });
    </script>
</body>
</html>
 | 
